diff --git a/CheatEnabler/CHANGELOG.md b/CheatEnabler/CHANGELOG.md index 2468b6f..c9cfa19 100644 --- a/CheatEnabler/CHANGELOG.md +++ b/CheatEnabler/CHANGELOG.md @@ -4,6 +4,7 @@ ## Changlog * 2.4.7 + * `Dev Shortcuts`: Work immediately when enabled after loading a save, and process each key press once per frame at accelerated game speeds. * Extend `Unlock Tech with Key-Modifiers` with a downgrade mode. * Press `Caps Lock` to toggle downgrade mode. Modifier clicks lower tech levels and revert their bonuses and dependent tech levels; `Alt` returns the tech to locked. * The current mode is shown by a popup and warning banner. A help button in the tech tree explains the key combinations. @@ -201,6 +202,7 @@ ## 更新日志 * 2.4.7 + * `开发模式快捷键`:修复载入存档后启用时不生效的问题;加速游戏时每次按键仍只执行一次。 * `使用组合键点击解锁科技` 新增降级模式。 * 按 `Caps Lock` 切换降级模式。按住组合键点击可降低科技等级,同时回退升级效果及后继科技等级;`Alt` 将科技回退到未解锁。 * 弹窗和警告横幅显示当前模式,科技树中的帮助按钮提供组合键说明。 diff --git a/CheatEnabler/Patches/GamePatch.cs b/CheatEnabler/Patches/GamePatch.cs index ad8ff26..06eb5a3 100644 --- a/CheatEnabler/Patches/GamePatch.cs +++ b/CheatEnabler/Patches/GamePatch.cs @@ -36,6 +36,12 @@ public static class GamePatch DevShortcuts.Enable(false); } + public static void OnInputUpdate() + { + if (!DevShortcutsEnabled.Value || DSPGame.IsMenuDemo || GameMain.isPaused || !GameMain.isRunning) return; + DevShortcuts.OnInputUpdate(); + } + private static void ResetState() { AbnormalDisabler.ResetState(); @@ -108,6 +114,7 @@ public static class GamePatch protected override void OnEnable() { + AttachToController(GameMain.mainPlayer?.controller); if (_test != null) _test.active = true; } @@ -125,25 +132,36 @@ public static class GamePatch [HarmonyPatch(typeof(PlayerController), nameof(PlayerController.Init))] private static void PlayerController_Init_Postfix(PlayerController __instance) { - var cnt = __instance.actions.Length; + AttachToController(__instance); + } + + private static void AttachToController(PlayerController controller) + { + if (controller?.actions == null) return; + foreach (var action in controller.actions) + { + if (action is not PlayerAction_Test test) continue; + _test = test; + _test.active = DevShortcutsEnabled.Value; + return; + } + var cnt = controller.actions.Length; var newActions = new PlayerAction[cnt + 1]; for (var i = 0; i < cnt; i++) { - newActions[i] = __instance.actions[i]; + newActions[i] = controller.actions[i]; } _test = new PlayerAction_Test(); - _test.Init(__instance.player); + _test.Init(controller.player); _test.active = DevShortcutsEnabled.Value; newActions[cnt] = _test; - __instance.actions = newActions; + controller.actions = newActions; } - [HarmonyPostfix] - [HarmonyPatch(typeof(PlayerAction_Test), nameof(PlayerAction_Test.GameTick))] - private static void PlayerAction_Test_GameTick_Postfix(PlayerAction_Test __instance) + internal static void OnInputUpdate() { - __instance.Update(); + if (GetHarmony() != null && _test?.active == true) _test.Update(); } // Harmony transpiler: PlayerAction_Test_Update_Transpiler // Target: PlayerAction_Test.Update