mirror of
https://github.com/Bragin-Stepan/project-entity.git
synced 2026-03-05 07:41:10 +00:00
init: add project
This commit is contained in:
8
Assets/_Project/Develop/Runtime/Infrastructure/DI.meta
Normal file
8
Assets/_Project/Develop/Runtime/Infrastructure/DI.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1e4eaab4c72c3874d9d069afed49278e
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Assets._Project.Develop.Runtime.Infrastructure.DI
|
||||
{
|
||||
public class DIContainer
|
||||
{
|
||||
private readonly Dictionary<Type, Registration> _container = new();
|
||||
|
||||
private readonly List<Type> _requests = new();
|
||||
|
||||
private readonly DIContainer _parent;
|
||||
|
||||
public DIContainer() { }
|
||||
|
||||
public DIContainer(DIContainer parent) => _parent = parent;
|
||||
|
||||
public IRegistrationOptions RegisterAsSingle<T>(Func<DIContainer, T> creator)
|
||||
{
|
||||
if (IsAlreadyRegister<T>())
|
||||
throw new InvalidOperationException($"{typeof(T)} already register");
|
||||
|
||||
Registration registration = new Registration(container => creator.Invoke(container));
|
||||
_container.Add(typeof(T), registration);
|
||||
return registration;
|
||||
}
|
||||
|
||||
public bool IsAlreadyRegister<T>()
|
||||
{
|
||||
if (_container.ContainsKey(typeof(T)))
|
||||
return true;
|
||||
|
||||
if (_parent != null)
|
||||
return _parent.IsAlreadyRegister<T>();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public T Resolve<T>()
|
||||
{
|
||||
if (_requests.Contains(typeof(T)))
|
||||
throw new InvalidOperationException($"Cycle resolve for {typeof(T)}");
|
||||
|
||||
_requests.Add(typeof(T));
|
||||
|
||||
try
|
||||
{
|
||||
if (_container.TryGetValue(typeof(T), out Registration registration))
|
||||
return (T)registration.CreateInstanceFrom(this);
|
||||
|
||||
if (_parent != null)
|
||||
return _parent.Resolve<T>();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_requests.Remove(typeof(T));
|
||||
}
|
||||
|
||||
throw new InvalidOperationException($"Registration for {typeof(T)} not exists");
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
foreach (Registration registration in _container.Values)
|
||||
{
|
||||
if (registration.IsNonLazy)
|
||||
registration.CreateInstanceFrom(this);
|
||||
|
||||
registration.OnInitialize();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (Registration registration in _container.Values)
|
||||
registration.OnDispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8344455a6210cd942a42f35b05dd49b6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Assets._Project.Develop.Runtime.Infrastructure.DI
|
||||
{
|
||||
public interface IInitializable
|
||||
{
|
||||
void Initialize();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d11727eaee46452e981b458b50cf78ff
|
||||
timeCreated: 1770823156
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Assets._Project.Develop.Runtime.Infrastructure.DI
|
||||
{
|
||||
public interface IRegistrationOptions
|
||||
{
|
||||
void NonLazy();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b9015cc75cdb4bb4b853d2da12436925
|
||||
timeCreated: 1770823170
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
|
||||
namespace Assets._Project.Develop.Runtime.Infrastructure.DI
|
||||
{
|
||||
public class Registration : IRegistrationOptions
|
||||
{
|
||||
private Func<DIContainer, object> _creator;
|
||||
private object _cachedInstance;
|
||||
|
||||
public bool IsNonLazy { get; private set; }
|
||||
|
||||
public Registration(Func<DIContainer, object> creator) => _creator = creator;
|
||||
|
||||
public object CreateInstanceFrom(DIContainer container)
|
||||
{
|
||||
if (_cachedInstance != null)
|
||||
return _cachedInstance;
|
||||
|
||||
if (_creator == null)
|
||||
throw new InvalidOperationException("Not has instance or creator");
|
||||
|
||||
_cachedInstance = _creator.Invoke(container);
|
||||
|
||||
return _cachedInstance;
|
||||
}
|
||||
|
||||
public void OnInitialize()
|
||||
{
|
||||
if (_cachedInstance != null)
|
||||
if (_cachedInstance is IInitializable initializable)
|
||||
initializable.Initialize();
|
||||
}
|
||||
|
||||
public void OnDispose()
|
||||
{
|
||||
if (_cachedInstance != null)
|
||||
if (_cachedInstance is IDisposable disposable)
|
||||
disposable.Dispose();
|
||||
}
|
||||
|
||||
public void NonLazy() => IsNonLazy = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 98360bf9a58f7d64885a7620c49a3692
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b3ad35e69ad17294cab44a1eca18420f
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,54 @@
|
||||
using Assets._Project.Develop.Runtime.Infrastructure.DI;
|
||||
using Assets._Project.Develop.Runtime.Utilities.ConfigsManagement;
|
||||
using Assets._Project.Develop.Runtime.Utilities.CoroutinesManagement;
|
||||
using Assets._Project.Develop.Runtime.Utilities.LoadingScreen;
|
||||
using Assets._Project.Develop.Runtime.Utilities.SceneManagement;
|
||||
using System.Collections;
|
||||
using _Project.Develop.Runtime.Utilities.DataManagement;
|
||||
using Assets._Project.Develop.Runtime.Gameplay.Infrastructure;
|
||||
using Assets._Project.Develop.Runtime.Utilities.DataManagement.DataProviders;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Assets._Project.Develop.Runtime.Infrastructure.EntryPoint
|
||||
{
|
||||
public class GameEntryPoint : MonoBehaviour
|
||||
{
|
||||
private void Awake()
|
||||
{
|
||||
SetupAppSettings();
|
||||
|
||||
DIContainer projectContainer = new();
|
||||
ProjectContextRegistrations.Process(projectContainer);
|
||||
|
||||
projectContainer.Initialize();
|
||||
projectContainer.Resolve<ICoroutinesPerformer>().StartPerform(Initialize(projectContainer));
|
||||
}
|
||||
|
||||
private void SetupAppSettings()
|
||||
{
|
||||
QualitySettings.vSyncCount = 0;
|
||||
Application.targetFrameRate = 60;
|
||||
}
|
||||
|
||||
private IEnumerator Initialize(DIContainer container)
|
||||
{
|
||||
ILoadingScreen loadingScreen = container.Resolve<ILoadingScreen>();
|
||||
SceneSwitcherService sceneSwitcherService = container.Resolve<SceneSwitcherService>();
|
||||
PlayerDataProvider playerDataProvider = container.Resolve<PlayerDataProvider>();
|
||||
|
||||
loadingScreen.Show();
|
||||
|
||||
yield return container.Resolve<ConfigsProviderService>().LoadAsync();
|
||||
|
||||
yield return DataUtils.LoadProviderAsync(playerDataProvider);
|
||||
// yield return DataUtils.LoadProviderAsync(SettingsDataProvider);
|
||||
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
|
||||
loadingScreen.Hide();
|
||||
|
||||
yield return sceneSwitcherService.ProcessSwitchTo(Scenes.Gameplay, new GameplayInputArgs(1));
|
||||
// yield return sceneSwitcherService.ProcessSwitchTo(Scenes.MainMenu);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c0684ffe977bb3f47ad179694b59b26a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,112 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using _Project.Develop.Runtime.Logic.Meta.Features.Wallet;
|
||||
using _Project.Develop.Runtime.UI;
|
||||
using _Project.Develop.Runtime.UI.Core;
|
||||
using _Project.Develop.Runtime.Utilities.InputManagement;
|
||||
using _Project.Develop.Runtime.Utils.ReactiveManagement;
|
||||
using Assets._Project.Develop.Runtime.Infrastructure.DI;
|
||||
using Assets._Project.Develop.Runtime.Meta.Features.Wallet;
|
||||
using Assets._Project.Develop.Runtime.Utilities.AssetsManagement;
|
||||
using Assets._Project.Develop.Runtime.Utilities.ConfigsManagement;
|
||||
using Assets._Project.Develop.Runtime.Utilities.CoroutinesManagement;
|
||||
using Assets._Project.Develop.Runtime.Utilities.DataManagement;
|
||||
using Assets._Project.Develop.Runtime.Utilities.DataManagement.DataProviders;
|
||||
using Assets._Project.Develop.Runtime.Utilities.LoadingScreen;
|
||||
using Assets._Project.Develop.Runtime.Utilities.SceneManagement;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace Assets._Project.Develop.Runtime.Infrastructure.EntryPoint
|
||||
{
|
||||
public class ProjectContextRegistrations
|
||||
{
|
||||
public static void Process(DIContainer container)
|
||||
{
|
||||
container.RegisterAsSingle<ICoroutinesPerformer>(CreateCoroutinesPerformer);
|
||||
container.RegisterAsSingle(CreateConfigsProviderService);
|
||||
container.RegisterAsSingle(CreateResourcesAssetsLoader);
|
||||
container.RegisterAsSingle(CreateSceneLoaderService);
|
||||
container.RegisterAsSingle(CreateSceneSwitcherService);
|
||||
|
||||
container.RegisterAsSingle<IPlayerInputService>(CreateDesktopPlayerInputService);
|
||||
|
||||
container.RegisterAsSingle(CreateSaveLoadFactory);
|
||||
container.RegisterAsSingle<ISaveLoadService>(CreateSaveLoadService);
|
||||
container.RegisterAsSingle(CreatePlayerDataProvider);
|
||||
|
||||
container.RegisterAsSingle(CreateWalletService).NonLazy();
|
||||
|
||||
container.RegisterAsSingle(CreateProjectPresentersFactory);
|
||||
container.RegisterAsSingle(CreateViewsFactory);
|
||||
container.RegisterAsSingle<ILoadingScreen>(CreateLoadingScreen);
|
||||
}
|
||||
|
||||
private static ConfigsProviderService CreateConfigsProviderService(DIContainer c)
|
||||
{
|
||||
ResourcesAssetsLoader resourcesAssetsLoader = c.Resolve<ResourcesAssetsLoader>();
|
||||
ResourcesConfigsLoader resourcesConfigsLoader = new(resourcesAssetsLoader);
|
||||
|
||||
return new ConfigsProviderService(resourcesConfigsLoader);
|
||||
}
|
||||
|
||||
private static SceneSwitcherService CreateSceneSwitcherService(DIContainer c)
|
||||
=> new(
|
||||
c.Resolve<SceneLoaderService>(),
|
||||
c.Resolve<ILoadingScreen>(),
|
||||
c);
|
||||
|
||||
private static SceneLoaderService CreateSceneLoaderService(DIContainer c)
|
||||
=> new();
|
||||
|
||||
private static ResourcesAssetsLoader CreateResourcesAssetsLoader(DIContainer c)
|
||||
=> new();
|
||||
|
||||
private static DesktopPlayerInputService CreateDesktopPlayerInputService(DIContainer c)
|
||||
=> new();
|
||||
|
||||
private static SaveLoadFactory CreateSaveLoadFactory(DIContainer c)
|
||||
=> new();
|
||||
|
||||
private static PlayerDataProvider CreatePlayerDataProvider(DIContainer c)
|
||||
=> new(c.Resolve<ISaveLoadService>(), c.Resolve<ConfigsProviderService>());
|
||||
|
||||
private static ProjectPresentersFactory CreateProjectPresentersFactory(DIContainer c)
|
||||
=> new(c);
|
||||
|
||||
private static ViewsFactory CreateViewsFactory(DIContainer c)
|
||||
=> new(c.Resolve<ResourcesAssetsLoader>());
|
||||
|
||||
private static SaveLoadService CreateSaveLoadService(DIContainer c)
|
||||
=> c.Resolve<SaveLoadFactory>().CreateDefaultSaveLoad();
|
||||
|
||||
private static CoroutinesPerformer CreateCoroutinesPerformer(DIContainer c)
|
||||
{
|
||||
ResourcesAssetsLoader resourcesAssetsLoader = c.Resolve<ResourcesAssetsLoader>();
|
||||
|
||||
CoroutinesPerformer coroutinesPerformerPrefab = resourcesAssetsLoader
|
||||
.Load<CoroutinesPerformer>(PathToResources.Util.Coroutine);
|
||||
|
||||
return Object.Instantiate(coroutinesPerformerPrefab);
|
||||
}
|
||||
|
||||
private static StandardLoadingScreen CreateLoadingScreen(DIContainer c)
|
||||
{
|
||||
ResourcesAssetsLoader resourcesAssetsLoader = c.Resolve<ResourcesAssetsLoader>();
|
||||
|
||||
StandardLoadingScreen standardLoadingScreenPrefab = resourcesAssetsLoader
|
||||
.Load<StandardLoadingScreen>(PathToResources.UI.LoadingScreen.Standard);
|
||||
|
||||
return Object.Instantiate(standardLoadingScreenPrefab);
|
||||
}
|
||||
|
||||
private static WalletService CreateWalletService(DIContainer c)
|
||||
{
|
||||
Dictionary<CurrencyTypes, ReactiveVariable<int>> currencies = new();
|
||||
|
||||
foreach (CurrencyTypes currencyType in Enum.GetValues(typeof(CurrencyTypes)))
|
||||
currencies[currencyType] = new ReactiveVariable<int>();
|
||||
|
||||
return new WalletService(currencies, c.Resolve<PlayerDataProvider>());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b9d1afe3e0d64ea49851f529d43b4c01
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,16 @@
|
||||
using Assets._Project.Develop.Runtime.Infrastructure.DI;
|
||||
using Assets._Project.Develop.Runtime.Utilities.SceneManagement;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Assets._Project.Develop.Runtime.Infrastructure
|
||||
{
|
||||
public abstract class SceneBootstrap : MonoBehaviour
|
||||
{
|
||||
public abstract void ProcessRegistrations(DIContainer container, IInputSceneArgs sceneArgs = null);
|
||||
|
||||
public abstract IEnumerator Initialize();
|
||||
|
||||
public abstract void Run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 063bc94e968610448a1faa8735795bb1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user