diff --git a/Assets/Scripts/Tests/DaemonRuntimeDocumentTests.cs b/Assets/Scripts/Tests/DaemonRuntimeDocumentTests.cs index 510502041..efa473425 100644 --- a/Assets/Scripts/Tests/DaemonRuntimeDocumentTests.cs +++ b/Assets/Scripts/Tests/DaemonRuntimeDocumentTests.cs @@ -2831,6 +2831,143 @@ public class DaemonRuntimeDocumentTests } } + [Test] + public void GenericEveUnityRuntimePresentsAetheriaDaemonPlayableWorldThroughProviderBridge() + { + var statePath = Path.Combine( + Path.GetTempPath(), + "aetheria-eveunity-scene-playable-world-tests", + Path.GetRandomFileName(), + "state.cc"); + var run = RunWithTwoEntities(); + run.CurrentEntityKey = "zone.0.entity.1"; + run.Zones[0].Entities[0].Kind = "ship"; + run.Zones[0].Entities[0].FactionKey = "raider"; + run.Zones[0].Entities[1].Kind = "ship"; + run.Zones[0].Entities[1].FactionKey = "player"; + + var frame = AetheriaRuntimeDaemonFrameDocument.Create( + run, + "aetheria-daemon", + "session-scene-playable", + 81, + 1.5, + 0.02); + var tickResult = AetheriaRuntimeDaemonTickRunner.Tick( + statePath, + frame.Run, + new AetheriaRuntimeDaemonTickOptions + { + DaemonId = frame.DaemonId, + SessionId = frame.SessionId, + VerseId = "aetheria.local", + CultMeshAddress = "cultmesh://aetheria.local/eve/providers/aetheria.daemon", + FrameId = frame.FrameId, + SimulationTimeSeconds = frame.SimulationTimeSeconds, + FixedDeltaSeconds = frame.FixedDeltaSeconds + }); + PublishLatestFrameThroughVerseClient(statePath, tickResult.Frame); + PublishDaemonSurfacesThroughVerseClient(statePath, tickResult); + + var previousResolveStateBoot = AetheriaEveRuntimeUnityHooks.ResolveStateBoot; + var previousRuntimeState = AetheriaEveRuntimeUnityHooks.RuntimeState; + var previousControl = AetheriaEveRuntimeUnityHooks.Control; + var previousUi = AetheriaEveRuntimeUnityHooks.Ui; + var previousStateRefResolver = AetheriaEveRuntimeUnityHooks.StateRefResolver; + + try + { + AetheriaEveRuntimeUnityHookInstaller.Install(); + + using var bridge = new AetheriaEveUnitySceneProviderBridge( + statePath, + AetheriaRuntimeDaemonGameSurfaceBuilder.SurfaceId, + "unity-scene-playable-test"); + bridge.Refresh(); + + var sceneSink = new RecordingPlayableWorldSceneSink(); + using var runtime = new EveUnityPlayableWorldRuntime( + bridge, + bridge, + sceneSink, + bridge, + bridge); + EveUnitySceneCommandReceipt observedReceipt = null; + runtime.ReceiptAvailable += receipt => observedReceipt = receipt; + + var presentation = runtime.Connect(); + + Assert.AreEqual(1, presentation.ActiveEntities); + Assert.AreEqual(1, sceneSink.Upserts.Count); + Assert.IsNotNull(runtime.ActiveWorld); + Assert.AreEqual( + AetheriaRuntimeVerseRecordKeys.DaemonFrameLatest.ToString(), + runtime.ActiveWorld.StatePointerId); + Assert.AreEqual( + AetheriaRuntimeVerseRecordKeys.DaemonAssetManifest.ToString(), + runtime.ActiveWorld.AssetManifest); + var playerEntityId = run.EntityRecordKey(0, 1); + Assert.AreEqual(playerEntityId, runtime.ActiveWorld.PlayerEntityId); + Assert.IsNotNull(runtime.AssetManifests.GetForWorld(runtime.ActiveWorld)); + var playerUpsert = sceneSink.Upserts.FirstOrDefault(upsert => + upsert.Entity.EntityId == playerEntityId && + upsert.Entity.Controllable); + Assert.IsNotNull( + playerUpsert, + "Expected controllable player upsert for " + playerEntityId + + "; world player=" + runtime.ActiveWorld.PlayerEntityId + + "; upserts=" + string.Join( + "|", + sceneSink.Upserts.Select(upsert => + upsert.Entity.EntityId + ":" + upsert.Entity.Controllable + ":" + upsert.Entity.AssetRef))); + Assert.AreEqual(playerUpsert.Entity.AssetRef, playerUpsert.Asset.AssetRef); + Assert.IsTrue(runtime.AssetManifests.GetForWorld(runtime.ActiveWorld).Entries.Any(entry => + entry.AssetRef == playerUpsert.Asset.AssetRef && + entry.ResourcesPath == "Prefabs/Ships/Djinni")); + + var moveIntent = runtime.SubmitMoveVectorIntent( + runtime.ActiveWorld.PlayerEntityId, + 0.25f, + 0.75f, + 1f, + DateTimeOffset.Parse("2026-07-09T00:00:00Z")); + + Assert.AreEqual("aetheria.daemon", moveIntent.ProviderId); + Assert.AreEqual(AetheriaRuntimeDaemonGameSurfaceBuilder.SurfaceId, moveIntent.SurfaceId); + Assert.AreEqual("aetheria.daemon.commands", moveIntent.CommandBoundary); + Assert.AreEqual("aetheria.eve_command_acceptance_status.v1", moveIntent.ReceiptSchema); + Assert.AreEqual( + AetheriaRuntimeDaemonSurfaceCommandCatalog.CommandName(AetheriaRuntimeDaemonCommandKinds.SetMoveVector), + moveIntent.Payload.GetString("commandId")); + Assert.AreEqual(playerEntityId, moveIntent.Payload.GetString("entityId")); + Assert.AreEqual("0.25", moveIntent.Payload.GetString("directionX")); + Assert.AreEqual("0.75", moveIntent.Payload.GetString("directionY")); + + Assert.IsNotNull(observedReceipt); + Assert.AreEqual("accepted", observedReceipt.State); + Assert.AreEqual("Aetheria", observedReceipt.OwnerRepo); + Assert.AreEqual("aetheria-daemon-command-boundary", observedReceipt.Authority); + Assert.AreEqual("aetheria.daemon", observedReceipt.ProviderId); + Assert.AreEqual(AetheriaRuntimeDaemonGameSurfaceBuilder.SurfaceId, observedReceipt.SurfaceId); + Assert.AreEqual( + AetheriaRuntimeDaemonOperationIds.ForKind(AetheriaRuntimeDaemonCommandKinds.SetMoveVector), + observedReceipt.Command); + Assert.IsTrue(observedReceipt.IsProviderOwned); + Assert.IsTrue(observedReceipt.ShouldRefreshProviderSurface); + Assert.AreSame(observedReceipt, runtime.LastReceipt); + Assert.GreaterOrEqual(sceneSink.Upserts.Count, 2); + } + finally + { + AetheriaEveRuntimeUnityClientCache.Dispose(); + AetheriaEveRuntimeUnityHooks.ResolveStateBoot = previousResolveStateBoot; + AetheriaEveRuntimeUnityHooks.RuntimeState = previousRuntimeState; + AetheriaEveRuntimeUnityHooks.Control = previousControl; + AetheriaEveRuntimeUnityHooks.Ui = previousUi; + AetheriaEveRuntimeUnityHooks.StateRefResolver = previousStateRefResolver; + } + } + [Test] public void DaemonEveSurfaceCommandRejectsUnsupportedArgumentlessOperation() { @@ -4702,6 +4839,45 @@ public class DaemonRuntimeDocumentTests }; } + private sealed class RecordingPlayableWorldSceneSink : IEveUnityPlayableWorldSceneSink + { + public readonly List Upserts = + new List(); + public readonly List RemovedEntityIds = new List(); + + public EveUnityPlayableWorldProjection ConfiguredWorld { get; private set; } + + public void ConfigureWorld(EveUnityPlayableWorldProjection world) + { + ConfiguredWorld = world; + } + + public void UpsertEntity(EveUnityPlayableWorldEntity entity, EveUnityPlayableWorldAssetBinding asset) + { + Upserts.Add(new RecordedPlayableWorldUpsert(entity, asset)); + } + + public void RemoveEntity(string entityId) + { + RemovedEntityIds.Add(entityId); + } + } + + private sealed class RecordedPlayableWorldUpsert + { + public RecordedPlayableWorldUpsert( + EveUnityPlayableWorldEntity entity, + EveUnityPlayableWorldAssetBinding asset) + { + Entity = entity; + Asset = asset; + } + + public EveUnityPlayableWorldEntity Entity { get; } + + public EveUnityPlayableWorldAssetBinding Asset { get; } + } + private static AetheriaRuntimeZoneSnapshotCommit FindZone(AetheriaRuntimeRunCheckpointCommit run, int zoneIndex) { foreach (var zone in run.Zones) diff --git a/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveUnitySceneProviderBridge.cs b/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveUnitySceneProviderBridge.cs index 5adfd60b8..9e69e782c 100644 --- a/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveUnitySceneProviderBridge.cs +++ b/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveUnitySceneProviderBridge.cs @@ -23,8 +23,6 @@ namespace GameCult.Aetheria.EveRuntime private readonly string _stateFilePathOverride; private readonly string _surfaceId; private readonly string _runtimeId; - private CultMeshReactiveDocument? _surfaceState; - private CultMeshReactiveDocument? _catalogState; private AetheriaRuntimeStateBootReport? _stateBoot; private AetheriaClientState? _runtimeState; private event Action? AssetManifestDocumentAvailable; @@ -110,10 +108,6 @@ namespace GameCult.Aetheria.EveRuntime public void Dispose() { - _surfaceState?.Dispose(); - _surfaceState = null; - _catalogState?.Dispose(); - _catalogState = null; } private AetheriaRuntimeStateBootReport ResolveStateBoot() @@ -140,9 +134,8 @@ namespace GameCult.Aetheria.EveRuntime if (handle == null) return null; - _surfaceState ??= handle.Reactive(); var resolver = AetheriaEveRuntimeUnityHooks.TryCreateStateRefResolver(stateBoot, _runtimeId); - return AetheriaRuntimeSurfaceDocuments.ToEveSurfaceDocument(_surfaceState.Current, resolver); + return AetheriaRuntimeSurfaceDocuments.ToEveSurfaceDocument(handle.Latest(), resolver); } catch (KeyNotFoundException) { @@ -152,9 +145,9 @@ namespace GameCult.Aetheria.EveRuntime private EveUnityPlayableWorldAssetManifestDocument ReadAssetManifest(AetheriaClientState runtimeState) { - _catalogState ??= runtimeState.Catalog.Reactive(); + var catalog = runtimeState.Catalog.Latest(); var manifest = AetheriaRuntimeAssets.ProjectManifest( - _catalogState.Current, + catalog, runId: "", baseUri: "cultmesh://aetheria.local/assets"); return ToUnityPlayableWorldAssetManifest(manifest); diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonGameSurfaceBuilder.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonGameSurfaceBuilder.cs index 9970c98e4..b05701c8b 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonGameSurfaceBuilder.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonGameSurfaceBuilder.cs @@ -278,6 +278,7 @@ namespace GameCult.Aetheria.State.Verse { run ??= new AetheriaRuntimeRunCheckpointCommit(); zone ??= new AetheriaRuntimeZoneSnapshotCommit(); + var playerEntityId = PlayableWorldEntityId(run, zone, currentEntityKey); var props = new Dictionary(StringComparer.Ordinal) { ["label"] = string.IsNullOrWhiteSpace(zone.Name) ? "Aetheria World" : zone.Name, @@ -285,7 +286,7 @@ namespace GameCult.Aetheria.State.Verse ["assetManifest"] = AetheriaRuntimeVerseRecordKeys.DaemonAssetManifest.ToString(), ["inputProfile"] = "arpg.pointer-keyboard.v1", ["cameraRig"] = "arpg.orbital-follow.v1", - ["playerEntityId"] = currentEntityKey ?? "", + ["playerEntityId"] = playerEntityId, ["movementCommand"] = CommandName(AetheriaRuntimeDaemonCommandKinds.SetMoveVector), ["focusCommand"] = CommandName(AetheriaRuntimeDaemonCommandKinds.TargetNearest), ["targetCommand"] = CommandName(AetheriaRuntimeDaemonCommandKinds.SetTarget), @@ -327,6 +328,7 @@ namespace GameCult.Aetheria.State.Verse string currentEntityKey) { var entityId = run.EntityRecordKey(zone.ZoneIndex, entity.EntityIndex); + var playerEntityId = PlayableWorldEntityId(run, zone, currentEntityKey); var props = new Dictionary(StringComparer.Ordinal) { ["entityId"] = entityId, @@ -338,7 +340,7 @@ namespace GameCult.Aetheria.State.Verse ["rotationY"] = HeadingYaw(entity).ToString("0.###", CultureInfo.InvariantCulture), ["radius"] = PlayableWorldRadius(entity).ToString("0.###", CultureInfo.InvariantCulture), ["selectable"] = "true", - ["controllable"] = string.Equals(entityId, currentEntityKey, StringComparison.Ordinal) ? "true" : "false", + ["controllable"] = string.Equals(entityId, playerEntityId, StringComparison.Ordinal) ? "true" : "false", ["focusCommand"] = CommandName(AetheriaRuntimeDaemonCommandKinds.TargetNearest), ["moveCommand"] = CommandName(AetheriaRuntimeDaemonCommandKinds.SetMoveVector), ["targetCommand"] = CommandName(AetheriaRuntimeDaemonCommandKinds.SetTarget), @@ -550,6 +552,29 @@ namespace GameCult.Aetheria.State.Verse }; } + private static string PlayableWorldEntityId( + AetheriaRuntimeRunCheckpointCommit run, + AetheriaRuntimeZoneSnapshotCommit zone, + string currentEntityKey) + { + if (run == null) + return currentEntityKey ?? ""; + + if (AetheriaRuntimeRunCheckpointCommit.TryParseEntityKey( + currentEntityKey ?? "", + out var zoneIndex, + out var entityIndex)) + { + return run.EntityRecordKey(zoneIndex, entityIndex); + } + + var parsedEntityIndex = TryParseEntityIndex(currentEntityKey ?? ""); + if (parsedEntityIndex >= 0) + return run.EntityRecordKey(zone?.ZoneIndex ?? run.CurrentZoneIndex, parsedEntityIndex); + + return currentEntityKey ?? ""; + } + private static string ViewportDocumentId(string prefix, AetheriaRuntimeViewportBounds viewport) { var normalized = AetheriaRuntimeGameDocuments.Normalize(viewport); diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperationsClient.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperationsClient.cs index 90b4b513b..a84d47b6e 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperationsClient.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperationsClient.cs @@ -392,14 +392,24 @@ public sealed class AetheriaRuntimeDaemonOperationsClient out AetheriaRuntimeDaemonCommandKinds kind) { kind = AetheriaRuntimeDaemonCommandKinds.None; - var command = request.Operation?.OperationId ?? ""; - if (command.StartsWith(AetheriaRuntimeDaemonSurfaceCommandCatalog.CommandPrefix, StringComparison.Ordinal)) - command = command.Substring(AetheriaRuntimeDaemonSurfaceCommandCatalog.CommandPrefix.Length); + var command = NormalizeSurfaceCommandName(request.Operation?.OperationId ?? ""); + if (!Enum.TryParse(command, ignoreCase: false, out kind) || + kind == AetheriaRuntimeDaemonCommandKinds.None) + { + command = NormalizeSurfaceCommandName(ReadPayloadString(request, "commandId", "")); + } return Enum.TryParse(command, ignoreCase: false, out kind) && kind != AetheriaRuntimeDaemonCommandKinds.None; } + private static string NormalizeSurfaceCommandName(string command) + { + if (command.StartsWith(AetheriaRuntimeDaemonSurfaceCommandCatalog.CommandPrefix, StringComparison.Ordinal)) + command = command.Substring(AetheriaRuntimeDaemonSurfaceCommandCatalog.CommandPrefix.Length); + return command; + } + private static bool TrySubmitSurfaceCommand( AetheriaRuntimeDaemonOperationClient client, AetheriaRuntimeDaemonFrameDocument? frame,