Pass in delegate instead of function pointer

This commit is contained in:
Kasuromi authored and ghorsington committed 2022-08-14 17:18:59 +03:00
1 parent e0023bb834
commit cbb864dfde
5 files changed
+16 -11

No files matched your search

+4 -2
View File
@@ -10,14 +10,16 @@ internal abstract class BaseNativeDetour<T> : 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; }
+3 -1
View File
@@ -1,8 +1,10 @@
using System;
namespace BepInEx.IL2CPP.Hook.Dobby;
internal class DobbyDetour : BaseNativeDetour<DobbyDetour>
{
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);
@@ -7,7 +7,7 @@ internal class FunchookDetour : BaseNativeDetour<FunchookDetour>
{
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();
}
+6 -6
View File
@@ -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<T>(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<T>(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<T>(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<T>();
detour.Apply();
@@ -59,9 +58,10 @@ public interface INativeDetour : IDetour
private List<object> _cache = new();
public CacheDetourWrapper(INativeDetour wrapped)
public CacheDetourWrapper(INativeDetour wrapped, Delegate target)
{
_wrapped = wrapped;
_cache.Add(target);
}
public void Dispose()
@@ -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<TDelegate>(nint original, TDelegate target) where TDelegate : Delegate =>
new Il2CppInteropDetour(INativeDetour.Create(original, target));
}