diff --git a/Aetheria.State.Unity.Smoke/Program.cs b/Aetheria.State.Unity.Smoke/Program.cs index 573ac7226..847da0cdd 100644 --- a/Aetheria.State.Unity.Smoke/Program.cs +++ b/Aetheria.State.Unity.Smoke/Program.cs @@ -235,7 +235,7 @@ try WeaponGroups = new[] { new[] { 0 } } } }); - AetheriaRuntimeStateCommitLog.QueueRunCheckpoint( + var runCommit = AetheriaRuntimeStateCommitLog.QueueRunCheckpoint( commitSmokeStatePath, new AetheriaRuntimeRunCheckpointCommit { @@ -694,6 +694,7 @@ try throw new InvalidOperationException("Runtime state commit log did not read the queued player settings command."); } AssertPendingCultCacheEnvelope(commit.Path, AetheriaRuntimeStateCommitDocument.SchemaId); + AssertRunCheckpointPendingPayloadUsesCurrentEntityKey(runCommit.Path); var typedNodeApplied = false; await using (var commitNode = await AetheriaStateNode.OpenAsync(commitSmokeStatePath, "aetheria-unity-runtime-commit-smoke")) @@ -1064,6 +1065,78 @@ static void AssertPendingCultCacheEnvelope(string path, string expectedSchemaId) } } +static void AssertRunCheckpointPendingPayloadUsesCurrentEntityKey(string path) +{ + var reader = new MessagePackReader(File.ReadAllBytes(path)); + var fieldCount = reader.ReadArrayHeader(); + if (fieldCount < 3) + { + throw new InvalidOperationException($"Pending file {path} is not a CultCache store snapshot."); + } + + reader.Skip(); // formatVersion + + var catalogCount = reader.ReadArrayHeader(); + for (var index = 0; index < catalogCount; index++) + { + reader.Skip(); + } + + var recordCount = reader.ReadArrayHeader(); + if (recordCount != 1) + { + throw new InvalidOperationException($"Pending file {path} should contain one CultCache record."); + } + + reader.ReadArrayHeader(); + reader.Skip(); // key + reader.Skip(); // schemaId + reader.Skip(); // storedAt + var payload = reader.ReadBytes() is { } payloadSequence + ? SequenceToArray(payloadSequence) + : Array.Empty(); + if (payload.Length == 0) + { + throw new InvalidOperationException($"Pending file {path} has an empty runtime commit payload."); + } + + var payloadReader = new MessagePackReader(payload); + var payloadFieldCount = payloadReader.ReadArrayHeader(); + if (payloadFieldCount < 7) + { + throw new InvalidOperationException($"Pending runtime commit payload {path} is incomplete."); + } + + payloadReader.Skip(); // schema + payloadReader.Skip(); // kind + payloadReader.Skip(); // commandId + payloadReader.Skip(); // createdAtUtc + payloadReader.Skip(); // playerSettings + payloadReader.Skip(); // loadoutTemplate + var runCheckpointPayload = SequenceToArray(payloadReader.ReadRaw()); + + var runReader = new MessagePackReader(runCheckpointPayload); + var runFieldCount = runReader.ReadArrayHeader(); + if (runFieldCount != 11) + { + throw new InvalidOperationException( + $"Pending run checkpoint payload should use the 11-field CurrentEntityKey layout, found {runFieldCount} fields."); + } +} + +static byte[] SequenceToArray(System.Buffers.ReadOnlySequence sequence) +{ + var payload = new byte[checked((int)sequence.Length)]; + var offset = 0; + foreach (var segment in sequence) + { + segment.Span.CopyTo(payload.AsSpan(offset)); + offset += segment.Length; + } + + return payload; +} + static int CountRequiredBehaviorItemRefsMissingItemKeys(AetheriaRuntimeBehaviorPayload payload) { return payload.Fields.Count(field => diff --git a/Aetheria.State.Verify/Program.cs b/Aetheria.State.Verify/Program.cs index dda0ae756..286fce20a 100644 --- a/Aetheria.State.Verify/Program.cs +++ b/Aetheria.State.Verify/Program.cs @@ -1973,6 +1973,16 @@ static void RequireMainMenuContinueRunState(string root) "Unity package runtime run snapshot still exposes integer current-entity slot authority."); } + var pendingCommitPath = Path.Combine(root, "Packages", "org.gamecult.aetheria.state", "Runtime", "AetheriaRuntimeStateCommitDocument.cs"); + var pendingCommit = File.Exists(pendingCommitPath) + ? File.ReadAllText(pendingCommitPath) + : throw new InvalidOperationException("Cannot verify pending commit authority; AetheriaRuntimeStateCommitDocument.cs is missing."); + if (pendingCommit.Contains("CurrentZoneEntityIndex", StringComparison.Ordinal)) + { + throw new InvalidOperationException( + "Pending runtime commit transport still exposes integer current-entity slot authority."); + } + var requiredPackageSymbols = new[] { "public string RecordKey", diff --git a/Aetheria.State/AetheriaRuntimeCommitLogApplier.cs b/Aetheria.State/AetheriaRuntimeCommitLogApplier.cs index 5ad851043..5d4fab371 100644 --- a/Aetheria.State/AetheriaRuntimeCommitLogApplier.cs +++ b/Aetheria.State/AetheriaRuntimeCommitLogApplier.cs @@ -281,22 +281,10 @@ public static class AetheriaRuntimeCommitLogApplier if (zoneKeys.Count > 0) run.ZoneKeys = zoneKeys.ToArray(); - run.CurrentEntityKey = string.IsNullOrWhiteSpace(checkpoint.CurrentEntityKey) - ? LegacyCurrentEntityKey(run.RunId, checkpoint.CurrentZoneIndex, checkpoint.CurrentZoneEntityIndex) - : checkpoint.CurrentEntityKey; + run.CurrentEntityKey = checkpoint.CurrentEntityKey ?? ""; await node.PutRunStateAsync(RunKey(run.RunId), run).ConfigureAwait(false); } - private static string LegacyCurrentEntityKey(string runId, int zoneIndex, int entityIndex) - { - if (string.IsNullOrWhiteSpace(runId) || zoneIndex < 0 || entityIndex < 0) - { - return ""; - } - - return EntityKey(runId, zoneIndex, entityIndex).ToString(); - } - private static AetheriaDroppedPickupSnapshot[] ToDroppedPickups( IReadOnlyList? pickups) { diff --git a/Aetheria.State/README.md b/Aetheria.State/README.md index 47dd00f31..6030d9fe9 100644 --- a/Aetheria.State/README.md +++ b/Aetheria.State/README.md @@ -329,7 +329,8 @@ The run document also carries `CurrentEntityKey`, so Continue restore binds the saved player entity by exact typed record identity instead of reconstructing it from a transient entity-slot index. Canonical typed run state no longer stores `CurrentZoneEntityIndex`; older stored runs are translated through the package -reader into `CurrentEntityKey` when that compatibility seam is needed. +reader into `CurrentEntityKey`, and older queued runtime commits are translated +through the pending commit reader when those compatibility seams are needed. `Aetheria.State.Verify` opens a materialized state file and checks that the typed migration ledger matches the actual item, corporation, and name-file diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimePendingCultCacheStore.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimePendingCultCacheStore.cs index 5fe0e0667..759a90bbd 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimePendingCultCacheStore.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimePendingCultCacheStore.cs @@ -31,8 +31,7 @@ namespace GameCult.Aetheria.State.Unity public static AetheriaRuntimeStateCommitDocument ReadStateCommit(string path) { - return MessagePackSerializer.Deserialize( - ReadDocumentPayload(path, AetheriaRuntimeStateCommitDocument.SchemaId)); + return ReadStateCommitDocument(ReadDocumentPayload(path, AetheriaRuntimeStateCommitDocument.SchemaId)); } public static void WriteEveCommand(string path, AetheriaRuntimeEveCommandDocument document) @@ -131,6 +130,120 @@ namespace GameCult.Aetheria.State.Unity return payload; } + private static AetheriaRuntimeStateCommitDocument ReadStateCommitDocument(byte[] payload) + { + var reader = new MessagePackReader(payload); + var fieldCount = reader.ReadArrayHeader(); + var document = new AetheriaRuntimeStateCommitDocument + { + Schema = ReadFieldString(ref reader, fieldCount, 0, AetheriaRuntimeStateCommitDocument.SchemaId), + Kind = ReadFieldString(ref reader, fieldCount, 1, ""), + CommandId = ReadFieldString(ref reader, fieldCount, 2, ""), + CreatedAtUtc = ReadFieldString(ref reader, fieldCount, 3, ""), + PlayerSettings = ReadFieldDocument(ref reader, fieldCount, 4), + LoadoutTemplate = ReadFieldDocument(ref reader, fieldCount, 5), + RunCheckpoint = ReadFieldRunCheckpoint(ref reader, fieldCount, 6) + }; + + for (var index = 7; index < fieldCount; index++) + { + reader.Skip(); + } + + return document; + } + + private static T? ReadFieldDocument(ref MessagePackReader reader, int fields, int index) + where T : class + { + if (index >= fields) + { + return null; + } + + if (reader.NextMessagePackType == MessagePackType.Nil) + { + reader.ReadNil(); + return null; + } + + return MessagePackSerializer.Deserialize(ReadRawBytes(ref reader)); + } + + private static AetheriaRuntimeRunCheckpointCommit? ReadFieldRunCheckpoint(ref MessagePackReader reader, int fields, int index) + { + if (index >= fields) + { + return null; + } + + if (reader.NextMessagePackType == MessagePackType.Nil) + { + reader.ReadNil(); + return null; + } + + return ReadRunCheckpoint(ReadRawBytes(ref reader)); + } + + private static AetheriaRuntimeRunCheckpointCommit ReadRunCheckpoint(byte[] payload) + { + var reader = new MessagePackReader(payload); + var fields = reader.ReadArrayHeader(); + + var runId = ReadFieldString(ref reader, fields, 0, "local"); + var isTutorial = ReadFieldBool(ref reader, fields, 1, false); + var entranceZoneIndex = ReadFieldInt32(ref reader, fields, 2, -1); + var exitZoneIndex = ReadFieldInt32(ref reader, fields, 3, -1); + var currentZoneIndex = ReadFieldInt32(ref reader, fields, 4, -1); + + string currentEntityKey; + int[] discoveredZoneIndices; + AetheriaRuntimeZoneSnapshotCommit[] zones; + AetheriaRuntimeActionBarBindingCommit[] actionBarBindings; + AetheriaRuntimeFactionRelationshipCommit[] factionRelationships; + uint generationSeed; + + if (fields >= 12) + { + var legacyCurrentZoneEntityIndex = ReadFieldInt32(ref reader, fields, 5, -1); + discoveredZoneIndices = ReadFieldArray(ref reader, fields, 6); + zones = ReadFieldArray(ref reader, fields, 7); + actionBarBindings = ReadFieldArray(ref reader, fields, 8); + factionRelationships = ReadFieldArray(ref reader, fields, 9); + generationSeed = ReadFieldUInt32(ref reader, fields, 10, 0); + currentEntityKey = ReadFieldString(ref reader, fields, 11, ""); + if (string.IsNullOrWhiteSpace(currentEntityKey)) + { + currentEntityKey = LegacyCurrentEntityKey(runId, currentZoneIndex, legacyCurrentZoneEntityIndex); + } + } + else + { + discoveredZoneIndices = ReadFieldArray(ref reader, fields, 5); + zones = ReadFieldArray(ref reader, fields, 6); + actionBarBindings = ReadFieldArray(ref reader, fields, 7); + factionRelationships = ReadFieldArray(ref reader, fields, 8); + generationSeed = ReadFieldUInt32(ref reader, fields, 9, 0); + currentEntityKey = ReadFieldString(ref reader, fields, 10, ""); + } + + return new AetheriaRuntimeRunCheckpointCommit + { + RunId = runId, + IsTutorial = isTutorial, + EntranceZoneIndex = entranceZoneIndex, + ExitZoneIndex = exitZoneIndex, + CurrentZoneIndex = currentZoneIndex, + DiscoveredZoneIndices = discoveredZoneIndices, + Zones = zones, + ActionBarBindings = actionBarBindings, + FactionRelationships = factionRelationships, + GenerationSeed = generationSeed, + CurrentEntityKey = currentEntityKey + }; + } + private static string[] ReadSchemaCatalog(ref MessagePackReader reader) { var count = reader.ReadArrayHeader(); @@ -179,6 +292,90 @@ namespace GameCult.Aetheria.State.Unity writer.Write(payload); } + private static string ReadFieldString(ref MessagePackReader reader, int fields, int index, string fallback) + { + return index >= fields ? fallback : reader.ReadString() ?? fallback; + } + + private static int ReadFieldInt32(ref MessagePackReader reader, int fields, int index, int fallback) + { + if (index >= fields) + { + return fallback; + } + + if (reader.NextMessagePackType == MessagePackType.Nil) + { + reader.ReadNil(); + return fallback; + } + + return reader.ReadInt32(); + } + + private static uint ReadFieldUInt32(ref MessagePackReader reader, int fields, int index, uint fallback) + { + if (index >= fields) + { + return fallback; + } + + if (reader.NextMessagePackType == MessagePackType.Nil) + { + reader.ReadNil(); + return fallback; + } + + return reader.ReadUInt32(); + } + + private static bool ReadFieldBool(ref MessagePackReader reader, int fields, int index, bool fallback) + { + if (index >= fields) + { + return fallback; + } + + if (reader.NextMessagePackType == MessagePackType.Nil) + { + reader.ReadNil(); + return fallback; + } + + return reader.ReadBoolean(); + } + + private static T[] ReadFieldArray(ref MessagePackReader reader, int fields, int index) + { + if (index >= fields) + { + return Array.Empty(); + } + + if (reader.NextMessagePackType == MessagePackType.Nil) + { + reader.ReadNil(); + return Array.Empty(); + } + + return MessagePackSerializer.Deserialize(ReadRawBytes(ref reader)) ?? Array.Empty(); + } + + private static byte[] ReadRawBytes(ref MessagePackReader reader) + { + return reader.ReadRaw().ToArray(); + } + + private static string LegacyCurrentEntityKey(string runId, int zoneIndex, int entityIndex) + { + if (string.IsNullOrWhiteSpace(runId) || zoneIndex < 0 || entityIndex < 0) + { + return ""; + } + + return $"global:aetheria.run_state.{runId}.zone.{zoneIndex}.entity.{entityIndex}.v1"; + } + private static string ContentHash(byte[] payload) { using var sha256 = SHA256.Create(); diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateCommitDocument.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateCommitDocument.cs index fb84faafa..fa4c9e135 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateCommitDocument.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateCommitDocument.cs @@ -231,24 +231,21 @@ namespace GameCult.Aetheria.State.Unity public int CurrentZoneIndex { get; set; } = -1; [Key(5)] - public int CurrentZoneEntityIndex { get; set; } = -1; - - [Key(6)] public IReadOnlyList DiscoveredZoneIndices { get; set; } = Array.Empty(); - [Key(7)] + [Key(6)] public IReadOnlyList Zones { get; set; } = Array.Empty(); - [Key(8)] + [Key(7)] public IReadOnlyList ActionBarBindings { get; set; } = Array.Empty(); - [Key(9)] + [Key(8)] public IReadOnlyList FactionRelationships { get; set; } = Array.Empty(); - [Key(10)] + [Key(9)] public uint GenerationSeed { get; set; } - [Key(11)] + [Key(10)] public string CurrentEntityKey { get; set; } = ""; } diff --git a/docs/aetheria-perfect-machine-map.md b/docs/aetheria-perfect-machine-map.md index fe6167e6a..12c7f0139 100644 --- a/docs/aetheria-perfect-machine-map.md +++ b/docs/aetheria-perfect-machine-map.md @@ -997,7 +997,9 @@ First Aetheria surfaces to publish: entity identity instead of reconstructing the player ship from an integer slot. Canonical run state and package runtime snapshots no longer keep the old current-entity slot field; the package reader only synthesizes the key - from older stored runs when that compatibility seam is encountered. + from older stored runs, and the pending commit reader only synthesizes it + from older queued run checkpoints, when those compatibility seams are + encountered. - Done: project run checkpoint entity snapshots from the flattened live entity graph, not only `Zone.Entities`, so docked child ships can receive typed entity records and stable child/docking references instead of