From cff88b79e4a36423d30076ecb3143ab82c177159 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 17 Jun 2026 10:39:28 +0100 Subject: [PATCH] Route Unity boot through typed client verse target --- .../Aetheria.State.Unity.csproj | 3 + Aetheria.State.Verify/Program.cs | 237 +++++++++++++++- Aetheria.State/README.md | 30 +- Assets/Scripts/Gameplay/ActionGameManager.cs | 10 +- Assets/Scripts/UI/MainMenu.cs | 57 +++- .../README.md | 9 +- .../Runtime/AetheriaEveRuntimeBootstrap.cs | 7 - .../Runtime/AetheriaEveSurfacePresenter.cs | 18 +- .../AetheriaRuntimeClientTargetStore.cs | 266 ++++++++++++++++++ .../Runtime/AetheriaRuntimeStateBoot.cs | 102 ++++++- .../Runtime/AetheriaRuntimeStateBoundary.cs | 17 ++ 11 files changed, 706 insertions(+), 50 deletions(-) create mode 100644 Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeClientTargetStore.cs diff --git a/Aetheria.State.Unity/Aetheria.State.Unity.csproj b/Aetheria.State.Unity/Aetheria.State.Unity.csproj index 0b12ca5e6..efe2a08e4 100644 --- a/Aetheria.State.Unity/Aetheria.State.Unity.csproj +++ b/Aetheria.State.Unity/Aetheria.State.Unity.csproj @@ -11,6 +11,9 @@ + + + diff --git a/Aetheria.State.Verify/Program.cs b/Aetheria.State.Verify/Program.cs index 8efa98364..f9bdfcdf0 100644 --- a/Aetheria.State.Verify/Program.cs +++ b/Aetheria.State.Verify/Program.cs @@ -50,6 +50,7 @@ RequireInventoryDropdownUseEveSurface(root); RequireNoDeadPopupShells(root); RequirePlayerSettingsEveSurface(root); RequireVerseHostSettingsAuthority(root); +RequireClientTargetBootAuthority(root); RequireMainMenuVerseHostProjection(root); RequireMainMenuContinueRunState(root); RequireDeadPropertiesPanelShellDeleted(root); @@ -536,6 +537,7 @@ Console.WriteLine("Typed behavior metadata authority: live heat/mining/thermotog Console.WriteLine("NameTools editor shell: the remaining name helper window lowers through UI Toolkit instead of IMGUI"); Console.WriteLine("Runtime state reader authority: Unity gameplay/UI read typed state through a shared runtime reader instead of direct store spelunking"); Console.WriteLine("Verse host authority: daemon-owned typed verse host settings now drive provider advertisement and operations telemetry"); +Console.WriteLine("Client target boot authority: Unity boot resolves the active Verse through a typed client target instead of local path folklore"); Console.WriteLine("Main-menu Verse projection: the Unity Eve shell lowers daemon-owned verse identity instead of ad-libbing local menu copy"); Console.WriteLine("Runtime simulation tuning authority: UI writes flow through gameplay checkpoint commits"); Console.WriteLine("Hull conductivity authority: inventory UI toggles flow through gameplay checkpoint commits"); @@ -692,6 +694,7 @@ static void RequirePackageSerializerBoundary(string root) var allowedFiles = new HashSet(StringComparer.Ordinal) { "AetheriaRuntimeCatalogStore.cs", + "AetheriaRuntimeClientTargetStore.cs", "AetheriaRuntimePendingCultCacheStore.cs", "AetheriaRuntimeStateCommitDocument.cs", "AetheriaRuntimeEveCommandDocument.cs" @@ -2525,7 +2528,9 @@ static void RequireMainMenuRootUsesEveSurface(string root) "QuitCommand", "BuildMainSurfaceDefinition(", "HandleMainSurfaceCommand(", - "RenderMenuSurface(BuildMainSurfaceDefinition(LatestContinueRun(), LatestVerseHostSettings(), InGame), HandleMainSurfaceCommand);", + "BuildMainSurfaceDefinition(", + "LatestContinueRun(stateBoot)", + "LatestVerseHostSettings(stateBoot)", "HideMenuSurface();" }; @@ -3813,6 +3818,223 @@ static void RequireVerseHostSettingsAuthority(string root) } } +static void RequireClientTargetBootAuthority(string root) +{ + var unityFacadeProjectPath = Path.Combine(root, "Aetheria.State.Unity", "Aetheria.State.Unity.csproj"); + var boundaryPath = Path.Combine(root, "Packages", "org.gamecult.aetheria.state", "Runtime", "AetheriaRuntimeStateBoundary.cs"); + var bootPath = Path.Combine(root, "Packages", "org.gamecult.aetheria.state", "Runtime", "AetheriaRuntimeStateBoot.cs"); + var clientTargetStorePath = Path.Combine(root, "Packages", "org.gamecult.aetheria.state", "Runtime", "AetheriaRuntimeClientTargetStore.cs"); + var bootstrapPath = Path.Combine(root, "Packages", "org.gamecult.aetheria.eve-runtime", "Runtime", "AetheriaEveRuntimeBootstrap.cs"); + var presenterPath = Path.Combine(root, "Packages", "org.gamecult.aetheria.eve-runtime", "Runtime", "AetheriaEveSurfacePresenter.cs"); + var actionGameManagerPath = Path.Combine(root, "Assets", "Scripts", "Gameplay", "ActionGameManager.cs"); + var mainMenuPath = Path.Combine(root, "Assets", "Scripts", "UI", "MainMenu.cs"); + + var requiredFiles = new[] + { + unityFacadeProjectPath, + boundaryPath, + bootPath, + clientTargetStorePath, + bootstrapPath, + presenterPath, + actionGameManagerPath, + mainMenuPath + }; + + var missingFiles = requiredFiles + .Where(path => !File.Exists(path)) + .Select(path => Path.GetRelativePath(root, path)) + .ToArray(); + if (missingFiles.Length > 0) + { + throw new InvalidOperationException( + "Client target boot authority cannot be verified because required files are missing: " + + string.Join(", ", missingFiles)); + } + + var unityFacadeProject = File.ReadAllText(unityFacadeProjectPath); + var boundary = File.ReadAllText(boundaryPath); + var boot = File.ReadAllText(bootPath); + var clientTargetStore = File.ReadAllText(clientTargetStorePath); + var bootstrap = File.ReadAllText(bootstrapPath); + var presenter = File.ReadAllText(presenterPath); + var actionGameManager = File.ReadAllText(actionGameManagerPath); + var mainMenu = File.ReadAllText(mainMenuPath); + + var requiredUnityFacadeSymbols = new[] + { + "AetheriaRuntimeClientTargetStore.cs", + "AetheriaRuntimeStateBoundary.cs", + "AetheriaRuntimeStateBoot.cs" + }; + var missingUnityFacadeSymbols = requiredUnityFacadeSymbols + .Where(symbol => !unityFacadeProject.Contains(symbol, StringComparison.Ordinal)) + .ToArray(); + if (missingUnityFacadeSymbols.Length > 0) + { + throw new InvalidOperationException( + "Aetheria.State.Unity.csproj no longer compiles the shared client-target boot files: " + + string.Join(", ", missingUnityFacadeSymbols)); + } + + var requiredBoundarySymbols = new[] + { + "RuntimeClientTargetFileName = \"aetheria-client.cc\"", + "RuntimeStatePathOverrideEnvironmentVariable = \"AETHERIA_STATE_PATH\"", + "LegacyRuntimeStatePathOverrideEnvironmentVariable = \"AETHERIA_EVE_STATE_PATH\"", + "GetClientTargetPath", + "ResolveStatePathOverride" + }; + var missingBoundarySymbols = requiredBoundarySymbols + .Where(symbol => !boundary.Contains(symbol, StringComparison.Ordinal)) + .ToArray(); + if (missingBoundarySymbols.Length > 0) + { + throw new InvalidOperationException( + "Runtime state boundary no longer owns the client target file and override resolution: " + + string.Join(", ", missingBoundarySymbols)); + } + + var requiredClientTargetSymbols = new[] + { + "public static class AetheriaRuntimeClientTargetKinds", + "public sealed class AetheriaRuntimeClientTargetDocument", + "SchemaId = \"gamecult.aetheria.runtime_client_target.v1\"", + "TargetKind", + "StateFilePath", + "CultMeshAddress", + "ReadOrInitialize", + "CreateDefault", + "Write(string clientTargetPath, AetheriaRuntimeClientTargetDocument document)", + "Read(string clientTargetPath)" + }; + var missingClientTargetSymbols = requiredClientTargetSymbols + .Where(symbol => !clientTargetStore.Contains(symbol, StringComparison.Ordinal)) + .ToArray(); + if (missingClientTargetSymbols.Length > 0) + { + throw new InvalidOperationException( + "Typed client target store is incomplete: " + + string.Join(", ", missingClientTargetSymbols)); + } + + var requiredBootSymbols = new[] + { + "ClientTargetPath", + "TargetKind", + "TargetSource", + "SupportsLocalStateFileRead", + "FailureMessage", + "TargetLabel", + "AetheriaRuntimeClientTargetStore.ReadOrInitialize", + "AetheriaRuntimeStateBoundary.ResolveStatePathOverride()", + "AetheriaRuntimeClientTargetKinds.CultMeshVerse", + "state-path-override", + "client-target" + }; + var missingBootSymbols = requiredBootSymbols + .Where(symbol => !boot.Contains(symbol, StringComparison.Ordinal)) + .ToArray(); + if (missingBootSymbols.Length > 0) + { + throw new InvalidOperationException( + "Runtime boot no longer resolves the active Verse through the client target owner: " + + string.Join(", ", missingBootSymbols)); + } + + var requiredActionGameManagerSymbols = new[] + { + "AetheriaRuntimeStateBoot.Inspect(GameDataDirectory)", + "Aetheria runtime target: {stateBoot.TargetLabel} via {stateBoot.TargetKind} ({stateBoot.TargetSource})", + "!stateBoot.SupportsLocalStateFileRead" + }; + var missingActionGameManagerSymbols = requiredActionGameManagerSymbols + .Where(symbol => !actionGameManager.Contains(symbol, StringComparison.Ordinal)) + .ToArray(); + if (missingActionGameManagerSymbols.Length > 0) + { + throw new InvalidOperationException( + "ActionGameManager no longer boots gameplay through the shared client-target report: " + + string.Join(", ", missingActionGameManagerSymbols)); + } + + var requiredPresenterSymbols = new[] + { + "AetheriaRuntimeStateBoot.Inspect(gameDataDirectory, stateFilePathOverride)", + "!stateBoot.SupportsLocalStateFileRead", + "stateBoot.StateFileExists" + }; + var missingPresenterSymbols = requiredPresenterSymbols + .Where(symbol => !presenter.Contains(symbol, StringComparison.Ordinal)) + .ToArray(); + if (missingPresenterSymbols.Length > 0) + { + throw new InvalidOperationException( + "Aetheria Eve presenter no longer mounts through the shared client-target boot report: " + + string.Join(", ", missingPresenterSymbols)); + } + + if (bootstrap.Contains("StatePathEnvironmentVariable", StringComparison.Ordinal) || + bootstrap.Contains("presenter.StateFilePathOverride =", StringComparison.Ordinal)) + { + throw new InvalidOperationException( + "Aetheria Eve runtime bootstrap still tries to own state-path override resolution."); + } + + var requiredMainMenuSymbols = new[] + { + "CurrentStateBoot()", + "LatestContinueRun(AetheriaRuntimeStateBootReport stateBoot)", + "LatestVerseHostSettings(AetheriaRuntimeStateBootReport stateBoot)", + "stateBoot.FailureMessage", + "\"Client Target\"", + "\"Transport\"", + "\"Target Source\"" + }; + var missingMainMenuSymbols = requiredMainMenuSymbols + .Where(symbol => !mainMenu.Contains(symbol, StringComparison.Ordinal)) + .ToArray(); + if (missingMainMenuSymbols.Length > 0) + { + throw new InvalidOperationException( + "MainMenu no longer lowers the shared client-target boot state through Eve: " + + string.Join(", ", missingMainMenuSymbols)); + } + + var forbiddenDirectPathSymbols = new Dictionary + { + [actionGameManagerPath] = new[] + { + "AetheriaRuntimeStateBoundary.GetStateFilePath(GameDataDirectory)" + }, + [presenterPath] = new[] + { + "AetheriaRuntimeStateBoundary.GetStateFilePath(gameDataDirectory)" + }, + [bootstrapPath] = new[] + { + "StatePathOverride()" + } + }; + + var hits = forbiddenDirectPathSymbols + .SelectMany(entry => + { + var source = File.ReadAllText(entry.Key); + return entry.Value + .Where(symbol => source.Contains(symbol, StringComparison.Ordinal)) + .Select(symbol => $"{Path.GetRelativePath(root, entry.Key)} -> {symbol}"); + }) + .ToArray(); + + if (hits.Length > 0) + { + throw new InvalidOperationException( + "Unity boot still contains direct local state-path owners instead of the shared client target: " + + string.Join("; ", hits)); + } +} + static void RequireMainMenuVerseHostProjection(string root) { var packageSnapshotPath = Path.Combine(root, "Packages", "org.gamecult.aetheria.state", "Runtime", "AetheriaRuntimeCatalogSnapshot.cs"); @@ -3887,12 +4109,15 @@ static void RequireMainMenuVerseHostProjection(string root) var requiredMainMenuSymbols = new[] { - "LatestVerseHostSettings()", - "AetheriaRuntimeStateReader.ReadVerseHostSettings(ActionGameManager.RuntimeStateFilePath)", + "LatestVerseHostSettings(AetheriaRuntimeStateBootReport stateBoot)", + "AetheriaRuntimeStateReader.ReadVerseHostSettings(stateBoot.StateFilePath)", + "\"Client Target\"", + "\"Transport\"", + "\"Target Source\"", "\"Verse\"", "\"Visibility\"", "\"CultMesh\"", - "Verse state and game truth belong to the daemon serving" + "The client target chooses which Verse it follows; game truth belongs to the daemon serving" }; var missingMainMenuSymbols = requiredMainMenuSymbols .Where(symbol => !mainMenu.Contains(symbol, StringComparison.Ordinal)) @@ -3948,7 +4173,7 @@ static void RequireMainMenuContinueRunState(string root) { "LatestContinueRun", "AetheriaRuntimeStateReader", - "ReadRunStates(ActionGameManager.RuntimeStateFilePath)", + "ReadRunStates(stateBoot.StateFilePath)", "ContinueGame(continueRun)", "ActionGameManager.ContinueRunState = run", "SceneManager.LoadScene(\"ARPG\")" @@ -4379,7 +4604,7 @@ static void RequireRuntimeStateReaderOwnsUnityStateAcquisition(string root) } if (!mainMenu.Contains("AetheriaRuntimeStateReader", StringComparison.Ordinal) || - !mainMenu.Contains("ReadRunStates(ActionGameManager.RuntimeStateFilePath)", StringComparison.Ordinal)) + !mainMenu.Contains("ReadRunStates(stateBoot.StateFilePath)", StringComparison.Ordinal)) { throw new InvalidOperationException( "MainMenu no longer routes Continue-run lookup through the shared runtime state reader."); diff --git a/Aetheria.State/README.md b/Aetheria.State/README.md index 622f5d03e..6cefedd2e 100644 --- a/Aetheria.State/README.md +++ b/Aetheria.State/README.md @@ -6,10 +6,12 @@ It uses modern `GameCult.Caching`, `GameCult.Caching.MessagePack`, and `GameCult.Mesh`. It does not reference the old `Assets/Scripts/ServerShared` tree, RethinkDB, JsonKnownTypes, or Newtonsoft.Json. -The first live state file is `GameData/aetheria-world.cc`. It is generated by -`Aetheria.State.Import` from checked-in legacy catalog inputs without embedding -machine-local absolute paths. Legacy files such as `GameData/AetherDB.msgpack` -are migration inputs only. +The first live state files are `GameData/aetheria-world.cc` for daemon-owned +Verse state and `GameData/aetheria-client.cc` for the client-owned active Verse +target. `Aetheria.State.Import` generates the daemon-owned world file from +checked-in legacy catalog inputs without embedding machine-local absolute +paths. Legacy files such as `GameData/AetherDB.msgpack` are migration inputs +only. The typed runtime state model is split into player settings, loadout templates, run state, zone state, entity snapshots, item slots, weapon groups, action-bar @@ -303,15 +305,17 @@ provider document and a local mirror of the Eve contract until the shared Eve Unity UI Toolkit lowering package exists. `Aetheria.State.Unity` is the Unity-facing runtime read facade over typed state. -It opens `aetheria-world.cc`, emits immutable catalog read models for trade, -equipment, behavior, hardpoint, manufacturer, corporation, and name-file -queries, exposes typed item shape masks for layout and fitting consumers, and -exposes typed interior masks and hardpoints for equipment/cargo layout -consumers, exposes typed behavior payload read models, can read typed player -settings for Unity boot, and can read the published Eve catalog surface. It -does not deserialize legacy runtime projection objects and does not write state. -Unity can use this as the first package boundary once CultLib/Eve runtime -packaging is available. +It resolves the active Verse through the client-owned `aetheria-client.cc` +target document, then opens the selected typed state source. Today that source +is still a local `.cc` file transport, so the facade emits immutable catalog +read models for trade, equipment, behavior, hardpoint, manufacturer, +corporation, and name-file queries, exposes typed item shape masks for layout +and fitting consumers, exposes typed interior masks and hardpoints for +equipment/cargo layout consumers, exposes typed behavior payload read models, +can read typed player settings for Unity boot, and can read the published Eve +catalog surface. It does not deserialize legacy runtime projection objects and +does not write daemon-owned Verse state. Unity can use this as the first +package boundary once CultLib/Eve runtime packaging is available. `Galaxy` now consumes this package-owned runtime catalog for faction selection and name generation, so generated sectors use typed corporation v2 and `aetheria.name_file.v2` records instead of legacy `Faction`/`NameFile` diff --git a/Assets/Scripts/Gameplay/ActionGameManager.cs b/Assets/Scripts/Gameplay/ActionGameManager.cs index ed42ac347..c53db197f 100644 --- a/Assets/Scripts/Gameplay/ActionGameManager.cs +++ b/Assets/Scripts/Gameplay/ActionGameManager.cs @@ -48,7 +48,7 @@ public class ActionGameManager : MonoBehaviour private static string _runtimeStateFilePath; public static string RuntimeStateFilePath => - _runtimeStateFilePath ??= AetheriaRuntimeStateBoundary.GetStateFilePath(GameDataDirectory); + _runtimeStateFilePath ??= AetheriaRuntimeStateBoot.Inspect(GameDataDirectory).StateFilePath; private static RuntimePlayerSettings _runtimePlayerSettings; public static RuntimePlayerSettings RuntimePlayerSettings @@ -2301,7 +2301,15 @@ public class ActionGameManager : MonoBehaviour var stateBoot = AetheriaRuntimeStateBoot.Inspect(GameDataDirectory); _runtimeStateFilePath = stateBoot.StateFilePath; + Debug.Log( + $"Aetheria runtime target: {stateBoot.TargetLabel} via {stateBoot.TargetKind} ({stateBoot.TargetSource})"); Debug.Log($"Aetheria typed state file: {stateBoot.StateFilePath}"); + if (!stateBoot.SupportsLocalStateFileRead) + { + throw new InvalidOperationException( + $"Aetheria runtime target cannot boot gameplay from local typed state: {stateBoot.FailureMessage}"); + } + if (!stateBoot.StateFileExists) { Debug.LogWarning("Aetheria typed state file is missing. Run the Aetheria.State importer before treating legacy catalog data as runtime state."); diff --git a/Assets/Scripts/UI/MainMenu.cs b/Assets/Scripts/UI/MainMenu.cs index ceda248f8..cfb07175c 100644 --- a/Assets/Scripts/UI/MainMenu.cs +++ b/Assets/Scripts/UI/MainMenu.cs @@ -52,7 +52,14 @@ public class MainMenu : MonoBehaviour private void ShowMain() { - RenderMenuSurface(BuildMainSurfaceDefinition(LatestContinueRun(), LatestVerseHostSettings(), InGame), HandleMainSurfaceCommand); + var stateBoot = CurrentStateBoot(); + RenderMenuSurface( + BuildMainSurfaceDefinition( + stateBoot, + LatestContinueRun(stateBoot), + LatestVerseHostSettings(stateBoot), + InGame), + HandleMainSurfaceCommand); } private void HandleMainSurfaceCommand(EveSurfaceCommandRequest request) @@ -60,7 +67,7 @@ public class MainMenu : MonoBehaviour switch (request.Command) { case ContinueRunCommand: - var continueRun = LatestContinueRun(); + var continueRun = LatestContinueRun(CurrentStateBoot()); if (continueRun == null) { Debug.LogWarning("Main-menu Continue requested without a typed run state."); @@ -156,12 +163,20 @@ public class MainMenu : MonoBehaviour return seed == 0 ? 1u : seed; } - private static AetheriaRuntimeRunStateSnapshot LatestContinueRun() + private static AetheriaRuntimeStateBootReport CurrentStateBoot() { + return AetheriaRuntimeStateBoot.Inspect(ActionGameManager.GameDataDirectory); + } + + private static AetheriaRuntimeRunStateSnapshot LatestContinueRun(AetheriaRuntimeStateBootReport stateBoot) + { + if (!stateBoot.SupportsLocalStateFileRead || !stateBoot.StateFileExists) + return null; + try { return AetheriaRuntimeStateReader - .ReadRunStates(ActionGameManager.RuntimeStateFilePath) + .ReadRunStates(stateBoot.StateFilePath) .Where(run => !string.IsNullOrWhiteSpace(run.RunId)) .OrderByDescending(run => run.UpdatedAtUtc, StringComparer.Ordinal) .ThenByDescending(run => run.RunId, StringComparer.Ordinal) @@ -174,11 +189,14 @@ public class MainMenu : MonoBehaviour } } - private static AetheriaRuntimeVerseHostSettingsSnapshot LatestVerseHostSettings() + private static AetheriaRuntimeVerseHostSettingsSnapshot LatestVerseHostSettings(AetheriaRuntimeStateBootReport stateBoot) { + if (!stateBoot.SupportsLocalStateFileRead || !stateBoot.StateFileExists) + return null; + try { - return AetheriaRuntimeStateReader.ReadVerseHostSettings(ActionGameManager.RuntimeStateFilePath); + return AetheriaRuntimeStateReader.ReadVerseHostSettings(stateBoot.StateFilePath); } catch (Exception ex) { @@ -386,6 +404,7 @@ public class MainMenu : MonoBehaviour } private static EveSurfaceDocument BuildMainSurfaceDefinition( + AetheriaRuntimeStateBootReport stateBoot, AetheriaRuntimeRunStateSnapshot continueRun, AetheriaRuntimeVerseHostSettingsSnapshot verseHost, bool inGame) @@ -393,6 +412,9 @@ public class MainMenu : MonoBehaviour var commands = new List(); var actionButtons = new List(); var cardChildren = new List(); + var targetLabel = stateBoot.TargetLabel; + var targetKind = string.IsNullOrWhiteSpace(stateBoot.TargetKind) ? "unknown" : stateBoot.TargetKind; + var targetSource = string.IsNullOrWhiteSpace(stateBoot.TargetSource) ? "unknown" : stateBoot.TargetSource; var verseTitle = string.IsNullOrWhiteSpace(verseHost?.Title) ? "Unknown Verse" : verseHost.Title; var verseId = string.IsNullOrWhiteSpace(verseHost?.VerseId) ? "unknown" : verseHost.VerseId; var verseVisibility = string.IsNullOrWhiteSpace(verseHost?.Visibility) ? "unknown" : verseHost.Visibility; @@ -403,7 +425,13 @@ public class MainMenu : MonoBehaviour if (!inGame) { - if (continueRun == null) + if (!stateBoot.SupportsLocalStateFileRead) + { + cardChildren.Add(Text( + $"{MainSurfaceId}.continue.note", + stateBoot.FailureMessage)); + } + else if (continueRun == null) { cardChildren.Add(Text( $"{MainSurfaceId}.continue.note", @@ -420,6 +448,19 @@ public class MainMenu : MonoBehaviour } } + cardChildren.Add(Metric( + $"{MainSurfaceId}.target.title", + "Client Target", + targetLabel)); + cardChildren.Add(Metric( + $"{MainSurfaceId}.target.transport", + "Transport", + targetKind)); + cardChildren.Add(Metric( + $"{MainSurfaceId}.target.source", + "Target Source", + targetSource)); + cardChildren.Add(Metric( $"{MainSurfaceId}.verse.title", "Verse", @@ -443,7 +484,7 @@ public class MainMenu : MonoBehaviour cardChildren.Add(Text( $"{MainSurfaceId}.note", - $"The client lowers this shell through Eve. Verse state and game truth belong to the daemon serving {verseLabel}.")); + $"The client lowers this shell through Eve. The client target chooses which Verse it follows; game truth belongs to the daemon serving {verseLabel}.")); cardChildren.Add(ButtonColumn($"{MainSurfaceId}.actions", actionButtons.ToArray())); return BuildMenuSurfaceDocument( diff --git a/Packages/org.gamecult.aetheria.eve-runtime/README.md b/Packages/org.gamecult.aetheria.eve-runtime/README.md index 3bbc7aa66..da5a88d53 100644 --- a/Packages/org.gamecult.aetheria.eve-runtime/README.md +++ b/Packages/org.gamecult.aetheria.eve-runtime/README.md @@ -5,7 +5,9 @@ CultCache state into Unity UI Toolkit. `AetheriaEveSurfacePresenter` is a projection bridge: -- it reads `gamecult.eve.surface.v1` from `GameData/aetheria-world.cc`; +- it resolves the active client target from `GameData/aetheria-client.cc`; +- it reads `gamecult.eve.surface.v1` from the selected typed state source, which + is currently still a local `.cc` file transport; - it lowers the retained tree through `org.gamecult.eve.unity-uitoolkit`; - it queues renderer-emitted commands as typed `.cc.eve.pending` command envelopes for Aetheria's provider-owned command bridge. @@ -14,8 +16,9 @@ CultCache state into Unity UI Toolkit. By default it creates a `UIDocument` host for `aetheria.operations`, so the provider-owned operations surface is present at runtime without a hand-wired scene object. Set `AETHERIA_EVE_SURFACE_ID` to mount a different surface, set -`AETHERIA_EVE_STATE_PATH` to point at a non-default `.cc` state file, or disable -the automatic mount with `AETHERIA_DISABLE_EVE_RUNTIME_BOOTSTRAP=true` or +`AETHERIA_STATE_PATH` to override the selected local `.cc` state file (`AETHERIA_EVE_STATE_PATH` +still works as a legacy fallback), or disable the automatic mount with +`AETHERIA_DISABLE_EVE_RUNTIME_BOOTSTRAP=true` or `--aetheria-disable-eve-runtime-bootstrap`. Batchmode disables the bootstrap so compile and smoke gates do not accidentally create renderer state. diff --git a/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveRuntimeBootstrap.cs b/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveRuntimeBootstrap.cs index 1252d4838..84c1843b6 100644 --- a/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveRuntimeBootstrap.cs +++ b/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveRuntimeBootstrap.cs @@ -12,7 +12,6 @@ namespace GameCult.Aetheria.EveRuntime public const string DefaultSurfaceId = "aetheria.operations"; public const string DisableEnvironmentVariable = "AETHERIA_DISABLE_EVE_RUNTIME_BOOTSTRAP"; public const string SurfaceEnvironmentVariable = "AETHERIA_EVE_SURFACE_ID"; - public const string StatePathEnvironmentVariable = "AETHERIA_EVE_STATE_PATH"; public const string DisableCommandLineSwitch = "--aetheria-disable-eve-runtime-bootstrap"; [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)] @@ -27,7 +26,6 @@ namespace GameCult.Aetheria.EveRuntime host.AddComponent(); var presenter = host.AddComponent(); presenter.SurfaceId = SurfaceId(); - presenter.StateFilePathOverride = StatePathOverride(); } private static bool IsDisabled() @@ -51,10 +49,5 @@ namespace GameCult.Aetheria.EveRuntime var configured = Environment.GetEnvironmentVariable(SurfaceEnvironmentVariable); return string.IsNullOrWhiteSpace(configured) ? DefaultSurfaceId : configured; } - - private static string StatePathOverride() - { - return Environment.GetEnvironmentVariable(StatePathEnvironmentVariable) ?? ""; - } } } diff --git a/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveSurfacePresenter.cs b/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveSurfacePresenter.cs index 2fe172327..a36cf7d58 100644 --- a/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveSurfacePresenter.cs +++ b/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveSurfacePresenter.cs @@ -43,8 +43,15 @@ namespace GameCult.Aetheria.EveRuntime var root = document.rootVisualElement; root.Clear(); - var statePath = ResolveStateFilePath(); - if (!File.Exists(statePath)) + var stateBoot = ResolveStateBoot(); + if (!stateBoot.SupportsLocalStateFileRead) + { + root.Add(BuildError(stateBoot.FailureMessage)); + return; + } + + var statePath = stateBoot.StateFilePath; + if (!stateBoot.StateFileExists) { root.Add(BuildError($"Aetheria state file not found: {statePath}")); return; @@ -74,13 +81,10 @@ namespace GameCult.Aetheria.EveRuntime return _document; } - private string ResolveStateFilePath() + private AetheriaRuntimeStateBootReport ResolveStateBoot() { - if (!string.IsNullOrWhiteSpace(stateFilePathOverride)) - return Path.GetFullPath(stateFilePathOverride); - var gameDataDirectory = new DirectoryInfo(Path.Combine(Application.dataPath, "..", "GameData")); - return AetheriaRuntimeStateBoundary.GetStateFilePath(gameDataDirectory); + return AetheriaRuntimeStateBoot.Inspect(gameDataDirectory, stateFilePathOverride); } private static VisualElement BuildError(string message) diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeClientTargetStore.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeClientTargetStore.cs new file mode 100644 index 000000000..047b73a9e --- /dev/null +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeClientTargetStore.cs @@ -0,0 +1,266 @@ +using System; +using System.Buffers; +using System.IO; +using MessagePack; + +#nullable enable + +namespace GameCult.Aetheria.State.Unity +{ + public static class AetheriaRuntimeClientTargetKinds + { + public const string StateFile = "state-file"; + public const string CultMeshVerse = "cultmesh-verse"; + } + + [MessagePackObject] + public sealed class AetheriaRuntimeClientTargetDocument + { + public const string SchemaId = "gamecult.aetheria.runtime_client_target.v1"; + public const string SchemaName = "aetheria.runtime_client_target"; + public const string SchemaVersion = "aetheria.runtime_client_target.v1"; + public const string DocumentKey = "local:aetheria.runtime_client_target.v1"; + + [Key(0)] public string Schema { get; set; } = SchemaId; + [Key(1)] public string TargetKind { get; set; } = AetheriaRuntimeClientTargetKinds.StateFile; + [Key(2)] public string Title { get; set; } = "Local Aetheria"; + [Key(3)] public string VerseId { get; set; } = "aetheria.local"; + [Key(4)] public string CultMeshAddress { get; set; } = "asgard.local.aetheria/eve"; + [Key(5)] public string StateFilePath { get; set; } = ""; + [Key(6)] public string UpdatedAtUtc { get; set; } = ""; + } + + public static class AetheriaRuntimeClientTargetStore + { + 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 AetheriaRuntimeClientTargetDocument ReadOrInitialize( + string clientTargetPath, + string defaultStateFilePath) + { + if (File.Exists(clientTargetPath)) + return Read(clientTargetPath); + + var document = CreateDefault(defaultStateFilePath); + Write(clientTargetPath, document); + return document; + } + + public static AetheriaRuntimeClientTargetDocument CreateDefault(string defaultStateFilePath) + { + return new AetheriaRuntimeClientTargetDocument + { + Schema = AetheriaRuntimeClientTargetDocument.SchemaId, + TargetKind = AetheriaRuntimeClientTargetKinds.StateFile, + Title = "Local Aetheria", + VerseId = "aetheria.local", + CultMeshAddress = "asgard.local.aetheria/eve", + StateFilePath = defaultStateFilePath ?? "", + UpdatedAtUtc = DateTime.UtcNow.ToString("O") + }; + } + + public static void Write(string clientTargetPath, AetheriaRuntimeClientTargetDocument document) + { + if (document == null) throw new ArgumentNullException(nameof(document)); + + document.Schema = AetheriaRuntimeClientTargetDocument.SchemaId; + document.UpdatedAtUtc = string.IsNullOrWhiteSpace(document.UpdatedAtUtc) + ? DateTime.UtcNow.ToString("O") + : document.UpdatedAtUtc; + + WriteDocument( + clientTargetPath, + AetheriaRuntimeClientTargetDocument.DocumentKey, + AetheriaRuntimeClientTargetDocument.SchemaId, + AetheriaRuntimeClientTargetDocument.SchemaName, + AetheriaRuntimeClientTargetDocument.SchemaVersion, + document.UpdatedAtUtc, + MessagePackSerializer.Serialize(document)); + } + + public static AetheriaRuntimeClientTargetDocument Read(string clientTargetPath) + { + return MessagePackSerializer.Deserialize( + ReadDocumentPayload(clientTargetPath, AetheriaRuntimeClientTargetDocument.SchemaId)); + } + + private static void WriteDocument( + string path, + string key, + string schemaId, + string schemaName, + string schemaVersion, + string storedAt, + byte[] payload) + { + var directory = Path.GetDirectoryName(path); + if (!string.IsNullOrWhiteSpace(directory)) + Directory.CreateDirectory(directory); + + 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($"Client target CultCache document '{path}' is missing store fields."); + } + + var formatVersion = reader.ReadString() ?? ""; + if (!formatVersion.StartsWith(FormatVersion, StringComparison.Ordinal)) + { + throw new InvalidDataException($"Client target '{path}' is not a CultCache store."); + } + + var schemaIds = ReadSchemaCatalog(ref reader); + var recordCount = reader.ReadArrayHeader(); + if (recordCount != 1) + { + throw new InvalidDataException($"Client target CultCache document '{path}' must contain exactly one record."); + } + + var recordFieldCount = reader.ReadArrayHeader(); + if (recordFieldCount < PersistedRecordFieldCount) + { + throw new InvalidDataException($"Client target CultCache record '{path}' is missing record fields."); + } + + reader.Skip(); + var schemaId = reader.ReadString() ?? ""; + reader.Skip(); + 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($"Client target '{path}' has schema '{schemaId}', expected '{expectedSchemaId}'."); + } + + if (Array.IndexOf(schemaIds, schemaId) < 0) + { + throw new InvalidDataException($"Client target '{path}' does not publish schema '{schemaId}' in its CultCache catalog."); + } + + return payload; + } + + private static string[] ReadSchemaCatalog(ref MessagePackReader reader) + { + var schemaCount = reader.ReadArrayHeader(); + var schemaIds = new string[schemaCount]; + for (var index = 0; index < schemaCount; index++) + { + var fieldCount = reader.ReadArrayHeader(); + if (fieldCount < SchemaCatalogEntryFieldCount) + { + throw new InvalidDataException("Client target CultCache schema entry is missing fields."); + } + + schemaIds[index] = reader.ReadString() ?? ""; + reader.Skip(); + reader.Skip(); + reader.Skip(); + reader.Skip(); + reader.Skip(); + + var memberCount = reader.ReadArrayHeader(); + for (var member = 0; member < memberCount; member++) + { + var memberFieldCount = reader.ReadArrayHeader(); + for (var field = 0; field < Math.Max(memberFieldCount, SchemaCatalogMemberFieldCount); field++) + { + if (field < memberFieldCount) + reader.Skip(); + } + } + + for (var field = SchemaCatalogEntryFieldCount; 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("Runtime client target"); + writer.Write(DateTime.UtcNow.ToString("O")); + writer.Write(StableHash(payload)); + 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 StableHash(byte[] payload) + { + if (payload.Length == 0) + return "empty"; + + using var sha = System.Security.Cryptography.SHA256.Create(); + var hash = sha.ComputeHash(payload); + return BitConverter.ToString(hash).Replace("-", "", StringComparison.Ordinal).ToLowerInvariant(); + } + + private static void WriteFileAtomically(string path, byte[] bytes) + { + var tempPath = path + ".tmp"; + File.WriteAllBytes(tempPath, bytes); + if (File.Exists(path)) + { + File.Delete(path); + } + + File.Move(tempPath, path); + } + } +} diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateBoot.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateBoot.cs index d82189d9a..0a7c38d28 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateBoot.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateBoot.cs @@ -1,28 +1,120 @@ +using System; using System.IO; namespace GameCult.Aetheria.State.Unity { public readonly struct AetheriaRuntimeStateBootReport { - public AetheriaRuntimeStateBootReport(string stateFilePath, bool stateFileExists) + public AetheriaRuntimeStateBootReport( + string clientTargetPath, + string targetKind, + string targetSource, + string title, + string verseId, + string cultMeshAddress, + string stateFilePath, + bool stateFileExists, + bool supportsLocalStateFileRead, + string failureMessage) { + ClientTargetPath = clientTargetPath; + TargetKind = targetKind; + TargetSource = targetSource; + Title = title; + VerseId = verseId; + CultMeshAddress = cultMeshAddress; StateFilePath = stateFilePath; StateFileExists = stateFileExists; + SupportsLocalStateFileRead = supportsLocalStateFileRead; + FailureMessage = failureMessage; } + public string ClientTargetPath { get; } + public string TargetKind { get; } + public string TargetSource { get; } + public string Title { get; } + public string VerseId { get; } + public string CultMeshAddress { get; } public string StateFilePath { get; } - public bool StateFileExists { get; } + public bool SupportsLocalStateFileRead { get; } + public string FailureMessage { get; } + + public string TargetLabel => + string.IsNullOrWhiteSpace(Title) + ? (string.IsNullOrWhiteSpace(VerseId) ? "Unknown Verse" : VerseId) + : (string.IsNullOrWhiteSpace(VerseId) || string.Equals(Title, VerseId, StringComparison.Ordinal) + ? Title + : $"{Title} ({VerseId})"); } public static class AetheriaRuntimeStateBoot { - public static AetheriaRuntimeStateBootReport Inspect(DirectoryInfo gameDataDirectory) + public static AetheriaRuntimeStateBootReport Inspect( + DirectoryInfo gameDataDirectory, + string explicitStateFilePathOverride = "") { - var stateFilePath = AetheriaRuntimeStateBoundary.GetStateFilePath(gameDataDirectory); + if (gameDataDirectory == null) throw new ArgumentNullException(nameof(gameDataDirectory)); + + var defaultStateFilePath = AetheriaRuntimeStateBoundary.GetStateFilePath(gameDataDirectory); + var clientTargetPath = AetheriaRuntimeStateBoundary.GetClientTargetPath(gameDataDirectory); + var target = AetheriaRuntimeClientTargetStore.ReadOrInitialize(clientTargetPath, defaultStateFilePath); + + var configuredOverride = string.IsNullOrWhiteSpace(explicitStateFilePathOverride) + ? AetheriaRuntimeStateBoundary.ResolveStatePathOverride() + : explicitStateFilePathOverride; + + var targetSource = string.IsNullOrWhiteSpace(configuredOverride) + ? "client-target" + : "state-path-override"; + var targetKind = target.TargetKind ?? ""; + var targetTitle = target.Title ?? ""; + var targetVerseId = target.VerseId ?? ""; + var targetCultMeshAddress = target.CultMeshAddress ?? ""; + var supportsLocalStateFileRead = true; + var failureMessage = ""; + string stateFilePath; + + if (!string.IsNullOrWhiteSpace(configuredOverride)) + { + stateFilePath = Path.GetFullPath(configuredOverride); + } + else if (string.Equals(targetKind, AetheriaRuntimeClientTargetKinds.CultMeshVerse, StringComparison.Ordinal)) + { + stateFilePath = ""; + supportsLocalStateFileRead = false; + failureMessage = + $"Selected client Verse target '{BuildTargetLabel(targetTitle, targetVerseId)}' points at CultMesh address '{targetCultMeshAddress}', but Unity still boots from local typed state until daemon transport lands."; + } + else + { + var configuredStateFilePath = string.IsNullOrWhiteSpace(target.StateFilePath) + ? defaultStateFilePath + : target.StateFilePath; + stateFilePath = Path.GetFullPath(configuredStateFilePath); + } + return new AetheriaRuntimeStateBootReport( + clientTargetPath, + targetKind, + targetSource, + targetTitle, + targetVerseId, + targetCultMeshAddress, stateFilePath, - File.Exists(stateFilePath)); + supportsLocalStateFileRead && File.Exists(stateFilePath), + supportsLocalStateFileRead, + failureMessage); + } + + private static string BuildTargetLabel(string title, string verseId) + { + if (string.IsNullOrWhiteSpace(title)) + return string.IsNullOrWhiteSpace(verseId) ? "Unknown Verse" : verseId; + + return string.IsNullOrWhiteSpace(verseId) || string.Equals(title, verseId, StringComparison.Ordinal) + ? title + : $"{title} ({verseId})"; } } } diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateBoundary.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateBoundary.cs index d3a99ac74..349311c66 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateBoundary.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateBoundary.cs @@ -5,10 +5,27 @@ namespace GameCult.Aetheria.State.Unity public static class AetheriaRuntimeStateBoundary { public const string RuntimeStateFileName = "aetheria-world.cc"; + public const string RuntimeClientTargetFileName = "aetheria-client.cc"; + public const string RuntimeStatePathOverrideEnvironmentVariable = "AETHERIA_STATE_PATH"; + public const string LegacyRuntimeStatePathOverrideEnvironmentVariable = "AETHERIA_EVE_STATE_PATH"; public static string GetStateFilePath(DirectoryInfo gameDataDirectory) { return Path.Combine(gameDataDirectory.FullName, RuntimeStateFileName); } + + public static string GetClientTargetPath(DirectoryInfo gameDataDirectory) + { + return Path.Combine(gameDataDirectory.FullName, RuntimeClientTargetFileName); + } + + public static string ResolveStatePathOverride() + { + var configured = System.Environment.GetEnvironmentVariable(RuntimeStatePathOverrideEnvironmentVariable); + if (!string.IsNullOrWhiteSpace(configured)) + return configured; + + return System.Environment.GetEnvironmentVariable(LegacyRuntimeStatePathOverrideEnvironmentVariable) ?? ""; + } } }