mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-31 21:03:43 +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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue