YATM.dll

Decompiled 11 months ago
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using TerminalApi;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("YATM")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Yet Another Terminal Mod for Lethal Company")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("YATM")]
[assembly: AssemblyTitle("YATM")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace YATM
{
	public class Config
	{
		public static ConfigEntry<bool> configEnableTeleport;

		public static ConfigEntry<bool> configEnableTeleportByName;

		public static ConfigEntry<bool> configEnableInverse;

		public static ConfigEntry<bool> configEnableStats;

		public Config(ConfigFile cfg)
		{
			configEnableTeleport = cfg.Bind<bool>("General.Toggles", "EnableTeleport", true, "Enable Teleport command");
			configEnableTeleportByName = cfg.Bind<bool>("General.Toggles", "EnableTeleportByName", false, "Enable ability to teleport specific player by their name");
			configEnableInverse = cfg.Bind<bool>("General.Toggles", "EnableInverse", true, "Enable ITP command");
			configEnableStats = cfg.Bind<bool>("General.Toggles", "EnableStats", true, "Enable Stats command");
		}
	}
	[BepInPlugin("com.leefsnake.yatm", "YATM", "1.0.1")]
	public class Plugin : BaseUnityPlugin
	{
		private const string pluginGUID = "com.leefsnake.yatm";

		private const string pluginName = "YATM";

		private const string pluginVersion = "1.0.1";

		public static ManualLogSource mls = Logger.CreateLogSource("com.leefsnake.yatm");

		public static Config YatmCfg { get; internal set; }

		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin YATM is loaded!");
			YatmCfg = new Config(((BaseUnityPlugin)this).Config);
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
			if (Config.configEnableTeleport.Value)
			{
				TerminalNode val = TerminalApi.CreateTerminalNode("[Teleport]", true, "teleport");
				TerminalKeyword val2 = TerminalApi.CreateTerminalKeyword("teleport", true, val);
				TerminalKeyword obj = TerminalApi.CreateTerminalKeyword("tp", true, val);
				TerminalApi.AddTerminalKeyword(val2);
				TerminalApi.AddTerminalKeyword(obj);
			}
			if (Config.configEnableInverse.Value)
			{
				TerminalNode val3 = TerminalApi.CreateTerminalNode("[ITP]", true, "inverse");
				TerminalApi.AddTerminalKeyword(TerminalApi.CreateTerminalKeyword("itp", true, val3));
			}
			if (Config.configEnableStats.Value)
			{
				TerminalNode val4 = TerminalApi.CreateTerminalNode("[ShowStats]", true, "stats");
				TerminalKeyword val5 = TerminalApi.CreateTerminalKeyword("show", true, (TerminalNode)null);
				TerminalKeyword val6 = TerminalApi.CreateTerminalKeyword("stats", false, val4);
				val5 = (val6.defaultVerb = TerminalExtenstionMethods.AddCompatibleNoun(val5, val6, val4));
				TerminalApi.AddTerminalKeyword(val6);
				TerminalApi.AddTerminalKeyword(val5);
			}
		}
	}
	[HarmonyPatch(typeof(Terminal), "RunTerminalEvents")]
	internal class EventsPatch
	{
		public static string BuildStats()
		{
			EndOfGameStats gameStats = StartOfRound.Instance.gameStats;
			if (gameStats.daysSpent == 0 && gameStats.allStepsTaken == 0)
			{
				return "No stats to display yet\n";
			}
			return "Days passed: " + gameStats.daysSpent + "\nDeaths died: " + gameStats.deaths + "\nTotal scrap value collected: " + gameStats.scrapValueCollected + "\nSteps Taken: " + gameStats.allStepsTaken + "\n";
		}

		private static IEnumerator PostfixCoroutine(Terminal __instance, TerminalNode node)
		{
			if (!string.IsNullOrWhiteSpace(node.terminalEvent) && !(node.terminalEvent != "stats"))
			{
				node.displayText = BuildStats();
			}
			yield break;
		}

		private static void Postfix(Terminal __instance, TerminalNode node)
		{
			((MonoBehaviour)__instance).StartCoroutine(PostfixCoroutine(__instance, node));
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "YATM";

		public const string PLUGIN_NAME = "YATM";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace YATM.Teleport
{
	[HarmonyPatch]
	internal class CameraPatch
	{
		private static bool first = true;

		private static int index = 0;

		[HarmonyPatch(typeof(ManualCameraRenderer), "updateMapTarget")]
		[HarmonyPostfix]
		private static void PostfixMap(ManualCameraRenderer __instance, int setRadarTargetIndex, bool calledFromRPC)
		{
			if (EventsPatch.sDoTp && calledFromRPC && setRadarTargetIndex == EventsPatch.result && !((NetworkBehaviour)__instance).NetworkManager.IsHost && !((NetworkBehaviour)__instance).NetworkManager.IsServer)
			{
				EventsPatch.TrgetShipTeleporter.PressTeleportButtonOnLocalClient();
				EventsPatch.sDoTp = false;
			}
			index = setRadarTargetIndex;
		}

		[HarmonyPatch(typeof(ManualCameraRenderer), "SwitchRadarTargetServerRpc")]
		[HarmonyPostfix]
		private static void PostfixSwitchSRPC(ManualCameraRenderer __instance, int targetIndex)
		{
			if (EventsPatch.sDoTp && targetIndex == EventsPatch.result && !first && (((NetworkBehaviour)__instance).NetworkManager.IsHost || ((NetworkBehaviour)__instance).NetworkManager.IsServer))
			{
				EventsPatch.TrgetShipTeleporter.PressTeleportButtonOnLocalClient();
				EventsPatch.sDoTp = false;
			}
			first = !first;
		}
	}
	[HarmonyPatch(typeof(Terminal), "RunTerminalEvents")]
	internal class EventsPatch
	{
		public static bool sDoSwitch;

		public static bool sDoTp;

		public static ShipTeleporter TrgetShipTeleporter;

		public static int result;

		public static int CheckPlayerName(string name, TerminalNode node)
		{
			List<string> list = new List<string>();
			for (int i = 0; i < StartOfRound.Instance.mapScreen.radarTargets.Count; i++)
			{
				list.Add(StartOfRound.Instance.mapScreen.radarTargets[i].name);
			}
			for (int j = 0; j < list.Count; j++)
			{
				if (list[j].ToLower() == name.ToLower())
				{
					node.displayText = "Teleporting " + list[j] + "...\n\n";
					return j;
				}
			}
			node.displayText = "Player " + name + " not found\n";
			return -1;
		}

		private static IEnumerator PostfixCoroutine(Terminal __instance, TerminalNode node)
		{
			result = -1;
			string text = null;
			if (string.IsNullOrWhiteSpace(node.terminalEvent) || node.terminalEvent != "teleport")
			{
				yield break;
			}
			string[] array = node.displayText.Split("\n");
			if (array.Length == 3)
			{
				text = array[1];
				result = CheckPlayerName(text, node);
			}
			ShipTeleporter[] array2 = Object.FindObjectsOfType<ShipTeleporter>();
			if (array2.Length != 0 && !array2[0].isInverseTeleporter)
			{
				TrgetShipTeleporter = array2[0];
			}
			else
			{
				if (array2.Length <= 1)
				{
					node.displayText = "Teleporter unavailble.\n";
					if (result > -1 && text != null)
					{
						TerminalApi.DeleteKeyword(text);
						TerminalApi.UpdateKeyword(TerminalApi.GetKeyword("teleport"));
						TerminalApi.UpdateKeyword(TerminalApi.GetKeyword("tp"));
					}
					yield break;
				}
				TrgetShipTeleporter = array2[1];
			}
			if (!TrgetShipTeleporter.buttonTrigger.interactable)
			{
				node.displayText = "Teleporter on cooldown.\n";
				if (result > -1 && text != null)
				{
					TerminalApi.DeleteKeyword(text);
					TerminalApi.UpdateKeyword(TerminalApi.GetKeyword("teleport"));
					TerminalApi.UpdateKeyword(TerminalApi.GetKeyword("tp"));
				}
				yield break;
			}
			if (array.Length == 3)
			{
				if (result > -1)
				{
					sDoSwitch = true;
					TerminalApi.DeleteKeyword(text);
					TerminalApi.UpdateKeyword(TerminalApi.GetKeyword("teleport"));
					TerminalApi.UpdateKeyword(TerminalApi.GetKeyword("tp"));
				}
			}
			else
			{
				node.displayText = "Teleporting player...";
			}
			sDoTp = true;
		}

		private static void Postfix(Terminal __instance, TerminalNode node)
		{
			((MonoBehaviour)__instance).StartCoroutine(PostfixCoroutine(__instance, node));
			if (sDoSwitch && sDoTp && Config.configEnableTeleportByName.Value)
			{
				sDoSwitch = false;
				StartOfRound.Instance.mapScreen.SwitchRadarTargetAndSync(result);
			}
			else if (sDoTp)
			{
				sDoTp = false;
				TrgetShipTeleporter.PressTeleportButtonOnLocalClient();
			}
		}
	}
	[HarmonyPatch(typeof(Terminal), "ParseWord")]
	internal class ParseWordPatch
	{
		private static void Postfix(ref TerminalKeyword __result, string playerWord, int specificityRequired = 2)
		{
			if (!Config.configEnableTeleportByName.Value || !((Object)(object)__result == (Object)null))
			{
				return;
			}
			for (int i = 0; i < StartOfRound.Instance.mapScreen.radarTargets.Count; i++)
			{
				string text = StartOfRound.Instance.mapScreen.radarTargets[i].name.ToLower();
				if (text.StartsWith(playerWord.ToLower()))
				{
					string text2 = text;
					TerminalNode specialKeywordResult = TerminalApi.GetKeyword("teleport").specialKeywordResult;
					specialKeywordResult.displayText = "[Teleport]\n" + text2 + "\n";
					TerminalKeyword val = TerminalApi.GetKeyword(text);
					if (!Object.op_Implicit((Object)(object)val))
					{
						val = TerminalApi.CreateTerminalKeyword(text, false, specialKeywordResult);
						TerminalKeyword keyword = TerminalApi.GetKeyword("teleport");
						TerminalKeyword keyword2 = TerminalApi.GetKeyword("tp");
						TerminalExtenstionMethods.AddCompatibleNoun(keyword, val, specialKeywordResult);
						TerminalExtenstionMethods.AddCompatibleNoun(keyword2, val, specialKeywordResult);
						TerminalApi.AddTerminalKeyword(val);
						TerminalApi.UpdateKeyword(keyword);
						TerminalApi.UpdateKeyword(keyword2);
					}
					__result = val;
				}
			}
		}
	}
}
namespace YATM.Inverse
{
	[HarmonyPatch(typeof(Terminal), "RunTerminalEvents")]
	internal class EventsPatch
	{
		private static IEnumerator PostfixCoroutine(Terminal __instance, TerminalNode node)
		{
			if (string.IsNullOrWhiteSpace(node.terminalEvent) || node.terminalEvent != "inverse")
			{
				yield break;
			}
			ShipTeleporter[] array = Object.FindObjectsOfType<ShipTeleporter>();
			ShipTeleporter val;
			if (array.Length != 0 && array[0].isInverseTeleporter)
			{
				val = array[0];
			}
			else
			{
				if (array.Length <= 1)
				{
					node.displayText = "Inverse Teleporter unavailble.\n";
					yield break;
				}
				val = array[1];
			}
			if (StartOfRound.Instance.inShipPhase)
			{
				node.displayText = "Cannot inverse teleport when not on a planet.\n";
				yield break;
			}
			if (!val.buttonTrigger.interactable)
			{
				node.displayText = "Inverse Teleporter on cooldown.\n";
				yield break;
			}
			node.displayText = "Inverse Teleporter activated.\nHave a nice trip and don't get stuck!\n";
			val.PressTeleportButtonOnLocalClient();
		}

		private static void Postfix(Terminal __instance, TerminalNode node)
		{
			((MonoBehaviour)__instance).StartCoroutine(PostfixCoroutine(__instance, node));
		}
	}
}