Ejemplo n.º 1
0
        public Coroutine LoadLevel(Package.Asset asset, string playerScene, string uiScene, SimulationMetaData ngs)
        {
            LoadingManager lm        = Singleton <LoadingManager> .instance;
            bool           activated = ngs.m_updateMode == SimulationManager.UpdateMode.LoadGame || ngs.m_updateMode == SimulationManager.UpdateMode.NewGameFromMap ||
                                       ngs.m_updateMode == SimulationManager.UpdateMode.NewGameFromScenario || Input.GetKey(KeyCode.LeftControl);

            instance.simulationFailed = false;

            if (!lm.m_currentlyLoading && !lm.m_applicationQuitting)
            {
                if (lm.m_LoadingWrapper != null)
                {
                    lm.m_LoadingWrapper.OnLevelUnloading(); // OnLevelUnloading
                }
                if (activated)
                {
                    Util.DebugPrint("Options: 1125", Settings.settings.loadEnabled, Settings.settings.loadUsed, Settings.settings.shareTextures,
                                    Settings.settings.shareMaterials, Settings.settings.shareMeshes, Settings.settings.reportAssets);

                    LoadingManager.instance.SetSceneProgress(0f);
                    instance.cityName = asset?.name ?? "NewGame";
                    Profiling.Init();
                    CustomDeserializer.Create();
                    Fixes.Create().Deploy();
                    AssetLoader.Create().Setup();
                    LoadingScreen.Create().Setup();
                }

                lm.LoadingAnimationComponent.enabled = true;
                lm.m_currentlyLoading      = true;
                lm.m_metaDataLoaded        = false;
                lm.m_simulationDataLoaded  = false;
                lm.m_loadingComplete       = false;
                lm.m_renderDataReady       = false;
                lm.m_essentialScenesLoaded = false;
                lm.m_brokenAssets          = string.Empty;
                Util.Set(lm, "m_sceneProgress", 0f);
                Util.Set(lm, "m_simulationProgress", 0f);

                if (activated)
                {
                    Profiling.Start();
                }

                lm.m_loadingProfilerMain.Reset();
                lm.m_loadingProfilerSimulation.Reset();
                lm.m_loadingProfilerScenes.Reset();

                IEnumerator iter = activated ? instance.LoadLevelCoroutine(asset, playerScene, uiScene, ngs) :
                                   (IEnumerator)Util.Invoke(lm, "LoadLevelCoroutine", asset, playerScene, uiScene, ngs);

                return(lm.StartCoroutine(iter));
            }

            return(null);
        }
Ejemplo n.º 2
0
        public IEnumerator LoadLevelCoroutine(Package.Asset asset, string playerScene, string uiScene, SimulationMetaData ngs, bool forceEnvironmentReload)
        {
            string scene;
            int    i;

            yield return(null);

            try
            {
                Util.InvokeVoid(LoadingManager.instance, "PreLoadLevel");
            }
            catch (Exception e)
            {
                Util.DebugPrint("PreLoadLevel: exception from some mod.");
                UnityEngine.Debug.LogException(e);
            }

            if (!LoadingManager.instance.LoadingAnimationComponent.AnimationLoaded)
            {
                LoadingManager.instance.m_loadingProfilerScenes.BeginLoading("LoadingAnimation");
                yield return(SceneManager.LoadSceneAsync("LoadingAnimation", LoadSceneMode.Additive));

                LoadingManager.instance.m_loadingProfilerScenes.EndLoading();
            }

            DateTime  skipStamp = Settings.settings.LoadSkipFile();
            AsyncTask task      = Singleton <SimulationManager> .instance.AddAction("Loading", (IEnumerator)Util.Invoke(LoadingManager.instance, "LoadSimulationData", asset, ngs));

            LoadSaveStatus.activeTask = task;

            if (LoadingManager.instance.m_loadedEnvironment == null) // loading from main menu
            {
                fastLoad = false;
            }
            else // loading from in-game (the pause menu)
            {
                while (!LoadingManager.instance.m_metaDataLoaded && !task.completedOrFailed) // IL_139
                {
                    yield return(null);
                }

                if (SimulationManager.instance.m_metaData == null)
                {
                    SimulationManager.instance.m_metaData = new SimulationMetaData();
                    SimulationManager.instance.m_metaData.m_environment = "Sunny";
                    SimulationManager.instance.m_metaData.Merge(ngs);
                }

                Util.InvokeVoid(LoadingManager.instance, "MetaDataLoaded"); // No OnCreated
                string mapThemeName = SimulationManager.instance.m_metaData.m_MapThemeMetaData?.name;
                fastLoad = SimulationManager.instance.m_metaData.m_environment == LoadingManager.instance.m_loadedEnvironment &&
                           mapThemeName == LoadingManager.instance.m_loadedMapTheme && !forceEnvironmentReload;

                // The game is nicely optimized when loading from the pause menu. We must specifically address the following situations:
                // - environment (biome) stays the same
                // - map theme stays the same
                // - forceEnvironmentReload is false
                // - 'load used assets' is enabled
                // - not all assets and prefabs used in the save being loaded are currently in memory
                // - prefab skipping has changed.

                if (fastLoad)
                {
                    // Check custom asset availability.
                    if (Settings.settings.loadUsed && !IsKnownFastLoad(asset))
                    {
                        while (!IsSaveDeserialized())
                        {
                            yield return(null);
                        }

                        fastLoad = AllAssetsAvailable();
                    }

                    // Check building prefab availability.
                    if (fastLoad)
                    {
                        if (skipStamp != savedSkipStamp)
                        {
                            fastLoad = false;
                        }
                        else if (Settings.settings.SkipPrefabs && !IsKnownFastLoad(asset))
                        {
                            while (!IsSaveDeserialized())
                            {
                                yield return(null);
                            }

                            fastLoad = AllPrefabsAvailable();
                        }
                    }

                    if (fastLoad) // optimized load
                    {
                        if (Settings.settings.SkipPrefabs && skippedPrefabs[0] != null)
                        {
                            while (!IsSaveDeserialized())
                            {
                                yield return(null);
                            }

                            PrefabLoader.Create().SetSkippedPrefabs(skippedPrefabs);
                            LoadingManager.instance.QueueLoadingAction(PrefabLoader.RemoveSkippedFromSimulation());
                        }

                        LoadingManager.instance.QueueLoadingAction((IEnumerator)Util.Invoke(LoadingManager.instance, "EssentialScenesLoaded"));
                        LoadingManager.instance.QueueLoadingAction((IEnumerator)Util.Invoke(LoadingManager.instance, "RenderDataReady"));
                        Util.DebugPrint("fast load at", Profiling.Millis);
                    }
                    else // fallback to full load
                    {
                        DestroyLoadedPrefabs();
                        LoadingManager.instance.m_loadedEnvironment = null;
                        LoadingManager.instance.m_loadedMapTheme    = null;
                        Util.DebugPrint("fallback to full load at", Profiling.Millis);
                    }
                }
                else // full load
                {
                    // Notice that there is a race condition in the base game at this point: DestroyAllPrefabs ruins the simulation
                    // if its deserialization has progressed far enough. Typically there is no problem.
                    Util.InvokeVoid(LoadingManager.instance, "DestroyAllPrefabs");
                    LoadingManager.instance.m_loadedEnvironment = null;
                    LoadingManager.instance.m_loadedMapTheme    = null;
                    Util.DebugPrint("full load at", Profiling.Millis);
                }
            }

            // Full load.
            if (LoadingManager.instance.m_loadedEnvironment == null) // IL_27C
            {
                AsyncOperation op;
                Reset();
                fullLoadTime   = DateTime.Now;
                savedSkipStamp = skipStamp;
                Array.Clear(skippedPrefabs, 0, skippedPrefabs.Length);
                loadingLock     = Util.Get(LoadingManager.instance, "m_loadingLock");
                mainThreadQueue = (Queue <IEnumerator>)Util.Get(LoadingManager.instance, "m_mainThreadQueue");

                if (!string.IsNullOrEmpty(playerScene))
                {
                    LoadingManager.instance.m_loadingProfilerScenes.BeginLoading(playerScene);
                    op = SceneManager.LoadSceneAsync(playerScene, LoadSceneMode.Single);

                    while (!op.isDone) // IL_2FF
                    {
                        LoadingManager.instance.SetSceneProgress(op.progress * 0.01f);
                        yield return(null);
                    }

                    LoadingManager.instance.m_loadingProfilerScenes.EndLoading();
                }

                while (!LoadingManager.instance.m_metaDataLoaded && !task.completedOrFailed) // IL_33C
                {
                    yield return(null);
                }

                if (SimulationManager.instance.m_metaData == null)
                {
                    SimulationManager.instance.m_metaData = new SimulationMetaData();
                    SimulationManager.instance.m_metaData.m_environment = "Sunny";
                    SimulationManager.instance.m_metaData.Merge(ngs);
                }

                try
                {
                    Util.InvokeVoid(LoadingManager.instance, "MetaDataLoaded"); // OnCreated if loading from the main manu
                }
                catch (Exception e)
                {
                    Util.DebugPrint("OnCreated: exception from some mod.");
                    UnityEngine.Debug.LogException(e);
                }

                KeyValuePair <string, float>[] levels = SetLevels();
                float currentProgress = 0.10f;

                for (i = 0; i < levels.Length; i++)
                {
                    scene = levels[i].Key;
                    LoadingManager.instance.m_loadingProfilerScenes.BeginLoading(scene);
                    op = SceneManager.LoadSceneAsync(scene, LoadSceneMode.Additive);

                    while (!op.isDone)
                    {
                        LoadingManager.instance.SetSceneProgress(currentProgress + op.progress * (levels[i].Value - currentProgress));
                        yield return(null);
                    }

                    LoadingManager.instance.m_loadingProfilerScenes.EndLoading();
                    currentProgress = levels[i].Value;
                }

                PrefabLoader.instance?.Revert();

                if (Settings.settings.SkipPrefabs)
                {
                    LoadingManager.instance.QueueLoadingAction(PrefabLoader.RemoveSkippedFromSimulation());
                }

                // Some major mods (Network Extensions 1 & 2, Single Train Track, Metro Overhaul) have a race condition issue
                // in their NetInfo Installer. Everything goes fine if LoadCustomContent() below is NOT queued before the
                // said Installers have finished. This is just a workaround for the issue. The actual fix should be in
                // the Installers. Notice that the built-in loader of the game is also affected.

                do
                {
                    yield return(null);

                    yield return(null);

                    lock (loadingLock)
                    {
                        i = mainThreadQueue.Count;
                    }
                }while (i > 0);

                AssetLoader.Create().Setup();
                LoadingManager.instance.QueueLoadingAction(AssetLoader.instance.LoadCustomContent());
                RenderManager.Managers_CheckReferences();
                LoadingManager.instance.QueueLoadingAction((IEnumerator)Util.Invoke(LoadingManager.instance, "EssentialScenesLoaded"));
                RenderManager.Managers_InitRenderData();
                LoadingManager.instance.QueueLoadingAction((IEnumerator)Util.Invoke(LoadingManager.instance, "RenderDataReady"));
                simulationFailed = HasFailed(task);

                // Performance optimization: do not load scenes while custom assets are loading.
                while (!assetsFinished)
                {
                    yield return(null);
                }

                scene = SimulationManager.instance.m_metaData.m_environment + "Properties";

                if (!string.IsNullOrEmpty(scene))
                {
                    LoadingManager.instance.m_loadingProfilerScenes.BeginLoading(scene);
                    op = SceneManager.LoadSceneAsync(scene, LoadSceneMode.Additive);

                    while (!op.isDone) // IL_C47
                    {
                        LoadingManager.instance.SetSceneProgress(0.85f + op.progress * 0.05f);
                        yield return(null);
                    }

                    LoadingManager.instance.m_loadingProfilerScenes.EndLoading();
                }

                if (!simulationFailed)
                {
                    simulationFailed = HasFailed(task);
                }

                if (!string.IsNullOrEmpty(uiScene)) // IL_C67
                {
                    LoadingManager.instance.m_loadingProfilerScenes.BeginLoading(uiScene);
                    op = SceneManager.LoadSceneAsync(uiScene, LoadSceneMode.Additive);

                    while (!op.isDone) // IL_CDE
                    {
                        LoadingManager.instance.SetSceneProgress(0.90f + op.progress * 0.08f);
                        yield return(null);
                    }

                    LoadingManager.instance.m_loadingProfilerScenes.EndLoading();
                }

                LoadingManager.instance.m_loadedEnvironment = SimulationManager.instance.m_metaData.m_environment; // IL_CFE
                LoadingManager.instance.m_loadedMapTheme    = SimulationManager.instance.m_metaData.m_MapThemeMetaData?.name;
            }
            else
            {
                scene = (string)Util.Invoke(LoadingManager.instance, "GetLoadingScene");

                if (!string.IsNullOrEmpty(scene))
                {
                    LoadingManager.instance.m_loadingProfilerScenes.BeginLoading(scene);
                    yield return(SceneManager.LoadSceneAsync(scene, LoadSceneMode.Additive));

                    LoadingManager.instance.m_loadingProfilerScenes.EndLoading();
                }
            }

            LoadingManager.instance.SetSceneProgress(1f); // IL_DBF

            if (!simulationFailed)
            {
                simulationFailed = HasFailed(task);
            }

            while (!task.completedOrFailed) // IL_DED
            {
                yield return(null);
            }

            LoadingManager.instance.m_simulationDataLoaded = LoadingManager.instance.m_metaDataLoaded;
            LoadingManager.SimulationDataReadyHandler SimDataReady = Util.Get(LoadingManager.instance, "m_simulationDataReady") as LoadingManager.SimulationDataReadyHandler;
            SimDataReady?.Invoke();
            SimulationManager.UpdateMode mode = SimulationManager.UpdateMode.Undefined;

            if (ngs != null)
            {
                mode = ngs.m_updateMode;
            }

            LoadingManager.instance.QueueLoadingAction(CheckPolicies());
            LoadingManager.instance.QueueLoadingAction((IEnumerator)Util.Invoke(LoadingManager.instance, "LoadLevelComplete", mode));  // OnLevelLoaded
            PrefabLoader.instance?.Dispose();
            LoadingManager.instance.QueueLoadingAction(LoadingComplete());
            knownFastLoads[asset.checksum] = true;
            AssetLoader.PrintMem();
        }