diff --git a/.voidbot/state/aetheria.cc b/.voidbot/state/aetheria.cc index 76797bcee..cf6dc536f 100644 Binary files a/.voidbot/state/aetheria.cc and b/.voidbot/state/aetheria.cc differ diff --git a/Aetheria.State.Unity.Smoke/Program.cs b/Aetheria.State.Unity.Smoke/Program.cs index 38b9b0bd7..be0360b0e 100644 --- a/Aetheria.State.Unity.Smoke/Program.cs +++ b/Aetheria.State.Unity.Smoke/Program.cs @@ -2,6 +2,7 @@ using Aetheria.State; using Aetheria.State.Unity; using GameCult.Aetheria.State.Unity; using GameCult.Eve.Surface; +using MessagePack; var root = args.Length > 0 ? Path.GetFullPath(args[0]) : Directory.GetCurrentDirectory(); var statePath = args.Length > 1 @@ -650,6 +651,7 @@ try { throw new InvalidOperationException("Runtime state commit log did not read the queued player settings command."); } + AssertPendingCultCacheEnvelope(commit.Path, AetheriaRuntimeStateCommitDocument.SchemaId); var typedNodeApplied = false; await using (var commitNode = await AetheriaStateNode.OpenAsync(commitSmokeStatePath, "aetheria-unity-runtime-commit-smoke")) @@ -917,6 +919,7 @@ try { throw new InvalidOperationException("Runtime Eve command log did not preserve typed command envelopes separately from state commits."); } + AssertPendingCultCacheEnvelope(eveCommand.Path, AetheriaRuntimeEveCommandDocument.SchemaId); } finally { @@ -936,3 +939,54 @@ Console.WriteLine($"Eve surface: {surface.Surface.Id}"); Console.WriteLine($"Package Eve surfaces: {packageSurfaces.Count}"); Console.WriteLine("Runtime state commit log smoke: settings, loadouts, action-bar bindings, faction relationships, and run zone/entity snapshots queued, applied, and cleared"); Console.WriteLine("Runtime Eve command log smoke: surface command queued separately from state commits"); + +static void AssertPendingCultCacheEnvelope(string path, string expectedSchemaId) +{ + 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."); + } + + var formatVersion = reader.ReadString(); + if (formatVersion != "cultcache.store.v1") + { + throw new InvalidOperationException($"Pending file {path} has format '{formatVersion}', not cultcache.store.v1."); + } + + var catalogCount = reader.ReadArrayHeader(); + var catalogHasSchema = false; + for (var index = 0; index < catalogCount; index++) + { + var catalogFieldCount = reader.ReadArrayHeader(); + var schemaId = catalogFieldCount > 0 ? reader.ReadString() : ""; + catalogHasSchema |= schemaId == expectedSchemaId; + for (var field = 1; field < catalogFieldCount; field++) + { + reader.Skip(); + } + } + + var recordCount = reader.ReadArrayHeader(); + if (recordCount != 1) + { + throw new InvalidOperationException($"Pending file {path} should contain one CultCache record, found {recordCount}."); + } + + var recordFieldCount = reader.ReadArrayHeader(); + if (recordFieldCount < 4) + { + throw new InvalidOperationException($"Pending file {path} has an incomplete CultCache record."); + } + + var key = reader.ReadString(); + var recordSchemaId = reader.ReadString(); + reader.Skip(); + var payload = reader.ReadBytes(); + var payloadLength = payload?.Length ?? 0; + if (!catalogHasSchema || recordSchemaId != expectedSchemaId || string.IsNullOrWhiteSpace(key) || payloadLength == 0) + { + throw new InvalidOperationException($"Pending file {path} does not publish a typed CultCache record for {expectedSchemaId}."); + } +} diff --git a/Aetheria.State.Unity/Aetheria.State.Unity.csproj b/Aetheria.State.Unity/Aetheria.State.Unity.csproj index e773ecc35..645c8928e 100644 --- a/Aetheria.State.Unity/Aetheria.State.Unity.csproj +++ b/Aetheria.State.Unity/Aetheria.State.Unity.csproj @@ -14,6 +14,7 @@ + diff --git a/Aetheria.State/Aetheria.State.csproj b/Aetheria.State/Aetheria.State.csproj index b6b8a8178..632998c04 100644 --- a/Aetheria.State/Aetheria.State.csproj +++ b/Aetheria.State/Aetheria.State.csproj @@ -10,6 +10,7 @@ + diff --git a/Aetheria.State/AetheriaEveCommandBridge.cs b/Aetheria.State/AetheriaEveCommandBridge.cs index 958a91852..cc199a2f0 100644 --- a/Aetheria.State/AetheriaEveCommandBridge.cs +++ b/Aetheria.State/AetheriaEveCommandBridge.cs @@ -1,6 +1,5 @@ using Aetheria.State.Documents; using GameCult.Aetheria.State.Unity; -using MessagePack; namespace Aetheria.State; @@ -82,7 +81,7 @@ public static class AetheriaEveCommandBridge private static AetheriaRuntimeEveCommandDocument ReadCommand(string path) { - return MessagePackSerializer.Deserialize(File.ReadAllBytes(path)); + return AetheriaRuntimePendingCultCacheStore.ReadEveCommand(path); } private static string Validate(AetheriaRuntimeEveCommandDocument command) diff --git a/Aetheria.State/AetheriaRuntimeCommitLogApplier.cs b/Aetheria.State/AetheriaRuntimeCommitLogApplier.cs index 18f317221..06121a217 100644 --- a/Aetheria.State/AetheriaRuntimeCommitLogApplier.cs +++ b/Aetheria.State/AetheriaRuntimeCommitLogApplier.cs @@ -6,7 +6,6 @@ using System.Threading.Tasks; using Aetheria.State.Documents; using GameCult.Aetheria.State.Unity; using GameCult.Caching; -using MessagePack; namespace Aetheria.State; @@ -75,7 +74,7 @@ public static class AetheriaRuntimeCommitLogApplier private static AetheriaRuntimeStateCommitDocument ReadCommand(string path) { - var command = MessagePackSerializer.Deserialize(File.ReadAllBytes(path)); + var command = AetheriaRuntimePendingCultCacheStore.ReadStateCommit(path); if (!string.Equals(command.Schema, AetheriaRuntimeStateCommitDocument.SchemaId, StringComparison.Ordinal)) throw new InvalidDataException($"Unexpected Aetheria runtime commit schema '{command.Schema}' in {path}."); return command; diff --git a/Aetheria.State/README.md b/Aetheria.State/README.md index b112f4eb9..8a967668d 100644 --- a/Aetheria.State/README.md +++ b/Aetheria.State/README.md @@ -200,7 +200,10 @@ Use it after import when `GameData/aetheria-world.cc` changes. facade and proves read-only Unity-facing catalog access without the legacy catalog reader. It also proves runtime commit envelopes for settings and run zone/entity snapshots can be queued, applied through `Aetheria.State`, and -cleared. +cleared. Pending runtime state and Eve command queue files are CultCache-shaped +`.cc` records with embedded schema catalogs; their current record payload codec +is still temporary package code until Unity consumes generated CultCache +serializers directly. `Economy.Server` opens the CultMesh state host, applies pending runtime commits and pending Eve surface commands on startup, and polls for new pending work diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeEveCommandLog.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeEveCommandLog.cs index c9c61dff6..1c67697e7 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeEveCommandLog.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeEveCommandLog.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; using GameCult.Eve.Surface; -using MessagePack; #nullable enable @@ -35,8 +34,6 @@ namespace GameCult.Aetheria.State.Unity var finalPath = Path.Combine( pendingDirectory, $"{issuedAtUtc.Replace(':', '-')}.{StableToken(request.SurfaceId)}.{StableToken(request.Command)}.{commandId}.cc"); - var tempPath = finalPath + ".tmp"; - var document = new AetheriaRuntimeEveCommandDocument { Schema = CommandSchema, @@ -49,10 +46,7 @@ namespace GameCult.Aetheria.State.Unity Payload = CopyPayload(request.Payload) }; - File.WriteAllBytes(tempPath, MessagePackSerializer.Serialize(document)); - if (File.Exists(finalPath)) - File.Delete(finalPath); - File.Move(tempPath, finalPath); + AetheriaRuntimePendingCultCacheStore.WriteEveCommand(finalPath, document); return new AetheriaRuntimeEveCommandEnvelope( document.Schema, @@ -79,9 +73,14 @@ namespace GameCult.Aetheria.State.Unity .ToArray(); } + public static AetheriaRuntimeEveCommandDocument ReadDocument(string path) + { + return AetheriaRuntimePendingCultCacheStore.ReadEveCommand(path); + } + private static AetheriaRuntimeEveCommandEnvelope ReadEnvelope(string path) { - var document = MessagePackSerializer.Deserialize(File.ReadAllBytes(path)); + var document = ReadDocument(path); return new AetheriaRuntimeEveCommandEnvelope( document.Schema ?? "", diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimePendingCultCacheStore.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimePendingCultCacheStore.cs new file mode 100644 index 000000000..5fe0e0667 --- /dev/null +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimePendingCultCacheStore.cs @@ -0,0 +1,206 @@ +using System; +using System.Buffers; +using System.IO; +using System.Linq; +using System.Security.Cryptography; +using MessagePack; + +#nullable enable + +namespace GameCult.Aetheria.State.Unity +{ + internal static class AetheriaRuntimePendingCultCacheStore + { + private const string FormatVersion = "cultcache.store.v1"; + private const int StoreSnapshotFieldCount = 3; + private const int SchemaCatalogEntryFieldCount = 7; + private const int SchemaCatalogMemberFieldCount = 8; + private const int PersistedRecordFieldCount = 4; + + public static void WriteStateCommit(string path, AetheriaRuntimeStateCommitDocument document) + { + WriteDocument( + path, + $"pending:aetheria.runtime_commit.{document.CommandId}.v1", + AetheriaRuntimeStateCommitDocument.SchemaId, + "aetheria.runtime_commit", + "aetheria.runtime_commit.v1", + document.CreatedAtUtc, + MessagePackSerializer.Serialize(document)); + } + + public static AetheriaRuntimeStateCommitDocument ReadStateCommit(string path) + { + return MessagePackSerializer.Deserialize( + ReadDocumentPayload(path, AetheriaRuntimeStateCommitDocument.SchemaId)); + } + + public static void WriteEveCommand(string path, AetheriaRuntimeEveCommandDocument document) + { + WriteDocument( + path, + $"pending:gamecult.eve.command.{document.CommandId}.v1", + AetheriaRuntimeEveCommandDocument.SchemaId, + "gamecult.eve.command", + "gamecult.eve.command.v1", + document.IssuedAtUtc, + MessagePackSerializer.Serialize(document)); + } + + public static AetheriaRuntimeEveCommandDocument ReadEveCommand(string path) + { + return MessagePackSerializer.Deserialize( + ReadDocumentPayload(path, AetheriaRuntimeEveCommandDocument.SchemaId)); + } + + private static void WriteDocument( + string path, + string key, + string schemaId, + string schemaName, + string schemaVersion, + string storedAt, + byte[] payload) + { + var buffer = new ArrayBufferWriter(); + var writer = new MessagePackWriter(buffer); + + writer.WriteArrayHeader(StoreSnapshotFieldCount); + writer.Write(FormatVersion); + writer.WriteArrayHeader(1); + WriteSchemaCatalogEntry(ref writer, schemaId, schemaName, schemaVersion, payload); + writer.WriteArrayHeader(1); + WritePersistedRecord(ref writer, key, schemaId, storedAt, payload); + writer.Flush(); + + WriteFileAtomically(path, buffer.WrittenSpan.ToArray()); + } + + private static byte[] ReadDocumentPayload(string path, string expectedSchemaId) + { + var reader = new MessagePackReader(File.ReadAllBytes(path)); + var fieldCount = reader.ReadArrayHeader(); + if (fieldCount < StoreSnapshotFieldCount) + { + throw new InvalidDataException($"Pending CultCache document '{path}' is missing store fields."); + } + + var formatVersion = reader.ReadString() ?? ""; + if (!formatVersion.StartsWith("cultcache.store.v1", StringComparison.Ordinal)) + { + throw new InvalidDataException($"Pending document '{path}' is not a CultCache store."); + } + + var schemaIds = ReadSchemaCatalog(ref reader); + var recordCount = reader.ReadArrayHeader(); + if (recordCount != 1) + { + throw new InvalidDataException($"Pending CultCache document '{path}' must contain exactly one record."); + } + + var recordFieldCount = reader.ReadArrayHeader(); + if (recordFieldCount < PersistedRecordFieldCount) + { + throw new InvalidDataException($"Pending CultCache record '{path}' is missing record fields."); + } + + reader.Skip(); // key + var schemaId = reader.ReadString() ?? ""; + reader.Skip(); // storedAt + var payload = reader.ReadBytes()?.ToArray() ?? Array.Empty(); + for (var index = PersistedRecordFieldCount; index < recordFieldCount; index++) + { + reader.Skip(); + } + + for (var index = StoreSnapshotFieldCount; index < fieldCount; index++) + { + reader.Skip(); + } + + if (!string.Equals(schemaId, expectedSchemaId, StringComparison.Ordinal)) + { + throw new InvalidDataException($"Pending document '{path}' has schema '{schemaId}', expected '{expectedSchemaId}'."); + } + + if (!schemaIds.Contains(schemaId, StringComparer.Ordinal)) + { + throw new InvalidDataException($"Pending document '{path}' does not publish schema '{schemaId}' in its CultCache catalog."); + } + + return payload; + } + + private static string[] ReadSchemaCatalog(ref MessagePackReader reader) + { + var count = reader.ReadArrayHeader(); + var schemaIds = new string[count]; + for (var index = 0; index < count; index++) + { + var fieldCount = reader.ReadArrayHeader(); + schemaIds[index] = fieldCount > 0 ? reader.ReadString() ?? "" : ""; + for (var field = 1; field < fieldCount; field++) + { + reader.Skip(); + } + } + + return schemaIds; + } + + private static void WriteSchemaCatalogEntry( + ref MessagePackWriter writer, + string schemaId, + string schemaName, + string schemaVersion, + byte[] payload) + { + writer.WriteArrayHeader(SchemaCatalogEntryFieldCount); + writer.Write(schemaId); + writer.Write(schemaName); + writer.Write(schemaVersion); + writer.Write(ContentHash(payload)); + writer.Write(""); + writer.WriteArrayHeader(0); + writer.WriteArrayHeader(0); + } + + private static void WritePersistedRecord( + ref MessagePackWriter writer, + string key, + string schemaId, + string storedAt, + byte[] payload) + { + writer.WriteArrayHeader(PersistedRecordFieldCount); + writer.Write(key); + writer.Write(schemaId); + writer.Write(storedAt ?? ""); + writer.Write(payload); + } + + private static string ContentHash(byte[] payload) + { + using var sha256 = SHA256.Create(); + return BitConverter.ToString(sha256.ComputeHash(payload)).Replace("-", "").ToLowerInvariant(); + } + + private static void WriteFileAtomically(string path, byte[] payload) + { + var directory = Path.GetDirectoryName(path); + if (!string.IsNullOrWhiteSpace(directory)) + { + Directory.CreateDirectory(directory); + } + + var tempPath = path + ".tmp"; + File.WriteAllBytes(tempPath, payload); + if (File.Exists(path)) + { + File.Delete(path); + } + + File.Move(tempPath, path); + } + } +} diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateCommitLog.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateCommitLog.cs index 654f6dfcc..2204d4f85 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateCommitLog.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateCommitLog.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; -using MessagePack; #nullable enable @@ -57,6 +56,11 @@ namespace GameCult.Aetheria.State.Unity .ToArray(); } + public static AetheriaRuntimeStateCommitDocument ReadDocument(string path) + { + return AetheriaRuntimePendingCultCacheStore.ReadStateCommit(path); + } + private static AetheriaRuntimeCommitEnvelope WriteCommit( string stateFilePath, AetheriaRuntimeCommitKind kind, @@ -70,8 +74,6 @@ namespace GameCult.Aetheria.State.Unity var finalPath = Path.Combine( pendingDirectory, $"{createdAtUtc.Replace(':', '-')}.{KindToken(kind)}.{commandId}.cc"); - var tempPath = finalPath + ".tmp"; - var document = new AetheriaRuntimeStateCommitDocument { Schema = CommitSchema, @@ -81,17 +83,14 @@ namespace GameCult.Aetheria.State.Unity }; attachPayload(document); - File.WriteAllBytes(tempPath, MessagePackSerializer.Serialize(document)); - if (File.Exists(finalPath)) - File.Delete(finalPath); - File.Move(tempPath, finalPath); + AetheriaRuntimePendingCultCacheStore.WriteStateCommit(finalPath, document); return new AetheriaRuntimeCommitEnvelope(CommitSchema, kind, commandId, createdAtUtc, finalPath); } private static AetheriaRuntimeCommitEnvelope ReadEnvelope(string path) { - var document = MessagePackSerializer.Deserialize(File.ReadAllBytes(path)); + var document = ReadDocument(path); return new AetheriaRuntimeCommitEnvelope( document.Schema ?? "", ParseKind(document.Kind ?? ""), diff --git a/docs/aetheria-perfect-machine-map.md b/docs/aetheria-perfect-machine-map.md index 6173735e7..a737397d7 100644 --- a/docs/aetheria-perfect-machine-map.md +++ b/docs/aetheria-perfect-machine-map.md @@ -47,8 +47,9 @@ legacy catalog cache, and legacy UI paths should be migration-only or deleted. legacy `RuntimePlayerSettings` runtime object is no longer named or decorated as a MessagePack persistence shape. `Economy.Server` now drains `aetheria-world.cc.pending` - into canonical typed state and drains typed `aetheria-world.cc.eve.pending` - command documents through the provider-owned Eve command bridge on startup and on a daemon polling loop + CultCache-record queue files into canonical typed state and drains typed + `aetheria-world.cc.eve.pending` CultCache-record command documents through + the provider-owned Eve command bridge on startup and on a daemon polling loop while hosting the CultMesh state node. `Aetheria.State.ApplyPending` remains a bounded local operator applicator for both pending lanes. - Shared item domain state is still partly built around `RuntimeItemDefinitionReference`, @@ -60,8 +61,9 @@ legacy catalog cache, and legacy UI paths should be migration-only or deleted. `StrategyGameManager.csbak` backup file and unused Unity asset MessagePack formatter classes have also been deleted, so the remaining direct `MessagePackSerializer` calls are no longer present in live gameplay source; - the package-owned pending command lanes still use MessagePack as the current - CultCache binary transport for typed `.cc` command envelopes. + the package-owned pending lanes now write CultCache-shaped `.cc` queue records. + Their current typed record payload codec still uses MessagePack until Unity + imports/generated real CultCache serializers from CultLib. - Local legacy catalog data remains in `GameData/AetherDB.msgpack` and `GameData/NameFile/*.msgpack` as migration inputs only. Unity gameplay no longer opens those MessagePack files at runtime. The old @@ -1228,8 +1230,11 @@ First Aetheria surfaces to publish: `AetheriaEveCommandBridge`; renderer/runtime command files are queued through `AetheriaRuntimeEveCommandLog`, while the provider bridge only validates, applies, reports, and deletes accounted commands. - - Done: replace the `.eve.pending` command lane's private raw MessagePack - array protocol with shared `AetheriaRuntimeEveCommandDocument` files. + - Done: replace the pending lane private raw MessagePack payload files with + CultCache-shaped `.cc` queue records for both runtime state commits and Eve + commands. `AetheriaRuntimePendingCultCacheStore` owns the temporary + envelope writer/reader until the Unity package can use generated CultCache + serializers directly. - Extend the command bridge beyond refresh commands as gameplay/editor Eve surfaces acquire provider-owned handlers. - Wire the presenter into a Unity scene/prefab for the first runtime surface @@ -1395,9 +1400,12 @@ First Aetheria surfaces to publish: can apply them into canonical typed settings/run/zone/entity state, and commands are cleared after application. It also proves renderer-emitted Eve commands are queued as typed command envelopes separately from state commits. -- Current Unity batchmode compile probes are blocked while another Unity editor - instance has `E:/Projects/Aetheria` open; typed state builds and smokes remain - the available verification until the editor lock is released. + The smoke now inspects pending queue bytes directly and verifies each pending + file is a CultCache store snapshot with a schema catalog and exactly one + typed record. +- Unity batchmode compile with Editor `6000.4.2f1` is currently available and + should be rerun after package/runtime C# edits; Unity may leave generated + package metadata and project files that must be cleaned before commit. - Direct `dotnet build .\Assembly-CSharp.csproj --no-restore` is not currently a valid gameplay compile substitute: the generated Unity project references a missing root-level `GameCult.Aetheria.State.Unity.csproj` instead of the live