diff --git a/FilterExtension/ConfigNodes/customSubCategory.cs b/FilterExtension/ConfigNodes/customSubCategory.cs index ab717d80..a21534e7 100644 --- a/FilterExtension/ConfigNodes/customSubCategory.cs +++ b/FilterExtension/ConfigNodes/customSubCategory.cs @@ -89,7 +89,7 @@ public bool checkFilters(AvailablePart part, int depth = 0) if (part.category == PartCategories.none && Editor.blackListedParts.Contains(part.name)) return false; } - if (!unPurchasedOverride && Core.Instance.hideUnpurchased && !ResearchAndDevelopment.PartModelPurchased(part) && !ResearchAndDevelopment.IsExperimentalPart(part)) + if (!unPurchasedOverride && Settings.hideUnpurchased && !ResearchAndDevelopment.PartModelPurchased(part) && !ResearchAndDevelopment.IsExperimentalPart(part)) return false; PartModuleFilter pmf; diff --git a/FilterExtension/Core.cs b/FilterExtension/Core.cs index b7307dc0..a41ca8f6 100644 --- a/FilterExtension/Core.cs +++ b/FilterExtension/Core.cs @@ -40,12 +40,7 @@ public class Core : MonoBehaviour // Config has options to disable the FbM replacement, and the default Category/SC and sort method - public bool hideUnpurchased = true; - public bool debug = false; - public bool setAdvanced = true; - public bool replaceFbM = true; - public string categoryDefault = ""; - public string subCategoryDefault = ""; + const string fallbackIcon = "stockIcon_fallback"; @@ -75,18 +70,7 @@ void Awake() /// private void getConfigs() { - ConfigNode settings = GameDatabase.Instance.GetConfigNodes("FilterSettings").FirstOrDefault(); - if (settings != null) - { - bool.TryParse(settings.GetValue("hideUnpurchased"), out hideUnpurchased); - bool.TryParse(settings.GetValue("debug"), out debug); - if (!bool.TryParse(settings.GetValue("setAdvanced"), out setAdvanced)) - setAdvanced = true; - if (!bool.TryParse(settings.GetValue("replaceFbM"), out replaceFbM)) - replaceFbM = true; - categoryDefault = settings.GetValue("categoryDefault"); - subCategoryDefault = settings.GetValue("subCategoryDefault"); - } + Settings.LoadSettings(); ConfigNode[] nodes = GameDatabase.Instance.GetConfigNodes("FilterRename"); for (int i = 0; i < nodes.Length; i++) @@ -179,8 +163,8 @@ private void getPartData() if (p.partPrefab.Modules.Contains("PartModuleFilter")) filterModules.Add(p.name, (PartModuleFilter)p.partPrefab.Modules["PartModuleFilter"]); } - - if (replaceFbM) + + if (Settings.replaceFbM) processFilterByManufacturer(modNames); } @@ -371,8 +355,8 @@ public static void setSelectedCategory() PartCategorizer.Category Filter = PartCategorizer.Instance.filters.FirstOrDefault(f => f.button.activeButton.State == RUIToggleButtonTyped.ButtonState.TRUE); if (Filter != null) Filter.button.activeButton.SetFalse(Filter.button.activeButton, RUIToggleButtonTyped.ClickType.FORCED); - - Filter = PartCategorizer.Instance.filters.FirstOrDefault(f => f.button.categoryName == instance.categoryDefault); + + Filter = PartCategorizer.Instance.filters.FirstOrDefault(f => f.button.categoryName == Settings.categoryDefault); if (Filter != null) Filter.button.activeButton.SetTrue(Filter.button.activeButton, RUIToggleButtonTyped.ClickType.FORCED); else @@ -499,7 +483,7 @@ private static void loadIcons() i++; if (i != 1000) name = name + i.ToString(); - if (instance.debug) + if (Settings.debug) Log("Duplicated texture name \"" + t.name.Split(new char[] { '/', '\\' }).Last() + "\" at:\r\n" + t.name + "\r\n New reference is: " + name); } @@ -571,7 +555,7 @@ public static bool checkSubCategoryHasParts(customSubCategory sC, string categor return true; } - if (instance.debug) + if (Settings.debug) { if (!string.IsNullOrEmpty(category)) Log(sC.subCategoryTitle + " in category " + category + " has no valid parts and was not initialised"); diff --git a/FilterExtension/Editor.cs b/FilterExtension/Editor.cs index f0181531..211a3425 100644 --- a/FilterExtension/Editor.cs +++ b/FilterExtension/Editor.cs @@ -31,7 +31,7 @@ IEnumerator editorInit() while (PartCategorizer.Instance == null) yield return null; - if (Core.Instance.debug) + if (Settings.debug) Core.Log("Starting on Stock Filters"); // stock filters // If I edit them later everything breaks @@ -55,7 +55,7 @@ IEnumerator editorInit() // frames after the flag is set to wait before initialising. Minimum of two for things to work consistently for (int i = 0; i < 4; i++) yield return null; - if (Core.Instance.debug) + if (Settings.debug) Core.Log("Starting on general categories"); // all FE categories @@ -68,7 +68,7 @@ IEnumerator editorInit() // wait again so icon edits don't occur immediately and cause breakages for (int i = 0; i < 4; i++) yield return null; - if (Core.Instance.debug) + if (Settings.debug) Core.Log("Starting on late categories"); // generate the set of parts to block @@ -92,7 +92,7 @@ IEnumerator editorInit() // Remove any category with no subCategories (causes major breakages if selected). for (int i = 0; i < 4; i++) yield return null; - if (Core.Instance.debug) + if (Settings.debug) Core.Log("Starting on removing categories"); List catsToDelete = PartCategorizer.Instance.filters.FindAll(c => c.subcategories.Count == 0); foreach (PartCategorizer.Category cat in catsToDelete) @@ -102,12 +102,12 @@ IEnumerator editorInit() } // make the categories visible - if (Core.Instance.setAdvanced) + if (Settings.setAdvanced) PartCategorizer.Instance.SetAdvancedMode(); for (int i = 0; i < 4; i++) yield return null; - if (Core.Instance.debug) + if (Settings.debug) Core.Log("Refreshing parts list"); Core.setSelectedCategory(); diff --git a/FilterExtension/FilterExtensions.csproj b/FilterExtension/FilterExtensions.csproj index 94a0dac9..f887a3d7 100644 --- a/FilterExtension/FilterExtensions.csproj +++ b/FilterExtension/FilterExtensions.csproj @@ -55,6 +55,7 @@ + diff --git a/FilterExtension/Settings.cs b/FilterExtension/Settings.cs index 5bced1ac..844a8508 100644 --- a/FilterExtension/Settings.cs +++ b/FilterExtension/Settings.cs @@ -1,10 +1,13 @@ using System; using System.Collections.Generic; using System.Linq; +using System.IO; using UnityEngine; namespace FilterExtensions { + using Utility; + [KSPAddon(KSPAddon.Startup.SpaceCentre, false)] class Settings : MonoBehaviour { @@ -12,6 +15,15 @@ class Settings : MonoBehaviour static bool showWindow; private static ApplicationLauncherButton btnLauncher; + public const string RelativeSettingsPath = "GameData/000_FilterExtensions/PluginData/"; + + public static bool hideUnpurchased = true; + public static bool debug = false; + public static bool setAdvanced = true; + public static bool replaceFbM = true; + public static string categoryDefault = ""; + public static string subCategoryDefault = ""; + public void Start() { showWindow = false; @@ -19,23 +31,13 @@ public void Start() btnLauncher = ApplicationLauncher.Instance.AddModApplication(() => showWindow = !showWindow, () => showWindow = !showWindow, null, null, null, null, ApplicationLauncher.AppScenes.SPACECENTER, GameDatabase.Instance.GetTexture("000_FilterExtensions/Icons/FilterCreator", false)); + + LoadSettings(); } public void OnDestroy() { - ConfigNode settingsNode = new ConfigNode("FilterSettings"); - settingsNode.AddValue("hideUnpurchased", Core.Instance.hideUnpurchased); - settingsNode.AddValue("debug", Core.Instance.debug); - settingsNode.AddValue("setAdvanced", Core.Instance.setAdvanced); - settingsNode.AddValue("replaceFbM", Core.Instance.replaceFbM); - settingsNode.AddValue("categoryDefault", Core.Instance.categoryDefault); - settingsNode.AddValue("subCategoryDefault", Core.Instance.subCategoryDefault); - - ConfigNode nodeToWrite = new ConfigNode(); - nodeToWrite.AddNode(settingsNode); - - #warning Need to move the save location inside PluginData so MM can't see it, cache it, and then recache it when settings are changed (or maybe just change the extension. Same effect though) - nodeToWrite.Save(KSPUtil.ApplicationRootPath.Replace("\\", "/") + "GameData/000_FilterExtensions/Settings.cfg"); + SaveSettings(); } public void OnGUI() @@ -46,22 +48,53 @@ public void OnGUI() settingsRect = GUILayout.Window(6548792, settingsRect, drawWindow, "Filter Extensions Settings"); } - private void drawWindow(int id) + public static void LoadSettings() { - Core.Instance.debug = GUILayout.Toggle(Core.Instance.debug, "Enable logging"); - Core.Instance.setAdvanced = GUILayout.Toggle(Core.Instance.setAdvanced, "Default to Advanced mode"); - Core.Instance.hideUnpurchased = GUILayout.Toggle(Core.Instance.hideUnpurchased, "Hide unpurchased parts"); - Core.Instance.replaceFbM = GUILayout.Toggle(Core.Instance.replaceFbM, "Sort parts by folder in manufacturer tab (requires restart)"); + if (File.Exists(KSPUtil.ApplicationRootPath.Replace("\\", "/") + RelativeSettingsPath + "Settings.cfg")) + { + ConfigNode settings = ConfigNode.Load(KSPUtil.ApplicationRootPath.Replace("\\", "/") + RelativeSettingsPath + "Settings.cfg"); + if (settings != null) + { + bool.TryParse(settings.GetValue("hideUnpurchased"), out hideUnpurchased); + bool.TryParse(settings.GetValue("debug"), out debug); + if (!bool.TryParse(settings.GetValue("setAdvanced"), out setAdvanced)) + setAdvanced = true; + if (!bool.TryParse(settings.GetValue("replaceFbM"), out replaceFbM)) + replaceFbM = true; + categoryDefault = settings.GetValue("categoryDefault"); + if (categoryDefault == null) + categoryDefault = string.Empty; + subCategoryDefault = settings.GetValue("subCategoryDefault"); + if (subCategoryDefault == null) + subCategoryDefault = string.Empty; + } + } + } + + public static void SaveSettings() + { + ConfigNode settingsNode = new ConfigNode("FilterSettings"); + settingsNode.AddValue("hideUnpurchased", hideUnpurchased); + settingsNode.AddValue("debug", debug); + settingsNode.AddValue("setAdvanced", setAdvanced); + settingsNode.AddValue("replaceFbM", replaceFbM); + settingsNode.AddValue("categoryDefault", categoryDefault); + settingsNode.AddValue("subCategoryDefault", subCategoryDefault); - GUILayout.BeginHorizontal(); - GUILayout.Label("Default Category"); - Core.Instance.categoryDefault = GUILayout.TextField(Core.Instance.categoryDefault); - GUILayout.EndHorizontal(); + if (!Directory.Exists(KSPUtil.ApplicationRootPath.Replace("\\", "/") + RelativeSettingsPath)) + Directory.CreateDirectory(KSPUtil.ApplicationRootPath.Replace("\\", "/") + RelativeSettingsPath); + settingsNode.Save(KSPUtil.ApplicationRootPath.Replace("\\", "/") + RelativeSettingsPath + "Settings.cfg"); + } + + private void drawWindow(int id) + { + debug = GUILayout.Toggle(debug, "Enable logging"); + setAdvanced = GUILayout.Toggle(setAdvanced, "Default to Advanced mode"); + hideUnpurchased = GUILayout.Toggle(hideUnpurchased, "Hide unpurchased parts"); + replaceFbM = GUILayout.Toggle(replaceFbM, "Sort parts by folder in manufacturer tab (requires restart)"); - GUILayout.BeginHorizontal(); - GUILayout.Label("Default Sub-category"); - Core.Instance.subCategoryDefault = GUILayout.TextField(Core.Instance.subCategoryDefault); - GUILayout.EndHorizontal(); + GUIUtils.DrawLabelPlusBox("Default Category", ref categoryDefault); + GUIUtils.DrawLabelPlusBox("Default Sub-category", ref subCategoryDefault); GUI.DragWindow(); } diff --git a/FilterExtension/Utility/GUIUtils.cs b/FilterExtension/Utility/GUIUtils.cs new file mode 100644 index 00000000..09924255 --- /dev/null +++ b/FilterExtension/Utility/GUIUtils.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using UnityEngine; + +namespace FilterExtensions.Utility +{ + public static class GUIUtils + { + /// + /// Draws a label and text box on the same row + /// + /// + /// + public static void DrawLabelPlusBox(string label, ref string text) + { + GUILayout.BeginHorizontal(); + GUILayout.Label(label); + text = GUILayout.TextField(text); + GUILayout.EndHorizontal(); + } + } +} diff --git a/GameData/000_FilterExtensions/FilterExtensions.dll b/GameData/000_FilterExtensions/FilterExtensions.dll index fb748e53..4372c74e 100644 Binary files a/GameData/000_FilterExtensions/FilterExtensions.dll and b/GameData/000_FilterExtensions/FilterExtensions.dll differ