From 7a6172d34c999c24f9fe8b9bd404c128ce443ae5 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 27 Jun 2026 14:50:15 +0100 Subject: [PATCH] Use CultMesh document handles for runtime state --- .../Runtime/AetheriaClient.cs | 4 + .../Runtime/AetheriaClientState.cs | 103 +++++++----------- .../Runtime/AetheriaRuntimeVerseClient.cs | 1 + 3 files changed, 42 insertions(+), 66 deletions(-) diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClient.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClient.cs index b50db52e5..d654073de 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClient.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClient.cs @@ -41,6 +41,7 @@ namespace GameCult.Aetheria.State.Verse public AetheriaClientState Aetheria() => State; public AetheriaRuntimeProjectedDocument Document() + where TDocument : class { ThrowIfDisposed(); return State.Document(); @@ -53,18 +54,21 @@ namespace GameCult.Aetheria.State.Verse } public Task LatestAsync() + where TDocument : class { ThrowIfDisposed(); return State.LatestAsync(); } public Observable Watch() + where TDocument : class { ThrowIfDisposed(); return State.Watch(); } public IDisposable Watch(Action onNext) + where TDocument : class { ThrowIfDisposed(); return State.Watch(onNext); diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClientState.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClientState.cs index 55eb53168..a520a9bec 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClientState.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClientState.cs @@ -1,9 +1,7 @@ using System; using System.Collections.Generic; -using System.Reflection; using System.Threading.Tasks; using GameCult.Caching; -using GameCult.Caching.MessagePack; using GameCult.Mesh; using R3; @@ -11,56 +9,52 @@ using R3; namespace GameCult.Aetheria.State.Verse { - public readonly struct AetheriaRuntimeStateQueryParameters - { - public static readonly AetheriaRuntimeStateQueryParameters Empty = new AetheriaRuntimeStateQueryParameters(); - } - public sealed class AetheriaRuntimeProjectedDocument : IAetheriaRuntimeProjectedDocument + where TDocument : class { - private static readonly CultMeshPollingWatchOptions DefaultWatchOptions = - new CultMeshPollingWatchOptions(TimeSpan.FromMilliseconds(100)); - private static readonly CultDocumentAttribute? DocumentAttribute = - typeof(TDocument).GetCustomAttribute(); - - private readonly CultMeshBoundLiveFeed _feed; + private readonly CultMeshDocumentHandle _handle; internal AetheriaRuntimeProjectedDocument( - CultMeshBoundLiveFeed feed) + CultMeshDocumentHandle handle) { - _feed = feed ?? throw new ArgumentNullException(nameof(feed)); + _handle = handle ?? throw new ArgumentNullException(nameof(handle)); } - public string DocumentId => _feed.FeedId; + public string DocumentId => _handle.DocumentId; - public Type DocumentType => typeof(TDocument); + public Type DocumentType => _handle.DocumentType; - public string SchemaName => DocumentAttribute?.SchemaName ?? ""; + public string SchemaName => _handle.SchemaName; - public string SchemaVersion => DocumentAttribute?.SchemaVersion ?? ""; + public string SchemaVersion => _handle.SchemaVersion; - public CultMeshRouteHint RouteHint => _feed.RouteHint; + public CultMeshRouteHint RouteHint => _handle.RouteHint; - public System.Collections.Generic.IReadOnlyList Sources => _feed.Sources; + public System.Collections.Generic.IReadOnlyList Sources => _handle.Sources; - public CultMeshLiveFeed Feed => _feed.Feed; + public bool CanReplace => _handle.CanReplace; - internal CultMeshVerseContext Context => _feed.Context; + public CultMeshDocumentHandle Handle => _handle; public Task LatestAsync() { - return _feed.SnapshotAsync(AetheriaRuntimeStateQueryParameters.Empty); + return _handle.LatestAsync(); } public Observable Watch() { - return _feed.Watch(AetheriaRuntimeStateQueryParameters.Empty); + return _handle.Watch(); } public IDisposable Watch(Action onNext) { if (onNext == null) throw new ArgumentNullException(nameof(onNext)); - return Watch().Subscribe(onNext); + return _handle.Watch(onNext); + } + + public Task ReplaceAsync(TDocument value) + { + return _handle.ReplaceAsync(value); } internal static AetheriaRuntimeProjectedDocument Create( @@ -79,53 +73,23 @@ namespace GameCult.Aetheria.State.Verse if (frameChanges == null) throw new ArgumentNullException(nameof(frameChanges)); if (projectFrame == null) throw new ArgumentNullException(nameof(projectFrame)); - var feed = new CultMeshLiveFeed( + var verse = CultMesh.Verse("aetheria.local", runtimeId); + var handle = CultMesh.Document( documentId, - (_parameters, _context) => latest(), - (_parameters, context) => frameChanges + verse, + _ => latest(), + _ => frameChanges .SelectAwait(async (frame, cancellationToken) => await projectFrame(frame).ConfigureAwait(false)), sources: sources, routeHint: new CultMeshRouteHint(CultMeshLocalityKind.SharedMemory, "Aetheria typed projected state")); - var verse = CultMesh.Verse("aetheria.local", runtimeId); - return new AetheriaRuntimeProjectedDocument(feed.Bind(verse)); + return new AetheriaRuntimeProjectedDocument(handle); } - public AetheriaRuntimeProjectedDocument AsSchemaAlias() + public AetheriaRuntimeProjectedDocument AsSchemaAlias() where TAlias : class { - ValidateSchemaAlias(typeof(TDocument), typeof(TAlias)); - - TAlias Convert(TDocument document) - { - if (document is TAlias alreadyTyped) - return alreadyTyped; - - var payload = CultDocumentMessagePackSerialization.SerializeUntyped(document!, typeof(TDocument)); - return (TAlias)CultDocumentMessagePackSerialization.DeserializeUntyped(typeof(TAlias), payload); - } - - var feed = new CultMeshLiveFeed( - DocumentId, - async (parameters, _context) => Convert(await _feed.SnapshotAsync(parameters).ConfigureAwait(false)), - (parameters, _context) => _feed.Watch(parameters).Select(Convert), - sources: Sources, - routeHint: RouteHint); - - return new AetheriaRuntimeProjectedDocument(feed.Bind(Context)); - } - - private static void ValidateSchemaAlias(Type sourceType, Type aliasType) - { - var source = sourceType.GetCustomAttribute(); - var alias = aliasType.GetCustomAttribute(); - if (source == null || alias == null || - !string.Equals(source.SchemaName, alias.SchemaName, StringComparison.Ordinal) || - !string.Equals(source.SchemaVersion, alias.SchemaVersion, StringComparison.Ordinal)) - { - throw new InvalidOperationException( - $"{aliasType.FullName} cannot alias {sourceType.FullName}; CultDocument schema name/version must match exactly."); - } + return new AetheriaRuntimeProjectedDocument(_handle.AsSchemaAlias()); } } @@ -143,7 +107,9 @@ namespace GameCult.Aetheria.State.Verse System.Collections.Generic.IReadOnlyList Sources { get; } - AetheriaRuntimeProjectedDocument AsSchemaAlias(); + bool CanReplace { get; } + + AetheriaRuntimeProjectedDocument AsSchemaAlias() where TAlias : class; } public sealed class AetheriaClientState @@ -213,6 +179,7 @@ namespace GameCult.Aetheria.State.Verse public bool TryGetDocument( out AetheriaRuntimeProjectedDocument document) + where TDocument : class { if (_documentsByType.TryGetValue(typeof(TDocument), out var untypedDocument) && untypedDocument is AetheriaRuntimeProjectedDocument typedDocument) @@ -240,6 +207,7 @@ namespace GameCult.Aetheria.State.Verse } public AetheriaRuntimeProjectedDocument Document() + where TDocument : class { if (TryGetDocument(out var document)) return document; @@ -249,16 +217,19 @@ namespace GameCult.Aetheria.State.Verse } public Task LatestAsync() + where TDocument : class { return Document().LatestAsync(); } public Observable Watch() + where TDocument : class { return Document().Watch(); } public IDisposable Watch(Action onNext) + where TDocument : class { return Document().Watch(onNext); } @@ -283,7 +254,7 @@ namespace GameCult.Aetheria.State.Verse private static string SchemaVersionFor(Type documentType) { - return documentType.GetCustomAttribute()?.SchemaVersion ?? ""; + return CultDocumentRegistry.Shared.GetRequired(documentType).SchemaVersion; } } diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeVerseClient.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeVerseClient.cs index f6b84f4ed..9fb7bad4d 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeVerseClient.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeVerseClient.cs @@ -749,6 +749,7 @@ namespace GameCult.Aetheria.State.Verse Func> project, string schemaId, params CultMeshProjectionSource[] additionalSources) + where TDocument : class { var sources = new[] {