Revert "refactor(Dalamud): switch to file-scoped namespaces"

This reverts commit b5f34c3199.
This commit is contained in:
goat 2021-11-18 15:23:40 +01:00
parent d473826247
commit 1561fbac00
No known key found for this signature in database
GPG key ID: 7773BB5B43BA52E5
325 changed files with 45549 additions and 45209 deletions

View file

@ -15,280 +15,281 @@ using ImGuiNET;
using Microsoft.Win32;
using Serilog;
namespace Dalamud.Utility;
/// <summary>
/// Class providing various helper methods for use in Dalamud and plugins.
/// </summary>
public static class Util
namespace Dalamud.Utility
{
private static string gitHashInternal;
/// <summary>
/// Gets an httpclient for usage.
/// Do NOT await this.
/// Class providing various helper methods for use in Dalamud and plugins.
/// </summary>
public static HttpClient HttpClient { get; } = new();
/// <summary>
/// Gets the assembly version of Dalamud.
/// </summary>
public static string AssemblyVersion { get; } = Assembly.GetAssembly(typeof(ChatHandlers)).GetName().Version.ToString();
/// <summary>
/// Gets the git hash value from the assembly
/// or null if it cannot be found.
/// </summary>
/// <returns>The git hash of the assembly.</returns>
public static string GetGitHash()
public static class Util
{
if (gitHashInternal != null)
private static string gitHashInternal;
/// <summary>
/// Gets an httpclient for usage.
/// Do NOT await this.
/// </summary>
public static HttpClient HttpClient { get; } = new();
/// <summary>
/// Gets the assembly version of Dalamud.
/// </summary>
public static string AssemblyVersion { get; } = Assembly.GetAssembly(typeof(ChatHandlers)).GetName().Version.ToString();
/// <summary>
/// Gets the git hash value from the assembly
/// or null if it cannot be found.
/// </summary>
/// <returns>The git hash of the assembly.</returns>
public static string GetGitHash()
{
if (gitHashInternal != null)
return gitHashInternal;
var asm = typeof(Util).Assembly;
var attrs = asm.GetCustomAttributes<AssemblyMetadataAttribute>();
gitHashInternal = attrs.FirstOrDefault(a => a.Key == "GitHash")?.Value;
return gitHashInternal;
var asm = typeof(Util).Assembly;
var attrs = asm.GetCustomAttributes<AssemblyMetadataAttribute>();
gitHashInternal = attrs.FirstOrDefault(a => a.Key == "GitHash")?.Value;
return gitHashInternal;
}
/// <summary>
/// Read memory from an offset and hexdump them via Serilog.
/// </summary>
/// <param name="offset">The offset to read from.</param>
/// <param name="len">The length to read.</param>
public static void DumpMemory(IntPtr offset, int len = 512)
{
try
{
SafeMemory.ReadBytes(offset, len, out var data);
Log.Information(ByteArrayToHex(data));
}
catch (Exception ex)
/// <summary>
/// Read memory from an offset and hexdump them via Serilog.
/// </summary>
/// <param name="offset">The offset to read from.</param>
/// <param name="len">The length to read.</param>
public static void DumpMemory(IntPtr offset, int len = 512)
{
Log.Error(ex, "Read failed");
}
}
/// <summary>
/// Create a hexdump of the provided bytes.
/// </summary>
/// <param name="bytes">The bytes to hexdump.</param>
/// <param name="offset">The offset in the byte array to start at.</param>
/// <param name="bytesPerLine">The amount of bytes to display per line.</param>
/// <returns>The generated hexdump in string form.</returns>
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++)
try
{
if (j > 0 && (j & 7) == 0) hexColumn++;
SafeMemory.ReadBytes(offset, len, out var data);
Log.Information(ByteArrayToHex(data));
}
catch (Exception ex)
{
Log.Error(ex, "Read failed");
}
}
if (i + j >= bytes.Length)
/// <summary>
/// Create a hexdump of the provided bytes.
/// </summary>
/// <param name="bytes">The bytes to hexdump.</param>
/// <param name="offset">The offset in the byte array to start at.</param>
/// <param name="bytesPerLine">The amount of bytes to display per line.</param>
/// <returns>The generated hexdump in string form.</returns>
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++)
{
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;
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++;
}
hexColumn += 3;
charColumn++;
sb.Append(line);
}
sb.Append(line);
return sb.ToString().TrimEnd(Environment.NewLine.ToCharArray());
}
return sb.ToString().TrimEnd(Environment.NewLine.ToCharArray());
}
/// <summary>
/// Show all properties and fields of the provided object via ImGui.
/// </summary>
/// <param name="obj">The object to show.</param>
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())
/// <summary>
/// Show all properties and fields of the provided object via ImGui.
/// </summary>
/// <param name="obj">The object to show.</param>
public static void ShowObject(object obj)
{
ImGui.TextColored(ImGuiColors.DalamudOrange, $" {propertyInfo.Name}: {propertyInfo.GetValue(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())
{
ImGui.TextColored(ImGuiColors.DalamudOrange, $" {propertyInfo.Name}: {propertyInfo.GetValue(obj)}");
}
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();
}
ImGui.Unindent();
ImGuiHelpers.ScaledDummy(5);
ImGui.TextColored(ImGuiColors.HealerGreen, "-> Fields:");
ImGui.Indent();
foreach (var fieldInfo in type.GetFields())
/// <summary>
/// Display an error MessageBox and exit the current process.
/// </summary>
/// <param name="message">MessageBox body.</param>
/// <param name="caption">MessageBox caption (title).</param>
public static void Fatal(string message, string caption)
{
ImGui.TextColored(ImGuiColors.HealerGreen, $" {fieldInfo.Name}: {fieldInfo.GetValue(obj)}");
var flags = NativeFunctions.MessageBoxType.Ok | NativeFunctions.MessageBoxType.IconError;
_ = NativeFunctions.MessageBoxW(Process.GetCurrentProcess().MainWindowHandle, message, caption, flags);
Environment.Exit(-1);
}
ImGui.Unindent();
}
/// <summary>
/// Display an error MessageBox and exit the current process.
/// </summary>
/// <param name="message">MessageBox body.</param>
/// <param name="caption">MessageBox caption (title).</param>
public static void Fatal(string message, string caption)
{
var flags = NativeFunctions.MessageBoxType.Ok | NativeFunctions.MessageBoxType.IconError;
_ = NativeFunctions.MessageBoxW(Process.GetCurrentProcess().MainWindowHandle, message, caption, flags);
Environment.Exit(-1);
}
/// <summary>
/// Retrieve a UTF8 string from a null terminated byte array.
/// </summary>
/// <param name="array">A null terminated UTF8 byte array.</param>
/// <returns>A UTF8 encoded string.</returns>
public static string GetUTF8String(byte[] array)
{
var count = 0;
for (; count < array.Length; count++)
/// <summary>
/// Retrieve a UTF8 string from a null terminated byte array.
/// </summary>
/// <param name="array">A null terminated UTF8 byte array.</param>
/// <returns>A UTF8 encoded string.</returns>
public static string GetUTF8String(byte[] array)
{
if (array[count] == 0)
break;
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;
}
string text;
if (count == array.Length)
/// <summary>
/// Compress a string using GZip.
/// </summary>
/// <param name="str">The input string.</param>
/// <returns>The compressed output bytes.</returns>
public static byte[] CompressString(string str)
{
text = Encoding.UTF8.GetString(array);
Log.Warning($"Warning: text exceeds underlying array length ({text})");
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);
CopyTo(msi, gs);
return mso.ToArray();
}
else
/// <summary>
/// Decompress a string using GZip.
/// </summary>
/// <param name="bytes">The input bytes.</param>
/// <returns>The compressed output string.</returns>
public static string DecompressString(byte[] bytes)
{
text = Encoding.UTF8.GetString(array, 0, count);
using var msi = new MemoryStream(bytes);
using var mso = new MemoryStream();
using var gs = new GZipStream(msi, CompressionMode.Decompress);
CopyTo(gs, mso);
return Encoding.UTF8.GetString(mso.ToArray());
}
return text;
}
/// <summary>
/// Compress a string using GZip.
/// </summary>
/// <param name="str">The input string.</param>
/// <returns>The compressed output bytes.</returns>
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);
CopyTo(msi, gs);
return mso.ToArray();
}
/// <summary>
/// Decompress a string using GZip.
/// </summary>
/// <param name="bytes">The input bytes.</param>
/// <returns>The compressed output string.</returns>
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);
CopyTo(gs, mso);
return Encoding.UTF8.GetString(mso.ToArray());
}
/// <summary>
/// Copy one stream to another.
/// </summary>
/// <param name="src">The source stream.</param>
/// <param name="dest">The destination stream.</param>
/// <param name="len">The maximum length to copy.</param>
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);
}
/// <summary>
/// Heuristically determine if Dalamud is running on Linux/WINE.
/// </summary>
/// <returns>Whether or not Dalamud is running on Linux/WINE.</returns>
public static bool IsLinux()
{
bool Check1()
/// <summary>
/// Copy one stream to another.
/// </summary>
/// <param name="src">The source stream.</param>
/// <param name="dest">The destination stream.</param>
/// <param name="len">The maximum length to copy.</param>
public static void CopyTo(Stream src, Stream dest, int len = 4069)
{
return EnvironmentConfiguration.XlWineOnLinux;
var bytes = new byte[len];
int cnt;
while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0) dest.Write(bytes, 0, cnt);
}
bool Check2()
/// <summary>
/// Heuristically determine if Dalamud is running on Linux/WINE.
/// </summary>
/// <returns>Whether or not Dalamud is running on Linux/WINE.</returns>
public static bool IsLinux()
{
var hModule = NativeFunctions.GetModuleHandleW("ntdll.dll");
var proc1 = NativeFunctions.GetProcAddress(hModule, "wine_get_version");
var proc2 = NativeFunctions.GetProcAddress(hModule, "wine_get_build_id");
bool Check1()
{
return EnvironmentConfiguration.XlWineOnLinux;
}
return proc1 != IntPtr.Zero || proc2 != IntPtr.Zero;
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();
}
bool Check3()
{
return Registry.CurrentUser.OpenSubKey(@"Software\Wine") != null ||
Registry.LocalMachine.OpenSubKey(@"Software\Wine") != null;
}
return Check1() || Check2() || Check3();
}
}