using Fie.Scene; using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; namespace Fie.Manager { public sealed class FieManagerFactory : MonoBehaviour { private FieSceneType _currentSceneType; private static FieManagerFactory instance; private Dictionary managerList = new Dictionary(); public static FieManagerFactory I { get { if (instance == null) { instance = UnityEngine.Object.FindObjectOfType(); if (instance == null) { GameObject gameObject = new GameObject(); UnityEngine.Object.DontDestroyOnLoad(gameObject); instance = gameObject.AddComponent(); gameObject.name = instance.GetType().Name; } } return instance; } } public FieSceneType currentSceneType { get { return _currentSceneType; } set { _currentSceneType = value; } } public void AddManager(FieManagerBase manager) { if (!managerList.ContainsKey(manager.GetType())) { managerList[manager.GetType()] = manager; } if (manager.transform != null) { manager.transform.parent = base.gameObject.transform; } } private void CleanupManager(FieSceneBase targertScene) { if (managerList.Count > 0) { if (targertScene != null) { FieSceneLink sceneLinkInfo = targertScene.GetSceneLinkInfo(); _currentSceneType = sceneLinkInfo.definedSceneType; } List list = new List(); foreach (KeyValuePair manager in managerList) { bool flag = false; if (manager.Value == null || manager.Value.transform == null) { flag = true; } else { FieManagerExists fieManagerExists = (FieManagerExists)Attribute.GetCustomAttribute(manager.Key, typeof(FieManagerExists)); if (fieManagerExists != null) { if ((fieManagerExists.existFlag & FieManagerExistSceneFlag.NEVER_DESTROY) != 0) { continue; } if ((fieManagerExists.existFlag & FieManagerExistSceneFlag.ANYTIME_DESTROY) != 0) { flag = true; } if (_currentSceneType == FieSceneType.INGAME && (fieManagerExists.existFlag & FieManagerExistSceneFlag.INGAME) == (FieManagerExistSceneFlag)0) { flag = true; } if (_currentSceneType == FieSceneType.OUTGAME && (fieManagerExists.existFlag & FieManagerExistSceneFlag.OUTGAME) == (FieManagerExistSceneFlag)0) { flag = true; } } } if (flag) { list.Add(manager.Key); } } foreach (Type item in list) { managerList[item].Destroy(); managerList.Remove(item); } } } public void StartUp() { FieManagerBehaviour.I.StartUp(); FieManagerBehaviour.I.StartUp(); FieManagerBehaviour.I.StartUp(); FieManagerBehaviour.I.StartUp(); FieManagerBehaviour.I.StartUp(); FieManagerBehaviour.I.StartUp(); FieManagerBehaviour.I.StartUp(); FieManagerBehaviour.I.StartUp(); FieManagerBehaviour.I.StartUp(); FieManagerBehaviour.I.FiePreparedForLoadSceneEvent += ScenePreparedToLoadCallback; FieManagerBehaviour.I.FieSceneWasLoadedEvent += SceneLoadedCallback; } private void ScenePreparedToLoadCallback(FieSceneBase targetScene) { CleanupManager(targetScene); } private void SceneLoadedCallback(FieSceneBase targetScene) { if (currentSceneType == FieSceneType.INGAME) { if (!FieManagerBehaviour.I.isBooted) { FieManagerBehaviour.I.StartUp(); } FieManagerBehaviour.I.StartUp(); } } public void Restart() { I.KillPopcornFxAll(); if (!FieManagerBehaviour.I.isTitle()) { foreach (KeyValuePair manager in managerList) { UnityEngine.Object.Destroy(manager.Value.gameObject); } UnityEngine.Object.Destroy(base.gameObject); SceneManager.LoadScene(0, LoadSceneMode.Single); } } public void KillPopcornFxAll() { PKFxFX[] array = UnityEngine.Object.FindObjectsOfType(); if (array != null && array.Length > 0) { PKFxFX[] array2 = array; foreach (PKFxFX pKFxFX in array2) { pKFxFX.KillEffect(); } } } } }