diff --git a/src/GameCult.Mesh/CultMeshVerse.cs b/src/GameCult.Mesh/CultMeshVerse.cs index 94a1b1fb..547a46ec 100644 --- a/src/GameCult.Mesh/CultMeshVerse.cs +++ b/src/GameCult.Mesh/CultMeshVerse.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; +using GameCult.Networking; using R3; namespace GameCult.Mesh @@ -225,6 +226,19 @@ namespace GameCult.Mesh _updates.OnNext(verse); } + /// + /// Adds or replaces Verses from a wire catalog response. + /// + public void Upsert(CultMeshVerseCatalogResponseMessage response) + { + ThrowIfDisposed(); + if (response == null) throw new ArgumentNullException(nameof(response)); + foreach (var verse in response.Verses) + { + Upsert(verse.ToVerseDescriptor()); + } + } + /// /// Gets a Verse by id, if known. /// diff --git a/src/GameCult.Mesh/docs/public-api.md b/src/GameCult.Mesh/docs/public-api.md index 6b5876d6..6adb15b5 100644 --- a/src/GameCult.Mesh/docs/public-api.md +++ b/src/GameCult.Mesh/docs/public-api.md @@ -45,6 +45,8 @@ It can publish discovery updates and find compatible transfer targets. `CultMesh.ServeVerseCatalog(node, catalog)` attaches schema-v0 Verse discovery responses to a node. The wire contracts are `cultmesh.verse_catalog_request.v0` and `cultmesh.verse_catalog_response.v0`. +Consumers can apply a response directly with +`CultMeshVerseCatalog.Upsert(CultMeshVerseCatalogResponseMessage)`. ### Shard Authority diff --git a/tests/GameCult.Networking.Tests/NetworkingTests.cs b/tests/GameCult.Networking.Tests/NetworkingTests.cs index bb2ab477..6852fcfe 100644 --- a/tests/GameCult.Networking.Tests/NetworkingTests.cs +++ b/tests/GameCult.Networking.Tests/NetworkingTests.cs @@ -861,6 +861,42 @@ namespace GameCult.Networking.Tests Assert.That(response.Verses[0].DiscoveryEndpoints, Is.EqualTo(["cultmesh://aetheria.example.test:3075"])); } + [Test] + public void CultMeshVerseCatalog_Upserts_WireDiscoveryResponse() + { + using var catalog = CultMesh.CreateVerseCatalog(); + var updates = new List(); + using var subscription = catalog.Watch().Subscribe(updates.Add); + + catalog.Upsert(new CultMeshVerseCatalogResponseMessage + { + MessageId = "verses-apply", + Verses = + [ + new CultMeshVerseDescriptorMessage + { + VerseId = "aetheria-branch", + DisplayName = "Aetheria Branch", + AuthorityModel = nameof(CultMeshVerseAuthorityModel.SubscribedOverlay), + Compatibility = new CultMeshVerseCompatibilityMessage + { + TransportVersion = "cultmesh.v0", + RulesHash = "branch-rules", + CompatibleVerseIds = ["aetheria-main"] + }, + ParentVerseId = "aetheria-main" + } + ] + }); + + var verse = catalog.Get("aetheria-branch"); + + Assert.That(verse, Is.Not.Null); + Assert.That(verse!.AuthorityModel, Is.EqualTo(CultMeshVerseAuthorityModel.SubscribedOverlay)); + Assert.That(verse.ParentVerseId, Is.EqualTo("aetheria-main")); + Assert.That(updates, Has.Count.EqualTo(1)); + } + [Test] public async Task CultMeshNode_CanEnable_DurableShardLogs_WithDefaultPath() {