From 2afd6b966e4c79c7c11bbcd9a2b2c8c074a91495 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 22 Jan 2025 17:36:01 +0100 Subject: [PATCH] Add debug logging facilities. --- OtterGui | 2 +- Penumbra.String | 2 +- Penumbra/DebugConfiguration.cs | 6 ++++ Penumbra/Meta/Files/ImcFile.cs | 29 +++++++++++++++++-- .../UI/Tabs/Debug/DebugConfigurationDrawer.cs | 14 +++++++++ Penumbra/UI/Tabs/Debug/DebugTab.cs | 1 + 6 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 Penumbra/DebugConfiguration.cs create mode 100644 Penumbra/UI/Tabs/Debug/DebugConfigurationDrawer.cs diff --git a/OtterGui b/OtterGui index 055f1695..3c1260c9 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 055f169572223fd1b59389549c88b4c861c94608 +Subproject commit 3c1260c9833303c2d33d12d6f77dc2b1afea3f34 diff --git a/Penumbra.String b/Penumbra.String index b9003b97..0bc2b0f6 160000 --- a/Penumbra.String +++ b/Penumbra.String @@ -1 +1 @@ -Subproject commit b9003b97da2d1191fa203a4d66956bc54c21db2a +Subproject commit 0bc2b0f66eee1a02c9575b2bb30f27ce166f8632 diff --git a/Penumbra/DebugConfiguration.cs b/Penumbra/DebugConfiguration.cs new file mode 100644 index 00000000..76987df8 --- /dev/null +++ b/Penumbra/DebugConfiguration.cs @@ -0,0 +1,6 @@ +namespace Penumbra; + +public class DebugConfiguration +{ + public static bool WriteImcBytesToLog = false; +} diff --git a/Penumbra/Meta/Files/ImcFile.cs b/Penumbra/Meta/Files/ImcFile.cs index c6e4ec94..23339cfc 100644 --- a/Penumbra/Meta/Files/ImcFile.cs +++ b/Penumbra/Meta/Files/ImcFile.cs @@ -1,3 +1,4 @@ +using OtterGui; using Penumbra.GameData; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; @@ -194,11 +195,28 @@ public unsafe class ImcFile : MetaBaseFile { var (data, length) = resource->GetData(); var actualLength = ActualLength; + + if (DebugConfiguration.WriteImcBytesToLog) + { + Penumbra.Log.Information($"Default IMC file -> Modified IMC File for {Path}:"); + Penumbra.Log.Information(new Span((void*)data, length).WriteHexBytes()); + Penumbra.Log.Information(new Span(Data, actualLength).WriteHexBytes()); + Penumbra.Log.Information(new Span(Data, actualLength).WriteHexByteDiff(new Span((void*)data, length))); + } + if (length >= actualLength) { MemoryUtility.MemCpyUnchecked((byte*)data, Data, actualLength); if (length > actualLength) MemoryUtility.MemSet((byte*)(data + actualLength), 0, length - actualLength); + if (DebugConfiguration.WriteImcBytesToLog) + { + Penumbra.Log.Information( + $"Copied {actualLength} bytes from local IMC file into {length} available bytes.{(length > actualLength ? $" Filled remaining {length - actualLength} bytes with 0." : string.Empty)}"); + Penumbra.Log.Information("Result IMC Resource Data:"); + Penumbra.Log.Information(new Span((void*)data, length).WriteHexBytes()); + } + return; } @@ -209,11 +227,18 @@ public unsafe class ImcFile : MetaBaseFile Penumbra.Log.Error($"Could not replace loaded IMC data at 0x{(ulong)resource:X}, allocation failed."); return; } - + MemoryUtility.MemCpyUnchecked(newData, Data, actualLength); if (paddedLength > actualLength) MemoryUtility.MemSet(newData + actualLength, 0, paddedLength - actualLength); - + if (DebugConfiguration.WriteImcBytesToLog) + { + Penumbra.Log.Information( + $"Allocated {paddedLength} bytes for IMC file, copied {actualLength} bytes from local IMC file. {(length > actualLength ? $" Filled remaining {length - actualLength} bytes with 0." : string.Empty)}"); + Penumbra.Log.Information("Result IMC Resource Data:"); + Penumbra.Log.Information(new Span(newData, paddedLength).WriteHexBytes()); + } + Manager.XivFileAllocator.Release((void*)data, length); resource->SetData((nint)newData, paddedLength); } diff --git a/Penumbra/UI/Tabs/Debug/DebugConfigurationDrawer.cs b/Penumbra/UI/Tabs/Debug/DebugConfigurationDrawer.cs new file mode 100644 index 00000000..34aafbea --- /dev/null +++ b/Penumbra/UI/Tabs/Debug/DebugConfigurationDrawer.cs @@ -0,0 +1,14 @@ +using OtterGui.Text; + +namespace Penumbra.UI.Tabs.Debug; + +public static class DebugConfigurationDrawer +{ + public static void Draw() + { + if (!ImUtf8.CollapsingHeaderId("Debug Logging Options")) + return; + + ImUtf8.Checkbox("Log IMC File Replacements"u8, ref DebugConfiguration.WriteImcBytesToLog); + } +} diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index 77eeb3d7..ad4824c3 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -181,6 +181,7 @@ public class DebugTab : Window, ITab, IUiService DrawDebugTabGeneral(); _crashHandlerPanel.Draw(); + DebugConfigurationDrawer.Draw(); _diagnostics.DrawDiagnostics(); DrawPerformanceTab(); DrawPathResolverDebug();