feat: add ReadRawNullTerminated, ReadSeStringNullTerminated to MemoryHelper.cs

This commit is contained in:
goat 2021-08-14 21:52:39 +02:00
parent 2f6009035b
commit 309a89acfd
No known key found for this signature in database
GPG key ID: F18F057873895461

View file

@ -48,11 +48,11 @@ namespace Dalamud.Memory
} }
/// <summary> /// <summary>
/// Reads a generic type from a specified memory address. /// Reads a byte array from a specified memory address.
/// </summary> /// </summary>
/// <param name="memoryAddress">The memory address to read from.</param> /// <param name="memoryAddress">The memory address to read from.</param>
/// <param name="length">The amount of bytes to read starting from the memoryAddress.</param> /// <param name="length">The amount of bytes to read starting from the memoryAddress.</param>
/// <returns>The read in struct.</returns> /// <returns>The read in byte array.</returns>
public static byte[] ReadRaw(IntPtr memoryAddress, int length) public static byte[] ReadRaw(IntPtr memoryAddress, int length)
{ {
var value = new byte[length]; var value = new byte[length];
@ -93,6 +93,22 @@ namespace Dalamud.Memory
return value; return value;
} }
/// <summary>
/// Reads a null-terminated byte array from a specified memory address.
/// </summary>
/// <param name="memoryAddress">The memory address to read from.</param>
/// <returns>The read in byte array.</returns>
public static unsafe byte[] ReadRawNullTerminated(IntPtr memoryAddress)
{
var byteCount = 0;
while (*(byte*)(memoryAddress + byteCount) != 0x00)
{
byteCount++;
}
return ReadRaw(memoryAddress, byteCount);
}
#endregion #endregion
#region Read(out) #region Read(out)
@ -207,13 +223,24 @@ namespace Dalamud.Memory
return eosPos >= 0 ? data.Substring(0, eosPos) : data; return eosPos >= 0 ? data.Substring(0, eosPos) : data;
} }
/// <summary>
/// Read a null-terminated SeString from a specified memory address.
/// </summary>
/// <param name="memoryAddress">The memory address to read from.</param>
/// <returns>The read in string.</returns>
public static SeString ReadSeStringNullTerminated(IntPtr memoryAddress)
{
var buffer = ReadRawNullTerminated(memoryAddress);
return seStringManager.Parse(buffer);
}
/// <summary> /// <summary>
/// Read an SeString from a specified memory address. /// Read an SeString from a specified memory address.
/// </summary> /// </summary>
/// <param name="memoryAddress">The memory address to read from.</param> /// <param name="memoryAddress">The memory address to read from.</param>
/// <param name="maxLength">The maximum length of the string.</param> /// <param name="maxLength">The maximum length of the string.</param>
/// <returns>The read in string.</returns> /// <returns>The read in string.</returns>
public static SeString ReadSeString(IntPtr memoryAddress, int maxLength = 256) public static SeString ReadSeString(IntPtr memoryAddress, int maxLength)
{ {
ReadRaw(memoryAddress, maxLength, out var buffer); ReadRaw(memoryAddress, maxLength, out var buffer);
@ -302,12 +329,12 @@ namespace Dalamud.Memory
=> value = ReadString(memoryAddress, encoding, maxLength); => value = ReadString(memoryAddress, encoding, maxLength);
/// <summary> /// <summary>
/// Read an SeString from a specified memory address. /// Read a null-terminated SeString from a specified memory address.
/// </summary> /// </summary>
/// <param name="memoryAddress">The memory address to read from.</param> /// <param name="memoryAddress">The memory address to read from.</param>
/// <param name="value">The read in SeString.</param> /// <param name="value">The read in SeString.</param>
public static void ReadSeString(IntPtr memoryAddress, out SeString value) public static void ReadSeStringNullTerminated(IntPtr memoryAddress, out SeString value)
=> value = ReadSeString(memoryAddress); => value = ReadSeStringNullTerminated(memoryAddress);
/// <summary> /// <summary>
/// Read an SeString from a specified memory address. /// Read an SeString from a specified memory address.