Skip to content

Commit

Permalink
#36 Disable game pause on a modal screen open
Browse files Browse the repository at this point in the history
  • Loading branch information
polycone committed Sep 25, 2023
1 parent 960789f commit 11e23c3
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/MultiplayerMod/Multiplayer/Patches/KModalButtonMenuPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections.Generic;
using System.Linq;
using HarmonyLib;
using JetBrains.Annotations;
using MultiplayerMod.Core.Patch;
using MultiplayerMod.ModRuntime;
using MultiplayerMod.ModRuntime.Context;

namespace MultiplayerMod.Multiplayer.Patches;

[UsedImplicitly]
[HarmonyPatch(typeof(KModalButtonMenu))]
public class KModalButtonMenuPatch {

[HarmonyTranspiler, UsedImplicitly]
[HarmonyPatch("OnShow")]
private static IEnumerable<CodeInstruction> OnShow(IEnumerable<CodeInstruction> instructions) {
using var source = instructions.GetEnumerator();
var result = new List<CodeInstruction>();
var instanceGetter = AccessTools.PropertyGetter(
typeof(SpeedControlScreen),
nameof(SpeedControlScreen.Instance)
);

result.AddConditional(source, it => it.Calls(instanceGetter));
result.AddConditional(source, it => it.Branches(out _));
var jumpOutOfSpeedControlBlock = result.Last();

result.Add(CodeInstruction.Call(typeof(KModalButtonMenuPatch), nameof(ShouldPause)));
result.Add(jumpOutOfSpeedControlBlock);

result.AddConditional(source, _ => false);
return result;
}

private static bool ShouldPause() => !Runtime.Instance.Dependencies.Get<ExecutionLevelManager>()
.LevelIsActive(ExecutionLevel.Game);

}

0 comments on commit 11e23c3

Please sign in to comment.