mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-12 18:27:23 +01:00
Fail fast on trying to dispose import hook that has been overwritten by something else
This commit is contained in:
parent
0899e6cecc
commit
c0954035da
1 changed files with 12 additions and 5 deletions
|
|
@ -13,10 +13,12 @@ namespace Dalamud.Hooking.Internal;
|
||||||
/// <typeparam name="T">Delegate type to represents a function prototype. This must be the same prototype as original function do.</typeparam>
|
/// <typeparam name="T">Delegate type to represents a function prototype. This must be the same prototype as original function do.</typeparam>
|
||||||
internal class FunctionPointerVariableHook<T> : Hook<T> where T : Delegate
|
internal class FunctionPointerVariableHook<T> : Hook<T> where T : Delegate
|
||||||
{
|
{
|
||||||
private readonly IntPtr pfnOriginal;
|
private readonly nint pfnDetour;
|
||||||
private readonly T originalDelegate;
|
|
||||||
private readonly T detourDelegate;
|
private readonly T detourDelegate;
|
||||||
|
|
||||||
|
private nint pfnOriginal;
|
||||||
|
private T? originalDelegate;
|
||||||
|
|
||||||
private bool enabled = false;
|
private bool enabled = false;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -40,9 +42,8 @@ internal class FunctionPointerVariableHook<T> : Hook<T> where T : Delegate
|
||||||
if (!HookManager.MultiHookTracker.TryGetValue(this.Address, out var indexList))
|
if (!HookManager.MultiHookTracker.TryGetValue(this.Address, out var indexList))
|
||||||
indexList = HookManager.MultiHookTracker[this.Address] = new();
|
indexList = HookManager.MultiHookTracker[this.Address] = new();
|
||||||
|
|
||||||
this.pfnOriginal = Marshal.ReadIntPtr(this.Address);
|
|
||||||
this.originalDelegate = Marshal.GetDelegateForFunctionPointer<T>(this.pfnOriginal);
|
|
||||||
this.detourDelegate = detour;
|
this.detourDelegate = detour;
|
||||||
|
this.pfnDetour = Marshal.GetFunctionPointerForDelegate(detour);
|
||||||
|
|
||||||
// Add afterwards, so the hookIdent starts at 0.
|
// Add afterwards, so the hookIdent starts at 0.
|
||||||
indexList.Add(this);
|
indexList.Add(this);
|
||||||
|
|
@ -100,7 +101,10 @@ internal class FunctionPointerVariableHook<T> : Hook<T> where T : Delegate
|
||||||
if (!NativeFunctions.VirtualProtect(this.Address, (UIntPtr)Marshal.SizeOf<IntPtr>(), MemoryProtection.ExecuteReadWrite, out var oldProtect))
|
if (!NativeFunctions.VirtualProtect(this.Address, (UIntPtr)Marshal.SizeOf<IntPtr>(), MemoryProtection.ExecuteReadWrite, out var oldProtect))
|
||||||
throw new Win32Exception(Marshal.GetLastWin32Error());
|
throw new Win32Exception(Marshal.GetLastWin32Error());
|
||||||
|
|
||||||
Marshal.WriteIntPtr(this.Address, Marshal.GetFunctionPointerForDelegate(this.detourDelegate));
|
this.pfnOriginal = Marshal.ReadIntPtr(this.Address);
|
||||||
|
this.originalDelegate = Marshal.GetDelegateForFunctionPointer<T>(this.pfnOriginal);
|
||||||
|
Marshal.WriteIntPtr(this.Address, this.pfnDetour);
|
||||||
|
|
||||||
NativeFunctions.VirtualProtect(this.Address, (UIntPtr)Marshal.SizeOf<IntPtr>(), oldProtect, out _);
|
NativeFunctions.VirtualProtect(this.Address, (UIntPtr)Marshal.SizeOf<IntPtr>(), oldProtect, out _);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -118,6 +122,9 @@ internal class FunctionPointerVariableHook<T> : Hook<T> where T : Delegate
|
||||||
if (!NativeFunctions.VirtualProtect(this.Address, (UIntPtr)Marshal.SizeOf<IntPtr>(), MemoryProtection.ExecuteReadWrite, out var oldProtect))
|
if (!NativeFunctions.VirtualProtect(this.Address, (UIntPtr)Marshal.SizeOf<IntPtr>(), MemoryProtection.ExecuteReadWrite, out var oldProtect))
|
||||||
throw new Win32Exception(Marshal.GetLastWin32Error());
|
throw new Win32Exception(Marshal.GetLastWin32Error());
|
||||||
|
|
||||||
|
if (Marshal.ReadIntPtr(this.Address) != this.pfnOriginal)
|
||||||
|
Environment.FailFast("Cannot disable this hook in a sane manner.");
|
||||||
|
|
||||||
Marshal.WriteIntPtr(this.Address, this.pfnOriginal);
|
Marshal.WriteIntPtr(this.Address, this.pfnOriginal);
|
||||||
NativeFunctions.VirtualProtect(this.Address, (UIntPtr)Marshal.SizeOf<IntPtr>(), oldProtect, out _);
|
NativeFunctions.VirtualProtect(this.Address, (UIntPtr)Marshal.SizeOf<IntPtr>(), oldProtect, out _);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue