diff --git a/.voidbot/state/aetheria.cc b/.voidbot/state/aetheria.cc index e5d0be56a..c9ef6df34 100644 Binary files a/.voidbot/state/aetheria.cc and b/.voidbot/state/aetheria.cc differ diff --git a/Aetheria.State/Aetheria.State.csproj b/Aetheria.State/Aetheria.State.csproj index 103d58879..1f5cca75b 100644 --- a/Aetheria.State/Aetheria.State.csproj +++ b/Aetheria.State/Aetheria.State.csproj @@ -8,6 +8,7 @@ + diff --git a/Aetheria.State/AetheriaRuntimeCommitLogApplier.cs b/Aetheria.State/AetheriaRuntimeCommitLogApplier.cs index 35b45e10d..e334de14e 100644 --- a/Aetheria.State/AetheriaRuntimeCommitLogApplier.cs +++ b/Aetheria.State/AetheriaRuntimeCommitLogApplier.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Threading.Tasks; using Aetheria.State.Documents; +using GameCult.Aetheria.State.Unity; using GameCult.Caching; using MessagePack; @@ -19,8 +20,6 @@ public sealed class AetheriaRuntimeCommitApplyReport public static class AetheriaRuntimeCommitLogApplier { - private const string CommitSchema = "gamecult.aetheria.runtime_commit.v1"; - public static async Task ApplyPendingAsync( AetheriaStateNode node, bool deleteApplied = true) @@ -40,42 +39,24 @@ public static class AetheriaRuntimeCommitLogApplier switch (command.Kind) { case "player_settings": - await node.PutPlayerSettingsAsync(command.PlayerSettings ?? throw MissingPayload(command)).ConfigureAwait(false); + await node.PutPlayerSettingsAsync( + ToPlayerSettings(command.PlayerSettings ?? throw MissingPayload(command), command.CreatedAtUtc)) + .ConfigureAwait(false); report.AppliedPlayerSettings++; break; case "loadout_template": - var loadout = command.LoadoutTemplate ?? throw MissingPayload(command); + var loadout = ToLoadoutTemplate( + command.LoadoutTemplate ?? throw MissingPayload(command), + command.CreatedAtUtc); await node.PutLoadoutTemplateAsync(LoadoutKey(loadout.Name), loadout).ConfigureAwait(false); report.AppliedLoadoutTemplates++; break; case "run_checkpoint": - var run = command.RunState ?? throw MissingPayload(command); - var zoneKeys = new List(); - foreach (var zone in command.ZoneSnapshots) - { - var zoneKey = ZoneKey(run.RunId, zone.ZoneIndex); - var entityKeys = new List(); - foreach (var entity in zone.Entities) - { - var entityKey = EntityKey(run.RunId, zone.ZoneIndex, entity.EntityIndex); - entity.Snapshot.ChildEntityKeys = entity.Snapshot.ChildEntityKeys - .Select(key => key.StartsWith("pending:entity:", StringComparison.Ordinal) - && int.TryParse(key["pending:entity:".Length..], out var childIndex) - ? EntityKey(run.RunId, zone.ZoneIndex, childIndex).ToString() - : key) - .ToArray(); - await node.PutEntitySnapshotAsync(entityKey, entity.Snapshot).ConfigureAwait(false); - entityKeys.Add(entityKey.ToString()); - } - - zone.State.EntityKeys = entityKeys.ToArray(); - await node.PutZoneStateAsync(zoneKey, zone.State).ConfigureAwait(false); - zoneKeys.Add(zoneKey.ToString()); - } - - if (zoneKeys.Count > 0) - run.ZoneKeys = zoneKeys.ToArray(); - await node.PutRunStateAsync(RunKey(run.RunId), run).ConfigureAwait(false); + await ApplyRunCheckpointAsync( + node, + command.RunCheckpoint ?? throw MissingPayload(command), + command.CreatedAtUtc) + .ConfigureAwait(false); report.AppliedRunCheckpoints++; break; default: @@ -92,219 +73,125 @@ public static class AetheriaRuntimeCommitLogApplier return report; } - private static PendingCommand ReadCommand(string path) + private static AetheriaRuntimeStateCommitDocument ReadCommand(string path) { - var reader = new MessagePackReader(File.ReadAllBytes(path)); - var fields = reader.ReadArrayHeader(); - var schema = fields > 0 ? ReadString(ref reader) : ""; - if (!string.Equals(schema, CommitSchema, StringComparison.Ordinal)) - throw new InvalidDataException($"Unexpected Aetheria runtime commit schema '{schema}' in {path}."); - - var kind = fields > 1 ? ReadString(ref reader) : ""; - var commandId = fields > 2 ? ReadString(ref reader) : ""; - var createdAtUtc = fields > 3 ? ReadString(ref reader) : ""; - if (fields > 4) - { - var command = kind switch - { - "player_settings" => PendingCommand.ForPlayerSettings( - kind, - commandId, - createdAtUtc, - ReadPlayerSettings(ref reader, createdAtUtc), - path), - "loadout_template" => PendingCommand.ForLoadoutTemplate( - kind, - commandId, - createdAtUtc, - ReadLoadoutTemplate(ref reader, createdAtUtc), - path), - "run_checkpoint" => PendingCommand.ForRunState( - kind, - commandId, - createdAtUtc, - ReadRunState(ref reader, createdAtUtc, out var zoneSnapshots), - zoneSnapshots, - path), - _ => throw new InvalidDataException($"Unknown Aetheria runtime commit kind '{kind}' in {path}.") - }; - - for (var field = 5; field < fields; field++) - reader.Skip(); - - return command; - } - - return PendingCommand.Empty(kind, commandId, createdAtUtc, path); + var command = MessagePackSerializer.Deserialize(File.ReadAllBytes(path)); + if (!string.Equals(command.Schema, AetheriaRuntimeStateCommitDocument.SchemaId, StringComparison.Ordinal)) + throw new InvalidDataException($"Unexpected Aetheria runtime commit schema '{command.Schema}' in {path}."); + return command; } - private static AetheriaPlayerSettings ReadPlayerSettings(ref MessagePackReader reader, string updatedAtUtc) + private static AetheriaPlayerSettings ToPlayerSettings( + AetheriaRuntimePlayerSettingsCommit settings, + string updatedAtUtc) { - var fields = reader.ReadArrayHeader(); return new AetheriaPlayerSettings { - PlayerName = ReadFieldString(ref reader, fields, 0), - TutorialPassed = ReadFieldBool(ref reader, fields, 1), - StoryFileHashes = ReadStoryFileHashes(ref reader, fields, 2), + PlayerName = settings.PlayerName ?? "", + TutorialPassed = settings.TutorialPassed, + StoryFileHashes = (settings.StoryFileHashes ?? Array.Empty()) + .Select(hash => new AetheriaStoryFileHash + { + StoryPath = hash.StoryPath ?? "", + Hash = hash.Hash ?? "" + }) + .ToArray(), Gameplay = new AetheriaPlayerGameplaySettings { - TemperatureUnit = ReadFieldString(ref reader, fields, 3), - SignificantDigits = ReadFieldInt32(ref reader, fields, 4) + TemperatureUnit = settings.TemperatureUnit ?? "", + SignificantDigits = settings.SignificantDigits }, Graphics = new AetheriaPlayerGraphicsSettings { - NebulaQuality = ReadFieldString(ref reader, fields, 5), - ShowAsteroidsInMinimap = ReadFieldBool(ref reader, fields, 6) + NebulaQuality = settings.NebulaQuality ?? "", + ShowAsteroidsInMinimap = settings.ShowAsteroidsInMinimap }, Input = new AetheriaPlayerInputSettings { - BindingOverrides = ReadBindings(ref reader, fields, 7), - ActionBarInputs = ReadStringArray(ref reader, fields, 8) + BindingOverrides = (settings.BindingOverrides ?? Array.Empty()) + .Select(binding => new AetheriaInputBindingOverride + { + ActionName = binding.ActionName ?? "", + BindingIndex = binding.BindingIndex, + BindingPath = binding.BindingPath ?? "" + }) + .ToArray(), + ActionBarInputs = (settings.ActionBarInputs ?? Array.Empty()) + .Select(input => input ?? "") + .ToArray() }, LastUpdatedAtUtc = updatedAtUtc }; } - private static AetheriaLoadoutTemplate ReadLoadoutTemplate(ref MessagePackReader reader, string updatedAtUtc) + private static AetheriaLoadoutTemplate ToLoadoutTemplate( + AetheriaRuntimeLoadoutTemplateCommit loadout, + string updatedAtUtc) { - var fields = reader.ReadArrayHeader(); return new AetheriaLoadoutTemplate { - Name = ReadFieldString(ref reader, fields, 0), - OwnerPlayerKey = ReadFieldString(ref reader, fields, 1), - RootEntity = fields > 2 ? ReadEntityLoadout(ref reader) : new AetheriaEntityLoadout(), + Name = loadout.Name ?? "", + OwnerPlayerKey = loadout.OwnerPlayerKey ?? "", + RootEntity = ToEntityLoadout(loadout.RootEntity), CreatedAtUtc = updatedAtUtc, UpdatedAtUtc = updatedAtUtc }; } - private static AetheriaEntityLoadout ReadEntityLoadout(ref MessagePackReader reader) + private static AetheriaEntityLoadout ToEntityLoadout(AetheriaRuntimeEntityLoadoutCommit? entity) { - var fields = reader.ReadArrayHeader(); + entity ??= new AetheriaRuntimeEntityLoadoutCommit(); return new AetheriaEntityLoadout { - Name = ReadFieldString(ref reader, fields, 0), - Kind = ReadFieldString(ref reader, fields, 1), - FactionKey = ReferenceKey("aetheria.corporation", ReadFieldString(ref reader, fields, 2)), - Hull = fields > 3 ? ReadLoadoutItem(ref reader) : new AetheriaLoadoutItem(), - Equipment = ReadItemSlots(ref reader, fields, 4), - CargoBays = ReadItemSlots(ref reader, fields, 5), - DockingBays = ReadItemSlots(ref reader, fields, 6), - CargoContents = ReadCargoBays(ref reader, fields, 7), - DockingBayContents = ReadCargoBays(ref reader, fields, 8), - DockingBayAssignments = ReadIntArray(ref reader, fields, 9), - WeaponGroups = ReadIntArrayArray(ref reader, fields, 10), - Children = ReadChildren(ref reader, fields, 11) + Name = entity.Name ?? "", + Kind = entity.Kind ?? "", + FactionKey = ReferenceKey("aetheria.corporation", entity.CorporationLegacyId ?? ""), + Hull = ToLoadoutItem(entity.Hull), + Equipment = ToItemSlots(entity.Equipment), + CargoBays = ToItemSlots(entity.CargoBays), + DockingBays = ToItemSlots(entity.DockingBays), + CargoContents = ToCargoBays(entity.CargoContents), + DockingBayContents = ToCargoBays(entity.DockingBayContents), + DockingBayAssignments = ToIntArray(entity.DockingBayAssignments), + WeaponGroups = ToIntArrayArray(entity.WeaponGroups), + Children = (entity.Children ?? Array.Empty()) + .Select(ToEntityLoadout) + .ToArray() }; } - private static AetheriaLoadoutItem ReadLoadoutItem(ref MessagePackReader reader) + private static AetheriaLoadoutItem ToLoadoutItem(AetheriaRuntimeLoadoutItemCommit? item) { - var fields = reader.ReadArrayHeader(); + item ??= new AetheriaRuntimeLoadoutItemCommit(); return new AetheriaLoadoutItem { - ItemKey = ReferenceKey("aetheria.item_definition", ReadFieldString(ref reader, fields, 0)), - Quality = ReadFieldDouble(ref reader, fields, 1), - Durability = ReadFieldDouble(ref reader, fields, 2), - Quantity = ReadFieldInt32(ref reader, fields, 3) + ItemKey = ReferenceKey("aetheria.item_definition", item.ItemDefinitionLegacyId ?? ""), + Quality = item.Quality, + Durability = item.Durability, + Quantity = item.Quantity }; } - private static AetheriaRunState ReadRunState( - ref MessagePackReader reader, - string updatedAtUtc, - out AetheriaPendingZoneSnapshot[] zoneSnapshots) + private static AetheriaLoadoutItemSlot[] ToItemSlots( + IReadOnlyList? slots) { - var fields = reader.ReadArrayHeader(); - var run = new AetheriaRunState - { - RunId = ReadFieldString(ref reader, fields, 0), - IsTutorial = ReadFieldBool(ref reader, fields, 1), - EntranceZoneIndex = ReadFieldInt32(ref reader, fields, 2), - ExitZoneIndex = ReadFieldInt32(ref reader, fields, 3), - CurrentZoneIndex = ReadFieldInt32(ref reader, fields, 4), - CurrentZoneEntityIndex = ReadFieldInt32(ref reader, fields, 5), - DiscoveredZoneIndices = ReadIntArray(ref reader, fields, 6), - UpdatedAtUtc = updatedAtUtc - }; - zoneSnapshots = ReadZoneSnapshots(ref reader, fields, 7); - return run; - } - - private static AetheriaPendingZoneSnapshot[] ReadZoneSnapshots(ref MessagePackReader reader, int fields, int index) - { - if (index >= fields) return []; - var count = reader.ReadArrayHeader(); - var zones = new AetheriaPendingZoneSnapshot[count]; - for (var item = 0; item < count; item++) - { - var itemFields = reader.ReadArrayHeader(); - var zoneIndex = ReadFieldInt32(ref reader, itemFields, 0); - var state = new AetheriaZoneState + return (slots ?? Array.Empty()) + .Select(slot => new AetheriaLoadoutItemSlot { - Name = ReadFieldString(ref reader, itemFields, 1), - Position = new AetheriaVector2 + Position = new AetheriaGridCoord { - X = ReadFieldDouble(ref reader, itemFields, 2), - Y = ReadFieldDouble(ref reader, itemFields, 3) + X = slot.X, + Y = slot.Y }, - AdjacentZoneIndices = ReadIntArray(ref reader, itemFields, 4), - FactionIndices = ReadIntArray(ref reader, itemFields, 5), - OwnerFactionIndex = ReadFieldInt32(ref reader, itemFields, 6) - }; - zones[item] = new AetheriaPendingZoneSnapshot( - zoneIndex, - state, - ReadEntitySnapshots(ref reader, itemFields, 7)); - } - - return zones; + Item = ToLoadoutItem(slot.Item) + }) + .ToArray(); } - private static AetheriaPendingEntitySnapshot[] ReadEntitySnapshots(ref MessagePackReader reader, int fields, int index) + private static AetheriaEntityItemSlot[] ToEntityItemSlots( + IReadOnlyList? slots) { - if (index >= fields) return []; - var count = reader.ReadArrayHeader(); - var entities = new AetheriaPendingEntitySnapshot[count]; - for (var item = 0; item < count; item++) - { - var itemFields = reader.ReadArrayHeader(); - var entityIndex = ReadFieldInt32(ref reader, itemFields, 0); - var snapshot = new AetheriaEntitySnapshot - { - Name = ReadFieldString(ref reader, itemFields, 1), - Kind = ReadFieldString(ref reader, itemFields, 2), - Position = new AetheriaVector3 - { - X = ReadFieldDouble(ref reader, itemFields, 3), - Y = ReadFieldDouble(ref reader, itemFields, 4), - Z = ReadFieldDouble(ref reader, itemFields, 5) - }, - Direction = new AetheriaVector2 - { - X = ReadFieldDouble(ref reader, itemFields, 6), - Y = ReadFieldDouble(ref reader, itemFields, 7) - }, - FactionKey = ReferenceKey("aetheria.corporation", ReadFieldString(ref reader, itemFields, 8)), - HullItemKey = ReferenceKey("aetheria.item_definition", ReadFieldString(ref reader, itemFields, 9)), - Equipment = ReadEntityItemSlots(ref reader, itemFields, 10), - CargoBays = ReadEntityItemSlots(ref reader, itemFields, 11), - DockingBays = ReadEntityItemSlots(ref reader, itemFields, 12), - ChildEntityKeys = ReadIntArray(ref reader, itemFields, 13) - .Select(childIndex => $"pending:entity:{childIndex}") - .ToArray(), - WeaponGroups = ReadWeaponGroups(ref reader, itemFields, 14) - }; - entities[item] = new AetheriaPendingEntitySnapshot(entityIndex, snapshot); - } - - return entities; - } - - private static AetheriaEntityItemSlot[] ReadEntityItemSlots(ref MessagePackReader reader, int fields, int index) - { - var slots = ReadItemSlots(ref reader, fields, index); - return slots + return ToItemSlots(slots) .Select(slot => new AetheriaEntityItemSlot { Position = slot.Position, @@ -313,151 +200,115 @@ public static class AetheriaRuntimeCommitLogApplier .ToArray(); } - private static AetheriaWeaponGroupSnapshot[] ReadWeaponGroups(ref MessagePackReader reader, int fields, int index) + private static AetheriaCargoBayLoadout[] ToCargoBays( + IReadOnlyList? cargoBays) { - return ReadIntArrayArray(ref reader, fields, index) - .Select(group => new AetheriaWeaponGroupSnapshot { EquipmentIndices = group }) + return (cargoBays ?? Array.Empty()) + .Select(bay => new AetheriaCargoBayLoadout + { + Items = ToItemSlots(bay.Items) + }) .ToArray(); } - private static AetheriaStoryFileHash[] ReadStoryFileHashes(ref MessagePackReader reader, int fields, int index) + private static async Task ApplyRunCheckpointAsync( + AetheriaStateNode node, + AetheriaRuntimeRunCheckpointCommit checkpoint, + string updatedAtUtc) { - if (index >= fields) return []; - var count = reader.ReadArrayHeader(); - var hashes = new AetheriaStoryFileHash[count]; - for (var item = 0; item < count; item++) + var run = new AetheriaRunState { - var itemFields = reader.ReadArrayHeader(); - hashes[item] = new AetheriaStoryFileHash - { - StoryPath = ReadFieldString(ref reader, itemFields, 0), - Hash = ReadFieldString(ref reader, itemFields, 1) - }; - } + RunId = checkpoint.RunId ?? "", + IsTutorial = checkpoint.IsTutorial, + EntranceZoneIndex = checkpoint.EntranceZoneIndex, + ExitZoneIndex = checkpoint.ExitZoneIndex, + CurrentZoneIndex = checkpoint.CurrentZoneIndex, + CurrentZoneEntityIndex = checkpoint.CurrentZoneEntityIndex, + DiscoveredZoneIndices = ToIntArray(checkpoint.DiscoveredZoneIndices), + UpdatedAtUtc = updatedAtUtc + }; - return hashes; - } - - private static AetheriaInputBindingOverride[] ReadBindings(ref MessagePackReader reader, int fields, int index) - { - if (index >= fields) return []; - var count = reader.ReadArrayHeader(); - var bindings = new AetheriaInputBindingOverride[count]; - for (var item = 0; item < count; item++) + var zoneKeys = new List(); + foreach (var zone in checkpoint.Zones ?? Array.Empty()) { - var itemFields = reader.ReadArrayHeader(); - bindings[item] = new AetheriaInputBindingOverride + var zoneKey = ZoneKey(run.RunId, zone.ZoneIndex); + var entityKeys = new List(); + foreach (var entity in zone.Entities ?? Array.Empty()) { - ActionName = ReadFieldString(ref reader, itemFields, 0), - BindingIndex = ReadFieldInt32(ref reader, itemFields, 1), - BindingPath = ReadFieldString(ref reader, itemFields, 2) - }; - } + var entityKey = EntityKey(run.RunId, zone.ZoneIndex, entity.EntityIndex); + var snapshot = ToEntitySnapshot(entity, run.RunId, zone.ZoneIndex); + await node.PutEntitySnapshotAsync(entityKey, snapshot).ConfigureAwait(false); + entityKeys.Add(entityKey.ToString()); + } - return bindings; - } - - private static AetheriaLoadoutItemSlot[] ReadItemSlots(ref MessagePackReader reader, int fields, int index) - { - if (index >= fields) return []; - var count = reader.ReadArrayHeader(); - var slots = new AetheriaLoadoutItemSlot[count]; - for (var item = 0; item < count; item++) - { - var itemFields = reader.ReadArrayHeader(); - slots[item] = new AetheriaLoadoutItemSlot - { - Position = new AetheriaGridCoord + await node.PutZoneStateAsync( + zoneKey, + new AetheriaZoneState { - X = ReadFieldInt32(ref reader, itemFields, 0), - Y = ReadFieldInt32(ref reader, itemFields, 1) - }, - Item = itemFields > 2 ? ReadLoadoutItem(ref reader) : new AetheriaLoadoutItem() - }; + Name = zone.Name ?? "", + Position = new AetheriaVector2 + { + X = zone.PositionX, + Y = zone.PositionY + }, + AdjacentZoneIndices = ToIntArray(zone.AdjacentZoneIndices), + FactionIndices = ToIntArray(zone.FactionIndices), + OwnerFactionIndex = zone.OwnerFactionIndex, + EntityKeys = entityKeys.ToArray() + }) + .ConfigureAwait(false); + zoneKeys.Add(zoneKey.ToString()); } - return slots; + if (zoneKeys.Count > 0) + run.ZoneKeys = zoneKeys.ToArray(); + await node.PutRunStateAsync(RunKey(run.RunId), run).ConfigureAwait(false); } - private static AetheriaCargoBayLoadout[] ReadCargoBays(ref MessagePackReader reader, int fields, int index) + private static AetheriaEntitySnapshot ToEntitySnapshot( + AetheriaRuntimeEntitySnapshotCommit entity, + string runId, + int zoneIndex) { - if (index >= fields) return []; - var count = reader.ReadArrayHeader(); - var bays = new AetheriaCargoBayLoadout[count]; - for (var item = 0; item < count; item++) + return new AetheriaEntitySnapshot { - bays[item] = new AetheriaCargoBayLoadout + Name = entity.Name ?? "", + Kind = entity.Kind ?? "", + Position = new AetheriaVector3 { - Items = ReadItemSlots(ref reader, 1, 0) - }; - } - - return bays; + X = entity.PositionX, + Y = entity.PositionY, + Z = entity.PositionZ + }, + Direction = new AetheriaVector2 + { + X = entity.DirectionX, + Y = entity.DirectionY + }, + FactionKey = ReferenceKey("aetheria.corporation", entity.CorporationLegacyId ?? ""), + HullItemKey = ReferenceKey("aetheria.item_definition", entity.HullItemDefinitionLegacyId ?? ""), + Equipment = ToEntityItemSlots(entity.Equipment), + CargoBays = ToEntityItemSlots(entity.CargoBays), + DockingBays = ToEntityItemSlots(entity.DockingBays), + ChildEntityKeys = (entity.ChildEntityIndices ?? Array.Empty()) + .Select(childIndex => EntityKey(runId, zoneIndex, childIndex).ToString()) + .ToArray(), + WeaponGroups = ToIntArrayArray(entity.WeaponGroups) + .Select(group => new AetheriaWeaponGroupSnapshot { EquipmentIndices = group }) + .ToArray() + }; } - private static AetheriaEntityLoadout[] ReadChildren(ref MessagePackReader reader, int fields, int index) + private static int[] ToIntArray(IReadOnlyList? values) { - if (index >= fields) return []; - var count = reader.ReadArrayHeader(); - var children = new AetheriaEntityLoadout[count]; - for (var item = 0; item < count; item++) - children[item] = ReadEntityLoadout(ref reader); - return children; + return (values ?? Array.Empty()).ToArray(); } - private static int[][] ReadIntArrayArray(ref MessagePackReader reader, int fields, int index) + private static int[][] ToIntArrayArray(IReadOnlyList>? values) { - if (index >= fields) return []; - var count = reader.ReadArrayHeader(); - var arrays = new int[count][]; - for (var item = 0; item < count; item++) - arrays[item] = ReadIntArray(ref reader, 1, 0); - return arrays; - } - - private static string[] ReadStringArray(ref MessagePackReader reader, int fields, int index) - { - if (index >= fields) return []; - var count = reader.ReadArrayHeader(); - var values = new string[count]; - for (var item = 0; item < count; item++) - values[item] = ReadString(ref reader); - return values; - } - - private static int[] ReadIntArray(ref MessagePackReader reader, int fields, int index) - { - if (index >= fields) return []; - var count = reader.ReadArrayHeader(); - var values = new int[count]; - for (var item = 0; item < count; item++) - values[item] = reader.ReadInt32(); - return values; - } - - private static string ReadFieldString(ref MessagePackReader reader, int fields, int index) - { - return index >= fields ? "" : ReadString(ref reader); - } - - private static int ReadFieldInt32(ref MessagePackReader reader, int fields, int index) - { - return index >= fields ? 0 : reader.ReadInt32(); - } - - private static double ReadFieldDouble(ref MessagePackReader reader, int fields, int index) - { - return index >= fields ? 0 : reader.ReadDouble(); - } - - private static bool ReadFieldBool(ref MessagePackReader reader, int fields, int index) - { - return index < fields && reader.ReadBoolean(); - } - - private static string ReadString(ref MessagePackReader reader) - { - return reader.ReadString() ?? ""; + return (values ?? Array.Empty>()) + .Select(ToIntArray) + .ToArray(); } private static CultRecordKey LoadoutKey(string name) @@ -496,105 +347,8 @@ public static class AetheriaRuntimeCommitLogApplier return string.IsNullOrWhiteSpace(token) ? "unnamed" : token; } - private static InvalidDataException MissingPayload(PendingCommand command) + private static InvalidDataException MissingPayload(AetheriaRuntimeStateCommitDocument command) { return new InvalidDataException($"Aetheria runtime commit '{command.CommandId}' has no {command.Kind} payload."); } - - private readonly struct PendingCommand - { - private PendingCommand( - string kind, - string commandId, - string createdAtUtc, - string path, - AetheriaPlayerSettings? playerSettings, - AetheriaLoadoutTemplate? loadoutTemplate, - AetheriaRunState? runState, - AetheriaPendingZoneSnapshot[] zoneSnapshots) - { - Kind = kind; - CommandId = commandId; - CreatedAtUtc = createdAtUtc; - Path = path; - PlayerSettings = playerSettings; - LoadoutTemplate = loadoutTemplate; - RunState = runState; - ZoneSnapshots = zoneSnapshots; - } - - public static PendingCommand Empty(string kind, string commandId, string createdAtUtc, string path) - { - return new PendingCommand(kind, commandId, createdAtUtc, path, null, null, null, []); - } - - public static PendingCommand ForPlayerSettings( - string kind, - string commandId, - string createdAtUtc, - AetheriaPlayerSettings settings, - string path) - { - return new PendingCommand(kind, commandId, createdAtUtc, path, settings, null, null, []); - } - - public static PendingCommand ForLoadoutTemplate( - string kind, - string commandId, - string createdAtUtc, - AetheriaLoadoutTemplate loadout, - string path) - { - return new PendingCommand(kind, commandId, createdAtUtc, path, null, loadout, null, []); - } - - public static PendingCommand ForRunState( - string kind, - string commandId, - string createdAtUtc, - AetheriaRunState run, - AetheriaPendingZoneSnapshot[] zoneSnapshots, - string path) - { - return new PendingCommand(kind, commandId, createdAtUtc, path, null, null, run, zoneSnapshots); - } - - public string Kind { get; } - public string CommandId { get; } - public string CreatedAtUtc { get; } - public string Path { get; } - public AetheriaPlayerSettings? PlayerSettings { get; } - public AetheriaLoadoutTemplate? LoadoutTemplate { get; } - public AetheriaRunState? RunState { get; } - public AetheriaPendingZoneSnapshot[] ZoneSnapshots { get; } - } - - private readonly struct AetheriaPendingZoneSnapshot - { - public AetheriaPendingZoneSnapshot( - int zoneIndex, - AetheriaZoneState state, - AetheriaPendingEntitySnapshot[] entities) - { - ZoneIndex = zoneIndex; - State = state; - Entities = entities; - } - - public int ZoneIndex { get; } - public AetheriaZoneState State { get; } - public AetheriaPendingEntitySnapshot[] Entities { get; } - } - - private readonly struct AetheriaPendingEntitySnapshot - { - public AetheriaPendingEntitySnapshot(int entityIndex, AetheriaEntitySnapshot snapshot) - { - EntityIndex = entityIndex; - Snapshot = snapshot; - } - - public int EntityIndex { get; } - public AetheriaEntitySnapshot Snapshot { get; } - } } diff --git a/Aetheria.State/README.md b/Aetheria.State/README.md index 85433054a..7869b24a3 100644 --- a/Aetheria.State/README.md +++ b/Aetheria.State/README.md @@ -29,8 +29,8 @@ docking item slots, nested child-entity loadouts, and weapon groups. and input binding edits; menu and input-screen changes queue typed `.cc.pending` player-settings commits and are not portable state authority. Unity settings, loadout templates, and run checkpoints now queue typed -`.cc.pending` runtime commit envelopes beside -`GameData/aetheria-world.cc`. `Aetheria.State` owns applying those envelopes +`.cc.pending` runtime commit documents beside +`GameData/aetheria-world.cc`. `Aetheria.State` owns applying those documents into canonical typed state through `AetheriaRuntimeCommitLogApplier`; the Unity-side command log is command-only and cannot become durable truth by itself. diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateCommitDocument.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateCommitDocument.cs new file mode 100644 index 000000000..c38305b6f --- /dev/null +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateCommitDocument.cs @@ -0,0 +1,313 @@ +using System; +using System.Collections.Generic; +using MessagePack; + +#nullable enable + +namespace GameCult.Aetheria.State.Unity +{ + public enum AetheriaRuntimeCommitKind + { + PlayerSettings, + LoadoutTemplate, + RunCheckpoint + } + + public sealed class AetheriaRuntimeCommitEnvelope + { + public AetheriaRuntimeCommitEnvelope( + string schema, + AetheriaRuntimeCommitKind kind, + string commandId, + string createdAtUtc, + string path) + { + Schema = schema; + Kind = kind; + CommandId = commandId; + CreatedAtUtc = createdAtUtc; + Path = path; + } + + public string Schema { get; } + public AetheriaRuntimeCommitKind Kind { get; } + public string CommandId { get; } + public string CreatedAtUtc { get; } + public string Path { get; } + } + + [MessagePackObject] + public sealed class AetheriaRuntimeStateCommitDocument + { + public const string SchemaId = "gamecult.aetheria.runtime_commit.v1"; + + [Key(0)] + public string Schema { get; set; } = SchemaId; + + [Key(1)] + public string Kind { get; set; } = ""; + + [Key(2)] + public string CommandId { get; set; } = ""; + + [Key(3)] + public string CreatedAtUtc { get; set; } = ""; + + [Key(4)] + public AetheriaRuntimePlayerSettingsCommit? PlayerSettings { get; set; } + + [Key(5)] + public AetheriaRuntimeLoadoutTemplateCommit? LoadoutTemplate { get; set; } + + [Key(6)] + public AetheriaRuntimeRunCheckpointCommit? RunCheckpoint { get; set; } + } + + [MessagePackObject] + public sealed class AetheriaRuntimePlayerSettingsCommit + { + [Key(0)] + public string PlayerName { get; set; } = ""; + + [Key(1)] + public bool TutorialPassed { get; set; } + + [Key(2)] + public IReadOnlyList StoryFileHashes { get; set; } = Array.Empty(); + + [Key(3)] + public string TemperatureUnit { get; set; } = "Celsius"; + + [Key(4)] + public int SignificantDigits { get; set; } = 3; + + [Key(5)] + public string NebulaQuality { get; set; } = "Normal"; + + [Key(6)] + public bool ShowAsteroidsInMinimap { get; set; } + + [Key(7)] + public IReadOnlyList BindingOverrides { get; set; } = Array.Empty(); + + [Key(8)] + public IReadOnlyList ActionBarInputs { get; set; } = Array.Empty(); + } + + [MessagePackObject] + public sealed class AetheriaRuntimeStoryFileHashCommit + { + [Key(0)] + public string StoryPath { get; set; } = ""; + + [Key(1)] + public string Hash { get; set; } = ""; + } + + [MessagePackObject] + public sealed class AetheriaRuntimeInputBindingCommit + { + [Key(0)] + public string ActionName { get; set; } = ""; + + [Key(1)] + public int BindingIndex { get; set; } + + [Key(2)] + public string BindingPath { get; set; } = ""; + } + + [MessagePackObject] + public sealed class AetheriaRuntimeLoadoutTemplateCommit + { + [Key(0)] + public string Name { get; set; } = ""; + + [Key(1)] + public string OwnerPlayerKey { get; set; } = ""; + + [Key(2)] + public AetheriaRuntimeEntityLoadoutCommit RootEntity { get; set; } = new AetheriaRuntimeEntityLoadoutCommit(); + } + + [MessagePackObject] + public sealed class AetheriaRuntimeEntityLoadoutCommit + { + [Key(0)] + public string Name { get; set; } = ""; + + [Key(1)] + public string Kind { get; set; } = ""; + + [Key(2)] + public string CorporationLegacyId { get; set; } = ""; + + [Key(3)] + public AetheriaRuntimeLoadoutItemCommit Hull { get; set; } = new AetheriaRuntimeLoadoutItemCommit(); + + [Key(4)] + public IReadOnlyList Equipment { get; set; } = Array.Empty(); + + [Key(5)] + public IReadOnlyList CargoBays { get; set; } = Array.Empty(); + + [Key(6)] + public IReadOnlyList DockingBays { get; set; } = Array.Empty(); + + [Key(7)] + public IReadOnlyList CargoContents { get; set; } = Array.Empty(); + + [Key(8)] + public IReadOnlyList DockingBayContents { get; set; } = Array.Empty(); + + [Key(9)] + public IReadOnlyList DockingBayAssignments { get; set; } = Array.Empty(); + + [Key(10)] + public IReadOnlyList> WeaponGroups { get; set; } = Array.Empty>(); + + [Key(11)] + public IReadOnlyList Children { get; set; } = Array.Empty(); + } + + [MessagePackObject] + public sealed class AetheriaRuntimeLoadoutItemCommit + { + [Key(0)] + public string ItemDefinitionLegacyId { get; set; } = ""; + + [Key(1)] + public double Quality { get; set; } = 1.0; + + [Key(2)] + public double Durability { get; set; } = 1.0; + + [Key(3)] + public int Quantity { get; set; } = 1; + } + + [MessagePackObject] + public sealed class AetheriaRuntimeLoadoutItemSlotCommit + { + [Key(0)] + public int X { get; set; } + + [Key(1)] + public int Y { get; set; } + + [Key(2)] + public AetheriaRuntimeLoadoutItemCommit Item { get; set; } = new AetheriaRuntimeLoadoutItemCommit(); + } + + [MessagePackObject] + public sealed class AetheriaRuntimeCargoBayLoadoutCommit + { + [Key(0)] + public IReadOnlyList Items { get; set; } = Array.Empty(); + } + + [MessagePackObject] + public sealed class AetheriaRuntimeRunCheckpointCommit + { + [Key(0)] + public string RunId { get; set; } = "local"; + + [Key(1)] + public bool IsTutorial { get; set; } + + [Key(2)] + public int EntranceZoneIndex { get; set; } = -1; + + [Key(3)] + public int ExitZoneIndex { get; set; } = -1; + + [Key(4)] + 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)] + public IReadOnlyList Zones { get; set; } = Array.Empty(); + } + + [MessagePackObject] + public sealed class AetheriaRuntimeZoneSnapshotCommit + { + [Key(0)] + public int ZoneIndex { get; set; } = -1; + + [Key(1)] + public string Name { get; set; } = ""; + + [Key(2)] + public double PositionX { get; set; } + + [Key(3)] + public double PositionY { get; set; } + + [Key(4)] + public IReadOnlyList AdjacentZoneIndices { get; set; } = Array.Empty(); + + [Key(5)] + public IReadOnlyList FactionIndices { get; set; } = Array.Empty(); + + [Key(6)] + public int OwnerFactionIndex { get; set; } = -1; + + [Key(7)] + public IReadOnlyList Entities { get; set; } = Array.Empty(); + } + + [MessagePackObject] + public sealed class AetheriaRuntimeEntitySnapshotCommit + { + [Key(0)] + public int EntityIndex { get; set; } = -1; + + [Key(1)] + public string Name { get; set; } = ""; + + [Key(2)] + public string Kind { get; set; } = ""; + + [Key(3)] + public double PositionX { get; set; } + + [Key(4)] + public double PositionY { get; set; } + + [Key(5)] + public double PositionZ { get; set; } + + [Key(6)] + public double DirectionX { get; set; } + + [Key(7)] + public double DirectionY { get; set; } + + [Key(8)] + public string CorporationLegacyId { get; set; } = ""; + + [Key(9)] + public string HullItemDefinitionLegacyId { get; set; } = ""; + + [Key(10)] + public IReadOnlyList Equipment { get; set; } = Array.Empty(); + + [Key(11)] + public IReadOnlyList CargoBays { get; set; } = Array.Empty(); + + [Key(12)] + public IReadOnlyList DockingBays { get; set; } = Array.Empty(); + + [Key(13)] + public IReadOnlyList ChildEntityIndices { get; set; } = Array.Empty(); + + [Key(14)] + public IReadOnlyList> WeaponGroups { get; set; } = Array.Empty>(); + } +} diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateCommitLog.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateCommitLog.cs index ad844501f..654f6dfcc 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateCommitLog.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateCommitLog.cs @@ -1,5 +1,4 @@ using System; -using System.Buffers; using System.Collections.Generic; using System.IO; using System.Linq; @@ -9,153 +8,9 @@ using MessagePack; namespace GameCult.Aetheria.State.Unity { - public enum AetheriaRuntimeCommitKind - { - PlayerSettings, - LoadoutTemplate, - RunCheckpoint - } - - public sealed class AetheriaRuntimeCommitEnvelope - { - public AetheriaRuntimeCommitEnvelope( - string schema, - AetheriaRuntimeCommitKind kind, - string commandId, - string createdAtUtc, - string path) - { - Schema = schema; - Kind = kind; - CommandId = commandId; - CreatedAtUtc = createdAtUtc; - Path = path; - } - - public string Schema { get; } - public AetheriaRuntimeCommitKind Kind { get; } - public string CommandId { get; } - public string CreatedAtUtc { get; } - public string Path { get; } - } - - public sealed class AetheriaRuntimePlayerSettingsCommit - { - public string PlayerName { get; set; } = ""; - public bool TutorialPassed { get; set; } - public IReadOnlyList StoryFileHashes { get; set; } = Array.Empty(); - public string TemperatureUnit { get; set; } = "Celsius"; - public int SignificantDigits { get; set; } = 3; - public string NebulaQuality { get; set; } = "Normal"; - public bool ShowAsteroidsInMinimap { get; set; } - public IReadOnlyList BindingOverrides { get; set; } = Array.Empty(); - public IReadOnlyList ActionBarInputs { get; set; } = Array.Empty(); - } - - public sealed class AetheriaRuntimeStoryFileHashCommit - { - public string StoryPath { get; set; } = ""; - public string Hash { get; set; } = ""; - } - - public sealed class AetheriaRuntimeInputBindingCommit - { - public string ActionName { get; set; } = ""; - public int BindingIndex { get; set; } - public string BindingPath { get; set; } = ""; - } - - public sealed class AetheriaRuntimeLoadoutTemplateCommit - { - public string Name { get; set; } = ""; - public string OwnerPlayerKey { get; set; } = ""; - public AetheriaRuntimeEntityLoadoutCommit RootEntity { get; set; } = new AetheriaRuntimeEntityLoadoutCommit(); - } - - public sealed class AetheriaRuntimeEntityLoadoutCommit - { - public string Name { get; set; } = ""; - public string Kind { get; set; } = ""; - public string CorporationLegacyId { get; set; } = ""; - public AetheriaRuntimeLoadoutItemCommit Hull { get; set; } = new AetheriaRuntimeLoadoutItemCommit(); - public IReadOnlyList Equipment { get; set; } = Array.Empty(); - public IReadOnlyList CargoBays { get; set; } = Array.Empty(); - public IReadOnlyList DockingBays { get; set; } = Array.Empty(); - public IReadOnlyList CargoContents { get; set; } = Array.Empty(); - public IReadOnlyList DockingBayContents { get; set; } = Array.Empty(); - public IReadOnlyList DockingBayAssignments { get; set; } = Array.Empty(); - public IReadOnlyList> WeaponGroups { get; set; } = Array.Empty>(); - public IReadOnlyList Children { get; set; } = Array.Empty(); - } - - public sealed class AetheriaRuntimeLoadoutItemCommit - { - public string ItemDefinitionLegacyId { get; set; } = ""; - public double Quality { get; set; } = 1.0; - public double Durability { get; set; } = 1.0; - public int Quantity { get; set; } = 1; - } - - public sealed class AetheriaRuntimeLoadoutItemSlotCommit - { - public int X { get; set; } - public int Y { get; set; } - public AetheriaRuntimeLoadoutItemCommit Item { get; set; } = new AetheriaRuntimeLoadoutItemCommit(); - } - - public sealed class AetheriaRuntimeCargoBayLoadoutCommit - { - public IReadOnlyList Items { get; set; } = Array.Empty(); - } - - public sealed class AetheriaRuntimeRunCheckpointCommit - { - public string RunId { get; set; } = "local"; - public bool IsTutorial { get; set; } - public int EntranceZoneIndex { get; set; } = -1; - public int ExitZoneIndex { get; set; } = -1; - public int CurrentZoneIndex { get; set; } = -1; - public int CurrentZoneEntityIndex { get; set; } = -1; - public IReadOnlyList DiscoveredZoneIndices { get; set; } = Array.Empty(); - public IReadOnlyList Zones { get; set; } = Array.Empty(); - } - - public sealed class AetheriaRuntimeZoneSnapshotCommit - { - public int ZoneIndex { get; set; } = -1; - public string Name { get; set; } = ""; - public double PositionX { get; set; } - public double PositionY { get; set; } - public IReadOnlyList AdjacentZoneIndices { get; set; } = Array.Empty(); - public IReadOnlyList FactionIndices { get; set; } = Array.Empty(); - public int OwnerFactionIndex { get; set; } = -1; - public IReadOnlyList Entities { get; set; } = Array.Empty(); - } - - public sealed class AetheriaRuntimeEntitySnapshotCommit - { - public int EntityIndex { get; set; } = -1; - public string Name { get; set; } = ""; - public string Kind { get; set; } = ""; - public double PositionX { get; set; } - public double PositionY { get; set; } - public double PositionZ { get; set; } - public double DirectionX { get; set; } - public double DirectionY { get; set; } - public string CorporationLegacyId { get; set; } = ""; - public string HullItemDefinitionLegacyId { get; set; } = ""; - public IReadOnlyList Equipment { get; set; } = Array.Empty(); - public IReadOnlyList CargoBays { get; set; } = Array.Empty(); - public IReadOnlyList DockingBays { get; set; } = Array.Empty(); - public IReadOnlyList ChildEntityIndices { get; set; } = Array.Empty(); - public IReadOnlyList> WeaponGroups { get; set; } = Array.Empty>(); - } - public static class AetheriaRuntimeStateCommitLog { - public const string CommitSchema = "gamecult.aetheria.runtime_commit.v1"; - - private delegate void WritePayload(ref MessagePackWriter writer); + public const string CommitSchema = AetheriaRuntimeStateCommitDocument.SchemaId; public static string GetPendingDirectory(string stateFilePath) { @@ -170,7 +25,7 @@ namespace GameCult.Aetheria.State.Unity AetheriaRuntimePlayerSettingsCommit settings) { if (settings == null) throw new ArgumentNullException(nameof(settings)); - return WriteCommit(stateFilePath, AetheriaRuntimeCommitKind.PlayerSettings, (ref MessagePackWriter writer) => WritePlayerSettings(ref writer, settings)); + return WriteCommit(stateFilePath, AetheriaRuntimeCommitKind.PlayerSettings, document => document.PlayerSettings = settings); } public static AetheriaRuntimeCommitEnvelope QueueLoadoutTemplate( @@ -178,7 +33,7 @@ namespace GameCult.Aetheria.State.Unity AetheriaRuntimeLoadoutTemplateCommit loadout) { if (loadout == null) throw new ArgumentNullException(nameof(loadout)); - return WriteCommit(stateFilePath, AetheriaRuntimeCommitKind.LoadoutTemplate, (ref MessagePackWriter writer) => WriteLoadoutTemplate(ref writer, loadout)); + return WriteCommit(stateFilePath, AetheriaRuntimeCommitKind.LoadoutTemplate, document => document.LoadoutTemplate = loadout); } public static AetheriaRuntimeCommitEnvelope QueueRunCheckpoint( @@ -186,7 +41,7 @@ namespace GameCult.Aetheria.State.Unity AetheriaRuntimeRunCheckpointCommit run) { if (run == null) throw new ArgumentNullException(nameof(run)); - return WriteCommit(stateFilePath, AetheriaRuntimeCommitKind.RunCheckpoint, (ref MessagePackWriter writer) => WriteRunCheckpoint(ref writer, run)); + return WriteCommit(stateFilePath, AetheriaRuntimeCommitKind.RunCheckpoint, document => document.RunCheckpoint = run); } public static IReadOnlyList ReadPending(string stateFilePath) @@ -205,7 +60,7 @@ namespace GameCult.Aetheria.State.Unity private static AetheriaRuntimeCommitEnvelope WriteCommit( string stateFilePath, AetheriaRuntimeCommitKind kind, - WritePayload writePayload) + Action attachPayload) { var commandId = Guid.NewGuid().ToString("N"); var createdAtUtc = DateTime.UtcNow.ToString("O"); @@ -217,17 +72,16 @@ namespace GameCult.Aetheria.State.Unity $"{createdAtUtc.Replace(':', '-')}.{KindToken(kind)}.{commandId}.cc"); var tempPath = finalPath + ".tmp"; - var buffer = new ArrayBufferWriter(); - var writer = new MessagePackWriter(buffer); - writer.WriteArrayHeader(5); - writer.Write(CommitSchema); - writer.Write(KindToken(kind)); - writer.Write(commandId); - writer.Write(createdAtUtc); - writePayload(ref writer); - writer.Flush(); + var document = new AetheriaRuntimeStateCommitDocument + { + Schema = CommitSchema, + Kind = KindToken(kind), + CommandId = commandId, + CreatedAtUtc = createdAtUtc + }; + attachPayload(document); - File.WriteAllBytes(tempPath, buffer.WrittenSpan.ToArray()); + File.WriteAllBytes(tempPath, MessagePackSerializer.Serialize(document)); if (File.Exists(finalPath)) File.Delete(finalPath); File.Move(tempPath, finalPath); @@ -237,198 +91,13 @@ namespace GameCult.Aetheria.State.Unity private static AetheriaRuntimeCommitEnvelope ReadEnvelope(string path) { - var reader = new MessagePackReader(File.ReadAllBytes(path)); - var fields = reader.ReadArrayHeader(); - var schema = fields > 0 ? ReadString(ref reader) : ""; - var kind = fields > 1 ? ParseKind(ReadString(ref reader)) : AetheriaRuntimeCommitKind.RunCheckpoint; - var commandId = fields > 2 ? ReadString(ref reader) : ""; - var createdAtUtc = fields > 3 ? ReadString(ref reader) : ""; - for (var field = 4; field < fields; field++) - reader.Skip(); - - return new AetheriaRuntimeCommitEnvelope(schema, kind, commandId, createdAtUtc, path); - } - - private static void WritePlayerSettings(ref MessagePackWriter writer, AetheriaRuntimePlayerSettingsCommit settings) - { - writer.WriteArrayHeader(9); - writer.Write(settings.PlayerName ?? ""); - writer.Write(settings.TutorialPassed); - WriteStoryHashes(ref writer, settings.StoryFileHashes); - writer.Write(settings.TemperatureUnit ?? ""); - writer.Write(settings.SignificantDigits); - writer.Write(settings.NebulaQuality ?? ""); - writer.Write(settings.ShowAsteroidsInMinimap); - WriteBindings(ref writer, settings.BindingOverrides); - WriteStrings(ref writer, settings.ActionBarInputs); - } - - private static void WriteStoryHashes(ref MessagePackWriter writer, IReadOnlyList? hashes) - { - hashes ??= Array.Empty(); - writer.WriteArrayHeader(hashes.Count); - foreach (var hash in hashes) - { - writer.WriteArrayHeader(2); - writer.Write(hash.StoryPath ?? ""); - writer.Write(hash.Hash ?? ""); - } - } - - private static void WriteBindings(ref MessagePackWriter writer, IReadOnlyList? bindings) - { - bindings ??= Array.Empty(); - writer.WriteArrayHeader(bindings.Count); - foreach (var binding in bindings) - { - writer.WriteArrayHeader(3); - writer.Write(binding.ActionName ?? ""); - writer.Write(binding.BindingIndex); - writer.Write(binding.BindingPath ?? ""); - } - } - - private static void WriteLoadoutTemplate(ref MessagePackWriter writer, AetheriaRuntimeLoadoutTemplateCommit loadout) - { - writer.WriteArrayHeader(3); - writer.Write(loadout.Name ?? ""); - writer.Write(loadout.OwnerPlayerKey ?? ""); - WriteEntityLoadout(ref writer, loadout.RootEntity); - } - - private static void WriteEntityLoadout(ref MessagePackWriter writer, AetheriaRuntimeEntityLoadoutCommit entity) - { - writer.WriteArrayHeader(12); - writer.Write(entity.Name ?? ""); - writer.Write(entity.Kind ?? ""); - writer.Write(entity.CorporationLegacyId ?? ""); - WriteLoadoutItem(ref writer, entity.Hull); - WriteItemSlots(ref writer, entity.Equipment); - WriteItemSlots(ref writer, entity.CargoBays); - WriteItemSlots(ref writer, entity.DockingBays); - WriteCargoBays(ref writer, entity.CargoContents); - WriteCargoBays(ref writer, entity.DockingBayContents); - WriteInts(ref writer, entity.DockingBayAssignments); - WriteIntLists(ref writer, entity.WeaponGroups); - WriteChildren(ref writer, entity.Children); - } - - private static void WriteLoadoutItem(ref MessagePackWriter writer, AetheriaRuntimeLoadoutItemCommit item) - { - writer.WriteArrayHeader(4); - writer.Write(item.ItemDefinitionLegacyId ?? ""); - writer.Write(item.Quality); - writer.Write(item.Durability); - writer.Write(item.Quantity); - } - - private static void WriteItemSlots(ref MessagePackWriter writer, IReadOnlyList? slots) - { - slots ??= Array.Empty(); - writer.WriteArrayHeader(slots.Count); - foreach (var slot in slots) - { - writer.WriteArrayHeader(3); - writer.Write(slot.X); - writer.Write(slot.Y); - WriteLoadoutItem(ref writer, slot.Item); - } - } - - private static void WriteCargoBays(ref MessagePackWriter writer, IReadOnlyList? cargoBays) - { - cargoBays ??= Array.Empty(); - writer.WriteArrayHeader(cargoBays.Count); - foreach (var bay in cargoBays) - WriteItemSlots(ref writer, bay.Items); - } - - private static void WriteChildren(ref MessagePackWriter writer, IReadOnlyList? children) - { - children ??= Array.Empty(); - writer.WriteArrayHeader(children.Count); - foreach (var child in children) - WriteEntityLoadout(ref writer, child); - } - - private static void WriteRunCheckpoint(ref MessagePackWriter writer, AetheriaRuntimeRunCheckpointCommit run) - { - writer.WriteArrayHeader(8); - writer.Write(run.RunId ?? ""); - writer.Write(run.IsTutorial); - writer.Write(run.EntranceZoneIndex); - writer.Write(run.ExitZoneIndex); - writer.Write(run.CurrentZoneIndex); - writer.Write(run.CurrentZoneEntityIndex); - WriteInts(ref writer, run.DiscoveredZoneIndices); - WriteZoneSnapshots(ref writer, run.Zones); - } - - private static void WriteZoneSnapshots(ref MessagePackWriter writer, IReadOnlyList? zones) - { - zones ??= Array.Empty(); - writer.WriteArrayHeader(zones.Count); - foreach (var zone in zones) - { - writer.WriteArrayHeader(8); - writer.Write(zone.ZoneIndex); - writer.Write(zone.Name ?? ""); - writer.Write(zone.PositionX); - writer.Write(zone.PositionY); - WriteInts(ref writer, zone.AdjacentZoneIndices); - WriteInts(ref writer, zone.FactionIndices); - writer.Write(zone.OwnerFactionIndex); - WriteEntitySnapshots(ref writer, zone.Entities); - } - } - - private static void WriteEntitySnapshots(ref MessagePackWriter writer, IReadOnlyList? entities) - { - entities ??= Array.Empty(); - writer.WriteArrayHeader(entities.Count); - foreach (var entity in entities) - { - writer.WriteArrayHeader(15); - writer.Write(entity.EntityIndex); - writer.Write(entity.Name ?? ""); - writer.Write(entity.Kind ?? ""); - writer.Write(entity.PositionX); - writer.Write(entity.PositionY); - writer.Write(entity.PositionZ); - writer.Write(entity.DirectionX); - writer.Write(entity.DirectionY); - writer.Write(entity.CorporationLegacyId ?? ""); - writer.Write(entity.HullItemDefinitionLegacyId ?? ""); - WriteItemSlots(ref writer, entity.Equipment); - WriteItemSlots(ref writer, entity.CargoBays); - WriteItemSlots(ref writer, entity.DockingBays); - WriteInts(ref writer, entity.ChildEntityIndices); - WriteIntLists(ref writer, entity.WeaponGroups); - } - } - - private static void WriteStrings(ref MessagePackWriter writer, IReadOnlyList? values) - { - values ??= Array.Empty(); - writer.WriteArrayHeader(values.Count); - foreach (var value in values) - writer.Write(value ?? ""); - } - - private static void WriteInts(ref MessagePackWriter writer, IReadOnlyList? values) - { - values ??= Array.Empty(); - writer.WriteArrayHeader(values.Count); - foreach (var value in values) - writer.Write(value); - } - - private static void WriteIntLists(ref MessagePackWriter writer, IReadOnlyList>? values) - { - values ??= Array.Empty>(); - writer.WriteArrayHeader(values.Count); - foreach (var value in values) - WriteInts(ref writer, value); + var document = MessagePackSerializer.Deserialize(File.ReadAllBytes(path)); + return new AetheriaRuntimeCommitEnvelope( + document.Schema ?? "", + ParseKind(document.Kind ?? ""), + document.CommandId ?? "", + document.CreatedAtUtc ?? "", + path); } private static string KindToken(AetheriaRuntimeCommitKind kind) @@ -453,9 +122,5 @@ namespace GameCult.Aetheria.State.Unity }; } - private static string ReadString(ref MessagePackReader reader) - { - return reader.ReadString() ?? ""; - } } } diff --git a/docs/aetheria-perfect-machine-map.md b/docs/aetheria-perfect-machine-map.md index f10855484..764db8b5a 100644 --- a/docs/aetheria-perfect-machine-map.md +++ b/docs/aetheria-perfect-machine-map.md @@ -314,14 +314,14 @@ legacy catalog cache, and legacy UI paths should be migration-only or deleted. `Aetheria.Shared.Unity` references this package directly so `Galaxy` can consume typed name files without loading legacy `NameFile` documents. The package also owns Unity's typed runtime commit log writer for settings, - loadout-template, and run-checkpoint command envelopes under + loadout-template, and run-checkpoint command documents under `aetheria-world.cc.pending`. This log is command-only: it cannot decide canonical state, and the `Aetheria.State` node applicator deletes applied - commands after writing typed documents. Run checkpoint envelopes now carry + commands after writing typed documents. Run checkpoint documents now carry current-zone and entity snapshots so the old `.zone` file path has a live typed runtime projection path. - `Aetheria.State.ApplyPending` opens the typed state node and applies queued - Unity runtime command envelopes from `aetheria-world.cc.pending`, deleting + Unity runtime command documents from `aetheria-world.cc.pending`, deleting successfully applied command files by default. It is an operational bridge, not a second state owner; the applicator delegates all writes to `AetheriaStateNode`. @@ -931,6 +931,9 @@ First Aetheria surfaces to publish: - Done: replace warning-only player settings, loadout, shutdown, and wormhole save paths with typed `.cc.pending` Verse commit commands and a state-node applicator that writes canonical typed documents. + - Done: replace the `.cc.pending` runtime commit lane's private raw + MessagePack array protocol with `AetheriaRuntimeStateCommitDocument`, + shared by the Unity runtime package and the provider applicator. - Done: extend run checkpoint commands to carry current-zone and entity snapshots into canonical `AetheriaZoneState` and `AetheriaEntitySnapshot` documents.