diff --git a/FilterExtension/Editor.cs b/FilterExtension/Editor.cs index b1ff46a9..e396e000 100644 --- a/FilterExtension/Editor.cs +++ b/FilterExtension/Editor.cs @@ -1,4 +1,5 @@ -using System; +using KSP.UI.Screens; +using System; using System.Collections; using System.Collections.Generic; using System.Linq; @@ -8,15 +9,15 @@ namespace FilterExtensions { using ConfigNodes; using Utility; - using KSP.UI.Screens; [KSPAddon(KSPAddon.Startup.EditorAny, false)] - class Editor : MonoBehaviour + public class Editor : MonoBehaviour { public static Editor instance; public static bool subcategoriesChecked; public bool ready = false; - void Start() + + public void Start() { instance = this; StartCoroutine(editorInit()); @@ -27,14 +28,16 @@ void Start() /// public static HashSet blackListedParts; - IEnumerator editorInit() + private IEnumerator editorInit() { ready = false; while (PartCategorizer.Instance == null) yield return null; + if (Settings.debug) Core.Log("Starting on Stock Filters"); + // stock filters // If I edit them later everything breaks // custom categories can't be created at this point @@ -56,7 +59,7 @@ IEnumerator editorInit() yield return null; if (Settings.debug) Core.Log("Starting on general categories"); - + // all FE categories foreach (customCategory c in Core.Instance.Categories) { @@ -73,7 +76,7 @@ IEnumerator editorInit() // generate the set of parts to block if (blackListedParts == null) { - #warning not known until now which parts are never visible so some completely empty subcategories may be present on the first VAB entry +#warning not known until now which parts are never visible so some completely empty subcategories may be present on the first VAB entry findPartsToBlock(); } @@ -84,7 +87,7 @@ IEnumerator editorInit() c.initialise(); } - // + // foreach (PartCategorizer.Category c in PartCategorizer.Instance.filters) namesAndIcons(c); @@ -172,43 +175,32 @@ public static void setSelectedCategory() } } - /// - /// checks all subcats not created by FE for visibility of parts set to "category = none" - /// - void findPartsToBlock() + private void findPartsToBlock() { - PartModuleFilter pmf; - // all parts that may not be visible - List partsToCheck = PartLoader.Instance.parts.FindAll(ap => ap.category == PartCategories.none - && !(Core.Instance.filterModules.TryGetValue(ap.name, out pmf) && pmf.hasForceAdd())); + blackListedParts = new HashSet(); + + List subcategoriesToCheck = new List(); + // Only checking the category which should be Filter by Function (should I find FbF explcitly?) PartCategorizer.Category mainCat = PartCategorizer.Instance.filters[0]; - // has a reference to all the subcats that FE added to the category - customCategory customMainCat = Core.Instance.Categories.Find(C => C.categoryName == mainCat.button.categoryName); - // loop through the subcategories. Mark FE ones as seen incase of duplication and check the shortlisted parts against other mods categories for visibility - HashSet subCatsSeen = new HashSet(); - for (int i = 0; i < mainCat.subcategories.Count; i++) + + AvailablePart part; + for (int i = 0; i < PartLoader.Instance.parts.Count; ++i) { - PartCategorizer.Category subCat = mainCat.subcategories[i]; - // if the name is an FE subcat and the category should have that FE subcat and it's not the duplicate of one already seen created by another mod, mark it seen and move on - if (Core.Instance.subCategoriesDict.ContainsKey(subCat.button.categoryName) && customMainCat.subCategories.Any(subItem => string.Equals(subItem.subcategoryName, subCat.button.categoryName, StringComparison.CurrentCulture))) - subCatsSeen.Add(subCat.button.categoryName); - else // subcat created by another mod - { - int j = 0; - while (j < partsToCheck.Count) - { - if (subCat.exclusionFilter.FilterCriteria.Invoke(partsToCheck[j])) // if visible - partsToCheck.RemoveAt(j); - else - j++; - } - } + part = PartLoader.Instance.parts[i]; + if (part.category == PartCategories.none && !checkPartVisible(part, mainCat)) + blackListedParts.Add(part.name); } - // add the blocked parts to a hashset for later lookup - blackListedParts = new HashSet(); - foreach (AvailablePart ap in partsToCheck) - blackListedParts.Add(ap.name); + } + + private bool checkPartVisible(AvailablePart part, PartCategorizer.Category category) + { + for (int i = 0; i < category.subcategories.Count; ++i) + { + if (category.subcategories[i].exclusionFilter.FilterCriteria.Invoke(part)) + return true; + } + return false; } } -} +} \ No newline at end of file