Read legacy CultMesh route records

This commit is contained in:
Meta Vens 2026-07-13 16:53:22 +01:00
commit 581c5d0249
4 changed files with 177 additions and 0 deletions

View file

@ -0,0 +1,79 @@
using System;
using MessagePack;
using MessagePack.Formatters;
#nullable enable
namespace GameCult.Mesh
{
/// <summary>
/// Reads the current structured invocation record and the legacy operation-id string.
/// </summary>
public sealed class CultMeshOperationInvocationRecordFormatter :
IMessagePackFormatter<CultMeshOperationInvocationRecord?>
{
private const int FieldCount = 5;
/// <inheritdoc />
public void Serialize(
ref MessagePackWriter writer,
CultMeshOperationInvocationRecord? value,
MessagePackSerializerOptions options)
{
if (value == null)
{
writer.WriteNil();
return;
}
writer.WriteArrayHeader(FieldCount);
writer.Write(value.OperationId);
writer.Write(value.SchemaId);
writer.Write(value.RouteKind);
writer.Write(value.RouteDescription);
writer.Write(value.IdempotencyKey);
}
/// <inheritdoc />
public CultMeshOperationInvocationRecord? Deserialize(
ref MessagePackReader reader,
MessagePackSerializerOptions options)
{
if (reader.TryReadNil())
return new CultMeshOperationInvocationRecord();
if (reader.NextMessagePackType == MessagePackType.String)
return new CultMeshOperationInvocationRecord(reader.ReadString());
if (reader.NextMessagePackType != MessagePackType.Array)
throw new MessagePackSerializationException(
$"CultMesh operation invocation must be a string or array, not {reader.NextMessagePackType}.");
options.Security.DepthStep(ref reader);
try
{
var fields = reader.ReadArrayHeader();
var operationId = ReadString(ref reader, fields, 0);
var schemaId = ReadString(ref reader, fields, 1);
var routeKind = ReadString(ref reader, fields, 2);
var routeDescription = ReadString(ref reader, fields, 3);
var idempotencyKey = ReadString(ref reader, fields, 4);
for (var index = FieldCount; index < fields; index++)
reader.Skip();
return new CultMeshOperationInvocationRecord(
operationId,
schemaId,
routeKind,
routeDescription,
idempotencyKey);
}
finally
{
reader.Depth--;
}
}
private static string ReadString(ref MessagePackReader reader, int fields, int index) =>
index < fields ? reader.ReadString() ?? "" : "";
}
}

View file

@ -58,6 +58,7 @@ namespace GameCult.Mesh
/// Flat, transport-friendly fields for a CultMesh route hint.
/// </summary>
[MessagePackObject]
[MessagePackFormatter(typeof(CultMeshRouteRecordFormatter))]
public sealed class CultMeshRouteRecord
{
/// <summary>Creates flat route fields.</summary>
@ -3439,6 +3440,7 @@ namespace GameCult.Mesh
/// Flat, transport-friendly fields for a concrete CultMesh operation invocation.
/// </summary>
[MessagePackObject]
[MessagePackFormatter(typeof(CultMeshOperationInvocationRecordFormatter))]
public sealed class CultMeshOperationInvocationRecord
{
/// <summary>Creates flat operation invocation fields.</summary>

View file

@ -0,0 +1,54 @@
using MessagePack;
using MessagePack.Formatters;
#nullable enable
namespace GameCult.Mesh
{
/// <summary>
/// Reads current structured route records and legacy locality strings.
/// </summary>
public sealed class CultMeshRouteRecordFormatter : IMessagePackFormatter<CultMeshRouteRecord?>
{
private const int FieldCount = 2;
public void Serialize(ref MessagePackWriter writer, CultMeshRouteRecord? value, MessagePackSerializerOptions options)
{
if (value == null)
{
writer.WriteNil();
return;
}
writer.WriteArrayHeader(FieldCount);
writer.Write(value.Kind);
writer.Write(value.Description);
}
public CultMeshRouteRecord? Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
if (reader.TryReadNil())
return new CultMeshRouteRecord();
if (reader.NextMessagePackType == MessagePackType.String)
return new CultMeshRouteRecord(reader.ReadString(), "");
if (reader.NextMessagePackType != MessagePackType.Array)
throw new MessagePackSerializationException(
$"CultMesh route must be a string or array, not {reader.NextMessagePackType}.");
options.Security.DepthStep(ref reader);
try
{
var fields = reader.ReadArrayHeader();
var kind = fields > 0 ? reader.ReadString() ?? "" : "";
var description = fields > 1 ? reader.ReadString() ?? "" : "";
for (var index = FieldCount; index < fields; index++)
reader.Skip();
return new CultMeshRouteRecord(kind, description);
}
finally
{
reader.Depth--;
}
}
}
}

View file

@ -448,6 +448,48 @@ public sealed class CultMeshStreamingTests
payloadFields["value"].Should().Be("Raven");
}
[Test]
public void OperationInvocationRecord_ReadsLegacyStringAndWritesStructuredRecord()
{
var legacyPayload = MessagePackSerializer.Serialize(
"aetheria.entity.pilot.move",
CultDocumentMessagePackSerialization.Options);
var restored = MessagePackSerializer.Deserialize<CultMeshOperationInvocationRecord>(
legacyPayload,
CultDocumentMessagePackSerialization.Options);
var currentPayload = MessagePackSerializer.Serialize(
restored,
CultDocumentMessagePackSerialization.Options);
var reader = new MessagePackReader(currentPayload);
restored.OperationId.Should().Be("aetheria.entity.pilot.move");
restored.SchemaId.Should().BeEmpty();
restored.RouteKind.Should().BeEmpty();
restored.IdempotencyKey.Should().BeEmpty();
reader.ReadArrayHeader().Should().Be(5);
reader.ReadString().Should().Be("aetheria.entity.pilot.move");
}
[Test]
public void RouteRecord_ReadsLegacyStringAndWritesStructuredRecord()
{
var legacyPayload = MessagePackSerializer.Serialize(
"SharedMemory",
CultDocumentMessagePackSerialization.Options);
var restored = MessagePackSerializer.Deserialize<CultMeshRouteRecord>(
legacyPayload,
CultDocumentMessagePackSerialization.Options);
var currentPayload = MessagePackSerializer.Serialize(
restored,
CultDocumentMessagePackSerialization.Options);
var reader = new MessagePackReader(currentPayload);
restored.Kind.Should().Be("SharedMemory");
restored.Description.Should().BeEmpty();
reader.ReadArrayHeader().Should().Be(2);
reader.ReadString().Should().Be("SharedMemory");
}
[Test]
public void RouteRecord_FlattensAndParsesCrossRuntimeRouteKinds()
{