mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-12 18:27:23 +01:00
refactor: new code style in Hook.cs
This commit is contained in:
parent
98d441318c
commit
b3044f33e3
1 changed files with 101 additions and 84 deletions
|
|
@ -2,126 +2,143 @@ using System;
|
|||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
using EasyHook;
|
||||
|
||||
namespace Dalamud.Hooking {
|
||||
namespace Dalamud.Hooking
|
||||
{
|
||||
/// <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>
|
||||
public sealed class Hook<T> : IDisposable, IDalamudHook where T : Delegate {
|
||||
public sealed class Hook<T> : IDisposable, IDalamudHook where T : Delegate
|
||||
{
|
||||
private readonly IntPtr address;
|
||||
|
||||
private readonly T original;
|
||||
|
||||
|
||||
private readonly LocalHook hookInfo;
|
||||
|
||||
/// <summary>
|
||||
/// A memory address of the target function.
|
||||
/// Initializes a new instance of the <see cref="Hook{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>
|
||||
/// <param name="callbackParam">A callback object which can be accessed within the detour.</param>
|
||||
public Hook(IntPtr address, Delegate detour, object callbackParam = null)
|
||||
{
|
||||
this.hookInfo = LocalHook.Create(address, detour, callbackParam); // Installs a hook here
|
||||
this.address = address;
|
||||
this.original = Marshal.GetDelegateForFunctionPointer<T>(this.hookInfo.HookBypassAddress);
|
||||
HookInfo.TrackedHooks.Add(new HookInfo() { Delegate = detour, Hook = this, Assembly = Assembly.GetCallingAssembly() });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a memory address of the target function.
|
||||
/// </summary>
|
||||
/// <exception cref="ObjectDisposedException">Hook is already disposed.</exception>
|
||||
public IntPtr Address {
|
||||
public IntPtr Address
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get {
|
||||
CheckDisposed();
|
||||
get
|
||||
{
|
||||
this.CheckDisposed();
|
||||
return this.address;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A delegate function that can be used to call the actual function as if function is not hooked yet.
|
||||
/// Gets a delegate function that can be used to call the actual function as if function is not hooked yet.
|
||||
/// </summary>
|
||||
/// <remarks></remarks>
|
||||
/// <exception cref="ObjectDisposedException">Hook is already disposed.</exception>
|
||||
public T Original {
|
||||
public T Original
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get {
|
||||
CheckDisposed();
|
||||
get
|
||||
{
|
||||
this.CheckDisposed();
|
||||
return this.original;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates a hook. Hooking address is inferred by calling to GetProcAddress() function. Hook is not activated until Enable() method is called.
|
||||
/// Gets a value indicating whether or not the hook is enabled.
|
||||
/// </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>
|
||||
/// <param name="callbackParam">A callback object which can be accessed within the detour.</param>
|
||||
/// <returns></returns>
|
||||
public static Hook<T> FromSymbol(string moduleName, string exportName, Delegate detour, object callbackParam = null) {
|
||||
// Get a function address from the symbol name.
|
||||
var address = LocalHook.GetProcAddress(moduleName, exportName);
|
||||
|
||||
return new Hook<T>(address, detour, callbackParam);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Createss a hook. 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>
|
||||
/// <param name="callbackParam">A callback object which can be accessed within the detour.</param>
|
||||
public Hook(IntPtr address, Delegate detour, object callbackParam = null) {
|
||||
this.hookInfo = LocalHook.Create(address, detour, callbackParam); // Installs a hook here
|
||||
this.address = address;
|
||||
this.original = Marshal.GetDelegateForFunctionPointer<T>(this.hookInfo.HookBypassAddress);
|
||||
HookInfo.TrackedHooks.Add(new HookInfo() { Delegate = detour, Hook = this, Assembly = Assembly.GetCallingAssembly()});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove a hook from the current process.
|
||||
/// </summary>
|
||||
public void Dispose() {
|
||||
if (this.IsDisposed) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.hookInfo.Dispose();
|
||||
|
||||
this.IsDisposed = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts intercepting a call to the function.
|
||||
/// </summary>
|
||||
public void Enable() {
|
||||
CheckDisposed();
|
||||
|
||||
this.hookInfo.ThreadACL.SetExclusiveACL(null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stops intercepting a call to the function.
|
||||
/// </summary>
|
||||
public void Disable() {
|
||||
CheckDisposed();
|
||||
|
||||
this.hookInfo.ThreadACL.SetInclusiveACL(null);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void CheckDisposed() {
|
||||
if (this.IsDisposed) {
|
||||
throw new ObjectDisposedException("Hook is already disposed.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if the hook is enabled.
|
||||
/// </summary>
|
||||
public bool IsEnabled {
|
||||
get {
|
||||
CheckDisposed();
|
||||
public bool IsEnabled
|
||||
{
|
||||
get
|
||||
{
|
||||
this.CheckDisposed();
|
||||
return this.hookInfo.ThreadACL.IsExclusive;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if the hook has been disposed
|
||||
/// 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.
|
||||
/// 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>
|
||||
/// <param name="callbackParam">A callback object which can be accessed within the detour.</param>
|
||||
/// <returns>The hook with the supplied parameters.</returns>
|
||||
public static Hook<T> FromSymbol(string moduleName, string exportName, Delegate detour, object callbackParam = null)
|
||||
{
|
||||
// Get a function address from the symbol name.
|
||||
var address = LocalHook.GetProcAddress(moduleName, exportName);
|
||||
|
||||
return new Hook<T>(address, detour, callbackParam);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove a hook from the current process.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (this.IsDisposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.hookInfo.Dispose();
|
||||
|
||||
this.IsDisposed = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts intercepting a call to the function.
|
||||
/// </summary>
|
||||
public void Enable()
|
||||
{
|
||||
this.CheckDisposed();
|
||||
|
||||
this.hookInfo.ThreadACL.SetExclusiveACL(null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stops intercepting a call to the function.
|
||||
/// </summary>
|
||||
public void Disable()
|
||||
{
|
||||
this.CheckDisposed();
|
||||
|
||||
this.hookInfo.ThreadACL.SetInclusiveACL(null);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void CheckDisposed()
|
||||
{
|
||||
if (this.IsDisposed)
|
||||
{
|
||||
throw new ObjectDisposedException("Hook is already disposed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue