Unity 2022.3.7f1 and .NET 7 DateTimeOffset serialize value incorrect
[MemoryPackable]
public sealed partial class MPTest
{
public DateTimeOffset D { get; set; }
}
var mptest = new MPTest() { D = new(2023, 10, 25, 17, 45, 00, TimeSpan.FromHours(8)) };
var d = MemoryPackSerializer.Serialize(mptest);
The d obtained by executing the above code in .NET 7 and Unity is inconsistent: in .NET 7 the result is 1-224-1-0-0-0-0-0-0-0-182-36-14-63-213-219-8, and in Unity the result is 1-0-182-36-14-63-213-219-8-224-1-0-0-0-0-0-0.
This seems to be caused by inconsistent memory layout. Is there a quick solution to make it work?
It was a known issue that if an AutoLayout (like DateTimeOffset) was included in struct, it would be incompatible, but I see that it also happens in class. https://github.com/Cysharp/MemoryPack/issues/107#issuecomment-1368190580 The only way to be sure is to transfer it to a Sequential DateTimeOffset and compatible struct for communication. ......
Okay, so now there's no way to make it work without changing the type?
Yes, I don't have any good ideas.
Will future versions consider handling the struct memory layout issues of different versions and platforms? This is bad and leaves us completely unable to use the struct type, as functionality can be broken at any time.
In addition, will Unity support custom formatters?
The standard .NET structs that use Auto Layout are only DateTime, DateTimeOffset, and ValueTuple. Since DateTime is a single field, the potential issues may arise with DateTimeOffset and ValueTuple.
We are considering support for custom formatters in Unity.
Regarding Unity's custom formatter I wrote an implementation and will probably submit a PR this weekend.
The issue that has not been figured out yet is how to define the interface type returned by GetFormatter.
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 30 days.