public static FastNativeDetour CreateAndApply <T>(IntPtr from, T to, out T original, CallingConvention?callingConvention = null) where T : Delegate { var toPtr = callingConvention != null?MonoExtensions.GetFunctionPointerForDelegate(to, callingConvention.Value) : Marshal.GetFunctionPointerForDelegate(to); var result = new FastNativeDetour(from, toPtr); original = result.GenerateTrampoline <T>(); result.Apply(); return(result); }
public override unsafe void Initialize(string gameExePath = null) { UnhollowerBaseLib.GeneratedDatabasesUtil.DatabasesLocationOverride = Preloader.IL2CPPUnhollowedPath; PatchManager.ResolvePatcher += IL2CPPDetourMethodPatcher.TryResolve; base.Initialize(gameExePath); Instance = this; var version = //Version.Parse(Application.unityVersion); Version.Parse(Process.GetCurrentProcess().MainModule.FileVersionInfo.FileVersion); UnityVersionHandler.Initialize(version.Major, version.Minor, version.Revision); // One or the other here for Unhollower to work correctly //ClassInjector.Detour = new DetourHandler(); ClassInjector.DoHook = (ptr, patchedFunctionPtr) => { IntPtr originalFunc = new IntPtr(*(void **)ptr); var detour = new FastNativeDetour(originalFunc, patchedFunctionPtr); detour.Apply(); *(void **)ptr = (void *)detour.TrampolinePtr; }; var gameAssemblyModule = Process.GetCurrentProcess().Modules.Cast <ProcessModule>().First(x => x.ModuleName.Contains("GameAssembly")); var functionPtr = GetProcAddress(gameAssemblyModule.BaseAddress, "il2cpp_runtime_invoke"); //DynDll.GetFunction(gameAssemblyModule.BaseAddress, "il2cpp_runtime_invoke"); PreloaderLogger.Log.LogDebug($"Runtime invoke pointer: 0x{functionPtr.ToInt64():X}"); RuntimeInvokeDetour = new FastNativeDetour(functionPtr, MonoExtensions.GetFunctionPointerForDelegate(new RuntimeInvokeDetourDelegate(OnInvokeMethod), CallingConvention.Cdecl)); RuntimeInvokeDetour.Apply(); originalInvoke = RuntimeInvokeDetour.GenerateTrampoline <RuntimeInvokeDetourDelegate>(); PreloaderLogger.Log.LogDebug("Runtime invoke patched"); }