Decompiled source of Rebindables v1.0.1

Rebindables.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using MonoMod.RuntimeDetour;
using On.RoR2;
using On.RoR2.UI;
using Rewired;
using Rewired.Data;
using RoR2;
using RoR2.UI;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Rebindables")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+92a073609133a40c801e07c71061ab4ac98901f0")]
[assembly: AssemblyProduct("Rebindables")]
[assembly: AssemblyTitle("Rebindables")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
[module: UnverifiableCode]
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 Rebindables
{
	public static class InputBankExtensions
	{
		public static ButtonState GetButtonState(this InputBankTest bank, ModKeybind bind)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			return bind.GetButtonState(bank);
		}
	}
	public class RebindAPI
	{
		internal delegate void orig_RewiredMoment(UserData self);

		[CompilerGenerated]
		private static class <>O
		{
			public static hook_LoadUserProfiles <0>__LoadProfiles;

			public static hook_Start <1>__SPCStart;

			public static hook_Start <2>__IBTStart;
		}

		private static int lastClaimedID = 932;

		public static List<ModKeybind> Keybinds = new List<ModKeybind>();

		public static Dictionary<ModKeybind, InputAction> KeybindActions = new Dictionary<ModKeybind, InputAction>();

		internal static void ApplyHooks()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			//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_003d: Expected O, but got Unknown
			//IL_008e: 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_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Expected O, but got Unknown
			object obj = <>O.<0>__LoadProfiles;
			if (obj == null)
			{
				hook_LoadUserProfiles val = LoadProfiles;
				<>O.<0>__LoadProfiles = val;
				obj = (object)val;
			}
			SaveSystem.LoadUserProfiles += (hook_LoadUserProfiles)obj;
			object obj2 = <>O.<1>__SPCStart;
			if (obj2 == null)
			{
				hook_Start val2 = SPCStart;
				<>O.<1>__SPCStart = val2;
				obj2 = (object)val2;
			}
			SettingsPanelController.Start += (hook_Start)obj2;
			object obj3 = <>O.<2>__IBTStart;
			if (obj3 == null)
			{
				hook_Start val3 = IBTStart;
				<>O.<2>__IBTStart = val3;
				obj3 = (object)val3;
			}
			InputBankTest.Start += (hook_Start)obj3;
			new Hook((MethodBase)typeof(UserData).GetMethod("gLOOAxUFAvrvUufkVjaYyZoeLbLE", (BindingFlags)(-1)), typeof(RebindAPI).GetMethod("RewiredMoment", (BindingFlags)(-1)));
		}

		private static void IBTStart(orig_Start orig, InputBankTest self)
		{
			orig.Invoke(self);
			((Component)self).gameObject.AddComponent<ModdedInputBank>();
		}

		private static void RewiredMoment(orig_RewiredMoment orig, UserData self)
		{
			foreach (ModKeybind keybind in Keybinds)
			{
				self.actions?.Add(KeybindActions[keybind]);
				self.joystickMaps?.FirstOrDefault().actionElementMaps.Add(keybind.jsMap);
				self.keyboardMaps?.FirstOrDefault().actionElementMaps.Add(keybind.kbMap);
			}
			orig(self);
		}

		private static void SPCStart(orig_Start orig, SettingsPanelController self)
		{
			orig.Invoke(self);
			if (!(((Object)self).name == "SettingsSubPanel, Controls (M&KB)") && !(((Object)self).name == "SettingsSubPanel, Controls (Gamepad)"))
			{
				return;
			}
			GameObject gameObject = ((Component)((Component)self).transform.Find("Scroll View").Find("Viewport").Find("VerticalLayout")
				.Find("SettingsEntryButton, Binding (Jump)")).gameObject;
			Transform val = ((Component)self).transform.Find("Scroll View").Find("Viewport").Find("VerticalLayout");
			foreach (ModKeybind keybind in Keybinds)
			{
				GameObject val2 = Object.Instantiate<GameObject>(gameObject, val);
				InputBindingControl component = val2.GetComponent<InputBindingControl>();
				component.actionName = keybind.InternalName;
				component.Awake();
				if (keybind.Position != null)
				{
					int num = FindSiblingIndex(keybind.Position, val);
					if (num != -1)
					{
						val2.transform.SetSiblingIndex(num + 1);
					}
				}
			}
		}

		private static int FindSiblingIndex(string input, Transform target)
		{
			Transform val = target.Find("SettingsEntryButton, Binding (" + input + ")");
			Transform val2 = target.Find("SettingsEntryButton, Bool (" + input + ")");
			Transform val3 = target.Find("SettingsEntryButton, Slider (" + input + ")");
			return ((Object)(object)val != (Object)null) ? val.GetSiblingIndex() : (((Object)(object)val2 != (Object)null) ? val2.GetSiblingIndex() : (((Object)(object)val3 != (Object)null) ? val3.GetSiblingIndex() : (-1)));
		}

		private static void LoadProfiles(orig_LoadUserProfiles orig, SaveSystem self)
		{
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0178: Unknown result type (might be due to invalid IL or missing references)
			//IL_019a: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ab: Unknown result type (might be due to invalid IL or missing references)
			orig.Invoke(self);
			ActionElementMap val = default(ActionElementMap);
			foreach (KeyValuePair<string, UserProfile> loadedUserProfile in self.loadedUserProfiles)
			{
				UserProfile value = loadedUserProfile.Value;
				foreach (ModKeybind bind in Keybinds)
				{
					if (((ControllerMap)value.keyboardMap).AllMaps.All((ActionElementMap x) => x.actionDescriptiveName != bind.kbMap.actionDescriptiveName))
					{
						((ControllerMap)value.keyboardMap).CreateElementMap(bind.kbMap.actionId, bind.kbMap.axisContribution, bind.kbMap.elementIdentifierId, bind.kbMap.elementType, bind.kbMap.axisRange, bind.kbMap.invert, ref val);
						val._keyboardKeyCode = (KeyboardKeyCode)bind.DefaultKeyboardInput;
						val.keyboardKeyCode = (KeyboardKeyCode)bind.DefaultKeyboardInput;
						bind.kbMap.cVYzDXVDMNXvRMrklCVdVyeXGAlK((ControllerMap)(object)value.keyboardMap);
					}
					if (((ControllerMap)value.joystickMap).AllMaps.All((ActionElementMap x) => x.actionDescriptiveName != bind.jsMap.actionDescriptiveName))
					{
						((ControllerMap)value.joystickMap).CreateElementMap(bind.jsMap.actionId, bind.jsMap.axisContribution, bind.jsMap.elementIdentifierId, bind.jsMap.elementType, bind.jsMap.axisRange, bind.jsMap.invert);
						bind.jsMap.cVYzDXVDMNXvRMrklCVdVyeXGAlK((ControllerMap)(object)value.joystickMap);
					}
				}
			}
		}

		public static ModKeybind RegisterModKeybind(ModKeybind keybind)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Expected O, but got Unknown
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: 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_0099: Invalid comparison between Unknown and I4
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Invalid comparison between Unknown and I4
			//IL_00b3: 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_00c2: Expected O, but got Unknown
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Expected I4, but got Unknown
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Expected O, but got Unknown
			InputAction val = new InputAction();
			val.QurFqcdYtHIEpCpYBddZfQQtnImHA(lastClaimedID++);
			val.MFwTFVDRoziRWNJMARlSUXIebtLw(0);
			val.nfWwbfSEwQCdVSicPaIBIPkMqJfu(keybind.InternalName);
			val.LEWtVuPGPZtPpCUSAMYCEbPqyAuf(keybind.InternalName);
			val.REsRGcKFtjbUwlqUSreCTUubayPb(0);
			val.BSFQAxJApIZsVFnOmINQEGShHPvFb(true);
			val.nLGCfekdjaMIxCLlcjdYgcEjEDxz(keybind.InputType);
			keybind.Id = val.id;
			ActionAxisPair key = default(ActionAxisPair);
			((ActionAxisPair)(ref key))..ctor(val.name, keybind.AxisRange);
			InputCatalog.actionToToken[key] = keybind.DisplayToken;
			keybind.jsMap = new ActionElementMap(val.id, (ControllerElementType)((int)keybind.InputType == 1), keybind.DefaultJoystickInput, (Pole)((int)keybind.AxisRange == 2), keybind.AxisRange);
			keybind.kbMap = new ActionElementMap(val.id, (ControllerElementType)1, (int)keybind.DefaultKeyboardInput);
			Keybinds.Add(keybind);
			KeybindActions.Add(keybind, val);
			return keybind;
		}
	}
	internal class ModdedInputBank : MonoBehaviour
	{
		public PlayerCharacterMasterController pcmc;

		public Dictionary<ModKeybind, ButtonState> ButtonStates = new Dictionary<ModKeybind, ButtonState>();

		public static Dictionary<InputBankTest, ModdedInputBank> BankMap = new Dictionary<InputBankTest, ModdedInputBank>();

		public void Start()
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			foreach (ModKeybind keybind in RebindAPI.Keybinds)
			{
				ButtonStates.Add(keybind, default(ButtonState));
			}
			BankMap.Add(((Component)this).GetComponent<InputBankTest>(), this);
		}

		public void OnDestroy()
		{
			BankMap.Remove(((Component)this).GetComponent<InputBankTest>());
		}

		public void FixedUpdate()
		{
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			LocalUser val = default(LocalUser);
			Player val2 = default(Player);
			CameraRigController val3 = default(CameraRigController);
			bool flag = default(bool);
			if (!Object.op_Implicit((Object)(object)pcmc))
			{
				CharacterBody component = ((Component)this).GetComponent<CharacterBody>();
				if (Object.op_Implicit((Object)(object)component))
				{
					pcmc = (Object.op_Implicit((Object)(object)component.master) ? component.master.playerCharacterMasterController : null);
				}
			}
			else if (PlayerCharacterMasterController.CanSendBodyInput(pcmc.networkUser, ref val, ref val2, ref val3, ref flag))
			{
				for (int i = 0; i < RebindAPI.Keybinds.Count; i++)
				{
					ModKeybind modKeybind = RebindAPI.Keybinds[i];
					ButtonState value = ButtonStates[modKeybind];
					((ButtonState)(ref value)).PushState(val2.GetButton(modKeybind.Id));
					ButtonStates[modKeybind] = value;
				}
			}
		}
	}
	public class ModKeybind
	{
		internal string InternalName;

		public string DisplayToken;

		public KeyCode DefaultKeyboardInput;

		public int DefaultJoystickInput;

		public InputActionType InputType;

		public AxisRange AxisRange;

		internal ActionElementMap kbMap;

		internal ActionElementMap jsMap;

		public string Position;

		internal int Id;

		public ModKeybind(string displayToken, KeyCode defaultKeyboardInput, int defaultJoystickInput, string position = null, InputActionType type = 1, AxisRange range = 0)
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			InternalName = displayToken.Replace("_", "");
			DisplayToken = displayToken;
			DefaultKeyboardInput = defaultKeyboardInput;
			DefaultJoystickInput = defaultJoystickInput;
			Position = position;
		}

		public ButtonState GetButtonState(InputBankTest inputBank)
		{
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: 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_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: 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)
			if ((Object)(object)inputBank != (Object)null && ModdedInputBank.BankMap.ContainsKey(inputBank))
			{
				return ModdedInputBank.BankMap[inputBank].ButtonStates[this];
			}
			Rebindables.ModLogger.LogError((object)("Attempted to get state for " + InternalName + " on a InputBankTest that does not exist!"));
			return default(ButtonState);
		}
	}
	[BepInPlugin("pseudopulse.Rebindables", "Rebindables", "1.0.1")]
	public class Rebindables : BaseUnityPlugin
	{
		public const string PluginGUID = "pseudopulse.Rebindables";

		public const string PluginAuthor = "pseudopulse";

		public const string PluginName = "Rebindables";

		public const string PluginVersion = "1.0.1";

		public static ManualLogSource ModLogger;

		public void Awake()
		{
			ModLogger = ((BaseUnityPlugin)this).Logger;
			RebindAPI.ApplyHooks();
		}
	}
}