-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#36 Disable game pause on a modal screen open
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/MultiplayerMod/Multiplayer/Patches/KModalButtonMenuPatch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |