mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-12 18:27:23 +01:00
feat: add SafeMemory.PtrToStructure/<T>
This commit is contained in:
parent
65d0acaf64
commit
ca15a9c035
2 changed files with 27 additions and 10 deletions
|
|
@ -190,6 +190,21 @@ namespace Dalamud
|
|||
return WriteBytes(address, encoding.GetBytes(str + "\0"));
|
||||
}
|
||||
|
||||
public static T? PtrToStructure<T>(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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the size of the passed type.
|
||||
/// </summary>
|
||||
|
|
@ -249,6 +264,8 @@ namespace Dalamud
|
|||
|
||||
public T Read<T>(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>(T data, int offset = 0) => Marshal.StructureToPtr(data, this.hGlobal + offset, false);
|
||||
|
|
|
|||
|
|
@ -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<string> 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<string>(path));
|
||||
}
|
||||
|
|
@ -215,24 +215,24 @@ namespace Dalamud.Utility
|
|||
{
|
||||
path ??= new List<string>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue