using System;
using System.Threading.Tasks;
using UnityEngine;
using VContainer;
using Vanguard.Api;
using Vanguard.Auth;
using Vanguard.Meta;
using Vanguard.State;
using Vanguard.UI;
namespace Vanguard.App
{
///
/// Composition root клиента. Создаётся из runtime-инициализатора, поэтому
/// не зависит от содержимого сцены; сцены появятся позже.
///
public sealed class VanguardApp : MonoBehaviour
{
public static string LastInitializationError { get; private set; }
private IObjectResolver _container;
private AppRootView _root;
private void Awake()
{
try
{
DontDestroyOnLoad(gameObject);
Application.targetFrameRate = 60;
var config = new AppConfig();
config.Load();
var builder = new ContainerBuilder();
builder.RegisterInstance(config);
builder.Register(Lifetime.Singleton);
builder.Register(Lifetime.Singleton);
builder.Register(Lifetime.Singleton);
builder.Register(Lifetime.Singleton);
builder.Register(Lifetime.Singleton);
builder.Register(Lifetime.Singleton);
_container = builder.Build();
_root = _container.Resolve();
_root.Initialize();
_ = RestoreSessionAsync();
}
catch (Exception exception)
{
LastInitializationError = exception.ToString();
Debug.LogException(exception);
}
}
private async Task RestoreSessionAsync()
{
var auth = _container.Resolve();
try
{
if (await auth.TryRestoreAsync())
{
_root.ShowAfterLogin();
return;
}
}
catch (Exception exception)
{
Debug.LogException(exception);
}
_root.ShowLogin("Введите данные игрока.");
}
private void Update()
{
_root?.Tick();
}
private void OnDestroy()
{
if (_container is IDisposable disposable)
disposable.Dispose();
}
}
}