From a3b217b3a04f64b0d1b07f362e16385a8636563e Mon Sep 17 00:00:00 2001 From: goat <16760685+goaaats@users.noreply.github.com> Date: Tue, 7 Dec 2021 22:15:10 +0100 Subject: [PATCH] refactor: PrintOutObject => ShowStructure --- Dalamud/Interface/Internal/UiDebug.cs | 34 +++---- Dalamud/Utility/Util.cs | 139 ++++++++++++++------------ 2 files changed, 90 insertions(+), 83 deletions(-) diff --git a/Dalamud/Interface/Internal/UiDebug.cs b/Dalamud/Interface/Internal/UiDebug.cs index 0a5c27004..9c847dd77 100644 --- a/Dalamud/Interface/Internal/UiDebug.cs +++ b/Dalamud/Interface/Internal/UiDebug.cs @@ -118,7 +118,7 @@ namespace Dalamud.Interface.Internal object addonObj = *atkUnitBase; - Util.PrintOutObject(addonObj, (ulong)atkUnitBase, new List()); + Util.ShowStruct(addonObj, (ulong)atkUnitBase, new List()); ImGui.Dummy(new Vector2(25 * ImGui.GetIO().FontGlobalScale)); ImGui.Separator(); @@ -195,12 +195,12 @@ namespace Dalamud.Interface.Internal ImGui.SameLine(); switch (node->Type) { - case NodeType.Text: Util.PrintOutObject(*(AtkTextNode*)node, (ulong)node); break; - case NodeType.Image: Util.PrintOutObject(*(AtkImageNode*)node, (ulong)node); break; - case NodeType.Collision: Util.PrintOutObject(*(AtkCollisionNode*)node, (ulong)node); break; - case NodeType.NineGrid: Util.PrintOutObject(*(AtkNineGridNode*)node, (ulong)node); break; - case NodeType.Counter: Util.PrintOutObject(*(AtkCounterNode*)node, (ulong)node); break; - default: Util.PrintOutObject(*node, (ulong)node, new List()); break; + case NodeType.Text: Util.ShowStruct(*(AtkTextNode*)node, (ulong)node); break; + case NodeType.Image: Util.ShowStruct(*(AtkImageNode*)node, (ulong)node); break; + case NodeType.Collision: Util.ShowStruct(*(AtkCollisionNode*)node, (ulong)node); break; + case NodeType.NineGrid: Util.ShowStruct(*(AtkNineGridNode*)node, (ulong)node); break; + case NodeType.Counter: Util.ShowStruct(*(AtkCounterNode*)node, (ulong)node); break; + default: Util.ShowStruct(*node, (ulong)node, new List()); break; } this.PrintResNode(node); @@ -329,7 +329,7 @@ namespace Dalamud.Interface.Internal ImGui.SameLine(); ImGuiHelpers.ClickToCopyText($"{(ulong)node:X}"); ImGui.SameLine(); - Util.PrintOutObject(*compNode, (ulong)compNode); + Util.ShowStruct(*compNode, (ulong)compNode); ImGui.Text("Component: "); ImGui.SameLine(); ImGuiHelpers.ClickToCopyText($"{(ulong)compNode->Component:X}"); @@ -337,15 +337,15 @@ namespace Dalamud.Interface.Internal switch (objectInfo->ComponentType) { - case ComponentType.Button: Util.PrintOutObject(*(AtkComponentButton*)compNode->Component, (ulong)compNode->Component); break; - case ComponentType.Slider: Util.PrintOutObject(*(AtkComponentSlider*)compNode->Component, (ulong)compNode->Component); break; - case ComponentType.Window: Util.PrintOutObject(*(AtkComponentWindow*)compNode->Component, (ulong)compNode->Component); break; - case ComponentType.CheckBox: Util.PrintOutObject(*(AtkComponentCheckBox*)compNode->Component, (ulong)compNode->Component); break; - case ComponentType.GaugeBar: Util.PrintOutObject(*(AtkComponentGaugeBar*)compNode->Component, (ulong)compNode->Component); break; - case ComponentType.RadioButton: Util.PrintOutObject(*(AtkComponentRadioButton*)compNode->Component, (ulong)compNode->Component); break; - case ComponentType.TextInput: Util.PrintOutObject(*(AtkComponentTextInput*)compNode->Component, (ulong)compNode->Component); break; - case ComponentType.Icon: Util.PrintOutObject(*(AtkComponentIcon*)compNode->Component, (ulong)compNode->Component); break; - default: Util.PrintOutObject(*compNode->Component, (ulong)compNode->Component); break; + case ComponentType.Button: Util.ShowStruct(*(AtkComponentButton*)compNode->Component, (ulong)compNode->Component); break; + case ComponentType.Slider: Util.ShowStruct(*(AtkComponentSlider*)compNode->Component, (ulong)compNode->Component); break; + case ComponentType.Window: Util.ShowStruct(*(AtkComponentWindow*)compNode->Component, (ulong)compNode->Component); break; + case ComponentType.CheckBox: Util.ShowStruct(*(AtkComponentCheckBox*)compNode->Component, (ulong)compNode->Component); break; + case ComponentType.GaugeBar: Util.ShowStruct(*(AtkComponentGaugeBar*)compNode->Component, (ulong)compNode->Component); break; + case ComponentType.RadioButton: Util.ShowStruct(*(AtkComponentRadioButton*)compNode->Component, (ulong)compNode->Component); break; + case ComponentType.TextInput: Util.ShowStruct(*(AtkComponentTextInput*)compNode->Component, (ulong)compNode->Component); break; + case ComponentType.Icon: Util.ShowStruct(*(AtkComponentIcon*)compNode->Component, (ulong)compNode->Component); break; + default: Util.ShowStruct(*compNode->Component, (ulong)compNode->Component); break; } this.PrintResNode(node); diff --git a/Dalamud/Utility/Util.cs b/Dalamud/Utility/Util.cs index f545c1877..40d092d18 100644 --- a/Dalamud/Utility/Util.cs +++ b/Dalamud/Utility/Util.cs @@ -29,6 +29,9 @@ namespace Dalamud.Utility 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. @@ -160,86 +163,35 @@ namespace Dalamud.Utility return sb.ToString().TrimEnd(Environment.NewLine.ToCharArray()); } - private static ulong ModuleStartAddr; - private static ulong ModuleEndAddr; - - private static unsafe void PrintOutValue(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 - { - PrintOutObject(ptrObj, (ulong)unboxed, new List(path)); - } - } - catch - { - // Ignored - } - } - else - { - ImGui.Text("null"); - } - } - else - { - if (!type.IsPrimitive) - { - PrintOutObject(value, addr, new List(path)); - } - else - { - ImGui.Text($"{value}"); - } - } - } - - public static void PrintOutObject(object obj, ulong addr, IEnumerable? path = null, bool autoExpand = false) + /// + /// Show a structure in an ImGui context. + /// + /// The structure to show. + /// The address to the structure. + /// The already followed path. + /// Whether or not this structure should start out expanded. + public static void ShowStruct(object obj, ulong addr, IEnumerable? path = null, bool autoExpand = false) { path ??= new List(); - if (ModuleEndAddr == 0 && ModuleStartAddr == 0) + if (moduleEndAddr == 0 && moduleStartAddr == 0) { try { var processModule = Process.GetCurrentProcess().MainModule; if (processModule != null) { - ModuleStartAddr = (ulong)processModule.BaseAddress.ToInt64(); - ModuleEndAddr = ModuleStartAddr + (ulong)processModule.ModuleMemorySize; + moduleStartAddr = (ulong)processModule.BaseAddress.ToInt64(); + moduleEndAddr = moduleStartAddr + (ulong)processModule.ModuleMemorySize; } else { - ModuleEndAddr = 1; + moduleEndAddr = 1; } } catch { - ModuleEndAddr = 1; + moduleEndAddr = 1; } } @@ -270,7 +222,7 @@ namespace Dalamud.Utility ImGui.TextColored(new Vector4(0.2f, 0.9f, 0.4f, 1), $"{f.Name}: "); ImGui.SameLine(); - PrintOutValue(addr, new List(path) { f.Name }, f.FieldType, f.GetValue(obj)); + ShowValue(addr, new List(path) { f.Name }, f.FieldType, f.GetValue(obj)); } foreach (var p in obj.GetType().GetProperties()) @@ -280,7 +232,7 @@ namespace Dalamud.Utility ImGui.TextColored(new Vector4(0.2f, 0.6f, 0.4f, 1), $"{p.Name}: "); ImGui.SameLine(); - PrintOutValue(addr, new List(path) { p.Name }, p.PropertyType, p.GetValue(obj)); + ShowValue(addr, new List(path) { p.Name }, p.PropertyType, p.GetValue(obj)); } ImGui.TreePop(); @@ -450,5 +402,60 @@ namespace Dalamud.Utility return Check1() || Check2() || Check3(); } + + 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, new List(path)); + } + } + catch + { + // Ignored + } + } + else + { + ImGui.Text("null"); + } + } + else + { + if (!type.IsPrimitive) + { + ShowStruct(value, addr, new List(path)); + } + else + { + ImGui.Text($"{value}"); + } + } + } } }