MemoryPack icon indicating copy to clipboard operation
MemoryPack copied to clipboard

Unity 2022.3.7f1 and .NET 7 DateTimeOffset serialize value incorrect

Open buzzers opened this issue 2 years ago • 6 comments

[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?

buzzers avatar Oct 25 '23 10:10 buzzers

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. ......

neuecc avatar Oct 26 '23 04:10 neuecc

Okay, so now there's no way to make it work without changing the type?

buzzers avatar Oct 26 '23 05:10 buzzers

Yes, I don't have any good ideas.

neuecc avatar Oct 26 '23 06:10 neuecc

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?

buzzers avatar Oct 26 '23 07:10 buzzers

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.

neuecc avatar Oct 26 '23 07:10 neuecc

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.

buzzers avatar Oct 26 '23 08:10 buzzers

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.

github-actions[bot] avatar Apr 24 '24 00:04 github-actions[bot]