From 48922a9a97c50d490586fdbac29d3555cd5a0742 Mon Sep 17 00:00:00 2001 From: SokyranTheDragon <36712560+SokyranTheDragon@users.noreply.github.com> Date: Mon, 27 Nov 2023 16:33:20 +0100 Subject: [PATCH] Fix Performance Optimizer speed controls in SP (#380) Fixes speed change using hotkeys if speed controls are hidden/disabled while playing in SP. The current patch unconditionally called the MP speed controls, despite not being in MP. On top of that, this will prevent the mod time control hotkey method from running in MP, which was pointless as (in best case) it would almost immediately return or (in worst case scenario, if MP just handled time change) it would try running the time change code without a reason as it wouldn't be able to do anything anyway. --- Source/Mods/PerformanceOptimizer.cs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Source/Mods/PerformanceOptimizer.cs b/Source/Mods/PerformanceOptimizer.cs index 39459478..2d6f4728 100644 --- a/Source/Mods/PerformanceOptimizer.cs +++ b/Source/Mods/PerformanceOptimizer.cs @@ -23,8 +23,11 @@ public PerformanceOptimizer(ModContentPack mod) { var doTimeControlsHotkeys = AccessTools.DeclaredMethod("Multiplayer.Client.AsyncTime.TimeControlPatch:DoTimeControlsHotkeys"); if (doTimeControlsHotkeys != null) + { + doTimeControlsHotkeysMethod = MethodInvoker.GetHandler(doTimeControlsHotkeys); MpCompat.harmony.Patch(AccessTools.DeclaredMethod("PerformanceOptimizer.Optimization_DoPlaySettings_DoTimespeedControls:DoTimeControlsGUI"), - prefix: new HarmonyMethod(doTimeControlsHotkeys)); + prefix: new HarmonyMethod(typeof(PerformanceOptimizer), nameof(PreDoTimeControlsGUI))); + } else Log.Error("Could not find TimeControlPatch:DoTimeControlsHotkeys, speed control hot keys won't work with disabled/hidden speed control UI."); } @@ -214,5 +217,20 @@ private static IEnumerable SeparateCachesTranspiler(IEnumerable } #endregion + + #region Time controls + + private static FastInvokeHandler doTimeControlsHotkeysMethod; + + private static bool PreDoTimeControlsGUI() + { + if (!MP.IsInMultiplayer) + return true; + + doTimeControlsHotkeysMethod(null); + return false; + } + + #endregion } } \ No newline at end of file