mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-02-15 20:37:42 +01:00
* Use new Lock objects * Fix CA1513: Use ObjectDisposedException.ThrowIf * Fix CA1860: Avoid using 'Enumerable.Any()' extension method * Fix IDE0028: Use collection initializers or expressions * Fix CA2263: Prefer generic overload when type is known * Fix CA1862: Use the 'StringComparison' method overloads to perform case-insensitive string comparisons * Fix IDE0270: Null check can be simplified * Fix IDE0280: Use 'nameof' * Fix IDE0009: Add '.this' * Fix IDE0007: Use 'var' instead of explicit type * Fix IDE0062: Make local function static * Fix CA1859: Use concrete types when possible for improved performance * Fix IDE0066: Use switch expression Only applied to where it doesn't look horrendous. * Use is over switch * Fix CA1847: Use String.Contains(char) instead of String.Contains(string) with single characters * Fix SYSLIB1045: Use 'GeneratedRegexAttribute' to generate the regular expression implementation at compile-time. * Fix CA1866: Use 'string.EndsWith(char)' instead of 'string.EndsWith(string)' when you have a string with a single char * Fix IDE0057: Substring can be simplified * Fix IDE0059: Remove unnecessary value assignment * Fix CA1510: Use ArgumentNullException throw helper * Fix IDE0300: Use collection expression for array * Fix IDE0250: Struct can be made 'readonly' * Fix IDE0018: Inline variable declaration * Fix CA1850: Prefer static HashData method over ComputeHash * Fi CA1872: Prefer 'Convert.ToHexString' and 'Convert.ToHexStringLower' over call chains based on 'BitConverter.ToString' * Update ModuleLog instantiations * Organize usings
105 lines
2.9 KiB
C#
105 lines
2.9 KiB
C#
using System.Reflection;
|
|
|
|
namespace Dalamud.Hooking.Internal;
|
|
|
|
/// <summary>
|
|
/// Manages a hook with MinHook.
|
|
/// </summary>
|
|
/// <typeparam name="T">Delegate type to represents a function prototype. This must be the same prototype as original function do.</typeparam>
|
|
internal class MinHookHook<T> : Hook<T> where T : Delegate
|
|
{
|
|
private readonly MinSharp.Hook<T> minHookImpl;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="MinHookHook{T}"/> class.
|
|
/// </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="callingAssembly">Calling assembly.</param>
|
|
internal MinHookHook(IntPtr address, T detour, Assembly callingAssembly)
|
|
: base(address)
|
|
{
|
|
lock (HookManager.HookEnableSyncRoot)
|
|
{
|
|
var unhooker = HookManager.RegisterUnhooker(this.Address);
|
|
|
|
if (!HookManager.MultiHookTracker.TryGetValue(this.Address, out var indexList))
|
|
indexList = HookManager.MultiHookTracker[this.Address] = [];
|
|
|
|
var index = (ulong)indexList.Count;
|
|
|
|
this.minHookImpl = new MinSharp.Hook<T>(this.Address, detour, index);
|
|
|
|
// Add afterwards, so the hookIdent starts at 0.
|
|
indexList.Add(this);
|
|
|
|
unhooker.TrimAfterHook();
|
|
|
|
HookManager.TrackedHooks.TryAdd(this.HookId, new HookInfo(this, detour, callingAssembly));
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public override T Original
|
|
{
|
|
get
|
|
{
|
|
this.CheckDisposed();
|
|
return this.minHookImpl.Original;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public override bool IsEnabled => !this.IsDisposed && this.minHookImpl.Enabled;
|
|
|
|
/// <inheritdoc/>
|
|
public override string BackendName => "MinHook";
|
|
|
|
/// <inheritdoc/>
|
|
public override void Dispose()
|
|
{
|
|
if (this.IsDisposed)
|
|
return;
|
|
|
|
lock (HookManager.HookEnableSyncRoot)
|
|
{
|
|
this.minHookImpl.Dispose();
|
|
|
|
var index = HookManager.MultiHookTracker[this.Address].IndexOf(this);
|
|
HookManager.MultiHookTracker[this.Address][index] = null;
|
|
}
|
|
|
|
HookManager.TrackedHooks.TryRemove(this.HookId, out _);
|
|
|
|
base.Dispose();
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public override void Enable()
|
|
{
|
|
lock (HookManager.HookEnableSyncRoot)
|
|
{
|
|
this.CheckDisposed();
|
|
|
|
if (!this.minHookImpl.Enabled)
|
|
return;
|
|
|
|
this.minHookImpl.Enable();
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public override void Disable()
|
|
{
|
|
lock (HookManager.HookEnableSyncRoot)
|
|
{
|
|
if (this.IsDisposed)
|
|
return;
|
|
|
|
if (!this.minHookImpl.Enabled)
|
|
return;
|
|
|
|
this.minHookImpl.Disable();
|
|
}
|
|
}
|
|
}
|