DI
This commit is contained in:
@@ -11,15 +11,13 @@ using UnityEngine.SceneManagement;
|
||||
using Logger = RebootKit.Engine.Foundation.Logger;
|
||||
|
||||
namespace RebootKit.Engine {
|
||||
|
||||
public abstract class RAsset : ScriptableObject {
|
||||
|
||||
}
|
||||
|
||||
public class RR : IDisposable {
|
||||
public static RR Shared { get; set; }
|
||||
|
||||
private static readonly Logger _logger = new Logger("RR");
|
||||
private static readonly Logger Logger = new("RR");
|
||||
|
||||
private EngineConfigAsset _engineConfigAsset;
|
||||
|
||||
@@ -28,40 +26,46 @@ namespace RebootKit.Engine {
|
||||
private WorldService _worldService;
|
||||
private GameService _gameService;
|
||||
|
||||
private DIContext _diContext;
|
||||
|
||||
public void Dispose() {
|
||||
}
|
||||
|
||||
public async UniTask Init(CancellationToken cancellationToken) {
|
||||
_logger.Info("Waking up");
|
||||
Logger.Info("Waking up");
|
||||
|
||||
_logger.Info("Loading engine config");
|
||||
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}");
|
||||
Logger.Error($"Couldn't load engine config from resources: {RConsts.EngineConfigResourcesPath}");
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.Info("Loading engine found");
|
||||
Logger.Info("Loading engine found");
|
||||
|
||||
_diContext = new DIContext();
|
||||
_diContext.AddInjector(new CVarFieldInjector());
|
||||
|
||||
await CreateCoreServices(cancellationToken);
|
||||
await RegisterServiceAssetsFromConfig(cancellationToken);
|
||||
await RegisterServicesFromMainSceneAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async UniTask Run(CancellationToken cancellationToken) {
|
||||
_logger.Info($"Starting initial game mode");
|
||||
Logger.Info($"Starting initial game mode");
|
||||
await _gameService.Start(_engineConfigAsset.StartingGameMode, cancellationToken);
|
||||
}
|
||||
|
||||
private async UniTask<TService> CreateServiceAsync<TService>(ServiceAsset<TService> asset, CancellationToken cancellationToken = default) where TService : IService {
|
||||
TService service = asset.Create();
|
||||
await ServicesLocator.Shared.RegisterAsync(service, cancellationToken);
|
||||
_diContext.Bind<TService>(service);
|
||||
_diContext.Inject(service);
|
||||
|
||||
await service.OnInit(cancellationToken);
|
||||
return service;
|
||||
}
|
||||
|
||||
private async UniTask CreateCoreServices(CancellationToken cancellationToken) {
|
||||
_logger.Debug("Registering core services");
|
||||
Logger.Debug("Registering core services");
|
||||
|
||||
_consoleService = await CreateServiceAsync(_engineConfigAsset.ConsoleService, cancellationToken);
|
||||
_inputService = await CreateServiceAsync(_engineConfigAsset.InputService, cancellationToken);
|
||||
@@ -69,38 +73,23 @@ namespace RebootKit.Engine {
|
||||
_gameService = await CreateServiceAsync(_engineConfigAsset.GameService, cancellationToken);
|
||||
}
|
||||
|
||||
private async UniTask RegisterServiceAssetsFromConfig(CancellationToken cancellationToken = default) {
|
||||
if (_engineConfigAsset.Services.Length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.Info($"Registering {_engineConfigAsset.Services.Length} services");
|
||||
foreach (ServiceAsset service in _engineConfigAsset.Services) {
|
||||
await ServicesLocator.Shared.RegisterAsync(service.Create(), cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
private async UniTask RegisterServicesFromMainSceneAsync(CancellationToken cancellationToken = default) {
|
||||
_logger.Info("Looking for services in mainscene");
|
||||
int foundCount = 0;
|
||||
|
||||
GameObject[] gameObjects = SceneManager.GetSceneByBuildIndex(RConsts.MainSceneBuildIndex).GetRootGameObjects();
|
||||
|
||||
if (gameObjects.Length == 0) {
|
||||
_logger.Info("No services found in main scene");
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (GameObject root in gameObjects) {
|
||||
ServiceMonoBehaviour[] services = root.GetComponentsInChildren<ServiceMonoBehaviour>();
|
||||
SceneDI di = root.GetComponent<SceneDI>();
|
||||
|
||||
foreach (ServiceMonoBehaviour service in services) {
|
||||
await ServicesLocator.Shared.RegisterAsync(service, cancellationToken);
|
||||
foundCount += 1;
|
||||
if (di == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
di.Install(_diContext);
|
||||
}
|
||||
|
||||
_logger.Info($"Found and registered {foundCount} services from active scene");
|
||||
await UniTask.Yield();
|
||||
}
|
||||
|
||||
//
|
||||
@@ -108,10 +97,6 @@ namespace RebootKit.Engine {
|
||||
//
|
||||
|
||||
/// Services
|
||||
public static T Service<T>() where T : IService {
|
||||
return ServicesLocator.Shared.Get<T>();
|
||||
}
|
||||
|
||||
public static ConsoleService Console() => Shared._consoleService;
|
||||
public static InputService Input() => Shared._inputService;
|
||||
public static WorldService World() => Shared._worldService;
|
||||
|
||||
Reference in New Issue
Block a user