IL2CPPChainloader.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Runtime.InteropServices;
  7. using BepInEx.Bootstrap;
  8. using BepInEx.Logging;
  9. using BepInEx.Preloader.Core;
  10. using BepInEx.Preloader.Core.Logging;
  11. using Iced.Intel;
  12. using MonoMod.RuntimeDetour;
  13. using UnhollowerBaseLib.Runtime;
  14. using UnhollowerRuntimeLib;
  15. using UnityEngine;
  16. using Logger = BepInEx.Logging.Logger;
  17. namespace BepInEx.IL2CPP
  18. {
  19. public class IL2CPPChainloader : BaseChainloader<BasePlugin>
  20. {
  21. private static ManualLogSource UnityLogSource = new ManualLogSource("Unity");
  22. public static void UnityLogCallback(string logLine, string exception, LogType type)
  23. {
  24. UnityLogSource.LogInfo(logLine.Trim());
  25. }
  26. // public const CallingConvention ArchConvention =
  27. //#if X64
  28. // CallingConvention.Stdcall;
  29. //#else
  30. // CallingConvention.Cdecl;
  31. //#endif
  32. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  33. private delegate IntPtr RuntimeInvokeDetour(IntPtr method, IntPtr obj, IntPtr parameters, IntPtr exc);
  34. [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
  35. private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
  36. private static RuntimeInvokeDetour originalInvoke;
  37. private static ManualLogSource unhollowerLogSource = Logger.CreateLogSource("Unhollower");
  38. private class DetourHandler : UnhollowerRuntimeLib.ManagedDetour
  39. {
  40. public T Detour<T>(IntPtr from, T to) where T : Delegate
  41. {
  42. var detour = new NativeDetour(from, to, new NativeDetourConfig { ManualApply = true });
  43. var trampoline = detour.GenerateTrampoline<T>();
  44. detour.Apply();
  45. return trampoline;
  46. }
  47. }
  48. private static void Disassemble(ManualLogSource logSource, IntPtr memoryPtr, int size)
  49. {
  50. byte[] data = new byte[size];
  51. Marshal.Copy(memoryPtr, data, 0, size);
  52. var formatter = new NasmFormatter();
  53. var output = new StringOutput();
  54. var codeReader = new ByteArrayCodeReader(data);
  55. var decoder = Decoder.Create(64, codeReader);
  56. decoder.IP = (ulong)memoryPtr.ToInt64();
  57. while (codeReader.CanReadByte)
  58. {
  59. decoder.Decode(out var instr);
  60. formatter.Format(instr, output);
  61. logSource.LogDebug($"{instr.IP:X16} {output.ToStringAndReset()}");
  62. }
  63. }
  64. public override unsafe void Initialize(string gameExePath = null)
  65. {
  66. base.Initialize(gameExePath);
  67. UnityVersionHandler.Initialize(2019, 2, 17);
  68. // One or the other here for Unhollower to work correctly
  69. ////////ClassInjector.Detour = new DetourHandler();
  70. ClassInjector.DoHook = (ptr, intPtr) =>
  71. {
  72. IntPtr originalFunc = new IntPtr(*(void**)ptr);
  73. unhollowerLogSource.LogDebug($"DoHook 0x{originalFunc.ToString("X")} -> 0x{intPtr.ToString("X")}");
  74. var trampolineAlloc = DetourHelper.Native.MemAlloc(80);
  75. DetourHelper.Native.MakeExecutable(trampolineAlloc, 80);
  76. DetourHelper.Native.MakeWritable(originalFunc, 24);
  77. unhollowerLogSource.LogDebug($"Trampoline allocation: 0x{ptr.ToString("X")}");
  78. unhollowerLogSource.LogDebug("Original (24) asm");
  79. Disassemble(unhollowerLogSource, originalFunc, 24);
  80. var trampolineLength = TrampolineGenerator.Generate(originalFunc, intPtr, trampolineAlloc,
  81. IntPtr.Size == 8 ? 64 : 32);
  82. unhollowerLogSource.LogDebug("Modified (24) asm");
  83. Disassemble(unhollowerLogSource, originalFunc, 24);
  84. unhollowerLogSource.LogDebug($"Trampoline ({trampolineLength}) asm");
  85. Disassemble(unhollowerLogSource, trampolineAlloc, trampolineLength);
  86. *(void**)ptr = (void*)trampolineAlloc;
  87. };
  88. var gameAssemblyModule = Process.GetCurrentProcess().Modules.Cast<ProcessModule>().First(x => x.ModuleName.Contains("GameAssembly"));
  89. var functionPtr = GetProcAddress(gameAssemblyModule.BaseAddress, "il2cpp_runtime_invoke"); //DynDll.GetFunction(gameAssemblyModule.BaseAddress, "il2cpp_runtime_invoke");
  90. PreloaderLogger.Log.LogDebug($"Runtime invoke pointer: 0x{functionPtr.ToInt64():X}");
  91. var invokeDetour = new NativeDetour(functionPtr, Marshal.GetFunctionPointerForDelegate(new RuntimeInvokeDetour(OnInvokeMethod)), new NativeDetourConfig { ManualApply = true });
  92. originalInvoke = invokeDetour.GenerateTrampoline<RuntimeInvokeDetour>();
  93. invokeDetour.Apply();
  94. }
  95. private static bool HasSet = false;
  96. private static IntPtr OnInvokeMethod(IntPtr method, IntPtr obj, IntPtr parameters, IntPtr exc)
  97. {
  98. string methodName = Marshal.PtrToStringAnsi(UnhollowerBaseLib.IL2CPP.il2cpp_method_get_name(method));
  99. if (!HasSet && methodName == "Internal_ActiveSceneChanged")
  100. {
  101. try
  102. {
  103. UnityEngine.Application.s_LogCallbackHandler = new Action<string, string, LogType>(UnityLogCallback);
  104. UnityLogSource.LogMessage($"callback set - {methodName}");
  105. UnityEngine.Application.CallLogCallback("test from OnInvokeMethod", "", LogType.Log, true);
  106. }
  107. catch (Exception ex)
  108. {
  109. UnityLogSource.LogError(ex);
  110. }
  111. HasSet = true;
  112. }
  113. //UnityLogSource.LogDebug(methodName);
  114. return originalInvoke(method, obj, parameters, exc);
  115. }
  116. protected override void InitializeLoggers()
  117. {
  118. //Logger.Listeners.Add(new UnityLogListener());
  119. //if (ConfigUnityLogging.Value)
  120. // Logger.Sources.Add(new UnityLogSource());
  121. Logger.Sources.Add(UnityLogSource);
  122. UnhollowerBaseLib.LogSupport.InfoHandler += unhollowerLogSource.LogInfo;
  123. UnhollowerBaseLib.LogSupport.WarningHandler += unhollowerLogSource.LogWarning;
  124. UnhollowerBaseLib.LogSupport.TraceHandler += unhollowerLogSource.LogDebug;
  125. UnhollowerBaseLib.LogSupport.ErrorHandler += unhollowerLogSource.LogError;
  126. base.InitializeLoggers();
  127. //if (!ConfigDiskWriteUnityLog.Value)
  128. //{
  129. // DiskLogListener.BlacklistedSources.Add("Unity Log");
  130. //}
  131. foreach (var preloaderLogEvent in PreloaderConsoleListener.LogEvents)
  132. {
  133. PreloaderLogger.Log.Log(preloaderLogEvent.Level, preloaderLogEvent.Data);
  134. }
  135. //UnityEngine.Application.s_LogCallbackHandler = DelegateSupport.ConvertDelegate<Application.LogCallback>(new Action<string>(UnityLogCallback));
  136. //UnityEngine.Application.s_LogCallbackHandler = (Application.LogCallback)new Action<string>(UnityLogCallback);
  137. //var loggerPointer = Marshal.GetFunctionPointerForDelegate(new UnityLogCallbackDelegate(UnityLogCallback));
  138. //UnhollowerBaseLib.IL2CPP.il2cpp_register_log_callback(loggerPointer);
  139. }
  140. public override BasePlugin LoadPlugin(PluginInfo pluginInfo, Assembly pluginAssembly)
  141. {
  142. var type = pluginAssembly.GetType(pluginInfo.TypeName);
  143. var pluginInstance = (BasePlugin)Activator.CreateInstance(type);
  144. pluginInstance.Load();
  145. return pluginInstance;
  146. }
  147. }
  148. }