From fe4cd60d6082682cf0fbd156e012c70729b4058c Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 21 Jul 2026 22:52:09 +0100 Subject: [PATCH] Decouple daemon from Unity Play lifecycle --- Aetheria.State.Verify/Program.cs | 10 ++- .../Editor/AetheriaDaemonDevelopmentWindow.cs | 90 ++++--------------- docs/unity-daemon-development.md | 19 ++-- 3 files changed, 37 insertions(+), 82 deletions(-) diff --git a/Aetheria.State.Verify/Program.cs b/Aetheria.State.Verify/Program.cs index 5131c4c27..e8d0b86fe 100644 --- a/Aetheria.State.Verify/Program.cs +++ b/Aetheria.State.Verify/Program.cs @@ -1358,11 +1358,15 @@ static void RequireClientTransportPlaneOwnership(string root) string.Join(", ", missingEditorResetSymbols)); if (editorLauncher.Contains("Start-Process", StringComparison.Ordinal) || !editorController.Contains("Process.Start(new ProcessStartInfo(DaemonExePath, arguments)", StringComparison.Ordinal) || - !editorController.Contains("SessionState.SetInt(ProcessIdSessionKey, daemon.Id)", StringComparison.Ordinal)) + !editorController.Contains("SessionState.SetInt(ProcessIdSessionKey, daemon.Id)", StringComparison.Ordinal) || + editorController.Contains("playModeStateChanged", StringComparison.Ordinal) || + editorController.Contains("_enterPlayWhenReady", StringComparison.Ordinal) || + editorController.Contains("Start & Play", StringComparison.Ordinal) || + editorController.Contains("Start before Play", StringComparison.Ordinal)) throw new InvalidOperationException( - "Unity must directly own the Debug daemon process after synchronous build/import preparation."); + "The daemon window must directly own its process without coupling build, launch, or shutdown to Unity Play lifecycle."); - Console.WriteLine("Client transport ownership: Ymir restores before transport listen; Unity directly owns the prepared Debug daemon and matching private state"); + Console.WriteLine("Client transport ownership: Ymir restores before transport listen; the daemon window owns an independent prepared Debug daemon and matching private state"); } static void RequireDaemonPlayableRunGenerationAuthority(string root) diff --git a/Aetheria.Unity/Assets/Editor/AetheriaDaemonDevelopmentWindow.cs b/Aetheria.Unity/Assets/Editor/AetheriaDaemonDevelopmentWindow.cs index 6738ea7dd..2e79a32c9 100644 --- a/Aetheria.Unity/Assets/Editor/AetheriaDaemonDevelopmentWindow.cs +++ b/Aetheria.Unity/Assets/Editor/AetheriaDaemonDevelopmentWindow.cs @@ -15,8 +15,6 @@ namespace Aetheria.Editor { private const string MenuPath = "Aetheria/Daemon Development"; private const string PortPreference = "Aetheria.DaemonDevelopment.Port"; - private const string AutoStartPreference = "Aetheria.DaemonDevelopment.AutoStart"; - private const string StopAfterPlayPreference = "Aetheria.DaemonDevelopment.StopAfterPlay"; internal const string ClientScenePath = "Assets/Aetheria.unity"; private Vector2 _scroll; @@ -24,13 +22,6 @@ namespace Aetheria.Editor [MenuItem(MenuPath)] public static void Open() => GetWindow("Aetheria Daemon"); - public static void StartAndPlay() - { - Open(); - EnsureClientSceneLoaded(); - AetheriaDaemonDevelopmentController.Start(enterPlayWhenReady: true); - } - public static void OpenClientScene() { Open(); @@ -83,16 +74,6 @@ namespace Aetheria.Editor if (nextPort != port && nextPort is > 0 and <= 65535) EditorPrefs.SetInt(PortPreference, nextPort); - var autoStart = EditorPrefs.GetBool(AutoStartPreference, true); - var nextAutoStart = EditorGUILayout.Toggle("Start before Play", autoStart); - if (nextAutoStart != autoStart) - EditorPrefs.SetBool(AutoStartPreference, nextAutoStart); - - var stopAfterPlay = EditorPrefs.GetBool(StopAfterPlayPreference, false); - var nextStopAfterPlay = EditorGUILayout.Toggle("Stop after Play", stopAfterPlay); - if (nextStopAfterPlay != stopAfterPlay) - EditorPrefs.SetBool(StopAfterPlayPreference, nextStopAfterPlay); - EditorGUILayout.Space(6); EditorGUILayout.LabelField("Status", AetheriaDaemonDevelopmentController.Status); EditorGUILayout.LabelField("Endpoint", AetheriaDaemonDevelopmentController.Endpoint); @@ -111,9 +92,7 @@ namespace Aetheria.Editor GUI.enabled = !AetheriaDaemonDevelopmentController.IsStarting && !AetheriaDaemonDevelopmentController.IsRunning; if (GUILayout.Button("Start daemon")) - AetheriaDaemonDevelopmentController.Start(enterPlayWhenReady: false); - if (GUILayout.Button("Start & Play")) - StartAndPlay(); + AetheriaDaemonDevelopmentController.Start(); GUI.enabled = AetheriaDaemonDevelopmentController.IsRunning || AetheriaDaemonDevelopmentController.IsStarting; if (GUILayout.Button("Stop")) @@ -129,7 +108,7 @@ namespace Aetheria.Editor GUI.enabled = !AetheriaDaemonDevelopmentController.IsRunning && !AetheriaDaemonDevelopmentController.IsStarting; if (GUILayout.Button("Reimport state & start")) - AetheriaDaemonDevelopmentController.Start(enterPlayWhenReady: false, forceImport: true); + AetheriaDaemonDevelopmentController.Start(forceImport: true); GUI.enabled = true; } @@ -185,11 +164,8 @@ namespace Aetheria.Editor { private const string ProcessIdSessionKey = "Aetheria.DaemonDevelopment.ProcessId"; private const string PortPreference = "Aetheria.DaemonDevelopment.Port"; - private const string AutoStartPreference = "Aetheria.DaemonDevelopment.AutoStart"; - private const string StopAfterPlayPreference = "Aetheria.DaemonDevelopment.StopAfterPlay"; private static Process _launcher; private static Process _daemon; - private static bool _enterPlayWhenReady; private static bool _restartWhenStopped; private static string _pendingClockAction = ""; private static string _status = "Stopped"; @@ -201,7 +177,6 @@ namespace Aetheria.Editor { EditorApplication.update += Update; EditorApplication.quitting += Stop; - EditorApplication.playModeStateChanged += OnPlayModeStateChanged; EditorApplication.pauseStateChanged += OnPauseStateChanged; Reattach(); ApplyClientEnvironment(); @@ -228,25 +203,19 @@ namespace Aetheria.Editor private static string DaemonExePath => Path.Combine( ProjectRoot, "Aetheria.State.Daemon", "bin", "Debug", "net10.0", "Aetheria.State.Daemon.exe"); - public static void Start(bool enterPlayWhenReady, bool forceImport = false) + public static void Start(bool forceImport = false) { if (TryRefreshDaemon()) { ApplyClientEnvironment(); - if (enterPlayWhenReady && !EditorApplication.isPlaying) - EditorApplication.isPlaying = true; return; } if (IsStarting) - { - _enterPlayWhenReady |= enterPlayWhenReady; return; - } Directory.CreateDirectory(ArtifactsRoot); File.Delete(PidPath); _lastError = ""; - _enterPlayWhenReady = enterPlayWhenReady; ApplyClientEnvironment(); var script = Path.Combine(ProjectRoot, "scripts", "start-aetheria-daemon-editor.ps1"); @@ -272,7 +241,6 @@ namespace Aetheria.Editor public static void Stop() { - _enterPlayWhenReady = false; _pendingClockAction = ""; try { @@ -336,7 +304,7 @@ namespace Aetheria.Editor if (_restartWhenStopped) { _restartWhenStopped = false; - Start(enterPlayWhenReady: EditorApplication.isPlaying); + Start(); return; } @@ -349,7 +317,6 @@ namespace Aetheria.Editor { _lastError = ReadTail(LauncherErrorPath, 24); _status = $"Daemon preparation failed (exit {exitCode})"; - _enterPlayWhenReady = false; Changed?.Invoke(); return; } @@ -363,7 +330,6 @@ namespace Aetheria.Editor { _lastError = exception.ToString(); _status = "Daemon launch failed"; - _enterPlayWhenReady = false; Changed?.Invoke(); return; } @@ -378,11 +344,6 @@ namespace Aetheria.Editor Changed?.Invoke(); } ApplyClientEnvironment(); - if (_enterPlayWhenReady && !EditorApplication.isPlayingOrWillChangePlaymode) - { - _enterPlayWhenReady = false; - EditorApplication.isPlaying = true; - } TrySubmitPendingClockAction(); } else if (TryTakeDaemonExit(out var exitCode)) @@ -393,36 +354,6 @@ namespace Aetheria.Editor } } - private static void OnPlayModeStateChanged(PlayModeStateChange state) - { - if (state == PlayModeStateChange.ExitingEditMode && - !AetheriaDaemonDevelopmentWindow.IsClientSceneLoaded()) - { - EditorApplication.isPlaying = false; - EditorApplication.delayCall += PrepareClientSceneAndPlay; - } - else if (state == PlayModeStateChange.ExitingEditMode && - EditorPrefs.GetBool(AutoStartPreference, true) && !TryRefreshDaemon()) - { - EditorApplication.isPlaying = false; - Start(enterPlayWhenReady: true); - } - else if (state == PlayModeStateChange.EnteredEditMode && - EditorPrefs.GetBool(StopAfterPlayPreference, false)) - { - Stop(); - } - } - - private static void PrepareClientSceneAndPlay() - { - AetheriaDaemonDevelopmentWindow.EnsureClientSceneLoaded(); - if (EditorPrefs.GetBool(AutoStartPreference, true)) - Start(enterPlayWhenReady: true); - else - EditorApplication.isPlaying = true; - } - private static void OnPauseStateChanged(PauseState state) { if (!EditorApplication.isPlaying || !TryRefreshDaemon()) return; @@ -608,6 +539,19 @@ namespace Aetheria.Editor RedirectStandardOutput = true, RedirectStandardError = true }) ?? throw new InvalidOperationException("Aetheria Debug daemon process did not start."); + daemon.EnableRaisingEvents = true; + daemon.Exited += (_, __) => + { + try + { + AppendLine( + ErrorLogPath, + $"Aetheria Debug daemon PID {daemon.Id} exited with code {daemon.ExitCode} at {DateTimeOffset.UtcNow:O}."); + } + catch (InvalidOperationException) + { + } + }; daemon.OutputDataReceived += (_, value) => AppendLine(LogPath, value.Data); daemon.ErrorDataReceived += (_, value) => AppendLine(ErrorLogPath, value.Data); daemon.BeginOutputReadLine(); diff --git a/docs/unity-daemon-development.md b/docs/unity-daemon-development.md index 21cec904b..8f8ebdc58 100644 --- a/docs/unity-daemon-development.md +++ b/docs/unity-daemon-development.md @@ -11,6 +11,11 @@ editor directly launches and owns the daemon process, its PID, and its output streams. The generic EveUnity client connects directly to the daemon's local CultMesh endpoint. Odin is not part of this path. +The daemon lifecycle is independent of Unity Play Mode. Play never builds, +starts, restarts, or stops the daemon. Entering Play connects a generic client; +leaving Play drops that client while the authoritative daemon remains alive and +ready for the next connection. + The editor connects through the daemon's `cultnet+tcp` control endpoint. The advertised session then selects the dedicated content and QUIC realtime planes; the editor bootstrap does not choose those planes or retain an RUDP fallback. @@ -26,14 +31,16 @@ being debugged. ## Normal loop -1. Press **Start & Play**, or press Unity Play with **Start before Play** - enabled. -2. Edit and inspect the generic EveUnity-lowered world in Play Mode. -3. Use Unity Pause to submit the advertised `simulation.pause` action to the +1. Press **Start daemon** and wait for the window to report the live endpoint. +2. Enter and leave Unity Play Mode normally. The daemon remains running across + client sessions. +3. Edit and inspect the generic EveUnity-lowered world in Play Mode. +4. Use Unity Pause to submit the advertised `simulation.pause` action to the daemon. Unity unpause submits `simulation.rate.realtime`. -4. While paused, use **Advance one step** to commit exactly one fixed daemon +5. While paused, use **Advance one step** to commit exactly one fixed daemon simulation step. -5. Stop, restart, or reimport the isolated state from the same window. +6. Stop, restart, or reimport the isolated state explicitly from the same + window. Unity's editor clock is never gameplay authority. The pause and step controls are ordinary advertised Eve operations accepted by the daemon. If the active