using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using Dalamud.Configuration.Internal;
using Dalamud.Game;
using Dalamud.Game.ClientState.Objects.Types;
using Dalamud.Interface;
using Dalamud.Interface.Colors;
using Dalamud.Logging.Internal;
using ImGuiNET;
using Microsoft.Win32;
using Serilog;
namespace Dalamud.Utility;
///
/// Class providing various helper methods for use in Dalamud and plugins.
///
public static class Util
{
private static string? gitHashInternal;
private static string? gitHashClientStructsInternal;
private static ulong moduleStartAddr;
private static ulong moduleEndAddr;
///
/// Gets an httpclient for usage.
/// Do NOT await this.
///
public static HttpClient HttpClient { get; } = new();
///
/// Gets the assembly version of Dalamud.
///
public static string AssemblyVersion { get; } = Assembly.GetAssembly(typeof(ChatHandlers)).GetName().Version.ToString();
///
/// Check two byte arrays for equality.
///
/// The first byte array.
/// The second byte array.
/// Whether or not the byte arrays are equal.
public static unsafe bool FastByteArrayCompare(byte[]? a1, byte[]? a2)
{
// Copyright (c) 2008-2013 Hafthor Stefansson
// Distributed under the MIT/X11 software license
// Ref: http://www.opensource.org/licenses/mit-license.php.
// https://stackoverflow.com/a/8808245
if (a1 == a2) return true;
if (a1 == null || a2 == null || a1.Length != a2.Length)
return false;
fixed (byte* p1 = a1, p2 = a2)
{
byte* x1 = p1, x2 = p2;
var l = a1.Length;
for (var i = 0; i < l / 8; i++, x1 += 8, x2 += 8)
{
if (*((long*)x1) != *((long*)x2))
return false;
}
if ((l & 4) != 0)
{
if (*((int*)x1) != *((int*)x2))
return false;
x1 += 4;
x2 += 4;
}
if ((l & 2) != 0)
{
if (*((short*)x1) != *((short*)x2))
return false;
x1 += 2;
x2 += 2;
}
if ((l & 1) != 0)
{
if (*((byte*)x1) != *((byte*)x2))
return false;
}
return true;
}
}
///
/// Gets the git hash value from the assembly
/// or null if it cannot be found.
///
/// The git hash of the assembly.
public static string GetGitHash()
{
if (gitHashInternal != null)
return gitHashInternal;
var asm = typeof(Util).Assembly;
var attrs = asm.GetCustomAttributes();
gitHashInternal = attrs.First(a => a.Key == "GitHash").Value;
return gitHashInternal;
}
///
/// Gets the git hash value from the assembly
/// or null if it cannot be found.
///
/// The git hash of the assembly.
public static string GetGitHashClientStructs()
{
if (gitHashClientStructsInternal != null)
return gitHashClientStructsInternal;
var asm = typeof(Util).Assembly;
var attrs = asm.GetCustomAttributes();
gitHashClientStructsInternal = attrs.First(a => a.Key == "GitHashClientStructs").Value;
return gitHashClientStructsInternal;
}
///
/// Read memory from an offset and hexdump them via Serilog.
///
/// The offset to read from.
/// The length to read.
public static void DumpMemory(IntPtr offset, int len = 512)
{
try
{
SafeMemory.ReadBytes(offset, len, out var data);
Log.Information(ByteArrayToHex(data));
}
catch (Exception ex)
{
Log.Error(ex, "Read failed");
}
}
///
/// Create a hexdump of the provided bytes.
///
/// The bytes to hexdump.
/// The offset in the byte array to start at.
/// The amount of bytes to display per line.
/// The generated hexdump in string form.
public static string ByteArrayToHex(byte[] bytes, int offset = 0, int bytesPerLine = 16)
{
if (bytes == null) return string.Empty;
var hexChars = "0123456789ABCDEF".ToCharArray();
var offsetBlock = 8 + 3;
var byteBlock = offsetBlock + (bytesPerLine * 3) + ((bytesPerLine - 1) / 8) + 2;
var lineLength = byteBlock + bytesPerLine + Environment.NewLine.Length;
var line = (new string(' ', lineLength - Environment.NewLine.Length) + Environment.NewLine).ToCharArray();
var numLines = (bytes.Length + bytesPerLine - 1) / bytesPerLine;
var sb = new StringBuilder(numLines * lineLength);
for (var i = 0; i < bytes.Length; i += bytesPerLine)
{
var h = i + offset;
line[0] = hexChars[(h >> 28) & 0xF];
line[1] = hexChars[(h >> 24) & 0xF];
line[2] = hexChars[(h >> 20) & 0xF];
line[3] = hexChars[(h >> 16) & 0xF];
line[4] = hexChars[(h >> 12) & 0xF];
line[5] = hexChars[(h >> 8) & 0xF];
line[6] = hexChars[(h >> 4) & 0xF];
line[7] = hexChars[(h >> 0) & 0xF];
var hexColumn = offsetBlock;
var charColumn = byteBlock;
for (var j = 0; j < bytesPerLine; j++)
{
if (j > 0 && (j & 7) == 0) hexColumn++;
if (i + j >= bytes.Length)
{
line[hexColumn] = ' ';
line[hexColumn + 1] = ' ';
line[charColumn] = ' ';
}
else
{
var by = bytes[i + j];
line[hexColumn] = hexChars[(by >> 4) & 0xF];
line[hexColumn + 1] = hexChars[by & 0xF];
line[charColumn] = by < 32 ? '.' : (char)by;
}
hexColumn += 3;
charColumn++;
}
sb.Append(line);
}
return sb.ToString().TrimEnd(Environment.NewLine.ToCharArray());
}
///
/// Show a structure in an ImGui context.
///
/// The structure to show.
/// The address to the structure.
/// Whether or not this structure should start out expanded.
/// The already followed path.
public static void ShowStruct(object obj, ulong addr, bool autoExpand = false, IEnumerable? path = null)
{
ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, new Vector2(3, 2));
path ??= new List();
if (moduleEndAddr == 0 && moduleStartAddr == 0)
{
try
{
var processModule = Process.GetCurrentProcess().MainModule;
if (processModule != null)
{
moduleStartAddr = (ulong)processModule.BaseAddress.ToInt64();
moduleEndAddr = moduleStartAddr + (ulong)processModule.ModuleMemorySize;
}
else
{
moduleEndAddr = 1;
}
}
catch
{
moduleEndAddr = 1;
}
}
ImGui.PushStyleColor(ImGuiCol.Text, 0xFF00FFFF);
if (autoExpand)
{
ImGui.SetNextItemOpen(true, ImGuiCond.Appearing);
}
if (ImGui.TreeNode($"{obj}##print-obj-{addr:X}-{string.Join("-", path)}"))
{
ImGui.PopStyleColor();
foreach (var f in obj.GetType().GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance))
{
var fixedBuffer = (FixedBufferAttribute)f.GetCustomAttribute(typeof(FixedBufferAttribute));
if (fixedBuffer != null)
{
ImGui.Text($"fixed");
ImGui.SameLine();
ImGui.TextColored(new Vector4(0.2f, 0.9f, 0.9f, 1), $"{fixedBuffer.ElementType.Name}[0x{fixedBuffer.Length:X}]");
}
else
{
ImGui.TextColored(new Vector4(0.2f, 0.9f, 0.9f, 1), $"{f.FieldType.Name}");
}
ImGui.SameLine();
ImGui.TextColored(new Vector4(0.2f, 0.9f, 0.4f, 1), $"{f.Name}: ");
ImGui.SameLine();
ShowValue(addr, new List(path) { f.Name }, f.FieldType, f.GetValue(obj));
}
foreach (var p in obj.GetType().GetProperties().Where(p => p.GetGetMethod()?.GetParameters().Length == 0))
{
ImGui.TextColored(new Vector4(0.2f, 0.9f, 0.9f, 1), $"{p.PropertyType.Name}");
ImGui.SameLine();
ImGui.TextColored(new Vector4(0.2f, 0.6f, 0.4f, 1), $"{p.Name}: ");
ImGui.SameLine();
ShowValue(addr, new List(path) { p.Name }, p.PropertyType, p.GetValue(obj));
}
ImGui.TreePop();
}
else
{
ImGui.PopStyleColor();
}
ImGui.PopStyleVar();
}
///
/// Show a structure in an ImGui context.
///
/// The type of the structure.
/// The pointer to the structure.
/// Whether or not this structure should start out expanded.
public static unsafe void ShowStruct(T* obj, bool autoExpand = false) where T : unmanaged
{
ShowStruct(*obj, (ulong)&obj, autoExpand);
}
///
/// Show a GameObject's internal data in an ImGui-context.
///
/// The GameObject to show.
/// Whether or not the struct should start as expanded.
public static unsafe void ShowGameObjectStruct(GameObject go, bool autoExpand = true)
{
switch (go)
{
case BattleChara bchara:
ShowStruct(bchara.Struct, autoExpand);
break;
case Character chara:
ShowStruct(chara.Struct, autoExpand);
break;
default:
ShowStruct(go.Struct, autoExpand);
break;
}
}
///
/// Show all properties and fields of the provided object via ImGui.
///
/// The object to show.
public static void ShowObject(object obj)
{
var type = obj.GetType();
ImGui.Text($"Object Dump({type.Name}) for {obj}({obj.GetHashCode()})");
ImGuiHelpers.ScaledDummy(5);
ImGui.TextColored(ImGuiColors.DalamudOrange, "-> Properties:");
ImGui.Indent();
foreach (var propertyInfo in type.GetProperties().Where(p => p.GetGetMethod()?.GetParameters().Length == 0))
{
var value = propertyInfo.GetValue(obj);
var valueType = value?.GetType();
if (valueType == typeof(IntPtr))
ImGui.TextColored(ImGuiColors.DalamudOrange, $" {propertyInfo.Name}: 0x{value:X}");
else
ImGui.TextColored(ImGuiColors.DalamudOrange, $" {propertyInfo.Name}: {value}");
}
ImGui.Unindent();
ImGuiHelpers.ScaledDummy(5);
ImGui.TextColored(ImGuiColors.HealerGreen, "-> Fields:");
ImGui.Indent();
foreach (var fieldInfo in type.GetFields())
{
ImGui.TextColored(ImGuiColors.HealerGreen, $" {fieldInfo.Name}: {fieldInfo.GetValue(obj)}");
}
ImGui.Unindent();
}
///
/// Display an error MessageBox and exit the current process.
///
/// MessageBox body.
/// MessageBox caption (title).
/// Specify whether to exit immediately.
public static void Fatal(string message, string caption, bool exit = true)
{
var flags = NativeFunctions.MessageBoxType.Ok | NativeFunctions.MessageBoxType.IconError | NativeFunctions.MessageBoxType.Topmost;
_ = NativeFunctions.MessageBoxW(Process.GetCurrentProcess().MainWindowHandle, message, caption, flags);
if (exit)
Environment.Exit(-1);
}
///
/// Transform byte count to human readable format.
///
/// Number of bytes.
/// Human readable version.
public static string FormatBytes(long bytes)
{
string[] suffix = { "B", "KB", "MB", "GB", "TB" };
int i;
double dblSByte = bytes;
for (i = 0; i < suffix.Length && bytes >= 1024; i++, bytes /= 1024)
{
dblSByte = bytes / 1024.0;
}
return $"{dblSByte:0.00} {suffix[i]}";
}
///
/// Retrieve a UTF8 string from a null terminated byte array.
///
/// A null terminated UTF8 byte array.
/// A UTF8 encoded string.
public static string GetUTF8String(byte[] array)
{
var count = 0;
for (; count < array.Length; count++)
{
if (array[count] == 0)
break;
}
string text;
if (count == array.Length)
{
text = Encoding.UTF8.GetString(array);
Log.Warning($"Warning: text exceeds underlying array length ({text})");
}
else
{
text = Encoding.UTF8.GetString(array, 0, count);
}
return text;
}
///
/// Compress a string using GZip.
///
/// The input string.
/// The compressed output bytes.
public static byte[] CompressString(string str)
{
var bytes = Encoding.UTF8.GetBytes(str);
using var msi = new MemoryStream(bytes);
using var mso = new MemoryStream();
using (var gs = new GZipStream(mso, CompressionMode.Compress))
{
msi.CopyTo(gs);
}
return mso.ToArray();
}
///
/// Decompress a string using GZip.
///
/// The input bytes.
/// The compressed output string.
public static string DecompressString(byte[] bytes)
{
using var msi = new MemoryStream(bytes);
using var mso = new MemoryStream();
using (var gs = new GZipStream(msi, CompressionMode.Decompress))
{
gs.CopyTo(mso);
}
return Encoding.UTF8.GetString(mso.ToArray());
}
///
/// Copy one stream to another.
///
/// The source stream.
/// The destination stream.
/// The maximum length to copy.
[Obsolete("Use Stream.CopyTo() instead", true)]
public static void CopyTo(Stream src, Stream dest, int len = 4069)
{
var bytes = new byte[len];
int cnt;
while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0) dest.Write(bytes, 0, cnt);
}
///
/// Heuristically determine if Dalamud is running on Linux/WINE.
///
/// Whether or not Dalamud is running on Linux/WINE.
public static bool IsLinux()
{
bool Check1()
{
return EnvironmentConfiguration.XlWineOnLinux;
}
bool Check2()
{
var hModule = NativeFunctions.GetModuleHandleW("ntdll.dll");
var proc1 = NativeFunctions.GetProcAddress(hModule, "wine_get_version");
var proc2 = NativeFunctions.GetProcAddress(hModule, "wine_get_build_id");
return proc1 != IntPtr.Zero || proc2 != IntPtr.Zero;
}
bool Check3()
{
return Registry.CurrentUser.OpenSubKey(@"Software\Wine") != null ||
Registry.LocalMachine.OpenSubKey(@"Software\Wine") != null;
}
return Check1() || Check2() || Check3();
}
///
/// Heuristically determine if the Windows version is higher than Windows 11's first build.
///
/// If Windows 11 has been detected.
public static bool IsWindows11() => Environment.OSVersion.Version.Build >= 22000;
///
/// Open a link in the default browser.
///
/// The link to open.
public static void OpenLink(string url)
{
var process = new ProcessStartInfo(url)
{
UseShellExecute = true,
};
Process.Start(process);
}
///
/// Dispose this object.
///
/// The object to dispose.
/// The type of object to dispose.
internal static void ExplicitDispose(this T obj) where T : IDisposable
{
obj.Dispose();
}
///
/// Dispose this object.
///
/// The object to dispose.
/// Log message to print, if specified and an error occurs.
/// Module logger, if any.
/// The type of object to dispose.
internal static void ExplicitDisposeIgnoreExceptions(this T obj, string? logMessage = null, ModuleLog? moduleLog = null) where T : IDisposable
{
try
{
obj.Dispose();
}
catch (Exception e)
{
if (logMessage == null)
return;
if (moduleLog != null)
moduleLog.Error(e, logMessage);
else
Log.Error(e, logMessage);
}
}
///
/// Overwrite text in a file by first writing it to a temporary file, and then
/// moving that file to the path specified.
///
/// The path of the file to write to.
/// The text to write.
internal static void WriteAllTextSafe(string path, string text)
{
var tmpPath = path + ".tmp";
if (File.Exists(tmpPath))
File.Delete(tmpPath);
File.WriteAllText(tmpPath, text);
File.Move(tmpPath, path, true);
}
private static unsafe void ShowValue(ulong addr, IEnumerable path, Type type, object value)
{
if (type.IsPointer)
{
var val = (Pointer)value;
var unboxed = Pointer.Unbox(val);
if (unboxed != null)
{
var unboxedAddr = (ulong)unboxed;
ImGuiHelpers.ClickToCopyText($"{(ulong)unboxed:X}");
if (moduleStartAddr > 0 && unboxedAddr >= moduleStartAddr && unboxedAddr <= moduleEndAddr)
{
ImGui.SameLine();
ImGui.PushStyleColor(ImGuiCol.Text, 0xffcbc0ff);
ImGuiHelpers.ClickToCopyText($"ffxiv_dx11.exe+{unboxedAddr - moduleStartAddr:X}");
ImGui.PopStyleColor();
}
try
{
var eType = type.GetElementType();
var ptrObj = SafeMemory.PtrToStructure(new IntPtr(unboxed), eType);
ImGui.SameLine();
if (ptrObj == null)
{
ImGui.Text("null or invalid");
}
else
{
ShowStruct(ptrObj, (ulong)unboxed, path: new List(path));
}
}
catch
{
// Ignored
}
}
else
{
ImGui.Text("null");
}
}
else
{
if (!type.IsPrimitive)
{
ShowStruct(value, addr, path: new List(path));
}
else
{
ImGui.Text($"{value}");
}
}
}
}