refactor
This commit is contained in:
66
Runtime/Engine/Code/Main/EntryPoint.cs
Normal file
66
Runtime/Engine/Code/Main/EntryPoint.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using RebootKit.Engine.Foundation;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using Logger = RebootKit.Engine.Foundation.Logger;
|
||||
|
||||
namespace RebootKit.Engine.Main {
|
||||
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();
|
||||
}
|
||||
|
||||
s_cancellationTokenSource = new CancellationTokenSource();
|
||||
RunAsync(s_cancellationTokenSource.Token).Forget();
|
||||
|
||||
#if UNITY_EDITOR
|
||||
static void OnPlayerModeState(UnityEditor.PlayModeStateChange state) {
|
||||
if (state == UnityEditor.PlayModeStateChange.ExitingPlayMode) {
|
||||
s_cancellationTokenSource.Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
UnityEditor.EditorApplication.playModeStateChanged -= OnPlayerModeState;
|
||||
UnityEditor.EditorApplication.playModeStateChanged += OnPlayerModeState;
|
||||
#endif
|
||||
}
|
||||
|
||||
static async UniTask RunAsync(CancellationToken cancellationToken) {
|
||||
ConfigVarsContainer.Init();
|
||||
|
||||
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 is null) {
|
||||
s_logger.Error($"Couldn't load engine config from resources: {RConsts.k_EngineConfigResourcesPath}");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!configAsset.initializeOnLoad) {
|
||||
return;
|
||||
}
|
||||
|
||||
s_logger.Info("Initializing RR");
|
||||
await RR.InitAsync(configAsset, cancellationToken);
|
||||
|
||||
s_logger.Info("Loading main scene");
|
||||
await SceneManager.LoadSceneAsync(RConsts.k_MainSceneBuildIndex, LoadSceneMode.Single).ToUniTask(cancellationToken: cancellationToken);
|
||||
|
||||
s_logger.Info("Starting RR");
|
||||
RR.Run();
|
||||
|
||||
await UniTask.WaitUntilCanceled(Application.exitCancellationToken);
|
||||
|
||||
RR.Shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Runtime/Engine/Code/Main/EntryPoint.cs.meta
Normal file
3
Runtime/Engine/Code/Main/EntryPoint.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 83bc8a98e30147babef3641863699cd9
|
||||
timeCreated: 1740670775
|
||||
201
Runtime/Engine/Code/Main/RR.cs
Normal file
201
Runtime/Engine/Code/Main/RR.cs
Normal file
@@ -0,0 +1,201 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using R3;
|
||||
using RebootKit.Engine.Foundation;
|
||||
using RebootKit.Engine.Services.Console;
|
||||
using RebootKit.Engine.Services.GameMode;
|
||||
using RebootKit.Engine.Services.Input;
|
||||
using RebootKit.Engine.Services.Simulation;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using Assert = UnityEngine.Assertions.Assert;
|
||||
using Logger = RebootKit.Engine.Foundation.Logger;
|
||||
|
||||
namespace RebootKit.Engine.Main {
|
||||
public interface IGame : IDisposable {
|
||||
UniTask InitAsync(CancellationToken cancellationToken);
|
||||
void Run();
|
||||
}
|
||||
|
||||
public abstract class GameAsset : ScriptableObject {
|
||||
public abstract IGame CreateGame();
|
||||
}
|
||||
|
||||
public static class RR {
|
||||
static readonly Logger s_logger = new("RR");
|
||||
|
||||
[ConfigVar("con.write_log", 1, "Enables writing game log to console output")]
|
||||
static ConfigVar s_writeLogToConsole;
|
||||
|
||||
static EngineConfigAsset s_engineConfigAsset;
|
||||
|
||||
static DisposableBag s_disposableBag;
|
||||
static DisposableBag s_servicesBag;
|
||||
static DIContext s_diContext;
|
||||
static ConsoleService s_consoleService;
|
||||
|
||||
static GameModesService s_gameModesService;
|
||||
static InputService s_inputService;
|
||||
static WorldService s_worldService;
|
||||
|
||||
public static ConsoleService Console => s_consoleService;
|
||||
public static InputService Input => s_inputService;
|
||||
public static WorldService World => s_worldService;
|
||||
public static GameModesService GameModes => s_gameModesService;
|
||||
public static DIContext DIContext => s_diContext;
|
||||
|
||||
static IGame s_game;
|
||||
|
||||
public static async UniTask InitAsync(EngineConfigAsset configAsset, CancellationToken cancellationToken) {
|
||||
Assert.IsNotNull(configAsset, "Config asset is required");
|
||||
Assert.IsNotNull(configAsset.gameAsset, "Game asset is required");
|
||||
|
||||
s_engineConfigAsset = configAsset;
|
||||
|
||||
s_logger.Info("Initializing");
|
||||
s_servicesBag = new DisposableBag();
|
||||
s_disposableBag = new DisposableBag();
|
||||
s_diContext = new DIContext();
|
||||
|
||||
s_logger.Debug("Registering core services");
|
||||
s_consoleService = CreateService(s_engineConfigAsset.coreServices.consoleService);
|
||||
s_inputService = CreateService(s_engineConfigAsset.coreServices.inputService);
|
||||
s_worldService = CreateService(s_engineConfigAsset.coreServices.worldService);
|
||||
s_gameModesService = CreateService(s_engineConfigAsset.coreServices.gameService);
|
||||
|
||||
await InitializeAssetsAsync(cancellationToken);
|
||||
|
||||
s_logger.Debug("Creating game");
|
||||
s_game = s_engineConfigAsset.gameAsset.CreateGame();
|
||||
|
||||
await s_game.InitAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public static void Shutdown() {
|
||||
s_logger.Info("Shutting down");
|
||||
s_servicesBag.Dispose();
|
||||
s_disposableBag.Dispose();
|
||||
}
|
||||
|
||||
public static void Run() {
|
||||
s_game.Run();
|
||||
|
||||
#if UNITY_EDITOR
|
||||
string scriptContent = UnityEditor.EditorPrefs.GetString("RebootKitEditor.OnGameRunScriptContent", "");
|
||||
s_logger.Info($"Executing script: {scriptContent}");
|
||||
|
||||
if (!string.IsNullOrEmpty(scriptContent)) {
|
||||
foreach (string cmd in scriptContent.Split('\n')) {
|
||||
s_logger.Info($"Executing command: {cmd}");
|
||||
Console.Execute(cmd);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Assets API
|
||||
static readonly List<GameModeAsset> s_gameModesAssets = new();
|
||||
static readonly List<WorldConfigAsset> s_worldConfigsAssets = new();
|
||||
|
||||
public static IReadOnlyList<GameModeAsset> GameModesAssets => s_gameModesAssets;
|
||||
public static IReadOnlyList<WorldConfigAsset> WorldConfigsAssets => s_worldConfigsAssets;
|
||||
|
||||
public static async UniTask InitializeAssetsAsync(CancellationToken cancellationToken) {
|
||||
s_gameModesAssets.Clear();
|
||||
s_worldConfigsAssets.Clear();
|
||||
|
||||
s_logger.Info("Loading game assets");
|
||||
|
||||
await Addressables.LoadAssetsAsync<GameModeAsset>("game_mode", asset => { s_gameModesAssets.Add(asset); }).ToUniTask(cancellationToken: cancellationToken);
|
||||
s_logger.Info($"Loaded {s_gameModesAssets.Count} game modes");
|
||||
|
||||
await Addressables.LoadAssetsAsync<WorldConfigAsset>("world", asset => { s_worldConfigsAssets.Add(asset); }).ToUniTask(cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
// Game API
|
||||
public static void StartGameMode(GameModeAsset gameMode, WorldConfig world) {
|
||||
if (gameMode is null) {
|
||||
throw new ArgumentNullException(nameof(gameMode));
|
||||
}
|
||||
|
||||
s_logger.Info($"Starting game mode: {gameMode.name} in world: {world.name}");
|
||||
s_gameModesService.Start(gameMode, world);
|
||||
}
|
||||
|
||||
public static TGame Game<TGame>() where TGame : IGame {
|
||||
if (s_game is TGame game) {
|
||||
return game;
|
||||
}
|
||||
|
||||
throw new InvalidOperationException($"Game is not of type {typeof(TGame)}");
|
||||
}
|
||||
|
||||
// Service API
|
||||
public static TService CreateService<TService>(ServiceAsset<TService> asset) where TService : class, IService {
|
||||
TService service = asset.Create(s_diContext);
|
||||
s_diContext.Bind(service);
|
||||
s_servicesBag.Add(service);
|
||||
return service;
|
||||
}
|
||||
|
||||
public static TService CreateService<TService>() where TService : class, IService {
|
||||
TService service = s_diContext.Create<TService>();
|
||||
s_diContext.Bind(service);
|
||||
s_servicesBag.Add(service);
|
||||
return service;
|
||||
}
|
||||
|
||||
// General API
|
||||
public static void Log(string message) {
|
||||
Debug.Log(message);
|
||||
|
||||
s_consoleService?.WriteToOutput(message);
|
||||
}
|
||||
|
||||
public static void LogWarning(string message) {
|
||||
Debug.LogWarning(message);
|
||||
s_consoleService?.WriteToOutput(message);
|
||||
}
|
||||
|
||||
public static void LogError(string message) {
|
||||
Debug.LogError(message);
|
||||
s_consoleService?.WriteToOutput(message);
|
||||
}
|
||||
|
||||
// CVar API
|
||||
public static ConfigVar CVarIndex(string name, int defaultValue = -1) {
|
||||
ConfigVar cvar = ConfigVarsContainer.Get(name);
|
||||
if (cvar != null) {
|
||||
return cvar;
|
||||
}
|
||||
|
||||
cvar = new ConfigVar(name, defaultValue);
|
||||
ConfigVarsContainer.Register(cvar);
|
||||
return cvar;
|
||||
}
|
||||
|
||||
public static ConfigVar CVarNumber(string name, double defaultValue = 0) {
|
||||
ConfigVar cvar = ConfigVarsContainer.Get(name);
|
||||
if (cvar != null) {
|
||||
return cvar;
|
||||
}
|
||||
|
||||
cvar = new ConfigVar(name, defaultValue);
|
||||
ConfigVarsContainer.Register(cvar);
|
||||
return cvar;
|
||||
}
|
||||
|
||||
public static ConfigVar CVarString(string name, string defaultValue = "") {
|
||||
ConfigVar cvar = ConfigVarsContainer.Get(name);
|
||||
if (cvar != null) {
|
||||
return cvar;
|
||||
}
|
||||
|
||||
cvar = new ConfigVar(name, defaultValue);
|
||||
ConfigVarsContainer.Register(cvar);
|
||||
return cvar;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Runtime/Engine/Code/Main/RR.cs.meta
Normal file
2
Runtime/Engine/Code/Main/RR.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 56749d0dd0042ce41b20d46fc486476c
|
||||
Reference in New Issue
Block a user