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

@ -2,72 +2,73 @@ using System;
using System.Diagnostics;
using System.Reflection;
namespace Dalamud.Hooking.Internal;
/// <summary>
/// Class containing information about registered hooks.
/// </summary>
internal class HookInfo
namespace Dalamud.Hooking.Internal
{
private ulong? inProcessMemory = 0;
/// <summary>
/// Initializes a new instance of the <see cref="HookInfo"/> class.
/// Class containing information about registered hooks.
/// </summary>
/// <param name="hook">The tracked hook.</param>
/// <param name="hookDelegate">The hook delegate.</param>
/// <param name="assembly">The assembly implementing the hook.</param>
public HookInfo(IDalamudHook hook, Delegate hookDelegate, Assembly assembly)
internal class HookInfo
{
this.Hook = hook;
this.Delegate = hookDelegate;
this.Assembly = assembly;
}
private ulong? inProcessMemory = 0;
/// <summary>
/// Gets the RVA of the hook.
/// </summary>
internal ulong? InProcessMemory
{
get
/// <summary>
/// Initializes a new instance of the <see cref="HookInfo"/> class.
/// </summary>
/// <param name="hook">The tracked hook.</param>
/// <param name="hookDelegate">The hook delegate.</param>
/// <param name="assembly">The assembly implementing the hook.</param>
public HookInfo(IDalamudHook hook, Delegate hookDelegate, Assembly assembly)
{
if (this.Hook.IsDisposed)
return 0;
this.Hook = hook;
this.Delegate = hookDelegate;
this.Assembly = assembly;
}
if (this.inProcessMemory == null)
return null;
if (this.inProcessMemory.Value > 0)
return this.inProcessMemory.Value;
var p = Process.GetCurrentProcess().MainModule;
var begin = (ulong)p.BaseAddress.ToInt64();
var end = begin + (ulong)p.ModuleMemorySize;
var hookAddr = (ulong)this.Hook.Address.ToInt64();
if (hookAddr >= begin && hookAddr <= end)
/// <summary>
/// Gets the RVA of the hook.
/// </summary>
internal ulong? InProcessMemory
{
get
{
return this.inProcessMemory = hookAddr - begin;
}
else
{
return this.inProcessMemory = null;
if (this.Hook.IsDisposed)
return 0;
if (this.inProcessMemory == null)
return null;
if (this.inProcessMemory.Value > 0)
return this.inProcessMemory.Value;
var p = Process.GetCurrentProcess().MainModule;
var begin = (ulong)p.BaseAddress.ToInt64();
var end = begin + (ulong)p.ModuleMemorySize;
var hookAddr = (ulong)this.Hook.Address.ToInt64();
if (hookAddr >= begin && hookAddr <= end)
{
return this.inProcessMemory = hookAddr - begin;
}
else
{
return this.inProcessMemory = null;
}
}
}
/// <summary>
/// Gets the tracked hook.
/// </summary>
internal IDalamudHook Hook { get; }
/// <summary>
/// Gets the tracked delegate.
/// </summary>
internal Delegate Delegate { get; }
/// <summary>
/// Gets the assembly implementing the hook.
/// </summary>
internal Assembly Assembly { get; }
}
/// <summary>
/// Gets the tracked hook.
/// </summary>
internal IDalamudHook Hook { get; }
/// <summary>
/// Gets the tracked delegate.
/// </summary>
internal Delegate Delegate { get; }
/// <summary>
/// Gets the assembly implementing the hook.
/// </summary>
internal Assembly Assembly { get; }
}

View file

@ -3,140 +3,143 @@ using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Dalamud.Configuration.Internal;
using Dalamud.Logging.Internal;
using Dalamud.Memory;
using Iced.Intel;
using Microsoft.Win32;
namespace Dalamud.Hooking.Internal;
/// <summary>
/// This class manages the final disposition of hooks, cleaning up any that have not reverted their changes.
/// </summary>
internal class HookManager : IDisposable
namespace Dalamud.Hooking.Internal
{
private static readonly ModuleLog Log = new("HM");
/// <summary>
/// Initializes a new instance of the <see cref="HookManager"/> class.
/// This class manages the final disposition of hooks, cleaning up any that have not reverted their changes.
/// </summary>
public HookManager()
internal class HookManager : IDisposable
{
}
private static readonly ModuleLog Log = new("HM");
/// <summary>
/// Gets a static list of tracked and registered hooks.
/// </summary>
internal static List<HookInfo> TrackedHooks { get; } = new();
/// <summary>
/// Gets a static dictionary of original code for a hooked address.
/// </summary>
internal static Dictionary<IntPtr, byte[]> Originals { get; } = new();
/// <summary>
/// Gets a static dictionary of the number of hooks on a given address.
/// </summary>
internal static Dictionary<IntPtr, List<IDalamudHook?>> MultiHookTracker { get; } = new();
/// <inheritdoc/>
public void Dispose()
{
RevertHooks();
TrackedHooks.Clear();
Originals.Clear();
}
/// <summary>
/// Follow a JMP or Jcc instruction to the next logical location.
/// </summary>
/// <param name="address">Address of the instruction.</param>
/// <returns>The address referenced by the jmp.</returns>
internal static IntPtr FollowJmp(IntPtr address)
{
while (true)
/// <summary>
/// Initializes a new instance of the <see cref="HookManager"/> class.
/// </summary>
public HookManager()
{
var hasOtherHooks = HookManager.Originals.ContainsKey(address);
if (hasOtherHooks)
{
// This address has been hooked already. Do not follow a jmp into a trampoline of our own making.
Log.Verbose($"Detected hook trampoline at {address.ToInt64():X}, stopping jump resolution.");
return address;
}
var bytes = MemoryHelper.ReadRaw(address, 8);
var codeReader = new ByteArrayCodeReader(bytes);
var decoder = Decoder.Create(64, codeReader);
decoder.IP = (ulong)address.ToInt64();
decoder.Decode(out var inst);
if (inst.Mnemonic == Mnemonic.Jmp)
{
var kind = inst.Op0Kind;
IntPtr newAddress;
switch (inst.Op0Kind)
{
case OpKind.NearBranch64:
case OpKind.NearBranch32:
case OpKind.NearBranch16:
newAddress = (IntPtr)inst.NearBranchTarget;
break;
case OpKind.Immediate16:
case OpKind.Immediate8to16:
case OpKind.Immediate8to32:
case OpKind.Immediate8to64:
case OpKind.Immediate32to64:
case OpKind.Immediate32 when IntPtr.Size == 4:
case OpKind.Immediate64:
newAddress = (IntPtr)inst.GetImmediate(0);
break;
case OpKind.Memory when inst.IsIPRelativeMemoryOperand:
newAddress = (IntPtr)inst.IPRelativeMemoryAddress;
newAddress = Marshal.ReadIntPtr(newAddress);
break;
case OpKind.Memory:
newAddress = (IntPtr)inst.MemoryDisplacement64;
newAddress = Marshal.ReadIntPtr(newAddress);
break;
default:
var debugBytes = string.Join(" ", bytes.Take(inst.Length).Select(b => $"{b:X2}"));
throw new Exception($"Unknown OpKind {inst.Op0Kind} from {debugBytes}");
}
Log.Verbose($"Resolving assembly jump ({kind}) from {address.ToInt64():X} to {newAddress.ToInt64():X}");
address = newAddress;
}
else
{
break;
}
}
return address;
}
/// <summary>
/// Gets a static list of tracked and registered hooks.
/// </summary>
internal static List<HookInfo> TrackedHooks { get; } = new();
private static unsafe void RevertHooks()
{
foreach (var (address, originalBytes) in Originals)
/// <summary>
/// Gets a static dictionary of original code for a hooked address.
/// </summary>
internal static Dictionary<IntPtr, byte[]> Originals { get; } = new();
/// <summary>
/// Gets a static dictionary of the number of hooks on a given address.
/// </summary>
internal static Dictionary<IntPtr, List<IDalamudHook?>> MultiHookTracker { get; } = new();
/// <inheritdoc/>
public void Dispose()
{
var i = 0;
var current = (byte*)address;
// Find how many bytes have been modified by comparing to the saved original
for (; i < originalBytes.Length; i++)
RevertHooks();
TrackedHooks.Clear();
Originals.Clear();
}
/// <summary>
/// Follow a JMP or Jcc instruction to the next logical location.
/// </summary>
/// <param name="address">Address of the instruction.</param>
/// <returns>The address referenced by the jmp.</returns>
internal static IntPtr FollowJmp(IntPtr address)
{
while (true)
{
if (current[i] == originalBytes[i])
var hasOtherHooks = HookManager.Originals.ContainsKey(address);
if (hasOtherHooks)
{
// This address has been hooked already. Do not follow a jmp into a trampoline of our own making.
Log.Verbose($"Detected hook trampoline at {address.ToInt64():X}, stopping jump resolution.");
return address;
}
var bytes = MemoryHelper.ReadRaw(address, 8);
var codeReader = new ByteArrayCodeReader(bytes);
var decoder = Decoder.Create(64, codeReader);
decoder.IP = (ulong)address.ToInt64();
decoder.Decode(out var inst);
if (inst.Mnemonic == Mnemonic.Jmp)
{
var kind = inst.Op0Kind;
IntPtr newAddress;
switch (inst.Op0Kind)
{
case OpKind.NearBranch64:
case OpKind.NearBranch32:
case OpKind.NearBranch16:
newAddress = (IntPtr)inst.NearBranchTarget;
break;
case OpKind.Immediate16:
case OpKind.Immediate8to16:
case OpKind.Immediate8to32:
case OpKind.Immediate8to64:
case OpKind.Immediate32to64:
case OpKind.Immediate32 when IntPtr.Size == 4:
case OpKind.Immediate64:
newAddress = (IntPtr)inst.GetImmediate(0);
break;
case OpKind.Memory when inst.IsIPRelativeMemoryOperand:
newAddress = (IntPtr)inst.IPRelativeMemoryAddress;
newAddress = Marshal.ReadIntPtr(newAddress);
break;
case OpKind.Memory:
newAddress = (IntPtr)inst.MemoryDisplacement64;
newAddress = Marshal.ReadIntPtr(newAddress);
break;
default:
var debugBytes = string.Join(" ", bytes.Take(inst.Length).Select(b => $"{b:X2}"));
throw new Exception($"Unknown OpKind {inst.Op0Kind} from {debugBytes}");
}
Log.Verbose($"Resolving assembly jump ({kind}) from {address.ToInt64():X} to {newAddress.ToInt64():X}");
address = newAddress;
}
else
{
break;
}
}
var snippet = originalBytes[0..i];
return address;
}
if (i > 0)
private static unsafe void RevertHooks()
{
foreach (var (address, originalBytes) in Originals)
{
Log.Verbose($"Reverting hook at 0x{address.ToInt64():X} ({snippet.Length} bytes)");
MemoryHelper.ChangePermission(address, i, MemoryProtection.ExecuteReadWrite, out var oldPermissions);
MemoryHelper.WriteRaw(address, snippet);
MemoryHelper.ChangePermission(address, i, oldPermissions);
var i = 0;
var current = (byte*)address;
// Find how many bytes have been modified by comparing to the saved original
for (; i < originalBytes.Length; i++)
{
if (current[i] == originalBytes[i])
break;
}
var snippet = originalBytes[0..i];
if (i > 0)
{
Log.Verbose($"Reverting hook at 0x{address.ToInt64():X} ({snippet.Length} bytes)");
MemoryHelper.ChangePermission(address, i, MemoryProtection.ExecuteReadWrite, out var oldPermissions);
MemoryHelper.WriteRaw(address, snippet);
MemoryHelper.ChangePermission(address, i, oldPermissions);
}
}
}
}