diff --git a/Dalamud/SafeMemory.cs b/Dalamud/SafeMemory.cs index 32972ec61..d4f79714e 100644 --- a/Dalamud/SafeMemory.cs +++ b/Dalamud/SafeMemory.cs @@ -190,6 +190,21 @@ namespace Dalamud return WriteBytes(address, encoding.GetBytes(str + "\0")); } + public static T? PtrToStructure(IntPtr addr) where T : struct => (T?)PtrToStructure(addr, typeof(T)); + + public static object? PtrToStructure(IntPtr addr, Type type) + { + var size = Marshal.SizeOf(type); + + if (!ReadBytes(addr, size, out var buffer)) + return null; + + var mem = new LocalMemory(size); + mem.Write(buffer); + + return mem.Read(type); + } + /// /// Get the size of the passed type. /// @@ -249,6 +264,8 @@ namespace Dalamud public T Read(int offset = 0) => (T)Marshal.PtrToStructure(this.hGlobal + offset, typeof(T)); + public object? Read(Type type, int offset = 0) => Marshal.PtrToStructure(this.hGlobal + offset, type); + public void Write(byte[] data, int index = 0) => Marshal.Copy(data, index, this.hGlobal, this.size); public void Write(T data, int offset = 0) => Marshal.StructureToPtr(data, this.hGlobal + offset, false); diff --git a/Dalamud/Utility/Util.cs b/Dalamud/Utility/Util.cs index fcb6d3c1d..c44fbb25b 100644 --- a/Dalamud/Utility/Util.cs +++ b/Dalamud/Utility/Util.cs @@ -160,8 +160,8 @@ namespace Dalamud.Utility return sb.ToString().TrimEnd(Environment.NewLine.ToCharArray()); } - private static ulong moduleStartAddr; - private static ulong moduleEndAddr; + private static ulong ModuleStartAddr; + private static ulong ModuleEndAddr; private static unsafe void PrintOutValue(ulong addr, IEnumerable path, Type type, object value) { @@ -173,18 +173,18 @@ namespace Dalamud.Utility { var unboxedAddr = (ulong)unboxed; ImGuiHelpers.ClickToCopyText($"{(ulong)unboxed:X}"); - if (moduleStartAddr > 0 && unboxedAddr >= moduleStartAddr && unboxedAddr <= moduleEndAddr) + if (ModuleStartAddr > 0 && unboxedAddr >= ModuleStartAddr && unboxedAddr <= ModuleEndAddr) { ImGui.SameLine(); ImGui.PushStyleColor(ImGuiCol.Text, 0xffcbc0ff); - ImGuiHelpers.ClickToCopyText($"ffxiv_dx11.exe+{unboxedAddr - moduleStartAddr:X}"); + ImGuiHelpers.ClickToCopyText($"ffxiv_dx11.exe+{unboxedAddr - ModuleStartAddr:X}"); ImGui.PopStyleColor(); } try { var eType = type.GetElementType(); - var ptrObj = Marshal.PtrToStructure(new IntPtr(unboxed), eType); + var ptrObj = SafeMemory.PtrToStructure(new IntPtr(unboxed), eType); ImGui.SameLine(); PrintOutObject(ptrObj, (ulong)unboxed, new List(path)); } @@ -215,24 +215,24 @@ namespace Dalamud.Utility { 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; } }