Decompiled source of lethalextendedterminal v1.2.1

LethalTerminalExtender.dll

Decompiled a year ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using HarmonyLib;
using JetBrains.Annotations;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.6", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("LethalTerminalExtender")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("My first plugin")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0+b193c9c48840b420c2907190433c67f83cfd430d")]
[assembly: AssemblyProduct("LethalTerminalExtender")]
[assembly: AssemblyTitle("LethalTerminalExtender")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace LethalTerminalExtender
{
	[BepInPlugin("us.domkalan.lethalextendedterminal", "Lethal Extended Terminal", "1.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string GUID = "us.domkalan.lethalextendedterminal";

		public const string NAME = "Lethal Extended Terminal";

		public const string VERSION = "1.1.0";

		public static Plugin instance;

		private void Awake()
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			instance = this;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin LethalTerminalExtender is loaded!");
			Harmony val = new Harmony("us.domkalan.lethalextendedterminal");
			val.PatchAll(Assembly.GetExecutingAssembly());
		}

		public void Log(LogType type, string contents)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)contents);
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "LethalTerminalExtender";

		public const string PLUGIN_NAME = "LethalTerminalExtender";

		public const string PLUGIN_VERSION = "1.1.0";
	}
}
namespace LethalTerminalExtender.Patches
{
	public struct TerminalCustomCommand
	{
		public TerminalNode node;

		public TerminalKeyword keyword;

		[CanBeNull]
		public Action<Terminal, TerminalNode> callback;
	}
	public static class TerminalExtenderUtils
	{
		private static List<TerminalCustomCommand> commands = new List<TerminalCustomCommand>();

		private static Terminal terminalInstance = null;

		private static Action<string> currentStapleCommand = null;

		private static bool stapleNoticeShown = false;

		private static TerminalNode blankNodeInstance;

		private static TerminalNode customTextNodeInstance;

		public static bool terminalConnected
		{
			get
			{
				return (Object)(object)terminalInstance != (Object)null;
			}
			set
			{
				throw new Exception("Can not set this!");
			}
		}

		public static bool currentStapleCommandPresent => currentStapleCommand != null;

		public static void addCustomCommand(TerminalCustomCommand custComm)
		{
			commands.Add(custComm);
		}

		public static TerminalCustomCommand addQuickCommand(string keyword, string displayText, bool clearsText = false, Action<Terminal, TerminalNode> callback = null)
		{
			TerminalNode val = ScriptableObject.CreateInstance<TerminalNode>();
			((Object)val).name = keyword + "Node";
			val.displayText = displayText;
			val.clearPreviousText = clearsText;
			val.terminalEvent = keyword + "Event";
			TerminalKeyword val2 = ScriptableObject.CreateInstance<TerminalKeyword>();
			((Object)val2).name = keyword + "Keyword";
			val2.word = keyword;
			val2.specialKeywordResult = val;
			TerminalCustomCommand terminalCustomCommand = default(TerminalCustomCommand);
			terminalCustomCommand.keyword = val2;
			terminalCustomCommand.node = val;
			terminalCustomCommand.callback = callback;
			TerminalCustomCommand terminalCustomCommand2 = terminalCustomCommand;
			addCustomCommand(terminalCustomCommand2);
			return terminalCustomCommand2;
		}

		public static void setOnSubmit(Action<string> action)
		{
			if (!stapleNoticeShown)
			{
				stapleNoticeShown = true;
				HUDManager.Instance.DisplayTip("TERMINAL EXTENDER", "This command is from a mod, it may change the default functionality of the terminal.", false, false, "LC_Tip1");
			}
			currentStapleCommand = action;
		}

		public static void releaseOnSubmit()
		{
			currentStapleCommand = null;
		}

		public static void emitOnSubmit(string text)
		{
			currentStapleCommand(text);
		}

		public static void setInputState(bool inputToggle)
		{
			if (inputToggle)
			{
				((Selectable)terminalInstance.screenText).interactable = true;
				terminalInstance.screenText.ActivateInputField();
				((Selectable)terminalInstance.screenText).Select();
			}
			if (!inputToggle)
			{
				((Selectable)terminalInstance.screenText).interactable = false;
				terminalInstance.screenText.DeactivateInputField(false);
			}
		}

		public static void playAudioClip(int clipIndex)
		{
			terminalInstance.PlayTerminalAudioServerRpc(clipIndex);
		}

		public static void cleanTerminalScreen()
		{
			terminalInstance.LoadNewNode(blankNodeInstance);
		}

		public static void writeToTerminal(string text, bool cleanScreen = false)
		{
			Plugin.instance.Log((LogType)3, text + "\n\n is " + text.Length);
			customTextNodeInstance.clearPreviousText = cleanScreen;
			customTextNodeInstance.displayText = "\n" + text;
			terminalInstance.LoadNewNode(customTextNodeInstance);
		}

		public static void displayNodeOnTerminal(TerminalNode node)
		{
			terminalInstance.LoadNewNode(node);
		}

		public static bool handleNodeEvents(TerminalNode node)
		{
			TerminalCustomCommand[] array = commands.ToArray();
			TerminalCustomCommand[] array2 = array;
			for (int i = 0; i < array2.Length; i++)
			{
				TerminalCustomCommand terminalCustomCommand = array2[i];
				if ((Object)(object)terminalCustomCommand.node == (Object)(object)node)
				{
					if (terminalCustomCommand.callback != null)
					{
						terminalCustomCommand.callback(terminalInstance, node);
						return false;
					}
					break;
				}
			}
			return true;
		}

		public static TerminalKeyword[] getKeywords()
		{
			TerminalKeyword[] array = (TerminalKeyword[])(object)new TerminalKeyword[commands.Count];
			for (int i = 0; i < commands.Count; i++)
			{
				array[i] = commands[i].keyword;
			}
			return array;
		}

		public static TerminalKeyword handleKeywordCommand(string typedKeyword)
		{
			TerminalKeyword[] keywords = getKeywords();
			foreach (TerminalKeyword val in keywords)
			{
				if (val.word == typedKeyword)
				{
					return val;
				}
			}
			return null;
		}

		public static bool handleTerminalSubmit()
		{
			if (currentStapleCommandPresent)
			{
				Plugin.instance.Log((LogType)3, "Staple mode is active!");
				string text = terminalInstance.RemovePunctuation(terminalInstance.screenText.text.Substring(terminalInstance.screenText.text.Length - terminalInstance.textAdded));
				Plugin.instance.Log((LogType)3, "Request to parse command in staple mode: " + text);
				emitOnSubmit(text);
				return false;
			}
			return true;
		}

		public static void handleTerminalQuit()
		{
			if (currentStapleCommandPresent)
			{
				releaseOnSubmit();
			}
		}

		public static void connectToTerminal(Terminal terminal)
		{
			if ((Object)(object)terminalInstance != (Object)null)
			{
				return;
			}
			terminalInstance = terminal;
			TerminalNode val = ScriptableObject.CreateInstance<TerminalNode>();
			((Object)val).name = "blankNode";
			val.displayText = "";
			val.clearPreviousText = true;
			blankNodeInstance = val;
			terminalInstance.terminalNodes.terminalNodes.Add(val);
			TerminalNode val2 = ScriptableObject.CreateInstance<TerminalNode>();
			((Object)val2).name = "textNode";
			val2.displayText = "Hello world!";
			val2.clearPreviousText = false;
			customTextNodeInstance = val2;
			terminalInstance.terminalNodes.terminalNodes.Add(val2);
			foreach (TerminalCustomCommand command in commands)
			{
				if (!terminalInstance.terminalNodes.terminalNodes.Contains(command.node))
				{
					terminalInstance.terminalNodes.terminalNodes.Add(command.node);
				}
				if (terminalInstance.terminalNodes.allKeywords.Contains(command.keyword))
				{
					CollectionExtensions.AddToArray<TerminalKeyword>(terminalInstance.terminalNodes.allKeywords, command.keyword);
				}
			}
		}
	}
	[HarmonyPatch(typeof(Terminal), "RunTerminalEvents")]
	public class TerminalExtender_HandleNodeEvent
	{
		public static bool Prefix(Terminal __instance, TerminalNode node)
		{
			if (!TerminalExtenderUtils.terminalConnected)
			{
				TerminalExtenderUtils.connectToTerminal(__instance);
			}
			return TerminalExtenderUtils.handleNodeEvents(node);
		}
	}
	[HarmonyPatch(typeof(Terminal), "CheckForExactSentences")]
	public class TerminalExtender_KeywordCheck
	{
		public static void Postfix(Terminal __instance, string playerWord, ref TerminalKeyword __result)
		{
			if (!TerminalExtenderUtils.terminalConnected)
			{
				TerminalExtenderUtils.connectToTerminal(__instance);
			}
			if ((Object)(object)__result == (Object)null)
			{
				Plugin.instance.Log((LogType)3, "request to parse command " + playerWord);
				__result = TerminalExtenderUtils.handleKeywordCommand(playerWord);
			}
		}
	}
	[HarmonyPatch(typeof(Terminal), "OnSubmit")]
	public class TerminalExtender_OnSubmit
	{
		public static bool Prefix(Terminal __instance)
		{
			if (!TerminalExtenderUtils.terminalConnected)
			{
				TerminalExtenderUtils.connectToTerminal(__instance);
			}
			return TerminalExtenderUtils.handleTerminalSubmit();
		}
	}
	[HarmonyPatch(typeof(Terminal), "QuitTerminal")]
	public class TerminalExtender_QuitTerminal
	{
		public static void Prefix(Terminal __instance)
		{
			if (!TerminalExtenderUtils.terminalConnected)
			{
				TerminalExtenderUtils.connectToTerminal(__instance);
			}
			TerminalExtenderUtils.handleTerminalQuit();
		}
	}
}