Decompiled source of LethalRebinding v1.0.0

LethalRebinding.dll

Decompiled a year ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Threading.Tasks;
using BepInEx;
using GameNetcodeStuff;
using HarmonyLib;
using LethalRebinding.Utilities;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("LethalRebinding")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Allows key rebinding in Lethal Company")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("LethalRebinding")]
[assembly: AssemblyTitle("LethalRebinding")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace LethalRebinding
{
	[BepInPlugin("LethalRebinding", "LethalRebinding", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin LethalRebinding is loaded!");
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "LethalRebinding";

		public const string PLUGIN_NAME = "LethalRebinding";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace LethalRebinding.Utilities
{
	internal static class DisplayUtilities
	{
		private static Dictionary<string, string> _keyNameReplacements = new Dictionary<string, string>
		{
			{ "LEFTBUTTON", "LMB" },
			{ "RIGHTBUTTON", "RMB" },
			{ "LEFTCTRL", "LCTRL" },
			{ "RIGHTCTRL", "LCTRL" },
			{ "LEFTSHIFT", "LSHIFT" },
			{ "RIGHTSHIFT", "RSHIFT" },
			{ "LEFTALT", "ALT" },
			{ "RIGHTALT", "RALT" }
		};

		internal static string LocalizeKey(InputAction action)
		{
			string text = ((object)action).ToString().Split(new char[1] { '/' })[^1].ToUpper().Replace("[", "").Replace("]", "");
			if (_keyNameReplacements.ContainsKey(text))
			{
				text = _keyNameReplacements[text];
			}
			return text.ToUpper();
		}
	}
	internal static class SettingsUtilities
	{
		internal static GameObject GetSettingForInputAction(InputAction action, SettingsOption settingTemplate, Transform parent, int index = 0)
		{
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			GameObject obj = Object.Instantiate<GameObject>(((Component)((Component)settingTemplate).transform.parent).gameObject);
			obj.transform.SetParent(parent, false);
			SettingsOption componentInChildren = obj.GetComponentInChildren<SettingsOption>();
			TextMeshProUGUI componentInChildren2 = obj.GetComponentInChildren<TextMeshProUGUI>();
			InputActionReference rebindableAction = InputActionReference.Create(action);
			componentInChildren.rebindableAction = rebindableAction;
			((TMP_Text)componentInChildren2).SetText(action.name + ":", true);
			obj.transform.localPosition = new Vector3(225f, (float)(-(index * 20)), 0f);
			return obj;
		}

		internal static GameObject CreateScrollRect(GameObject template, Image scrollBackgroundImage)
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Expected O, but got Unknown
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Expected O, but got Unknown
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Expected O, but got Unknown
			//IL_011e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_014f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_0165: Unknown result type (might be due to invalid IL or missing references)
			//IL_017d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0184: Expected O, but got Unknown
			//IL_01cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0209: Unknown result type (might be due to invalid IL or missing references)
			//IL_0218: Unknown result type (might be due to invalid IL or missing references)
			//IL_021f: Expected O, but got Unknown
			//IL_0238: Unknown result type (might be due to invalid IL or missing references)
			//IL_024c: Unknown result type (might be due to invalid IL or missing references)
			//IL_025b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0260: Unknown result type (might be due to invalid IL or missing references)
			//IL_026d: Expected O, but got Unknown
			//IL_027b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0291: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ed: Expected O, but got Unknown
			Transform obj = Object.Instantiate<Transform>(template.transform.GetChild(0));
			obj.SetParent(template.transform, false);
			((Component)obj).transform.localPosition = new Vector3(-152.7989f, 36.4002f, 0f);
			((TMP_Text)((Component)obj).GetComponent<TextMeshProUGUI>()).SetText("BINDINGS", true);
			GameObject val = new GameObject("Scroll View");
			ScrollRect val2 = val.AddComponent<ScrollRect>();
			val.transform.SetParent(template.transform, false);
			val.transform.localPosition = new Vector3(-113f, -50f, 0f);
			val.GetComponent<RectTransform>().sizeDelta = new Vector2(245f, 150f);
			GameObject val3 = new GameObject("Viewport");
			ApplyParentSize(val3, val.transform);
			val3.GetComponent<RectTransform>().pivot = new Vector2(0f, 1f);
			Image obj2 = val3.AddComponent<Image>();
			val3.AddComponent<Mask>();
			((Graphic)obj2).color = new Color(0f, 0f, 0f, 0.002f);
			GameObject val4 = new GameObject("Content");
			ApplyParentSize(val4, val3.transform);
			RectTransform component = val4.GetComponent<RectTransform>();
			component.pivot = new Vector2(0f, 1f);
			((Component)component).transform.localPosition = new Vector3(14f, 0f, 0f);
			val4.AddComponent<ContentSizeFitter>().verticalFit = (FitMode)1;
			VerticalLayoutGroup obj3 = val4.AddComponent<VerticalLayoutGroup>();
			((HorizontalOrVerticalLayoutGroup)obj3).childControlHeight = false;
			((HorizontalOrVerticalLayoutGroup)obj3).childControlWidth = false;
			GameObject val5 = new GameObject("Scrollbar Vertical");
			Scrollbar val6 = val5.AddComponent<Scrollbar>();
			Image obj4 = val5.AddComponent<Image>();
			obj4.sprite = scrollBackgroundImage.sprite;
			((Graphic)obj4).material = ((Graphic)scrollBackgroundImage).material;
			ApplyParentSize(val5, ((Component)val2).transform);
			RectTransform component2 = val5.GetComponent<RectTransform>();
			component2.pivot = new Vector2(1f, 1f);
			component2.sizeDelta = new Vector2(10f, 0f);
			component2.anchorMin = new Vector2(1f, 0f);
			component2.anchorMax = new Vector2(1f, 1f);
			GameObject val7 = new GameObject("Sliding area");
			RectTransform obj5 = ApplyParentSize(val7, val5.transform);
			obj5.offsetMin = new Vector2(10f, 10f);
			obj5.offsetMax = new Vector2(-10f, -10f);
			GameObject val8 = new GameObject("Handle");
			RectTransform val9 = ApplyParentSize(val8, val7.transform);
			val9.offsetMin = new Vector2(-10f, -10f);
			val9.offsetMax = new Vector2(10f, 10f);
			Image targetGraphic = val8.AddComponent<Image>();
			val6.handleRect = val9;
			((Selectable)val6).targetGraphic = (Graphic)(object)targetGraphic;
			val6.direction = (Direction)2;
			val2.content = component;
			val2.viewport = val3.GetComponent<RectTransform>();
			val2.horizontal = false;
			val2.vertical = true;
			val2.verticalScrollbar = val6;
			val2.verticalScrollbarVisibility = (ScrollbarVisibility)2;
			return val4;
		}

		private static RectTransform ApplyParentSize(GameObject uiElement, Transform parent)
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			RectTransform val = uiElement.GetComponent<RectTransform>();
			if ((Object)(object)val == (Object)null)
			{
				val = uiElement.AddComponent<RectTransform>();
			}
			((Transform)val).SetParent(parent);
			val.anchorMin = new Vector2(0f, 0f);
			val.anchorMax = new Vector2(1f, 1f);
			val.pivot = new Vector2(0.5f, 0.5f);
			RectTransform obj = val;
			Vector2 offsetMin = (val.offsetMax = Vector2.zero);
			obj.offsetMin = offsetMin;
			((Transform)val).localRotation = Quaternion.identity;
			((Transform)val).localScale = Vector3.one;
			((Transform)val).localPosition = Vector3.zero;
			return val;
		}
	}
}
namespace LethalRebinding.Patches
{
	[HarmonyPatch(typeof(HUDManager))]
	internal class HUDManagerPatch
	{
		private static List<HUDManager>? _instances = new List<HUDManager>();

		private static Dictionary<string, string> _actionNameByHudText = new Dictionary<string, string>
		{
			{ "Sprint:", "Sprint" },
			{ "Scan :", "PingScan" }
		};

		public static void ApplyNewBindings(string bindings)
		{
			foreach (HUDManager instance in _instances)
			{
				if ((Object)(object)instance == (Object)null)
				{
					continue;
				}
				InputActionRebindingExtensions.LoadBindingOverridesFromJson((IInputActionCollection2)(object)instance.playerActions, bindings, true);
				TextMeshProUGUI[] controlTipLines = instance.controlTipLines;
				foreach (TextMeshProUGUI val in controlTipLines)
				{
					if ((Object)(object)val == (Object)null)
					{
						continue;
					}
					string text = ((TMP_Text)val).text.Split(new char[1] { ':' })[0] + ":";
					if (_actionNameByHudText.TryGetValue(text, out var value) || text.StartsWith("Drop"))
					{
						if (text.StartsWith("Drop"))
						{
							value = "Discard";
						}
						InputAction val2 = instance.playerActions.FindAction(value, false);
						if (val2 != null)
						{
							((TMP_Text)val).text = text + " [" + DisplayUtilities.LocalizeKey(val2) + "]";
						}
					}
				}
			}
		}

		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		private static void Postfix(HUDManager __instance)
		{
			_instances.Add(__instance);
			ApplyNewBindings(IngamePlayerSettings.Instance.settings.keyBindings);
		}

		[HarmonyPatch("ChangeControlTip")]
		[HarmonyPostfix]
		private static void PostfixControlTip()
		{
			ApplyNewBindings(IngamePlayerSettings.Instance.settings.keyBindings);
		}

		[HarmonyPatch("ChangeControlTipMultiple")]
		[HarmonyPostfix]
		private static void PostfixControlTipMultiple()
		{
			ApplyNewBindings(IngamePlayerSettings.Instance.settings.keyBindings);
		}
	}
	[HarmonyPatch(typeof(IngamePlayerSettings), "UpdateGameToMatchSettings")]
	internal class IngamePlayerSettingsPatch
	{
		private static List<string> _whitelistedValueActions = new List<string> { "Sprint", "Interact" };

		private static void Prefix(IngamePlayerSettings __instance)
		{
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Invalid comparison between Unknown and I4
			SettingsOption[] source = Object.FindObjectsOfType<SettingsOption>(true);
			List<SettingsOption> list = source.Where((SettingsOption x) => (Object)(object)x != (Object)null && (int)x.optionType == 6).ToList();
			SettingsOption val = source.Where((SettingsOption x) => (Object)(object)x != (Object)null && (int)x.optionType == 8).ToList().FirstOrDefault();
			if (list.Count != 1 || (Object)(object)val == (Object)null)
			{
				return;
			}
			Image component = ((Component)((Component)val).transform.GetChild(0)).GetComponent<Image>();
			SettingsOption val2 = list.First();
			List<InputAction> list2 = ((IEnumerable<InputAction>)__instance.playerInput.actions).ToList();
			GameObject val3 = SettingsUtilities.CreateScrollRect(((Component)((Component)val2).transform.parent.parent).gameObject, component);
			for (int i = 0; i < list2.Count; i++)
			{
				InputAction val4 = list2[i];
				if (((int)val4.type == 1 || _whitelistedValueActions.Contains(val4.name)) && !(val4.name == "SpeedCheat"))
				{
					SettingsUtilities.GetSettingForInputAction(val4, val2, val3.transform, i);
				}
			}
			Debug.Log((object)InputActionRebindingExtensions.SaveBindingOverridesAsJson((IInputActionCollection2)(object)__instance.playerInput.actions));
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB))]
	internal class PlayerControllerBPatch
	{
		private static List<PlayerControllerB>? _instances = new List<PlayerControllerB>();

		private static MethodInfo? _onEnable;

		private static MethodInfo? _onDisable;

		public static void ApplyNewBindings(string bindings)
		{
			foreach (PlayerControllerB instance in _instances)
			{
				if (!((Object)(object)instance == (Object)null))
				{
					InputActionRebindingExtensions.LoadBindingOverridesFromJson((IInputActionCollection2)(object)instance.playerActions, bindings, true);
				}
			}
			Debug.Log((object)"Applying new bindings!");
			Debug.Log((object)bindings);
		}

		[HarmonyPatch("ConnectClientToPlayerObject")]
		[HarmonyPostfix]
		private static void Postfix(PlayerControllerB __instance)
		{
			_instances.Add(__instance);
			if (_onEnable == null)
			{
				_onEnable = AccessTools.Method(typeof(PlayerControllerB), "OnEnable", (Type[])null, (Type[])null);
			}
			if (_onDisable == null)
			{
				_onDisable = AccessTools.Method(typeof(PlayerControllerB), "OnDisable", (Type[])null, (Type[])null);
			}
			ApplyNewBindings(IngamePlayerSettings.Instance.settings.keyBindings);
		}

		[HarmonyPatch("SetHoverTipAndCurrentInteractTrigger")]
		[HarmonyPostfix]
		private static void PostfixCursorTip(PlayerControllerB __instance)
		{
			if (((TMP_Text)__instance.cursorTip).text.Contains("[E]"))
			{
				InputAction action = __instance.playerActions.FindAction("Interact", false);
				((TMP_Text)__instance.cursorTip).text = ((TMP_Text)__instance.cursorTip).text.Replace("[E]", "[" + DisplayUtilities.LocalizeKey(action) + "]");
			}
		}
	}
	[HarmonyPatch(typeof(IngamePlayerSettings), "ResetSettingsToDefault")]
	internal class RebindEventPatch
	{
		private static void Postfix(IngamePlayerSettings __instance)
		{
			PlayerControllerBPatch.ApplyNewBindings(__instance.settings.keyBindings);
			HUDManagerPatch.ApplyNewBindings(__instance.settings.keyBindings);
		}
	}
	[HarmonyPatch(typeof(IngamePlayerSettings), "SaveChangedSettings")]
	internal class RebindEventPatch2
	{
		private static void Postfix(IngamePlayerSettings __instance)
		{
			PlayerControllerBPatch.ApplyNewBindings(__instance.settings.keyBindings);
			HUDManagerPatch.ApplyNewBindings(__instance.settings.keyBindings);
		}
	}
	[HarmonyPatch(typeof(Terminal))]
	internal class TerminalPatch
	{
		[HarmonyPatch("QuitTerminal")]
		[HarmonyPostfix]
		private static void EndPostfix(Terminal __instance)
		{
			PlayerControllerB localPlayerController = GameNetworkManager.Instance.localPlayerController;
			localPlayerController.inTerminalMenu = true;
			DelayEnablingPausing(localPlayerController);
		}

		private static async void DelayEnablingPausing(PlayerControllerB localPlayerController)
		{
			await Task.Delay(100);
			localPlayerController.inTerminalMenu = false;
		}
	}
}
namespace MouseBind.Patches
{
	[HarmonyPatch(typeof(IngamePlayerSettings), "RebindKey")]
	public class AllowMouseBindings
	{
		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> codes)
		{
			return codes.Where(delegate(CodeInstruction code)
			{
				bool num = code.opcode == OpCodes.Ldstr && CodeInstructionExtensions.OperandIs(code, (object)"Mouse");
				bool flag = CodeInstructionExtensions.Calls(code, typeof(RebindingOperation).GetMethod("WithControlsExcluding"));
				return !num && !flag;
			});
		}
	}
}