Skip to content

Commit

Permalink
Update WzBinaryProperty - fix depreciated FormatterServices.GetUninit…
Browse files Browse the repository at this point in the history
…ializedObject(typeof(T)) func
  • Loading branch information
lastbattle committed Oct 16, 2024
1 parent 2de16b9 commit a24bb09
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions MapleLib/WzLib/WzProperties/WzBinaryProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,21 @@ private static byte[] StructToBytes<T>(T obj)
}
}

private static T BytesToStructConstructorless<T>(byte[] data)
private static T BytesToStructConstructorless<T>(byte[] data) where T : class
{
if (data == null)
throw new ArgumentNullException(nameof(data));

int objectSize = Marshal.SizeOf(typeof(T));
if (data.Length < objectSize)
throw new ArgumentException($"The byte array must be at least {objectSize} bytes long to contain the entire object.", nameof(data));

GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
T obj = (T)FormatterServices.GetUninitializedObject(typeof(T));
Marshal.PtrToStructure<T>(handle.AddrOfPinnedObject(), obj);
return obj;
IntPtr ptr = handle.AddrOfPinnedObject();
object obj = Marshal.PtrToStructure(ptr, typeof(T));
return (T)obj;
}
finally
{
Expand Down

0 comments on commit a24bb09

Please sign in to comment.