232 lines
8.3 KiB
C#
Executable File
232 lines
8.3 KiB
C#
Executable File
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;
|
|
|
|
// RR
|
|
// Game
|
|
// GameMode
|
|
|
|
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 Logger("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 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 Camera MainCamera { get; internal set; }
|
|
|
|
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_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<GameModesService>();
|
|
|
|
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 List<GameModeAsset>();
|
|
static readonly List<WorldConfigAsset> s_WorldConfigsAssets = new List<WorldConfigAsset>();
|
|
|
|
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);
|
|
}
|
|
|
|
public static GameModeAsset GetGameMode(string name) {
|
|
if (string.IsNullOrEmpty(name)) {
|
|
throw new ArgumentException("Game mode name cannot be null or empty", nameof(name));
|
|
}
|
|
|
|
GameModeAsset gameMode = s_GameModesAssets.Find(asset => asset.name.Equals(name, StringComparison.Ordinal));
|
|
if (!gameMode) {
|
|
throw new KeyNotFoundException($"Game mode '{name}' not found");
|
|
}
|
|
|
|
return gameMode;
|
|
}
|
|
|
|
public static WorldConfigAsset GetWorldConfigAsset(string name) {
|
|
if (string.IsNullOrEmpty(name)) {
|
|
throw new ArgumentException("World config name cannot be null or empty", nameof(name));
|
|
}
|
|
|
|
WorldConfigAsset worldConfig = s_WorldConfigsAssets.Find(asset => asset.Config.name.Equals(name, StringComparison.Ordinal));
|
|
if (!worldConfig) {
|
|
throw new KeyNotFoundException($"World config '{name}' not found");
|
|
}
|
|
|
|
return worldConfig;
|
|
}
|
|
|
|
// 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 {
|
|
if (asset is null) {
|
|
throw new ArgumentNullException($"Null asset of type {typeof(TService)}");
|
|
}
|
|
|
|
TService service = asset.Create();
|
|
s_servicesBag.Add(service);
|
|
return service;
|
|
}
|
|
|
|
public static TService CreateService<TService>() where TService : class, IService {
|
|
TService service = Activator.CreateInstance<TService>();
|
|
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;
|
|
}
|
|
}
|
|
} |