Decompiled source of CustomKeybinds v0.1.1

plugins/danielrbz.CustomKeybinds.dll

Decompiled 3 months ago
using System;
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 Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.InputSystem.Utilities;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("danielrbz.CustomKeybinds")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("danielrbz.CustomKeybinds")]
[assembly: AssemblyTitle("CustomKeybinds")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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;
		}
	}
}
[BepInPlugin("danielrbz.CustomKeybinds", "Custom Keybinds", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	private InputActionAsset actions;

	private Dictionary<string, ConfigEntry<string>> keybindConfigs = new Dictionary<string, ConfigEntry<string>>();

	private List<string> actionNames = new List<string>();

	private ConfigEntry<string> possibleActions;

	private ConfigEntry<string> acceptableKeys;

	private bool showUI;

	private string listeningFor;

	private Vector2 scrollPos = Vector2.zero;

	private static readonly HashSet<string> allowedActions = new HashSet<string> { "Interact", "Drop", "UsePrimary", "UseSecondary", "Jump", "Sprint", "Crouch", "Emote", "Ping", "PushToTalk" };

	private static readonly string[] mouseButtons = new string[6] { "leftButton", "rightButton", "middleButton", "backButton", "forwardButton", "press" };

	private static readonly Dictionary<string, string> mouseDisplayNames = new Dictionary<string, string>
	{
		{ "leftButton", "Mouse1" },
		{ "rightButton", "Mouse2" },
		{ "middleButton", "Mouse3" },
		{ "backButton", "Mouse4" },
		{ "forwardButton", "Mouse5" },
		{ "press", "MousePress" }
	};

	private void Awake()
	{
		//IL_014d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0152: Unknown result type (might be due to invalid IL or missing references)
		//IL_0156: Unknown result type (might be due to invalid IL or missing references)
		//IL_015b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0164: Unknown result type (might be due to invalid IL or missing references)
		//IL_0169: Unknown result type (might be due to invalid IL or missing references)
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin Custom Keybinds is loaded!");
		actions = Resources.FindObjectsOfTypeAll<InputActionAsset>()[0];
		actionNames.Clear();
		foreach (InputAction action in actions)
		{
			if (allowedActions.Contains(action.name) && !actionNames.Contains(action.name))
			{
				actionNames.Add(action.name);
			}
		}
		possibleActions = ((BaseUnityPlugin)this).Config.Bind<string>("Help", "PossibleActions", string.Join(", ", actionNames), "List of all possible action names (read-only).");
		string text = string.Join(", ", Enum.GetNames(typeof(Key)));
		string text2 = string.Join(", ", mouseDisplayNames.Values);
		acceptableKeys = ((BaseUnityPlugin)this).Config.Bind<string>("Help", "AcceptableKeys", text + ", " + text2, "List of all acceptable key/mouse names for keybinds (read-only).");
		foreach (string actionName in actionNames)
		{
			string text3 = "";
			InputAction val = actions.FindAction(actionName, true);
			if (val != null)
			{
				Enumerator<InputBinding> enumerator3 = val.bindings.GetEnumerator();
				try
				{
					while (enumerator3.MoveNext())
					{
						InputBinding current3 = enumerator3.Current;
						if (((InputBinding)(ref current3)).effectivePath == null)
						{
							continue;
						}
						if (((InputBinding)(ref current3)).effectivePath.StartsWith("<Keyboard>/"))
						{
							string text4 = ((InputBinding)(ref current3)).effectivePath.Substring("<Keyboard>/".Length);
							if (text4.StartsWith("digit") && text4.Length == 6)
							{
								int num = text4[5] - 48;
								if (num >= 0 && num <= 9)
								{
									text4 = $"Digit{num}";
								}
							}
							text3 = text4;
							((BaseUnityPlugin)this).Logger.LogInfo((object)("Default for " + actionName + ": " + text3 + " from binding " + ((InputBinding)(ref current3)).effectivePath));
							break;
						}
						if (((InputBinding)(ref current3)).effectivePath.StartsWith("<Mouse>/"))
						{
							string text5 = ((InputBinding)(ref current3)).effectivePath.Substring("<Mouse>/".Length);
							text3 = (mouseDisplayNames.ContainsKey(text5) ? mouseDisplayNames[text5] : text5);
							((BaseUnityPlugin)this).Logger.LogInfo((object)("Default for " + actionName + ": " + text3 + " from binding " + ((InputBinding)(ref current3)).effectivePath));
							break;
						}
					}
				}
				finally
				{
					((IDisposable)enumerator3).Dispose();
				}
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Config default for " + actionName + ": " + text3));
			ConfigEntry<string> val2 = ((BaseUnityPlugin)this).Config.Bind<string>("Keybinds", actionName + "Bind", text3, "Key or mouse button to bind " + actionName);
			keybindConfigs[actionName] = val2;
			if (!string.IsNullOrEmpty(val2.Value))
			{
				ApplyRebind(actionName, val2.Value);
			}
		}
	}

	private void Update()
	{
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_0093: 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)
		//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_017b: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_0126: Unknown result type (might be due to invalid IL or missing references)
		if (((ButtonControl)Keyboard.current.f10Key).wasPressedThisFrame)
		{
			showUI = !showUI;
		}
		if (listeningFor == null)
		{
			return;
		}
		if (((ButtonControl)Keyboard.current.escapeKey).wasPressedThisFrame)
		{
			keybindConfigs[listeningFor].Value = "";
			RemoveRebind(listeningFor);
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Removed keybind for " + listeningFor));
			listeningFor = null;
			return;
		}
		Enumerator<KeyControl> enumerator = Keyboard.current.allKeys.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				KeyControl current = enumerator.Current;
				if (!((ButtonControl)current).wasPressedThisFrame)
				{
					continue;
				}
				Key keyCode;
				foreach (KeyValuePair<string, ConfigEntry<string>> keybindConfig in keybindConfigs)
				{
					if (keybindConfig.Key != listeningFor)
					{
						string value = keybindConfig.Value.Value;
						keyCode = current.keyCode;
						if (value == ((object)(Key)(ref keyCode)).ToString() && !string.IsNullOrEmpty(keybindConfig.Value.Value))
						{
							((BaseUnityPlugin)this).Logger.LogWarning((object)$"Key {current.keyCode} is already bound to {keybindConfig.Key}. Unbind it first.");
							listeningFor = null;
							return;
						}
					}
				}
				((BaseUnityPlugin)this).Logger.LogInfo((object)$"Rebinding {listeningFor} to {current.keyCode}");
				ConfigEntry<string> obj = keybindConfigs[listeningFor];
				keyCode = current.keyCode;
				obj.Value = ((object)(Key)(ref keyCode)).ToString();
				string actionName = listeningFor;
				keyCode = current.keyCode;
				ApplyRebind(actionName, ((object)(Key)(ref keyCode)).ToString());
				listeningFor = null;
				break;
			}
		}
		finally
		{
			((IDisposable)enumerator).Dispose();
		}
		for (int i = 0; i < mouseButtons.Length; i++)
		{
			ButtonControl mouseControl = GetMouseControl(mouseButtons[i]);
			if (mouseControl == null || !mouseControl.wasPressedThisFrame)
			{
				continue;
			}
			string text = (mouseDisplayNames.ContainsKey(mouseButtons[i]) ? mouseDisplayNames[mouseButtons[i]] : mouseButtons[i]);
			foreach (KeyValuePair<string, ConfigEntry<string>> keybindConfig2 in keybindConfigs)
			{
				if (keybindConfig2.Key != listeningFor && keybindConfig2.Value.Value == text && !string.IsNullOrEmpty(keybindConfig2.Value.Value))
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)("Mouse button " + text + " is already bound to " + keybindConfig2.Key + ". Unbind it first."));
					listeningFor = null;
					return;
				}
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Rebinding " + listeningFor + " to " + text));
			keybindConfigs[listeningFor].Value = text;
			ApplyRebind(listeningFor, text);
			listeningFor = null;
			break;
		}
	}

	private ButtonControl GetMouseControl(string buttonName)
	{
		return (ButtonControl)(buttonName switch
		{
			"leftButton" => Mouse.current.leftButton, 
			"rightButton" => Mouse.current.rightButton, 
			"middleButton" => Mouse.current.middleButton, 
			"backButton" => Mouse.current.backButton, 
			"forwardButton" => Mouse.current.forwardButton, 
			"press" => ((Pointer)Mouse.current).press, 
			_ => null, 
		});
	}

	private void OnGUI()
	{
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_006c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0071: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bf: Invalid comparison between Unknown and I4
		//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c4: Invalid comparison between Unknown and I4
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cc: Expected I4, but got Unknown
		if (!showUI)
		{
			return;
		}
		GUILayout.BeginArea(new Rect(20f, 20f, 400f, 650f), "Keybinds", GUI.skin.window);
		GUILayout.Label("Change any keybind below:", Array.Empty<GUILayoutOption>());
		scrollPos = GUILayout.BeginScrollView(scrollPos, (GUILayoutOption[])(object)new GUILayoutOption[2]
		{
			GUILayout.Width(380f),
			GUILayout.Height(550f)
		});
		foreach (string actionName in actionNames)
		{
			string text = keybindConfigs[actionName].Value;
			Key result;
			if (mouseDisplayNames.ContainsValue(text))
			{
				text = text;
			}
			else if (Enum.TryParse<Key>(text, out result))
			{
				text = (((int)result < 50 || (int)result > 49) ? ((object)(Key)(ref result)).ToString() : (result - 50).ToString());
			}
			else if (string.IsNullOrEmpty(text))
			{
				text = "None";
			}
			GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
			GUILayout.Label(actionName + ": " + text, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(250f) });
			if (GUILayout.Button("Change", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(100f) }))
			{
				listeningFor = actionName;
			}
			GUILayout.EndHorizontal();
		}
		GUILayout.EndScrollView();
		GUILayout.Space(10f);
		GUILayout.Label("Press F10 to toggle this menu.", Array.Empty<GUILayoutOption>());
		GUILayout.EndArea();
		if (listeningFor != null)
		{
			GUI.Label(new Rect(20f, 680f, 400f, 30f), "Press a key or mouse button to rebind " + listeningFor + "... (Escape to remove)");
		}
	}

	private void ApplyRebind(string actionName, string newBind)
	{
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: Invalid comparison between Unknown and I4
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Invalid comparison between Unknown and I4
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Expected I4, but got Unknown
		InputAction val = actions.FindAction(actionName, true);
		if (val != null)
		{
			string text = null;
			if (Enum.TryParse<Key>(newBind, out Key result))
			{
				string text2 = ((object)(Key)(ref result)).ToString().ToLower();
				if ((int)result >= 50 && (int)result <= 49)
				{
					text2 = $"digit{result - 50}";
				}
				text = "<Keyboard>/" + text2;
			}
			else if (mouseDisplayNames.ContainsValue(newBind))
			{
				string text3 = null;
				foreach (KeyValuePair<string, string> mouseDisplayName in mouseDisplayNames)
				{
					if (mouseDisplayName.Value == newBind)
					{
						text3 = mouseDisplayName.Key;
						break;
					}
				}
				if (text3 != null)
				{
					text = "<Mouse>/" + text3;
				}
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Trying to rebind " + actionName + " to " + text + " (from " + newBind + ")"));
			if (!string.IsNullOrEmpty(text))
			{
				InputActionRebindingExtensions.ApplyBindingOverride(val, text, (string)null, (string)null);
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Rebound " + actionName + " to " + text));
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("Invalid bind: " + newBind));
			}
		}
		else
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not find action: " + actionName));
		}
	}

	private void RemoveRebind(string actionName)
	{
		InputAction val = actions.FindAction(actionName, true);
		if (val != null)
		{
			InputActionRebindingExtensions.ApplyBindingOverride(val, string.Empty, (string)null, (string)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Removed binding override for " + actionName));
		}
	}
}
namespace BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}