Platform should be initialized in the Boot, before everything else. Got rid of the coroutines

This commit is contained in:
Sebastian Bularca
2026-06-18 23:53:51 +02:00
parent 5517fc17a2
commit 7cb7089d2f
9 changed files with 71 additions and 79 deletions

View File

@@ -1,96 +1,58 @@
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Nox.Game;
using Nox.Game.UI;
using Nox.Input;
using Nox.Platform;
using Jovian.SaveSystem;
using Jovian.InGameLogging;
using Nox.EditorCode;
using Unity.Profiling;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
namespace Nox.Core {
/// <summary>
/// The main boot class which is initializing the project, loads the platforms settings, initializes the platforms and creates the game states
/// </summary>
public class EntryPoint : MonoBehaviour {
public AssetReferenceT<InitializerSettingsFile> initializerSettingsFile;
private IPlatform platform;
private GameDataState gameDataState;
private Dictionary<GameState, IGameState> applicationStates;
private ProfilerMarker createApplicationStateMarker = new ProfilerMarker("createApplicationState");
private ISceneTransition sceneTransition;
private ISaveSystem saveSystem;
private IGameLogStore gameLogStore;
private InitializerSettingsFile initializerSettings;
private BootstrapReferences bootstrapReferences;
private MenuGameStateData menuGameStateData;
public IEnumerator Start() {
public async Task StartGame(GameDataState gameDataState, InitializerSettingsFile initializerSettings, IPlatform selectedPlatform) {
DontDestroyOnLoad(this);
// scene authoring is helping with making each scene stand-alone playable
var activeSceneReference = FindFirstObjectByType<SceneReference>();
if(activeSceneReference == null) {
if(!activeSceneReference) {
Debug.LogWarning("The scene has no SceneReference script. Will start by default in Splash Application State");
var sceneRef = new GameObject {
name = "Scene Reference"
};
activeSceneReference = sceneRef.AddComponent<SceneReference>();
activeSceneReference.gameState = GameState.BootState;
yield return null;
await Task.Yield();
}
// Initialize platform
gameDataState = new GameDataState {
platformSelector = new PlatformSelector(PlatformSelector.GetDevicePlatform(), PlatformSelector.GetPlatformDefaultInputMode()),
};
gameDataState.ChangeGameState(activeSceneReference.gameState);
// Load Initialization Settings
var initSettingsHandle = Addressables.LoadAssetAsync<InitializerSettingsFile>(initializerSettingsFile);
yield return new WaitUntil(() => initSettingsHandle.IsDone);
initializerSettings = initSettingsHandle.Result;
yield return CreatePlatformFactory();
var bootStrapsSettingsHandle = Addressables.LoadAssetAsync<BootstrapReferences>(initializerSettings.bootstrapSettings);
yield return new WaitUntil(() => bootStrapsSettingsHandle.IsDone);
await bootStrapsSettingsHandle.Task;
bootstrapReferences = bootStrapsSettingsHandle.Result;
menuGameStateData = new MenuGameStateData();
var fadeObject = new GameObject("ScreenFadeTransition");
sceneTransition = fadeObject.AddComponent<ScreenFadeTransition>();
CreateApplicationStates();
CreateApplicationStates(gameDataState, selectedPlatform);
var gameStateRunner = gameObject.AddComponent<GameStateRunner>();
gameStateRunner.Initialize(applicationStates, gameDataState, platform, sceneTransition);
gameStateRunner.Initialize(applicationStates, gameDataState, selectedPlatform, sceneTransition);
}
private IEnumerator CreatePlatformFactory() {
platform = null;
var devicePlatform = gameDataState.platformSelector.devicePlatform;
platform = devicePlatform switch {
DevicePlatform.Desktop => new DesktopPlatform(initializerSettings.desktopPlatformSettings),
DevicePlatform.UnityEditor => new UnityEditorPlatform(initializerSettings.unityEditorPlatformSettings),
_ => platform
};
if(!Equals(gameDataState.platformSelector.devicePlatform, DevicePlatform.Desktop) &&
PlatformSelector.GetPlatformDefaultInputMode() == InputMode.Desktop) {
platform = new DesktopPlatform(initializerSettings.desktopPlatformSettings);
}
yield return platform?.Initialize(gameDataState);
}
private void CreateApplicationStates() {
createApplicationStateMarker.Begin();
private void CreateApplicationStates(GameDataState gameDataState, IPlatform selectedPlatform) {
// Save System
var saveSettings = SaveSystemSettings.Load();
ISaveSerializer saveSerializer = saveSettings.saveFormat == SaveFormat.Json
@@ -104,12 +66,10 @@ namespace Nox.Core {
var adventureData = new AdventureData();
applicationStates = new Dictionary<GameState, IGameState> {
[GameState.BootState] = new SplashGameState(bootstrapReferences, gameDataState),
[GameState.MainMenu] = new MainMenuGameState(gameDataState, menuGameStateData, bootstrapReferences, saveSystem, adventureData, gameLogStore),
[GameState.GameMode] = new GameModeGameState(gameDataState, bootstrapReferences, platform.PlatformSettings, saveSystem, sceneTransition, adventureData, gameLogStore),
};
createApplicationStateMarker.End();
applicationStates = new Dictionary<GameState, IGameState>();
applicationStates.TryAdd(GameState.BootState, new SplashGameState(bootstrapReferences, gameDataState));
applicationStates.TryAdd(GameState.MainMenu, new MainMenuGameState(gameDataState, menuGameStateData, bootstrapReferences, saveSystem, adventureData, gameLogStore));
applicationStates.TryAdd(GameState.GameMode, new GameModeGameState(gameDataState, bootstrapReferences, selectedPlatform.PlatformSettings, saveSystem, sceneTransition, adventureData, gameLogStore));
}
}
}