2021-04-10 20:48:32 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-05-23 09:43:48 +00:00
|
|
|
|
using Dalamud.Game.Text.SeStringHandling;
|
2021-04-10 20:48:32 +00:00
|
|
|
|
|
2023-10-03 06:25:45 +00:00
|
|
|
|
namespace XivCommon;
|
2021-04-27 19:49:00 +00:00
|
|
|
|
|
2023-10-03 06:25:45 +00:00
|
|
|
|
internal static class Util {
|
|
|
|
|
internal static byte[] Terminate(this byte[] array) {
|
|
|
|
|
var terminated = new byte[array.Length + 1];
|
|
|
|
|
Array.Copy(array, terminated, array.Length);
|
|
|
|
|
terminated[^1] = 0;
|
2021-04-10 20:48:32 +00:00
|
|
|
|
|
2023-10-03 06:25:45 +00:00
|
|
|
|
return terminated;
|
|
|
|
|
}
|
2021-04-10 20:48:32 +00:00
|
|
|
|
|
2023-10-03 06:25:45 +00:00
|
|
|
|
internal static unsafe byte[] ReadTerminated(IntPtr memory) {
|
|
|
|
|
if (memory == IntPtr.Zero) {
|
|
|
|
|
return Array.Empty<byte>();
|
2021-04-10 20:48:32 +00:00
|
|
|
|
}
|
2021-04-24 17:10:13 +00:00
|
|
|
|
|
2023-10-03 06:25:45 +00:00
|
|
|
|
var buf = new List<byte>();
|
2021-05-23 09:43:48 +00:00
|
|
|
|
|
2023-10-03 06:25:45 +00:00
|
|
|
|
var ptr = (byte*) memory;
|
|
|
|
|
while (*ptr != 0) {
|
|
|
|
|
buf.Add(*ptr);
|
|
|
|
|
ptr += 1;
|
2021-04-24 17:10:13 +00:00
|
|
|
|
}
|
2021-08-22 20:46:41 +00:00
|
|
|
|
|
2023-10-03 06:25:45 +00:00
|
|
|
|
return buf.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static SeString ReadSeString(IntPtr memory) {
|
|
|
|
|
var terminated = ReadTerminated(memory);
|
|
|
|
|
return SeString.Parse(terminated);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void PrintMissingSig(string name) {
|
|
|
|
|
Logger.Log.Warning($"Could not find signature for {name}. This functionality will be disabled.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static unsafe IntPtr FollowPointerChain(IntPtr start, IEnumerable<int> offsets) {
|
|
|
|
|
if (start == IntPtr.Zero) {
|
|
|
|
|
return IntPtr.Zero;
|
2021-08-22 20:46:41 +00:00
|
|
|
|
}
|
2023-10-03 06:25:45 +00:00
|
|
|
|
|
|
|
|
|
foreach (var offset in offsets) {
|
|
|
|
|
start = *(IntPtr*) (start + offset);
|
2021-11-09 22:58:52 +00:00
|
|
|
|
if (start == IntPtr.Zero) {
|
|
|
|
|
return IntPtr.Zero;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-03 06:25:45 +00:00
|
|
|
|
|
|
|
|
|
return start;
|
2021-04-10 20:48:32 +00:00
|
|
|
|
}
|
2023-10-03 06:25:45 +00:00
|
|
|
|
}
|