Files
RebootKit/Runtime/Engine/Code/Foundation/EntryPoint.cs
2025-04-14 23:22:38 +02:00

68 lines
2.4 KiB
C#

using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace RebootKit.Engine.Foundation {
public static class EntryPoint {
static readonly Logger s_logger = new(nameof(EntryPoint));
static CancellationTokenSource s_cancellationTokenSource;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
public static void Start() {
if (s_cancellationTokenSource != null) {
s_cancellationTokenSource.Cancel();
}
RR.s_Shared = null;
// unload all scenes
s_cancellationTokenSource = new CancellationTokenSource();
RunAsync(s_cancellationTokenSource.Token).Forget();
#if UNITY_EDITOR
static void OnPlayerModeState(PlayModeStateChange state) {
if (state == PlayModeStateChange.ExitingPlayMode) {
s_cancellationTokenSource.Cancel();
}
}
EditorApplication.playModeStateChanged -= OnPlayerModeState;
EditorApplication.playModeStateChanged += OnPlayerModeState;
#endif
}
static async UniTask RunAsync(CancellationToken cancellationToken) {
s_logger.Info("Loading boot scene");
SceneManager.LoadScene(RConsts.k_BootSceneBuildIndex, LoadSceneMode.Single);
s_logger.Info("Loading engine config");
EngineConfigAsset configAsset = Resources.Load<EngineConfigAsset>(RConsts.k_EngineConfigResourcesPath);
if (configAsset == null) {
s_logger.Error($"Couldn't load engine config from resources: {RConsts.k_EngineConfigResourcesPath}");
return;
}
if (!configAsset.initializeOnLoad) {
return;
}
using RR instance = new();
RR.s_Shared = instance;
s_logger.Info("Initializing RR");
await instance.Init(configAsset, cancellationToken);
s_logger.Info("Loading main scene");
await SceneManager.LoadSceneAsync(RConsts.k_MainSceneBuildIndex, LoadSceneMode.Single).ToUniTask(cancellationToken: cancellationToken);
s_logger.Info("Starting RR");
instance.Run();
await UniTask.WaitUntilCanceled(Application.exitCancellationToken);
}
}
}