mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-14 20:54:16 +01:00
refactor: remove Hook<T>.compatHookImpl, make abstract
This commit is contained in:
parent
8ecc00f75f
commit
458ae57918
1 changed files with 7 additions and 33 deletions
|
|
@ -1,4 +1,3 @@
|
||||||
using System;
|
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
|
@ -13,7 +12,7 @@ namespace Dalamud.Hooking;
|
||||||
/// 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 class Hook<T> : IDisposable, IDalamudHook where T : Delegate
|
public abstract class Hook<T> : IDisposable, IDalamudHook where T : Delegate
|
||||||
{
|
{
|
||||||
#pragma warning disable SA1310
|
#pragma warning disable SA1310
|
||||||
// ReSharper disable once InconsistentNaming
|
// ReSharper disable once InconsistentNaming
|
||||||
|
|
@ -24,8 +23,6 @@ public class Hook<T> : IDisposable, IDalamudHook where T : Delegate
|
||||||
|
|
||||||
private readonly IntPtr address;
|
private readonly IntPtr address;
|
||||||
|
|
||||||
private readonly Hook<T>? compatHookImpl;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="Hook{T}"/> class.
|
/// Initializes a new instance of the <see cref="Hook{T}"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -52,28 +49,19 @@ public class Hook<T> : IDisposable, IDalamudHook where T : Delegate
|
||||||
/// Gets 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>
|
||||||
/// <exception cref="ObjectDisposedException">Hook is already disposed.</exception>
|
/// <exception cref="ObjectDisposedException">Hook is already disposed.</exception>
|
||||||
public virtual T Original => this.compatHookImpl != null ? this.compatHookImpl!.Original : throw new NotImplementedException();
|
public virtual T Original => throw new NotImplementedException();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets 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.
|
||||||
/// This can be called even after Dispose.
|
/// This can be called even after Dispose.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public T OriginalDisposeSafe
|
public T OriginalDisposeSafe
|
||||||
{
|
=> this.IsDisposed ? Marshal.GetDelegateForFunctionPointer<T>(this.address) : this.Original;
|
||||||
get
|
|
||||||
{
|
|
||||||
if (this.compatHookImpl != null)
|
|
||||||
return this.compatHookImpl!.OriginalDisposeSafe;
|
|
||||||
if (this.IsDisposed)
|
|
||||||
return Marshal.GetDelegateForFunctionPointer<T>(this.address);
|
|
||||||
return this.Original;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a value indicating whether or not the hook is enabled.
|
/// Gets a value indicating whether or not the hook is enabled.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual bool IsEnabled => this.compatHookImpl != null ? this.compatHookImpl!.IsEnabled : throw new NotImplementedException();
|
public virtual bool IsEnabled => throw new NotImplementedException();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a value indicating whether or not the hook has been disposed.
|
/// Gets a value indicating whether or not the hook has been disposed.
|
||||||
|
|
@ -81,7 +69,7 @@ public class Hook<T> : IDisposable, IDalamudHook where T : Delegate
|
||||||
public bool IsDisposed { get; private set; }
|
public bool IsDisposed { get; private set; }
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public virtual string BackendName => this.compatHookImpl != null ? this.compatHookImpl!.BackendName : throw new NotImplementedException();
|
public virtual string BackendName => throw new NotImplementedException();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a hook by rewriting import table address.
|
/// Creates a hook by rewriting import table address.
|
||||||
|
|
@ -230,32 +218,18 @@ public class Hook<T> : IDisposable, IDalamudHook where T : Delegate
|
||||||
if (this.IsDisposed)
|
if (this.IsDisposed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
this.compatHookImpl?.Dispose();
|
|
||||||
|
|
||||||
this.IsDisposed = true;
|
this.IsDisposed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Starts intercepting a call to the function.
|
/// Starts intercepting a call to the function.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual void Enable()
|
public virtual void Enable() => throw new NotImplementedException();
|
||||||
{
|
|
||||||
if (this.compatHookImpl != null)
|
|
||||||
this.compatHookImpl.Enable();
|
|
||||||
else
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Stops intercepting a call to the function.
|
/// Stops intercepting a call to the function.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual void Disable()
|
public virtual void Disable() => throw new NotImplementedException();
|
||||||
{
|
|
||||||
if (this.compatHookImpl != null)
|
|
||||||
this.compatHookImpl.Disable();
|
|
||||||
else
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Check if this object has been disposed already.
|
/// Check if this object has been disposed already.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue