Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author: Washi (https://github.com/Washi1337 - https://rtn-team.cc/)
- using System;
- using System.Collections;
- using System.Linq;
- using System.Reflection;
- using HarmonyLib;
- namespace ConsoleApplication4
- {
- internal class Program
- {
- private static FieldInfo _stackField;
- private static FieldInfo _pcField;
- public static void Main(string[] args)
- {
- var assembly = Assembly.LoadFile(@"awesome.vmp_nodbg.exe");
- var harmony = new Harmony("com.example.patch");
- var vmType = assembly.GetType("4775349C");
- var readMethod = vmType.GetMethod("15154B6D", BindingFlags.Instance | BindingFlags.NonPublic);
- _stackField = vmType.GetField("6BAE5C1B", BindingFlags.Instance | BindingFlags.NonPublic);
- _pcField = vmType.GetField("58392466", BindingFlags.Instance | BindingFlags.NonPublic);
- var prefixMethod = typeof(Program).GetMethod(nameof(ReadBytePrefix), BindingFlags.Static | BindingFlags.Public);
- var postfixMethod = typeof(Program).GetMethod(nameof(ReadBytePostfix), BindingFlags.Static | BindingFlags.Public);
- harmony.Patch(readMethod, new HarmonyMethod(prefixMethod), new HarmonyMethod(postfixMethod));
- var invokeMethod = typeof(object).Assembly
- .GetType("System.Reflection.RuntimeMethodInfo")
- .GetMethod("UnsafeInvokeInternal", BindingFlags.NonPublic | BindingFlags.Instance);
- prefixMethod = typeof(Program).GetMethod(nameof(InvokePrefix), BindingFlags.Static | BindingFlags.Public);
- postfixMethod = typeof(Program).GetMethod(nameof(InvokePostfix), BindingFlags.Static | BindingFlags.Public);
- harmony.Patch(invokeMethod, new HarmonyMethod(prefixMethod), new HarmonyMethod(postfixMethod));
- assembly.EntryPoint.Invoke(null, null);
- }
- private static string FormatObject(object obj)
- {
- try
- {
- switch (obj)
- {
- case null:
- return "null";
- case string x:
- return $"\"{x}\"";
- case IEnumerable enumerable:
- return
- $"{obj.GetType().Name} {{{string.Join(", ", enumerable.Cast<object>().Select(FormatObject))}}}";
- case { } o when o.GetType().Name == "0FE23521":
- {
- var field = o.GetType().GetField("5BE47E90", BindingFlags.Instance | BindingFlags.NonPublic);
- return FormatObject(field.GetValue(o));
- }
- case { } o when o.GetType().Name == "6F9B56A3":
- {
- var field = o.GetType().GetField("1B4E1C53", BindingFlags.Instance | BindingFlags.NonPublic);
- return FormatObject(field.GetValue(o));
- }
- default:
- return obj.ToString();
- }
- }
- catch (Exception ex)
- {
- return "???";
- }
- }
- public static void ReadBytePrefix(object __instance)
- {
- Console.Write("{0:X8} ({0}): ", _pcField.GetValue(__instance));
- var stackContents = ((IEnumerable) _stackField.GetValue(__instance))
- .Cast<object>()
- .Reverse()
- .ToArray();
- Console.WriteLine(FormatObject(stackContents));
- }
- public static void ReadBytePostfix()
- {
- }
- public static void InvokePrefix(object __instance, object obj, object[] parameters, object[] arguments)
- {
- var method = (MethodBase) __instance;
- string returnType = method is MethodInfo info ? info.ReturnType.FullName : "System.Object";
- Console.WriteLine($"--- call to {returnType} {method.DeclaringType}::{method.Name}({string.Join(", ", method.GetParameters().Cast<object>())})");
- if (arguments != null)
- {
- for (int i = 0; i < arguments.Length; i++)
- Console.WriteLine($"--- {i}: {FormatObject(arguments[i])}");
- }
- }
- public static void InvokePostfix(object __instance, ref object __result, object obj, object[] parameters, object[] arguments)
- {
- Console.WriteLine("--- Resulted in " + FormatObject(__result));
- }
- }
- }
Add Comment
Please, Sign In to add comment