From 76a38ec36e6ed64d1b45a917053ae8ba41c86e7d Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 27 Jun 2026 23:18:48 +0100 Subject: [PATCH] Add C# typed peer snapshot node sync --- src/GameCult.Mesh/CultMeshSnapshots.cs | 74 +++++++++++++++++++ src/GameCult.Mesh/README.md | 15 ++++ .../CultMeshStreamingTests.cs | 54 ++++++++++++++ 3 files changed, 143 insertions(+) diff --git a/src/GameCult.Mesh/CultMeshSnapshots.cs b/src/GameCult.Mesh/CultMeshSnapshots.cs index 82828fbd..5b01c8b2 100644 --- a/src/GameCult.Mesh/CultMeshSnapshots.cs +++ b/src/GameCult.Mesh/CultMeshSnapshots.cs @@ -510,6 +510,80 @@ namespace GameCult.Mesh return new CultMeshSnapshotDocumentBinding(recordKey, documentId); } + /// + /// Fetches one typed snapshot document and syncs it into a local node in one call. + /// + public static Task SyncDocumentFromPeerSnapshotAsync( + CultMeshNode node, + string endpoint, + string recordKey, + CultMeshSnapshotEndpointOptions? options = null, + bool flush = false) + where TDocument : class + { + if (node == null) throw new ArgumentNullException(nameof(node)); + if (string.IsNullOrWhiteSpace(endpoint)) throw new ArgumentException("Value must be non-empty.", nameof(endpoint)); + if (string.IsNullOrWhiteSpace(recordKey)) throw new ArgumentException("Value must be non-empty.", nameof(recordKey)); + + return SnapshotEndpoint(endpoint, options).SyncDocumentAsync(node, recordKey, flush); + } + + /// + /// Fetches one typed snapshot document through a caller-provided schema client and syncs it into a local node in one call. + /// + public static Task SyncDocumentFromPeerSnapshotAsync( + CultMeshNode node, + Func createClient, + string endpoint, + string recordKey, + CultMeshSnapshotEndpointOptions? options = null, + bool flush = false) + where TDocument : class + { + if (node == null) throw new ArgumentNullException(nameof(node)); + if (createClient == null) throw new ArgumentNullException(nameof(createClient)); + if (string.IsNullOrWhiteSpace(endpoint)) throw new ArgumentException("Value must be non-empty.", nameof(endpoint)); + if (string.IsNullOrWhiteSpace(recordKey)) throw new ArgumentException("Value must be non-empty.", nameof(recordKey)); + + var request = CloneSnapshotFacadeRequestOptions(options?.Request); + request.CreateClient = createClient; + var resolvedOptions = new CultMeshSnapshotEndpointOptions + { + Context = options?.Context, + DocumentRegistry = options?.DocumentRegistry, + Request = request, + SourceId = options?.SourceId, + RouteHint = options?.RouteHint, + PollInterval = options?.PollInterval ?? TimeSpan.FromMilliseconds(250) + }; + return SnapshotEndpoint(endpoint, resolvedOptions).SyncDocumentAsync(node, recordKey, flush); + } + + private static CultMeshSnapshotRequestOptions CloneSnapshotFacadeRequestOptions(CultMeshSnapshotRequestOptions? source) + { + if (source == null) + return new CultMeshSnapshotRequestOptions(); + + return new CultMeshSnapshotRequestOptions + { + SchemaIds = source.SchemaIds, + RecordKeys = source.RecordKeys, + ShardId = source.ShardId, + ShardEpoch = source.ShardEpoch, + ResponseTimeout = source.ResponseTimeout, + ConnectTimeout = source.ConnectTimeout, + MessageIdPrefix = source.MessageIdPrefix, + Security = source.Security, + ConfigureClient = source.ConfigureClient, + CreateClient = source.CreateClient, + RudpRuntimeId = source.RudpRuntimeId, + RudpConnectionId = source.RudpConnectionId, + RudpConnectPayload = source.RudpConnectPayload, + RudpMaxFragmentBytes = source.RudpMaxFragmentBytes, + RudpResendDelayMs = source.RudpResendDelayMs + }; + } + /// /// Fetches one scoped raw CultNet snapshot from an endpoint. /// diff --git a/src/GameCult.Mesh/README.md b/src/GameCult.Mesh/README.md index 20ae25c2..4689136a 100644 --- a/src/GameCult.Mesh/README.md +++ b/src/GameCult.Mesh/README.md @@ -186,6 +186,21 @@ Use `DocumentFromPublication()` when the caller should provide a configured source instead of branching between local files, backing stores, and remote CultNet snapshots itself. +When the remote snapshot should hydrate a local node, use the direct sync +facade and keep the rest of the code on local typed handles: + +```csharp +var syncedHealth = await CultMesh.SyncDocumentFromPeerSnapshotAsync( + node, + "cultnet://daemon.local:3075", + "daemon:aetheria.health.v1", + new CultMeshSnapshotEndpointOptions { DocumentRegistry = daemonRegistry }); + +var localHealth = await node + .Document("daemon:aetheria.health.v1", verse) + .LatestAsync(); +``` + ## Game Session `CultMeshGameSession` wires the common gameplay loop: local catalogs, diff --git a/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs b/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs index 5659d17a..4f6114ea 100644 --- a/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs +++ b/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs @@ -2480,6 +2480,60 @@ public sealed class CultMeshStreamingTests node.Cache.Get(key)!.Revision.Should().Be(45); } + [Test] + public async Task SnapshotFacade_SyncsRemoteDocumentIntoLocalNodeWithSameSchemaAlias() + { + var sourceCache = new CultCache(); + var registry = new CultNetDocumentRegistry(CultDocumentRegistry.Shared); + registry.Register(CultNetDocumentBinding.ForDocument(sourceCache.Registry)); + var key = new CultRecordKey("mesh-note:facade-sync-alias"); + await sourceCache.UpsertAsync(new MeshNoteDocument + { + Schema = "tests.mesh_note.v1", + Text = "facade-synced", + Revision = 46 + }, new CultRecordHandle(key)); + var requests = new List(); + using var node = await CultMesh.CreateNodeAsync( + Path.Combine(Path.GetTempPath(), $"cultmesh-facade-sync-alias-{Guid.NewGuid():N}.ccmp"), + new CultMeshNodeOptions + { + StartServer = false, + CacheOptions = new CultCacheOpenOptions + { + Registry = sourceCache.Registry, + PullOnOpen = false + }, + DatabaseOptions = new CultNetDatabaseOptions + { + DocumentRegistry = registry + } + }); + + var synced = await CultMesh.SyncDocumentFromPeerSnapshotAsync( + node, + () => new MeshSnapshotSchemaClient(request => + { + requests.Add(request); + return registry.CreateRawSnapshotResponse(sourceCache, request.MessageId, request); + }), + "cultnet://facade-sync.test:3075", + key.Value, + new CultMeshSnapshotEndpointOptions + { + DocumentRegistry = registry, + Context = CultMesh.Verse("starbridge", "unity-raven").Context + }); + var localAlias = await node.Document(key.Value, CultMesh.Verse("starbridge", "unity-raven")) + .LatestAsync(); + + synced.Text.Should().Be("facade-synced"); + synced.Revision.Should().Be(46); + localAlias.Text.Should().Be("facade-synced"); + node.Cache.Get(key)!.Revision.Should().Be(46); + requests.Should().ContainSingle().Which.RecordKeys.Should().Equal(key.Value); + } + [Test] public async Task DocumentRegistryHelpers_CreateTypedNetworkRegistriesForAliases() {