mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-14 12:44:16 +01:00
Hook Changes
This commit is contained in:
parent
42be18965d
commit
984ff81cb5
10 changed files with 231 additions and 434 deletions
|
|
@ -12,6 +12,7 @@ using Dalamud.Game.ClientState;
|
||||||
using Dalamud.Game.Command;
|
using Dalamud.Game.Command;
|
||||||
using Dalamud.Game.Internal;
|
using Dalamud.Game.Internal;
|
||||||
using Dalamud.Game.Network;
|
using Dalamud.Game.Network;
|
||||||
|
using Dalamud.Game.Network.Internal;
|
||||||
using Dalamud.Game.Text.SeStringHandling;
|
using Dalamud.Game.Text.SeStringHandling;
|
||||||
using Dalamud.Hooking.Internal;
|
using Dalamud.Hooking.Internal;
|
||||||
using Dalamud.Interface.Internal;
|
using Dalamud.Interface.Internal;
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,6 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CheapLoc" Version="1.1.5" />
|
<PackageReference Include="CheapLoc" Version="1.1.5" />
|
||||||
<PackageReference Include="CoreHook" Version="1.0.4" />
|
|
||||||
<PackageReference Include="JetBrains.Annotations" Version="2021.1.0" />
|
<PackageReference Include="JetBrains.Annotations" Version="2021.1.0" />
|
||||||
<PackageReference Include="Lib.Harmony" Version="2.1.0" />
|
<PackageReference Include="Lib.Harmony" Version="2.1.0" />
|
||||||
<PackageReference Include="Lumina" Version="3.3.0" />
|
<PackageReference Include="Lumina" Version="3.3.0" />
|
||||||
|
|
@ -107,12 +106,6 @@
|
||||||
</EmbeddedResource>
|
</EmbeddedResource>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<None Include="corehook64.dll">
|
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
|
||||||
</None>
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<Target Name="AddRuntimeDependenciesToContent" BeforeTargets="GetCopyToOutputDirectoryItems" DependsOnTargets="GenerateBuildDependencyFile;GenerateBuildRuntimeConfigurationFiles">
|
<Target Name="AddRuntimeDependenciesToContent" BeforeTargets="GetCopyToOutputDirectoryItems" DependsOnTargets="GenerateBuildDependencyFile;GenerateBuildRuntimeConfigurationFiles">
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ContentWithTargetPath Include="$(ProjectDepsFilePath)" CopyToOutputDirectory="PreserveNewest" TargetPath="$(ProjectDepsFileName)" />
|
<ContentWithTargetPath Include="$(ProjectDepsFilePath)" CopyToOutputDirectory="PreserveNewest" TargetPath="$(ProjectDepsFileName)" />
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,13 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
using Dalamud.Hooking.Internal;
|
using Dalamud.Hooking.Internal;
|
||||||
using Dalamud.Hooking.Internal.Implementations;
|
using Dalamud.Memory;
|
||||||
|
using Iced.Intel;
|
||||||
|
using Reloaded.Hooks;
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
namespace Dalamud.Hooking
|
namespace Dalamud.Hooking
|
||||||
{
|
{
|
||||||
|
|
@ -10,9 +16,10 @@ 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 sealed class Hook<T> : IDisposable where T : Delegate
|
public sealed class Hook<T> : IDisposable, IDalamudHook where T : Delegate
|
||||||
{
|
{
|
||||||
private readonly IDalamudHookImpl<T> hookImpl;
|
private readonly IntPtr address;
|
||||||
|
private readonly Reloaded.Hooks.Definitions.IHook<T> hookImpl;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="Hook{T}"/> class.
|
/// Initializes a new instance of the <see cref="Hook{T}"/> class.
|
||||||
|
|
@ -21,50 +28,64 @@ namespace Dalamud.Hooking
|
||||||
/// <param name="address">A memory address to install a hook.</param>
|
/// <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="detour">Callback function. Delegate must have a same original function prototype.</param>
|
||||||
public Hook(IntPtr address, T detour)
|
public Hook(IntPtr address, T detour)
|
||||||
: this(address, detour, false)
|
|
||||||
{
|
{
|
||||||
}
|
address = FollowJmp(address);
|
||||||
|
|
||||||
/// <summary>
|
var hasOtherHooks = HookManager.Originals.ContainsKey(address);
|
||||||
/// Initializes a new instance of the <see cref="Hook{T}"/> class.
|
if (!hasOtherHooks)
|
||||||
/// Hook is not activated until Enable() method is called.
|
{
|
||||||
/// </summary>
|
MemoryHelper.ReadRaw(address, 0x32, out var original);
|
||||||
/// <param name="address">A memory address to install a hook.</param>
|
HookManager.Originals[address] = original;
|
||||||
/// <param name="detour">Callback function. Delegate must have a same original function prototype.</param>
|
}
|
||||||
/// <param name="followJmp">Follow any JMPs to the actual method that needs hooking.</param>
|
|
||||||
/// <remarks>
|
this.address = address;
|
||||||
/// The followJmp parameter is only used when ReloadedHooks are used, which currently is only for Linux users.
|
this.hookImpl = ReloadedHooks.Instance.CreateHook<T>(detour, address.ToInt64());
|
||||||
/// Generally, this is only necessary when hooking Win32 functions.
|
|
||||||
/// </remarks>
|
HookManager.TrackedHooks.Add(new HookInfo(this, detour, Assembly.GetCallingAssembly()));
|
||||||
public Hook(IntPtr address, T detour, bool followJmp)
|
|
||||||
{
|
|
||||||
this.hookImpl = HookManager.DirtyLinuxUser
|
|
||||||
? new ReloadedHookImpl<T>(address, detour, followJmp)
|
|
||||||
: new CoreHookImpl<T>(address, detour);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a memory address of the target function.
|
/// 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 => this.hookImpl.Address;
|
public IntPtr Address
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
this.CheckDisposed();
|
||||||
|
return this.address;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <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.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <exception cref="ObjectDisposedException">Hook is already disposed.</exception>
|
/// <exception cref="ObjectDisposedException">Hook is already disposed.</exception>
|
||||||
public T Original => this.hookImpl.Original;
|
public T Original
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
this.CheckDisposed();
|
||||||
|
return this.hookImpl.OriginalFunction;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <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 bool IsEnabled => this.hookImpl.IsEnabled;
|
public bool IsEnabled
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
this.CheckDisposed();
|
||||||
|
return this.hookImpl.IsHookEnabled;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <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.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsDisposed => this.hookImpl.IsDisposed;
|
public bool IsDisposed { get; private set; }
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a hook. Hooking address is inferred by calling to GetProcAddress() function.
|
/// Creates a hook. Hooking address is inferred by calling to GetProcAddress() function.
|
||||||
|
|
@ -76,39 +97,138 @@ namespace Dalamud.Hooking
|
||||||
/// <returns>The hook with the supplied parameters.</returns>
|
/// <returns>The hook with the supplied parameters.</returns>
|
||||||
public static Hook<T> FromSymbol(string moduleName, string exportName, T detour)
|
public static Hook<T> FromSymbol(string moduleName, string exportName, T detour)
|
||||||
{
|
{
|
||||||
if (HookManager.DirtyLinuxUser)
|
var moduleHandle = NativeFunctions.GetModuleHandleW(moduleName);
|
||||||
{
|
if (moduleHandle == IntPtr.Zero)
|
||||||
var moduleHandle = NativeFunctions.GetModuleHandleW(moduleName);
|
throw new Exception($"Could not get a handle to module {moduleName}");
|
||||||
if (moduleHandle == IntPtr.Zero)
|
|
||||||
throw new Exception($"Could not get a handle to module {moduleName}");
|
|
||||||
|
|
||||||
var procAddress = NativeFunctions.GetProcAddress(moduleHandle, exportName);
|
var procAddress = NativeFunctions.GetProcAddress(moduleHandle, exportName);
|
||||||
if (procAddress == IntPtr.Zero)
|
if (procAddress == IntPtr.Zero)
|
||||||
throw new Exception($"Could not get the address of {moduleName}::{exportName}");
|
throw new Exception($"Could not get the address of {moduleName}::{exportName}");
|
||||||
|
|
||||||
return new Hook<T>(procAddress, detour, true);
|
return new Hook<T>(procAddress, detour);
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var address = CoreHook.LocalHook.GetProcAddress(moduleName, exportName);
|
|
||||||
return new Hook<T>(address, detour);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Remove a hook from the current process.
|
/// Remove a hook from the current process.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Dispose() => this.hookImpl.Dispose();
|
public void Dispose()
|
||||||
|
{
|
||||||
|
if (this.IsDisposed)
|
||||||
|
return;
|
||||||
|
|
||||||
|
this.IsDisposed = true;
|
||||||
|
|
||||||
|
if (this.hookImpl.IsHookEnabled)
|
||||||
|
this.hookImpl.Disable();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Starts intercepting a call to the function.
|
/// Starts intercepting a call to the function.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Enable() => this.hookImpl.Enable();
|
public void Enable()
|
||||||
|
{
|
||||||
|
this.CheckDisposed();
|
||||||
|
|
||||||
|
if (!this.hookImpl.IsHookActivated)
|
||||||
|
this.hookImpl.Activate();
|
||||||
|
|
||||||
|
if (!this.hookImpl.IsHookEnabled)
|
||||||
|
this.hookImpl.Enable();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Stops intercepting a call to the function.
|
/// Stops intercepting a call to the function.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Disable() => this.hookImpl.Disable();
|
public void Disable()
|
||||||
|
{
|
||||||
|
this.CheckDisposed();
|
||||||
|
|
||||||
|
if (!this.hookImpl.IsHookActivated)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (this.hookImpl.IsHookEnabled)
|
||||||
|
this.hookImpl.Disable();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
private static IntPtr FollowJmp(IntPtr address)
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
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>
|
||||||
|
/// Check if this object has been disposed already.
|
||||||
|
/// </summary>
|
||||||
|
private void CheckDisposed()
|
||||||
|
{
|
||||||
|
if (this.IsDisposed)
|
||||||
|
{
|
||||||
|
throw new ObjectDisposedException("Hook is already disposed.");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Dalamud.Hooking.Internal
|
namespace Dalamud.Hooking
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Interface describing a generic hook.
|
/// Interface describing a generic hook.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal interface IDalamudHook
|
public interface IDalamudHook
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the address to hook.
|
/// Gets the address to hook.
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
|
|
@ -12,6 +11,19 @@ namespace Dalamud.Hooking.Internal
|
||||||
{
|
{
|
||||||
private ulong? inProcessMemory = 0;
|
private ulong? inProcessMemory = 0;
|
||||||
|
|
||||||
|
/// <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)
|
||||||
|
{
|
||||||
|
this.Hook = hook;
|
||||||
|
this.Delegate = hookDelegate;
|
||||||
|
this.Assembly = assembly;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the RVA of the hook.
|
/// Gets the RVA of the hook.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -19,9 +31,14 @@ namespace Dalamud.Hooking.Internal
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (this.Hook.IsDisposed) return 0;
|
if (this.Hook.IsDisposed)
|
||||||
if (this.inProcessMemory == null) return null;
|
return 0;
|
||||||
if (this.inProcessMemory.Value > 0) return this.inProcessMemory.Value;
|
|
||||||
|
if (this.inProcessMemory == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
if (this.inProcessMemory.Value > 0)
|
||||||
|
return this.inProcessMemory.Value;
|
||||||
|
|
||||||
var p = Process.GetCurrentProcess().MainModule;
|
var p = Process.GetCurrentProcess().MainModule;
|
||||||
var begin = (ulong)p.BaseAddress.ToInt64();
|
var begin = (ulong)p.BaseAddress.ToInt64();
|
||||||
|
|
@ -30,30 +47,28 @@ namespace Dalamud.Hooking.Internal
|
||||||
|
|
||||||
if (hookAddr >= begin && hookAddr <= end)
|
if (hookAddr >= begin && hookAddr <= end)
|
||||||
{
|
{
|
||||||
this.inProcessMemory = hookAddr - begin;
|
return this.inProcessMemory = hookAddr - begin;
|
||||||
return this.inProcessMemory.Value;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.inProcessMemory = null;
|
return this.inProcessMemory = null;
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the tracked hook.
|
/// Gets the tracked hook.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal IDalamudHook Hook { get; set; }
|
internal IDalamudHook Hook { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the tracked delegate.
|
/// Gets the tracked delegate.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal Delegate Delegate { get; set; }
|
internal Delegate Delegate { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the hooked assembly.
|
/// Gets the hooked assembly.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal Assembly Assembly { get; set; }
|
internal Assembly Assembly { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
using Dalamud.Memory;
|
using Dalamud.Memory;
|
||||||
|
using Microsoft.Win32;
|
||||||
|
|
||||||
namespace Dalamud.Hooking.Internal
|
namespace Dalamud.Hooking.Internal
|
||||||
{
|
{
|
||||||
|
|
@ -33,8 +34,32 @@ namespace Dalamud.Hooking.Internal
|
||||||
{
|
{
|
||||||
if (checkLinuxOnce)
|
if (checkLinuxOnce)
|
||||||
{
|
{
|
||||||
var value = Environment.GetEnvironmentVariable("XL_WINEONLINUX");
|
checkLinuxOnce = false;
|
||||||
isRunningLinux = value is not null;
|
|
||||||
|
bool Check1()
|
||||||
|
{
|
||||||
|
return Environment.GetEnvironmentVariable("XL_WINEONLINUX") != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Check2()
|
||||||
|
{
|
||||||
|
var hModule = NativeFunctions.GetModuleHandleW("ntdll.dll");
|
||||||
|
var proc1 = NativeFunctions.GetProcAddress(hModule, "wine_get_version");
|
||||||
|
var proc2 = NativeFunctions.GetProcAddress(hModule, "wine_get_build_id");
|
||||||
|
|
||||||
|
return proc1 != IntPtr.Zero || proc2 != IntPtr.Zero;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Check3()
|
||||||
|
{
|
||||||
|
return Registry.CurrentUser.OpenSubKey(@"Software\Wine") != null ||
|
||||||
|
Registry.LocalMachine.OpenSubKey(@"Software\Wine") != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isRunningLinux = Check1() || Check2() || Check3())
|
||||||
|
{
|
||||||
|
Log.Information($"Dalamud detected running on Wine");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return isRunningLinux;
|
return isRunningLinux;
|
||||||
|
|
@ -47,9 +72,9 @@ namespace Dalamud.Hooking.Internal
|
||||||
internal static List<HookInfo> TrackedHooks { get; } = new();
|
internal static List<HookInfo> TrackedHooks { get; } = new();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a static list of original code for a hooked address.
|
/// Gets a static dictionary of original code for a hooked address.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal static List<(IntPtr Address, byte[] Original)> Originals { get; } = new();
|
internal static Dictionary<IntPtr, byte[]> Originals { get; } = new();
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
|
@ -74,7 +99,7 @@ namespace Dalamud.Hooking.Internal
|
||||||
|
|
||||||
if (i > 0)
|
if (i > 0)
|
||||||
{
|
{
|
||||||
Log.Debug($"Reverting hook at 0x{address.ToInt64():X}");
|
Log.Verbose($"Reverting hook at 0x{address.ToInt64():X}");
|
||||||
fixed (byte* original = originalBytes)
|
fixed (byte* original = originalBytes)
|
||||||
{
|
{
|
||||||
MemoryHelper.ChangePermission(address, i, MemoryProtection.ExecuteReadWrite, out var oldPermissions);
|
MemoryHelper.ChangePermission(address, i, MemoryProtection.ExecuteReadWrite, out var oldPermissions);
|
||||||
|
|
|
||||||
|
|
@ -1,133 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
namespace Dalamud.Hooking.Internal.Implementations
|
|
||||||
{
|
|
||||||
/// <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>
|
|
||||||
/// <typeparam name="T">Delegate type to represents a function prototype. This must be the same prototype as original function do.</typeparam>
|
|
||||||
internal sealed class CoreHookImpl<T> : IDisposable, IDalamudHookImpl<T> where T : Delegate
|
|
||||||
{
|
|
||||||
private readonly IntPtr address;
|
|
||||||
|
|
||||||
private readonly CoreHook.LocalHook hookImpl;
|
|
||||||
private readonly T original;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="CoreHookImpl{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>
|
|
||||||
public CoreHookImpl(IntPtr address, T detour)
|
|
||||||
{
|
|
||||||
this.address = address;
|
|
||||||
|
|
||||||
this.hookImpl = CoreHook.LocalHook.Create(address, detour, null);
|
|
||||||
this.original = Marshal.GetDelegateForFunctionPointer<T>(this.hookImpl.OriginalAddress);
|
|
||||||
|
|
||||||
HookManager.TrackedHooks.Add(new HookInfo(this, detour, Assembly.GetCallingAssembly()));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a delegate function that can be used to call the actual function as if function is not hooked yet.
|
|
||||||
/// </summary>
|
|
||||||
/// <exception cref="ObjectDisposedException">Hook is already disposed.</exception>
|
|
||||||
public T Original
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
this.CheckDisposed();
|
|
||||||
return this.original;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a value indicating whether or not the hook is enabled.
|
|
||||||
/// </summary>
|
|
||||||
public bool IsEnabled
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
this.CheckDisposed();
|
|
||||||
return this.hookImpl.ThreadACL.IsExclusive;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a value indicating whether or not the hook has been disposed.
|
|
||||||
/// </summary>
|
|
||||||
public bool IsDisposed { get; private set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a hook. Hooking address is inferred by calling to GetProcAddress() function.
|
|
||||||
/// The 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>
|
|
||||||
/// <returns>The hook with the supplied parameters.</returns>
|
|
||||||
public static IDalamudHookImpl<T> FromSymbol(string moduleName, string exportName, T detour)
|
|
||||||
{
|
|
||||||
var address = CoreHook.LocalHook.GetProcAddress(moduleName, exportName);
|
|
||||||
return new CoreHookImpl<T>(address, detour);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Remove a hook from the current process.
|
|
||||||
/// </summary>
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
if (this.IsDisposed)
|
|
||||||
return;
|
|
||||||
|
|
||||||
this.IsDisposed = true;
|
|
||||||
this.hookImpl.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Starts intercepting a call to the function.
|
|
||||||
/// </summary>
|
|
||||||
public void Enable()
|
|
||||||
{
|
|
||||||
this.CheckDisposed();
|
|
||||||
this.hookImpl.ThreadACL.SetExclusiveACL(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Stops intercepting a call to the function.
|
|
||||||
/// </summary>
|
|
||||||
public void Disable()
|
|
||||||
{
|
|
||||||
this.CheckDisposed();
|
|
||||||
this.hookImpl.ThreadACL.SetExclusiveACL(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Check if this object has been disposed already.
|
|
||||||
/// </summary>
|
|
||||||
private void CheckDisposed()
|
|
||||||
{
|
|
||||||
if (this.IsDisposed)
|
|
||||||
{
|
|
||||||
throw new ObjectDisposedException("Hook is already disposed.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace Dalamud.Hooking.Internal.Implementations
|
|
||||||
{
|
|
||||||
/// <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>
|
|
||||||
/// <typeparam name="T">Delegate type to represents a function prototype. This must be the same prototype as original function do.</typeparam>
|
|
||||||
internal interface IDalamudHookImpl<T> : IDisposable, IDalamudHook where T : Delegate
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a delegate function that can be used to call the actual function as if function is not hooked yet.
|
|
||||||
/// </summary>
|
|
||||||
/// <exception cref="ObjectDisposedException">Hook is already disposed.</exception>
|
|
||||||
public T Original { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Starts intercepting a call to the function.
|
|
||||||
/// </summary>
|
|
||||||
public void Enable();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Stops intercepting a call to the function.
|
|
||||||
/// </summary>
|
|
||||||
public void Disable();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,196 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
using Dalamud.Memory;
|
|
||||||
|
|
||||||
namespace Dalamud.Hooking.Internal.Implementations
|
|
||||||
{
|
|
||||||
/// <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>
|
|
||||||
/// <typeparam name="T">Delegate type to represents a function prototype. This must be the same prototype as original function do.</typeparam>
|
|
||||||
internal sealed class ReloadedHookImpl<T> : IDalamudHookImpl<T> where T : Delegate
|
|
||||||
{
|
|
||||||
private readonly IntPtr address;
|
|
||||||
private readonly Reloaded.Hooks.Definitions.IHook<T> hookImpl;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="ReloadedHookImpl{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>
|
|
||||||
public ReloadedHookImpl(IntPtr address, T detour)
|
|
||||||
: this(address, detour, false)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="ReloadedHookImpl{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="followJmp">Follow any JMPs to the actual method that needs hooking.</param>
|
|
||||||
public ReloadedHookImpl(IntPtr address, T detour, bool followJmp)
|
|
||||||
{
|
|
||||||
if (followJmp)
|
|
||||||
{
|
|
||||||
// This is horrible hackery to follow various types of JMP.
|
|
||||||
// It likely needs to stop when entering a reloaded hook trampoline.
|
|
||||||
// I would much rather use Iced to check against a Instruction type.
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
var b1 = Marshal.ReadByte(address);
|
|
||||||
if (b1 == 0xE9)
|
|
||||||
{
|
|
||||||
var jumpOffset = Marshal.ReadInt32(address + 1);
|
|
||||||
address += jumpOffset + 5;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var b2 = Marshal.ReadByte(address, 1);
|
|
||||||
if (b1 == 0xFF && b2 == 0x25)
|
|
||||||
{
|
|
||||||
address = Marshal.ReadIntPtr(address + 6);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var otherHook = HookManager.Originals.FirstOrDefault(o => o.Address == address);
|
|
||||||
if (otherHook == default)
|
|
||||||
{
|
|
||||||
MemoryHelper.ReadRaw(address, 50, out var original);
|
|
||||||
HookManager.Originals.Add((address, original));
|
|
||||||
}
|
|
||||||
|
|
||||||
this.address = address;
|
|
||||||
this.hookImpl = Reloaded.Hooks.ReloadedHooks.Instance.CreateHook<T>(detour, address.ToInt64());
|
|
||||||
|
|
||||||
HookManager.TrackedHooks.Add(new HookInfo(this, detour, Assembly.GetCallingAssembly()));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a delegate function that can be used to call the actual function as if function is not hooked yet.
|
|
||||||
/// </summary>
|
|
||||||
/// <exception cref="ObjectDisposedException">Hook is already disposed.</exception>
|
|
||||||
public T Original
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
this.CheckDisposed();
|
|
||||||
return this.hookImpl.OriginalFunction;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a value indicating whether or not the hook is enabled.
|
|
||||||
/// </summary>
|
|
||||||
public bool IsEnabled
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
this.CheckDisposed();
|
|
||||||
return this.hookImpl.IsHookEnabled;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a value indicating whether or not the hook has been disposed.
|
|
||||||
/// </summary>
|
|
||||||
public bool IsDisposed { get; private set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a hook. Hooking address is inferred by calling to GetProcAddress() function.
|
|
||||||
/// The 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>
|
|
||||||
/// <returns>The hook with the supplied parameters.</returns>
|
|
||||||
public static IDalamudHookImpl<T> FromSymbol(string moduleName, string exportName, T detour)
|
|
||||||
{
|
|
||||||
var moduleHandle = NativeFunctions.GetModuleHandleW(moduleName);
|
|
||||||
if (moduleHandle == IntPtr.Zero)
|
|
||||||
throw new Exception($"Could not get a handle to module {moduleName}");
|
|
||||||
|
|
||||||
var procAddress = NativeFunctions.GetProcAddress(moduleHandle, exportName);
|
|
||||||
if (procAddress == IntPtr.Zero)
|
|
||||||
throw new Exception($"Could not get the address of {moduleName}::{exportName}");
|
|
||||||
|
|
||||||
return new ReloadedHookImpl<T>(procAddress, detour, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Remove a hook from the current process.
|
|
||||||
/// </summary>
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
if (this.IsDisposed)
|
|
||||||
return;
|
|
||||||
|
|
||||||
this.IsDisposed = true;
|
|
||||||
|
|
||||||
if (this.hookImpl.IsHookEnabled)
|
|
||||||
this.hookImpl.Disable();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Starts intercepting a call to the function.
|
|
||||||
/// </summary>
|
|
||||||
public void Enable()
|
|
||||||
{
|
|
||||||
this.CheckDisposed();
|
|
||||||
|
|
||||||
if (!this.hookImpl.IsHookActivated)
|
|
||||||
this.hookImpl.Activate();
|
|
||||||
|
|
||||||
if (!this.hookImpl.IsHookEnabled)
|
|
||||||
this.hookImpl.Enable();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Stops intercepting a call to the function.
|
|
||||||
/// </summary>
|
|
||||||
public void Disable()
|
|
||||||
{
|
|
||||||
this.CheckDisposed();
|
|
||||||
|
|
||||||
if (!this.hookImpl.IsHookActivated)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (this.hookImpl.IsHookEnabled)
|
|
||||||
this.hookImpl.Disable();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Check if this object has been disposed already.
|
|
||||||
/// </summary>
|
|
||||||
private void CheckDisposed()
|
|
||||||
{
|
|
||||||
if (this.IsDisposed)
|
|
||||||
{
|
|
||||||
throw new ObjectDisposedException("Hook is already disposed.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -104,8 +104,8 @@ namespace Dalamud.Interface.Internal
|
||||||
|
|
||||||
this.setCursorHook = HookManager.DirtyLinuxUser ? null
|
this.setCursorHook = HookManager.DirtyLinuxUser ? null
|
||||||
: Hook<SetCursorDelegate>.FromSymbol("user32.dll", "SetCursor", this.SetCursorDetour);
|
: Hook<SetCursorDelegate>.FromSymbol("user32.dll", "SetCursor", this.SetCursorDetour);
|
||||||
this.presentHook = new Hook<PresentDelegate>(this.address.Present, this.PresentDetour, true);
|
this.presentHook = new Hook<PresentDelegate>(this.address.Present, this.PresentDetour);
|
||||||
this.resizeBuffersHook = new Hook<ResizeBuffersDelegate>(this.address.ResizeBuffers, this.ResizeBuffersDetour, true);
|
this.resizeBuffersHook = new Hook<ResizeBuffersDelegate>(this.address.ResizeBuffers, this.ResizeBuffersDetour);
|
||||||
|
|
||||||
var setCursorAddress = this.setCursorHook?.Address ?? IntPtr.Zero;
|
var setCursorAddress = this.setCursorHook?.Address ?? IntPtr.Zero;
|
||||||
|
|
||||||
|
|
@ -190,7 +190,7 @@ namespace Dalamud.Interface.Internal
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Enable()
|
public void Enable()
|
||||||
{
|
{
|
||||||
this.setCursorHook.Enable();
|
this.setCursorHook?.Enable();
|
||||||
this.presentHook.Enable();
|
this.presentHook.Enable();
|
||||||
this.resizeBuffersHook.Enable();
|
this.resizeBuffersHook.Enable();
|
||||||
|
|
||||||
|
|
@ -226,7 +226,7 @@ namespace Dalamud.Interface.Internal
|
||||||
Thread.Sleep(500);
|
Thread.Sleep(500);
|
||||||
|
|
||||||
this.scene?.Dispose();
|
this.scene?.Dispose();
|
||||||
this.setCursorHook.Dispose();
|
this.setCursorHook?.Dispose();
|
||||||
this.presentHook.Dispose();
|
this.presentHook.Dispose();
|
||||||
this.resizeBuffersHook.Dispose();
|
this.resizeBuffersHook.Dispose();
|
||||||
}
|
}
|
||||||
|
|
@ -502,7 +502,7 @@ namespace Dalamud.Interface.Internal
|
||||||
|
|
||||||
private void Disable()
|
private void Disable()
|
||||||
{
|
{
|
||||||
this.setCursorHook.Disable();
|
this.setCursorHook?.Disable();
|
||||||
this.presentHook.Disable();
|
this.presentHook.Disable();
|
||||||
this.resizeBuffersHook.Disable();
|
this.resizeBuffersHook.Disable();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue