Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.

Commit

Permalink
simplify part blocking search
Browse files Browse the repository at this point in the history
  • Loading branch information
Crzyrndm committed May 7, 2016
1 parent 9c87394 commit 31fd515
Showing 1 changed file with 33 additions and 41 deletions.
74 changes: 33 additions & 41 deletions FilterExtension/Editor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using KSP.UI.Screens;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -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());
Expand All @@ -27,14 +28,16 @@ void Start()
/// </summary>
public static HashSet<string> 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
Expand All @@ -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)
{
Expand All @@ -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();
}

Expand All @@ -84,7 +87,7 @@ IEnumerator editorInit()
c.initialise();
}

//
//
foreach (PartCategorizer.Category c in PartCategorizer.Instance.filters)
namesAndIcons(c);

Expand Down Expand Up @@ -172,43 +175,32 @@ public static void setSelectedCategory()
}
}

/// <summary>
/// checks all subcats not created by FE for visibility of parts set to "category = none"
/// </summary>
void findPartsToBlock()
private void findPartsToBlock()
{
PartModuleFilter pmf;
// all parts that may not be visible
List<AvailablePart> partsToCheck = PartLoader.Instance.parts.FindAll(ap => ap.category == PartCategories.none
&& !(Core.Instance.filterModules.TryGetValue(ap.name, out pmf) && pmf.hasForceAdd()));
blackListedParts = new HashSet<string>();

List<PartCategorizer.Category> subcategoriesToCheck = new List<PartCategorizer.Category>();

// 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<string> subCatsSeen = new HashSet<string>();
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<string>();
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;
}
}
}
}

0 comments on commit 31fd515

Please sign in to comment.