From 3743839344ec075a40c0714817cda41bddaf7fdc Mon Sep 17 00:00:00 2001 From: Lozen <865466388@qq.com> Date: Fri, 8 Nov 2024 02:33:42 +0800 Subject: [PATCH] re-impl prevent options submenu go to main menu fix that accidentally use SRT utils --- Source/Module/Menu/OptionSubMenuExt.cs | 32 ++++++++++++++++++++++---- Source/Module/Menu/TASHelperMenu.cs | 26 --------------------- 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/Source/Module/Menu/OptionSubMenuExt.cs b/Source/Module/Menu/OptionSubMenuExt.cs index 63af36b..b30bda0 100644 --- a/Source/Module/Menu/OptionSubMenuExt.cs +++ b/Source/Module/Menu/OptionSubMenuExt.cs @@ -1,9 +1,10 @@ using Celeste.Mod.Core; -using Celeste.Mod.SpeedrunTool.Utils; +using Celeste.Mod.TASHelper.Utils; using Microsoft.Xna.Framework; using Mono.Cecil.Cil; using Monocle; -using MonoMod.Cil; +using MonoMod.Cil; +using Celeste.Mod.UI; using CMCore = Celeste.Mod.Core; namespace Celeste.Mod.TASHelper.Module.Menu; @@ -524,7 +525,7 @@ private static void DrawIcon(Vector2 position, MTexture icon, Vector2 justify, b [Initialize] private static void InitializeHook() { - typeof(TextMenu).GetMethod("Update").ILHook((cursor, _) => { + typeof(TextMenu).GetMethod("Update").IlHook((cursor, _) => { if (cursor.TryGotoNext(ins => ins.OpCode == OpCodes.Call, ins => ins.MatchCallvirt(typeof(CoreModuleSettings), "get_MenuPageDown"), ins => true, ins => ins.OpCode == OpCodes.Brfalse)) { ILLabel target = (ILLabel)cursor.Next.Next.Next.Next.Operand; cursor.MoveAfterLabels(); @@ -535,7 +536,7 @@ private static void InitializeHook() { }); - typeof(TextMenu).GetMethod("orig_Update").ILHook((cursor, _) => { + typeof(TextMenu).GetMethod("orig_Update").IlHook((cursor, _) => { if (cursor.TryGotoNext(ins => ins.MatchLdsfld(typeof(Input), nameof(Input.MenuDown)), ins => ins.MatchCallvirt(typeof(VirtualButton), "get_Pressed"), ins => ins.OpCode == OpCodes.Brfalse_S)) { ILLabel target = (ILLabel)cursor.Next.Next.Next.Operand; cursor.MoveAfterLabels(); @@ -543,7 +544,9 @@ private static void InitializeHook() { cursor.EmitDelegate(OnMenuTryDown); cursor.Emit(OpCodes.Brtrue, target); } - }); + }); + + typeof(OuiModOptions).GetMethod("Update").IlHook(PreventGotoMainMenu); } private static bool OnMenuTryPageDown(TextMenu menu) { @@ -568,6 +571,25 @@ private static bool OnMenuTryDown(TextMenu menu) { return true; } return false; + } + + private static void PreventGotoMainMenu(ILContext il) { + ILCursor cursor = new ILCursor(il); + if (cursor.TryGotoNext(ins => ins.MatchLdsfld(typeof(Input), nameof(Input.MenuCancel)), ins => ins.MatchCallvirt("get_Pressed"))) { + cursor.Index += 2; + cursor.Emit(OpCodes.Ldarg_0); + cursor.EmitDelegate(GetShouldGotoMainMenu); + } + } + + private static bool GetShouldGotoMainMenu(bool input, OuiModOptions oui) { + if (!input) { + return false; + } + if (oui?.menu?.Current is OptionSubMenuExt subMenu && subMenu.Visible && subMenu.MenuIndex != 0) { + return false; + } + return true; } } diff --git a/Source/Module/Menu/TASHelperMenu.cs b/Source/Module/Menu/TASHelperMenu.cs index e1e4c16..8e6a902 100644 --- a/Source/Module/Menu/TASHelperMenu.cs +++ b/Source/Module/Menu/TASHelperMenu.cs @@ -462,8 +462,6 @@ public override void Update() { } Visible = alpha != 0; - - shouldGotoMainMenu &= !Visible || MenuIndex == 0; } public override void Render(Vector2 position, bool highlighted) { float c = Container.Alpha; @@ -471,30 +469,6 @@ public override void Render(Vector2 position, bool highlighted) { base.Render(position, highlighted); Container.Alpha = c; } - - [Initialize] - - private static void InitializeHook() { - typeof(OuiModOptions).GetMethod("Update").IlHook(PreventGotoMainMenu); - } - - private static void PreventGotoMainMenu(ILContext il) { - ILCursor cursor = new ILCursor(il); - if (cursor.TryGotoNext(ins => ins.OpCode == OpCodes.Brfalse_S)) { - ILLabel label = (ILLabel)cursor.Next.Operand; - cursor.Index -= 2; - cursor.EmitDelegate(GetShouldGotoMainMenu); - cursor.Emit(OpCodes.Brfalse, label); - } - } - - private static bool shouldGotoMainMenu = true; - - private static bool GetShouldGotoMainMenu() { - bool result = shouldGotoMainMenu; - shouldGotoMainMenu = true; - return result; - } }