mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-13 12:14:16 +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.Reflection;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
using EasyHook;
|
using EasyHook;
|
||||||
|
|
||||||
namespace Dalamud.Hooking {
|
namespace Dalamud.Hooking
|
||||||
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Manages a hook which can be used to intercept a call to native function.
|
/// 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.
|
/// This class is basically a thin wrapper around the LocalHook type to provide helper functions.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <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>
|
||||||
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 IntPtr address;
|
||||||
|
|
||||||
private readonly T original;
|
private readonly T original;
|
||||||
|
|
||||||
private readonly LocalHook hookInfo;
|
private readonly LocalHook hookInfo;
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
/// <exception cref="ObjectDisposedException">Hook is already disposed.</exception>
|
/// <exception cref="ObjectDisposedException">Hook is already disposed.</exception>
|
||||||
public IntPtr Address {
|
public IntPtr Address
|
||||||
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
get {
|
get
|
||||||
CheckDisposed();
|
{
|
||||||
|
this.CheckDisposed();
|
||||||
return this.address;
|
return this.address;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
/// <remarks></remarks>
|
|
||||||
/// <exception cref="ObjectDisposedException">Hook is already disposed.</exception>
|
/// <exception cref="ObjectDisposedException">Hook is already disposed.</exception>
|
||||||
public T Original {
|
public T Original
|
||||||
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
get {
|
get
|
||||||
CheckDisposed();
|
{
|
||||||
|
this.CheckDisposed();
|
||||||
return this.original;
|
return this.original;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
/// <param name="moduleName">A name of the module currently loaded in the memory. (e.g. ws2_32.dll)</param>
|
public bool IsEnabled
|
||||||
/// <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>
|
get
|
||||||
/// <param name="callbackParam">A callback object which can be accessed within the detour.</param>
|
{
|
||||||
/// <returns></returns>
|
this.CheckDisposed();
|
||||||
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();
|
|
||||||
return this.hookInfo.ThreadACL.IsExclusive;
|
return this.hookInfo.ThreadACL.IsExclusive;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Check if the hook has been disposed
|
/// Gets a value indicating whether or not the hook has been disposed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsDisposed { get; private set; }
|
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