Publish Aetheria hot state on negotiated planes

This commit is contained in:
Meta Vens 2026-07-19 03:29:53 +01:00
commit 1e6deff644
9 changed files with 129 additions and 64 deletions

View file

@ -31,9 +31,6 @@ await using var node = await AetheriaStateNode.OpenAsync(
runtimeId: options.DaemonId,
startServer: true,
enableDurableShardLogs: false).ConfigureAwait(false);
using var soaPublisher = new AetheriaRuntimeDaemonSoaFramePublisher(
node.Cache,
DateTimeOffset.UtcNow.ToUnixTimeMilliseconds());
using var discoveryHost = new AetheriaVerseDiscoveryHost(node);
await EnsureWorldDocumentAsync(node).ConfigureAwait(false);
@ -53,6 +50,11 @@ using var physicsPersistence = await AetheriaYmirPersistenceCoordinator.OpenAsyn
latestFrame).ConfigureAwait(false);
using var cultMeshRudpHost = StartClientCultMeshHost(node, options, () => latestFrame);
using var clientSubscriptions = new CultNetDatabaseSubscriptionServer(cultMeshRudpHost, node.Database);
using var bodyDemand = new CultMeshBodyDemandTracker(clientSubscriptions);
using var soaPublisher = new AetheriaRuntimeDaemonSoaFramePublisher(
node.Cache,
DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
bodyDemand);
using var clientPumpCancellation = new CancellationTokenSource();
var clientPump = RunClientCultMeshPumpAsync(cultMeshRudpHost, clientPumpCancellation.Token);
var nextApiPublicationUtc = DateTimeOffset.UtcNow;
@ -1584,7 +1586,8 @@ static async Task PublishDaemonApiDocumentsAsync(
.ReplaceAsync(result.SoaView)
.ConfigureAwait(false);
var soaPublication = await soaPublisher.PublishAsync(result.SoaFrame).ConfigureAwait(false);
await node.MutableDocument<CultMeshBodyPublicationDocument>(soaPublication.Body.RecordKey)
await node.MutableDocument<CultMeshBodyPublicationDocument>(
CultMeshBodyPublicationDocument.CreateLatestRecordKey(soaPublication.Body.BodyId))
.ReplaceAsync(soaPublication.Body)
.ConfigureAwait(false);
await node.MutableDocument<EveEntitySoaViewDocument>(AetheriaRuntimeVerseRecordKeys.EveEntitySoaViewLatest)

View file

@ -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.19",
"org.gamecult.cultlib": "https://github.com/GameCult/CultLib.git?path=/unity/org.gamecult.cultlib#cultlib-unity-v1.0.20",
"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.72"
"org.gamecult.eve.unity-scene": "https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.unity-scene#eveunity-scene-v0.3.74"
}
}

View file

@ -127,11 +127,11 @@
}
},
"org.gamecult.cultlib": {
"version": "https://github.com/GameCult/CultLib.git?path=/unity/org.gamecult.cultlib#cultlib-unity-v1.0.19",
"version": "https://github.com/GameCult/CultLib.git?path=/unity/org.gamecult.cultlib#cultlib-unity-v1.0.20",
"depth": 0,
"source": "git",
"dependencies": {},
"hash": "693df0901d75cfd8e3a0a5225e270011eeddb0be"
"hash": "3e223ceecd25c2d2d0c57dfb0cb0c2f177c87a65"
},
"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.72",
"version": "https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.unity-scene#eveunity-scene-v0.3.74",
"depth": 0,
"source": "git",
"dependencies": {
"org.gamecult.cultlib": "1.0.18",
"org.gamecult.cultlib": "1.0.20",
"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": "edaf3b1e3bbb06a00b18b9faf508e27e33ae050b"
"hash": "092fda75177aca7776474d43df87a29872e51d01"
},
"com.unity.modules.assetbundle": {
"version": "1.0.0",

View file

@ -516,6 +516,8 @@ public class DaemonRuntimeDocumentTests
AetheriaRuntimeVerseRecordKeys.EveEntitySoaViewLatest.ToString()));
Assert.IsTrue(ContainsSurfaceProp(surface.Surface.Root, "entityViewSchema",
EveEntitySoaViewDocument.SchemaId));
Assert.IsTrue(ContainsSurfaceProp(surface.Surface.Root, "entityBodyId",
AetheriaRuntimeDaemonSoaFramePublisher.BodyId));
Assert.IsFalse(ContainsSurfaceKind(surface.Surface.Root, "world.entity3d"));
Assert.IsTrue(ContainsSurfaceKind(surface.Surface.Root, "field.volume3d"));
Assert.IsTrue(ContainsSurfaceProp(surface.Surface.Root, "materialAssetRef",

View file

@ -21,7 +21,7 @@
"org.gamecult.aetheria.eve-runtime": "file:org.gamecult.aetheria.eve-runtime",
"org.gamecult.aetheria.state": "file:org.gamecult.aetheria.state",
"org.gamecult.cultmath": "file:../../CultMath",
"org.gamecult.cultlib": "https://github.com/GameCult/CultLib.git?path=/unity/org.gamecult.cultlib#cultlib-unity-v1.0.19",
"org.gamecult.cultlib": "https://github.com/GameCult/CultLib.git?path=/unity/org.gamecult.cultlib#cultlib-unity-v1.0.20",
"org.gamecult.eve.surface": "file:../../EveUnity/packages/org.gamecult.eve.surface",
"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.unity-scene": "file:../../EveUnity/packages/org.gamecult.eve.unity-scene",

View file

@ -629,6 +629,7 @@ namespace GameCult.Aetheria.State.Verse
["statePointerId"] = AetheriaRuntimeVerseRecordKeys.DaemonFrameLatest.ToString(),
["entityViewPointerId"] = AetheriaRuntimeVerseRecordKeys.EveEntitySoaViewLatest.ToString(),
["entityViewSchema"] = EveEntitySoaViewDocument.SchemaId,
["entityBodyId"] = AetheriaRuntimeDaemonSoaFramePublisher.BodyId,
["zoneRenderPointerId"] = AetheriaRuntimeVerseRecordKeys.ZoneRenderLatest.ToString(),
["zoneRenderSchema"] = AetheriaRuntimeDaemonSchemas.ZoneRender,
["assetManifest"] = AetheriaRuntimeVerseRecordKeys.EveAssetCatalog.ToString(),
@ -827,6 +828,7 @@ namespace GameCult.Aetheria.State.Verse
["statePointerId"] = AetheriaRuntimeVerseRecordKeys.DaemonFrameLatest.ToString(),
["entityViewPointerId"] = AetheriaRuntimeVerseRecordKeys.EveEntitySoaViewLatest.ToString(),
["entityViewSchema"] = EveEntitySoaViewDocument.SchemaId,
["entityBodyId"] = AetheriaRuntimeDaemonSoaFramePublisher.BodyId,
["zoneRenderPointerId"] = AetheriaRuntimeVerseRecordKeys.ZoneRenderLatest.ToString(),
["zoneRenderSchema"] = AetheriaRuntimeDaemonSchemas.ZoneRender,
["assetManifest"] = AetheriaRuntimeVerseRecordKeys.EveAssetCatalog.ToString(),

View file

@ -1,4 +1,5 @@
using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@ -10,16 +11,43 @@ using GameCult.Mesh;
namespace GameCult.Aetheria.State.Verse
{
public sealed class AetheriaRuntimeDaemonSoaFrame
public sealed class AetheriaRuntimeDaemonSoaFrame : IDisposable
{
public AetheriaRuntimeDaemonSoaFrame(AetheriaRuntimeDaemonSoaViewDocument view, byte[] bytes)
private CultMeshFrameBodyWriteLease? _write;
public AetheriaRuntimeDaemonSoaFrame(
AetheriaRuntimeDaemonSoaViewDocument view,
CultMeshFrameBodyWriteLease write,
int byteLength,
bool publishSharedMemory,
bool publishNetwork)
{
View = view ?? throw new ArgumentNullException(nameof(view));
Bytes = bytes ?? throw new ArgumentNullException(nameof(bytes));
_write = write ?? throw new ArgumentNullException(nameof(write));
ByteLength = byteLength;
PublishSharedMemory = publishSharedMemory;
PublishNetwork = publishNetwork;
}
public AetheriaRuntimeDaemonSoaViewDocument View { get; }
public byte[] Bytes { get; }
public int ByteLength { get; }
public bool PublishSharedMemory { get; }
public bool PublishNetwork { get; }
public ReadOnlySpan<byte> Span => (_write ?? throw new ObjectDisposedException(nameof(AetheriaRuntimeDaemonSoaFrame))).Span[..ByteLength];
public CultMeshBodyDescriptor Commit(DateTimeOffset nowUtc)
{
var write = _write ?? throw new ObjectDisposedException(nameof(AetheriaRuntimeDaemonSoaFrame));
var descriptor = write.Commit(ByteLength, nowUtc);
_write = null;
return descriptor;
}
public void Dispose()
{
_write?.Dispose();
_write = null;
}
}
public sealed class AetheriaRuntimeDaemonSoaPublication
@ -55,11 +83,15 @@ namespace GameCult.Aetheria.State.Verse
private readonly CultMeshFrameBodyPublisher _localPublisher;
private readonly CultMeshNetworkBodyPublisher _networkPublisher;
private readonly CultMeshBodyDemandTracker? _demand;
private readonly Dictionary<string, int> _syntheticEntityIndices =
new Dictionary<string, int>(StringComparer.Ordinal);
private int _nextSyntheticEntityIndex = -2;
public AetheriaRuntimeDaemonSoaFramePublisher(CultCache cache, long producerEpoch)
public AetheriaRuntimeDaemonSoaFramePublisher(
CultCache cache,
long producerEpoch,
CultMeshBodyDemandTracker? demand = null)
{
if (cache == null) throw new ArgumentNullException(nameof(cache));
_localPublisher = new CultMeshFrameBodyPublisher(
@ -69,14 +101,20 @@ namespace GameCult.Aetheria.State.Verse
_networkPublisher = new CultMeshNetworkBodyPublisher(
cache,
generation => string.Equals(generation.ProducerId, ProducerId, StringComparison.Ordinal));
_demand = demand;
}
public AetheriaRuntimeDaemonSoaFrame BuildCurrentZoneEntities(
public AetheriaRuntimeDaemonSoaFrame? BuildCurrentZoneEntities(
AetheriaRuntimeDaemonFrameDocument frame,
AetheriaRuntimeCatalogSnapshot? catalog = null)
{
if (frame == null)
throw new ArgumentNullException(nameof(frame));
var demand = _demand?.Plan(BodyId);
if (demand != null && !demand.HasConsumers)
return null;
var publishSharedMemory = demand?.RequiresSharedMemory ?? true;
var publishNetwork = demand?.RequiresNetwork ?? true;
var run = frame.Run ?? new AetheriaRuntimeRunCheckpointCommit();
var zone = (run.Zones ?? Array.Empty<AetheriaRuntimeZoneSnapshotCommit>())
@ -142,22 +180,27 @@ namespace GameCult.Aetheria.State.Verse
throw new InvalidOperationException($"Aetheria entity SoA capacity {Capacity} was exceeded by {count} rows.");
var generation = Math.Max(frame.FrameId, 0);
var layout = EntityHotSlabLayout.Create(count);
var bytes = new byte[layout.TotalByteLength];
WriteEntities(bytes, layout, entities);
WritePickups(bytes, layout, pickups, pickupEntityIds, syntheticEntityIndices, entities.Length);
WritePayloads(bytes, layout, payloads, payloadEntityIds, syntheticEntityIndices, entities.Length + pickups.Length);
WriteCelestialBodies(
bytes,
layout,
celestialBodies,
syntheticEntityIndices,
entities.Length + pickups.Length + payloads.Length);
WriteAsteroidInstances(
bytes,
layout,
asteroidInstances,
syntheticEntityIndices,
entities.Length + pickups.Length + payloads.Length + celestialBodies.Count);
if (!_localPublisher.TryAcquireWrite(out var write))
throw new InvalidOperationException("CultMesh has no unleased frame slot for the Aetheria SoA generation.");
try
{
var bytes = write.Span[..checked((int)layout.TotalByteLength)];
bytes.Clear();
WriteEntities(bytes, layout, entities);
WritePickups(bytes, layout, pickups, pickupEntityIds, syntheticEntityIndices, entities.Length);
WritePayloads(bytes, layout, payloads, payloadEntityIds, syntheticEntityIndices, entities.Length + pickups.Length);
WriteCelestialBodies(
bytes,
layout,
celestialBodies,
syntheticEntityIndices,
entities.Length + pickups.Length + payloads.Length);
WriteAsteroidInstances(
bytes,
layout,
asteroidInstances,
syntheticEntityIndices,
entities.Length + pickups.Length + payloads.Length + celestialBodies.Count);
var view = AetheriaRuntimeDaemonSoaViewDocument.Create(
string.IsNullOrWhiteSpace(frame.DaemonId) ? "aetheria-daemon" : frame.DaemonId,
@ -239,15 +282,26 @@ namespace GameCult.Aetheria.State.Verse
}))
.ToArray());
return new AetheriaRuntimeDaemonSoaFrame(view, bytes);
return new AetheriaRuntimeDaemonSoaFrame(
view,
write,
checked((int)layout.TotalByteLength),
publishSharedMemory,
publishNetwork);
}
catch
{
write.Dispose();
throw;
}
}
public async Task<AetheriaRuntimeDaemonSoaPublication> PublishAsync(AetheriaRuntimeDaemonSoaFrame frame)
{
if (frame == null) throw new ArgumentNullException(nameof(frame));
var now = DateTimeOffset.UtcNow;
if (!_localPublisher.TryPublish(frame.Bytes, now, out var local))
throw new InvalidOperationException("CultMesh has no unleased frame slot for the Aetheria SoA generation.");
var networkBytes = frame.PublishNetwork ? frame.Span.ToArray() : null;
var local = frame.Commit(now);
var generation = new CultMeshBodyGeneration
{
BodyId = BodyId,
@ -260,7 +314,10 @@ namespace GameCult.Aetheria.State.Verse
Synchronization = local.Synchronization,
LeaseExpiresAtUnixMs = local.LeaseExpiresAtUnixMs
};
var network = await _networkPublisher.PublishAsync(generation, frame.Bytes).ConfigureAwait(false);
var representations = new List<CultMeshBodyDescriptor>();
if (frame.PublishSharedMemory) representations.Add(local);
if (frame.PublishNetwork)
representations.Add(await _networkPublisher.PublishAsync(generation, networkBytes!).ConfigureAwait(false));
var publication = new CultMeshBodyPublicationDocument
{
BodyId = BodyId,
@ -273,8 +330,7 @@ namespace GameCult.Aetheria.State.Verse
Sequence = local.Sequence,
Synchronization = local.Synchronization,
LivenessExpiresAtUnixMs = local.LeaseExpiresAtUnixMs,
PreferredLocal = local,
NetworkFallback = network
Representations = representations.ToArray()
};
new CultMeshBodyPublicationHandle(BodyId, publication.ProducerEpoch, publication.Sequence)
.Validate(publication);
@ -350,7 +406,7 @@ namespace GameCult.Aetheria.State.Verse
}
private static void WriteEntities(
byte[] bytes,
Span<byte> bytes,
EntityHotSlabLayout layout,
IReadOnlyList<AetheriaRuntimeEntitySnapshotCommit> entities)
{
@ -373,7 +429,7 @@ namespace GameCult.Aetheria.State.Verse
}
private static void WritePickups(
byte[] bytes,
Span<byte> bytes,
EntityHotSlabLayout layout,
IReadOnlyList<AetheriaRuntimeDroppedPickupCommit> pickups,
IReadOnlyList<string> pickupEntityIds,
@ -400,7 +456,7 @@ namespace GameCult.Aetheria.State.Verse
}
private static void WritePayloads(
byte[] bytes,
Span<byte> bytes,
EntityHotSlabLayout layout,
IReadOnlyList<AetheriaRuntimePhysicalPayloadCommit> payloads,
IReadOnlyList<string> payloadEntityIds,
@ -528,7 +584,7 @@ namespace GameCult.Aetheria.State.Verse
}
private static void WriteCelestialBodies(
byte[] bytes,
Span<byte> bytes,
EntityHotSlabLayout layout,
IReadOnlyList<CelestialBodyPresentation> values,
IReadOnlyDictionary<string, int> syntheticEntityIndices,
@ -552,7 +608,7 @@ namespace GameCult.Aetheria.State.Verse
}
private static void WriteAsteroidInstances(
byte[] bytes,
Span<byte> bytes,
EntityHotSlabLayout layout,
IReadOnlyList<AsteroidPresentation> values,
IReadOnlyDictionary<string, int> syntheticEntityIndices,
@ -576,7 +632,7 @@ namespace GameCult.Aetheria.State.Verse
}
private static void WritePresentationRow(
byte[] bytes,
Span<byte> bytes,
EntityHotSlabLayout layout,
int row,
int entityIndex,
@ -673,13 +729,16 @@ namespace GameCult.Aetheria.State.Verse
public double Scale { get; }
}
private static void WriteFloat(byte[] bytes, long byteOffset, int index, double value)
private static void WriteFloat(Span<byte> bytes, long byteOffset, int index, double value)
{
WriteBytes(bytes, byteOffset + index * FloatStride, BitConverter.GetBytes(IsFinite(value) ? (float)value : 0.0f));
var scalar = IsFinite(value) ? (float)value : 0.0f;
BinaryPrimitives.WriteInt32LittleEndian(
bytes.Slice(checked((int)(byteOffset + index * FloatStride)), FloatStride),
BitConverter.SingleToInt32Bits(scalar));
}
private static void WriteFloat3(
byte[] bytes,
Span<byte> bytes,
long byteOffset,
int index,
double x,
@ -692,14 +751,11 @@ namespace GameCult.Aetheria.State.Verse
WriteFloat(bytes, elementOffset + FloatStride * 2, 0, z);
}
private static void WriteInt32(byte[] bytes, long offset, int value) =>
WriteBytes(bytes, offset, BitConverter.GetBytes(value));
private static void WriteInt32(Span<byte> bytes, long offset, int value) =>
BinaryPrimitives.WriteInt32LittleEndian(bytes.Slice(checked((int)offset), IntStride), value);
private static void WriteUInt32(byte[] bytes, long offset, int value) =>
WriteBytes(bytes, offset, BitConverter.GetBytes((uint)value));
private static void WriteBytes(byte[] destination, long offset, byte[] source) =>
Buffer.BlockCopy(source, 0, destination, checked((int)offset), source.Length);
private static void WriteUInt32(Span<byte> bytes, long offset, int value) =>
BinaryPrimitives.WriteUInt32LittleEndian(bytes.Slice(checked((int)offset), IntStride), (uint)value);
private static bool IsFinite(double value)
{

View file

@ -4,7 +4,9 @@
"version": "file:../../Brokkr/surfaces/unity/Packages/com.gamecult.brokkr/",
"depth": 0,
"source": "local",
"dependencies": {}
"dependencies": {
"org.gamecult.cultlib": "1.0.16"
}
},
"com.unity.2d.sprite": {
"version": "1.0.0",
@ -311,11 +313,11 @@
}
},
"org.gamecult.cultlib": {
"version": "https://github.com/GameCult/CultLib.git?path=/unity/org.gamecult.cultlib#cultlib-unity-v1.0.18",
"version": "https://github.com/GameCult/CultLib.git?path=/unity/org.gamecult.cultlib#cultlib-unity-v1.0.20",
"depth": 0,
"source": "git",
"dependencies": {},
"hash": "49c43baa4c80b3bf8f5febae40baf99e1f2a0262"
"hash": "3e223ceecd25c2d2d0c57dfb0cb0c2f177c87a65"
},
"org.gamecult.cultmath": {
"version": "file:../../CultMath",
@ -345,7 +347,7 @@
"depth": 0,
"source": "local",
"dependencies": {
"org.gamecult.cultlib": "1.0.15",
"org.gamecult.cultlib": "1.0.20",
"org.gamecult.eve.plugin-fields": "0.2.3",
"org.gamecult.eve.surface": "0.2.4",
"com.unity.mathematics": "1.3.2",

View file

@ -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.19",
"693df0901d75cfd8e3a0a5225e270011eeddb0be")
"https://github.com/GameCult/CultLib.git?path=/unity/org.gamecult.cultlib#cultlib-unity-v1.0.20",
"3e223ceecd25c2d2d0c57dfb0cb0c2f177c87a65")
"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.72",
"edaf3b1e3bbb06a00b18b9faf508e27e33ae050b")
"https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.unity-scene#eveunity-scene-v0.3.74",
"092fda75177aca7776474d43df87a29872e51d01")
}
foreach ($packageName in $expected.Keys) {