diff --git a/TLM/TLM/LoadingExtension.cs b/TLM/TLM/LoadingExtension.cs index 8ae3f7de9..cb7c9e4bb 100644 --- a/TLM/TLM/LoadingExtension.cs +++ b/TLM/TLM/LoadingExtension.cs @@ -24,23 +24,10 @@ public class LoadingExtension : LoadingExtensionBase { private const string HARMONY_ID = "de.viathinksoft.tmpe"; internal static LoadingExtension Instance = null; - internal bool InGameHotReload { get; private set; } = false; - - internal static AppMode currentMode => SimulationManager.instance.m_ManagersWrapper.loading.currentMode; - - internal static bool InGame() { - try { - return currentMode == AppMode.Game; - } catch { - return false; - } - } - FastList simManager => typeof(SimulationManager).GetField("m_managers", BindingFlags.Static | BindingFlags.NonPublic) ?.GetValue(null) as FastList; - public class Detour { public MethodInfo OriginalMethod; public MethodInfo CustomMethod; @@ -275,6 +262,11 @@ public override void OnCreated(ILoading loading) { // SelfDestruct.DestructOldInstances(this); base.OnCreated(loading); + if(IsGameLoaded) { + // When another mod is detected, OnCreated is called again for god - or CS team - knows what reason! + Log._Debug("Hot reload of another mod detected. Skipping LoadingExtension.OnCreated() ..."); + return; + } Detours = new List(); RegisteredManagers = new List(); @@ -284,7 +276,6 @@ public override void OnCreated(ILoading loading) { RegisterCustomManagers(); Instance = this; - InGameHotReload = InGame(); } private void RegisterCustomManagers() { @@ -319,6 +310,7 @@ private void RegisterCustomManagers() { } public override void OnReleased() { + TrafficManagerMod.Instance.InGameHotReload = false; Instance = null; base.OnReleased(); } diff --git a/TLM/TLM/State/SerializableDataExtension.cs b/TLM/TLM/State/SerializableDataExtension.cs index 9a9a55fc4..d8747dce7 100644 --- a/TLM/TLM/State/SerializableDataExtension.cs +++ b/TLM/TLM/State/SerializableDataExtension.cs @@ -20,11 +20,14 @@ public class SerializableDataExtension public static bool StateLoading; public override void OnCreated(ISerializableData serializableData) { - _serializableData = serializableData; - Log._Debug("SerializableDataExtension.OnCreated() called"); + if(LoadingExtension.IsGameLoaded) { + Log._Debug("Hot reload of another mod detected. Skipping SerializableDataExtension.OnCreated() ..."); + return; + } + _serializableData = serializableData; - if (LoadingExtension.Instance.InGameHotReload) { + if (TrafficManagerMod.Instance.InGameHotReload) { Log._Debug("HOT RELOAD ..."); OnLoadData(); LoadingExtension.Instance.OnLevelLoaded(LoadMode.LoadGame); diff --git a/TLM/TLM/TrafficManagerMod.cs b/TLM/TLM/TrafficManagerMod.cs index 099646550..a5525ceb8 100644 --- a/TLM/TLM/TrafficManagerMod.cs +++ b/TLM/TLM/TrafficManagerMod.cs @@ -11,6 +11,7 @@ namespace TrafficManager { using TrafficManager.Util; using static TrafficManager.Util.Shortcuts; using ColossalFramework; + using UnityEngine.SceneManagement; public class TrafficManagerMod : IUserMod { #if LABS @@ -41,6 +42,12 @@ public class TrafficManagerMod : IUserMod { public string Description => "Manage your city's traffic"; + internal static TrafficManagerMod Instance = null; + + internal bool InGameHotReload { get; set; } = false; + + internal static bool InGame() => SceneManager.GetActiveScene().name == "Game"; + [UsedImplicitly] public void OnEnabled() { Log.InfoFormat( @@ -75,6 +82,9 @@ public void OnEnabled() { Log.InfoFormat("Mono version: {0}", displayName.Invoke(null, null)); } } + + Instance = this; + InGameHotReload = InGame(); } [UsedImplicitly] @@ -84,11 +94,12 @@ public void OnDisabled() { LocaleManager.eventLocaleChanged -= Translation.HandleGameLocaleChange; Translation.IsListeningToGameLocaleChanged = false; // is this necessary? - if (LoadingExtension.InGame() && LoadingExtension.Instance != null) { + if (InGame() && LoadingExtension.Instance != null) { //Hot reload Unloading LoadingExtension.Instance.OnLevelUnloading(); LoadingExtension.Instance.OnReleased(); } + Instance = null; } [UsedImplicitly] diff --git a/TLM/TLM/UI/MainMenu/VersionLabel.cs b/TLM/TLM/UI/MainMenu/VersionLabel.cs index 1f009e164..d4e53f5f3 100644 --- a/TLM/TLM/UI/MainMenu/VersionLabel.cs +++ b/TLM/TLM/UI/MainMenu/VersionLabel.cs @@ -11,7 +11,7 @@ public override void Start() { MainMenuPanel.ScaledSize.GetTitlebarHeight()); text = TrafficManagerMod.ModName; - if(LoadingExtension.Instance.InGameHotReload) { + if(TrafficManagerMod.Instance.InGameHotReload) { // make it easier to Identify Hot reload. text += " HOT RELOAD " + Assembly.GetExecutingAssembly().GetName().Version;