XivCommon/XivCommon/Util.cs

49 lines
1.5 KiB
C#
Raw Normal View History

2021-04-10 20:48:32 +00:00
using System;
using System.Collections.Generic;
2021-08-22 20:46:41 +00:00
using System.Reflection;
2021-05-23 09:43:48 +00:00
using Dalamud.Game.Text.SeStringHandling;
2021-08-22 21:53:37 +00:00
using Dalamud.Plugin;
2021-04-10 20:48:32 +00:00
namespace XivCommon {
internal static class Util {
internal static byte[] Terminate(this byte[] array) {
2021-04-10 20:48:32 +00:00
var terminated = new byte[array.Length + 1];
Array.Copy(array, terminated, array.Length);
2021-07-22 20:33:54 +00:00
terminated[^1] = 0;
2021-04-10 20:48:32 +00:00
return terminated;
}
internal static unsafe byte[] ReadTerminated(IntPtr memory) {
if (memory == IntPtr.Zero) {
2021-08-22 20:46:41 +00:00
return Array.Empty<byte>();
}
2021-04-10 20:48:32 +00:00
var buf = new List<byte>();
var ptr = (byte*) memory;
while (*ptr != 0) {
buf.Add(*ptr);
ptr += 1;
}
return buf.ToArray();
}
2021-08-28 19:02:03 +00:00
internal static SeString ReadSeString(IntPtr memory) {
2021-05-23 09:43:48 +00:00
var terminated = ReadTerminated(memory);
2021-08-28 19:02:03 +00:00
return SeString.Parse(terminated);
2021-05-23 09:43:48 +00:00
}
internal static void PrintMissingSig(string name) {
2021-04-29 03:57:29 +00:00
Logger.LogWarning($"Could not find signature for {name}. This functionality will be disabled.");
}
2021-08-22 20:46:41 +00:00
internal static T GetService<T>() {
2021-08-22 21:53:37 +00:00
var service = typeof(IDalamudPlugin).Assembly.GetType("Dalamud.Service`1")!.MakeGenericType(typeof(T));
2021-08-22 20:46:41 +00:00
var get = service.GetMethod("Get", BindingFlags.Public | BindingFlags.Static)!;
return (T) get.Invoke(null, null)!;
}
2021-04-10 20:48:32 +00:00
}
}