From fb657bea1d338f1abf8627030c6c5f2e037255e7 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 27 Jun 2026 14:16:20 +0100 Subject: [PATCH] Add schema lookup for Aetheria state documents --- ...ltmesh-ergonomics-staged-migration-plan.md | 11 ++- .../scripts/verify-stage7d-unity-parity.ps1 | 5 ++ .../Tests/DaemonRuntimeDocumentTests.cs | 13 +++ .../Runtime/AetheriaClient.cs | 6 ++ .../Runtime/AetheriaClientState.cs | 84 ++++++++++++++++++- 5 files changed, 115 insertions(+), 4 deletions(-) diff --git a/Aetheria.State/docs/cultmesh-ergonomics-staged-migration-plan.md b/Aetheria.State/docs/cultmesh-ergonomics-staged-migration-plan.md index c372ab498..74d2728be 100644 --- a/Aetheria.State/docs/cultmesh-ergonomics-staged-migration-plan.md +++ b/Aetheria.State/docs/cultmesh-ergonomics-staged-migration-plan.md @@ -276,7 +276,9 @@ of transitional `Current*Async()`, `StationRefitAsync()`, `SectorMapAsync()`, `ZoneContactsAsync()`, or `ZoneRenderAsync()` helpers. `AetheriaClientState` also indexes projected state documents by shared document type so callers can retrieve or watch a known typed document with one call while CultMesh owns the -bound live feed. +bound live feed. The facade also indexes those handles by `CultDocument` +schema name/version, so a caller can resolve the shared schema identity even +when exact local CLR type identity is not the public contract. Goal: every client-facing state access becomes a single CultMesh document, collection, query, or pointer call. The caller names the domain state it wants and gets a typed reactive value. It does not manually inspect daemon frames, current-state projections, station rows, record keys, local facade indexes, or transport route details. @@ -312,6 +314,9 @@ var refit = await client.Aetheria() var contacts = await client.Aetheria() .LatestAsync(); +var contactsHandle = client.Aetheria() + .DocumentBySchema(AetheriaRuntimeDaemonSchemas.ZoneContacts); + using var render = client .Watch(RenderZone); ``` @@ -333,7 +338,7 @@ const refit = await client.aetheria() Work: -- Keep pushing shared CultMesh typed reactive document and collection handles down until type/schema lookup is owned by CultMesh rather than by the Aetheria facade registry. +- Keep pushing shared CultMesh typed reactive document and collection handles down until type/schema lookup, including safe conversion for schema-compatible CLR aliases, is owned by CultMesh rather than by the Aetheria facade registry. - Generate Aetheria current/station/zone/catalog accessors from schema metadata. - Move derived-state joins, such as current docking bay from current docking plus station refit plus entity records, into generated/internal query executors. - Replace remaining Unity menu/HUD/render calls to `TryResolve*` and `_observedFacadeIndex` with typed handles or generated/internal render adapters. @@ -344,7 +349,7 @@ Work: Gates: - `InventoryMenu`, `InventoryPanel`, `TradeMenu`, `LocalMenu`, `SchematicDisplay`, `SectorRenderer`, `MapRenderer`, and `ZoneRenderer` do not manually join state projections to obtain domain values. -- Shared projected documents are reachable through `client.Aetheria().Document()`, `client.Aetheria().LatestAsync()`, and `client.Watch()` as the C# stepping stone toward CultMesh-native type/schema lookup. +- Shared projected documents are reachable through `client.Aetheria().Document()`, `client.Aetheria().DocumentBySchema(schemaVersion)`, `client.Aetheria().LatestAsync()`, and `client.Watch()` as the C# stepping stone toward CultMesh-native type/schema lookup. - Client-facing code contains no `_observedFacadeIndex.TryResolve*` outside Unity render adapter internals. - Client-facing code contains no `ResolveClient().Current*Async().GetAwaiter().GetResult()` or equivalent blocking state reads. - `TryGetTypedCurrentDockingBayFacade` and similar transitional helpers are deleted rather than renamed. diff --git a/Aetheria.State/scripts/verify-stage7d-unity-parity.ps1 b/Aetheria.State/scripts/verify-stage7d-unity-parity.ps1 index 7df0598b5..627b4ee7d 100644 --- a/Aetheria.State/scripts/verify-stage7d-unity-parity.ps1 +++ b/Aetheria.State/scripts/verify-stage7d-unity-parity.ps1 @@ -577,6 +577,7 @@ $client = Read-Text "Packages\org.gamecult.aetheria.state\Runtime\AetheriaClient Assert-Contains $client "AetheriaClientState State" "AetheriaClient must expose the semantic typed state facade." Assert-Contains $client "Aetheria()" "AetheriaClient must expose the canonical domain facade entrypoint." Assert-Contains $client "Document()" "AetheriaClient must allow callers to retrieve a projected state document by shared document type." +Assert-Contains $client "DocumentBySchema(string schemaVersion)" "AetheriaClient must allow projected state lookup by shared schema identity." Assert-Contains $client "LatestAsync()" "AetheriaClient must allow one-call typed state reads by shared document type." Assert-Contains $client "Watch()" "AetheriaClient must allow reactive typed state reads by shared document type." Assert-Contains $client "ObjectsViewportAsync" "AetheriaClient must expose objects viewport facade." @@ -596,9 +597,13 @@ Assert-Contains $verseClient "CreateAetheriaStateFacade" "AetheriaRuntimeVerseCl $clientState = Read-Text "Packages\org.gamecult.aetheria.state\Runtime\AetheriaClientState.cs" Assert-Contains $clientState "CultMeshBoundLiveFeed" "Aetheria state documents must bind through CultMesh live feeds." +Assert-Contains $clientState "IAetheriaRuntimeProjectedDocument" "Aetheria projected documents must expose schema metadata independent of exact CLR type." Assert-Contains $clientState "_documentsByType" "Aetheria state facade must index projected documents by shared document type." +Assert-Contains $clientState "_documentsBySchema" "Aetheria state facade must index projected documents by shared schema identity." Assert-Contains $clientState "TryGetDocument" "Aetheria state facade must support safe typed document lookup." +Assert-Contains $clientState "TryGetDocumentBySchema" "Aetheria state facade must support safe schema document lookup." Assert-Contains $clientState "Document()" "Aetheria state facade must support direct typed document lookup." +Assert-Contains $clientState "DocumentBySchema(string schemaVersion)" "Aetheria state facade must support direct schema document lookup." Assert-Contains $clientState "LatestAsync()" "Aetheria state facade must support one-call typed document reads." Assert-Contains $clientState "Watch()" "Aetheria state facade must support reactive typed document lookup." Assert-Contains $clientState "LatestAsync()" "Aetheria state documents must support one-shot typed reads." diff --git a/Assets/Scripts/Tests/DaemonRuntimeDocumentTests.cs b/Assets/Scripts/Tests/DaemonRuntimeDocumentTests.cs index c6facb471..fb302fb2d 100644 --- a/Assets/Scripts/Tests/DaemonRuntimeDocumentTests.cs +++ b/Assets/Scripts/Tests/DaemonRuntimeDocumentTests.cs @@ -180,8 +180,21 @@ public class DaemonRuntimeDocumentTests Assert.AreEqual("aetheria.current.entity", client.State.Current.Entity.DocumentId); Assert.AreSame(client.State.Current.Entity, client.State.Document()); Assert.AreSame(client.State.ZoneRender, client.Document()); + Assert.AreSame( + client.State.Current.Entity, + client.State.DocumentBySchema(AetheriaRuntimeDaemonSchemas.CurrentEntity)); + Assert.AreSame( + client.State.ZoneContacts, + client.DocumentBySchema("gamecult.aetheria.zone_contacts")); + Assert.AreEqual( + typeof(AetheriaRuntimeCurrentEntityDocument), + client.DocumentBySchema(AetheriaRuntimeDaemonSchemas.CurrentEntity).DocumentType); Assert.IsTrue(client.State.TryGetDocument(out var sectorMapDocument)); Assert.AreSame(client.State.SectorMap, sectorMapDocument); + Assert.IsTrue(client.State.TryGetDocumentBySchema( + AetheriaRuntimeDaemonSchemas.SectorMap, + out var sectorMapBySchema)); + Assert.AreSame(client.State.SectorMap, sectorMapBySchema); Assert.AreEqual(AetheriaRuntimeDaemonSchemas.CurrentEntity, currentEntity.Schema); Assert.AreEqual("zone.0.entity.0", currentEntity.EntityKey); Assert.AreEqual(0, currentEntity.EntityIndex); diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClient.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClient.cs index 916bd70f9..b50db52e5 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClient.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClient.cs @@ -46,6 +46,12 @@ namespace GameCult.Aetheria.State.Verse return State.Document(); } + public IAetheriaRuntimeProjectedDocument DocumentBySchema(string schemaVersion) + { + ThrowIfDisposed(); + return State.DocumentBySchema(schemaVersion); + } + public Task LatestAsync() { ThrowIfDisposed(); diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClientState.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClientState.cs index 70501feaa..213fadf2f 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClientState.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClientState.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.Reflection; using System.Threading.Tasks; +using GameCult.Caching; using GameCult.Mesh; using R3; @@ -13,10 +15,12 @@ namespace GameCult.Aetheria.State.Verse public static readonly AetheriaRuntimeStateQueryParameters Empty = new AetheriaRuntimeStateQueryParameters(); } - public sealed class AetheriaRuntimeProjectedDocument + public sealed class AetheriaRuntimeProjectedDocument : IAetheriaRuntimeProjectedDocument { private static readonly CultMeshPollingWatchOptions DefaultWatchOptions = new CultMeshPollingWatchOptions(TimeSpan.FromMilliseconds(100)); + private static readonly CultDocumentAttribute? DocumentAttribute = + typeof(TDocument).GetCustomAttribute(); private readonly CultMeshBoundLiveFeed _feed; @@ -28,6 +32,12 @@ namespace GameCult.Aetheria.State.Verse public string DocumentId => _feed.FeedId; + public Type DocumentType => typeof(TDocument); + + public string SchemaName => DocumentAttribute?.SchemaName ?? ""; + + public string SchemaVersion => DocumentAttribute?.SchemaVersion ?? ""; + public CultMeshRouteHint RouteHint => _feed.RouteHint; public System.Collections.Generic.IReadOnlyList Sources => _feed.Sources; @@ -80,9 +90,25 @@ namespace GameCult.Aetheria.State.Verse } } + public interface IAetheriaRuntimeProjectedDocument + { + Type DocumentType { get; } + + string DocumentId { get; } + + string SchemaName { get; } + + string SchemaVersion { get; } + + CultMeshRouteHint RouteHint { get; } + + System.Collections.Generic.IReadOnlyList Sources { get; } + } + public sealed class AetheriaClientState { private readonly IReadOnlyDictionary _documentsByType; + private readonly IReadOnlyDictionary _documentsBySchema; internal AetheriaClientState( AetheriaRuntimeProjectedDocument currentZone, @@ -108,6 +134,7 @@ namespace GameCult.Aetheria.State.Verse [typeof(AetheriaRuntimeSectorMapDocument)] = SectorMap, [typeof(AetheriaRuntimeZoneRenderDocument)] = ZoneRender }; + _documentsBySchema = BuildSchemaIndex(_documentsByType.Values); } public AetheriaClientCurrentState Current { get; } @@ -120,6 +147,29 @@ namespace GameCult.Aetheria.State.Verse public AetheriaRuntimeProjectedDocument ZoneRender { get; } + public bool TryGetDocumentBySchema( + string schemaVersion, + out IAetheriaRuntimeProjectedDocument document) + { + if (!string.IsNullOrWhiteSpace(schemaVersion) && + _documentsBySchema.TryGetValue(schemaVersion, out document!)) + { + return true; + } + + document = null!; + return false; + } + + public IAetheriaRuntimeProjectedDocument DocumentBySchema(string schemaVersion) + { + if (TryGetDocumentBySchema(schemaVersion, out var document)) + return document; + + throw new NotSupportedException( + $"Aetheria typed state does not expose a projected document for schema '{schemaVersion}'."); + } + public bool TryGetDocument( out AetheriaRuntimeProjectedDocument document) { @@ -130,6 +180,15 @@ namespace GameCult.Aetheria.State.Verse return true; } + var schemaVersion = SchemaVersionFor(typeof(TDocument)); + if (!string.IsNullOrWhiteSpace(schemaVersion) && + _documentsBySchema.TryGetValue(schemaVersion, out var schemaDocument) && + schemaDocument is AetheriaRuntimeProjectedDocument schemaTypedDocument) + { + document = schemaTypedDocument; + return true; + } + document = null!; return false; } @@ -157,6 +216,29 @@ namespace GameCult.Aetheria.State.Verse { return Document().Watch(onNext); } + + private static IReadOnlyDictionary BuildSchemaIndex( + IEnumerable documents) + { + var index = new Dictionary(StringComparer.Ordinal); + foreach (var document in documents) + { + if (document is not IAetheriaRuntimeProjectedDocument projectedDocument) + continue; + + if (!string.IsNullOrWhiteSpace(projectedDocument.SchemaVersion)) + index[projectedDocument.SchemaVersion] = projectedDocument; + if (!string.IsNullOrWhiteSpace(projectedDocument.SchemaName)) + index[projectedDocument.SchemaName] = projectedDocument; + } + + return index; + } + + private static string SchemaVersionFor(Type documentType) + { + return documentType.GetCustomAttribute()?.SchemaVersion ?? ""; + } } public sealed class AetheriaClientCurrentState