Decompiled source of TerminalKeyBindings v1.0.0

BepInEx/plugins/TerminalKeyBindings.dll

Decompiled 7 months ago
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using TerminalApi.Events;
using UnityEngine;
using UnityEngine.InputSystem;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("TerminalKeyBindings")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TerminalKeyBindings")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("5e69b1d4-1293-4a88-bf97-a3e2f3d8d3ae")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace TerminalKeyBindings;

[BepInPlugin("net.navarrotech.TerminalKeyBindings", "TerminalKeyBindings", "1.0.0")]
[BepInDependency("atomic.terminalapi", "1.2.0")]
public class TerminalKeyBindings : BaseUnityPlugin
{
	public const string ModGUID = "net.navarrotech.TerminalKeyBindings";

	public const string ModName = "TerminalKeyBindings";

	public const string ModVersion = "1.0.0";

	private static HUDManager hudManager;

	public static ConfigEntry<string> assignedKeys;

	public static string[] validKeyNames = GetAllKeyNames();

	internal static CommandBinding[] keyBindings = new CommandBinding[0];

	internal static InputAction[] inputActions = (InputAction[])(object)new InputAction[0];

	public static TerminalKeyBindings instance;

	private void Awake()
	{
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Expected O, but got Unknown
		instance = this;
		Log("TerminalKeyBindings loading config...");
		loadConfig();
		Log(string.Join(",", validKeyNames));
		Events.TerminalAwake += new TerminalEventHandler(TerminalIsAwake);
		Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
		Log("TerminalKeyBindings ready to go!");
	}

	public static void AddItemToArray<T>(ref T[] array, T itemToAdd)
	{
		T[] array2 = new T[array.Length + 1];
		array.CopyTo(array2, 0);
		array2[array.Length] = itemToAdd;
		array = array2;
	}

	private void loadConfig()
	{
		assignedKeys = ((BaseUnityPlugin)instance).Config.Bind<string>("KeyBindings", "KeyMap", "Numpad0:scan;Numpad5:view monitor;", "Map commands as \"key:terminal command;key:teriminal command;\"Keys must be keyboard buttons, as 'X' will be the 'X' key, 'Digit9' will be '9' and 'num_5' will be numpad 5.Then write the command after the colon, and end the command with a semi-colon. Commands like ROUTE or BUY will be auto-confirmed for you.Acceptable values: " + string.Join(", ", validKeyNames));
		if (assignedKeys == null || assignedKeys.Value.Length == 0)
		{
			return;
		}
		string text = assignedKeys.Value.Trim();
		if (text.EndsWith(";"))
		{
			text = text.Substring(0, text.Length - 1);
		}
		string[] array = text.Split(new char[1] { ';' });
		string[] array2 = array;
		foreach (string text2 in array2)
		{
			if (text2 == "")
			{
				Log("Skipping empty command: " + text2);
				break;
			}
			CommandBinding commandBinding = new CommandBinding(text2);
			if (commandBinding.valid)
			{
				AddItemToArray(ref keyBindings, commandBinding);
				Log("Registered key: " + commandBinding.key + " to the command " + commandBinding.value);
			}
			else
			{
				Log("Invalid formatting of keybinding: '" + text2 + "'");
			}
		}
	}

	private static string[] GetAllKeyNames()
	{
		return (from Key key in Enum.GetValues(typeof(Key))
			select ((object)(Key)(ref key)).ToString()).ToArray();
	}

	private void TerminalIsAwake(object sender, TerminalEventArgs e)
	{
		Log("Terminal is awake");
		BindKeys();
	}

	public static void BindKeys()
	{
		//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d9: Expected O, but got Unknown
		Log("Binding keys...");
		OnDestroy();
		if (keyBindings.Length == 0)
		{
			Log("No keybindings to add.");
			return;
		}
		Log(keyBindings.Length + " key bindings to add...");
		CommandBinding[] array = keyBindings;
		foreach (CommandBinding binding in array)
		{
			try
			{
				Log("Adding input action for: <Keyboard>/" + binding.key);
				Log("Raw input action: " + binding.raw);
				InputAction val = new InputAction(binding.raw, (InputActionType)0, "<Keyboard>/" + binding.key, "Press", (string)null, (string)null);
				val.Enable();
				val.performed += delegate
				{
					remoteCommand(binding.value);
				};
				AddItemToArray(ref inputActions, val);
			}
			catch (Exception ex)
			{
				Log("Error setting up input actions: " + ex.Message);
			}
		}
	}

	private static void OnDestroy()
	{
		Log("Input actions cleaning up bound keys...");
		if (inputActions != null)
		{
			InputAction[] array = inputActions;
			foreach (InputAction val in array)
			{
				val.Disable();
				val.Dispose();
			}
			inputActions = null;
		}
	}

	private static void remoteCommand(string command)
	{
		Log("Triggering remote command: " + command);
		Terminal val = Object.FindObjectOfType<Terminal>();
		if ((Object)(object)val == (Object)null)
		{
			Log("Unable to find a terminal");
			return;
		}
		TerminalNode currentNode = val.currentNode;
		bool terminalInUse = val.terminalInUse;
		string text = val.screenText.text;
		int textAdded = val.textAdded;
		val.currentNode = null;
		val.terminalInUse = true;
		val.screenText.text = command;
		val.textAdded = command.Length;
		bool flag = isConfirmationCommand(command);
		val.OnSubmit();
		if (flag || val.currentNode.isConfirmationNode)
		{
			Log("Is a confirmation node, auto-applying CONFIRM to it...");
			val.screenText.text = "CONFIRM";
			val.textAdded = "CONFIRM".Length;
			val.OnSubmit();
			DisplayMessage("Terminal:", val.screenText.text.TrimStart(new char[1] { '\n' }));
		}
		else
		{
			DisplayMessage("Terminal:", val.screenText.text.TrimStart(new char[1] { '\n' }));
		}
		val.currentNode = currentNode;
		val.terminalInUse = terminalInUse;
		val.screenText.text = text;
		val.textAdded = textAdded;
		Log("Remote command finished.");
	}

	public static bool isConfirmationCommand(string message)
	{
		if (message == null || message.Length == 0)
		{
			return false;
		}
		string[] array = message.Split(new char[1] { ' ' });
		string text = array[0];
		string text2 = text;
		string text3 = text2;
		if (text3 == "route" || text3 == "buy")
		{
			return true;
		}
		return false;
	}

	public static void Log(string message)
	{
		((BaseUnityPlugin)instance).Logger.LogInfo((object)message);
	}

	private static void DisplayMessage(string header, string message)
	{
		if ((Object)(object)hudManager == (Object)null)
		{
			hudManager = Object.FindObjectOfType<HUDManager>();
		}
		hudManager.DisplayTip(header, message, false, false, "LC_Tip1");
	}
}
internal class CommandBinding
{
	public string raw;

	public string key;

	public string value;

	public bool valid = false;

	public CommandBinding(string binding)
	{
		valid = parse(binding);
	}

	public bool parse(string binding)
	{
		raw = binding;
		string[] array = binding.Split(new char[1] { ':' });
		if (array.Length != 2)
		{
			return false;
		}
		key = array[0].Trim();
		value = array[1].Trim();
		if (key.Length == 0)
		{
			return false;
		}
		if (value.Length == 0)
		{
			return false;
		}
		if (!TerminalKeyBindings.validKeyNames.Contains(key))
		{
			return false;
		}
		return true;
	}
}