oecs-sharp/OECS/EntityFormatter.cs

26 lines
791 B
C#
Raw Normal View History

using MessagePack;
using MessagePack.Formatters;
namespace OECS;
/// <summary>
/// Custom MessagePack formatter for <see cref="Entity"/>.
///
/// Reads and writes the raw 32-bit packed value as a single uint.
/// This avoids the readonly-field issue where MessagePack's built-in
/// deserialization path (reflection-based field assignment) cannot
/// write to <c>readonly</c> fields on value types.
/// </summary>
public class EntityFormatter : IMessagePackFormatter<Entity>
{
public void Serialize(ref MessagePackWriter writer, Entity value, MessagePackSerializerOptions options)
{
writer.Write((uint)value);
}
public Entity Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
return (Entity)reader.ReadUInt32();
}
}