71 lines
2.0 KiB
C#
71 lines
2.0 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
using VContainer;
|
|
using Vanguard.Api;
|
|
using Vanguard.Auth;
|
|
using Vanguard.State;
|
|
using Vanguard.UI;
|
|
|
|
namespace Vanguard.App
|
|
{
|
|
/// <summary>
|
|
/// Composition root клиента. Создаётся из runtime-инициализатора, поэтому
|
|
/// не зависит от содержимого сцены; сцены появятся позже.
|
|
/// </summary>
|
|
public sealed class VanguardApp : MonoBehaviour
|
|
{
|
|
private IObjectResolver _container;
|
|
private AppRootView _root;
|
|
|
|
private void Awake()
|
|
{
|
|
DontDestroyOnLoad(gameObject);
|
|
Application.targetFrameRate = 60;
|
|
|
|
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<AppRootView>(Lifetime.Singleton);
|
|
|
|
_container = builder.Build();
|
|
_root = _container.Resolve<AppRootView>();
|
|
_root.Initialize();
|
|
|
|
_ = RestoreSessionAsync();
|
|
}
|
|
|
|
private async Task RestoreSessionAsync()
|
|
{
|
|
var auth = _container.Resolve<AuthService>();
|
|
|
|
try
|
|
{
|
|
if (await auth.TryRestoreAsync())
|
|
{
|
|
_root.ShowMeta();
|
|
return;
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Debug.LogException(exception);
|
|
}
|
|
|
|
_root.ShowLogin("Введите данные игрока.");
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (_container is IDisposable disposable)
|
|
disposable.Dispose();
|
|
}
|
|
}
|
|
}
|