mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-14 20:54:16 +01:00
Update SigScanner.cs
revert style changes
This commit is contained in:
parent
4482f305b0
commit
8fd683c2b4
1 changed files with 32 additions and 36 deletions
|
|
@ -8,17 +8,14 @@ using Serilog;
|
||||||
|
|
||||||
namespace Dalamud.Game {
|
namespace Dalamud.Game {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A SigScanner facilitates searching for memory signatures in a given ProcessModule.
|
/// A SigScanner facilitates searching for memory signatures in a given ProcessModule.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class SigScanner : IDisposable {
|
public sealed class SigScanner : IDisposable {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Set up the SigScanner.
|
/// Set up the SigScanner.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="module">The ProcessModule to be used for scanning</param>
|
/// <param name="module">The ProcessModule to be used for scanning</param>
|
||||||
/// <param name="doCopy">
|
/// <param name="doCopy">Whether or not to copy the module upon initialization for search operations to use, as to not get disturbed by possible hooks.</param>
|
||||||
/// Whether or not to copy the module upon initialization for search operations to use, as to not get
|
|
||||||
/// disturbed by possible hooks.
|
|
||||||
/// </param>
|
|
||||||
public SigScanner(ProcessModule module, bool doCopy = false) {
|
public SigScanner(ProcessModule module, bool doCopy = false) {
|
||||||
Module = module;
|
Module = module;
|
||||||
Is32BitProcess = !Environment.Is64BitProcess;
|
Is32BitProcess = !Environment.Is64BitProcess;
|
||||||
|
|
@ -34,52 +31,52 @@ namespace Dalamud.Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// If the search on this module is performed on a copy.
|
/// If the search on this module is performed on a copy.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsCopy { get; }
|
public bool IsCopy { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// If the ProcessModule is 32-bit.
|
/// If the ProcessModule is 32-bit.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool Is32BitProcess { get; }
|
public bool Is32BitProcess { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The base address of the search area. When copied, this will be the address of the copy.
|
/// The base address of the search area. When copied, this will be the address of the copy.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IntPtr SearchBase => IsCopy ? _moduleCopyPtr : Module.BaseAddress;
|
public IntPtr SearchBase => IsCopy ? this.moduleCopyPtr : Module.BaseAddress;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The base address of the .text section search area.
|
/// The base address of the .text section search area.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IntPtr TextSectionBase => new IntPtr(SearchBase.ToInt64() + TextSectionOffset);
|
public IntPtr TextSectionBase => new IntPtr(SearchBase.ToInt64() + TextSectionOffset);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The offset of the .text section from the base of the module.
|
/// The offset of the .text section from the base of the module.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public long TextSectionOffset { get; private set; }
|
public long TextSectionOffset { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The size of the text section.
|
/// The size of the text section.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int TextSectionSize { get; private set; }
|
public int TextSectionSize { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The base address of the .data section search area.
|
/// The base address of the .data section search area.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IntPtr DataSectionBase => new IntPtr(SearchBase.ToInt64() + DataSectionOffset);
|
public IntPtr DataSectionBase => new IntPtr(SearchBase.ToInt64() + DataSectionOffset);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The offset of the .data section from the base of the module.
|
/// The offset of the .data section from the base of the module.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public long DataSectionOffset { get; private set; }
|
public long DataSectionOffset { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The size of the .data section.
|
/// The size of the .data section.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int DataSectionSize { get; private set; }
|
public int DataSectionSize { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The ProcessModule on which the search is performed.
|
/// The ProcessModule on which the search is performed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ProcessModule Module { get; }
|
public ProcessModule Module { get; }
|
||||||
|
|
||||||
|
|
@ -126,23 +123,23 @@ namespace Dalamud.Game {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private IntPtr _moduleCopyPtr;
|
private IntPtr moduleCopyPtr;
|
||||||
private long _moduleCopyOffset;
|
private long moduleCopyOffset;
|
||||||
|
|
||||||
private unsafe void SetupCopiedSegments() {
|
private unsafe void SetupCopiedSegments() {
|
||||||
Log.Verbose("module copy START");
|
Log.Verbose("module copy START");
|
||||||
// .text
|
// .text
|
||||||
_moduleCopyPtr = Marshal.AllocHGlobal(Module.ModuleMemorySize);
|
this.moduleCopyPtr = Marshal.AllocHGlobal(Module.ModuleMemorySize);
|
||||||
Buffer.MemoryCopy(Module.BaseAddress.ToPointer(), _moduleCopyPtr.ToPointer(), Module.ModuleMemorySize, Module.ModuleMemorySize);
|
Buffer.MemoryCopy(Module.BaseAddress.ToPointer(), this.moduleCopyPtr.ToPointer(), Module.ModuleMemorySize, Module.ModuleMemorySize);
|
||||||
_moduleCopyOffset = _moduleCopyPtr.ToInt64() - Module.BaseAddress.ToInt64();
|
this.moduleCopyOffset = this.moduleCopyPtr.ToInt64() - Module.BaseAddress.ToInt64();
|
||||||
Log.Verbose("copy OK!");
|
Log.Verbose("copy OK!");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Free the memory of the copied module search area on object disposal, if applicable.
|
/// Free the memory of the copied module search area on object disposal, if applicable.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Dispose() {
|
public void Dispose() {
|
||||||
Marshal.FreeHGlobal(_moduleCopyPtr);
|
Marshal.FreeHGlobal(this.moduleCopyPtr);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IntPtr ResolveRelativeAddress(IntPtr nextInstAddr, int relOffset) {
|
public IntPtr ResolveRelativeAddress(IntPtr nextInstAddr, int relOffset) {
|
||||||
|
|
@ -151,15 +148,15 @@ namespace Dalamud.Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Scan for a byte signature in the .text section.
|
/// Scan for a byte signature in the .text section.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="signature">The signature.</param>
|
/// <param name="signature">The signature.</param>
|
||||||
/// <returns>The real offset of the found signature.</returns>
|
/// <returns>The real offset of the found signature.</returns>
|
||||||
public IntPtr ScanText(string signature) {
|
public IntPtr ScanText(string signature) {
|
||||||
var mBase = IsCopy ? _moduleCopyPtr : TextSectionBase;
|
var mBase = IsCopy ? this.moduleCopyPtr : TextSectionBase;
|
||||||
var scanRet = Scan(mBase, TextSectionSize, signature);
|
var scanRet = Scan(mBase, TextSectionSize, signature);
|
||||||
if (IsCopy)
|
if (IsCopy)
|
||||||
scanRet = new IntPtr(scanRet.ToInt64() - _moduleCopyOffset);
|
scanRet = new IntPtr(scanRet.ToInt64() - this.moduleCopyOffset);
|
||||||
var insnByte = Marshal.ReadByte(scanRet);
|
var insnByte = Marshal.ReadByte(scanRet);
|
||||||
if (insnByte == 0xE8 || insnByte == 0xE9)
|
if (insnByte == 0xE8 || insnByte == 0xE9)
|
||||||
return ReadCallSig(scanRet);
|
return ReadCallSig(scanRet);
|
||||||
|
|
@ -167,8 +164,7 @@ namespace Dalamud.Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Helper for ScanText to get the correct address for
|
/// Helper for ScanText to get the correct address for IDA sigs that mark the first CALL location.
|
||||||
/// IDA sigs that mark the first CALL location.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sigLocation">The address the CALL sig resolved to.</param>
|
/// <param name="sigLocation">The address the CALL sig resolved to.</param>
|
||||||
/// <returns>The real offset of the signature.</returns>
|
/// <returns>The real offset of the signature.</returns>
|
||||||
|
|
@ -178,9 +174,9 @@ namespace Dalamud.Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Scan for a .data address using a .text function.
|
/// Scan for a .data address using a .text function.
|
||||||
/// This is intended to be used with IDA sigs.
|
/// This is intended to be used with IDA sigs.
|
||||||
/// Place your cursor on the line calling a static address, and create and IDA sig.
|
/// Place your cursor on the line calling a static address, and create and IDA sig.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="signature">The signature of the function using the data.</param>
|
/// <param name="signature">The signature of the function using the data.</param>
|
||||||
/// <param name="offset">The offset from function start of the instruction using the data.</param>
|
/// <param name="offset">The offset from function start of the instruction using the data.</param>
|
||||||
|
|
@ -199,26 +195,26 @@ namespace Dalamud.Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Scan for a byte signature in the .data section.
|
/// Scan for a byte signature in the .data section.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="signature">The signature.</param>
|
/// <param name="signature">The signature.</param>
|
||||||
/// <returns>The real offset of the found signature.</returns>
|
/// <returns>The real offset of the found signature.</returns>
|
||||||
public IntPtr ScanData(string signature) {
|
public IntPtr ScanData(string signature) {
|
||||||
var scanRet = Scan(DataSectionBase, DataSectionSize, signature);
|
var scanRet = Scan(DataSectionBase, DataSectionSize, signature);
|
||||||
if (IsCopy)
|
if (IsCopy)
|
||||||
scanRet = new IntPtr(scanRet.ToInt64() - _moduleCopyOffset);
|
scanRet = new IntPtr(scanRet.ToInt64() - this.moduleCopyOffset);
|
||||||
return scanRet;
|
return scanRet;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Scan for a byte signature in the whole module search area.
|
/// Scan for a byte signature in the whole module search area.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="signature">The signature.</param>
|
/// <param name="signature">The signature.</param>
|
||||||
/// <returns>The real offset of the found signature.</returns>
|
/// <returns>The real offset of the found signature.</returns>
|
||||||
public IntPtr ScanModule(string signature) {
|
public IntPtr ScanModule(string signature) {
|
||||||
var scanRet = Scan(SearchBase, Module.ModuleMemorySize, signature);
|
var scanRet = Scan(SearchBase, Module.ModuleMemorySize, signature);
|
||||||
if (IsCopy)
|
if (IsCopy)
|
||||||
scanRet = new IntPtr(scanRet.ToInt64() - _moduleCopyOffset);
|
scanRet = new IntPtr(scanRet.ToInt64() - this.moduleCopyOffset);
|
||||||
return scanRet;
|
return scanRet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue