Skip to content

Commit

Permalink
Fix Performance Optimizer speed controls in SP (#380)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SokyranTheDragon authored Nov 27, 2023
1 parent 0ff9806 commit 48922a9
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion Source/Mods/PerformanceOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}

Expand Down Expand Up @@ -214,5 +217,20 @@ private static IEnumerable<CodeInstruction> SeparateCachesTranspiler(IEnumerable
}

#endregion

#region Time controls

private static FastInvokeHandler doTimeControlsHotkeysMethod;

private static bool PreDoTimeControlsGUI()
{
if (!MP.IsInMultiplayer)
return true;

doTimeControlsHotkeysMethod(null);
return false;
}

#endregion
}
}

0 comments on commit 48922a9

Please sign in to comment.