diff --git a/BepInEx.IL2CPP/Hook/BaseNativeDetour.cs b/BepInEx.IL2CPP/Hook/BaseNativeDetour.cs index 43cf047..431da02 100644 --- a/BepInEx.IL2CPP/Hook/BaseNativeDetour.cs +++ b/BepInEx.IL2CPP/Hook/BaseNativeDetour.cs @@ -10,14 +10,16 @@ internal abstract class BaseNativeDetour : INativeDetour where T : BaseNative { protected static readonly ManualLogSource Logger = BepInEx.Logging.Logger.CreateLogSource(typeof(T).Name); - protected BaseNativeDetour(nint originalMethodPtr, nint detourMethodPtr) + protected BaseNativeDetour(nint originalMethodPtr, Delegate detourMethod) { OriginalMethodPtr = originalMethodPtr; - DetourMethodPtr = detourMethodPtr; + DetourMethod = detourMethod; + DetourMethodPtr = Marshal.GetFunctionPointerForDelegate(detourMethod); } public bool IsPrepared { get; protected set; } protected MethodInfo TrampolineMethod { get; set; } + protected Delegate DetourMethod { get; set; } public nint OriginalMethodPtr { get; } public nint DetourMethodPtr { get; } diff --git a/BepInEx.IL2CPP/Hook/Dobby/DobbyDetour.cs b/BepInEx.IL2CPP/Hook/Dobby/DobbyDetour.cs index b981009..fe8ab0a 100644 --- a/BepInEx.IL2CPP/Hook/Dobby/DobbyDetour.cs +++ b/BepInEx.IL2CPP/Hook/Dobby/DobbyDetour.cs @@ -1,8 +1,10 @@ +using System; + namespace BepInEx.IL2CPP.Hook.Dobby; internal class DobbyDetour : BaseNativeDetour { - public DobbyDetour(nint originalMethodPtr, nint detourMethodPtr) : base(originalMethodPtr, detourMethodPtr) { } + public DobbyDetour(nint originalMethodPtr, Delegate detourMethod) : base(originalMethodPtr, detourMethod) { } protected override void ApplyImpl() => DobbyLib.Commit(OriginalMethodPtr); diff --git a/BepInEx.IL2CPP/Hook/Funchook/FunchookDetour.cs b/BepInEx.IL2CPP/Hook/Funchook/FunchookDetour.cs index 66bc93c..0711794 100644 --- a/BepInEx.IL2CPP/Hook/Funchook/FunchookDetour.cs +++ b/BepInEx.IL2CPP/Hook/Funchook/FunchookDetour.cs @@ -7,7 +7,7 @@ internal class FunchookDetour : BaseNativeDetour { private readonly nint funchookInstance; - public FunchookDetour(nint originalMethodPtr, nint detourMethodPtr) : base(originalMethodPtr, detourMethodPtr) + public FunchookDetour(nint originalMethodPtr, Delegate detourMethod) : base(originalMethodPtr, detourMethod) { funchookInstance = FunchookLib.Create(); } diff --git a/BepInEx.IL2CPP/Hook/INativeDetour.cs b/BepInEx.IL2CPP/Hook/INativeDetour.cs index 2216a80..7476c3e 100644 --- a/BepInEx.IL2CPP/Hook/INativeDetour.cs +++ b/BepInEx.IL2CPP/Hook/INativeDetour.cs @@ -22,11 +22,11 @@ public interface INativeDetour : IDetour public nint DetourMethodPtr { get; } public nint TrampolinePtr { get; } - private static INativeDetour CreateDefault(nint original, nint target) => + private static INativeDetour CreateDefault(nint original, T target) where T : Delegate => // TODO: check and provide an OS accurate provider new DobbyDetour(original, target); - public static INativeDetour Create(nint original, nint target) + public static INativeDetour Create(nint original, T target) where T : Delegate { var detour = DetourProviderType.Value switch { @@ -36,7 +36,7 @@ public interface INativeDetour : IDetour }; if (!ReflectionHelper.IsMono) { - return new CacheDetourWrapper(detour); + return new CacheDetourWrapper(detour, target); } return detour; } @@ -44,8 +44,7 @@ public interface INativeDetour : IDetour public static INativeDetour CreateAndApply(nint from, T to, out T original) where T : Delegate { - var toPtr = Marshal.GetFunctionPointerForDelegate(to); - var detour = Create(from, toPtr); + var detour = Create(from, to); original = detour.GenerateTrampoline(); detour.Apply(); @@ -59,9 +58,10 @@ public interface INativeDetour : IDetour private List _cache = new(); - public CacheDetourWrapper(INativeDetour wrapped) + public CacheDetourWrapper(INativeDetour wrapped, Delegate target) { _wrapped = wrapped; + _cache.Add(target); } public void Dispose() diff --git a/BepInEx.IL2CPP/Hook/Il2CppInteropDetourProvider.cs b/BepInEx.IL2CPP/Hook/Il2CppInteropDetourProvider.cs index f5fffb0..db5c10e 100644 --- a/BepInEx.IL2CPP/Hook/Il2CppInteropDetourProvider.cs +++ b/BepInEx.IL2CPP/Hook/Il2CppInteropDetourProvider.cs @@ -1,10 +1,11 @@ using Il2CppInterop.Runtime.Injection; +using System; namespace BepInEx.IL2CPP.Hook; internal class Il2CppInteropDetourProvider : IDetourProvider { - public IDetour Create(nint original, nint target) => + public IDetour Create(nint original, TDelegate target) where TDelegate : Delegate => new Il2CppInteropDetour(INativeDetour.Create(original, target)); }