diff --git a/Aetheria.State.Verify/Program.cs b/Aetheria.State.Verify/Program.cs index c49106abc..954fc225d 100644 --- a/Aetheria.State.Verify/Program.cs +++ b/Aetheria.State.Verify/Program.cs @@ -4122,6 +4122,12 @@ static void RequireInventoryCargoItemDetailsUseEveSurface(string root) "org.gamecult.aetheria.eve-runtime", "Runtime", "AetheriaEveUnitySurfaceHost.cs"); + var runtimeEveSurfaceAdapterPath = Path.Combine( + root, + "Packages", + "org.gamecult.aetheria.state", + "Runtime", + "AetheriaRuntimeEveSurfaceAdapter.cs"); var cargoItemSurfaceBuilderPath = Path.Combine( root, "Packages", @@ -4141,6 +4147,7 @@ static void RequireInventoryCargoItemDetailsUseEveSurface(string root) surfaceDocumentPath, daemonItemStatQueriesPath, eveUnitySurfaceHostPath, + runtimeEveSurfaceAdapterPath, cargoItemSurfaceBuilderPath, equippedItemSurfaceBuilderPath }; @@ -4160,6 +4167,7 @@ static void RequireInventoryCargoItemDetailsUseEveSurface(string root) var surfaceDocument = File.ReadAllText(surfaceDocumentPath); var daemonItemStatQueries = File.ReadAllText(daemonItemStatQueriesPath); var eveUnitySurfaceHost = File.ReadAllText(eveUnitySurfaceHostPath); + var runtimeEveSurfaceAdapter = File.ReadAllText(runtimeEveSurfaceAdapterPath); var cargoItemSurfaceBuilder = File.ReadAllText(cargoItemSurfaceBuilderPath); var equippedItemSurfaceBuilder = File.ReadAllText(equippedItemSurfaceBuilderPath); var requiredSymbols = new[] @@ -4215,9 +4223,10 @@ static void RequireInventoryCargoItemDetailsUseEveSurface(string root) !daemonItemStatQueries.Contains("public static bool TryReadItemStatRef(", StringComparison.Ordinal) || !daemonItemStatQueries.Contains("public string ValueRef =>", StringComparison.Ordinal) || !eveUnitySurfaceHost.Contains("Func stateRefResolver", StringComparison.Ordinal) || - !eveUnitySurfaceHost.Contains("surface = ResolveStateRefs(surface, stateRefResolver)", StringComparison.Ordinal) || - !eveUnitySurfaceHost.Contains("ResolvePropRef(props, AetheriaRuntimeSurfaceStateRefs.Source, \"value\", stateRefResolver)", StringComparison.Ordinal) || - !eveUnitySurfaceHost.Contains("ResolvePropRef(props, AetheriaRuntimeSurfaceStateRefs.Value, \"value\", stateRefResolver)", StringComparison.Ordinal) || + !eveUnitySurfaceHost.Contains("AetheriaRuntimeEveSurfaceAdapter.ResolveStateRefs(surface, stateRefResolver)", StringComparison.Ordinal) || + !runtimeEveSurfaceAdapter.Contains("public static EveSurfaceDocument ResolveStateRefs(", StringComparison.Ordinal) || + !runtimeEveSurfaceAdapter.Contains("ResolvePropRef(props, AetheriaRuntimeSurfaceStateRefs.Source, \"value\", stateRefResolver)", StringComparison.Ordinal) || + !runtimeEveSurfaceAdapter.Contains("ResolvePropRef(props, AetheriaRuntimeSurfaceStateRefs.Value, \"value\", stateRefResolver)", StringComparison.Ordinal) || !cargoItemSurfaceBuilder.Contains("public string ValueRef { get; }", StringComparison.Ordinal) || !cargoItemSurfaceBuilder.Contains("props.Add(AetheriaRuntimeSurfaceStateRefs.ValueRef(valueRef))", StringComparison.Ordinal) || !equippedItemSurfaceBuilder.Contains("public string ValueRef { get; }", StringComparison.Ordinal) || @@ -9501,7 +9510,10 @@ static void RequireRuntimeStateReaderOwnsUnityStateAcquisition(string root) "AetheriaRuntimeDaemonGameSurfaceBuilder.TuiSurfaceId", "AetheriaRuntimeDaemonEditorSurfaceBuilder.SurfaceId", "AetheriaRuntimeDaemonEditorSurfaceBuilder.TuiSurfaceId", - "AetheriaRuntimeEveSurfaceAdapter.ToEveSurfaceDocument(surface)", + "ToResolvedEveSurfaceDocument(stateFilePath, surface)", + "ResolveSurfaceStateRefs(stateFilePath, surface)", + "CreateStateRefResolver(string stateFilePath)", + "AetheriaRuntimeEveSurfaceAdapter.ResolveStateRefs(", "AetheriaRuntimeEveSurfaceAdapter.EmptySurface(surfaceId)" }; @@ -9532,6 +9544,10 @@ static void RequireRuntimeStateReaderOwnsUnityStateAcquisition(string root) { "public static class AetheriaRuntimeEveSurfaceAdapter", "public static EveSurfaceDocument ToEveSurfaceDocument(AetheriaRuntimeSurfaceDocument document)", + "public static EveSurfaceDocument ResolveStateRefs(", + "ResolvePropRef(props, AetheriaRuntimeSurfaceStateRefs.Source, \"value\", stateRefResolver)", + "ResolvePropRef(props, AetheriaRuntimeSurfaceStateRefs.Value, \"value\", stateRefResolver)", + "ResolvePropRef(props, AetheriaRuntimeSurfaceStateRefs.Label, \"label\", stateRefResolver)", "public static EveSurfaceDocument EmptySurface(string surfaceId)", "new EveSurfaceDocument(", "new EveSurfaceComponent(" diff --git a/Assets/Scripts/Tests/DaemonRuntimeDocumentTests.cs b/Assets/Scripts/Tests/DaemonRuntimeDocumentTests.cs index b0bb36caf..6c10e98a7 100644 --- a/Assets/Scripts/Tests/DaemonRuntimeDocumentTests.cs +++ b/Assets/Scripts/Tests/DaemonRuntimeDocumentTests.cs @@ -352,6 +352,11 @@ public class DaemonRuntimeDocumentTests Assert.IsNotNull(genericGameSurface); Assert.AreEqual(AetheriaRuntimeDaemonGameSurfaceBuilder.SurfaceId, genericGameSurface.Surface.Id); Assert.AreEqual(unityGameSurface.ProviderId, genericGameSurface.ProviderId); + Assert.IsTrue(ContainsEveSurfaceMetric(genericGameSurface.Surface.Root, "Target", "Target")); + Assert.IsTrue(ContainsEveSurfaceProp( + genericGameSurface.Surface.Root, + AetheriaRuntimeSurfaceStateRefs.Source, + AetheriaRuntimeDaemonStateRefs.CurrentTargetName)); var genericGameTuiSurface = AetheriaRuntimeStateReader.ReadEveSurface( statePath, AetheriaRuntimeDaemonGameSurfaceBuilder.TuiSurfaceId); @@ -364,6 +369,43 @@ public class DaemonRuntimeDocumentTests Assert.AreEqual(AetheriaRuntimeDaemonEditorSurfaceBuilder.TuiSurfaceId, genericEditorTuiSurface.Surface.Id); } + [Test] + public void EveSurfaceAdapterResolvesDaemonStateRefsBeforeLowering() + { + var surface = new EveSurfaceDocument( + "surface-state", + "gamecult.eve.surface.v1", + "aetheria.daemon", + "daemon", + "Daemon", + 1, + "", + new EveSurfaceTree( + "aetheria.test.surface", + new EveSurfaceComponent( + "aetheria.test.metric", + "metric", + new Dictionary(StringComparer.Ordinal) + { + ["label"] = "Target", + ["value"] = "stale", + [AetheriaRuntimeSurfaceStateRefs.Source] = AetheriaRuntimeDaemonStateRefs.CurrentTargetName + }, + Array.Empty()), + Array.Empty()), + Array.Empty()); + + var resolved = AetheriaRuntimeEveSurfaceAdapter.ResolveStateRefs( + surface, + stateRef => stateRef == AetheriaRuntimeDaemonStateRefs.CurrentTargetName ? "Live Target" : ""); + + Assert.IsTrue(ContainsEveSurfaceMetric(resolved.Surface.Root, "Target", "Live Target")); + Assert.IsTrue(ContainsEveSurfaceProp( + resolved.Surface.Root, + AetheriaRuntimeSurfaceStateRefs.Source, + AetheriaRuntimeDaemonStateRefs.CurrentTargetName)); + } + [Test] public void TickRunnerSkipsPreviouslyAccountedObservedCommands() { @@ -3489,4 +3531,35 @@ public class DaemonRuntimeDocumentTests return component.Children.Any(child => ContainsSurfaceProp(child, key, value)); } + + private static bool ContainsEveSurfaceMetric( + EveSurfaceComponent component, + string label, + string value) + { + if (component.Kind == "metric" && + component.Props.TryGetValue("label", out var actualLabel) && + component.Props.TryGetValue("value", out var actualValue) && + actualLabel == label && + actualValue == value) + { + return true; + } + + return component.Children.Any(child => ContainsEveSurfaceMetric(child, label, value)); + } + + private static bool ContainsEveSurfaceProp( + EveSurfaceComponent component, + string key, + string value) + { + if (component.Props.TryGetValue(key, out var actual) && + string.Equals(actual, value, StringComparison.Ordinal)) + { + return true; + } + + return component.Children.Any(child => ContainsEveSurfaceProp(child, key, value)); + } } diff --git a/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveUnitySurfaceHost.cs b/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveUnitySurfaceHost.cs index 28997834a..f4510d66c 100644 --- a/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveUnitySurfaceHost.cs +++ b/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveUnitySurfaceHost.cs @@ -62,7 +62,7 @@ namespace GameCult.Aetheria.EveRuntime var root = document.rootVisualElement; ConfigureRoot(root, chrome); - surface = ResolveStateRefs(surface, stateRefResolver); + surface = AetheriaRuntimeEveSurfaceAdapter.ResolveStateRefs(surface, stateRefResolver); var lowerer = new EveUiToolkitSurfaceLowerer(); if (chrome.UseShell) { @@ -189,69 +189,5 @@ namespace GameCult.Aetheria.EveRuntime return shell; } - private static EveSurfaceDocument ResolveStateRefs( - EveSurfaceDocument surface, - Func stateRefResolver) - { - if (surface == null || stateRefResolver == null) - return surface; - - return new EveSurfaceDocument( - surface.Type, - surface.Schema, - surface.ProviderId, - surface.ProviderKind, - surface.Title, - surface.Version, - surface.UpdatedAtUtc, - new EveSurfaceTree( - surface.Surface.Id, - ResolveStateRefs(surface.Surface.Root, stateRefResolver), - surface.Surface.Styles), - surface.Commands); - } - - private static EveSurfaceComponent ResolveStateRefs( - EveSurfaceComponent component, - Func stateRefResolver) - { - var props = new Dictionary(component.Props, StringComparer.Ordinal); - ResolvePropRef(props, AetheriaRuntimeSurfaceStateRefs.Source, "value", stateRefResolver); - ResolvePropRef(props, AetheriaRuntimeSurfaceStateRefs.Value, "value", stateRefResolver); - ResolvePropRef(props, AetheriaRuntimeSurfaceStateRefs.Label, "label", stateRefResolver); - - return new EveSurfaceComponent( - component.Id, - component.Kind, - props, - ResolveStateRefs(component.Children, stateRefResolver)); - } - - private static IReadOnlyList ResolveStateRefs( - IReadOnlyList children, - Func stateRefResolver) - { - if (children == null || children.Count == 0) - return Array.Empty(); - - var resolved = new EveSurfaceComponent[children.Count]; - for (var index = 0; index < children.Count; index++) - resolved[index] = ResolveStateRefs(children[index], stateRefResolver); - return resolved; - } - - private static void ResolvePropRef( - Dictionary props, - string refKey, - string valueKey, - Func stateRefResolver) - { - if (!props.TryGetValue(refKey, out var stateRef) || string.IsNullOrWhiteSpace(stateRef)) - return; - - var resolved = stateRefResolver(stateRef); - if (!string.IsNullOrWhiteSpace(resolved)) - props[valueKey] = resolved; - } } } diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeEveSurfaceAdapter.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeEveSurfaceAdapter.cs index 4cdbeb8f3..e29f01ff3 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeEveSurfaceAdapter.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeEveSurfaceAdapter.cs @@ -10,10 +10,17 @@ namespace GameCult.Aetheria.State.Verse public static class AetheriaRuntimeEveSurfaceAdapter { public static EveSurfaceDocument ToEveSurfaceDocument(AetheriaRuntimeSurfaceDocument document) + { + return ToEveSurfaceDocument(document, null); + } + + public static EveSurfaceDocument ToEveSurfaceDocument( + AetheriaRuntimeSurfaceDocument document, + Func? stateRefResolver) { if (document == null) throw new ArgumentNullException(nameof(document)); - return new EveSurfaceDocument( + var surface = new EveSurfaceDocument( "surface-state", "gamecult.eve.surface.v1", document.ProviderId, @@ -30,6 +37,31 @@ namespace GameCult.Aetheria.State.Verse document.Commands .Select(command => new EveCommandTemplate(command.Command, command.Label, command.Transport)) .ToArray()); + + return ResolveStateRefs(surface, stateRefResolver); + } + + public static EveSurfaceDocument ResolveStateRefs( + EveSurfaceDocument surface, + Func? stateRefResolver) + { + if (surface == null) throw new ArgumentNullException(nameof(surface)); + if (stateRefResolver == null) + return surface; + + return new EveSurfaceDocument( + surface.Type, + surface.Schema, + surface.ProviderId, + surface.ProviderKind, + surface.Title, + surface.Version, + surface.UpdatedAtUtc, + new EveSurfaceTree( + surface.Surface.Id, + ResolveStateRefs(surface.Surface.Root, stateRefResolver), + surface.Surface.Styles), + surface.Commands); } public static EveSurfaceDocument EmptySurface(string surfaceId) @@ -62,5 +94,48 @@ namespace GameCult.Aetheria.State.Verse new Dictionary(component.Props, StringComparer.Ordinal), component.Children.Select(ToEveSurfaceComponent).ToArray()); } + + private static EveSurfaceComponent ResolveStateRefs( + EveSurfaceComponent component, + Func stateRefResolver) + { + var props = new Dictionary(component.Props, StringComparer.Ordinal); + ResolvePropRef(props, AetheriaRuntimeSurfaceStateRefs.Source, "value", stateRefResolver); + ResolvePropRef(props, AetheriaRuntimeSurfaceStateRefs.Value, "value", stateRefResolver); + ResolvePropRef(props, AetheriaRuntimeSurfaceStateRefs.Label, "label", stateRefResolver); + + return new EveSurfaceComponent( + component.Id, + component.Kind, + props, + ResolveStateRefs(component.Children, stateRefResolver)); + } + + private static IReadOnlyList ResolveStateRefs( + IReadOnlyList children, + Func stateRefResolver) + { + if (children == null || children.Count == 0) + return Array.Empty(); + + var resolved = new EveSurfaceComponent[children.Count]; + for (var index = 0; index < children.Count; index++) + resolved[index] = ResolveStateRefs(children[index], stateRefResolver); + return resolved; + } + + private static void ResolvePropRef( + Dictionary props, + string refKey, + string valueKey, + Func stateRefResolver) + { + if (!props.TryGetValue(refKey, out var stateRef) || string.IsNullOrWhiteSpace(stateRef)) + return; + + var resolved = stateRefResolver(stateRef); + if (!string.IsNullOrWhiteSpace(resolved)) + props[valueKey] = resolved; + } } } diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateReader.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateReader.cs index d581d11b0..caa9e6bcb 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateReader.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeStateReader.cs @@ -105,8 +105,9 @@ namespace GameCult.Aetheria.State.Verse return editorTuiSurface; } - return AetheriaRuntimeCatalogStore.ReadEveSurfaces(stateFilePath) + var surface = AetheriaRuntimeCatalogStore.ReadEveSurfaces(stateFilePath) .FirstOrDefault(candidate => string.Equals(candidate.Surface.Id, surfaceId, StringComparison.Ordinal)); + return surface == null ? null : ResolveSurfaceStateRefs(stateFilePath, surface); } public static string ResolveEveSurfaceStateRef(string stateFilePath, string stateRef) @@ -220,7 +221,7 @@ namespace GameCult.Aetheria.State.Verse return false; } - document = AetheriaRuntimeEveSurfaceAdapter.ToEveSurfaceDocument(surface); + document = ToResolvedEveSurfaceDocument(stateFilePath, surface); return true; } @@ -234,7 +235,7 @@ namespace GameCult.Aetheria.State.Verse return false; } - document = AetheriaRuntimeEveSurfaceAdapter.ToEveSurfaceDocument(surface); + document = ToResolvedEveSurfaceDocument(stateFilePath, surface); return true; } @@ -248,7 +249,7 @@ namespace GameCult.Aetheria.State.Verse return false; } - document = AetheriaRuntimeEveSurfaceAdapter.ToEveSurfaceDocument(surface); + document = ToResolvedEveSurfaceDocument(stateFilePath, surface); return true; } @@ -262,7 +263,7 @@ namespace GameCult.Aetheria.State.Verse return false; } - document = AetheriaRuntimeEveSurfaceAdapter.ToEveSurfaceDocument(surface); + document = ToResolvedEveSurfaceDocument(stateFilePath, surface); return true; } @@ -271,6 +272,35 @@ namespace GameCult.Aetheria.State.Verse return AetheriaRuntimeEveSurfaceAdapter.EmptySurface(surfaceId); } + private static EveSurfaceDocument ToResolvedEveSurfaceDocument( + string stateFilePath, + AetheriaRuntimeSurfaceDocument surface) + { + return AetheriaRuntimeEveSurfaceAdapter.ToEveSurfaceDocument( + surface, + CreateStateRefResolver(stateFilePath)); + } + + private static EveSurfaceDocument ResolveSurfaceStateRefs( + string stateFilePath, + EveSurfaceDocument surface) + { + return AetheriaRuntimeEveSurfaceAdapter.ResolveStateRefs( + surface, + CreateStateRefResolver(stateFilePath)); + } + + private static Func CreateStateRefResolver(string stateFilePath) + { + TryReadDaemonFrame(stateFilePath, out var frame); + AetheriaRuntimeDaemonPublicationStore.TryReadHealth(stateFilePath, out var health); + AetheriaRuntimeDaemonPublicationStore.TryReadCommandBoundary(stateFilePath, out var commandBoundary); + + return stateRef => TryResolveDaemonStateRef(frame, health, commandBoundary, stateRef, out var value) + ? value + : ""; + } + private static AetheriaRuntimeZoneSnapshotCommit FindCurrentZone(AetheriaRuntimeRunCheckpointCommit run) { return run.Zones.FirstOrDefault(zone => zone.ZoneIndex == run.CurrentZoneIndex)