Dalamud/Dalamud/Hooking/Internal/Implementations/CoreHookImpl.cs
2021-08-02 09:15:46 -04:00

133 lines
4.6 KiB
C#

using System;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Dalamud.Hooking.Internal.Implementations
{
/// <summary>
/// Manages a hook which can be used to intercept a call to native function.
/// This class is basically a thin wrapper around the LocalHook type to provide helper functions.
/// </summary>
/// <typeparam name="T">Delegate type to represents a function prototype. This must be the same prototype as original function do.</typeparam>
internal sealed class CoreHookImpl<T> : IDisposable, IDalamudHookImpl<T> where T : Delegate
{
private readonly IntPtr address;
private readonly CoreHook.LocalHook hookImpl;
private readonly T original;
/// <summary>
/// Initializes a new instance of the <see cref="CoreHookImpl{T}"/> class.
/// Hook is not activated until Enable() method is called.
/// </summary>
/// <param name="address">A memory address to install a hook.</param>
/// <param name="detour">Callback function. Delegate must have a same original function prototype.</param>
public CoreHookImpl(IntPtr address, T detour)
{
this.address = address;
this.hookImpl = CoreHook.LocalHook.Create(address, detour, null);
this.original = Marshal.GetDelegateForFunctionPointer<T>(this.hookImpl.OriginalAddress);
HookManager.TrackedHooks.Add(new HookInfo(this, detour, Assembly.GetCallingAssembly()));
}
/// <summary>
/// Gets a memory address of the target function.
/// </summary>
/// <exception cref="ObjectDisposedException">Hook is already disposed.</exception>
public IntPtr Address
{
get
{
this.CheckDisposed();
return this.address;
}
}
/// <summary>
/// Gets a delegate function that can be used to call the actual function as if function is not hooked yet.
/// </summary>
/// <exception cref="ObjectDisposedException">Hook is already disposed.</exception>
public T Original
{
get
{
this.CheckDisposed();
return this.original;
}
}
/// <summary>
/// Gets a value indicating whether or not the hook is enabled.
/// </summary>
public bool IsEnabled
{
get
{
this.CheckDisposed();
return this.hookImpl.ThreadACL.IsExclusive;
}
}
/// <summary>
/// Gets a value indicating whether or not the hook has been disposed.
/// </summary>
public bool IsDisposed { get; private set; }
/// <summary>
/// Creates a hook. Hooking address is inferred by calling to GetProcAddress() function.
/// The hook is not activated until Enable() method is called.
/// </summary>
/// <param name="moduleName">A name of the module currently loaded in the memory. (e.g. ws2_32.dll).</param>
/// <param name="exportName">A name of the exported function name (e.g. send).</param>
/// <param name="detour">Callback function. Delegate must have a same original function prototype.</param>
/// <returns>The hook with the supplied parameters.</returns>
public static IDalamudHookImpl<T> FromSymbol(string moduleName, string exportName, T detour)
{
var address = CoreHook.LocalHook.GetProcAddress(moduleName, exportName);
return new CoreHookImpl<T>(address, detour);
}
/// <summary>
/// Remove a hook from the current process.
/// </summary>
public void Dispose()
{
if (this.IsDisposed)
return;
this.IsDisposed = true;
this.hookImpl.Dispose();
}
/// <summary>
/// Starts intercepting a call to the function.
/// </summary>
public void Enable()
{
this.CheckDisposed();
this.hookImpl.ThreadACL.SetExclusiveACL(null);
}
/// <summary>
/// Stops intercepting a call to the function.
/// </summary>
public void Disable()
{
this.CheckDisposed();
this.hookImpl.ThreadACL.SetExclusiveACL(null);
}
/// <summary>
/// Check if this object has been disposed already.
/// </summary>
private void CheckDisposed()
{
if (this.IsDisposed)
{
throw new ObjectDisposedException("Hook is already disposed.");
}
}
}
}