Decompiled source of SimpleCommandAPI v1.0.2

BepInEx/plugins/SimpleCommand.API.dll

Decompiled 10 months 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 System.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using SimpleCommand.API.Classes;
using TMPro;
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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("SimpleCommand.API")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("API for simple command creation.")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2")]
[assembly: AssemblyProduct("SimpleCommand.API")]
[assembly: AssemblyTitle("SimpleCommand.API")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.2.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 SimpleCommand.API
{
	[BepInPlugin("SimpleCommand.API", "SimpleCommand.API", "1.0.2")]
	[BepInProcess("Lethal Company.exe")]
	public class Plugin : BaseUnityPlugin
	{
		public static ConfigEntry<bool> ConfigSimpleCommandSortCommands;

		public static ConfigEntry<bool> ConfigSimpleCommandLogging;

		public static ManualLogSource Log;

		private readonly Harmony _harmony = new Harmony("SimpleCommand.API");

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			ConfigSimpleCommandSortCommands = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "SortCommandsAlphabetically", false, "By default, commands are sorted by plugin. Set the value to 'true' if you want them to be sorted alphabetically.");
			ConfigSimpleCommandLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("Developer", "EnableDebugLogging", false, "Enables logging of every SimpleCommand.API action.");
			if (ConfigSimpleCommandLogging.Value)
			{
				Log.LogDebug((object)"Debug Logging enabled. Logging..");
				if (ConfigSimpleCommandSortCommands.Value)
				{
					Log.LogDebug((object)"List of commands is now sorted alphabetically.");
				}
			}
			_harmony.PatchAll();
			PrepareSetup();
			Log.LogInfo((object)"Plugin SimpleCommand.API is loaded!");
		}

		private void PrepareSetup()
		{
			SimpleCommandModule simpleCommandModule = new SimpleCommandModule();
			simpleCommandModule.DisplayName = "modcommands";
			simpleCommandModule.HideFromCommandList = true;
			simpleCommandModule.HasDynamicInput = true;
			simpleCommandModule.Method = ShowModCommands;
			simpleCommandModule.Abbreviations = new string[3] { "mdc", "modc", "modcmds" };
			SimpleCommandModule module = simpleCommandModule;
			SimpleCommand.AddSimpleCommand(module);
		}

		private static TerminalNode ShowModCommands(Terminal __instance)
		{
			string text = "All available mod commands: \n";
			TerminalNode val = ScriptableObject.CreateInstance<TerminalNode>();
			val.clearPreviousText = true;
			string s = SimpleCommand.GetInputValue(__instance, 1)[0];
			int num = 1;
			int num2 = 1;
			if (int.TryParse(s, out var result))
			{
				num = ((result <= 1) ? 1 : result);
			}
			if (SimpleCommand.SimpleCommandDictionary != null && SimpleCommand.SimpleCommandDictionary.Count > 0)
			{
				num2 = SimpleCommand.SimpleCommandDictionary.Last().Key;
				if (num > num2)
				{
					num = num2;
				}
				if (SimpleCommand.SimpleCommandDictionary.TryGetValue(num, out var value))
				{
					text = value.Where((string str) => !string.IsNullOrEmpty(str)).Aggregate(text, (string current, string str) => current + str);
				}
			}
			text = text + "\nPage " + num + " of " + num2 + "\n";
			val.displayText = text;
			return val;
		}
	}
	public static class SimpleCommand
	{
		internal static List<SimpleCommandModule> SimpleCommandList = new List<SimpleCommandModule>();

		internal static SortedDictionary<int, List<string>> SimpleCommandDictionary = new SortedDictionary<int, List<string>>();

		public static void AddSimpleCommand(SimpleCommandModule module)
		{
			if (module.DisplayName != null)
			{
				SimpleCommandList.Add(module);
				if (Plugin.ConfigSimpleCommandLogging.Value)
				{
					Plugin.Log.LogDebug((object)("Module " + module.DisplayName + " was successfully registered by SimpleCommand.API."));
				}
			}
			else
			{
				Plugin.Log.LogWarning((object)"Warning, DisplayName of module not assigned. Module will not load!");
			}
		}

		public static void AddSimpleCommands(SimpleCommandModule[] modules)
		{
			foreach (SimpleCommandModule module in modules)
			{
				AddSimpleCommand(module);
			}
		}

		public static string RemovePunctuation(string s)
		{
			StringBuilder stringBuilder = new StringBuilder();
			foreach (char c in s)
			{
				if (!char.IsPunctuation(c))
				{
					stringBuilder.Append(c);
				}
			}
			return stringBuilder.ToString().ToLower();
		}

		public static string GetInputValue(Terminal terminal)
		{
			string text = RemovePunctuation(terminal.screenText.text.Substring(terminal.screenText.text.Length - terminal.textAdded));
			if (Plugin.ConfigSimpleCommandLogging.Value)
			{
				Plugin.Log.LogDebug((object)("GetInputValue:" + text));
			}
			return text;
		}

		public static string[] GetInputValue(Terminal terminal, int index)
		{
			string text = RemovePunctuation(terminal.screenText.text.Substring(terminal.screenText.text.Length - terminal.textAdded));
			string[] array = text.Split(" ");
			string[] array2 = array.Skip(Math.Max(0, array.Length - index)).Take(index).ToArray();
			if (Plugin.ConfigSimpleCommandLogging.Value)
			{
				int num = 1;
				Plugin.Log.LogDebug((object)"GetInputValue Array:");
				string[] array3 = array2;
				foreach (string text2 in array3)
				{
					Plugin.Log.LogDebug((object)(num + ": " + text2));
					num++;
				}
			}
			return array2;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "SimpleCommand.API";

		public const string PLUGIN_NAME = "SimpleCommand.API";

		public const string PLUGIN_VERSION = "1.0.2";
	}
}
namespace SimpleCommand.API.Patches
{
	[HarmonyPatch(typeof(Terminal))]
	public class SimpleCommandPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch("Awake")]
		private static void PatchAwake(ref Terminal __instance)
		{
			TMP_InputField screenText = __instance.screenText;
			List<string> list = new List<string>();
			List<string> list2 = new List<string>();
			int num = 1;
			int num2 = 0;
			if (Plugin.ConfigSimpleCommandLogging.Value)
			{
				Plugin.Log.LogDebug((object)"Patching Terminal Awake");
			}
			if ((Object)(object)__instance.terminalNodes.specialNodes[13] != (Object)null)
			{
				if (Plugin.ConfigSimpleCommandLogging.Value)
				{
					Plugin.Log.LogDebug((object)"Editing SpecialNode 13");
				}
				TerminalNode val = __instance.terminalNodes.specialNodes[13];
				string displayText = val.displayText;
				string text = displayText.Substring(0, displayText.Length - 23);
				string displayText2 = val.displayText;
				int length = displayText2.Length;
				int num3 = length - 23;
				val.displayText = text + ">MODCOMMANDS [page] (short. mdc, modc, modcmds)\nTo see a list of all SimpleCommand.API commands." + displayText2.Substring(num3, length - num3);
			}
			if (SimpleCommand.SimpleCommandList == null || SimpleCommand.SimpleCommandList.Count <= 0)
			{
				return;
			}
			foreach (SimpleCommandModule simpleCommand in SimpleCommand.SimpleCommandList)
			{
				if (!simpleCommand.HideFromCommandList)
				{
					string text2 = "";
					text2 = text2 + "\n>" + simpleCommand.DisplayName.ToUpper();
					if (simpleCommand.Arguments != null && simpleCommand.HasDynamicInput)
					{
						string[] arguments = simpleCommand.Arguments;
						foreach (string text3 in arguments)
						{
							text2 = text2 + " [" + text3.ToLower() + "]";
						}
					}
					if (simpleCommand.Abbreviations != null)
					{
						text2 += " (short. ";
						string[] abbreviations = simpleCommand.Abbreviations;
						foreach (string text4 in abbreviations)
						{
							text2 = text2 + text4.ToLower() + ", ";
						}
						text2 = text2.Substring(0, text2.Length - 2) + ")";
					}
					text2 += "\n";
					if (simpleCommand.Description != null)
					{
						text2 = text2 + simpleCommand.Description + "\n";
					}
					list.Add(text2);
				}
				if (simpleCommand.ChildrenModules != null)
				{
					SimpleCommandModule[] childrenModules = simpleCommand.ChildrenModules;
					foreach (SimpleCommandModule module in childrenModules)
					{
						AddChildrenToList(module, simpleCommand.DisplayName.ToUpper(), list);
					}
				}
			}
			if (Plugin.ConfigSimpleCommandSortCommands.Value)
			{
				list.Sort();
			}
			if (Plugin.ConfigSimpleCommandLogging.Value)
			{
				Plugin.Log.LogDebug((object)"Creating SimpleCommandDictionary");
			}
			SimpleCommand.SimpleCommandDictionary = new SortedDictionary<int, List<string>>();
			foreach (string item5 in list)
			{
				int num4 = Mathf.CeilToInt((float)screenText.textComponent.GetTextInfo(item5).lineCount);
				if (num2 + num4 <= 16)
				{
					list2.Add(item5);
					num2 += num4;
					continue;
				}
				int num5 = 16 - num2;
				if (num5 > 0)
				{
					string item = new string('\n', num5);
					list2.Add(item);
				}
				SimpleCommand.SimpleCommandDictionary.TryAdd(num, list2);
				list2 = new List<string> { item5 };
				num2 = num4;
				num++;
			}
			if (list2.Count > 0)
			{
				int num6 = 16 - num2;
				if (num6 > 0)
				{
					string item2 = new string('\n', num6);
					list2.Add(item2);
				}
				SimpleCommand.SimpleCommandDictionary.TryAdd(num, list2);
			}
			else if (list2.Count == 0)
			{
				string item3 = "\n [ERROR] No commands added to list..\n";
				string item4 = new string('\n', 14);
				list2.Add(item3);
				list2.Add(item4);
				SimpleCommand.SimpleCommandDictionary.TryAdd(num, list2);
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("OnSubmit")]
		private static bool PatchSubmit(ref Terminal __instance)
		{
			if (__instance.terminalInUse && __instance.textAdded != 0 && SimpleCommand.SimpleCommandList != null && SimpleCommand.SimpleCommandList.Count > 0)
			{
				string text = SimpleCommand.RemovePunctuation(__instance.screenText.text.Substring(__instance.screenText.text.Length - __instance.textAdded));
				string[] screenTextArray = text.Split(" ", StringSplitOptions.RemoveEmptyEntries);
				foreach (SimpleCommandModule simpleCommand in SimpleCommand.SimpleCommandList)
				{
					if (text.Replace(" ", "") == "")
					{
						break;
					}
					if (simpleCommand.DisplayName != null && screenTextArray[0].Replace(" ", "").Equals(simpleCommand.DisplayName.ToLower()))
					{
						if (simpleCommand.IgnoreModule)
						{
							return true;
						}
						if (__instance.terminalNodes.allKeywords.Any((TerminalKeyword keyword) => keyword.word.Equals(screenTextArray[0].Replace(" ", ""))))
						{
							Plugin.Log.LogWarning((object)(screenTextArray[0].Replace(" ", "") + " already exists as a default terminal keyword. This might cause issues."));
						}
						if (Plugin.ConfigSimpleCommandLogging.Value)
						{
							Plugin.Log.LogDebug((object)("Player submitted sentence: " + text));
						}
						if (screenTextArray.Length > 1)
						{
							if (simpleCommand.HasDynamicInput)
							{
								if (simpleCommand.Method != null)
								{
									__instance.LoadNewNode(simpleCommand.Method(__instance));
									if (Plugin.ConfigSimpleCommandLogging.Value)
									{
										Plugin.Log.LogDebug((object)("Method from module " + simpleCommand.DisplayName + " successfully invoked."));
									}
									LoadNecessarySubmitActions(__instance);
									return false;
								}
								Plugin.Log.LogDebug((object)("No Method found for module " + simpleCommand.DisplayName));
							}
							else
							{
								if (simpleCommand.ChildrenModules == null)
								{
									continue;
								}
								SimpleCommandModule[] childrenModules = simpleCommand.ChildrenModules;
								foreach (SimpleCommandModule module in childrenModules)
								{
									if (IterateThroughChildren(module, __instance, screenTextArray, 1))
									{
										LoadNecessarySubmitActions(__instance);
										return false;
									}
								}
							}
							continue;
						}
						if (simpleCommand.Method != null)
						{
							__instance.LoadNewNode(simpleCommand.Method(__instance));
							if (Plugin.ConfigSimpleCommandLogging.Value)
							{
								Plugin.Log.LogDebug((object)("Method from module " + simpleCommand.DisplayName + " successfully invoked."));
							}
							LoadNecessarySubmitActions(__instance);
							return false;
						}
						Plugin.Log.LogDebug((object)("No Method found for module " + simpleCommand.DisplayName));
					}
					else
					{
						if (simpleCommand.Abbreviations == null)
						{
							continue;
						}
						string[] abbreviations = simpleCommand.Abbreviations;
						foreach (string value in abbreviations)
						{
							if (!screenTextArray[0].Replace(" ", "").Equals(value))
							{
								continue;
							}
							if (Plugin.ConfigSimpleCommandLogging.Value)
							{
								Plugin.Log.LogDebug((object)("Player submitted sentence: " + text));
							}
							if (screenTextArray.Length > 1)
							{
								if (simpleCommand.HasDynamicInput)
								{
									if (simpleCommand.Method != null)
									{
										__instance.LoadNewNode(simpleCommand.Method(__instance));
										if (Plugin.ConfigSimpleCommandLogging.Value)
										{
											Plugin.Log.LogDebug((object)("Method from module " + simpleCommand.DisplayName + " successfully invoked."));
										}
										LoadNecessarySubmitActions(__instance);
										return false;
									}
									Plugin.Log.LogDebug((object)("No Method found for module " + simpleCommand.DisplayName));
								}
								else
								{
									if (simpleCommand.ChildrenModules == null)
									{
										continue;
									}
									SimpleCommandModule[] childrenModules2 = simpleCommand.ChildrenModules;
									foreach (SimpleCommandModule module2 in childrenModules2)
									{
										if (IterateThroughChildren(module2, __instance, screenTextArray, 1))
										{
											LoadNecessarySubmitActions(__instance);
											return false;
										}
									}
								}
								continue;
							}
							if (simpleCommand.Method != null)
							{
								__instance.LoadNewNode(simpleCommand.Method(__instance));
								if (Plugin.ConfigSimpleCommandLogging.Value)
								{
									Plugin.Log.LogDebug((object)("Method from module " + simpleCommand.DisplayName + " successfully invoked."));
								}
								LoadNecessarySubmitActions(__instance);
								return false;
							}
							Plugin.Log.LogDebug((object)("No Method found for module " + simpleCommand.DisplayName));
						}
					}
				}
			}
			return true;
		}

		private static bool IterateThroughChildren(SimpleCommandModule module, Terminal instance, string[] screenTextArray, int count)
		{
			if (module.IgnoreModule)
			{
				return false;
			}
			if (screenTextArray[count].Replace(" ", "").Equals(module.DisplayName))
			{
				if (module.HasDynamicInput && module.Method != null)
				{
					instance.LoadNewNode(module.Method(instance));
					if (Plugin.ConfigSimpleCommandLogging.Value)
					{
						Plugin.Log.LogDebug((object)("Method from module " + module.DisplayName + " successfully invoked."));
					}
					return true;
				}
				if (module.ChildrenModules != null)
				{
					SimpleCommandModule[] childrenModules = module.ChildrenModules;
					foreach (SimpleCommandModule module2 in childrenModules)
					{
						IterateThroughChildren(module2, instance, screenTextArray, count + 1);
					}
				}
				else if (module.Method != null)
				{
					instance.LoadNewNode(module.Method(instance));
					if (Plugin.ConfigSimpleCommandLogging.Value)
					{
						Plugin.Log.LogDebug((object)("Method from module " + module.DisplayName + " successfully invoked."));
					}
					return true;
				}
			}
			else if (module.Abbreviations != null)
			{
				string[] abbreviations = module.Abbreviations;
				foreach (string value in abbreviations)
				{
					if (!screenTextArray[count].Replace(" ", "").Equals(value))
					{
						continue;
					}
					if (module.HasDynamicInput && module.Method != null)
					{
						instance.LoadNewNode(module.Method(instance));
						if (Plugin.ConfigSimpleCommandLogging.Value)
						{
							Plugin.Log.LogDebug((object)("Method from module " + module.DisplayName + " successfully invoked."));
						}
						return true;
					}
					if (module.ChildrenModules != null)
					{
						SimpleCommandModule[] childrenModules2 = module.ChildrenModules;
						foreach (SimpleCommandModule module3 in childrenModules2)
						{
							IterateThroughChildren(module3, instance, screenTextArray, count + 1);
						}
					}
					else if (module.Method != null)
					{
						instance.LoadNewNode(module.Method(instance));
						if (Plugin.ConfigSimpleCommandLogging.Value)
						{
							Plugin.Log.LogDebug((object)("Method from module " + module.DisplayName + " successfully invoked."));
						}
						return true;
					}
				}
			}
			return false;
		}

		private static void AddChildrenToList(SimpleCommandModule module, string nestedString, List<string> commands)
		{
			string text = nestedString + " " + module.DisplayName.ToUpper();
			if (!module.HideFromCommandList)
			{
				string text2 = "\n>" + text;
				if (module.Arguments != null && module.HasDynamicInput)
				{
					string[] arguments = module.Arguments;
					foreach (string text3 in arguments)
					{
						text2 = text2 + " [" + text3.ToLower() + "]";
					}
				}
				if (module.Abbreviations != null)
				{
					text2 += " (short. ";
					string[] abbreviations = module.Abbreviations;
					foreach (string text4 in abbreviations)
					{
						text2 = text2 + text4.ToLower() + ", ";
					}
					string text5 = text2;
					text2 = text5.Substring(0, text5.Length - 2) + ")";
				}
				text2 += "\n";
				if (module.Description != null)
				{
					text2 = text2 + module.Description + "\n";
				}
				commands.Add(text2);
			}
			if (module.ChildrenModules != null)
			{
				SimpleCommandModule[] childrenModules = module.ChildrenModules;
				foreach (SimpleCommandModule module2 in childrenModules)
				{
					AddChildrenToList(module2, text, commands);
				}
			}
		}

		private static void LoadNecessarySubmitActions(Terminal instance)
		{
			if (Plugin.ConfigSimpleCommandLogging.Value)
			{
				Plugin.Log.LogDebug((object)"Loading necessary actions after Method submit.");
			}
			instance.screenText.text = instance.screenText.text.Substring(0, instance.screenText.text.Length - instance.textAdded);
			instance.currentText = instance.screenText.text;
			instance.textAdded = 0;
			instance.screenText.ActivateInputField();
			((Selectable)instance.screenText).Select();
		}
	}
}
namespace SimpleCommand.API.Classes
{
	public class SimpleCommandModule
	{
		public string DisplayName { get; set; }

		public string Description { get; set; }

		public string[] Abbreviations { get; set; }

		public string[] Arguments { get; set; }

		public bool HasDynamicInput { get; set; }

		public bool HideFromCommandList { get; set; }

		public bool IgnoreModule { get; set; }

		public Func<Terminal, TerminalNode> Method { get; set; }

		public SimpleCommandModule[] ChildrenModules { get; set; }
	}
}