Queue galaxy generation phase labels in MainMenu UI without delaying generation

This commit is contained in:
soul 2026-12-04 12:33:13 +01:00
commit 84c6980f8b

View file

@ -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<string>();
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<Galaxy> generation,
ConcurrentQueue<string> phases,
Func<string> currentState,
Action<string> 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,