Cache Harmony Delegates & Runtime Invoke

This commit is contained in:
Kasuromi committed 2022-05-22 03:41:53 +03:00
1 parent b26971f31d
commit 34c375fdf9
2 files changed
+19 -5

No files matched your search

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
@@ -59,6 +59,8 @@ public unsafe class IL2CPPDetourMethodPatcher : MethodPatcher
private static ModuleBuilder fixedStructModuleBuilder;
private static readonly Dictionary<int, Type> FixedStructCache = new();
private static readonly Dictionary<object, object> DelegateCache = new();
private bool isValid;
private INativeMethodInfoStruct modifiedNativeMethodInfo;
@@ -140,8 +142,12 @@ public unsafe class IL2CPPDetourMethodPatcher : MethodPatcher
var unmanagedDelegateType = DelegateTypeFactory.instance.CreateDelegateType(unmanagedTrampolineMethod,
CallingConvention.Cdecl);
var unmanagedDelegate = unmanagedTrampolineMethod.CreateDelegate(unmanagedDelegateType);
DelegateCache[Original] = unmanagedDelegate;
var detourPtr =
Marshal.GetFunctionPointerForDelegate(unmanagedTrampolineMethod.CreateDelegate(unmanagedDelegateType));
Marshal.GetFunctionPointerForDelegate(unmanagedDelegate);
nativeDetour = INativeDetour.Create(originalNativeMethodInfo.MethodPointer, detourPtr);
nativeDetour.Apply();
modifiedNativeMethodInfo.MethodPointer = nativeDetour.TrampolinePtr;
+11 -3
View File
@@ -1,4 +1,5 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
@@ -34,6 +35,8 @@ public class IL2CPPChainloader : BaseChainloader<BasePlugin>
false,
"Include unity log messages in log file output.");
private static List<object> DetourCache = new();
private static INativeDetour RuntimeInvokeDetour { get; set; }
public static IL2CPPChainloader Instance { get; set; }
@@ -74,9 +77,12 @@ public class IL2CPPChainloader : BaseChainloader<BasePlugin>
gameAssemblyModule.BaseAddress.TryGetFunction("il2cpp_runtime_invoke", out var runtimeInvokePtr);
PreloaderLogger.Log.Log(LogLevel.Debug, $"Runtime invoke pointer: 0x{runtimeInvokePtr.ToInt64():X}");
RuntimeInvokeDetour =
INativeDetour.CreateAndApply(runtimeInvokePtr, OnInvokeMethod, out originalInvoke);
RuntimeInvokeDetourDelegate invokeMethodDetour = OnInvokeMethod;
RuntimeInvokeDetour =
INativeDetour.CreateAndApply(runtimeInvokePtr, invokeMethodDetour, out originalInvoke);
DetourCache.Add(invokeMethodDetour);
DetourCache.Add(originalInvoke);
PreloaderLogger.Log.Log(LogLevel.Debug, "Runtime invoke patched");
}
@@ -113,6 +119,8 @@ public class IL2CPPChainloader : BaseChainloader<BasePlugin>
{
RuntimeInvokeDetour.Dispose();
DetourCache.Clear();
PreloaderLogger.Log.Log(LogLevel.Debug, "Runtime invoke unpatched");
}