Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- C#:
- public static void HookEvilMenu(ILContext il)
- {
- var rightTarget = false;
- var c = new ILCursor(il);
- while (!rightTarget)
- {
- if (!c.TryGotoNext(i => i.MatchLdcI4(-71)))
- throw new Exception("Couldn't find case: -71");
- if (!c.Next.Next.MatchBneUn(out _))
- continue;
- if (!c.Next.Next.Next.MatchLdcI4(200))
- continue;
- rightTarget = true;
- }
- c.TryGotoNext(i => i.MatchLdcI4(3));
- int buttonVerticalSpacingIndex = -1;
- c.TryGotoNext(i => i.MatchLdloc(out buttonVerticalSpacingIndex));
- if (buttonVerticalSpacingIndex == -1)
- throw new Exception("Couldn't find the variable index for buttonVerticalSpacing");
- c.TryGotoNext(i => i.MatchLdcI4(4));
- c.TryGotoNext(i => i.MatchLdcI4(70));
- c.Remove();
- c.Emit(Ldc_I4_S, (sbyte) 30);
- c.TryGotoNext(i => i.MatchStelemI4());
- c.Emit(Ldloc_S, (byte) buttonVerticalSpacingIndex);
- c.Emit(Ldc_I4_5);
- c.Emit(Ldc_I4_S, (sbyte) 70);
- c.Emit(Stelem_I4);
- c.TryGotoNext(i => i.MatchLdcI4(5));
- c.Remove();
- c.Emit(Ldc_I4_6);
- var buttonNames = -1;
- c.TryGotoNext(i => i.MatchLdloc(out buttonNames));
- if (buttonNames == -1)
- throw new Exception("Couldn't find the variable index for buttonNames");
- FieldReference selectedMenu = null;
- c.TryGotoNext(i => i.MatchLdarg(0));
- c.TryGotoNext(i => i.MatchLdfld(out selectedMenu));
- if (selectedMenu == null)
- throw new Exception("Couldn't find the private variable selectedMenu");
- var index9 = il.AddVariable(typeof(int));
- c.TryGotoNext(i => i.MatchLdstr("UI.Back"));
- c.TryGotoPrev(i => i.MatchAdd());
- c.Emit(Add);
- c.Emit(Stloc_S, (byte) index9);
- c.Emit(Ldloc_S, (byte) buttonNames);
- c.Emit(Ldloc_S, (byte) index9);
- c.Emit(Ldstr, "Contagion");
- c.Emit(Call, typeof(Language).GetMethod("GetTextValue", new[] {typeof(string)}));
- c.Emit(Stelem_Ref);
- c.Emit(Ldarg_0);
- c.Emit(Ldfld, selectedMenu);
- c.Emit(Ldloc_S, (byte) index9);
- var endif = il.DefineLabel();
- c.Emit(Bne_Un_S, endif);
- c.Emit(Ldc_I4_2);
- c.Emit(Stsfld, typeof(WorldGen).GetField(nameof(WorldGen.WorldGenParam_Evil)));
- c.Emit(Ldc_I4_S, (sbyte) 10);
- c.Emit(Ldc_I4_M1);
- c.Emit(Ldc_I4_M1);
- c.Emit(Ldc_I4_1);
- c.Emit(Ldc_R4, 1f);
- c.Emit(Ldc_R4, 0f);
- c.Emit(Call, typeof(Main).GetMethod("PlaySound", new[] {typeof(Int32), typeof(Int32), typeof(Int32), typeof(Int32), typeof(float), typeof(float)}));
- c.Emit(Pop);
- c.Emit(Ldc_I4_7);
- c.Emit(Stfld, typeof(Main).GetField(nameof(Main.menuMode)));
- c.MarkLabel(endif);
- c.Emit(Ldloc_S, (byte) index9);
- c.Emit(Ldc_I4_1);
- }
- (Different file)
- public static int AddVariable(this ILContext context, Type type)
- {
- context.Body.Variables.Add(new VariableDefinition(context.Import(type)));
- return context.Body.Variables.Count - 1;
- }
- IL:
- ILCode:
- IL_2b36: ldc.i4 200 // 0x000000c8
- IL_2b3b: stloc.s offY
- // [35742 19 - 35742 31]
- IL_2b3d: ldc.i4.s 60 // 0x3c
- IL_2b3f: stloc.s spacing
- // [35743 19 - 35743 48]
- IL_2b41: ldloc.s buttonVerticalSpacing
- IL_2b43: ldc.i4.1
- IL_2b44: ldc.i4.s 30 // 0x1e
- IL_2b46: stelem.i4
- // [35744 19 - 35744 48]
- IL_2b47: ldloc.s buttonVerticalSpacing
- IL_2b49: ldc.i4.2
- IL_2b4a: ldc.i4.s 30 // 0x1e
- IL_2b4c: stelem.i4
- // [35745 19 - 35745 48]
- IL_2b4d: ldloc.s buttonVerticalSpacing
- IL_2b4f: ldc.i4.3
- IL_2b50: ldc.i4.s 30 // 0x1e
- IL_2b52: stelem.i4
- // [35746 19 - 35746 48]
- IL_2b53: ldloc.s buttonVerticalSpacing
- IL_2b55: ldc.i4.4
- IL_2b56: ldc.i4.s 70 // 0x46
- IL_2b58: stelem.i4
- // [35747 19 - 35747 33]
- IL_2b59: ldc.i4.5
- IL_2b5a: stloc.s numButtons
- // [35748 19 - 35748 33]
- IL_2b5c: ldc.i4.0
- IL_2b5d: stloc.s index4
- // [35749 19 - 35749 61]
- IL_2b5f: ldloc.s buttonNames
- IL_2b61: ldloc.s index4
- IL_2b63: ldsfld class Terraria.Localization.LocalizedText[] Terraria.Lang::misc
- IL_2b68: ldc.i4.s 100 // 0x64
- IL_2b6a: ldelem.ref
- IL_2b6b: callvirt instance string Terraria.Localization.LocalizedText::get_Value()
- IL_2b70: stelem.ref
- // [35750 19 - 35750 44]
- IL_2b71: ldloc.s flagArray1
- IL_2b73: ldloc.s index4
- IL_2b75: ldc.i4.1
- IL_2b76: stelem.i1
- // [35751 19 - 35751 42]
- IL_2b77: ldloc.s index4
- IL_2b79: ldc.i4.1
- IL_2b7a: add
- IL_2b7b: stloc.s index5
- // [35752 19 - 35752 61]
- IL_2b7d: ldloc.s buttonNames
- IL_2b7f: ldloc.s index5
- IL_2b81: ldsfld class Terraria.Localization.LocalizedText[] Terraria.Lang::misc
- IL_2b86: ldc.i4.s 101 // 0x65
- IL_2b88: ldelem.ref
- IL_2b89: callvirt instance string Terraria.Localization.LocalizedText::get_Value()
- IL_2b8e: stelem.ref
- // [35753 19 - 35753 51]
- IL_2b8f: ldarg.0 // this
- IL_2b90: ldfld int32 Terraria.Main::selectedMenu
- IL_2b95: ldloc.s index5
- IL_2b97: bne.un.s IL_2bba
- // [35755 21 - 35755 52]
- IL_2b99: ldc.i4.0
- IL_2b9a: stsfld int32 Terraria.WorldGen::WorldGenParam_Evil
- // [35756 21 - 35756 60]
- IL_2b9f: ldc.i4.s 10 // 0x0a
- IL_2ba1: ldc.i4.m1
- IL_2ba2: ldc.i4.m1
- IL_2ba3: ldc.i4.1
- IL_2ba4: ldc.r4 1
- IL_2ba9: ldc.r4 0.0
- IL_2bae: call class [Microsoft.Xna.Framework]Microsoft.Xna.Framework.Audio.SoundEffectInstance Terraria.Main::PlaySound(int32, int32, int32, int32, float32, float32)
- IL_2bb3: pop
- // [35757 21 - 35757 38]
- IL_2bb4: ldc.i4.7
- IL_2bb5: stsfld int32 Terraria.Main::menuMode
- // [35759 19 - 35759 42]
- IL_2bba: ldloc.s index5
- IL_2bbc: ldc.i4.1
- IL_2bbd: add
- IL_2bbe: stloc.s index6
- // [35760 19 - 35760 61]
- IL_2bc0: ldloc.s buttonNames
- IL_2bc2: ldloc.s index6
- IL_2bc4: ldsfld class Terraria.Localization.LocalizedText[] Terraria.Lang::misc
- IL_2bc9: ldc.i4.s 102 // 0x66
- IL_2bcb: ldelem.ref
- IL_2bcc: callvirt instance string Terraria.Localization.LocalizedText::get_Value()
- IL_2bd1: stelem.ref
- // [35761 19 - 35761 51]
- IL_2bd2: ldarg.0 // this
- IL_2bd3: ldfld int32 Terraria.Main::selectedMenu
- IL_2bd8: ldloc.s index6
- IL_2bda: bne.un.s IL_2bfd
- // [35763 21 - 35763 52]
- IL_2bdc: ldc.i4.1
- IL_2bdd: stsfld int32 Terraria.WorldGen::WorldGenParam_Evil
- // [35764 21 - 35764 60]
- IL_2be2: ldc.i4.s 10 // 0x0a
- IL_2be4: ldc.i4.m1
- IL_2be5: ldc.i4.m1
- IL_2be6: ldc.i4.1
- IL_2be7: ldc.r4 1
- IL_2bec: ldc.r4 0.0
- IL_2bf1: call class [Microsoft.Xna.Framework]Microsoft.Xna.Framework.Audio.SoundEffectInstance Terraria.Main::PlaySound(int32, int32, int32, int32, float32, float32)
- IL_2bf6: pop
- // [35765 21 - 35765 38]
- IL_2bf7: ldc.i4.7
- IL_2bf8: stsfld int32 Terraria.Main::menuMode
- // [35767 19 - 35767 42]
- IL_2bfd: ldloc.s index6
- IL_2bff: ldc.i4.1
- IL_2c00: add
- IL_2c01: stloc.s index7
- // [35768 19 - 35768 61]
- IL_2c03: ldloc.s buttonNames
- IL_2c05: ldloc.s index7
- IL_2c07: ldsfld class Terraria.Localization.LocalizedText[] Terraria.Lang::misc
- IL_2c0c: ldc.i4.s 103 // 0x67
- IL_2c0e: ldelem.ref
- IL_2c0f: callvirt instance string Terraria.Localization.LocalizedText::get_Value()
- IL_2c14: stelem.ref
- // [35769 19 - 35769 51]
- IL_2c15: ldarg.0 // this
- IL_2c16: ldfld int32 Terraria.Main::selectedMenu
- IL_2c1b: ldloc.s index7
- IL_2c1d: bne.un.s IL_2c40
- // [35771 21 - 35771 53]
- IL_2c1f: ldc.i4.m1
- IL_2c20: stsfld int32 Terraria.WorldGen::WorldGenParam_Evil
- // [35772 21 - 35772 60]
- IL_2c25: ldc.i4.s 10 // 0x0a
- IL_2c27: ldc.i4.m1
- IL_2c28: ldc.i4.m1
- IL_2c29: ldc.i4.1
- IL_2c2a: ldc.r4 1
- IL_2c2f: ldc.r4 0.0
- IL_2c34: call class [Microsoft.Xna.Framework]Microsoft.Xna.Framework.Audio.SoundEffectInstance Terraria.Main::PlaySound(int32, int32, int32, int32, float32, float32)
- IL_2c39: pop
- // [35773 21 - 35773 38]
- IL_2c3a: ldc.i4.7
- IL_2c3b: stsfld int32 Terraria.Main::menuMode
- // [35775 19 - 35775 42]
- IL_2c40: ldloc.s index7
- IL_2c42: ldc.i4.1
- IL_2c43: add
- IL_2c44: stloc.s index8
- // [35776 19 - 35776 73]
- IL_2c46: ldloc.s buttonNames
- IL_2c48: ldloc.s index8
- IL_2c4a: ldstr "UI.Back"
- IL_2c4f: call string Terraria.Localization.Language::GetTextValue(string)
- IL_2c54: stelem.ref
- // [35777 19 - 35777 68]
- IL_2c55: ldarg.0 // this
- IL_2c56: ldfld int32 Terraria.Main::selectedMenu
- IL_2c5b: ldloc.s index8
- IL_2c5d: ceq
- IL_2c5f: ldloc.s backButtonDown
- IL_2c61: or
- IL_2c62: brfalse.s IL_2c83
- // [35779 21 - 35779 43]
- IL_2c64: ldc.i4.0
- IL_2c65: stloc.s backButtonDown
- // [35780 21 - 35780 60]
- IL_2c67: ldc.i4.s 11 // 0x0b
- IL_2c69: ldc.i4.m1
- IL_2c6a: ldc.i4.m1
- IL_2c6b: ldc.i4.1
- IL_2c6c: ldc.r4 1
- IL_2c71: ldc.r4 0.0
- IL_2c76: call class [Microsoft.Xna.Framework]Microsoft.Xna.Framework.Audio.SoundEffectInstance Terraria.Main::PlaySound(int32, int32, int32, int32, float32, float32)
- IL_2c7b: pop
- // [35781 21 - 35781 39]
- IL_2c7c: ldc.i4.s -7 // 0xf9
- IL_2c7e: stsfld int32 Terraria.Main::menuMode
- // [35783 19 - 35783 40]
- IL_2c83: ldloc.s index8
- IL_2c85: ldc.i4.1
- IL_2c86: add
- IL_2c87: stloc.s num8
- // [35784 19 - 35784 34]
- IL_2c89: call void Terraria.Main::clrInput()
- Terraria Source Code (yes, I know, but it isn't much):
- case -71:
- offY = 200;
- spacing = 60;
- buttonVerticalSpacing[1] = 30;
- buttonVerticalSpacing[2] = 30;
- buttonVerticalSpacing[3] = 30;
- buttonVerticalSpacing[4] = 70;
- numButtons = 5;
- int index4 = 0;
- buttonNames[index4] = Lang.misc[100].Value;
- flagArray1[index4] = true;
- int index5 = index4 + 1;
- buttonNames[index5] = Lang.misc[101].Value;
- if (this.selectedMenu == index5)
- {
- WorldGen.WorldGenParam_Evil = 0;
- Main.PlaySound(10, -1, -1, 1, 1f, 0.0f);
- Main.menuMode = 7;
- }
- int index6 = index5 + 1;
- buttonNames[index6] = Lang.misc[102].Value;
- if (this.selectedMenu == index6)
- {
- WorldGen.WorldGenParam_Evil = 1;
- Main.PlaySound(10, -1, -1, 1, 1f, 0.0f);
- Main.menuMode = 7;
- }
- int index7 = index6 + 1;
- buttonNames[index7] = Lang.misc[103].Value;
- if (this.selectedMenu == index7)
- {
- WorldGen.WorldGenParam_Evil = -1;
- Main.PlaySound(10, -1, -1, 1, 1f, 0.0f);
- Main.menuMode = 7;
- }
- int index8 = index7 + 1;
- buttonNames[index8] = Language.GetTextValue("UI.Back");
- if (this.selectedMenu == index8 | backButtonDown)
- {
- backButtonDown = false;
- Main.PlaySound(11, -1, -1, 1, 1f, 0.0f);
- Main.menuMode = -7;
- }
- int num8 = index8 + 1;
- Main.clrInput();
- break;
- Error:
- [21:58:46] [14/ERROR] [tML]: An error occurred while loading EndoAvalon
- The mod(s) have been automatically been disabled.
- System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidProgramException: Common Language Runtime detected an invalid program.
- at System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(IRuntimeMethodInfo method)
- --- End of inner exception stack trace ---
- at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
- at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
- at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
- at MonoMod.RuntimeDetour.Platforms.DetourRuntimeNETPlatform.GetMethodHandle(MethodBase method)
- at MonoMod.RuntimeDetour.Platforms.DetourRuntimeILPlatform.Pin(MethodBase method)
- at MonoMod.RuntimeDetour.DetourHelper.Pin[T](T method)
- at MonoMod.RuntimeDetour.DetourHelper.GetNativeStart(MethodBase method)
- at MonoMod.RuntimeDetour.Detour._TopApply()
- at MonoMod.RuntimeDetour.Detour..ctor(MethodBase from, MethodBase to)
- at MonoMod.RuntimeDetour.HookGen.HookEndpoint.UpdateILManipulated(Boolean force)
- at MonoMod.RuntimeDetour.HookGen.HookEndpoint.Modify(Delegate callback)
- at MonoMod.RuntimeDetour.HookGen.HookEndpointManager.Modify(MethodBase method, Delegate callback)
- at IL.Terraria.Main.add_DrawMenu(Manipulator )
- at EndoAvalon.EndoAvalon.AddHooks() in EndoAvalon.cs:line 153
- at EndoAvalon.EndoAvalon.Load() in EndoAvalon.cs:line 112
- at Terraria.ModLoader.ModContent.<>c.<Load>b__30_0(Mod mod)
- at Terraria.ModLoader.ModContent.LoadModContent(Action`1 loadAction)
- at Terraria.ModLoader.ModContent.Load()
- at Terraria.ModLoader.ModLoader.Load()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement