mirror of
https://github.com/kwsch/pkNX.git
synced 2026-09-29 20:57:18 -05:00
Cumulative changes from the team. Co-Authored-By: Matt <17801814+sora10pls@users.noreply.github.com> Co-Authored-By: SciresM <8676005+SciresM@users.noreply.github.com> Co-Authored-By: Lusamine <30205550+Lusamine@users.noreply.github.com>
27 lines
744 B
C#
27 lines
744 B
C#
using System;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace pkNX.Containers;
|
|
|
|
internal static class StructConverter
|
|
{
|
|
public static T ToClass<T>(this byte[] bytes) where T : class
|
|
{
|
|
var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
|
|
try { return (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T))!; }
|
|
finally { handle.Free(); }
|
|
}
|
|
|
|
public static byte[] ToBytesClass<T>(this T obj) where T : class
|
|
{
|
|
int size = Marshal.SizeOf(obj);
|
|
byte[] arr = new byte[size];
|
|
|
|
IntPtr ptr = Marshal.AllocHGlobal(size);
|
|
Marshal.StructureToPtr(obj, ptr, true);
|
|
Marshal.Copy(ptr, arr, 0, size);
|
|
Marshal.FreeHGlobal(ptr);
|
|
return arr;
|
|
}
|
|
}
|