diff --git a/NewHorizons/Handlers/PlanetCreationHandler.cs b/NewHorizons/Handlers/PlanetCreationHandler.cs index de710065..5817d0ac 100644 --- a/NewHorizons/Handlers/PlanetCreationHandler.cs +++ b/NewHorizons/Handlers/PlanetCreationHandler.cs @@ -158,8 +158,20 @@ namespace NewHorizons.Handlers } catch (Exception) { - if (body?.Config?.name == null) NHLogger.LogError($"How is there no name for {body}"); - else existingPlanet = SearchUtilities.Find(body.Config.name.Replace(" ", "") + "_Body", false); + if (body?.Config?.name == null) + { + NHLogger.LogError($"How is there no name for {body}"); + } + else + { + existingPlanet = SearchUtilities.Find(body.Config.name.Replace(" ", "") + "_Body", false); + } + } + + if (existingPlanet == null && body.Config.destroy) + { + NHLogger.LogError($"{body.Config.name} was meant to be destroyed, but was not found"); + return false; } if (existingPlanet != null) diff --git a/NewHorizons/Handlers/PlanetDestructionHandler.cs b/NewHorizons/Handlers/PlanetDestructionHandler.cs index 6b796bac..cc36636c 100644 --- a/NewHorizons/Handlers/PlanetDestructionHandler.cs +++ b/NewHorizons/Handlers/PlanetDestructionHandler.cs @@ -134,14 +134,14 @@ namespace NewHorizons.Handlers public static void DisableAstroObject(AstroObject ao, List toDisable = null) { - NHLogger.LogVerbose($"Removing [{ao.name}]"); - if (ao.gameObject == null || !ao.gameObject.activeInHierarchy) { - NHLogger.LogVerbose($"[{ao.name}] was already removed"); + NHLogger.LogVerbose($"[{ao?.name}] was already removed"); return; } + NHLogger.LogVerbose($"Removing [{ao.name}]"); + toDisable ??= new List(); if (toDisable.Contains(ao)) @@ -222,6 +222,7 @@ namespace NewHorizons.Handlers NHLogger.LogError($"Exception thrown when trying to delete bodies related to [{ao.name}]:\n{e}"); } + DisableGameObject(ao.gameObject); RemoveProxy(ao); } diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index a78e9eff..ab67503e 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -64,16 +64,21 @@ namespace NewHorizons { return _currentStarSystem; } - internal set + set { - _previousStarSystem = _currentStarSystem; + // Prevent invalid values + if (value != "SolarSystem" && value != "EyeOfTheUniverse" && !SystemDict.ContainsKey(value) && !BodyDict.ContainsKey(value)) + { + NHLogger.LogError($"System \"{value}\" does not exist!"); + _currentStarSystem = DefaultStarSystem; + } _currentStarSystem = value; } } - private string _currentStarSystem = "SolarSystem"; + private string _currentStarSystem; private string _previousStarSystem; - public bool TimeLoopEnabled => SystemDict[CurrentStarSystem]?.Config?.enableTimeLoop ?? true; + public bool TimeLoopEnabled => CurrentStarSystem == null || (SystemDict[CurrentStarSystem]?.Config?.enableTimeLoop ?? true); public bool IsWarpingFromShip { get; private set; } = false; public bool IsWarpingFromVessel { get; private set; } = false; public bool IsWarpingBackToEye { get; internal set; } = false; @@ -280,6 +285,7 @@ namespace NewHorizons // Caches of other assets only have to be cleared if we changed star systems if (CurrentStarSystem != _previousStarSystem) { + NHLogger.Log($"Changing star system from {_previousStarSystem} to {CurrentStarSystem} - Clearing system-specific caches!"); ImageUtilities.ClearCache(); AudioUtilities.ClearCache(); AssetBundleUtilities.ClearCache(); @@ -362,12 +368,6 @@ namespace NewHorizons return; } - if (!SystemDict.ContainsKey(CurrentStarSystem) || !BodyDict.ContainsKey(CurrentStarSystem)) - { - NHLogger.LogError($"System \"{CurrentStarSystem}\" does not exist!"); - CurrentStarSystem = DefaultStarSystem; - } - // Set time loop stuff if its enabled and if we're warping to a new place if (IsChangingStarSystem && (SystemDict[CurrentStarSystem].Config.enableTimeLoop || CurrentStarSystem == "SolarSystem") && SecondsElapsedInLoop > 0f) { @@ -576,6 +576,9 @@ namespace NewHorizons { ResetCurrentStarSystem(); } + + // We only check previous when the scene unloads, and at that point current should be updated to the new system + _previousStarSystem = CurrentStarSystem; } // Had a bunch of separate unity things firing stuff when the system is ready so I moved it all to here