From bb1437dc5840989e83ab3c9409bd1e3c3efeb2f7 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 16 May 2026 09:16:43 +0100 Subject: [PATCH] Use CultNet binding schema ids for raw records --- .../CultNetDocumentRegistry.cs | 42 +++++++++++-------- .../Program.cs | 2 + 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/GameCult.Networking/CultNetDocumentRegistry.cs b/src/GameCult.Networking/CultNetDocumentRegistry.cs index 7b402a4d..d6446b2d 100644 --- a/src/GameCult.Networking/CultNetDocumentRegistry.cs +++ b/src/GameCult.Networking/CultNetDocumentRegistry.cs @@ -74,6 +74,7 @@ namespace GameCult.Networking /// public static CultNetDocumentBinding ForDocument( CultDocumentRegistry? registry = null, + string? schemaId = null, Func? payloadSerializer = null, Func? payloadDeserializer = null) where T : class @@ -81,7 +82,7 @@ namespace GameCult.Networking var descriptor = (registry ?? CultDocumentRegistry.Shared).GetRequired(); return new CultNetDocumentBinding( typeof(T), - descriptor.SchemaId, + string.IsNullOrWhiteSpace(schemaId) ? descriptor.SchemaId : schemaId!, document => { var typed = (T)document; @@ -185,7 +186,7 @@ namespace GameCult.Networking MessageId = RequireNonEmpty(messageId, nameof(messageId)), Document = new CultNetRawDocumentRecord { - SchemaId = descriptor.SchemaId, + SchemaId = binding.SchemaId, RecordKey = handle.Key.Value, StoredAt = storedAt, PayloadEncoding = "messagepack", @@ -235,9 +236,19 @@ namespace GameCult.Networking foreach (var document in cache.AllEntries) { var descriptor = _documents.GetRequired(document.GetType()); + var binding = GetByDocumentType(document.GetType()) ?? + new CultNetDocumentBinding( + document.GetType(), + descriptor.SchemaId, + value => CultDocumentMessagePackSerialization.SerializeUntyped(value, value.GetType()), + payload => CultDocumentMessagePackSerialization.DeserializeUntyped(document.GetType(), payload)); + if (requestedSchemaIds != null && !requestedSchemaIds.Contains(descriptor.SchemaId)) { - continue; + if (!requestedSchemaIds.Contains(binding.SchemaId)) + { + continue; + } } var handleMethod = typeof(CultCache) @@ -260,16 +271,9 @@ namespace GameCult.Networking continue; } - var binding = GetByDocumentType(document.GetType()) ?? - new CultNetDocumentBinding( - document.GetType(), - descriptor.SchemaId, - value => CultDocumentMessagePackSerialization.SerializeUntyped(value, value.GetType()), - payload => CultDocumentMessagePackSerialization.DeserializeUntyped(document.GetType(), payload)); - documents.Add(new CultNetRawDocumentRecord { - SchemaId = descriptor.SchemaId, + SchemaId = binding.SchemaId, RecordKey = key, StoredAt = storedAt, PayloadEncoding = "messagepack", @@ -301,13 +305,15 @@ namespace GameCult.Networking } ValidateRawDocumentRecord(message.Document); - var descriptor = _documents.GetRequiredBySchemaId(message.Document.SchemaId); - var binding = GetBySchemaId(message.Document.SchemaId) ?? - new CultNetDocumentBinding( - descriptor.DocumentType, - descriptor.SchemaId, - value => CultDocumentMessagePackSerialization.SerializeUntyped(value, value.GetType()), - payload => CultDocumentMessagePackSerialization.DeserializeUntyped(descriptor.DocumentType, payload)); + var binding = GetBySchemaId(message.Document.SchemaId); + var descriptor = binding != null + ? _documents.GetRequired(binding.DocumentType) + : _documents.GetRequiredBySchemaId(message.Document.SchemaId); + binding ??= new CultNetDocumentBinding( + descriptor.DocumentType, + descriptor.SchemaId, + value => CultDocumentMessagePackSerialization.SerializeUntyped(value, value.GetType()), + payload => CultDocumentMessagePackSerialization.DeserializeUntyped(descriptor.DocumentType, payload)); var document = binding.PayloadDeserializer(message.Document.Payload); var addMethod = typeof(CultCache).GetMethod(nameof(CultCache.AddAsync))! diff --git a/tests/GameCult.Networking.InteropPeer/Program.cs b/tests/GameCult.Networking.InteropPeer/Program.cs index 13761363..b1179a84 100644 --- a/tests/GameCult.Networking.InteropPeer/Program.cs +++ b/tests/GameCult.Networking.InteropPeer/Program.cs @@ -56,6 +56,7 @@ static async Task ServeAsync(ServeConfig config) var schemaRegistration = LoadSchemaRegistration(config.SchemaPath); var documentRegistry = new CultNetDocumentRegistry() .Register(CultNetDocumentBinding.ForDocument( + schemaId: schemaRegistration.SchemaId, payloadSerializer: SerializeInteropNotePayload, payloadDeserializer: DeserializeInteropNotePayload)); @@ -172,6 +173,7 @@ static async Task DialAsync(DialConfig config) var cache = new CultCache(); var documentRegistry = new CultNetDocumentRegistry() .Register(CultNetDocumentBinding.ForDocument( + schemaId: schemaRegistration.SchemaId, payloadSerializer: SerializeInteropNotePayload, payloadDeserializer: DeserializeInteropNotePayload));