Decompiled source of Binder v1.1.0

Binder.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("Binder")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+25ed598289f104ce51619b03bed9419d3b872af5")]
[assembly: AssemblyProduct("Binder")]
[assembly: AssemblyTitle("Binder")]
[assembly: AssemblyVersion("1.0.0.0")]
public static class BindAddCommand
{
	public static void Execute(string[] args)
	{
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		if (args.Length < 2)
		{
			ConsoleHelper.AddMessage("Usage: bind add <key> <command1>, <command2>, ...");
			return;
		}
		try
		{
			KeyCode val = (KeyCode)Enum.Parse(typeof(KeyCode), args[0], ignoreCase: true);
			string[] array = (from c in string.Join(" ", Utils.SubArray(args, 1)).Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries)
				select c.Trim()).ToArray();
			BindManager.AddBind(val, array);
			ConsoleHelper.AddMessage(string.Format("Bound key [{0}] to commands: {1}", val, string.Join(", ", array)));
			ConsoleHelper.AddMessage("<color=orange>* When you use the binds, cheat mode will also be activated (scoring disabled)</color>");
		}
		catch
		{
			ConsoleHelper.AddMessage("Invalid key: " + args[0]);
		}
	}
}
public static class BindClearCommand
{
	public static void Execute(string[] args)
	{
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		if (args.Length < 1)
		{
			ConsoleHelper.AddMessage("Usage: bind clear <key>");
			return;
		}
		try
		{
			KeyCode val = (KeyCode)Enum.Parse(typeof(KeyCode), args[0], ignoreCase: true);
			if (BindManager.ClearBind(val))
			{
				ConsoleHelper.AddMessage($"All binds on the key [{val}] are cleared");
			}
			else
			{
				ConsoleHelper.AddMessage($"No bind found for key [{val}]");
			}
		}
		catch
		{
			ConsoleHelper.AddMessage("Invalid key: " + args[0]);
		}
	}
}
[Serializable]
public class BindConfig
{
	public Dictionary<string, string[]> Binds { get; set; } = new Dictionary<string, string[]>();

}
public static class BindListCommand
{
	public static void Execute(string[] args)
	{
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		IReadOnlyDictionary<KeyCode, string[]> allBinds = BindManager.GetAllBinds();
		if (allBinds.Count == 0)
		{
			ConsoleHelper.AddMessage("No keybinds.");
			return;
		}
		foreach (KeyValuePair<KeyCode, string[]> item in allBinds)
		{
			string arg = string.Join(", ", item.Value);
			ConsoleHelper.AddMessage($"{item.Key} -> {arg}");
		}
	}
}
public static class BindManager
{
	private static Dictionary<KeyCode, string[]> binds = new Dictionary<KeyCode, string[]>();

	private static readonly string configPath = Path.Combine(Paths.ConfigPath, "binder_binds.json");

	public static void AddBind(KeyCode key, string[] commands)
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		binds[key] = commands;
		SaveBinds();
	}

	public static bool ClearBind(KeyCode key)
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		bool num = binds.Remove(key);
		if (num)
		{
			SaveBinds();
		}
		return num;
	}

	public static IReadOnlyDictionary<KeyCode, string[]> GetAllBinds()
	{
		return binds;
	}

	public static void CheckInput()
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		foreach (KeyValuePair<KeyCode, string[]> bind in binds)
		{
			if (Input.GetKeyDown(bind.Key))
			{
				ConsoleHelper.EnableCheatsSilently();
				string[] value = bind.Value;
				for (int i = 0; i < value.Length; i++)
				{
					ConsoleHelper.ExecuteCommandSilenty(value[i]);
				}
			}
		}
	}

	public static void LoadBinds()
	{
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_0081: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: Invalid comparison between Unknown and I4
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b1: Invalid comparison between Unknown and I4
		//IL_008e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
		if (!File.Exists(configPath))
		{
			return;
		}
		try
		{
			string text = File.ReadAllText(configPath);
			if (string.IsNullOrEmpty(text))
			{
				return;
			}
			JToken val = JObject.Parse(text)["Binds"];
			if (val == null)
			{
				return;
			}
			binds.Clear();
			bool flag = false;
			foreach (JProperty item in val.Children<JProperty>())
			{
				if (Enum.TryParse<KeyCode>(item.Name, ignoreCase: true, out KeyCode result))
				{
					JToken value = item.Value;
					if ((int)value.Type == 8)
					{
						binds[result] = new string[1] { ((object)value).ToString() };
						flag = true;
					}
					else if ((int)value.Type == 2)
					{
						binds[result] = value.ToObject<string[]>();
					}
				}
			}
			if (flag)
			{
				Debug.Log((object)"Binder: Converted old bind format to new version.");
				SaveBinds();
			}
		}
		catch (Exception arg)
		{
			Debug.LogError((object)$"Binder: Failed to load binds: {arg}");
		}
	}

	private static void SaveBinds()
	{
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			BindConfig bindConfig = new BindConfig();
			foreach (KeyValuePair<KeyCode, string[]> bind in binds)
			{
				Dictionary<string, string[]> dictionary = bindConfig.Binds;
				KeyCode key = bind.Key;
				dictionary[((object)(KeyCode)(ref key)).ToString()] = bind.Value;
			}
			string contents = JsonConvert.SerializeObject((object)bindConfig, (Formatting)1);
			File.WriteAllText(configPath, contents);
		}
		catch (Exception arg)
		{
			Debug.LogError((object)$"Binder: Failed to save binds: {arg}");
		}
	}
}
public static class BindModifyCommand
{
	public static void Execute(string[] args)
	{
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: Unknown result type (might be due to invalid IL or missing references)
		if (args.Length < 3)
		{
			ConsoleHelper.AddMessage("Usage:");
			ConsoleHelper.AddMessage("  bind modify removefrom <key> <position>");
			ConsoleHelper.AddMessage("  bind modify appendto <key> <command>");
			return;
		}
		string text = args[0].ToLower();
		string text2 = args[1];
		string[] value;
		if (!Enum.TryParse<KeyCode>(text2, ignoreCase: true, out KeyCode result))
		{
			ConsoleHelper.AddMessage("Invalid key: " + text2);
		}
		else if (!BindManager.GetAllBinds().TryGetValue(result, out value))
		{
			ConsoleHelper.AddMessage($"No binds found for key {result}");
		}
		else if (!(text == "removefrom"))
		{
			if (text == "appendto")
			{
				AppendCommand(result, value, args);
			}
			else
			{
				ConsoleHelper.AddMessage("Unknown modify action: " + text);
			}
		}
		else
		{
			RemoveCommand(result, value, args);
		}
	}

	private static void RemoveCommand(KeyCode key, string[] commands, string[] args)
	{
		//IL_006c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		if (args.Length < 3 || !int.TryParse(args[2], out var result))
		{
			ConsoleHelper.AddMessage("Invalid or missing position.");
			return;
		}
		if (result < 1 || result > commands.Length)
		{
			ConsoleHelper.AddMessage("Position out of range.");
			return;
		}
		int num = result - 1;
		string arg = commands[num];
		string[] array = new string[commands.Length - 1];
		int num2 = 0;
		for (int i = 0; i < commands.Length; i++)
		{
			if (i != num)
			{
				array[num2++] = commands[i];
			}
		}
		BindManager.AddBind(key, array);
		ConsoleHelper.AddMessage($"Removed \"{arg}\" command from key {key}");
	}

	private static void AppendCommand(KeyCode key, string[] commands, string[] args)
	{
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		if (args.Length < 3)
		{
			ConsoleHelper.AddMessage("Missing command to append.");
			return;
		}
		string text = string.Join(" ", args, 2, args.Length - 2);
		string[] array = new string[commands.Length + 1];
		Array.Copy(commands, array, commands.Length);
		array[^1] = text;
		BindManager.AddBind(key, array);
		ConsoleHelper.AddMessage($"Appended \"{text}\" command to key {key}");
	}
}
public static class SilentLogController
{
	public static bool SuppressLogging;
}
[HarmonyPatch(typeof(CommandConsole), "Awake")]
public static class CommandConsolePatch
{
	[HarmonyPostfix]
	public static void Postfix(CommandConsole __instance)
	{
		if ((Object)(object)__instance == (Object)null)
		{
			return;
		}
		MethodInfo methodInfo = AccessTools.Method(typeof(CommandConsole), "RegisterCommand", (Type[])null, (Type[])null);
		if (methodInfo == null)
		{
			Debug.LogError((object)"RegisterCommand not found");
			return;
		}
		Action<string[]> action = delegate(string[] args)
		{
			if (args.Length == 0)
			{
				ConsoleHelper.AddMessage("Usage: add | clear | list | modify");
			}
			else
			{
				string text = args[0].ToLowerInvariant();
				string[] args2 = Utils.SubArray(args, 1);
				switch (text)
				{
				case "add":
					BindAddCommand.Execute(args2);
					break;
				case "clear":
					BindClearCommand.Execute(args2);
					break;
				case "list":
					BindListCommand.Execute(args2);
					break;
				case "modify":
					BindModifyCommand.Execute(args2);
					break;
				default:
					ConsoleHelper.AddMessage("Unknown bind subcommand. Use: add | clear | list | modify");
					break;
				}
			}
		};
		methodInfo.Invoke(__instance, new object[3] { "bind", action, false });
	}
}
[HarmonyPatch(typeof(ENT_Player), "LateUpdate")]
public static class ENT_Player_LateUpdate_Patch
{
	[HarmonyPostfix]
	public static void Postfix(ENT_Player __instance)
	{
		if (!((Object)(object)__instance == (Object)null) && !__instance.IsInputLocked())
		{
			BindManager.CheckInput();
		}
	}
}
[HarmonyPatch(typeof(CommandConsole))]
public static class CommandConsole_Log_Patch
{
	[HarmonyPrefix]
	[HarmonyPatch("Log")]
	public static bool Prefix_Log(ref string message)
	{
		if (SilentLogController.SuppressLogging)
		{
			Debug.Log((object)("Binder: Suppressed log message: " + message));
			return false;
		}
		return true;
	}
}
public static class ConsoleHelper
{
	private static MethodInfo addMessageMethod = AccessTools.Method(typeof(CommandConsole), "AddMessageToHistory", (Type[])null, (Type[])null);

	private static FieldInfo cheatsEnabledField = AccessTools.Field(typeof(CommandConsole), "cheatsEnabled");

	private static FieldInfo hasCheatedField = AccessTools.Field(typeof(CommandConsole), "hasCheated");

	public static void AddMessage(string message)
	{
		if ((Object)(object)CommandConsole.instance != (Object)null && addMessageMethod != null)
		{
			addMessageMethod.Invoke(CommandConsole.instance, new object[1] { message });
		}
	}

	public static void EnableCheatsSilently()
	{
		if ((Object)(object)CommandConsole.instance != (Object)null)
		{
			cheatsEnabledField?.SetValue(CommandConsole.instance, true);
			hasCheatedField?.SetValue(CommandConsole.instance, true);
		}
		Type typeFromHandle = typeof(CL_UIManager);
		object obj = AccessTools.Field(typeFromHandle, "instance")?.GetValue(null);
		if (obj != null)
		{
			object? obj2 = AccessTools.Field(typeFromHandle, "cheatTracker")?.GetValue(obj);
			object? obj3 = ((obj2 is GameObject) ? obj2 : null);
			if (obj3 != null)
			{
				((GameObject)obj3).SetActive(true);
			}
		}
	}

	public static bool ExecuteCommandSilenty(string commandLine)
	{
		if ((Object)(object)CommandConsole.instance == (Object)null)
		{
			return false;
		}
		string[] array = commandLine.Split(new char[1] { ' ' });
		if (array.Length == 0)
		{
			return false;
		}
		string text = array[0].ToLower();
		string[] array2 = new string[Mathf.Max(array.Length - 1, 0)];
		Array.Copy(array, 1, array2, 0, array2.Length);
		if (!Traverse.Create((object)CommandConsole.instance).Field("commands").GetValue<Dictionary<string, Command>>()
			.TryGetValue(text, out var value))
		{
			return false;
		}
		try
		{
			SilentLogController.SuppressLogging = true;
			value.callback(array2);
		}
		catch (Exception arg)
		{
			Debug.LogError((object)$"Binder: Failed to execute {text}: {arg}");
		}
		finally
		{
			SilentLogController.SuppressLogging = false;
		}
		return true;
	}
}
public class Utils
{
	public static T[] SubArray<T>(T[] data, int index)
	{
		if (index >= data.Length)
		{
			return new T[0];
		}
		int num = data.Length - index;
		T[] array = new T[num];
		Array.Copy(data, index, array, 0, num);
		return array;
	}
}
namespace Binder;

[BepInPlugin("com.cyfral.binder", "Console Binder", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	private void Awake()
	{
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		BindManager.LoadBinds();
		new Harmony("com.cyfral.binder").PatchAll();
	}
}