diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index 506630b5..495f47ca 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -446,15 +446,15 @@ namespace NewHorizons if (!foundFile) Logger.LogWarning($"{mod.ModHelper.Manifest.Name} has a folder for translations but none were loaded"); } - public NewHorizonsBody LoadConfig(IModBehaviour mod, string relativeDirectory) + public NewHorizonsBody LoadConfig(IModBehaviour mod, string relativePath) { NewHorizonsBody body = null; try { - var config = mod.ModHelper.Storage.Load(relativeDirectory); + var config = mod.ModHelper.Storage.Load(relativePath); if (config == null) { - Logger.LogError($"Couldn't load {relativeDirectory}. Is your Json formatted correctly?"); + Logger.LogError($"Couldn't load {relativePath}. Is your Json formatted correctly?"); return null; } @@ -479,11 +479,11 @@ namespace NewHorizons // Has to happen after we make sure theres a system config config.MigrateAndValidate(); - body = new NewHorizonsBody(config, mod, relativeDirectory); + body = new NewHorizonsBody(config, mod, relativePath); } catch (Exception e) { - Logger.LogError($"Error encounter when loading {relativeDirectory}: {e.Message} {e.StackTrace}"); + Logger.LogError($"Error encounter when loading {relativePath}: {e.Message} {e.StackTrace}"); } return body; diff --git a/NewHorizons/Utility/DebugMenu/DebugMenu.cs b/NewHorizons/Utility/DebugMenu/DebugMenu.cs index ca306e27..c33e77f5 100644 --- a/NewHorizons/Utility/DebugMenu/DebugMenu.cs +++ b/NewHorizons/Utility/DebugMenu/DebugMenu.cs @@ -28,17 +28,16 @@ namespace NewHorizons.Utility.DebugMenu static bool openMenuOnPause; static bool staticInitialized; - // menu params + // Menu params internal static IModBehaviour loadedMod = null; internal Dictionary loadedConfigFiles = new Dictionary(); private bool saveButtonUnlocked = false; private Vector2 recentModListScrollPosition = Vector2.zero; - // submenus + // Submenus private List submenus; private int activeSubmenu = 0; - private static JsonSerializerSettings jsonSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, @@ -55,7 +54,6 @@ namespace NewHorizons.Utility.DebugMenu new DebugMenuNomaiText() }; - submenus.ForEach((submenu) => submenu.OnAwake(this)); } @@ -89,6 +87,7 @@ namespace NewHorizons.Utility.DebugMenu pauseMenuButton = Main.Instance.ModHelper.Menus.PauseMenu.OptionsButton.Duplicate(TranslationHandler.GetTranslation("Toggle Dev Tools Menu", TranslationHandler.TextType.UI).ToUpper()); InitMenu(); } + public static void UpdatePauseMenuButton() { if (pauseMenuButton != null) @@ -99,11 +98,11 @@ namespace NewHorizons.Utility.DebugMenu } private void RestoreMenuOpennessState() { menuOpen = openMenuOnPause; } + private void ToggleMenu() { menuOpen = !menuOpen; openMenuOnPause = !openMenuOnPause; } private void CloseMenu() { menuOpen = false; } - private void OnGUI() { if (!menuOpen) return; @@ -113,8 +112,7 @@ namespace NewHorizons.Utility.DebugMenu GUILayout.BeginArea(new Rect(menuPosition.x, menuPosition.y, EditorMenuSize.x, EditorMenuSize.y), _editorMenuStyle); - // continue working on existing mod - + // Continue working on existing mod GUILayout.Label("Name of your mod"); if (loadedMod == null) { @@ -138,9 +136,9 @@ namespace NewHorizons.Utility.DebugMenu GUILayout.Space(5); - // save your work - - if (loadedMod != null) { + // Save your work + if (loadedMod != null) + { GUILayout.BeginHorizontal(); if (GUILayout.Button(saveButtonUnlocked ? " O " : " | ", GUILayout.ExpandWidth(false))) { @@ -157,23 +155,23 @@ namespace NewHorizons.Utility.DebugMenu } GUILayout.Space(20); - - // draw submenu stuff + + // Draw submenu stuff if (loadedMod != null) { GUILayout.BeginHorizontal(_tabBarStyle); GUILayout.Space(5); - for (int i = 0; i < submenus.Count; i++) + for (int i = 0; i < submenus.Count; i++) { GUI.enabled = i != activeSubmenu; var style = i == activeSubmenu ? _submenuStyle : _tabBarStyle; - if (GUILayout.Button(" "+submenus[i].SubmenuName()+" ", style, GUILayout.ExpandWidth(false))) - { + if (GUILayout.Button(" " + submenus[i].SubmenuName() + " ", style, GUILayout.ExpandWidth(false))) + { GUI.enabled = true; submenus[activeSubmenu].LoseActive(); activeSubmenu = i; submenus[activeSubmenu].GainActive(); - + } GUI.enabled = true; @@ -186,7 +184,6 @@ namespace NewHorizons.Utility.DebugMenu submenus[activeSubmenu].OnGUI(this); GUILayout.EndVertical(); } - GUILayout.EndArea(); } @@ -198,15 +195,16 @@ namespace NewHorizons.Utility.DebugMenu var folder = loadedMod.ModHelper.Manifest.ModFolderPath; - List bodiesForThisMod = Main.BodyDict.Values.SelectMany(x => x).Where(x => x.Mod == loadedMod).ToList(); - foreach (NewHorizonsBody body in bodiesForThisMod) + var bodiesForThisMod = Main.BodyDict.Values.SelectMany(x => x).Where(x => x.Mod == loadedMod).ToList(); + foreach (var body in bodiesForThisMod) { if (body.RelativePath == null) { - Logger.Log("Error loading config for " + body.Config.name + " in " + body.Config.starSystem); + Logger.Log($"Error loading config for {body.Config.name} in {body.Config.starSystem}"); + continue; } - loadedConfigFiles[folder + body.RelativePath] = (body.Config as PlanetConfig); + loadedConfigFiles[folder + body.RelativePath] = body.Config; submenus.ForEach(submenu => submenu.LoadConfigFile(this, body.Config)); } } @@ -215,12 +213,14 @@ namespace NewHorizons.Utility.DebugMenu { submenus.ForEach(submenu => submenu.PreSave(this)); - string backupFolderName = "configBackups\\" + DateTime.Now.ToString("yyyyMMddTHHmmss") + "\\"; + var backupFolderName = $"configBackups\\{DateTime.Now.ToString("yyyyMMddTHHmmss")}\\"; + Logger.Log($"Potentially saving {loadedConfigFiles.Keys.Count} files"); foreach (var filePath in loadedConfigFiles.Keys) { - Logger.Log("Possibly Saving... " + loadedConfigFiles[filePath].name + " @ " + filePath); + Logger.Log($"Possibly Saving... {loadedConfigFiles[filePath].name} @ {filePath}"); + if (loadedConfigFiles[filePath].starSystem != Main.Instance.CurrentStarSystem) continue; var relativePath = filePath.Replace(loadedMod.ModHelper.Manifest.ModFolderPath, ""); @@ -231,14 +231,17 @@ namespace NewHorizons.Utility.DebugMenu try { - Logger.Log("Saving... " + relativePath + " to " + filePath); + Logger.Log($"Saving... {relativePath} to {filePath}"); var path = loadedMod.ModHelper.Manifest.ModFolderPath + relativePath; var directoryName = Path.GetDirectoryName(path); Directory.CreateDirectory(directoryName); File.WriteAllText(path, json); } - catch (Exception e) { Logger.LogError("Failed to save file " + backupFolderName + relativePath); Logger.LogError(e.Message + "\n" + e.StackTrace); } + catch (Exception e) + { + Logger.LogError($"Failed to save file {relativePath}:\n{e.Message}\n{e.StackTrace}"); + } try { @@ -248,7 +251,10 @@ namespace NewHorizons.Utility.DebugMenu File.WriteAllText(path, json); } - catch (Exception e) { Logger.LogError("Failed to save backup file " + backupFolderName + relativePath); Logger.LogError(e.Message + "\n" + e.StackTrace); } + catch (Exception e) + { + Logger.LogError($"Failed to save backup file {backupFolderName}{relativePath}:\n{e.Message}\n{e.StackTrace}"); + } } } @@ -262,7 +268,6 @@ namespace NewHorizons.Utility.DebugMenu pauseMenuButton.OnClick += ToggleMenu; submenus.ForEach(submenu => submenu.OnInit(this)); - _editorMenuStyle = new GUIStyle {