Decompiled source of LimitedTeleporterCharges v1.0.3

LCLimitedTeleporterCharges.dll

Decompiled 2 months ago
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using LCLimitedTeleporterCharges.Patches;
using LC_API.GameInterfaceAPI.Events.EventArgs.Player;
using LC_API.GameInterfaceAPI.Events.Handlers;
using LC_API.GameInterfaceAPI.Features;
using LC_API.Networking;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("LCTeleporterTweaks")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LCTeleporterTweaks")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("c336465b-0fff-4218-bc0d-cb73ef3a99fa")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace LCLimitedTeleporterCharges
{
	internal class ConfigurationController
	{
		private ConfigEntry<int> TeleportMaxChargesCfg;

		internal int TeleportMaxCharges
		{
			get
			{
				if (TeleportMaxChargesCfg.Value < 0)
				{
					return (int)((ConfigEntryBase)TeleportMaxChargesCfg).DefaultValue;
				}
				return TeleportMaxChargesCfg.Value;
			}
			set
			{
				TeleportMaxChargesCfg.Value = value;
			}
		}

		public ConfigurationController(ConfigFile Config)
		{
			TeleportMaxChargesCfg = Config.Bind<int>("General", "Teleporter max charges", 2, "The maximum number of times the teleporter can be used in one round. Note that everyone's maximum number of charges will depend on the host's setting of this value.");
		}
	}
	[BepInPlugin("Tesert.LCLimitedTeleporterCharges", "Limited Teleporter Charges", "1.0.2")]
	public class ModBase : BaseUnityPlugin
	{
		public class Wrapper<T>
		{
			public T Value { get; set; }
		}

		private const string modGUID = "Tesert.LCLimitedTeleporterCharges";

		private const string modName = "Limited Teleporter Charges";

		private const string modVersion = "1.0.2";

		private readonly Harmony harmony = new Harmony("Tesert.LCLimitedTeleporterCharges");

		internal static ModBase Instance;

		internal ManualLogSource mls = Logger.CreateLogSource("Tesert.LCLimitedTeleporterCharges");

		internal ConfigurationController ConfigManager;

		internal static InteractTrigger teleportButton;

		internal static string baseBeamUpString;

		private static int teleportMaxCharges;

		private static int teleporterCharges;

		internal static int TeleporterCharges
		{
			get
			{
				return teleporterCharges;
			}
			set
			{
				Instance.mls.LogInfo((object)("Changing teleporter charges from: " + teleporterCharges + " to: " + value));
				teleporterCharges = value;
				if ((Object)(object)teleportButton != (Object)null)
				{
					if (teleporterCharges == 1)
					{
						teleportButton.hoverTip = baseBeamUpString + "\n[" + TeleporterCharges + " charge left]";
					}
					else
					{
						teleportButton.hoverTip = baseBeamUpString + "\n[" + TeleporterCharges + " charges left]";
					}
				}
			}
		}

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			mls = Logger.CreateLogSource("Tesert.LCLimitedTeleporterCharges");
			mls.LogInfo((object)"LimitedTeleporterCharges loaded!");
			ConfigManager = new ConfigurationController(((BaseUnityPlugin)this).Config);
			harmony.PatchAll(typeof(ModBase));
			harmony.PatchAll(typeof(ShipTeleporterPatch));
			harmony.PatchAll(typeof(StartOfRoundPatch));
			Network.RegisterAll();
			teleportMaxCharges = ConfigManager.TeleportMaxCharges;
			teleporterCharges = teleportMaxCharges;
			Player.Joined += PlayerJoined;
		}

		internal static void ResetTeleporterCharges()
		{
			Instance.mls.LogInfo((object)("Resetting teleporter charges to: " + teleportMaxCharges));
			TeleporterCharges = teleportMaxCharges;
		}

		[NetworkMessage("teleporterActivatedMessage", false)]
		private static void TeleporterActivatedHandler(ulong sender)
		{
			Instance.mls.LogInfo((object)("Teleport use message received from " + sender + "!"));
			TeleporterCharges--;
		}

		internal static void TeleporterActivated()
		{
			Instance.mls.LogInfo((object)"Teleport use message sent!");
			Network.Broadcast("teleporterActivatedMessage");
		}

		[NetworkMessage("hostSharingTeleporterMaximumChargesMsg", false)]
		private static void HostSharingTeleporterMaximumChargesHandler(ulong sender, Wrapper<string> message)
		{
			if (!Player.LocalPlayer.IsHost)
			{
				string[] array = message.Value.Split(new char[1] { ',' });
				Instance.mls.LogInfo((object)("Client received max teleporter charges + [" + array[0] + "] and current teleporter charges + [" + array[1] + "] from host, updating charges to sync with host."));
				teleportMaxCharges = int.Parse(array[0]);
				TeleporterCharges = int.Parse(array[1]);
			}
		}

		internal bool isHost()
		{
			Instance.mls.LogInfo((object)"Attempting to check if player is host.");
			return Player.LocalPlayer.IsHost;
		}

		private void PlayerJoined(JoinedEventArgs e)
		{
			if (isHost())
			{
				Instance.mls.LogInfo((object)"A player joined, sharing teleporter charges data with them.");
				Network.Broadcast<Wrapper<string>>("hostSharingTeleporterMaximumChargesMsg", new Wrapper<string>
				{
					Value = teleportMaxCharges + "," + TeleporterCharges
				});
			}
		}
	}
}
namespace LCLimitedTeleporterCharges.Patches
{
	[HarmonyPatch(typeof(ShipTeleporter))]
	internal class ShipTeleporterPatch
	{
		[HarmonyPatch("Awake")]
		[HarmonyPrefix]
		private static void AwakePatch(ref bool ___isInverseTeleporter, ref InteractTrigger ___buttonTrigger)
		{
			if (!___isInverseTeleporter)
			{
				ModBase.teleportButton = ___buttonTrigger;
				ModBase.baseBeamUpString = ___buttonTrigger.hoverTip;
				if (ModBase.TeleporterCharges == 1)
				{
					___buttonTrigger.hoverTip = ModBase.baseBeamUpString + "\n[" + ModBase.TeleporterCharges + " charge left]";
				}
				else
				{
					___buttonTrigger.hoverTip = ModBase.baseBeamUpString + "\n[" + ModBase.TeleporterCharges + " charges left]";
				}
			}
		}

		[HarmonyPatch("PressTeleportButtonOnLocalClient")]
		[HarmonyPrefix]
		private static bool PressTeleportButtonOnLocalClientPatch(ref bool ___isInverseTeleporter)
		{
			if (!___isInverseTeleporter)
			{
				if (ModBase.TeleporterCharges <= 0)
				{
					ModBase.Instance.mls.LogWarning((object)"No teleporter charges remaining yet button is interactable. Should not reach this point!");
					return false;
				}
				ModBase.TeleporterCharges--;
				ModBase.TeleporterActivated();
			}
			return true;
		}

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void UpdatePostPatch(ref bool ___isInverseTeleporter, ref InteractTrigger ___buttonTrigger)
		{
			if (!___isInverseTeleporter && ModBase.TeleporterCharges <= 0)
			{
				___buttonTrigger.interactable = false;
				___buttonTrigger.disabledHoverTip = "[No charges left]";
			}
			else if (___isInverseTeleporter)
			{
				___buttonTrigger.hoverTip = ModBase.baseBeamUpString;
			}
		}
	}
	[HarmonyPatch(typeof(StartOfRound))]
	internal class StartOfRoundPatch
	{
		[HarmonyPatch("StartGame")]
		[HarmonyPostfix]
		private static void StartGamePatch()
		{
			ModBase.ResetTeleporterCharges();
		}

		[HarmonyPatch("ShipHasLeft")]
		[HarmonyPostfix]
		private static void ShipHasLeftPatch()
		{
			ModBase.ResetTeleporterCharges();
		}
	}
}