Program.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using XUnity.RuntimeHooker.Core;
  9. namespace XUnity.RuntimeHooker.ConsoleTests
  10. {
  11. class Program
  12. {
  13. static void Main( string[] args )
  14. {
  15. var sayHello = typeof( Program ).GetMethod( "SayHello" );
  16. var sayGoodbye = typeof( Program ).GetMethod( "SayGoodbye" );
  17. RuntimeMethodPatcher.Patch( sayHello, new HookMethod( sayGoodbye ), null );
  18. SayHello();
  19. var textComponent = new TextComponent();
  20. textComponent.Text = "こんにちは";
  21. var textGetter = typeof( TextComponent ).GetProperty( "Text" ).GetGetMethod();
  22. var textGetterHook = typeof( Program ).GetMethod( "TextGetterHook" );
  23. RuntimeMethodPatcher.Patch( textGetter, null, new HookMethod( textGetterHook ) );
  24. Console.WriteLine( textComponent.Text );
  25. var printValue = typeof( Program ).GetMethod( "PrintValue" );
  26. var printValueHook = typeof( Program ).GetMethod( "PrintValueHook" );
  27. RuntimeMethodPatcher.Patch( printValue, new HookMethod( printValueHook ), null );
  28. PrintValue( 7331 );
  29. }
  30. [MethodImpl( MethodImplOptions.NoInlining )]
  31. public static void SayHello()
  32. {
  33. Console.WriteLine( "Hello" );
  34. }
  35. public static bool SayGoodbye()
  36. {
  37. Console.WriteLine( "Goodbye" );
  38. return false;
  39. }
  40. public static void TextGetterHook( ref string __result, TextComponent __instance )
  41. {
  42. Console.WriteLine( "Instance: " + __instance );
  43. __result = "Hello there!";
  44. }
  45. [MethodImpl( MethodImplOptions.NoInlining )]
  46. public static void PrintValue( int i )
  47. {
  48. Console.WriteLine( i );
  49. }
  50. public static void PrintValueHook( ref int i )
  51. {
  52. i = 1337;
  53. }
  54. }
  55. public class TextComponent
  56. {
  57. public string Text { [MethodImpl( MethodImplOptions.NoInlining )]get; set; }
  58. }
  59. }