using System.Threading.Tasks; using asap.core; using Game; using GameCore; using UnityEngine; namespace game { public class QuitAct : AGameAct { [Inject] public IConfig config { get; set; } [Inject] public IRTService rtService { get; set; } [Inject] public IUserService userService { get; set; } [Inject] public SoundMgr soundMgr { get; set; } [Inject] public IAdsService adsService { get; set; } #if AGG [Inject] public HeartBeat heartBeat {get; set;} #endif protected override void OnDestroy() { } public string[] DestoryRoot; public override async Task StartAsync() { #if UNITY_EDITOR && !SERVICE_HOST if(game.PlayerDataStorage.PlayerKVGameplayLoop.Enabled) _=game.PlayerDataStorage.PlayerKVGameplayLoop.CloseAsync(); #endif if(userService is PlayerDataStorage.IKVUserLifecycle closingKVUser) _=closingKVUser.CloseKVForValidationAsync(); // Invalidate identity before any await can allow old login/HTTP callbacks to resume. if (QuitStage.Logout) { QuitStage.Logout = false; try { userService.Logout(); } catch (System.Exception ex) { Debug.LogException(ex); } } LoadingScreen.Hide(); GameObject.Destroy(UIManager.Instance.gameObject); config.Rest(); try { await rtService.ReleaseAsync(); } catch (System.Exception ex) { Debug.LogException(ex); } // Observation is bounded; the process registry still owns any unfinished close. #if UNITY_EDITOR && !SERVICE_HOST if(game.PlayerDataStorage.PlayerKVGameplayLoop.Enabled) await PlayerDataStorage.PlayerKVShutdownRegistry.Process.ObserveAsync(5000); else #endif if(userService is PlayerDataStorage.IKVUserLifecycle kvUser) await kvUser.ObserveKVQuitAsync(); adsService.Release(); GameEventMgr.Instance.Clear(); #if AGG heartBeat?.Dispose(); #endif GameCore.Timer.CancelAllRegisteredTimers(); GameCore.Timer.Destroy(); soundMgr.Release(); RedPointManager.Instance.Init(); return await base.StartAsync(); } protected override void OnStarted() { RootCtx.DestoryRootCtx(); for (int i = 0; i < DestoryRoot.Length; i++) { GameObject go = GameObject.Find(DestoryRoot[i]); if (go != null) { GameObject.DestroyImmediate(go); } } base.OnStarted(); #if UNITY_EDITOR && !SERVICE_HOST if (game.PlayerDataStorage.PlayerKVGameplayLoop.Enabled) { var status = PlayerDataStorage.PlayerKVShutdownRegistry.Process.Status; Debug.Log("KV loop ended; restart Unity before another run. Shutdown: " + status); if (status == PlayerDataStorage.PlayerKVShutdownStatus.Ready) UnityEditor.EditorApplication.isPlaying = false; return; // Keep the process barrier; never return this run to PreBoot. } #endif Invoke("Unload", 2f); } } }