Advertisement
spacechase0

Untitled

May 22nd, 2017
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.73 KB | None | 0 0
  1.  
  2.             Hijack.hijack(typeof(Game1).GetMethod("setGraphicsForSeason", BindingFlags.Static | BindingFlags.Public),
  3.                           typeof(NewGame1).GetMethod("setGraphicsForSeason", BindingFlags.Static | BindingFlags.Public));
  4.  
  5.  
  6.         public static void hijack( MethodInfo target, MethodInfo replaceWith )
  7.         {
  8.             // I had some verification stuff here, but it handle everything correctly
  9.            
  10.             Log.debug("Hijacking method \"" + target + "\", replacing with \"" + replaceWith + "\".");
  11.  
  12.             RuntimeHelpers.PrepareMethod(target.MethodHandle);
  13.             RuntimeHelpers.PrepareMethod(replaceWith.MethodHandle);
  14.  
  15.             if (target.IsVirtual)
  16.             {/*
  17.                 // http://stackoverflow.com/a/38783635
  18.                 unsafe
  19.                 {
  20.                     UI
  21.                 }*/
  22.             }
  23.             //else
  24.             {
  25.                     // http://stackoverflow.com/a/36415711
  26.                     unsafe
  27.                     {
  28.                     int insOffset = 1;
  29.                     if (IntPtr.Size == 4)
  30.                     {
  31.                         insOffset = 2;
  32.                     }
  33.                     Log.trace("Offset: " + insOffset);
  34.  
  35.                     int* ttar = (int*)target.MethodHandle.Value.ToPointer() + insOffset;
  36.                     int* rtar = (int*)replaceWith.MethodHandle.Value.ToPointer() + insOffset;
  37.  
  38.                     if (Debugger.IsAttached)
  39.                     {
  40.                         Log.trace("Debugger is attached.");
  41.  
  42.                         byte* tinsn = (byte*)(*ttar);
  43.                         byte* rinsn = (byte*)(*rtar);
  44.  
  45.                         int* tsrc = (int*)(tinsn + 1);
  46.                         int* rsrc = (int*)(rinsn + 1);
  47.  
  48.                         Log.trace("Data (1): " + new IntPtr(ttar) + "=" + (ttar == null ? 0 : (*ttar)) + " " + new IntPtr(rtar) + "=" + (rtar == null ? 0 : (*rtar)));
  49.                         Log.trace("Data (2): " + new IntPtr(tinsn) + "=" + (tinsn == null ? 0 : (*tinsn)) + " " + new IntPtr(rinsn) + "=" + (rinsn == null ? 0 : (*rinsn)));
  50.                         Log.trace("Data (3): " + new IntPtr(tsrc) + "=" + (tinsn == null ? 1 : (*tsrc)) + " " + new IntPtr(rsrc) + "=" + (rinsn == null ? 1 : (*rsrc)));
  51.                         (*tsrc) = (((int)rinsn + 5) + (*rsrc)) - ((int)tinsn + 5);
  52.                     }
  53.                     else
  54.                     {
  55.                         Log.trace("Debugger is not attached.");
  56.                         Log.trace("Data: " + new IntPtr(ttar) + "=" + (*ttar) + " " + new IntPtr(rtar) + "=" + (*rtar));
  57.                         (*ttar) = (*rtar);
  58.                     }
  59.                 }
  60.             }
  61.  
  62.             Log.trace("Done");
  63.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement