From 11e23c3068b98ff60aea89f754ac0f117148154a Mon Sep 17 00:00:00 2001 From: Denis Pakhorukov Date: Tue, 26 Sep 2023 00:05:06 +0300 Subject: [PATCH] #36 Disable game pause on a modal screen open --- .../Patches/KModalButtonMenuPatch.cs | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/MultiplayerMod/Multiplayer/Patches/KModalButtonMenuPatch.cs diff --git a/src/MultiplayerMod/Multiplayer/Patches/KModalButtonMenuPatch.cs b/src/MultiplayerMod/Multiplayer/Patches/KModalButtonMenuPatch.cs new file mode 100644 index 00000000..d5f8a8df --- /dev/null +++ b/src/MultiplayerMod/Multiplayer/Patches/KModalButtonMenuPatch.cs @@ -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 OnShow(IEnumerable instructions) { + using var source = instructions.GetEnumerator(); + var result = new List(); + 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() + .LevelIsActive(ExecutionLevel.Game); + +}