Skip to content

Commit

Permalink
re-impl prevent options submenu go to main menu
Browse files Browse the repository at this point in the history
fix that accidentally use SRT utils
  • Loading branch information
LozenChen committed Nov 7, 2024
1 parent 1aa846a commit 3743839
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
32 changes: 27 additions & 5 deletions Source/Module/Menu/OptionSubMenuExt.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -535,15 +536,17 @@ 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();
cursor.Emit(OpCodes.Ldarg_0);
cursor.EmitDelegate(OnMenuTryDown);
cursor.Emit(OpCodes.Brtrue, target);
}
});
});

typeof(OuiModOptions).GetMethod("Update").IlHook(PreventGotoMainMenu);
}

private static bool OnMenuTryPageDown(TextMenu menu) {
Expand All @@ -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<VirtualButton>("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;
}
}

Expand Down
26 changes: 0 additions & 26 deletions Source/Module/Menu/TASHelperMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -462,39 +462,13 @@ public override void Update() {
}

Visible = alpha != 0;

shouldGotoMainMenu &= !Visible || MenuIndex == 0;
}
public override void Render(Vector2 position, bool highlighted) {
float c = Container.Alpha;
Container.Alpha = alpha;
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;
}
}


Expand Down

0 comments on commit 3743839

Please sign in to comment.