145 lines
4.5 KiB
C#
145 lines
4.5 KiB
C#
using System;
|
|
using System.Threading;
|
|
using Cysharp.Threading.Tasks;
|
|
using RebootKit.Engine.Foundation;
|
|
using RebootKit.Engine.Services.Console;
|
|
using RebootKit.Engine.Services.Game;
|
|
using RebootKit.Engine.Services.Input;
|
|
using RebootKit.Engine.Services.Simulation;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using Logger = RebootKit.Engine.Foundation.Logger;
|
|
|
|
|
|
/*
|
|
* Game starts:
|
|
* - Entry point creates instance of RR
|
|
* - Core Services are installed
|
|
* - Loads main scene and installs deps
|
|
* -
|
|
*/
|
|
|
|
namespace RebootKit.Engine {
|
|
public abstract class RAsset : ScriptableObject {
|
|
}
|
|
|
|
public class RR : IDisposable {
|
|
public static RR Shared { get; set; }
|
|
|
|
private static readonly Logger Logger = new("RR");
|
|
|
|
private EngineConfigAsset _engineConfigAsset;
|
|
|
|
private ConsoleService _consoleService;
|
|
private InputService _inputService;
|
|
private WorldService _worldService;
|
|
private GameService _gameService;
|
|
|
|
private DIContext _diContext;
|
|
|
|
public void Dispose() {
|
|
}
|
|
|
|
public async UniTask Init(CancellationToken cancellationToken) {
|
|
Logger.Info("Waking up");
|
|
|
|
Logger.Info("Loading engine config");
|
|
_engineConfigAsset = Resources.Load<EngineConfigAsset>(RConsts.EngineConfigResourcesPath);
|
|
if (_engineConfigAsset == null) {
|
|
Logger.Error($"Couldn't load engine config from resources: {RConsts.EngineConfigResourcesPath}");
|
|
return;
|
|
}
|
|
|
|
Logger.Info("Loading engine found");
|
|
|
|
_diContext = new DIContext();
|
|
_diContext.AddInjector(new CVarFieldInjector());
|
|
|
|
CreateCoreServices();
|
|
InstallMainScene();
|
|
|
|
await UniTask.Yield(cancellationToken);
|
|
}
|
|
|
|
public async UniTask Run(CancellationToken cancellationToken) {
|
|
Logger.Info($"Starting initial game mode");
|
|
await _gameService.Start(_engineConfigAsset.StartingGameMode, cancellationToken);
|
|
}
|
|
|
|
private TService CreateService<TService>(ServiceAsset<TService> asset) where TService : class, IService {
|
|
TService service = asset.Create(_diContext);
|
|
_diContext.Bind(service);
|
|
return service;
|
|
}
|
|
|
|
private void CreateCoreServices() {
|
|
Logger.Debug("Registering core services");
|
|
|
|
_consoleService = CreateService(_engineConfigAsset.ConsoleService);
|
|
_inputService = CreateService(_engineConfigAsset.InputService);
|
|
_worldService = CreateService(_engineConfigAsset.WorldService);
|
|
_gameService = CreateService(_engineConfigAsset.GameService);
|
|
}
|
|
|
|
private void InstallMainScene() {
|
|
GameObject[] gameObjects = SceneManager.GetSceneByBuildIndex(RConsts.MainSceneBuildIndex).GetRootGameObjects();
|
|
if (gameObjects.Length == 0) {
|
|
return;
|
|
}
|
|
|
|
foreach (GameObject root in gameObjects) {
|
|
SceneDI di = root.GetComponent<SceneDI>();
|
|
|
|
if (di == null) {
|
|
continue;
|
|
}
|
|
|
|
di.Install(_diContext);
|
|
}
|
|
}
|
|
|
|
//
|
|
// API
|
|
//
|
|
|
|
/// Services
|
|
public static ConsoleService Console() => Shared._consoleService;
|
|
public static InputService Input() => Shared._inputService;
|
|
public static WorldService World() => Shared._worldService;
|
|
public static GameService Game() => Shared._gameService;
|
|
|
|
/// CVars
|
|
public static CVar CVarIndex(string name, int defaultValue = -1) {
|
|
CVar cvar = Console().GetCVar(name);
|
|
if (cvar != null) {
|
|
return cvar;
|
|
}
|
|
|
|
cvar = new CVar(name, defaultValue);
|
|
Console().Replace(cvar);
|
|
return cvar;
|
|
}
|
|
|
|
public static CVar CVarNumber(string name, double defaultValue = 0) {
|
|
CVar cvar = Console().GetCVar(name);
|
|
if (cvar != null) {
|
|
return cvar;
|
|
}
|
|
|
|
cvar = new CVar(name, defaultValue);
|
|
Console().Replace(cvar);
|
|
return cvar;
|
|
}
|
|
|
|
public static CVar CVarString(string name, string defaultValue = "") {
|
|
CVar cvar = Console().GetCVar(name);
|
|
if (cvar != null) {
|
|
return cvar;
|
|
}
|
|
|
|
cvar = new CVar(name, defaultValue);
|
|
Console().Replace(cvar);
|
|
return cvar;
|
|
}
|
|
}
|
|
} |