Please disclose if any significant portion of your mod was created using AI tools by adding the 'AI Generated' category. Failing to do so may result in the mod being removed from Thunderstore.
Decompiled source of KeyboardToolRebinds v2.2.0
KeyboardToolRebinds.dll
Decompiled 3 weeks agousing 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 BepInEx.Logging; using GlobalEnums; using HarmonyLib; using InControl; using Microsoft.CodeAnalysis; using UnityEngine; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")] [assembly: AssemblyCompany("KeyboardToolRebinds")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyFileVersion("2.2.0.0")] [assembly: AssemblyInformationalVersion("2.2.0+9e60bf1801f20181cf96bd9a1a47d9061af86ce0")] [assembly: AssemblyProduct("KeyboardToolRebinds")] [assembly: AssemblyTitle("KeyboardToolRebinds")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("2.2.0.0")] [module: UnverifiableCode] [module: RefSafetyRules(11)] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [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 KeyboardToolRebinds { internal static class ToolKeyState { public static AttackToolBinding? ActiveBinding; private static bool upArmed; private static bool downArmed; private static int lastUpdatedFrame = -1; public static void Refresh() { //IL_0045: Unknown result type (might be due to invalid IL or missing references) //IL_004f: Unknown result type (might be due to invalid IL or missing references) //IL_0054: Unknown result type (might be due to invalid IL or missing references) //IL_005b: 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_0088: Unknown result type (might be due to invalid IL or missing references) if (lastUpdatedFrame == Time.frameCount) { return; } lastUpdatedFrame = Time.frameCount; if (!Plugin.ModEnabled.Value) { upArmed = (downArmed = false); ActiveBinding = null; return; } KeyConflictChecker.CheckIfNeeded(); KeyCode value = Plugin.ToolUpKey.Value; KeyCode value2 = Plugin.ToolDownKey.Value; bool flag = CanStartToolInput(); if (Input.GetKeyDown(value) && flag) { upArmed = true; } if (Input.GetKeyDown(value2) && flag) { downArmed = true; } if (!Input.GetKey(value)) { upArmed = false; } if (!Input.GetKey(value2)) { downArmed = false; } ActiveBinding = (upArmed ? new AttackToolBinding?((AttackToolBinding)1) : (downArmed ? new AttackToolBinding?((AttackToolBinding)2) : ((AttackToolBinding?)null))); } internal static bool CanStartToolInput() { HeroController silentInstance = HeroController.SilentInstance; if ((Object)(object)silentInstance == (Object)null) { return false; } InputHandler instance = ManagerSingleton<InputHandler>.Instance; if ((Object)(object)instance == (Object)null || !instance.acceptingInput) { return false; } if (silentInstance.CanInput() && !silentInstance.controlReqlinquished) { return !silentInstance.IsInputBlocked(); } return false; } } [HarmonyPatch(typeof(PlayerAction), "UpdateBindings")] internal static class PlayerAction_UpdateBindings_Patch { private static void Prefix(PlayerAction __instance, ulong updateTick, float deltaTime) { PlayerActionSet owner = __instance.Owner; HeroActions val = (HeroActions)(object)((owner is HeroActions) ? owner : null); if (val != null && __instance == val.QuickCast) { ToolKeyState.Refresh(); if (ToolKeyState.ActiveBinding.HasValue) { ((OneAxisInputControl)__instance).UpdateWithState(true, updateTick, deltaTime); } } } } [HarmonyPatch(typeof(HeroController))] internal static class HeroController_Patches { private static readonly FieldRef<HeroController, InputHandler> InputHandlerRef = AccessTools.FieldRefAccess<HeroController, InputHandler>("inputHandler"); [HarmonyPatch("GetWillThrowTool")] [HarmonyPrefix] private static bool GetWillThrowTool_Prefix(HeroController __instance, bool reportFailure, ref ToolItem ___willThrowTool, ref bool __result) { //IL_001f: Unknown result type (might be due to invalid IL or missing references) //IL_0024: Unknown result type (might be due to invalid IL or missing references) //IL_0042: Unknown result type (might be due to invalid IL or missing references) //IL_003c: 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) if (!Plugin.ModEnabled.Value) { return true; } AttackToolBinding val; if (ToolKeyState.ActiveBinding.HasValue) { val = ToolKeyState.ActiveBinding.Value; } else { if (!Plugin.BlockOriginalInput.Value || !IsDirectionHeld(__instance)) { return true; } val = (AttackToolBinding)0; } AttackToolBinding binding = default(AttackToolBinding); ___willThrowTool = ToolItemManager.GetBoundAttackTool(val, (ToolEquippedReadSource)0, ref binding); __result = (Object)(object)___willThrowTool != (Object)null && CanThrowTool(__instance, ___willThrowTool, binding, reportFailure); return false; } [HarmonyReversePatch(/*Could not decode attribute arguments.*/)] [HarmonyPatch("CanThrowTool", new Type[] { typeof(ToolItem), typeof(AttackToolBinding), typeof(bool) })] private static bool CanThrowTool(HeroController instance, ToolItem tool, AttackToolBinding binding, bool reportFailure) { throw new NotImplementedException("Stub replaced by Harmony reverse patch"); } [HarmonyReversePatch(/*Could not decode attribute arguments.*/)] [HarmonyPatch("ThrowTool", new Type[] { typeof(bool) })] private static void ThrowTool(HeroController instance, bool isAutoThrow) { throw new NotImplementedException("Stub replaced by Harmony reverse patch"); } [HarmonyPatch("LookForInput")] [HarmonyPostfix] private static void LookForInput_Postfix(HeroController __instance, ref ToolItem ___willThrowTool) { //IL_008a: Unknown result type (might be due to invalid IL or missing references) if (!Plugin.HoldToRepeat.Value || !ToolKeyState.ActiveBinding.HasValue) { return; } InputHandler val = InputHandlerRef.Invoke(__instance); if (!((Object)(object)val == (Object)null) && val.inputActions != null) { PlayerAction quickCast = val.inputActions.QuickCast; if (((OneAxisInputControl)quickCast).IsPressed && !((OneAxisInputControl)quickCast).WasPressed && !__instance.cState.isToolThrowing && ToolKeyState.CanStartToolInput() && __instance.CanThrowTool(false) && __instance.GetWillThrowTool(false) && !((Object)(object)___willThrowTool == (Object)null) && (int)___willThrowTool.Type == 0) { ThrowTool(__instance, isAutoThrow: false); } } } private static bool IsDirectionHeld(HeroController hero) { InputHandler val = InputHandlerRef.Invoke(hero); if ((Object)(object)val == (Object)null || val.inputActions == null) { return false; } if (!((OneAxisInputControl)val.inputActions.Up).IsPressed) { return ((OneAxisInputControl)val.inputActions.Down).IsPressed; } return true; } } internal static class KeyConflictChecker { private static readonly HashSet<string> IgnoredActions = new HashSet<string> { "Submit", "Cancel", "Menu Extra", "Menu Super", "RS_Up", "RS_Down", "RS_Left", "RS_Right", "Pane Right", "Pane Left", "swipeInventoryMap", "swipeInventoryJournal", "swipeInventoryTools", "swipeInventoryQuests" }; private const float CheckInterval = 2f; private static string lastSignature; private static float nextCheckTime; public unsafe static void CheckIfNeeded() { //IL_003f: Unknown result type (might be due to invalid IL or missing references) //IL_0044: Unknown result type (might be due to invalid IL or missing references) //IL_004a: Unknown result type (might be due to invalid IL or missing references) //IL_004f: Unknown result type (might be due to invalid IL or missing references) //IL_00b3: Unknown result type (might be due to invalid IL or missing references) //IL_00b4: 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_00e6: Unknown result type (might be due to invalid IL or missing references) //IL_00b7: 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) if (Time.unscaledTime < nextCheckTime) { return; } nextCheckTime = Time.unscaledTime + 2f; InputHandler instance = ManagerSingleton<InputHandler>.Instance; HeroActions val = (((Object)(object)instance != (Object)null) ? instance.inputActions : null); if (val == null) { return; } KeyCode value = Plugin.ToolUpKey.Value; KeyCode value2 = Plugin.ToolDownKey.Value; string text = ((object)(*(KeyCode*)(&value))/*cast due to .constrained prefix*/).ToString() + "|" + ((object)(*(KeyCode*)(&value2))/*cast due to .constrained prefix*/).ToString() + "|" + BindingsSignature(val); if (text == lastSignature) { return; } lastSignature = text; List<string> list = new List<string>(); if (value == value2 && (int)value != 0) { list.Add($"ToolUp and ToolDown are both set to {value}; only the Up tool will be used."); } AddConflicts(list, "ToolUp", value, val); AddConflicts(list, "ToolDown", value2, val); foreach (string item in list) { Plugin.Log.LogWarning((object)item); } } private static void AddConflicts(List<string> warnings, string setting, KeyCode keyCode, HeroActions actions) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0056: Unknown result type (might be due to invalid IL or missing references) //IL_005b: Unknown result type (might be due to invalid IL or missing references) //IL_006a: Unknown result type (might be due to invalid IL or missing references) if (!TryToInControlKey(keyCode, out var key)) { return; } foreach (PlayerAction action in ((PlayerActionSet)actions).Actions) { if (IgnoredActions.Contains(action.Name)) { continue; } foreach (BindingSource binding in action.Bindings) { KeyBindingSource val = (KeyBindingSource)(object)((binding is KeyBindingSource) ? binding : null); if (val != null && Uses(val.Control, key)) { warnings.Add($"{setting} = {keyCode} is also the game's \"{action.Name}\" key. " + "Pressing it will do both. Change the key in the mod config or in the game's keyboard settings."); break; } } } } private static bool Uses(KeyCombo combo, Key key) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Unknown result type (might be due to invalid IL or missing references) for (int i = 0; i < ((KeyCombo)(ref combo)).IncludeCount; i++) { if (((KeyCombo)(ref combo)).GetInclude(i) == key) { return true; } } return false; } private static string BindingsSignature(HeroActions actions) { //IL_005a: 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) List<string> list = new List<string>(); foreach (PlayerAction action in ((PlayerActionSet)actions).Actions) { if (IgnoredActions.Contains(action.Name)) { continue; } foreach (BindingSource binding in action.Bindings) { KeyBindingSource val = (KeyBindingSource)(object)((binding is KeyBindingSource) ? binding : null); if (val != null) { list.Add(action.Name + "=" + ((object)val.Control/*cast due to .constrained prefix*/).ToString()); } } } return string.Join(",", list); } internal unsafe static bool TryToInControlKey(KeyCode keyCode, out Key key) { string text = ((object)(*(KeyCode*)(&keyCode))/*cast due to .constrained prefix*/).ToString(); if (text.StartsWith("Alpha") && text.Length == 6) { text = "Key" + text.Substring(5); } else if (text.StartsWith("Keypad") && text.Length == 7) { text = "Pad" + text.Substring(6); } if (Enum.TryParse<Key>(text, out key)) { return (int)key != 0; } return false; } } [HarmonyPatch] internal static class ToolPromptPatches { [CompilerGenerated] private static class <>O { public static ActiveControllerSwitch <0>__RedrawShownPrompts; } private static readonly FieldRef<InventoryItemComboButtonPromptDisplay, GameObject> ParentWithModifier = AccessTools.FieldRefAccess<InventoryItemComboButtonPromptDisplay, GameObject>("parentWithModifier"); private static readonly FieldRef<InventoryItemComboButtonPromptDisplay, GameObject> ParentWithoutModifier = AccessTools.FieldRefAccess<InventoryItemComboButtonPromptDisplay, GameObject>("parentWithoutModifier"); private static readonly FieldRef<InventoryItemComboButtonPromptDisplay, ActionButtonIcon> IconWithoutModifier = AccessTools.FieldRefAccess<InventoryItemComboButtonPromptDisplay, ActionButtonIcon>("actionButtonWithoutModifier"); private static readonly Dictionary<InventoryItemComboButtonPromptDisplay, Display> Shown = new Dictionary<InventoryItemComboButtonPromptDisplay, Display>(); private static string keySkinOverride; private static InputHandler subscribedHandler; [HarmonyPostfix] [HarmonyPatch(typeof(InventoryItemComboButtonPromptDisplay), "Show")] private unsafe static void Show_Postfix(InventoryItemComboButtonPromptDisplay __instance, Display display) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0008: Invalid comparison between Unknown and I4 //IL_000a: 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) //IL_0011: Invalid comparison between Unknown and I4 //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0014: Unknown result type (might be due to invalid IL or missing references) //IL_001a: Invalid comparison between Unknown and I4 //IL_0037: Unknown result type (might be due to invalid IL or missing references) //IL_0056: Unknown result type (might be due to invalid IL or missing references) //IL_0057: Unknown result type (might be due to invalid IL or missing references) //IL_005d: Invalid comparison between Unknown and I4 //IL_0070: 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) if ((int)display.ActionButton != 15 || ((int)display.DirectionModifier != 1 && (int)display.DirectionModifier != 2)) { Shown.Remove(__instance); return; } Shown[__instance] = display; SubscribeToDeviceSwitch(); if (!Plugin.ModEnabled.Value || !IsKeyboardActive() || !KeyConflictChecker.TryToInControlKey(((int)display.DirectionModifier == 1) ? Plugin.ToolUpKey.Value : Plugin.ToolDownKey.Value, out var key)) { return; } ParentWithModifier.Invoke(__instance).SetActive(false); ParentWithoutModifier.Invoke(__instance).SetActive(true); keySkinOverride = ((object)(*(Key*)(&key))/*cast due to .constrained prefix*/).ToString(); try { IconWithoutModifier.Invoke(__instance).SetAction((HeroActionButton)15); } finally { keySkinOverride = null; } } [HarmonyPostfix] [HarmonyPatch(typeof(InventoryItemComboButtonPromptDisplay), "Hide")] private static void Hide_Postfix(InventoryItemComboButtonPromptDisplay __instance) { Shown.Remove(__instance); } [HarmonyPrefix] [HarmonyPatch(typeof(UIButtonSkins), "GetButtonSkinFor", new Type[] { typeof(HeroActionButton) })] private static bool GetButtonSkinFor_Prefix(UIButtonSkins __instance, ref ButtonSkin __result) { if (keySkinOverride == null) { return true; } __result = KeyboardSkin(__instance, keySkinOverride); return false; } [HarmonyReversePatch(/*Could not decode attribute arguments.*/)] [HarmonyPatch(typeof(UIButtonSkins), "GetButtonSkinFor", new Type[] { typeof(string) })] private static ButtonSkin KeyboardSkin(UIButtonSkins instance, string buttonName) { throw new NotImplementedException("Stub replaced by Harmony reverse patch"); } private static bool IsKeyboardActive() { //IL_0012: Unknown result type (might be due to invalid IL or missing references) //IL_0017: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) //IL_001b: Unknown result type (might be due to invalid IL or missing references) //IL_001d: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Invalid comparison between Unknown and I4 InputHandler instance = ManagerSingleton<InputHandler>.Instance; if ((Object)(object)instance == (Object)null) { return true; } BindingSourceType lastActiveController = instance.lastActiveController; if ((int)lastActiveController == 0 || lastActiveController - 2 <= 1) { return true; } return false; } private static void SubscribeToDeviceSwitch() { //IL_005b: Unknown result type (might be due to invalid IL or missing references) //IL_0060: Unknown result type (might be due to invalid IL or missing references) //IL_0066: Expected O, but got Unknown //IL_003a: 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_0045: Expected O, but got Unknown InputHandler instance = ManagerSingleton<InputHandler>.Instance; if ((Object)(object)instance == (Object)null || instance == subscribedHandler) { return; } if ((Object)(object)subscribedHandler != (Object)null) { InputHandler obj = subscribedHandler; object obj2 = <>O.<0>__RedrawShownPrompts; if (obj2 == null) { ActiveControllerSwitch val = RedrawShownPrompts; <>O.<0>__RedrawShownPrompts = val; obj2 = (object)val; } obj.RefreshActiveControllerEvent -= (ActiveControllerSwitch)obj2; } object obj3 = <>O.<0>__RedrawShownPrompts; if (obj3 == null) { ActiveControllerSwitch val2 = RedrawShownPrompts; <>O.<0>__RedrawShownPrompts = val2; obj3 = (object)val2; } instance.RefreshActiveControllerEvent += (ActiveControllerSwitch)obj3; subscribedHandler = instance; } private static void RedrawShownPrompts() { //IL_004a: Unknown result type (might be due to invalid IL or missing references) foreach (InventoryItemComboButtonPromptDisplay item in new List<InventoryItemComboButtonPromptDisplay>(Shown.Keys)) { if ((Object)(object)item == (Object)null) { Shown.Remove(item); } else if (((Component)item).gameObject.activeInHierarchy) { item.Show(Shown[item]); } } } } [BepInPlugin("com.ilgax.keyboardtoolrebinds", "KeyboardToolRebinds", "2.2.0")] public class Plugin : BaseUnityPlugin { public static ManualLogSource Log; public static ConfigEntry<KeyCode> ToolUpKey; public static ConfigEntry<KeyCode> ToolDownKey; public static ConfigEntry<bool> ModEnabled; public static ConfigEntry<bool> BlockOriginalInput; public static ConfigEntry<bool> HoldToRepeat; private void Awake() { //IL_00c1: Unknown result type (might be due to invalid IL or missing references) Log = ((BaseUnityPlugin)this).Logger; Log.LogInfo((object)"KeyboardToolRebinds 2.2.0 loaded"); ModEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ModEnabled", true, "Turn the mod on or off. When off, the game behaves exactly like vanilla. Takes effect immediately."); ToolUpKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Keybinds", "ToolUp", (KeyCode)101, "Key that uses the Up tool. Avoid keys the game already uses (vanilla keyboard defaults use Z, X, C, A, S, D, F, V, Tab, I, M, J, Q, T and the arrow keys)."); ToolDownKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Keybinds", "ToolDown", (KeyCode)114, "Key that uses the Down tool. Avoid keys the game already uses (vanilla keyboard defaults use Z, X, C, A, S, D, F, V, Tab, I, M, J, Q, T and the arrow keys)."); BlockOriginalInput = ((BaseUnityPlugin)this).Config.Bind<bool>("Settings", "BlockOriginalInput", true, "Block the original Up+Cast and Down+Cast combinations. Set to false to allow both systems."); HoldToRepeat = ((BaseUnityPlugin)this).Config.Bind<bool>("Settings", "HoldToRepeat", false, "Holding a tool key keeps using that attack tool as fast as the game allows (normal cooldowns still apply). Silk skills are not repeated. Tools that already act while held (like Rosary Cannon) keep their normal hold behaviour."); new Harmony("com.ilgax.keyboardtoolrebinds").PatchAll(Assembly.GetExecutingAssembly()); } } public static class MyPluginInfo { public const string PLUGIN_GUID = "com.ilgax.keyboardtoolrebinds"; public const string PLUGIN_NAME = "KeyboardToolRebinds"; public const string PLUGIN_VERSION = "2.2.0"; } }