From d5ee5ff33a9a23811cf65f45b0dc61df78a3a814 Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 13 May 2022 16:30:37 -0400 Subject: [PATCH] Add default star system override option --- NewHorizons/Main.cs | 81 ++++++++++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 26 deletions(-) diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index 9290c3d9..c537f99d 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -35,9 +35,10 @@ namespace NewHorizons public static Main Instance { get; private set; } // Settings - public static bool Debug; + public static bool Debug { get; private set; } private static bool _useCustomTitleScreen; private static bool _wasConfigured = false; + private static string _defaultSystemOverride; public static Dictionary SystemDict = new Dictionary(); public static Dictionary> BodyDict = new Dictionary>(); @@ -84,6 +85,8 @@ namespace NewHorizons DebugReload.UpdateReloadButton(); Logger.UpdateLogLevel(Debug ? Logger.LogType.Log : Logger.LogType.Error); + _defaultSystemOverride = config.GetSettingsValue("Default System Override"); + var wasUsingCustomTitleScreen = _useCustomTitleScreen; _useCustomTitleScreen = config.GetSettingsValue("Custom title screen"); // Reload the title screen if this was updated on it @@ -111,11 +114,14 @@ namespace NewHorizons destroyStockPlanets = false } }; - foreach (AssetBundle bundle in AssetBundles.Values) + + foreach (var pair in AssetBundles) { - bundle.Unload(true); + if (pair.Value == null) Logger.LogError($"The asset bundle for {pair.Key} was null when trying to unload"); + else pair.Value.Unload(true); } AssetBundles.Clear(); + if (!resetTranslation) return; TranslationHandler.ClearTables(); TextTranslation.Get().SetLanguage(TextTranslation.Get().GetLanguage()); @@ -136,9 +142,9 @@ namespace NewHorizons GlobalMessenger.AddListener("PlayerDeath", OnDeath); GlobalMessenger.AddListener("WakeUp", new Callback(OnWakeUp)); ShaderBundle = Main.Instance.ModHelper.Assets.LoadBundle("AssetBundle/shader"); - + ResetConfigs(resetTranslation: false); - + Logger.Log("Begin load of config files...", Logger.LogType.Log); try @@ -153,7 +159,7 @@ namespace NewHorizons Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => OnSceneLoaded(SceneManager.GetActiveScene(), LoadSceneMode.Single)); Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => _firstLoad = false); Instance.ModHelper.Menus.PauseMenu.OnInit += DebugReload.InitializePauseMenu; - } + } public void OnDestroy() { @@ -182,22 +188,22 @@ namespace NewHorizons Logger.Log($"Scene Loaded: {scene.name} {mode}"); // Set time loop stuff if its enabled and if we're warping to a new place - if(_isChangingStarSystem && (SystemDict[_currentStarSystem].Config.enableTimeLoop || _currentStarSystem == "SolarSystem") && SecondsLeftInLoop > 0f) + if (_isChangingStarSystem && (SystemDict[_currentStarSystem].Config.enableTimeLoop || _currentStarSystem == "SolarSystem") && SecondsLeftInLoop > 0f) { TimeLoop.SetSecondsRemaining(SecondsLeftInLoop); // Prevent the OPC from firing var launchController = GameObject.FindObjectOfType(); - if(launchController != null) + if (launchController != null) { GlobalMessenger.RemoveListener("StartOfTimeLoop", launchController.OnStartOfTimeLoop); - foreach(var fakeDebris in launchController._fakeDebrisBodies) + foreach (var fakeDebris in launchController._fakeDebrisBodies) { fakeDebris.gameObject.SetActive(false); } launchController.enabled = false; } var nomaiProbe = GameObject.Find("NomaiProbe_Body"); - if(nomaiProbe != null) nomaiProbe.gameObject.SetActive(false); + if (nomaiProbe != null) nomaiProbe.gameObject.SetActive(false); } // Reset this @@ -210,21 +216,21 @@ namespace NewHorizons TitleSceneHandler.DisplayBodyOnTitleScreen(BodyDict.Values.ToList().SelectMany(x => x).ToList()); } - if(scene.name == "EyeOfTheUniverse" && IsWarping) + if (scene.name == "EyeOfTheUniverse" && IsWarping) { - if(_ship != null) SceneManager.MoveGameObjectToScene(_ship, SceneManager.GetActiveScene()); + if (_ship != null) SceneManager.MoveGameObjectToScene(_ship, SceneManager.GetActiveScene()); _ship.transform.position = new Vector3(50, 0, 0); _ship.SetActive(true); } - if(scene.name == "SolarSystem") + if (scene.name == "SolarSystem") { - foreach(var body in GameObject.FindObjectsOfType()) + foreach (var body in GameObject.FindObjectsOfType()) { Logger.Log($"{body.name}, {body.transform.rotation}"); } - if(_ship != null) + if (_ship != null) { _ship = GameObject.Find("Ship_Body").InstantiateInactive(); DontDestroyOnLoad(_ship); @@ -252,14 +258,14 @@ namespace NewHorizons if (IsWarping && _shipWarpController) { Instance.ModHelper.Events.Unity.RunWhen( - () => IsSystemReady, + () => IsSystemReady, () => _shipWarpController.WarpIn(WearingSuit) ); } else { Instance.ModHelper.Events.Unity.RunWhen( - () => IsSystemReady, + () => IsSystemReady, () => FindObjectOfType().DebugWarp(SystemDict[_currentStarSystem].SpawnPoint) ); } @@ -274,7 +280,16 @@ namespace NewHorizons else { // Reset back to original solar system after going to main menu. - _currentStarSystem = _defaultStarSystem; + // If the override is a valid system then we go there + if (SystemDict.Keys.Contains(_defaultSystemOverride)) + { + _currentStarSystem = _defaultSystemOverride; + IsWarping = true; // always do this else sometimes the spawn gets messed up + } + else + { + _currentStarSystem = _defaultStarSystem; + } } } @@ -309,6 +324,12 @@ namespace NewHorizons var relativePath = file.Replace(folder, ""); var starSystemConfig = mod.ModHelper.Storage.Load(relativePath); + if (starSystemConfig.startHere) + { + // We always want to allow mods to overwrite setting the main SolarSystem as default but not the other way around + if (name != "SolarSystem") SetDefaultSystem(name); + } + var system = new NewHorizonsSystem(name, starSystemConfig, mod); SystemDict[name] = system; } @@ -336,7 +357,7 @@ namespace NewHorizons LoadTranslations(folder, mod); } } - catch(Exception ex) + catch (Exception ex) { Logger.LogError($"{ex.Message}, {ex.StackTrace}"); } @@ -380,15 +401,13 @@ namespace NewHorizons if (config.Base.CenterOfSolarSystem) config.Orbit.IsStatic = true; if (!SystemDict.ContainsKey(config.StarSystem)) { - // See if theres a star system config + // Since we didn't load it earlier there shouldn't be a star system config var starSystemConfig = mod.ModHelper.Storage.Load($"systems/{config.StarSystem}.json"); if (starSystemConfig == null) starSystemConfig = new StarSystemConfig(null); - else Logger.Log($"Loaded system config for {config.StarSystem}"); + else Logger.LogWarning($"Loaded system config for {config.StarSystem}. Why wasn't this loaded earlier?"); var system = new NewHorizonsSystem(config.StarSystem, starSystemConfig, mod); - if (system.Config.startHere) SetDefaultSystem(system.Name); - SystemDict.Add(config.StarSystem, system); BodyDict.Add(config.StarSystem, new List()); @@ -420,7 +439,7 @@ namespace NewHorizons OnChangeStarSystem?.Invoke(newStarSystem); Logger.Log($"Warping to {newStarSystem}"); - if(warp && _shipWarpController) _shipWarpController.WarpOut(); + if (warp && _shipWarpController) _shipWarpController.WarpOut(); _currentStarSystem = newStarSystem; _isChangingStarSystem = true; IsWarping = warp; @@ -429,7 +448,7 @@ namespace NewHorizons // We kill them so they don't move as much Locator.GetDeathManager().KillPlayer(DeathType.Meditation); - if(newStarSystem == "EyeOfTheUniverse") + if (newStarSystem == "EyeOfTheUniverse") { PlayerData.SaveWarpedToTheEye(TimeLoop.GetSecondsRemaining()); LoadManager.LoadSceneAsync(OWScene.EyeOfTheUniverse, true, LoadManager.FadeType.ToBlack, 0.1f, true); @@ -446,7 +465,17 @@ namespace NewHorizons // We reset the solar system on death (unless we just killed the player) if (!_isChangingStarSystem) { - _currentStarSystem = _defaultStarSystem; + // If the override is a valid system then we go there + if (SystemDict.Keys.Contains(_defaultSystemOverride)) + { + _currentStarSystem = _defaultSystemOverride; + IsWarping = true; // always do this else sometimes the spawn gets messed up + } + else + { + _currentStarSystem = _defaultStarSystem; + } + IsWarping = false; } }