79 lines
2.5 KiB
C#
79 lines
2.5 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using VContainer;
|
|
using Vanguard.Api;
|
|
using Vanguard.Auth;
|
|
using Vanguard.Meta;
|
|
using Vanguard.State;
|
|
using Vanguard.UI;
|
|
|
|
namespace Vanguard.App
|
|
{
|
|
/// <summary>
|
|
/// Composition root клиента. Живёт между сценами, хранит DI-контейнер и
|
|
/// общие сервисы; сцены отвечают только за 3D-вид и свой UI.
|
|
/// </summary>
|
|
public sealed class VanguardApp : MonoBehaviour
|
|
{
|
|
public static VanguardApp Instance { get; private set; }
|
|
public static string LastInitializationError { get; private set; }
|
|
|
|
private IObjectResolver _container;
|
|
|
|
public IObjectResolver Container => _container;
|
|
public SceneFlow Flow { get; private set; }
|
|
|
|
private void Awake()
|
|
{
|
|
try
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
Application.targetFrameRate = 60;
|
|
if (!Application.isEditor)
|
|
Screen.SetResolution(1920, 1080, FullScreenMode.FullScreenWindow);
|
|
Loc.Initialize();
|
|
|
|
var config = new AppConfig();
|
|
config.Load();
|
|
|
|
var builder = new ContainerBuilder();
|
|
builder.RegisterInstance(config);
|
|
builder.Register<TokenStore>(Lifetime.Singleton);
|
|
builder.Register<PlayerState>(Lifetime.Singleton);
|
|
builder.Register<ApiClient>(Lifetime.Singleton);
|
|
builder.Register<AuthService>(Lifetime.Singleton);
|
|
builder.Register<MetaService>(Lifetime.Singleton);
|
|
builder.Register<SceneFlow>(Lifetime.Singleton);
|
|
|
|
_container = builder.Build();
|
|
Flow = _container.Resolve<SceneFlow>();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LastInitializationError = exception.ToString();
|
|
Debug.LogException(exception);
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (Flow != null
|
|
&& UnityEngine.SceneManagement.SceneManager.GetActiveScene().name
|
|
!= SceneFlow.PlaymatSandbox)
|
|
{
|
|
_ = Flow.LoadLoginAsync();
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (Instance == this)
|
|
Instance = null;
|
|
|
|
if (_container is IDisposable disposable)
|
|
disposable.Dispose();
|
|
}
|
|
}
|
|
}
|