diff --git a/Assets/Scripts/Tests/DaemonRuntimeDocumentTests.cs b/Assets/Scripts/Tests/DaemonRuntimeDocumentTests.cs index 46133cc19..ab6aac53a 100644 --- a/Assets/Scripts/Tests/DaemonRuntimeDocumentTests.cs +++ b/Assets/Scripts/Tests/DaemonRuntimeDocumentTests.cs @@ -437,6 +437,13 @@ public class DaemonRuntimeDocumentTests Assert.AreEqual("cultnet://127.0.0.1:39751", verse.RemoteEndpoint); Assert.IsFalse(shard.IsPrimary); CollectionAssert.Contains(shard.PrimaryEndpoints, verse.RemoteEndpoint); + + using var bridge = new AetheriaEveUnitySceneProviderBridge( + statePath, + AetheriaRuntimeDaemonGameSurfaceBuilder.SurfaceId, + "unity-remote-test", + verse.RemoteEndpoint); + Assert.AreEqual("aetheria-remote-cultmesh-replica", bridge.TransportKind); } [Test] @@ -2952,7 +2959,7 @@ public class DaemonRuntimeDocumentTests var presentation = runtime.Connect(); - Assert.AreEqual("aetheria-cultmesh-cultcache-transport", liveBridge.TransportKind); + Assert.AreEqual("aetheria-local-cultmesh-replica", liveBridge.TransportKind); Assert.AreEqual(AetheriaRuntimeVerseRecordKeys.DaemonGameSurface.ToString(), liveBridge.SurfacePointer); Assert.AreEqual(AetheriaRuntimeVerseRecordKeys.DaemonAssetManifest.ToString(), liveBridge.AssetManifestPointer); Assert.AreEqual("interactive-world", liveBridge.CurrentSurfaceDocument.AdvertisedSurface.SurfaceKind); diff --git a/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveUnitySceneProviderBridge.cs b/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveUnitySceneProviderBridge.cs index 16fd787a6..c7c340be5 100644 --- a/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveUnitySceneProviderBridge.cs +++ b/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveUnitySceneProviderBridge.cs @@ -24,20 +24,24 @@ namespace GameCult.Aetheria.EveRuntime private readonly string _stateFilePathOverride; private readonly string _surfaceId; private readonly string _runtimeId; + private readonly string _cultMeshEndpoint; private AetheriaRuntimeStateBootReport? _stateBoot; private AetheriaClientState? _runtimeState; + private AetheriaClient? _remoteClient; private event Action? AssetManifestDocumentAvailable; public AetheriaEveUnitySceneProviderBridge( string stateFilePathOverride = "", string surfaceId = AetheriaRuntimeDaemonGameSurfaceBuilder.SurfaceId, - string runtimeId = "unity-scene") + string runtimeId = "unity-scene", + string cultMeshEndpoint = "") { _stateFilePathOverride = stateFilePathOverride ?? ""; _surfaceId = string.IsNullOrWhiteSpace(surfaceId) ? AetheriaRuntimeDaemonGameSurfaceBuilder.SurfaceId : surfaceId; _runtimeId = string.IsNullOrWhiteSpace(runtimeId) ? "unity-scene" : runtimeId; + _cultMeshEndpoint = cultMeshEndpoint?.Trim() ?? ""; CurrentDocument = CreateSurfaceDocument(AetheriaRuntimeSurfaceDocuments.EmptySurface(_surfaceId), 0); CurrentDocumentManifest = new EveUnityPlayableWorldAssetManifestDocument( AetheriaRuntimeVerseRecordKeys.DaemonAssetManifest.ToString(), @@ -45,7 +49,9 @@ namespace GameCult.Aetheria.EveRuntime "aetheria.daemon"); } - public string TransportKind => "aetheria-local-cultmesh-replica"; + public string TransportKind => IsRemote + ? "aetheria-remote-cultmesh-replica" + : "aetheria-local-cultmesh-replica"; public string SurfacePointer => CurrentDocument.SourcePointer; @@ -107,11 +113,25 @@ namespace GameCult.Aetheria.EveRuntime public void Refresh() { + if (IsRemote) + { + var remote = ResolveRemoteClient(); + remote.RefreshRemoteAsync().GetAwaiter().GetResult(); + RefreshFromRuntimeState(remote.State, null); + return; + } + var stateBoot = ResolveStateBoot(); if (!stateBoot.SupportsLocalStateFileRead || !stateBoot.StateFileExists) return; - var runtimeState = ResolveRuntimeState(stateBoot); + RefreshFromRuntimeState(ResolveRuntimeState(stateBoot), stateBoot); + } + + private void RefreshFromRuntimeState( + AetheriaClientState runtimeState, + AetheriaRuntimeStateBootReport? stateBoot) + { var surface = ReadSurface(runtimeState, stateBoot); if (surface != null) { @@ -130,6 +150,17 @@ namespace GameCult.Aetheria.EveRuntime { if (request == null) throw new ArgumentNullException(nameof(request)); var clientId = string.IsNullOrWhiteSpace(request.ClientId) ? _runtimeId : request.ClientId; + if (IsRemote) + { + var remote = ResolveRemoteClient(); + if (!remote.Control.TrySubmitSurfaceCommand(request, out var submitted)) + throw new InvalidOperationException($"Aetheria daemon did not advertise command {request.Operation?.OperationId ?? "unknown"}."); + + Debug.Log( + $"Forwarded Aetheria operation to remote CultMesh authority: {submitted!.Kind} {submitted.CommandId}"); + return; + } + var stateBoot = ResolveStateBoot(); var control = AetheriaEveRuntimeUnityHooks.RequireControl(stateBoot, clientId); if (control.TrySubmitSurfaceCommand(request, out var daemonEnvelope)) @@ -156,6 +187,30 @@ namespace GameCult.Aetheria.EveRuntime public void Dispose() { + _remoteClient?.Dispose(); + _remoteClient = null; + } + + private bool IsRemote => !string.IsNullOrWhiteSpace(_cultMeshEndpoint); + + private AetheriaClient ResolveRemoteClient() + { + if (_remoteClient != null) + return _remoteClient; + + var replicaPath = string.IsNullOrWhiteSpace(_stateFilePathOverride) + ? System.IO.Path.Combine(Application.persistentDataPath, "aetheria-remote-replica.cc") + : _stateFilePathOverride; + _remoteClient = AetheriaClient + .OpenRemoteAsync( + replicaPath, + _cultMeshEndpoint, + _runtimeId, + sessionId: "eve-unity", + synchronizeOnOpen: false) + .GetAwaiter() + .GetResult(); + return _remoteClient; } private AetheriaRuntimeStateBootReport ResolveStateBoot() @@ -174,7 +229,7 @@ namespace GameCult.Aetheria.EveRuntime private EveSurfaceDocument? ReadSurface( AetheriaClientState runtimeState, - AetheriaRuntimeStateBootReport stateBoot) + AetheriaRuntimeStateBootReport? stateBoot) { try { @@ -182,7 +237,9 @@ namespace GameCult.Aetheria.EveRuntime if (handle == null) return null; - var resolver = AetheriaEveRuntimeUnityHooks.TryCreateStateRefResolver(stateBoot, _runtimeId); + var resolver = stateBoot == null + ? null + : AetheriaEveRuntimeUnityHooks.TryCreateStateRefResolver(stateBoot.Value, _runtimeId); return AetheriaRuntimeSurfaceDocuments.ToEveSurfaceDocument(handle.Latest(), resolver); } catch (KeyNotFoundException) diff --git a/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveUnitySceneProviderComponent.cs b/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveUnitySceneProviderComponent.cs index 3c2ba408d..f9a2314ba 100644 --- a/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveUnitySceneProviderComponent.cs +++ b/Packages/org.gamecult.aetheria.eve-runtime/Runtime/AetheriaEveUnitySceneProviderComponent.cs @@ -18,6 +18,7 @@ namespace GameCult.Aetheria.EveRuntime IDisposable { [SerializeField] private string stateFilePathOverride = ""; + [SerializeField] private string cultMeshEndpoint = ""; [SerializeField] private string surfaceId = AetheriaRuntimeDaemonGameSurfaceBuilder.SurfaceId; [SerializeField] private string runtimeId = "unity-scene"; [SerializeField] private bool refreshOnEnable = true; @@ -84,7 +85,8 @@ namespace GameCult.Aetheria.EveRuntime public void Configure( string stateFilePath, string surfaceId = AetheriaRuntimeDaemonGameSurfaceBuilder.SurfaceId, - string runtimeId = "unity-scene") + string runtimeId = "unity-scene", + string endpoint = "") { Dispose(); stateFilePathOverride = stateFilePath ?? ""; @@ -94,6 +96,7 @@ namespace GameCult.Aetheria.EveRuntime this.runtimeId = string.IsNullOrWhiteSpace(runtimeId) ? "unity-scene" : runtimeId; + cultMeshEndpoint = endpoint ?? ""; } public override void Connect() @@ -136,7 +139,8 @@ namespace GameCult.Aetheria.EveRuntime _bridge = new AetheriaEveUnitySceneProviderBridge( stateFilePathOverride, surfaceId, - runtimeId); + runtimeId, + cultMeshEndpoint); } return _bridge; diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClient.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClient.cs index 6c36632d7..98b0a37c1 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClient.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClient.cs @@ -52,6 +52,30 @@ namespace GameCult.Aetheria.State.Verse return new AetheriaClient(verse, runtimeId, sessionId); } + public static async Task OpenRemoteAsync( + string replicaStatePath, + string endpoint, + string runtimeId = AetheriaRuntimeVerseClient.DefaultRuntimeId, + string sessionId = "remote", + bool synchronizeOnOpen = true) + { + var verse = await AetheriaRuntimeVerseClient + .OpenRemoteAsync( + replicaStatePath, + endpoint, + runtimeId, + pullOnOpen: false, + synchronizeOnOpen: synchronizeOnOpen) + .ConfigureAwait(false); + return new AetheriaClient(verse, runtimeId, sessionId); + } + + public Task RefreshRemoteAsync() + { + ThrowIfDisposed(); + return _verse.RefreshRemoteAsync(); + } + public static Task OpenLocalAsync( DirectoryInfo gameDataDirectory, string runtimeId,