From f5ef9a085a62485040926f0c03385d540040b609 Mon Sep 17 00:00:00 2001 From: Anna Clemens Date: Wed, 22 Sep 2021 16:25:06 -0400 Subject: [PATCH 1/2] feat: add fallible methods to SigScanner --- Dalamud/Game/SigScanner.cs | 105 +++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/Dalamud/Game/SigScanner.cs b/Dalamud/Game/SigScanner.cs index 28bb0137b..d6b5a77a3 100644 --- a/Dalamud/Game/SigScanner.cs +++ b/Dalamud/Game/SigScanner.cs @@ -134,6 +134,28 @@ namespace Dalamud.Game return baseAddress + index; } + /// + /// Try scanning memory for a signature. + /// + /// The base address to scan from. + /// The amount of bytes to scan. + /// The signature to search for. + /// The offset, if found. + /// true if the signature was found. + public static bool TryScan(IntPtr baseAddress, int size, string signature, out IntPtr result) + { + result = IntPtr.Zero; + try + { + result = Scan(baseAddress, size, signature); + return true; + } + catch (KeyNotFoundException) + { + return false; + } + } + /// /// Scan for a .data address using a .text function. /// This is intended to be used with IDA sigs. @@ -160,6 +182,29 @@ namespace Dalamud.Game return IntPtr.Add(instrAddr, Marshal.ReadInt32(instrAddr) + 4); } + /// + /// Try scanning for a .data address using a .text function. + /// This is intended to be used with IDA sigs. + /// Place your cursor on the line calling a static address, and create and IDA sig. + /// + /// The signature of the function using the data. + /// An IntPtr to the static memory location, if found. + /// The offset from function start of the instruction using the data. + /// true if the signature was found. + public bool TryGetStaticAddressFromSig(string signature, out IntPtr result, int offset = 0) + { + result = IntPtr.Zero; + try + { + result = this.GetStaticAddressFromSig(signature, offset); + return true; + } + catch (KeyNotFoundException) + { + return false; + } + } + /// /// Scan for a byte signature in the .data section. /// @@ -175,6 +220,26 @@ namespace Dalamud.Game return scanRet; } + /// + /// Try scanning for a byte signature in the .data section. + /// + /// The signature. + /// The real offset of the signature, if found. + /// true if the signature was found. + public bool TryScanData(string signature, out IntPtr result) + { + result = IntPtr.Zero; + try + { + result = this.ScanData(signature); + return true; + } + catch (KeyNotFoundException) + { + return false; + } + } + /// /// Scan for a byte signature in the whole module search area. /// @@ -190,6 +255,26 @@ namespace Dalamud.Game return scanRet; } + /// + /// Try scanning for a byte signature in the whole module search area. + /// + /// The signature. + /// The real offset of the signature, if found. + /// true if the signature was found. + public bool TryScanModule(string signature, out IntPtr result) + { + result = IntPtr.Zero; + try + { + result = this.ScanModule(signature); + return true; + } + catch (KeyNotFoundException) + { + return false; + } + } + /// /// Resolve a RVA address. /// @@ -224,6 +309,26 @@ namespace Dalamud.Game return scanRet; } + /// + /// Try scanning for a byte signature in the .text section. + /// + /// The signature. + /// The real offset of the signature, if found. + /// true if the signature was found. + public bool TryScanText(string signature, out IntPtr result) + { + result = IntPtr.Zero; + try + { + result = this.ScanText(signature); + return true; + } + catch (KeyNotFoundException) + { + return false; + } + } + /// /// Free the memory of the copied module search area on object disposal, if applicable. /// From 06d3ad80484beda857d7dab566c90d1826f2962d Mon Sep 17 00:00:00 2001 From: Raymond Date: Wed, 22 Sep 2021 17:13:06 -0400 Subject: [PATCH 2/2] formatting --- Dalamud/Game/SigScanner.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Dalamud/Game/SigScanner.cs b/Dalamud/Game/SigScanner.cs index d6b5a77a3..e35b17cc4 100644 --- a/Dalamud/Game/SigScanner.cs +++ b/Dalamud/Game/SigScanner.cs @@ -144,7 +144,6 @@ namespace Dalamud.Game /// true if the signature was found. public static bool TryScan(IntPtr baseAddress, int size, string signature, out IntPtr result) { - result = IntPtr.Zero; try { result = Scan(baseAddress, size, signature); @@ -152,6 +151,7 @@ namespace Dalamud.Game } catch (KeyNotFoundException) { + result = IntPtr.Zero; return false; } } @@ -193,7 +193,6 @@ namespace Dalamud.Game /// true if the signature was found. public bool TryGetStaticAddressFromSig(string signature, out IntPtr result, int offset = 0) { - result = IntPtr.Zero; try { result = this.GetStaticAddressFromSig(signature, offset); @@ -201,6 +200,7 @@ namespace Dalamud.Game } catch (KeyNotFoundException) { + result = IntPtr.Zero; return false; } } @@ -228,7 +228,6 @@ namespace Dalamud.Game /// true if the signature was found. public bool TryScanData(string signature, out IntPtr result) { - result = IntPtr.Zero; try { result = this.ScanData(signature); @@ -236,6 +235,7 @@ namespace Dalamud.Game } catch (KeyNotFoundException) { + result = IntPtr.Zero; return false; } } @@ -263,7 +263,6 @@ namespace Dalamud.Game /// true if the signature was found. public bool TryScanModule(string signature, out IntPtr result) { - result = IntPtr.Zero; try { result = this.ScanModule(signature); @@ -271,6 +270,7 @@ namespace Dalamud.Game } catch (KeyNotFoundException) { + result = IntPtr.Zero; return false; } } @@ -317,7 +317,6 @@ namespace Dalamud.Game /// true if the signature was found. public bool TryScanText(string signature, out IntPtr result) { - result = IntPtr.Zero; try { result = this.ScanText(signature); @@ -325,6 +324,7 @@ namespace Dalamud.Game } catch (KeyNotFoundException) { + result = IntPtr.Zero; return false; } }