Skip to content

Commit

Permalink
Add support to Unity..Mathematics.int2
Browse files Browse the repository at this point in the history
  • Loading branch information
KonH committed Oct 13, 2023
1 parent 7dcea24 commit 923d788
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,47 @@

namespace MessagePack.Unity
{
public sealed class Int2Formatter : global::MessagePack.Formatters.IMessagePackFormatter<global::Unity.Mathematics.int2>
{
public void Serialize(ref MessagePackWriter writer, global::Unity.Mathematics.int2 value, global::MessagePack.MessagePackSerializerOptions options)
{
writer.WriteArrayHeader(2);
writer.Write(value.x);
writer.Write(value.y);
}

public global::Unity.Mathematics.int2 Deserialize(ref MessagePackReader reader, global::MessagePack.MessagePackSerializerOptions options)
{
if (reader.IsNil)
{
throw new InvalidOperationException("typecode is null, struct not supported");
}

var length = reader.ReadArrayHeader();
var x = default(int);
var y = default(int);
for (int i = 0; i < length; i++)
{
var key = i;
switch (key)
{
case 0:
x = reader.ReadInt32();
break;
case 1:
y = reader.ReadInt32();
break;
default:
reader.Skip();
break;
}
}

var result = new global::Unity.Mathematics.int2(x, y);
return result;
}
}

public sealed class Vector2Formatter : global::MessagePack.Formatters.IMessagePackFormatter<global::UnityEngine.Vector2>
{
public void Serialize(ref MessagePackWriter writer, global::UnityEngine.Vector2 value, global::MessagePack.MessagePackSerializerOptions options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using MessagePack.Formatters;
using Unity.Mathematics;
using UnityEngine;

namespace MessagePack.Unity
Expand Down Expand Up @@ -186,6 +187,9 @@ internal static class UnityResolveryResolverGetFormatterHelper
{typeof(List<BoundsInt?>), new ListFormatter<BoundsInt?>()},

#endif

// Unity Mathematics
{ typeof(int2), new Int2Formatter() },
};

internal static object? GetFormatter(Type t)
Expand Down
19 changes: 19 additions & 0 deletions src/MessagePack.UnityShims/Shims.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,22 @@ public BoundsInt(Vector3Int position, Vector3Int size)
}
}
}

namespace Unity.Mathematics
{
[MessagePackObject]
public struct int2
{
[Key(0)]
public int x;
[Key(1)]
public int y;

[SerializationConstructor]
public int2(int x, int y)
{
this.x = x;
this.y = y;
}
}
}

0 comments on commit 923d788

Please sign in to comment.