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

Commit

Permalink
exclude subcategories from template
Browse files Browse the repository at this point in the history
  • Loading branch information
Crzyrndm committed Jul 21, 2015
1 parent 07e6f25 commit 2615d9c
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,4 @@ FakesAssemblies/

PartDatabase.cfg
Physics.cfg
Testing/
90 changes: 75 additions & 15 deletions FilterExtension/ConfigNodes/customCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class customCategory : IEquatable<customCategory>
public Color colour { get; set; }
public categoryTypeAndBehaviour behaviour { get; set; }
public bool all { get; set; } // has an all parts subCategory
public List<string> subCategories { get; set; } // array of subcategories
public List<subCategoryItem> subCategories { get; set; } // array of subcategories
public List<Filter> templates { get; set; } // Checks to add to every Filter in a category with the template tag

private static readonly List<string> categoryNames = new List<string> { "Pods", "Engines", "Fuel Tanks", "Command and Control", "Structural", "Aerodynamics", "Utility", "Science" };
Expand All @@ -40,27 +40,43 @@ public customCategory(ConfigNode node)
this.all = tmp;

ConfigNode subcategoryList = node.GetNode("SUBCATEGORIES", 0);
subCategories = new List<subCategoryItem>();
if (subcategoryList != null)
{
List<string> unorderedSubCats = new List<string>();
List<subCategoryItem> unorderedSubCats = new List<subCategoryItem>();
string[] stringList = subcategoryList.GetValues();
string[] subs = new string[1000];
subCategoryItem[] subs = new subCategoryItem[1000];
for (int i = 0; i < stringList.Length; i++)
{
string[] indexAndValue = stringList[i].Split(',').Select(s => s.Trim()).ToArray();

subCategoryItem newSubItem = new subCategoryItem();
int index;
if (int.TryParse(indexAndValue[0], out index)) // has position index
{
if (indexAndValue.Length >= 2)
subs[index] = indexAndValue[1];
newSubItem.subcategoryName = indexAndValue[1];
if (string.IsNullOrEmpty(newSubItem.subcategoryName))
continue;

if (indexAndValue.Length >= 3 && string.Equals(indexAndValue[2], "dont template", StringComparison.CurrentCultureIgnoreCase))
newSubItem.applyTemplate = false;
subs[index] = newSubItem;
}
else // no valid position index
{
unorderedSubCats.Add(indexAndValue[0]);
newSubItem.subcategoryName = indexAndValue[0];
if (string.IsNullOrEmpty(newSubItem.subcategoryName))
continue;

if (indexAndValue.Length >= 2 && string.Equals(indexAndValue[1], "dont template", StringComparison.CurrentCultureIgnoreCase))
newSubItem.applyTemplate = false;
unorderedSubCats.Add(newSubItem);
}
}
subCategories = subs.Distinct().ToList(); // no duplicates and no gaps in a single line. Yay
subCategories.AddUniqueRange(unorderedSubCats); // tack unordered subcats on to the end
subCategories.RemoveAll(s => s == null);
}
typeSwitch(node.GetValue("type"), node.GetValue("value"));
}
Expand Down Expand Up @@ -92,21 +108,28 @@ public void initialise()
Core.Log("No stock category of this name was found: " + categoryName);
return;
}

List<string> subcategoryNames = new List<string>();
for (int i = 0; i < subCategories.Count; i++ )
subcategoryNames.Add(subCategories[i].subcategoryName);

for (int i = 0; i < subCategories.Count; i++)
{
string subcategoryName = subCategories[i];
subCategoryItem subcategoryItem = subCategories[i];
if (subcategoryItem == null)
continue;

customSubCategory subcategory = null;
if (string.IsNullOrEmpty(subcategoryName) || !Core.Instance.subCategoriesDict.TryGetValue(subcategoryName, out subcategory))
if (string.IsNullOrEmpty(subcategoryItem.subcategoryName) || !Core.Instance.subCategoriesDict.TryGetValue(subcategoryItem.subcategoryName, out subcategory))
continue;

List<string> conflictsList;
if (Core.Instance.conflictsDict.TryGetValue(subcategoryName, out conflictsList))
if (Core.Instance.conflictsDict.TryGetValue(subcategoryItem.subcategoryName, out conflictsList))
{
// all of the possible conflicts that are also subcategories of this category
List<string> conflicts = conflictsList.Intersect(subCategories).ToList();
List<string> conflicts = conflictsList.Intersect(subcategoryNames).ToList();
// if there are any conflicts that show up in the subcategories list before this one
if (conflicts.Any(c => subCategories.IndexOf(c) < i))
if (conflicts.Any(c => subcategoryNames.IndexOf(c) < i))
{
string conflictList = "";
foreach (string s in conflicts)
Expand All @@ -116,8 +139,8 @@ public void initialise()
}
}

customSubCategory sC = new customSubCategory(subcategory.toConfigNode());
if (templates != null && templates.Any())
customSubCategory sC = new customSubCategory(subcategory.toConfigNode());
if (subcategoryItem.applyTemplate && templates != null && templates.Any())
{
List<Filter> baseSubCatFilters = new List<Filter>();
foreach (Filter f in sC.filters)
Expand Down Expand Up @@ -148,7 +171,7 @@ public void initialise()
}
}

#warning Need another type which runs after other mods for editing thier categories
#warning Need another type which runs after other mods for editing their categories
private void typeSwitch(string type, string value)
{
switch (type)
Expand All @@ -171,7 +194,7 @@ private void typeSwitch(string type, string value)

private void generateEngineTypes()
{
List<string> engines = new List<string>();
List<subCategoryItem> engines = new List<subCategoryItem>();
for (int i = 0; i < Core.Instance.propellantCombos.Count; i++ )
{
List<string> ls = Core.Instance.propellantCombos[i];
Expand Down Expand Up @@ -200,7 +223,8 @@ private void generateEngineTypes()
sC.filters.Add(f);
Core.Instance.subCategoriesDict.Add(name, sC);
}
engines.Add(name);
if (!string.IsNullOrEmpty(name))
engines.Add(new subCategoryItem(name));
}
if (subCategories != null)
subCategories.AddUniqueRange(engines);
Expand Down Expand Up @@ -287,4 +311,40 @@ public bool stockCategory
}
}
}

public class subCategoryItem : IEquatable<subCategoryItem>
{
public string subcategoryName { get; set; }
public bool applyTemplate { get; set; }

public subCategoryItem()
{
applyTemplate = true;
}
public subCategoryItem(string name, bool useTemplate = true)
{
subcategoryName = name;
applyTemplate = useTemplate;
}

public bool Equals(subCategoryItem sub)
{
if (ReferenceEquals(null, sub))
return false;
if (ReferenceEquals(this, sub))
return true;

return subcategoryName.Equals(sub.subcategoryName);
}

public override int GetHashCode()
{
return subcategoryName.GetHashCode();
}

public override string ToString()
{
return subcategoryName;
}
}
}
30 changes: 17 additions & 13 deletions FilterExtension/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ void Awake()

getConfigs();
getPartData();

// load all category configs
processFilterDefinitions();

loadIcons();
checkAndMarkConflicts();
}
Expand Down Expand Up @@ -198,7 +195,7 @@ private void processFilterDefinitions()
if (!Categories.Any(n => n.categoryName == C.categoryName))
Categories.Add(C);
}

//load all subCategory configs
nodes = GameDatabase.Instance.GetConfigNodes("SUBCATEGORY");
for (int i = 0; i < nodes.Length; i++)
Expand All @@ -214,7 +211,7 @@ private void processFilterDefinitions()
else // if nothing else has the same title
subCategoriesDict.Add(sC.subCategoryTitle, sC);
}

customCategory Cat = Categories.Find(C => C.categoryName == "Filter by Resource");
if (Cat != null)
{
Expand All @@ -241,31 +238,35 @@ private void processFilterDefinitions()
sC.filters.Add(f);
subCategoriesDict.Add(name, sC);
}
Cat.subCategories.AddUnique(name);
if (!string.IsNullOrEmpty(name))
Cat.subCategories.AddUnique(new subCategoryItem(name));
}
}

for (int i = 0; i < Categories.Count; i++)
{
customCategory C = Categories[i];

if (!C.all)
if (C == null || !C.all)
continue;

List<Filter> filterList = new List<Filter>();
if (C.subCategories != null)
{
for (int j = 0; j < C.subCategories.Count; j++)
{
string s = C.subCategories[j];
subCategoryItem s = C.subCategories[j];
if (s == null)
continue;

customSubCategory subcategory;
if (!string.IsNullOrEmpty(s) && subCategoriesDict.TryGetValue(s, out subcategory))
if (subCategoriesDict.TryGetValue(s.subcategoryName, out subcategory))
filterList.AddUniqueRange(subcategory.filters);
}
}
customSubCategory newSub = new customSubCategory("All parts in " + C.categoryName, C.iconName);
newSub.filters = filterList;
subCategoriesDict.Add(newSub.subCategoryTitle, newSub);
C.subCategories.Insert(0, newSub.subCategoryTitle);
C.subCategories.Insert(0, new subCategoryItem(newSub.subCategoryTitle));
}
}

Expand Down Expand Up @@ -334,7 +335,10 @@ private void processFilterByManufacturer(List<string> modNames)
Categories.Add(new customCategory(filterByManufacturer));
}
else
fbm.subCategories.AddUniqueRange(modNames); // append the mod names
{
for (int i = 0; i < modNames.Count; i++)
fbm.subCategories.AddUnique(new subCategoryItem(modNames[i])); // append the mod names
}
}

/// <summary>
Expand Down Expand Up @@ -557,7 +561,7 @@ private void RepairAvailablePartUrl(AvailablePart ap)
}

/// <summary>
/// if a subcategory doesn't have any parts, it shouldn't be used
/// if a subcategory doesn't have any parts, it shouldn't be used. Doesn't account for the blackListed parts the first time the editor is entered
/// </summary>
/// <param name="sC">the subcat to check</param>
/// <param name="category">the category for logging purposes</param>
Expand Down
26 changes: 25 additions & 1 deletion FilterExtension/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,31 @@ IEnumerator editorInit()
if (Core.Instance.debug)
Core.Log("Starting on setting names and icons");
if (blackListedParts == null)
{
findPartsToBlock();
// not known until now which parts are never visible so some empty subcategories will be present
for (int i = 0; i < PartCategorizer.Instance.filters.Count; i++)
{
List<PartCategorizer.Category> subCatsToDelete = new List<PartCategorizer.Category>();
PartCategorizer.Category C = PartCategorizer.Instance.filters[i];
if (C == null)
continue;
for (int j = 0; j < C.subcategories.Count; j++)
{
PartCategorizer.Category sub = C.subcategories[j];
if (sub == null)
continue;

if (!PartLoader.Instance.parts.Any(p => sub.exclusionFilter.FilterCriteria.Invoke(p)))
subCatsToDelete.Add(sub);
}
for (int j = 0; j < subCatsToDelete.Count; j++)
{
PartCategorizer.Category sub = subCatsToDelete[j];
C.subcategories.Remove(sub);
}
}
}
foreach (PartCategorizer.Category c in PartCategorizer.Instance.filters)
Core.Instance.namesAndIcons(c);

Expand Down Expand Up @@ -112,7 +136,7 @@ void findPartsToBlock()
{
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.Contains(subCat.button.categoryName) && !subCatsSeen.Contains(subCat.button.categoryName))
if (Core.Instance.subCategoriesDict.ContainsKey(subCat.button.categoryName) && customMainCat.subCategories.Any(subItem => string.Equals(subItem.subcategoryName, subCat.button.categoryName, StringComparison.CurrentCulture)) && !subCatsSeen.Contains(subCat.button.categoryName))
subCatsSeen.Add(subCat.button.categoryName);
else // subcat created by another mod
{
Expand Down
Binary file modified GameData/000_FilterExtensions/FilterExtensions.dll
Binary file not shown.

0 comments on commit 2615d9c

Please sign in to comment.