Skip to content

Commit

Permalink
release v3.0.3
Browse files Browse the repository at this point in the history
## Nino.Serialization v3.0.3
- [Fix] Consists serialization strategy for unmanaged polymorphic structs
- [Fix] Fix Unity unable to serialize unmanaged list correctly
- [Fix] Add check endianness to ensure collection can be serialized appropriately on big endian architectures
  • Loading branch information
JasonXuDeveloper committed Nov 15, 2024
1 parent 94935ca commit edd2e0c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/Nino.Core/Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public bool ReadCollectionHeader(out int length)

//if value is 0 or sign bit is not set, then it's a null collection
Read(out uint value);
#if BIGENDIAN
length = (int)(value & 0x7FFFFFFF);
return true;
#else
#if NET5_0_OR_GREATER
value = System.Buffers.Binary.BinaryPrimitives.ReverseEndianness(value);
#else
Expand All @@ -40,6 +44,7 @@ public bool ReadCollectionHeader(out int length)
#endif
length = (int)(value & 0x7FFFFFFF);
return true;
#endif
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
4 changes: 4 additions & 0 deletions src/Nino.Core/TypeCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ public static uint GetCollectionHeader(int size)
{
// set sign bit to 1 - indicates that this is a collection and not null
uint ret = (uint)size | 0x80000000;
#if BIGENDIAN
return ret;
#else
//to big endian
#if NET5_0_OR_GREATER
return System.Buffers.Binary.BinaryPrimitives.ReverseEndianness(ret);
#else
return (ret << 24) | (ret >> 24) | ((ret & 0x0000FF00) << 8) | ((ret & 0x00FF0000) >> 8);
#endif
#endif
}

Expand Down
13 changes: 10 additions & 3 deletions src/Nino.Core/Writer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,29 @@ public void Write<T>(List<T> value) where T : unmanaged
Write(TypeCollector.NullCollection);
return;
}

#if NET6_0_OR_GREATER
Write(CollectionsMarshal.AsSpan(value));
#else
ref var lst = ref Unsafe.As<List<T>, TypeCollector.ListView<T>>(ref value);
Write(lst._items);
Write(lst._items.AsSpan(0, lst._size));
#endif
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Write<T>(List<T?> value) where T : unmanaged
{
if (value == null)
{
Write(TypeCollector.NullCollection);
return;
}

#if NET6_0_OR_GREATER
Write(CollectionsMarshal.AsSpan(value));
#else
Write((ICollection<T>)value);
ref var lst = ref Unsafe.As<List<T?>, TypeCollector.ListView<T?>>(ref value);
Write(lst._items.AsSpan(0, lst._size));
#endif
}

Expand Down

0 comments on commit edd2e0c

Please sign in to comment.