From 84c6980f8b45358cbbdc25cc87610b4fc81c261d Mon Sep 17 00:00:00 2001 From: soul Date: Fri, 4 Dec 2026 12:33:13 +0100 Subject: [PATCH] Queue galaxy generation phase labels in MainMenu UI without delaying generation --- Assets/Scripts/UI/MainMenu.cs | 65 +++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 15 deletions(-) diff --git a/Assets/Scripts/UI/MainMenu.cs b/Assets/Scripts/UI/MainMenu.cs index 0b333e83b..d63b4b859 100644 --- a/Assets/Scripts/UI/MainMenu.cs +++ b/Assets/Scripts/UI/MainMenu.cs @@ -1,5 +1,6 @@ using System; using System.Collections; +using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; @@ -238,10 +239,13 @@ public class MainMenu : MonoBehaviour // The only way into a new run: generates the scenario's galaxy off the main thread and enters the game, where // StartGame has RunStart stage it and only then replaces the saved run. A galaxy that cannot generate is refused - // here, with the saved run untouched. + // here, with the saved run untouched. Phase labels queue for display here so generation never sleeps. + private const float PhaseDisplaySeconds = 0.35f; + public void Launch(Scenario scenario) { var generatorState = "Loading Database Contents"; + var phases = new ConcurrentQueue(); Launching = true; Fade(true); @@ -257,23 +261,54 @@ public class MainMenu : MonoBehaviour Settings.CastBackgroundSettings, ActionGameManager.CultCache, Debug.Log, - state => generatorState = state); - Task.Run(() => RunStart.Generate(scenario, stage)).ContinueWith(generation => - Observable.NextFrame().Subscribe(_ => + state => { - if (generation.IsFaulted) + if (!string.IsNullOrEmpty(state)) phases.Enqueue(state); + }); + var generation = Task.Run(() => RunStart.Generate(scenario, stage)); + StartCoroutine(PaceGalaxyLaunch(scenario, generation, phases, () => generatorState, state => generatorState = state)); + } + + private IEnumerator PaceGalaxyLaunch( + Scenario scenario, + Task generation, + ConcurrentQueue phases, + Func currentState, + Action setState) + { + while (!generation.IsCompleted || !phases.IsEmpty) + { + if (generation.IsFaulted) break; + if (phases.TryDequeue(out var next)) + { + if (string.Equals(next, currentState(), StringComparison.Ordinal)) continue; + setState(next); + var elapsed = 0f; + while (elapsed < PhaseDisplaySeconds && !generation.IsFaulted) { - Debug.LogException(generation.Exception); - Launching = false; - ShowMain(); - Fade(false); - Refuse($"{scenario.Name} could not generate its galaxy", generation.Exception.GetBaseException().Message); - return; + yield return null; + elapsed += Time.unscaledDeltaTime; } - ActionGameManager.CurrentGalaxy = generation.Result; - ActionGameManager.PendingScenario = scenario; - EnterGame(); - })); + } + else + { + yield return null; + } + } + + if (generation.IsFaulted) + { + Debug.LogException(generation.Exception); + Launching = false; + ShowMain(); + Fade(false); + Refuse($"{scenario.Name} could not generate its galaxy", generation.Exception.GetBaseException().Message); + yield break; + } + + ActionGameManager.CurrentGalaxy = generation.Result; + ActionGameManager.PendingScenario = scenario; + EnterGame(); } // The one way into the game scene. Mod ship prototypes import asynchronously at boot, so the scene waits for them,