From fc2255099fae1a6ac4dd6fb3e37324477fca8c83 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 19 Jul 2026 12:48:05 +0100 Subject: [PATCH] Publish managed viewports from exact demand --- Aetheria.State.Daemon/Program.cs | 116 ++++++++++++++++++++- Aetheria.Unity/Packages/manifest.json | 4 +- Aetheria.Unity/Packages/packages-lock.json | 10 +- scripts/verify-aetheria-unity-client.ps1 | 14 +-- 4 files changed, 129 insertions(+), 15 deletions(-) diff --git a/Aetheria.State.Daemon/Program.cs b/Aetheria.State.Daemon/Program.cs index 305e90ca0..acdfef300 100644 --- a/Aetheria.State.Daemon/Program.cs +++ b/Aetheria.State.Daemon/Program.cs @@ -11,6 +11,7 @@ using System.Globalization; using System.Diagnostics; using System.Net; using System.Net.Sockets; +using System.Collections.Concurrent; var options = AetheriaDaemonHostOptions.Parse(args); var startedAtUtc = DateTimeOffset.UtcNow.ToString("O"); @@ -50,10 +51,13 @@ using var physicsPersistence = await AetheriaYmirPersistenceCoordinator.OpenAsyn latestFrame).ConfigureAwait(false); using var cultMeshRudpHost = StartClientCultMeshHost(node, options, () => latestFrame); using var clientSubscriptions = new CultNetDatabaseSubscriptionServer(cultMeshRudpHost, node.Database); +var managedViewportDemand = new AetheriaManagedViewportDemandState(); +clientSubscriptions.DemandChanged += managedViewportDemand.Observe; if (traceClientRudp) clientSubscriptions.DemandChanged += demand => Console.WriteLine( - $"CultMesh body demand consumer={demand.ConsumerRuntimeId} subscription={demand.SubscriptionId} " + + $"CultMesh state demand consumer={demand.ConsumerRuntimeId} subscription={demand.SubscriptionId} " + $"active={demand.Active} sameMachine={demand.SameMachine} " + + $"records=[{string.Join(",", demand.RecordKeys)}] schemas=[{string.Join(",", demand.SchemaIds)}] " + $"bodies=[{string.Join(",", demand.BodyIds)}] transports=[{string.Join(",", demand.SupportedBodyTransports)}]"); using var bodyDemand = new CultMeshBodyDemandTracker(clientSubscriptions); using var soaPublisher = new AetheriaRuntimeDaemonSoaFramePublisher( @@ -142,6 +146,7 @@ while (!stopped.Task.IsCompleted) publication, reactiveSurfaceState, publishTopology: false); + await PublishDemandedManagedViewportsAsync(node, options, tick.Frame, managedViewportDemand).ConfigureAwait(false); nextApiPublicationUtc = DateTimeOffset.UtcNow.Add(options.ApiPublicationInterval); } if (tick.Frame.FrameId % (traceClientRudp ? 10 : 120) == 0) @@ -1516,6 +1521,71 @@ static bool TryGetDouble( double.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out value); } +static async Task PublishDemandedManagedViewportsAsync( + AetheriaStateNode node, + AetheriaDaemonHostOptions options, + AetheriaRuntimeDaemonFrameDocument frame, + AetheriaManagedViewportDemandState demand) +{ + foreach (var request in demand.Snapshot()) + { + if (!TryGetGameViewportRequest( + new CultNetSnapshotRequestMessage + { + RecordKeys = request.RecordKeys.ToArray(), + SchemaIds = request.SchemaIds.ToArray() + }, + out var recordKey, + out var schemaId, + out var viewport)) + { + continue; + } + + CultNetDocumentPutRawMessage put; + var messageId = $"aetheria-managed-viewport:{frame.FrameId}:{recordKey}"; + var messageOptions = new CultNetDocumentMessageOptions + { + SourceRuntimeId = options.DaemonId, + SourceRole = "aetheria-daemon" + }; + if (string.Equals(schemaId, AetheriaRuntimeDaemonSchemas.RenderSplatsViewport, StringComparison.Ordinal)) + { + put = node.Database.Documents.CreateRawDocumentPutMessage( + messageId, + new CultRecordHandle(new CultRecordKey(recordKey)), + AetheriaRuntimeGameDocuments.RenderSplatsViewport(frame, viewport), + messageOptions); + } + else if (string.Equals(schemaId, AetheriaRuntimeDaemonSchemas.GravityViewport, StringComparison.Ordinal)) + { + put = node.Database.Documents.CreateRawDocumentPutMessage( + messageId, + new CultRecordHandle(new CultRecordKey(recordKey)), + AetheriaRuntimeGameDocuments.GravityViewport(frame, viewport), + messageOptions); + } + else if (string.Equals(schemaId, AetheriaRuntimeDaemonSchemas.ObjectsViewport, StringComparison.Ordinal)) + { + put = node.Database.Documents.CreateRawDocumentPutMessage( + messageId, + new CultRecordHandle(new CultRecordKey(recordKey)), + AetheriaRuntimeGameDocuments.ObjectsViewport(frame, viewport), + messageOptions); + } + else + { + put = node.Database.Documents.CreateRawDocumentPutMessage( + messageId, + new CultRecordHandle(new CultRecordKey(recordKey)), + AetheriaRuntimeGameDocuments.Viewport(frame, viewport), + messageOptions); + } + + await node.Database.ApplyPutAsync(put).ConfigureAwait(false); + } +} + static async Task PublishHotEntityStateAsync( AetheriaStateNode node, AetheriaRuntimeDaemonSoaFramePublisher publisher, @@ -3167,6 +3237,50 @@ internal sealed record AetheriaPreparedPublication( AetheriaRuntimeDaemonTickResult Publication, IReadOnlyList Physics); +internal sealed class AetheriaManagedViewportDemandState +{ + private readonly ConcurrentDictionary _active = + new(StringComparer.Ordinal); + + public void Observe(CultNetDatabaseSubscriptionDemand demand) + { + var key = demand.ConsumerRuntimeId + "\u001f" + demand.SubscriptionId; + if (!demand.Active) + { + _active.TryRemove(key, out _); + return; + } + + if (demand.RecordKeys.Count == 0) + return; + _active[key] = new AetheriaManagedViewportDemand( + demand.RecordKeys.ToArray(), + demand.SchemaIds.ToArray()); + } + + public IReadOnlyList Snapshot() => + _active.Values + .GroupBy( + demand => string.Join("\u001f", demand.RecordKeys) + "\u001e" + string.Join("\u001f", demand.SchemaIds), + StringComparer.Ordinal) + .Select(group => group.First()) + .ToArray(); +} + +internal sealed class AetheriaManagedViewportDemand +{ + public AetheriaManagedViewportDemand( + IReadOnlyList recordKeys, + IReadOnlyList schemaIds) + { + RecordKeys = recordKeys; + SchemaIds = schemaIds; + } + + public IReadOnlyList RecordKeys { get; } + public IReadOnlyList SchemaIds { get; } +} + internal sealed class AetheriaReactiveSurfacePublicationState { private long? _version; diff --git a/Aetheria.Unity/Packages/manifest.json b/Aetheria.Unity/Packages/manifest.json index 50ff3b5f3..a27d70649 100644 --- a/Aetheria.Unity/Packages/manifest.json +++ b/Aetheria.Unity/Packages/manifest.json @@ -7,9 +7,9 @@ "com.unity.modules.physics": "1.0.0", "com.unity.modules.uielements": "1.0.0", "com.unity.render-pipelines.universal": "17.4.0", - "org.gamecult.cultlib": "https://github.com/GameCult/CultLib.git?path=/unity/org.gamecult.cultlib#cultlib-unity-v1.0.29", + "org.gamecult.cultlib": "https://github.com/GameCult/CultLib.git?path=/unity/org.gamecult.cultlib#cultlib-unity-v1.0.30", "org.gamecult.eve.plugin-fields": "https://github.com/GameCult/EvePlugins.git?path=/plugins/eve-plugin-fields/unity/org.gamecult.eve.plugin-fields#eve-plugin-fields-unity-v0.2.3", "org.gamecult.eve.surface": "https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.surface#eveunity-surface-v0.2.4", - "org.gamecult.eve.unity-scene": "https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.unity-scene#eveunity-scene-v0.3.87" + "org.gamecult.eve.unity-scene": "https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.unity-scene#eveunity-scene-v0.3.88" } } diff --git a/Aetheria.Unity/Packages/packages-lock.json b/Aetheria.Unity/Packages/packages-lock.json index a4267c0a1..ef9743027 100644 --- a/Aetheria.Unity/Packages/packages-lock.json +++ b/Aetheria.Unity/Packages/packages-lock.json @@ -127,11 +127,11 @@ } }, "org.gamecult.cultlib": { - "version": "https://github.com/GameCult/CultLib.git?path=/unity/org.gamecult.cultlib#cultlib-unity-v1.0.29", + "version": "https://github.com/GameCult/CultLib.git?path=/unity/org.gamecult.cultlib#cultlib-unity-v1.0.30", "depth": 0, "source": "git", "dependencies": {}, - "hash": "a1f25af2e0f66001270819737f3062133d91d199" + "hash": "0ef59b50c27ac019929f5e68bcd602983a190062" }, "org.gamecult.eve.plugin-fields": { "version": "https://github.com/GameCult/EvePlugins.git?path=/plugins/eve-plugin-fields/unity/org.gamecult.eve.plugin-fields#eve-plugin-fields-unity-v0.2.3", @@ -152,18 +152,18 @@ "hash": "e08fa08335f99e9edddeb706912eecfad07cb281" }, "org.gamecult.eve.unity-scene": { - "version": "https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.unity-scene#eveunity-scene-v0.3.87", + "version": "https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.unity-scene#eveunity-scene-v0.3.88", "depth": 0, "source": "git", "dependencies": { - "org.gamecult.cultlib": "1.0.29", + "org.gamecult.cultlib": "1.0.30", "org.gamecult.eve.plugin-fields": "0.2.3", "org.gamecult.eve.surface": "0.2.4", "com.unity.mathematics": "1.3.2", "com.unity.render-pipelines.universal": "17.4.0", "com.unity.modules.particlesystem": "1.0.0" }, - "hash": "ab226e4f5f15b935a3f692b1eaad55e0c618a4ee" + "hash": "3ce813e29dcc5342b51985187de26c62eb8c90b4" }, "com.unity.modules.assetbundle": { "version": "1.0.0", diff --git a/scripts/verify-aetheria-unity-client.ps1 b/scripts/verify-aetheria-unity-client.ps1 index 0debbdcf5..7b094bc0f 100644 --- a/scripts/verify-aetheria-unity-client.ps1 +++ b/scripts/verify-aetheria-unity-client.ps1 @@ -14,14 +14,14 @@ $manifest = Get-Content (Join-Path $ProjectPath "Packages/manifest.json") -Raw | $lock = Get-Content (Join-Path $ProjectPath "Packages/packages-lock.json") -Raw | ConvertFrom-Json $expected = @{ "org.gamecult.cultlib" = @( - "https://github.com/GameCult/CultLib.git?path=/unity/org.gamecult.cultlib#cultlib-unity-v1.0.29", - "a1f25af2e0f66001270819737f3062133d91d199") + "https://github.com/GameCult/CultLib.git?path=/unity/org.gamecult.cultlib#cultlib-unity-v1.0.30", + "0ef59b50c27ac019929f5e68bcd602983a190062") "org.gamecult.eve.surface" = @( "https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.surface#eveunity-surface-v0.2.4", "e08fa08335f99e9edddeb706912eecfad07cb281") "org.gamecult.eve.unity-scene" = @( - "https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.unity-scene#eveunity-scene-v0.3.87", - "ab226e4f5f15b935a3f692b1eaad55e0c618a4ee") + "https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.unity-scene#eveunity-scene-v0.3.88", + "3ce813e29dcc5342b51985187de26c62eb8c90b4") } foreach ($packageName in $expected.Keys) { @@ -34,7 +34,7 @@ foreach ($packageName in $expected.Keys) { $cultLibPackage = Get-ChildItem (Join-Path $ProjectPath "Library/PackageCache") -Directory | Where-Object Name -Like "org.gamecult.cultlib@*" | - Where-Object { (Get-Content (Join-Path $_.FullName "package.json") -Raw | ConvertFrom-Json).version -eq "1.0.29" } | + Where-Object { (Get-Content (Join-Path $_.FullName "package.json") -Raw | ConvertFrom-Json).version -eq "1.0.30" } | Select-Object -First 1 if (-not $cultLibPackage) { throw "Resolved CultLib package is missing from Library/PackageCache." } @@ -68,9 +68,9 @@ foreach ($requiredInputContract in @("PayloadKeys", "CurrentValue", "ActionBar", $scenePackage = Get-ChildItem (Join-Path $ProjectPath "Library/PackageCache") -Directory | Where-Object Name -Like "org.gamecult.eve.unity-scene@*" | - Where-Object { (Get-Content (Join-Path $_.FullName "package.json") -Raw | ConvertFrom-Json).version -eq "0.3.87" } | + Where-Object { (Get-Content (Join-Path $_.FullName "package.json") -Raw | ConvertFrom-Json).version -eq "0.3.88" } | Select-Object -First 1 -if (-not $scenePackage) { throw "Resolved Eve Unity scene 0.3.87 package is missing from Library/PackageCache." } +if (-not $scenePackage) { throw "Resolved Eve Unity scene 0.3.88 package is missing from Library/PackageCache." } $advertisedInputSource = Get-Content (Join-Path $scenePackage.FullName "Runtime/EveUnityAdvertisedInputAction.cs") -Raw $inputDriverSource = Get-Content (Join-Path $scenePackage.FullName "Runtime/EveUnityPlayableWorldInputDriver.cs") -Raw $actionBarSource = Get-Content (Join-Path $scenePackage.FullName "Runtime/EveUnityInputActionBar.cs") -Raw