Skip to content

Commit 769f080

Browse files
authored
Implement Managed Detour in Il2CppDetourMethodPatcher (#237)
* implement IL Hook in Il2CppDetourMethodPatcher to skip managed->native->managed * update to use detour instead of il hook
1 parent dbaf825 commit 769f080

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Il2CppInterop.HarmonySupport/Il2CppDetourMethodPatcher.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
using Il2CppInterop.Runtime.Startup;
1313
using Microsoft.Extensions.Logging;
1414
using MonoMod.Cil;
15+
using MonoMod.RuntimeDetour;
1516
using MonoMod.Utils;
17+
using Detour = MonoMod.RuntimeDetour.Detour;
18+
using IDetour = Il2CppInterop.Runtime.Injection.IDetour;
1619
using ValueType = Il2CppSystem.ValueType;
17-
using Void = Il2CppSystem.Void;
1820

1921
namespace Il2CppInterop.HarmonySupport;
2022

@@ -57,6 +59,8 @@ private static readonly MethodInfo ReportExceptionMethodInfo
5759
};
5860

5961
private static readonly List<object> DelegateCache = new();
62+
private static readonly List<object> DetourCache = new();
63+
6064
private INativeMethodInfoStruct modifiedNativeMethodInfo;
6165

6266
private IDetour nativeDetour;
@@ -147,8 +151,10 @@ public override MethodBase DetourTo(MethodBase replacement)
147151
nativeDetour.Apply();
148152
modifiedNativeMethodInfo.MethodPointer = nativeDetour.OriginalTrampoline;
149153

150-
// TODO: Add an ILHook for the original unhollowed method to go directly to managedHookedMethod
151-
// Right now it goes through three times as much interop conversion as it needs to, when being called from managed side
154+
var detour = new Detour(Original, managedHookedMethod);
155+
detour.Apply();
156+
DetourCache.Add(detour);
157+
152158
return managedHookedMethod;
153159
}
154160

0 commit comments

Comments
 (0)