Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.47 KB | None | 0 0
  1. C#:
  2.  
  3.  
  4.         public static void HookEvilMenu(ILContext il)
  5.         {
  6.             var rightTarget = false;
  7.             var c = new ILCursor(il);
  8.             while (!rightTarget)
  9.             {
  10.                 if (!c.TryGotoNext(i => i.MatchLdcI4(-71)))
  11.                     throw new Exception("Couldn't find case: -71");
  12.                 if (!c.Next.Next.MatchBneUn(out _))
  13.                     continue;
  14.                 if (!c.Next.Next.Next.MatchLdcI4(200))
  15.                     continue;
  16.                 rightTarget = true;
  17.             }
  18.             c.TryGotoNext(i => i.MatchLdcI4(3));
  19.             int buttonVerticalSpacingIndex = -1;
  20.             c.TryGotoNext(i => i.MatchLdloc(out buttonVerticalSpacingIndex));
  21.             if (buttonVerticalSpacingIndex == -1)
  22.                 throw new Exception("Couldn't find the variable index for buttonVerticalSpacing");
  23.             c.TryGotoNext(i => i.MatchLdcI4(4));
  24.             c.TryGotoNext(i => i.MatchLdcI4(70));
  25.             c.Remove();
  26.             c.Emit(Ldc_I4_S, (sbyte) 30);
  27.             c.TryGotoNext(i => i.MatchStelemI4());
  28.             c.Emit(Ldloc_S, (byte) buttonVerticalSpacingIndex);
  29.             c.Emit(Ldc_I4_5);
  30.             c.Emit(Ldc_I4_S, (sbyte) 70);
  31.             c.Emit(Stelem_I4);
  32.             c.TryGotoNext(i => i.MatchLdcI4(5));
  33.             c.Remove();
  34.             c.Emit(Ldc_I4_6);
  35.             var buttonNames = -1;
  36.             c.TryGotoNext(i => i.MatchLdloc(out buttonNames));
  37.             if (buttonNames == -1)
  38.                 throw new Exception("Couldn't find the variable index for buttonNames");
  39.             FieldReference selectedMenu = null;
  40.             c.TryGotoNext(i => i.MatchLdarg(0));
  41.             c.TryGotoNext(i => i.MatchLdfld(out selectedMenu));
  42.             if (selectedMenu == null)
  43.                 throw new Exception("Couldn't find the private variable selectedMenu");
  44.             var index9 = il.AddVariable(typeof(int));
  45.             c.TryGotoNext(i => i.MatchLdstr("UI.Back"));
  46.             c.TryGotoPrev(i => i.MatchAdd());
  47.             c.Emit(Add);
  48.             c.Emit(Stloc_S, (byte) index9);
  49.             c.Emit(Ldloc_S, (byte) buttonNames);
  50.             c.Emit(Ldloc_S, (byte) index9);
  51.             c.Emit(Ldstr, "Contagion");
  52.             c.Emit(Call, typeof(Language).GetMethod("GetTextValue", new[] {typeof(string)}));
  53.             c.Emit(Stelem_Ref);
  54.             c.Emit(Ldarg_0);
  55.             c.Emit(Ldfld, selectedMenu);
  56.             c.Emit(Ldloc_S, (byte) index9);
  57.             var endif = il.DefineLabel();
  58.             c.Emit(Bne_Un_S, endif);
  59.             c.Emit(Ldc_I4_2);
  60.             c.Emit(Stsfld, typeof(WorldGen).GetField(nameof(WorldGen.WorldGenParam_Evil)));
  61.             c.Emit(Ldc_I4_S, (sbyte) 10);
  62.             c.Emit(Ldc_I4_M1);
  63.             c.Emit(Ldc_I4_M1);
  64.             c.Emit(Ldc_I4_1);
  65.             c.Emit(Ldc_R4, 1f);
  66.             c.Emit(Ldc_R4, 0f);
  67.             c.Emit(Call, typeof(Main).GetMethod("PlaySound", new[] {typeof(Int32), typeof(Int32), typeof(Int32), typeof(Int32), typeof(float), typeof(float)}));
  68.             c.Emit(Pop);
  69.             c.Emit(Ldc_I4_7);
  70.             c.Emit(Stfld, typeof(Main).GetField(nameof(Main.menuMode)));
  71.             c.MarkLabel(endif);
  72.             c.Emit(Ldloc_S, (byte) index9);
  73.             c.Emit(Ldc_I4_1);
  74.         }
  75.        
  76. (Different file)
  77.         public static int AddVariable(this ILContext context, Type type)
  78.         {
  79.             context.Body.Variables.Add(new VariableDefinition(context.Import(type)));
  80.             return context.Body.Variables.Count - 1;
  81.         }
  82.  
  83. IL:
  84. ILCode:
  85.  
  86.     IL_2b36: ldc.i4       200 // 0x000000c8
  87.     IL_2b3b: stloc.s      offY
  88.  
  89.     // [35742 19 - 35742 31]
  90.     IL_2b3d: ldc.i4.s     60 // 0x3c
  91.     IL_2b3f: stloc.s      spacing
  92.  
  93.     // [35743 19 - 35743 48]
  94.     IL_2b41: ldloc.s      buttonVerticalSpacing
  95.     IL_2b43: ldc.i4.1
  96.     IL_2b44: ldc.i4.s     30 // 0x1e
  97.     IL_2b46: stelem.i4
  98.  
  99.     // [35744 19 - 35744 48]
  100.     IL_2b47: ldloc.s      buttonVerticalSpacing
  101.     IL_2b49: ldc.i4.2
  102.     IL_2b4a: ldc.i4.s     30 // 0x1e
  103.     IL_2b4c: stelem.i4
  104.  
  105.     // [35745 19 - 35745 48]
  106.     IL_2b4d: ldloc.s      buttonVerticalSpacing
  107.     IL_2b4f: ldc.i4.3
  108.     IL_2b50: ldc.i4.s     30 // 0x1e
  109.     IL_2b52: stelem.i4
  110.  
  111.     // [35746 19 - 35746 48]
  112.     IL_2b53: ldloc.s      buttonVerticalSpacing
  113.     IL_2b55: ldc.i4.4
  114.     IL_2b56: ldc.i4.s     70 // 0x46
  115.     IL_2b58: stelem.i4
  116.  
  117.     // [35747 19 - 35747 33]
  118.     IL_2b59: ldc.i4.5
  119.     IL_2b5a: stloc.s      numButtons
  120.  
  121.     // [35748 19 - 35748 33]
  122.     IL_2b5c: ldc.i4.0
  123.     IL_2b5d: stloc.s      index4
  124.  
  125.     // [35749 19 - 35749 61]
  126.     IL_2b5f: ldloc.s      buttonNames
  127.     IL_2b61: ldloc.s      index4
  128.     IL_2b63: ldsfld       class Terraria.Localization.LocalizedText[] Terraria.Lang::misc
  129.     IL_2b68: ldc.i4.s     100 // 0x64
  130.     IL_2b6a: ldelem.ref
  131.     IL_2b6b: callvirt     instance string Terraria.Localization.LocalizedText::get_Value()
  132.     IL_2b70: stelem.ref
  133.  
  134.     // [35750 19 - 35750 44]
  135.     IL_2b71: ldloc.s      flagArray1
  136.     IL_2b73: ldloc.s      index4
  137.     IL_2b75: ldc.i4.1
  138.     IL_2b76: stelem.i1
  139.  
  140.     // [35751 19 - 35751 42]
  141.     IL_2b77: ldloc.s      index4
  142.     IL_2b79: ldc.i4.1
  143.     IL_2b7a: add
  144.     IL_2b7b: stloc.s      index5
  145.  
  146.     // [35752 19 - 35752 61]
  147.     IL_2b7d: ldloc.s      buttonNames
  148.     IL_2b7f: ldloc.s      index5
  149.     IL_2b81: ldsfld       class Terraria.Localization.LocalizedText[] Terraria.Lang::misc
  150.     IL_2b86: ldc.i4.s     101 // 0x65
  151.     IL_2b88: ldelem.ref
  152.     IL_2b89: callvirt     instance string Terraria.Localization.LocalizedText::get_Value()
  153.     IL_2b8e: stelem.ref
  154.  
  155.     // [35753 19 - 35753 51]
  156.     IL_2b8f: ldarg.0      // this
  157.     IL_2b90: ldfld        int32 Terraria.Main::selectedMenu
  158.     IL_2b95: ldloc.s      index5
  159.     IL_2b97: bne.un.s     IL_2bba
  160.  
  161.     // [35755 21 - 35755 52]
  162.     IL_2b99: ldc.i4.0
  163.     IL_2b9a: stsfld       int32 Terraria.WorldGen::WorldGenParam_Evil
  164.  
  165.     // [35756 21 - 35756 60]
  166.     IL_2b9f: ldc.i4.s     10 // 0x0a
  167.     IL_2ba1: ldc.i4.m1
  168.     IL_2ba2: ldc.i4.m1
  169.     IL_2ba3: ldc.i4.1
  170.     IL_2ba4: ldc.r4       1
  171.     IL_2ba9: ldc.r4       0.0
  172.     IL_2bae: call         class [Microsoft.Xna.Framework]Microsoft.Xna.Framework.Audio.SoundEffectInstance Terraria.Main::PlaySound(int32, int32, int32, int32, float32, float32)
  173.     IL_2bb3: pop
  174.  
  175.     // [35757 21 - 35757 38]
  176.     IL_2bb4: ldc.i4.7
  177.     IL_2bb5: stsfld       int32 Terraria.Main::menuMode
  178.  
  179.     // [35759 19 - 35759 42]
  180.     IL_2bba: ldloc.s      index5
  181.     IL_2bbc: ldc.i4.1
  182.     IL_2bbd: add
  183.     IL_2bbe: stloc.s      index6
  184.  
  185.     // [35760 19 - 35760 61]
  186.     IL_2bc0: ldloc.s      buttonNames
  187.     IL_2bc2: ldloc.s      index6
  188.     IL_2bc4: ldsfld       class Terraria.Localization.LocalizedText[] Terraria.Lang::misc
  189.     IL_2bc9: ldc.i4.s     102 // 0x66
  190.     IL_2bcb: ldelem.ref
  191.     IL_2bcc: callvirt     instance string Terraria.Localization.LocalizedText::get_Value()
  192.     IL_2bd1: stelem.ref
  193.  
  194.     // [35761 19 - 35761 51]
  195.     IL_2bd2: ldarg.0      // this
  196.     IL_2bd3: ldfld        int32 Terraria.Main::selectedMenu
  197.     IL_2bd8: ldloc.s      index6
  198.     IL_2bda: bne.un.s     IL_2bfd
  199.  
  200.     // [35763 21 - 35763 52]
  201.     IL_2bdc: ldc.i4.1
  202.     IL_2bdd: stsfld       int32 Terraria.WorldGen::WorldGenParam_Evil
  203.  
  204.     // [35764 21 - 35764 60]
  205.     IL_2be2: ldc.i4.s     10 // 0x0a
  206.     IL_2be4: ldc.i4.m1
  207.     IL_2be5: ldc.i4.m1
  208.     IL_2be6: ldc.i4.1
  209.     IL_2be7: ldc.r4       1
  210.     IL_2bec: ldc.r4       0.0
  211.     IL_2bf1: call         class [Microsoft.Xna.Framework]Microsoft.Xna.Framework.Audio.SoundEffectInstance Terraria.Main::PlaySound(int32, int32, int32, int32, float32, float32)
  212.     IL_2bf6: pop
  213.  
  214.     // [35765 21 - 35765 38]
  215.     IL_2bf7: ldc.i4.7
  216.     IL_2bf8: stsfld       int32 Terraria.Main::menuMode
  217.  
  218.     // [35767 19 - 35767 42]
  219.     IL_2bfd: ldloc.s      index6
  220.     IL_2bff: ldc.i4.1
  221.     IL_2c00: add
  222.     IL_2c01: stloc.s      index7
  223.  
  224.     // [35768 19 - 35768 61]
  225.     IL_2c03: ldloc.s      buttonNames
  226.     IL_2c05: ldloc.s      index7
  227.     IL_2c07: ldsfld       class Terraria.Localization.LocalizedText[] Terraria.Lang::misc
  228.     IL_2c0c: ldc.i4.s     103 // 0x67
  229.     IL_2c0e: ldelem.ref
  230.     IL_2c0f: callvirt     instance string Terraria.Localization.LocalizedText::get_Value()
  231.     IL_2c14: stelem.ref
  232.  
  233.     // [35769 19 - 35769 51]
  234.     IL_2c15: ldarg.0      // this
  235.     IL_2c16: ldfld        int32 Terraria.Main::selectedMenu
  236.     IL_2c1b: ldloc.s      index7
  237.     IL_2c1d: bne.un.s     IL_2c40
  238.  
  239.     // [35771 21 - 35771 53]
  240.     IL_2c1f: ldc.i4.m1
  241.     IL_2c20: stsfld       int32 Terraria.WorldGen::WorldGenParam_Evil
  242.  
  243.     // [35772 21 - 35772 60]
  244.     IL_2c25: ldc.i4.s     10 // 0x0a
  245.     IL_2c27: ldc.i4.m1
  246.     IL_2c28: ldc.i4.m1
  247.     IL_2c29: ldc.i4.1
  248.     IL_2c2a: ldc.r4       1
  249.     IL_2c2f: ldc.r4       0.0
  250.     IL_2c34: call         class [Microsoft.Xna.Framework]Microsoft.Xna.Framework.Audio.SoundEffectInstance Terraria.Main::PlaySound(int32, int32, int32, int32, float32, float32)
  251.     IL_2c39: pop
  252.  
  253.     // [35773 21 - 35773 38]
  254.     IL_2c3a: ldc.i4.7
  255.     IL_2c3b: stsfld       int32 Terraria.Main::menuMode
  256.  
  257.     // [35775 19 - 35775 42]
  258.     IL_2c40: ldloc.s      index7
  259.     IL_2c42: ldc.i4.1
  260.     IL_2c43: add
  261.     IL_2c44: stloc.s      index8
  262.  
  263.     // [35776 19 - 35776 73]
  264.     IL_2c46: ldloc.s      buttonNames
  265.     IL_2c48: ldloc.s      index8
  266.     IL_2c4a: ldstr        "UI.Back"
  267.     IL_2c4f: call         string Terraria.Localization.Language::GetTextValue(string)
  268.     IL_2c54: stelem.ref
  269.  
  270.     // [35777 19 - 35777 68]
  271.     IL_2c55: ldarg.0      // this
  272.     IL_2c56: ldfld        int32 Terraria.Main::selectedMenu
  273.     IL_2c5b: ldloc.s      index8
  274.     IL_2c5d: ceq
  275.     IL_2c5f: ldloc.s      backButtonDown
  276.     IL_2c61: or
  277.     IL_2c62: brfalse.s    IL_2c83
  278.  
  279.     // [35779 21 - 35779 43]
  280.     IL_2c64: ldc.i4.0
  281.     IL_2c65: stloc.s      backButtonDown
  282.  
  283.     // [35780 21 - 35780 60]
  284.     IL_2c67: ldc.i4.s     11 // 0x0b
  285.     IL_2c69: ldc.i4.m1
  286.     IL_2c6a: ldc.i4.m1
  287.     IL_2c6b: ldc.i4.1
  288.     IL_2c6c: ldc.r4       1
  289.     IL_2c71: ldc.r4       0.0
  290.     IL_2c76: call         class [Microsoft.Xna.Framework]Microsoft.Xna.Framework.Audio.SoundEffectInstance Terraria.Main::PlaySound(int32, int32, int32, int32, float32, float32)
  291.     IL_2c7b: pop
  292.  
  293.     // [35781 21 - 35781 39]
  294.     IL_2c7c: ldc.i4.s     -7 // 0xf9
  295.     IL_2c7e: stsfld       int32 Terraria.Main::menuMode
  296.  
  297.     // [35783 19 - 35783 40]
  298.     IL_2c83: ldloc.s      index8
  299.     IL_2c85: ldc.i4.1
  300.     IL_2c86: add
  301.     IL_2c87: stloc.s      num8
  302.  
  303.     // [35784 19 - 35784 34]
  304.     IL_2c89: call         void Terraria.Main::clrInput()
  305.  
  306. Terraria Source Code (yes, I know, but it isn't much):
  307.               case -71:
  308.                  offY = 200;
  309.                  spacing = 60;
  310.                  buttonVerticalSpacing[1] = 30;
  311.                  buttonVerticalSpacing[2] = 30;
  312.                  buttonVerticalSpacing[3] = 30;
  313.                  buttonVerticalSpacing[4] = 70;
  314.                  numButtons = 5;
  315.                  int index4 = 0;
  316.                  buttonNames[index4] = Lang.misc[100].Value;
  317.                  flagArray1[index4] = true;
  318.                  int index5 = index4 + 1;
  319.                  buttonNames[index5] = Lang.misc[101].Value;
  320.                  if (this.selectedMenu == index5)
  321.                  {
  322.                    WorldGen.WorldGenParam_Evil = 0;
  323.                    Main.PlaySound(10, -1, -1, 1, 1f, 0.0f);
  324.                    Main.menuMode = 7;
  325.                  }
  326.                  int index6 = index5 + 1;
  327.                  buttonNames[index6] = Lang.misc[102].Value;
  328.                  if (this.selectedMenu == index6)
  329.                  {
  330.                    WorldGen.WorldGenParam_Evil = 1;
  331.                    Main.PlaySound(10, -1, -1, 1, 1f, 0.0f);
  332.                    Main.menuMode = 7;
  333.                  }
  334.                  int index7 = index6 + 1;
  335.                  buttonNames[index7] = Lang.misc[103].Value;
  336.                  if (this.selectedMenu == index7)
  337.                  {
  338.                    WorldGen.WorldGenParam_Evil = -1;
  339.                    Main.PlaySound(10, -1, -1, 1, 1f, 0.0f);
  340.                    Main.menuMode = 7;
  341.                  }
  342.                  int index8 = index7 + 1;
  343.                  buttonNames[index8] = Language.GetTextValue("UI.Back");
  344.                  if (this.selectedMenu == index8 | backButtonDown)
  345.                  {
  346.                    backButtonDown = false;
  347.                    Main.PlaySound(11, -1, -1, 1, 1f, 0.0f);
  348.                    Main.menuMode = -7;
  349.                  }
  350.                  int num8 = index8 + 1;
  351.                  Main.clrInput();
  352.                  break;
  353.  
  354. Error:
  355. [21:58:46] [14/ERROR] [tML]: An error occurred while loading EndoAvalon
  356. The mod(s) have been automatically been disabled.
  357. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidProgramException: Common Language Runtime detected an invalid program.
  358.   at System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(IRuntimeMethodInfo method)
  359.   --- End of inner exception stack trace ---
  360.   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
  361.   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
  362.   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
  363.   at MonoMod.RuntimeDetour.Platforms.DetourRuntimeNETPlatform.GetMethodHandle(MethodBase method)
  364.   at MonoMod.RuntimeDetour.Platforms.DetourRuntimeILPlatform.Pin(MethodBase method)
  365.   at MonoMod.RuntimeDetour.DetourHelper.Pin[T](T method)
  366.   at MonoMod.RuntimeDetour.DetourHelper.GetNativeStart(MethodBase method)
  367.   at MonoMod.RuntimeDetour.Detour._TopApply()
  368.   at MonoMod.RuntimeDetour.Detour..ctor(MethodBase from, MethodBase to)
  369.   at MonoMod.RuntimeDetour.HookGen.HookEndpoint.UpdateILManipulated(Boolean force)
  370.   at MonoMod.RuntimeDetour.HookGen.HookEndpoint.Modify(Delegate callback)
  371.   at MonoMod.RuntimeDetour.HookGen.HookEndpointManager.Modify(MethodBase method, Delegate callback)
  372.   at IL.Terraria.Main.add_DrawMenu(Manipulator )
  373.   at EndoAvalon.EndoAvalon.AddHooks() in EndoAvalon.cs:line 153
  374.   at EndoAvalon.EndoAvalon.Load() in EndoAvalon.cs:line 112
  375.   at Terraria.ModLoader.ModContent.<>c.<Load>b__30_0(Mod mod)
  376.   at Terraria.ModLoader.ModContent.LoadModContent(Action`1 loadAction)
  377.   at Terraria.ModLoader.ModContent.Load()
  378.   at Terraria.ModLoader.ModLoader.Load()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement