IL2CPPChainloader.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 MonoMod.RuntimeDetour;
  12. using MonoMod.Utils;
  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. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  23. private delegate void UnityLogCallbackDelegate([In] [MarshalAs(UnmanagedType.LPStr)] string log);
  24. private static void UnityLogCallback([In] [MarshalAs(UnmanagedType.LPStr)] string log)
  25. {
  26. UnityLogSource.LogInfo(log.Trim());
  27. }
  28. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  29. private delegate IntPtr RuntimeInvokeDetour(IntPtr method, IntPtr obj, IntPtr parameters, IntPtr exc);
  30. [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
  31. private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
  32. private static RuntimeInvokeDetour originalInvoke;
  33. public unsafe IL2CPPChainloader()
  34. {
  35. UnityVersionHandler.Initialize(2019, 3, 15);
  36. File.AppendAllText("log.log", "Initialized unhollower\n");
  37. ClassInjector.DoHook = (ptr, intPtr) =>
  38. {
  39. var detour = new NativeDetour(new IntPtr(*((int**)ptr)), intPtr);
  40. detour.Apply();
  41. };
  42. foreach (var processModule in Process.GetCurrentProcess().Modules.Cast<ProcessModule>())
  43. {
  44. File.AppendAllText("wew.log", $"{processModule.ModuleName}\n");
  45. }
  46. var gameAssemblyModule = Process.GetCurrentProcess().Modules.Cast<ProcessModule>().First(x => x.ModuleName.Contains("GameAssembly"));
  47. File.AppendAllText("wew.log", $"Got module: {gameAssemblyModule.ModuleName}; addr: {gameAssemblyModule.BaseAddress}\n");
  48. var functionPtr = GetProcAddress(gameAssemblyModule.BaseAddress, "il2cpp_runtime_invoke"); //DynDll.GetFunction(gameAssemblyModule.BaseAddress, "il2cpp_runtime_invoke");
  49. File.AppendAllText("wew.log", $"Got fptr: {functionPtr}\n");
  50. // RuntimeInvokeDetour invokeHook = (method, obj, parameters, exc) =>
  51. // {
  52. // // UnityLogSource.LogInfo(Marshal.PtrToStringAnsi(UnhollowerBaseLib.IL2CPP.il2cpp_method_get_name(method)));
  53. // return originalInvoke(method, obj, parameters, exc);
  54. // };
  55. // UnhollowerBaseLib.IL2CPP.il2cpp_method_get_name(method)
  56. var invokeDetour = new NativeDetour(functionPtr, Marshal.GetFunctionPointerForDelegate(new RuntimeInvokeDetour(OnInvokeMethod)), new NativeDetourConfig {ManualApply = true});
  57. File.AppendAllText("log.log", "Got detour\n");
  58. originalInvoke = invokeDetour.GenerateTrampoline<RuntimeInvokeDetour>();
  59. File.AppendAllText("log.log", "Got trampoline\n");
  60. invokeDetour.Apply();
  61. File.AppendAllText("log.log", "Applied!\n");
  62. }
  63. private static IntPtr OnInvokeMethod(IntPtr method, IntPtr obj, IntPtr parameters, IntPtr exc)
  64. {
  65. lock (originalInvoke)
  66. {
  67. try
  68. {
  69. File.AppendAllText("log.log", $"Got call: {Marshal.PtrToStringAnsi(UnhollowerBaseLib.IL2CPP.il2cpp_method_get_name(method))}\n");
  70. }
  71. catch (Exception e)
  72. {
  73. File.AppendAllText("err.log", e.ToString() + "\n");
  74. }
  75. return originalInvoke(method, obj, parameters, exc);
  76. }
  77. }
  78. protected override void InitializeLoggers()
  79. {
  80. //Logger.Listeners.Add(new UnityLogListener());
  81. //if (ConfigUnityLogging.Value)
  82. // Logger.Sources.Add(new UnityLogSource());
  83. Logger.Sources.Add(UnityLogSource);
  84. ManualLogSource unhollowerLogSource = Logger.CreateLogSource("Unhollower");
  85. UnhollowerBaseLib.LogSupport.InfoHandler += unhollowerLogSource.LogInfo;
  86. UnhollowerBaseLib.LogSupport.WarningHandler += unhollowerLogSource.LogWarning;
  87. UnhollowerBaseLib.LogSupport.TraceHandler += unhollowerLogSource.LogDebug;
  88. UnhollowerBaseLib.LogSupport.ErrorHandler += unhollowerLogSource.LogError;
  89. base.InitializeLoggers();
  90. //if (!ConfigDiskWriteUnityLog.Value)
  91. //{
  92. // DiskLogListener.BlacklistedSources.Add("Unity Log");
  93. //}
  94. foreach (var preloaderLogEvent in PreloaderConsoleListener.LogEvents)
  95. {
  96. PreloaderLogger.Log.Log(preloaderLogEvent.Level, preloaderLogEvent.Data);
  97. }
  98. //UnityEngine.Application.s_LogCallbackHandler = DelegateSupport.ConvertDelegate<Application.LogCallback>(new Action<string>(UnityLogCallback));
  99. //UnityEngine.Application.s_LogCallbackHandler = (Application.LogCallback)new Action<string>(UnityLogCallback);
  100. var loggerPointer = Marshal.GetFunctionPointerForDelegate(new UnityLogCallbackDelegate(UnityLogCallback));
  101. UnhollowerBaseLib.IL2CPP.il2cpp_register_log_callback(loggerPointer);
  102. }
  103. public override BasePlugin LoadPlugin(PluginInfo pluginInfo, Assembly pluginAssembly)
  104. {
  105. var type = pluginAssembly.GetType(pluginInfo.TypeName);
  106. var pluginInstance = (BasePlugin)Activator.CreateInstance(type);
  107. pluginInstance.Load();
  108. return pluginInstance;
  109. }
  110. }
  111. }