From 62189007341945b6aab9148988db30fc9fbddf33 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 27 Jun 2026 22:25:15 +0100 Subject: [PATCH] Prefer typed peer snapshot aliases by record key --- src/GameCult.Mesh/CultMesh.cs | 29 +++++++++++++ .../CultMeshStreamingTests.cs | 43 +++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/src/GameCult.Mesh/CultMesh.cs b/src/GameCult.Mesh/CultMesh.cs index 3875e251..27c0aa0c 100644 --- a/src/GameCult.Mesh/CultMesh.cs +++ b/src/GameCult.Mesh/CultMesh.cs @@ -2316,6 +2316,9 @@ namespace GameCult.Mesh var record = response.Documents.FirstOrDefault(candidate => string.Equals(candidate.SchemaId, schemaId, StringComparison.Ordinal) && string.Equals(candidate.RecordKey, recordKey, StringComparison.Ordinal)) + ?? response.Documents.FirstOrDefault(candidate => + string.Equals(candidate.RecordKey, recordKey, StringComparison.Ordinal) && + TryDecodeSnapshotDocument(candidate, out TDocument? _)) ?? response.Documents.FirstOrDefault(candidate => string.Equals(candidate.SchemaId, schemaId, StringComparison.Ordinal)) ?? response.Documents.FirstOrDefault(candidate => @@ -2332,6 +2335,32 @@ namespace GameCult.Mesh $"CultNet raw document payloadEncoding must be \"messagepack\", not \"{record.PayloadEncoding}\"."); } + return DecodeSnapshotDocument(record); + } + + private static bool TryDecodeSnapshotDocument( + CultNetRawDocumentRecord record, + out TDocument? document) + where TDocument : class + { + document = null; + if (!string.Equals(record.PayloadEncoding, "messagepack", StringComparison.Ordinal)) + return false; + + try + { + document = DecodeSnapshotDocument(record); + return true; + } + catch + { + return false; + } + } + + private static TDocument DecodeSnapshotDocument(CultNetRawDocumentRecord record) + where TDocument : class + { return (TDocument)CultDocumentMessagePackSerialization.DeserializeUntyped( typeof(TDocument), record.Payload); diff --git a/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs b/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs index 3d4e3c04..1ba030f3 100644 --- a/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs +++ b/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs @@ -2260,6 +2260,49 @@ public sealed class CultMeshStreamingTests CultMesh.Verse("starbridge", "unity-pilot")); (await aliasSchemaHandle.LatestAsync()).Text.Should().Be("record-key-fallback"); + + var mixedAliasSchemaHandle = CultMesh.DocumentFromPeerSnapshot( + _ => Task.FromResult(new CultNetSnapshotResponseRawMessage + { + MessageId = "alias-schema-with-neighbor", + Documents = new[] + { + new CultNetRawDocumentRecord + { + SchemaId = CultDocumentRegistry.Shared.GetRequired().SchemaId, + RecordKey = "mesh-note:other", + StoredAt = DateTimeOffset.UtcNow.ToString("O"), + PayloadEncoding = "messagepack", + Payload = CultDocumentMessagePackSerialization.SerializeUntyped( + new MeshNoteDocument + { + Schema = "tests.mesh_note.v1", + Text = "wrong-schema-neighbor", + Revision = 11 + }, + typeof(MeshNoteDocument)) + }, + new CultNetRawDocumentRecord + { + SchemaId = "remote.generated.mesh_note_alias.v100", + RecordKey = key.Value, + StoredAt = DateTimeOffset.UtcNow.ToString("O"), + PayloadEncoding = "messagepack", + Payload = CultDocumentMessagePackSerialization.SerializeUntyped( + new MeshNoteDocument + { + Schema = "tests.mesh_note.v1", + Text = "compatible-record-key", + Revision = 12 + }, + typeof(MeshNoteDocument)) + } + } + }), + key.Value, + CultMesh.Verse("starbridge", "unity-pilot")); + + (await mixedAliasSchemaHandle.LatestAsync()).Text.Should().Be("compatible-record-key"); } [Test]