Revert "refactor(Dalamud): switch to file-scoped namespaces"

This reverts commit b5f34c3199.
This commit is contained in:
goat 2021-11-18 15:23:40 +01:00
parent d473826247
commit 1561fbac00
No known key found for this signature in database
GPG key ID: 7773BB5B43BA52E5
325 changed files with 45549 additions and 45209 deletions

View file

@ -6,177 +6,178 @@ using Dalamud.Hooking.Internal;
using Dalamud.Memory;
using Reloaded.Hooks;
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>
public sealed class AsmHook : IDisposable, IDalamudHook
namespace Dalamud.Hooking
{
private readonly IntPtr address;
private readonly Reloaded.Hooks.Definitions.IAsmHook hookImpl;
private bool isActivated = false;
private bool isEnabled = false;
private DynamicMethod statsMethod;
/// <summary>
/// Initializes a new instance of the <see cref="AsmHook"/> class.
/// This is an assembly hook and should not be used for except under unique circumstances.
/// Hook is not activated until Enable() method is called.
/// 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>
/// <param name="address">A memory address to install a hook.</param>
/// <param name="assembly">Assembly code representing your hook.</param>
/// <param name="name">The name of what you are hooking, since a delegate is not required.</param>
/// <param name="asmHookBehaviour">How the hook is inserted into the execution flow.</param>
public AsmHook(IntPtr address, byte[] assembly, string name, AsmHookBehaviour asmHookBehaviour = AsmHookBehaviour.ExecuteFirst)
public sealed class AsmHook : IDisposable, IDalamudHook
{
address = HookManager.FollowJmp(address);
private readonly IntPtr address;
private readonly Reloaded.Hooks.Definitions.IAsmHook hookImpl;
var hasOtherHooks = HookManager.Originals.ContainsKey(address);
if (!hasOtherHooks)
private bool isActivated = false;
private bool isEnabled = false;
private DynamicMethod statsMethod;
/// <summary>
/// Initializes a new instance of the <see cref="AsmHook"/> class.
/// This is an assembly hook and should not be used for except under unique circumstances.
/// Hook is not activated until Enable() method is called.
/// </summary>
/// <param name="address">A memory address to install a hook.</param>
/// <param name="assembly">Assembly code representing your hook.</param>
/// <param name="name">The name of what you are hooking, since a delegate is not required.</param>
/// <param name="asmHookBehaviour">How the hook is inserted into the execution flow.</param>
public AsmHook(IntPtr address, byte[] assembly, string name, AsmHookBehaviour asmHookBehaviour = AsmHookBehaviour.ExecuteFirst)
{
MemoryHelper.ReadRaw(address, 0x32, out var original);
HookManager.Originals[address] = original;
address = HookManager.FollowJmp(address);
var hasOtherHooks = HookManager.Originals.ContainsKey(address);
if (!hasOtherHooks)
{
MemoryHelper.ReadRaw(address, 0x32, out var original);
HookManager.Originals[address] = original;
}
this.address = address;
this.hookImpl = ReloadedHooks.Instance.CreateAsmHook(assembly, address.ToInt64(), (Reloaded.Hooks.Definitions.Enums.AsmHookBehaviour)asmHookBehaviour);
this.statsMethod = new DynamicMethod(name, null, null);
this.statsMethod.GetILGenerator().Emit(OpCodes.Ret);
var dele = this.statsMethod.CreateDelegate(typeof(Action));
HookManager.TrackedHooks.Add(new HookInfo(this, dele, Assembly.GetCallingAssembly()));
}
this.address = address;
this.hookImpl = ReloadedHooks.Instance.CreateAsmHook(assembly, address.ToInt64(), (Reloaded.Hooks.Definitions.Enums.AsmHookBehaviour)asmHookBehaviour);
this.statsMethod = new DynamicMethod(name, null, null);
this.statsMethod.GetILGenerator().Emit(OpCodes.Ret);
var dele = this.statsMethod.CreateDelegate(typeof(Action));
HookManager.TrackedHooks.Add(new HookInfo(this, dele, Assembly.GetCallingAssembly()));
}
/// <summary>
/// Initializes a new instance of the <see cref="AsmHook"/> class.
/// This is an assembly hook and should not be used for except under unique circumstances.
/// Hook is not activated until Enable() method is called.
/// </summary>
/// <param name="address">A memory address to install a hook.</param>
/// <param name="assembly">FASM syntax assembly code representing your hook. The first line should be use64.</param>
/// <param name="name">The name of what you are hooking, since a delegate is not required.</param>
/// <param name="asmHookBehaviour">How the hook is inserted into the execution flow.</param>
public AsmHook(IntPtr address, string[] assembly, string name, AsmHookBehaviour asmHookBehaviour = AsmHookBehaviour.ExecuteFirst)
{
address = HookManager.FollowJmp(address);
var hasOtherHooks = HookManager.Originals.ContainsKey(address);
if (!hasOtherHooks)
/// <summary>
/// Initializes a new instance of the <see cref="AsmHook"/> class.
/// This is an assembly hook and should not be used for except under unique circumstances.
/// Hook is not activated until Enable() method is called.
/// </summary>
/// <param name="address">A memory address to install a hook.</param>
/// <param name="assembly">FASM syntax assembly code representing your hook. The first line should be use64.</param>
/// <param name="name">The name of what you are hooking, since a delegate is not required.</param>
/// <param name="asmHookBehaviour">How the hook is inserted into the execution flow.</param>
public AsmHook(IntPtr address, string[] assembly, string name, AsmHookBehaviour asmHookBehaviour = AsmHookBehaviour.ExecuteFirst)
{
MemoryHelper.ReadRaw(address, 0x32, out var original);
HookManager.Originals[address] = original;
address = HookManager.FollowJmp(address);
var hasOtherHooks = HookManager.Originals.ContainsKey(address);
if (!hasOtherHooks)
{
MemoryHelper.ReadRaw(address, 0x32, out var original);
HookManager.Originals[address] = original;
}
this.address = address;
this.hookImpl = ReloadedHooks.Instance.CreateAsmHook(assembly, address.ToInt64(), (Reloaded.Hooks.Definitions.Enums.AsmHookBehaviour)asmHookBehaviour);
this.statsMethod = new DynamicMethod(name, null, null);
this.statsMethod.GetILGenerator().Emit(OpCodes.Ret);
var dele = this.statsMethod.CreateDelegate(typeof(Action));
HookManager.TrackedHooks.Add(new HookInfo(this, dele, Assembly.GetCallingAssembly()));
}
this.address = address;
this.hookImpl = ReloadedHooks.Instance.CreateAsmHook(assembly, address.ToInt64(), (Reloaded.Hooks.Definitions.Enums.AsmHookBehaviour)asmHookBehaviour);
/// <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;
}
}
this.statsMethod = new DynamicMethod(name, null, null);
this.statsMethod.GetILGenerator().Emit(OpCodes.Ret);
var dele = this.statsMethod.CreateDelegate(typeof(Action));
/// <summary>
/// Gets a value indicating whether or not the hook is enabled.
/// </summary>
public bool IsEnabled
{
get
{
this.CheckDisposed();
return this.isEnabled;
}
}
HookManager.TrackedHooks.Add(new HookInfo(this, dele, Assembly.GetCallingAssembly()));
}
/// <summary>
/// Gets a value indicating whether or not the hook has been disposed.
/// </summary>
public bool IsDisposed { get; private set; }
/// <summary>
/// Gets a memory address of the target function.
/// </summary>
/// <exception cref="ObjectDisposedException">Hook is already disposed.</exception>
public IntPtr Address
{
get
/// <inheritdoc/>
public string BackendName => "Reloaded/Asm";
/// <summary>
/// Remove a hook from the current process.
/// </summary>
public void Dispose()
{
if (this.IsDisposed)
return;
this.IsDisposed = true;
if (this.isEnabled)
{
this.isEnabled = false;
this.hookImpl.Disable();
}
}
/// <summary>
/// Starts intercepting a call to the function.
/// </summary>
public void Enable()
{
this.CheckDisposed();
return this.address;
}
}
/// <summary>
/// Gets a value indicating whether or not the hook is enabled.
/// </summary>
public bool IsEnabled
{
get
if (!this.isActivated)
{
this.isActivated = true;
this.hookImpl.Activate();
}
if (!this.isEnabled)
{
this.isEnabled = true;
this.hookImpl.Enable();
}
}
/// <summary>
/// Stops intercepting a call to the function.
/// </summary>
public void Disable()
{
this.CheckDisposed();
return this.isEnabled;
}
}
/// <summary>
/// Gets a value indicating whether or not the hook has been disposed.
/// </summary>
public bool IsDisposed { get; private set; }
if (!this.isEnabled)
return;
/// <inheritdoc/>
public string BackendName => "Reloaded/Asm";
/// <summary>
/// Remove a hook from the current process.
/// </summary>
public void Dispose()
{
if (this.IsDisposed)
return;
this.IsDisposed = true;
if (this.isEnabled)
{
this.isEnabled = false;
this.hookImpl.Disable();
}
}
/// <summary>
/// Starts intercepting a call to the function.
/// </summary>
public void Enable()
{
this.CheckDisposed();
if (!this.isActivated)
{
this.isActivated = true;
this.hookImpl.Activate();
if (this.isEnabled)
{
this.isEnabled = false;
this.hookImpl.Disable();
}
}
if (!this.isEnabled)
/// <summary>
/// Check if this object has been disposed already.
/// </summary>
private void CheckDisposed()
{
this.isEnabled = true;
this.hookImpl.Enable();
}
}
/// <summary>
/// Stops intercepting a call to the function.
/// </summary>
public void Disable()
{
this.CheckDisposed();
if (!this.isEnabled)
return;
if (this.isEnabled)
{
this.isEnabled = false;
this.hookImpl.Disable();
}
}
/// <summary>
/// Check if this object has been disposed already.
/// </summary>
private void CheckDisposed()
{
if (this.IsDisposed)
{
throw new ObjectDisposedException(message: "Hook is already disposed", null);
if (this.IsDisposed)
{
throw new ObjectDisposedException(message: "Hook is already disposed", null);
}
}
}
}