public override void OnLevelUnloading() { Log.Info("OnLevelUnloading"); base.OnLevelUnloading(); CustomPathManager._instance.WaitForAllPaths(); try { var reverseManagers = new List <ICustomManager>(RegisteredManagers); reverseManagers.Reverse(); foreach (ICustomManager manager in reverseManagers) { Log.Info($"OnLevelUnloading: {manager.GetType().Name}"); manager.OnLevelUnloading(); } Flags.OnLevelUnloading(); GlobalConfig.OnLevelUnloading(); var gameObject = UIView.GetAView().gameObject; void Destroy <T>() where T : MonoBehaviour { Object obj = (Object)gameObject.GetComponent <T>(); if (obj != null) { Object.Destroy(obj); } } // remove vehicle button Destroy <RemoveVehicleButtonExtender>(); Destroy <RemoveCitizenInstanceButtonExtender>(); // Custom path manger is destroyed when reloading. That is why the following code // is commented out. //simManager?.Remove(CustomPathManager); //Object.Destroy(CustomPathManager); //CustomPathManager = null; if (TransportDemandUI != null) { UIView uiView = UIView.GetAView(); Object.Destroy(TransportDemandUI); TransportDemandUI = null; } Log.Info("Removing Controls from UI."); if (ModUI.Instance != null) { ModUI.Instance.Close(); // Hide the UI ASAP Object.Destroy(ModUI.Instance); ModUI.SetSingletonInstance(null); Log._Debug("removed UIBase instance."); } #if TRACE Singleton <CodeProfiler> .instance.OnLevelUnloading(); #endif } catch (Exception e) { Log.Error("Exception unloading mod. " + e.Message); // ignored - prevents collision with other mods } RevertDetours(); IsGameLoaded = false; }
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; } } 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()); } 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); }); } Log.Info("Adding Controls to UI."); if (ModUI.Instance == null) { Log._Debug("Adding UIBase instance."); ModUI.SetSingletonInstance( ToolsModifierControl.toolController.gameObject.AddComponent <ModUI>()); } // 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>(); InitDetours(); // 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."); }
public override void OnLevelUnloading() { Log.Info("OnLevelUnloading"); base.OnLevelUnloading(); if (IsPathManagerReplaced) { CustomPathManager._instance.WaitForAllPaths(); } try { var reverseManagers = new List <ICustomManager>(RegisteredManagers); reverseManagers.Reverse(); foreach (ICustomManager manager in reverseManagers) { Log.Info($"OnLevelUnloading: {manager.GetType().Name}"); manager.OnLevelUnloading(); } Flags.OnLevelUnloading(); GlobalConfig.OnLevelUnloading(); var gameObject = UIView.GetAView().gameObject; void Destroy <T>() where T : MonoBehaviour { Object obj = (Object)gameObject.GetComponent <T>(); if (obj != null) { Object.Destroy(obj); } } Destroy <RoadSelectionPanels>(); Destroy <RemoveVehicleButtonExtender>(); Destroy <RemoveCitizenInstanceButtonExtender>(); //It's MonoBehaviour - comparing to null is wrong if (TransportDemandUI) { Object.Destroy(TransportDemandUI); TransportDemandUI = null; } Log.Info("Removing Controls from UI."); if (ModUI.Instance != null) { ModUI.Instance.Close(); // Hide the UI ASAP Object.Destroy(ModUI.Instance); ModUI.SetSingletonInstance(null); Log._Debug("removed UIBase instance."); } #if TRACE Singleton <CodeProfiler> .instance.OnLevelUnloading(); #endif } catch (Exception e) { Log.Error("Exception unloading mod. " + e.Message); // ignored - prevents collision with other mods } RevertDetours(); IsGameLoaded = false; }