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 GameNetcodeStuff;
using HarmonyLib;
using LethalRebinding;
using LethalRebinding.Utilities;
using MelonLoader;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
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: MelonInfo(typeof(Plugin), "LethalRebinding", "1.0.0", "Bobbie (Cyconi)", null)]
[assembly: MelonGame(null, "Lethal Company")]
[assembly: MelonColor(1, 255, 0, 255)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("LethalRebinding")]
[assembly: AssemblyConfiguration("Debug")]
[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
{
	public class Plugin : MelonMod
	{
		public override void OnApplicationStart()
		{
			MelonLogger.Msg("LethalRebinding MelonLoader Port by Cyconi");
			MelonLogger.Msg("original - https://github.com/legoandmars/LethalRebinding");
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
		}
	}
}
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_006f: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = Object.Instantiate<GameObject>(((Component)((Component)settingTemplate).transform.parent).gameObject);
			val.transform.SetParent(parent, false);
			SettingsOption componentInChildren = val.GetComponentInChildren<SettingsOption>();
			TextMeshProUGUI componentInChildren2 = val.GetComponentInChildren<TextMeshProUGUI>();
			InputActionReference rebindableAction = InputActionReference.Create(action);
			componentInChildren.rebindableAction = rebindableAction;
			((TMP_Text)componentInChildren2).SetText(action.name + ":", true);
			val.transform.localPosition = new Vector3(225f, (float)(-(index * 20)), 0f);
			return val;
		}

		internal static GameObject CreateScrollRect(GameObject template, Image scrollBackgroundImage)
		{
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Expected O, but got Unknown
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: 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)
			//IL_00be: Expected O, but got Unknown
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Expected O, but got Unknown
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_0166: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01aa: Expected O, but got Unknown
			//IL_01fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0212: Unknown result type (might be due to invalid IL or missing references)
			//IL_0229: Unknown result type (might be due to invalid IL or missing references)
			//IL_0240: Unknown result type (might be due to invalid IL or missing references)
			//IL_0250: Unknown result type (might be due to invalid IL or missing references)
			//IL_0257: Expected O, but got Unknown
			//IL_0273: Unknown result type (might be due to invalid IL or missing references)
			//IL_028a: Unknown result type (might be due to invalid IL or missing references)
			//IL_029a: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a1: Expected O, but got Unknown
			//IL_02bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d4: Unknown result type (might be due to invalid IL or missing references)
			Transform val = Object.Instantiate<Transform>(template.transform.GetChild(0));
			val.SetParent(template.transform, false);
			((Component)val).transform.localPosition = new Vector3(-152.7989f, 36.4002f, 0f);
			((TMP_Text)((Component)val).GetComponent<TextMeshProUGUI>()).SetText("BINDINGS", true);
			GameObject val2 = new GameObject("Scroll View");
			ScrollRect val3 = val2.AddComponent<ScrollRect>();
			val2.transform.SetParent(template.transform, false);
			val2.transform.localPosition = new Vector3(-113f, -50f, 0f);
			val2.GetComponent<RectTransform>().sizeDelta = new Vector2(245f, 150f);
			GameObject val4 = new GameObject("Viewport");
			ApplyParentSize(val4, val2.transform);
			val4.GetComponent<RectTransform>().pivot = new Vector2(0f, 1f);
			Image val5 = val4.AddComponent<Image>();
			val4.AddComponent<Mask>();
			((Graphic)val5).color = new Color(0f, 0f, 0f, 0.002f);
			GameObject val6 = new GameObject("Content");
			ApplyParentSize(val6, val4.transform);
			RectTransform component = val6.GetComponent<RectTransform>();
			component.pivot = new Vector2(0f, 1f);
			((Component)component).transform.localPosition = new Vector3(14f, 0f, 0f);
			ContentSizeFitter val7 = val6.AddComponent<ContentSizeFitter>();
			val7.verticalFit = (FitMode)1;
			VerticalLayoutGroup val8 = val6.AddComponent<VerticalLayoutGroup>();
			((HorizontalOrVerticalLayoutGroup)val8).childControlHeight = false;
			((HorizontalOrVerticalLayoutGroup)val8).childControlWidth = false;
			GameObject val9 = new GameObject("Scrollbar Vertical");
			Scrollbar val10 = val9.AddComponent<Scrollbar>();
			Image val11 = val9.AddComponent<Image>();
			val11.sprite = scrollBackgroundImage.sprite;
			((Graphic)val11).material = ((Graphic)scrollBackgroundImage).material;
			ApplyParentSize(val9, ((Component)val3).transform);
			RectTransform component2 = val9.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 val12 = new GameObject("Sliding area");
			RectTransform val13 = ApplyParentSize(val12, val9.transform);
			val13.offsetMin = new Vector2(10f, 10f);
			val13.offsetMax = new Vector2(-10f, -10f);
			GameObject val14 = new GameObject("Handle");
			RectTransform val15 = ApplyParentSize(val14, val12.transform);
			val15.offsetMin = new Vector2(-10f, -10f);
			val15.offsetMax = new Vector2(10f, 10f);
			Image targetGraphic = val14.AddComponent<Image>();
			val10.handleRect = val15;
			((Selectable)val10).targetGraphic = (Graphic)(object)targetGraphic;
			val10.direction = (Direction)2;
			val3.content = component;
			val3.viewport = val4.GetComponent<RectTransform>();
			val3.horizontal = false;
			val3.vertical = true;
			val3.verticalScrollbar = val10;
			val3.verticalScrollbarVisibility = (ScrollbarVisibility)2;
			return val6;
		}

		private static RectTransform ApplyParentSize(GameObject uiElement, Transform parent)
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: 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_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: 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)
			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_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: 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();
			PlayerInput playerInput = __instance.playerInput;
			List<InputAction> list2 = ((IEnumerable<InputAction>)playerInput.actions).ToList();
			GameObject gameObject = ((Component)((Component)val2).transform.parent.parent).gameObject;
			GameObject val3 = SettingsUtilities.CreateScrollRect(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 flag = code.opcode == OpCodes.Ldstr && CodeInstructionExtensions.OperandIs(code, (object)"Mouse");
				bool flag2 = CodeInstructionExtensions.Calls(code, typeof(RebindingOperation).GetMethod("WithControlsExcluding"));
				return !flag && !flag2;
			});
		}
	}
}