Decompiled source of BetterControls v0.1.4

BetterControls.dll

Decompiled 7 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
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: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("BetterControls")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("0.1.4.0")]
[assembly: AssemblyInformationalVersion("0.1.4")]
[assembly: AssemblyProduct("BetterControls")]
[assembly: AssemblyTitle("BetterControls")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.4.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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace DysonSphereProgram.Modding.BetterControls
{
	[BepInPlugin("com.zach.BetterControls", "BetterControls", "0.1.3")]
	[BepInProcess("DSPGAME.exe")]
	public class Plugin : BaseUnityPlugin
	{
		public const string GUID = "com.zach.BetterControls";

		public const string NAME = "BetterControls";

		public const string VERSION = "0.1.3";

		private Harmony _harmony;

		internal static ManualLogSource Log;

		private void Awake()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			_harmony = new Harmony("com.zach.BetterControls");
			_harmony.PatchAll(typeof(BetterControls));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"BetterControls Awake() called");
		}

		private void OnDestroy()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"BetterControls OnDestroy() called");
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Log = null;
		}
	}
	public static class KeyIds
	{
		public const int Jump = 8;

		public const int Takeoff = 9;

		public const int ClosePanel0 = 32;

		public const int Inventory = 34;

		public const int ClosePanel1 = 33;

		public const int GameMenu = 47;
	}
	internal class BetterControls
	{
		private static bool[] conflicts = null;

		private static Color conflictColor = Color.red;

		private static Dictionary<int, int> indexes = new Dictionary<int, int>();

		private static Dictionary<int, int> groups = new Dictionary<int, int>();

		private static Dictionary<int, int> linked = new Dictionary<int, int>();

		private static CombineKey getKey(CombineKey builtin, CombineKey overrider)
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			return ((CombineKey)(ref overrider)).IsNull() ? builtin : overrider;
		}

		public static bool inventoryKeyDown()
		{
			if (!((CombineKey)(ref VFInput.override_keys[34])).IsNull())
			{
				return ((CombineKey)(ref VFInput.override_keys[34])).GetKeyDown();
			}
			return VFInput.noModifier && Input.GetKeyDown((KeyCode)101);
		}

		public static bool menuKeyDown()
		{
			if (!((CombineKey)(ref VFInput.override_keys[47])).IsNull())
			{
				return ((CombineKey)(ref VFInput.override_keys[47])).GetKeyDown();
			}
			return VFInput.noModifier && Input.GetKeyDown((KeyCode)27);
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(UIGame), "_OnUpdate")]
		private static IEnumerable<CodeInstruction> PatchInventoryKey(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Expected O, but got Unknown
			return new CodeMatcher(instructions, generator).MatchForward(false, (CodeMatch[])(object)new CodeMatch[2]
			{
				new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction ci) => CodeInstructionExtensions.LoadsConstant(ci, (Enum)(object)(KeyCode)101)), (string)null),
				new CodeMatch((OpCode?)OpCodes.Call, (object)AccessTools.Method(typeof(Input), "GetKeyDown", new Type[1] { typeof(KeyCode) }, (Type[])null), (string)null)
			}).RemoveInstruction().SetOperandAndAdvance((object)AccessTools.Method(typeof(BetterControls), "inventoryKeyDown", (Type[])null, (Type[])null))
				.InstructionEnumeration();
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(VFInput), "OnUpdate")]
		private static void OnUpdate()
		{
			VFInput.escape = menuKeyDown() && !VFInput.inputing;
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(UIOptionWindow), "_OnOpen")]
		private static void _OnOpen(ref UIOptionWindow __instance)
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_0177: Unknown result type (might be due to invalid IL or missing references)
			//IL_017c: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0308: Unknown result type (might be due to invalid IL or missing references)
			//IL_030f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0316: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_033b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0340: Unknown result type (might be due to invalid IL or missing references)
			//IL_0347: Unknown result type (might be due to invalid IL or missing references)
			//IL_034e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0200: Unknown result type (might be due to invalid IL or missing references)
			BuiltinKey[] builtinKeys = DSPGame.key.builtinKeys;
			CombineKey[] overrideKeys = __instance.tempOption.overrideKeys;
			int num = builtinKeys.Length;
			CombineKey[] array = (CombineKey[])(object)new CombineKey[num];
			for (int i = 0; i < num; i++)
			{
				BuiltinKey val = builtinKeys[i];
				array[i] = getKey(val.key, overrideKeys[val.id]);
				indexes[val.id] = i;
			}
			for (int j = 0; j < num - 1; j++)
			{
				CombineKey key = builtinKeys[j].key;
				if (groups.ContainsKey(j))
				{
					continue;
				}
				groups[j] = -1;
				for (int k = j + 1; k < num; k++)
				{
					CombineKey key2 = builtinKeys[k].key;
					if (((CombineKey)(ref key)).IsEquals(key2.keyCode, key2.modifier, key2.noneKey))
					{
						groups[j] = j;
						groups[k] = j;
					}
				}
			}
			if (!groups.ContainsKey(num - 1))
			{
				groups[num - 1] = -1;
			}
			conflicts = new bool[num];
			for (int l = 0; l < num - 1; l++)
			{
				CombineKey val2 = array[l];
				if (conflicts[l])
				{
					continue;
				}
				for (int m = l + 1; m < num; m++)
				{
					CombineKey val3 = array[m];
					if ((((BuiltinKey)(ref builtinKeys[l])).conflictKeyGroup & ((BuiltinKey)(ref builtinKeys[m])).conflictKeyGroup) > 0 && (groups[l] != groups[m] || groups[l] < 0) && ((CombineKey)(ref val2)).IsEquals(val3.keyCode, val3.modifier, val3.noneKey))
					{
						conflicts[l] = true;
						conflicts[m] = true;
					}
				}
			}
			int[] array2 = new int[6]
			{
				indexes[9],
				indexes[8],
				indexes[32],
				indexes[34],
				indexes[33],
				indexes[47]
			};
			for (int n = 0; n < array2.Length; n += 2)
			{
				linked[array2[n]] = array2[n + 1];
				linked[array2[n + 1]] = array2[n];
				CombineKey val4 = array[array2[n + 1]];
				if (!((CombineKey)(ref array[array2[n]])).IsEquals(val4.keyCode, val4.modifier, val4.noneKey))
				{
					__instance.keyEntries[array2[n]].OverrideKey(__instance.tempOption, val4.keyCode, val4.modifier, val4.noneKey);
				}
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(BuiltinKey), "IsKeyVaild")]
		private static bool IsKeyValid(ref bool __result)
		{
			__result = true;
			return false;
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(UIKeyEntry), "SetEntry")]
		private static void SetEntry(ref UIKeyEntry __instance, int _index, BuiltinKey _builtinKey, UIOptionWindow _optionWin)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			if (_builtinKey.id < 71 || 78 < _builtinKey.id)
			{
				((Component)__instance.setTheKeyInput).gameObject.SetActive(true);
				((Component)__instance.setTheKeyToggle).gameObject.SetActive(true);
				((Component)__instance.setDefaultUIButton).gameObject.SetActive(true);
				((Component)__instance.setNoneKeyUIButton).gameObject.SetActive(true);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(UIKeyEntry), "Update")]
		private static void Update(ref UIKeyEntry __instance)
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			if (conflicts[indexes[__instance.builtinKey.id]])
			{
				((Graphic)__instance.keyText).color = Color.red;
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(UIKeyEntry), "OnSetNoneKeyClick")]
		private static bool OnSetNoneKeyClick(ref UIKeyEntry __instance)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			__instance.OverrideKey(__instance.optionWin.GetTempOption(), 0, (byte)0, true);
			return false;
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(UIKeyEntry), "OverrideKey")]
		private static bool OverrideKey(ref UIKeyEntry __instance, GameOption tempOption, int keyCode, byte modifier, bool noneKey)
		{
			//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_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0137: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_0468: Unknown result type (might be due to invalid IL or missing references)
			Plugin.Log.LogInfo((object)"A");
			if (__instance.inputUIButton._isPointerEnter && Input.GetKeyDown((KeyCode)323))
			{
				__instance.nextNotOn = true;
			}
			Plugin.Log.LogInfo((object)"B");
			int id = __instance.builtinKey.id;
			int num = indexes[id];
			CombineKey key = __instance.builtinKey.key;
			BuiltinKey[] builtinKeys = DSPGame.key.builtinKeys;
			CombineKey[] overrideKeys = tempOption.overrideKeys;
			int num2 = builtinKeys.Length;
			CombineKey[] array = (CombineKey[])(object)new CombineKey[num2];
			for (int i = 0; i < num2; i++)
			{
				array[i] = getKey(builtinKeys[i].key, overrideKeys[builtinKeys[i].id]);
			}
			Plugin.Log.LogInfo((object)"C");
			int keyCode2 = array[num].keyCode;
			byte modifier2 = array[num].modifier;
			bool noneKey2 = array[num].noneKey;
			int num3 = keyCode;
			byte b = modifier;
			bool flag = noneKey;
			if (keyCode == 0 && modifier == 0 && !noneKey)
			{
				num3 = key.keyCode;
				b = key.modifier;
				flag = key.noneKey;
			}
			if (keyCode2 == num3 && modifier2 == b && noneKey2 == flag)
			{
				__instance.inputUIButton.highlighted = false;
				return false;
			}
			Plugin.Log.LogInfo((object)"D");
			if (((CombineKey)(ref key)).IsEquals(num3, b, flag))
			{
				((CombineKey)(ref overrideKeys[id])).Reset();
			}
			else
			{
				overrideKeys[id].keyCode = num3;
				overrideKeys[id].modifier = b;
				overrideKeys[id].noneKey = flag;
			}
			__instance.optionWin.SetTempOption(tempOption);
			__instance.inputUIButton.highlighted = false;
			Plugin.Log.LogInfo((object)"E");
			int num4 = -1;
			for (int j = 0; j < num2; j++)
			{
				if (j != num && conflicts[j] && (((BuiltinKey)(ref builtinKeys[j])).conflictKeyGroup & ((BuiltinKey)(ref builtinKeys[num])).conflictKeyGroup) > 0 && (groups[j] != groups[num] || groups[j] < 0) && ((CombineKey)(ref array[j])).IsEquals(keyCode2, modifier2, noneKey2))
				{
					if (num4 > -1 && (!linked.ContainsKey(num4) || linked[num4] != j))
					{
						num4 = -1;
						break;
					}
					num4 = j;
				}
			}
			Plugin.Log.LogInfo((object)"F");
			if (num4 > -1)
			{
				conflicts[num4] = false;
				if (linked.ContainsKey(num4))
				{
					conflicts[linked[num4]] = false;
				}
			}
			conflicts[num] = false;
			Plugin.Log.LogInfo((object)"G");
			for (int k = 0; k < num2; k++)
			{
				if (k != num && (((BuiltinKey)(ref builtinKeys[k])).conflictKeyGroup & ((BuiltinKey)(ref builtinKeys[num])).conflictKeyGroup) > 0 && (groups[k] != groups[num] || groups[k] < 0) && ((CombineKey)(ref array[k])).IsEquals(num3, b, flag))
				{
					conflicts[k] = true;
					conflicts[num] = true;
					if (linked.ContainsKey(k))
					{
						conflicts[linked[k]] = true;
					}
					__instance.StartConflictText(k);
					__instance.setTheKeyToggle.isOn = false;
					((Component)__instance.waitingText).gameObject.SetActive(false);
					break;
				}
			}
			Plugin.Log.LogInfo((object)"ASDF");
			if (linked.ContainsKey(num))
			{
				__instance.optionWin.keyEntries[linked[num]].OverrideKey(tempOption, num3, b, flag);
			}
			Plugin.Log.LogInfo((object)"BSDF");
			return false;
		}
	}
}