diff --git a/Patches/AimToggleHoldPatches.cs b/Patches/AimToggleHoldPatches.cs index e8d3210..10cda72 100644 --- a/Patches/AimToggleHoldPatches.cs +++ b/Patches/AimToggleHoldPatches.cs @@ -1,7 +1,9 @@ using Comfort.Common; using EFT.InputSystem; using HarmonyLib; +using JsonType; using SPT.Reflection.Patching; +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -12,17 +14,18 @@ public static class AimToggleHoldPatches { public static void Enable() { - new AddStatesPatch().Enable(); + new AddTwoKeyStatesPatch().Enable(); + new AddOneKeyStatesPatch().Enable(); new UpdateInputPatch().Enable(); - Settings.ToggleOrHoldAim.SettingChanged += (_, _) => - { - // Will "save" control settings, running GClass1911.UpdateInput, which will set (or unset) toggle/hold behavior - Singleton.Instance.Control.Controller.method_3(); - }; + Settings.ToggleOrHoldAim.SettingChanged += OnSettingChanged; + Settings.ToggleOrHoldSprint.SettingChanged += OnSettingChanged; + Settings.ToggleOrHoldTactical.SettingChanged += OnSettingChanged; + Settings.ToggleOrHoldHeadlight.SettingChanged += OnSettingChanged; + Settings.ToggleOrHoldGoggles.SettingChanged += OnSettingChanged; } - public class AddStatesPatch : ModulePatch + public class AddTwoKeyStatesPatch : ModulePatch { private static FieldInfo StateMachineArray; @@ -35,17 +38,61 @@ public static class AimToggleHoldPatches [PatchPostfix] public static void Postfix(ToggleKeyCombination __instance, EGameKey gameKey, ECommand disableCommand, KeyCombination.KeyCombinationState[] ___keyCombinationState_1) { - if (!Settings.ToggleOrHoldAim.Value || gameKey != EGameKey.Aim) + bool useToggleHold = gameKey switch + { + EGameKey.Aim => Settings.ToggleOrHoldAim.Value, + EGameKey.Sprint => Settings.ToggleOrHoldSprint.Value, + _ => false + }; + + if (!useToggleHold) { return; } List states = new(___keyCombinationState_1) - { - new ToggleHoldIdleState(__instance), - new ToggleHoldClickOrHoldState(__instance), - new ToggleHoldHoldState(__instance, disableCommand) - }; + { + new ToggleHoldIdleState(__instance), + new ToggleHoldClickOrHoldState(__instance), + new ToggleHoldHoldState(__instance, disableCommand) + }; + + StateMachineArray.SetValue(__instance, states.ToArray()); + } + } + + public class AddOneKeyStatesPatch : ModulePatch + { + private static FieldInfo StateMachineArray; + + protected override MethodBase GetTargetMethod() + { + StateMachineArray = AccessTools.Field(typeof(KeyCombination), "keyCombinationState_1"); + return AccessTools.GetDeclaredConstructors(typeof(KeyCombination)).Single(); + } + + [PatchPostfix] + public static void Postfix(ToggleKeyCombination __instance, EGameKey gameKey, ECommand command, KeyCombination.KeyCombinationState[] ___keyCombinationState_1) + { + bool useToggleHold = gameKey switch + { + EGameKey.Tactical => Settings.ToggleOrHoldTactical.Value, + EGameKey.ToggleGoggles => Settings.ToggleOrHoldGoggles.Value, + EGameKey.ToggleHeadLight => Settings.ToggleOrHoldHeadlight.Value, + _ => false + }; + + if (!useToggleHold) + { + return; + } + + List states = new(___keyCombinationState_1) + { + new ToggleHoldIdleState(__instance), + new ToggleHoldClickOrHoldState(__instance), + new ToggleHoldHoldState(__instance, command) + }; StateMachineArray.SetValue(__instance, states.ToArray()); } @@ -61,12 +108,26 @@ public static class AimToggleHoldPatches [PatchPostfix] public static void Postfix(KeyCombination __instance) { - if (!Settings.ToggleOrHoldAim.Value || __instance.GameKey != EGameKey.Aim) + bool useToggleHold = __instance.GameKey switch { - return; - } + EGameKey.Aim => Settings.ToggleOrHoldAim.Value, + EGameKey.Tactical => Settings.ToggleOrHoldTactical.Value, + EGameKey.ToggleGoggles => Settings.ToggleOrHoldGoggles.Value, + EGameKey.ToggleHeadLight => Settings.ToggleOrHoldHeadlight.Value, + EGameKey.Sprint => Settings.ToggleOrHoldSprint.Value, + _ => false + }; - __instance.method_0((KeyCombination.EKeyState)ToggleHoldState.Idle); + if (useToggleHold) + { + __instance.method_0((KeyCombination.EKeyState)ToggleHoldState.Idle); + } } } + + private static void OnSettingChanged(object sender, EventArgs args) + { + // Will "save" control settings, running GClass1911.UpdateInput, which will set (or unset) toggle/hold behavior + Singleton.Instance.Control.Controller.method_3(); + } } diff --git a/Settings.cs b/Settings.cs index f940946..b879569 100644 --- a/Settings.cs +++ b/Settings.cs @@ -67,6 +67,10 @@ internal class Settings // Input public static ConfigEntry ToggleOrHoldAim { get; set; } + public static ConfigEntry ToggleOrHoldSprint { get; set; } + public static ConfigEntry ToggleOrHoldTactical { get; set; } + public static ConfigEntry ToggleOrHoldHeadlight { get; set; } + public static ConfigEntry ToggleOrHoldGoggles { get; set; } public static ConfigEntry UseHomeEnd { get; set; } public static ConfigEntry RebindPageUpDown { get; set; } public static ConfigEntry MouseScrollMulti { get; set; } @@ -225,7 +229,43 @@ internal class Settings "Use Toggle/Hold Aiming", false, new ConfigDescription( - "Tap the aim key to toggle aiming, or hold the aim key for continuous aiming", + "Tap the aim key to toggle aiming, or hold the key for continuous aiming", + null, + new ConfigurationManagerAttributes { }))); + + configEntries.Add(ToggleOrHoldSprint = config.Bind( + InputSection, + "Use Toggle/Hold Sprint", + false, + new ConfigDescription( + "Tap the sprint key to toggle sprinting, or hold the key for continuous sprinting", + null, + new ConfigurationManagerAttributes { }))); + + configEntries.Add(ToggleOrHoldTactical = config.Bind( + InputSection, + "Use Toggle/Hold Tactical Device", + false, + new ConfigDescription( + "Tap the tactical device key to toggle your tactical device, or hold the key for continuous", + null, + new ConfigurationManagerAttributes { }))); + + configEntries.Add(ToggleOrHoldHeadlight = config.Bind( + InputSection, + "Use Toggle/Hold Headlight", + false, + new ConfigDescription( + "Tap the headlight key to toggle your headlight, or hold the key for continuous", + null, + new ConfigurationManagerAttributes { }))); + + configEntries.Add(ToggleOrHoldGoggles = config.Bind( + InputSection, + "Use Toggle/Hold Goggles", + false, + new ConfigDescription( + "Tap the goggles key to toggle night vision/goggles/faceshield, or hold the key for continuous", null, new ConfigurationManagerAttributes { })));