From ca08a8ab28f2ca0fe8254a0c9e5f5116d17ec737 Mon Sep 17 00:00:00 2001 From: Raymond Date: Mon, 16 Aug 2021 08:52:46 -0400 Subject: [PATCH] feat: add ReadStringNullTerminated, remove confusion --- Dalamud/Memory/MemoryHelper.cs | 66 +++++++++++++++++----------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/Dalamud/Memory/MemoryHelper.cs b/Dalamud/Memory/MemoryHelper.cs index cf483b65b..08c624bfc 100644 --- a/Dalamud/Memory/MemoryHelper.cs +++ b/Dalamud/Memory/MemoryHelper.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; @@ -20,7 +19,6 @@ namespace Dalamud.Memory public static unsafe class MemoryHelper { private static SeStringManager seStringManager; - private static IntPtr handle; #region Read @@ -174,8 +172,23 @@ namespace Dalamud.Memory /// /// The memory address to read from. /// The read in string. - public static string ReadString(IntPtr memoryAddress) - => ReadString(memoryAddress, 256); + public static string ReadStringNullTerminated(IntPtr memoryAddress) + => ReadStringNullTerminated(memoryAddress, Encoding.UTF8); + + /// + /// Read a string with the given encoding from a specified memory address. + /// + /// + /// Attention! If this is an SeString, use the to decode or the applicable helper method. + /// + /// The memory address to read from. + /// The encoding to use to decode the string. + /// The read in string. + public static string ReadStringNullTerminated(IntPtr memoryAddress, Encoding encoding) + { + var buffer = ReadRawNullTerminated(memoryAddress); + return encoding.GetString(buffer); + } /// /// Read a UTF-8 encoded string from a specified memory address. @@ -189,18 +202,6 @@ namespace Dalamud.Memory public static string ReadString(IntPtr memoryAddress, int maxLength) => ReadString(memoryAddress, Encoding.UTF8, maxLength); - /// - /// Read a string with the given encoding from a specified memory address. - /// - /// - /// Attention! If this is an SeString, use the to decode or the applicable helper method. - /// - /// The memory address to read from. - /// The encoding to use to decode the string. - /// The read in string. - public static string ReadString(IntPtr memoryAddress, Encoding encoding) - => ReadString(memoryAddress, encoding, 256); - /// /// Read a string with the given encoding from a specified memory address. /// @@ -288,8 +289,20 @@ namespace Dalamud.Memory /// /// The memory address to read from. /// The read in string. - public static void ReadString(IntPtr memoryAddress, out string value) - => value = ReadString(memoryAddress); + public static void ReadStringNullTerminated(IntPtr memoryAddress, out string value) + => value = ReadStringNullTerminated(memoryAddress); + + /// + /// Read a string with the given encoding from a specified memory address. + /// + /// + /// Attention! If this is an SeString, use the to decode or the applicable helper method. + /// + /// The memory address to read from. + /// The encoding to use to decode the string. + /// The read in string. + public static void ReadStringNullTerminated(IntPtr memoryAddress, Encoding encoding, out string value) + => value = ReadStringNullTerminated(memoryAddress, encoding); /// /// Read a UTF-8 encoded string from a specified memory address. @@ -303,18 +316,6 @@ namespace Dalamud.Memory public static void ReadString(IntPtr memoryAddress, out string value, int maxLength) => value = ReadString(memoryAddress, maxLength); - /// - /// Read a string with the given encoding from a specified memory address. - /// - /// - /// Attention! If this is an SeString, use the to decode or the applicable helper method. - /// - /// The memory address to read from. - /// The encoding to use to decode the string. - /// The read in string. - public static void ReadString(IntPtr memoryAddress, Encoding encoding, out string value) - => value = ReadString(memoryAddress, encoding); - /// /// Read a string with the given encoding from a specified memory address. /// @@ -584,7 +585,7 @@ namespace Dalamud.Memory public static void ReadProcessMemory(IntPtr memoryAddress, ref byte[] value) { var length = value.Length; - var result = NativeFunctions.ReadProcessMemory(handle, memoryAddress, value, length, out _); + var result = NativeFunctions.ReadProcessMemory((IntPtr)0xFFFFFFFF, memoryAddress, value, length, out _); if (!result) throw new MemoryReadException($"Unable to read memory at 0x{memoryAddress.ToInt64():X} of length {length} (result={result})"); @@ -603,7 +604,7 @@ namespace Dalamud.Memory public static void WriteProcessMemory(IntPtr memoryAddress, byte[] data) { var length = data.Length; - var result = NativeFunctions.WriteProcessMemory(handle, memoryAddress, data, length, out _); + var result = NativeFunctions.WriteProcessMemory((IntPtr)0xFFFFFFFF, memoryAddress, data, length, out _); if (!result) throw new MemoryWriteException($"Unable to write memory at 0x{memoryAddress.ToInt64():X} of length {length} (result={result})"); @@ -662,7 +663,6 @@ namespace Dalamud.Memory internal static void Initialize(Dalamud dalamud) { seStringManager = dalamud.SeStringManager; - handle = Process.GetCurrentProcess().Handle; } } }