예제 #1
0
        internal void Load()
        {
            try {
                Log.Info($"TMPELifecycle.Load() called. Mode={Mode}, UpdateMode={UpdateMode}, Scene={Scene}");

                if (Scene == "ThemeEditor")
                {
                    return;
                }

                IsGameLoaded = false;

                InGameUtil.Instantiate();

                VersionUtil.CheckGameVersion();

                IsGameLoaded = true;

                CustomPathManager.OnLevelLoaded();

                ModUI.OnLevelLoaded();
                if (PlayMode)
                {
                    UIView uiView = UIView.GetAView();
                    uiView.AddUIComponent(typeof(UITransportDemand));
                    uiView.gameObject.AddComponent <RemoveVehicleButtonExtender>();
                    uiView.gameObject.AddComponent <RemoveCitizenInstanceButtonExtender>();
                    uiView.gameObject.AddComponent <RoadSelectionPanels>();
                }

                Patcher.Install();

                Log.Info("Notifying managers...");
                foreach (ICustomManager manager in RegisteredManagers)
                {
                    Log.Info($"OnLevelLoading: {manager.GetType().Name}");
                    manager.OnLevelLoading();
                }

                // must be subscribed last to notify other mods about TMPE changes
                // after all TMPE rules are applied.
                GeometryNotifierDisposable = GeometryManager.Instance.Subscribe(new GeometryNotifier());
                Notifier.Instance.OnLevelLoaded();
                Log.Info("OnLevelLoaded complete.");
            } catch (Exception ex) {
                ex.LogException(true);
            }
        }
예제 #2
0
        public override void OnLevelLoaded(LoadMode mode)
        {
            SimulationManager.UpdateMode updateMode = SimulationManager.instance.m_metaData.m_updateMode;
            Log.Info($"OnLevelLoaded({mode}) called. updateMode={updateMode}");
            base.OnLevelLoaded(mode);

            Log._Debug("OnLevelLoaded Returned from base, calling custom code.");

            IsGameLoaded = false;

            switch (updateMode)
            {
            case SimulationManager.UpdateMode.NewGameFromMap:
            case SimulationManager.UpdateMode.NewGameFromScenario:
            case SimulationManager.UpdateMode.LoadGame: {
                if (BuildConfig.applicationVersion != BuildConfig.VersionToString(
                        TrafficManagerMod.GAME_VERSION,
                        false))
                {
                    string[] majorVersionElms = BuildConfig.applicationVersion.Split('-');
                    string[] versionElms      = majorVersionElms[0].Split('.');
                    uint     versionA         = Convert.ToUInt32(versionElms[0]);
                    uint     versionB         = Convert.ToUInt32(versionElms[1]);
                    uint     versionC         = Convert.ToUInt32(versionElms[2]);

                    Log.Info($"Detected game version v{BuildConfig.applicationVersion}");

                    bool isModTooOld = TrafficManagerMod.GAME_VERSION_A < versionA ||
                                       (TrafficManagerMod.GAME_VERSION_A == versionA &&
                                        TrafficManagerMod.GAME_VERSION_B < versionB);
                    // || (TrafficManagerMod.GameVersionA == versionA
                    // && TrafficManagerMod.GameVersionB == versionB
                    // && TrafficManagerMod.GameVersionC < versionC);

                    bool isModNewer = TrafficManagerMod.GAME_VERSION_A < versionA ||
                                      (TrafficManagerMod.GAME_VERSION_A == versionA &&
                                       TrafficManagerMod.GAME_VERSION_B > versionB);
                    // || (TrafficManagerMod.GameVersionA == versionA
                    // && TrafficManagerMod.GameVersionB == versionB
                    // && TrafficManagerMod.GameVersionC > versionC);

                    if (isModTooOld)
                    {
                        string msg = string.Format(
                            "Traffic Manager: President Edition detected that you are running " +
                            "a newer game version ({0}) than TM:PE has been built for ({1}). " +
                            "Please be aware that TM:PE has not been updated for the newest game " +
                            "version yet and thus it is very likely it will not work as expected.",
                            BuildConfig.applicationVersion,
                            BuildConfig.VersionToString(TrafficManagerMod.GAME_VERSION, false));

                        Log.Error(msg);
                        Singleton <SimulationManager> .instance.m_ThreadingWrapper.QueueMainThread(
                            () => {
                                UIView.library
                                .ShowModal <ExceptionPanel>("ExceptionPanel")
                                .SetMessage(
                                    "TM:PE has not been updated yet",
                                    msg,
                                    false);
                            });
                    }
                    else if (isModNewer)
                    {
                        string msg = string.Format(
                            "Traffic Manager: President Edition has been built for game version {0}. " +
                            "You are running game version {1}. Some features of TM:PE will not " +
                            "work with older game versions. Please let Steam update your game.",
                            BuildConfig.VersionToString(TrafficManagerMod.GAME_VERSION, false),
                            BuildConfig.applicationVersion);

                        Log.Error(msg);
                        Singleton <SimulationManager>
                        .instance.m_ThreadingWrapper.QueueMainThread(
                            () => {
                                UIView.library
                                .ShowModal <ExceptionPanel>("ExceptionPanel")
                                .SetMessage(
                                    "Your game should be updated",
                                    msg,
                                    false);
                            });
                    }
                }

                IsGameLoaded = true;
                break;
            }

            default: {
                Log.Info($"OnLevelLoaded: Unsupported game mode {mode}");
                return;
            }
            }

            //it will replace stock PathManager or already Replaced before HotReload
            if (!IsPathManagerReplaced || TrafficManagerMod.Instance.InGameHotReload)
            {
                try {
                    Log.Info("Pathfinder Compatible. Setting up CustomPathManager and SimManager.");
                    FieldInfo pathManagerInstance = typeof(Singleton <PathManager>).GetField(
                        "sInstance",
                        BindingFlags.Static | BindingFlags.NonPublic);
                    if (pathManagerInstance == null)
                    {
                        throw new Exception("pathManagerInstance is null");
                    }


                    PathManager stockPathManager = PathManager.instance;
                    if (stockPathManager == null)
                    {
                        throw new Exception("stockPathManager is null");
                    }

                    Log._Debug($"Got stock PathManager instance {stockPathManager?.GetName()}");

                    CustomPathManager =
                        stockPathManager.gameObject.AddComponent <CustomPathManager>();
                    Log._Debug("Added CustomPathManager to gameObject List");

                    if (CustomPathManager == null)
                    {
                        Log.Error("CustomPathManager null. Error creating it.");
                        return;
                    }

                    CustomPathManager.UpdateWithPathManagerValues(stockPathManager);
                    Log._Debug("UpdateWithPathManagerValues success");

                    pathManagerInstance.SetValue(null, CustomPathManager);

                    Log._Debug("Getting Current SimulationManager");
                    var simManager = this.simManager;
                    if (simManager == null)
                    {
                        throw new Exception("simManager is null");
                    }

                    Log._Debug("Removing Stock PathManager");
                    simManager.Remove(stockPathManager);

                    Log._Debug("Adding Custom PathManager");
                    simManager.Add(CustomPathManager);

                    Object.Destroy(stockPathManager, 10f);

                    Log._Debug("Should be custom: " + Singleton <PathManager> .instance.GetType());

                    IsPathManagerReplaced = true;
                }
                catch (Exception ex) {
                    string error =
                        "Traffic Manager: President Edition failed to load. You can continue " +
                        "playing but it's NOT recommended. Traffic Manager will not work as expected.";
                    Log.Error(error);
                    Log.Error($"Path manager replacement error: {ex}");

                    Singleton <SimulationManager> .instance.m_ThreadingWrapper.QueueMainThread(
                        () => {
                        UIView.library
                        .ShowModal <ExceptionPanel>(
                            "ExceptionPanel")
                        .SetMessage(
                            "TM:PE failed to load",
                            error,
                            true);
                    });
                }
            }

            ModUI.OnLevelLoaded();

            // Init transport demand UI
            if (TransportDemandUI == null)
            {
                UIView uiView = UIView.GetAView();
                TransportDemandUI = (UITransportDemand)uiView.AddUIComponent(typeof(UITransportDemand));
            }

            // add "remove vehicle" button
            UIView.GetAView().gameObject.AddComponent <RemoveVehicleButtonExtender>();

            // add "remove citizen instance" button
            UIView.GetAView().gameObject.AddComponent <RemoveCitizenInstanceButtonExtender>();

            UIView.GetAView().gameObject.AddComponent <RoadSelectionPanels>();

            Patcher.Create().Install();

            // Log.Info("Fixing non-created nodes with problems...");
            // FixNonCreatedNodeProblems();
            Log.Info("Notifying managers...");
            foreach (ICustomManager manager in RegisteredManagers)
            {
                Log.Info($"OnLevelLoading: {manager.GetType().Name}");
                manager.OnLevelLoading();
            }

            // InitTool();
            // Log._Debug($"Current tool: {ToolManager.instance.m_properties.CurrentTool}");
            Log.Info("OnLevelLoaded complete.");
        }