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

Commit

Permalink
Clean up category node a little
Browse files Browse the repository at this point in the history
  • Loading branch information
Crzyrndm committed Apr 25, 2017
1 parent 6618ab6 commit c8e9ae5
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 102 deletions.
2 changes: 1 addition & 1 deletion FilterExtension/CategoryInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void Initialise()
return;
}

if (Type == CategoryNode.CategoryType.New)
if (Type == CategoryNode.CategoryType.NEW)
{
RUI.Icons.Selectable.Icon icon = LoadAndProcess.GetIcon(Icon);
PartCategorizer.Category category = PartCategorizer.AddCustomFilter(Name, icon, Colour);
Expand Down
161 changes: 70 additions & 91 deletions FilterExtension/ConfigNodes/CategoryNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,119 +11,55 @@ public class CategoryNode : IEquatable<CategoryNode>
{
public enum CategoryType
{
New = 0, // new category
Stock, // modification to stock category
Mod // modification to a mod cateogry
NEW = 0, // new category
STOCK, // modification to stock category
MOD // modification to a mod cateogry
}

public enum CategoryBehaviour
{
Add = 1, // only add to existing categories
Replace = 2, // wipe existing categories
Engines = 4 // generate unique engine types
Add = 0, // only add to existing categories
Replace, // wipe existing categories
Engines // generate unique engine types
}

public string CategoryName { get; }
public string IconName { get; }
public Color Colour { get; }
public CategoryType Type { get; }
public CategoryBehaviour Behaviour { get; }
public bool All { get; } // has an all parts subCategory
public List<SubCategoryItem> SubCategories { get; } // array of subcategories
public CategoryType Type { get; } = CategoryType.NEW;
public CategoryBehaviour Behaviour { get; } = CategoryBehaviour.Add;
public bool All { get; } = false; // has an all parts subCategory
public List<SubCategoryItem> SubCategories { get; } = new List<SubCategoryItem>(); // array of subcategories
public List<FilterNode> Templates { get; } // Checks to add to every Filter in a category with the template tag

public CategoryNode(ConfigNode node, LoadAndProcess data)
{
string tmpStr = string.Empty;

CategoryName = node.GetValue("name");
IconName = node.GetValue("icon");
Colour = GUIUtils.ConvertToColor(node.GetValue("colour"));

ConfigNode[] filtNodes = node.GetNodes("FILTER");
if (filtNodes == null)
{
return;
}
Templates = new List<FilterNode>();
foreach (ConfigNode n in filtNodes)
if (filtNodes != null)
{
Templates.Add(new FilterNode(n));
Templates = new List<FilterNode>();
foreach (ConfigNode n in filtNodes)
{
Templates.Add(new FilterNode(n));
}
}
if (bool.TryParse(node.GetValue("all"), out bool tmpBool))
{
All = tmpBool;
}

ConfigNode[] subcategoryList = node.GetNodes("SUBCATEGORIES");
SubCategories = new List<SubCategoryItem>();
if (subcategoryList != null)
LoadSubcategoryItems(node.GetNodes("SUBCATEGORIES"), SubCategories);
string tmpStr = string.Empty;
if (node.TryGetValue("type", ref tmpStr))
{
var unorderedSubCats = new List<SubCategoryItem>();
var stringList = new List<string>();
for (int i = 0; i < subcategoryList.Length; i++)
{
stringList.AddRange(subcategoryList[i].GetValues());
}
SubCategoryItem[] subs = new SubCategoryItem[1000];
for (int i = 0; i < stringList.Count; i++)
try
{
string[] indexAndValue = stringList[i].Split(',').Select(s => s.Trim()).ToArray();

var newSubItem = new SubCategoryItem();
if (int.TryParse(indexAndValue[0], out int index)) // has position index
{
if (indexAndValue.Length >= 2)
{
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
{
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);
}
Type = (CategoryType)Enum.Parse(typeof(CategoryType), tmpStr.ToUpperInvariant());
}
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);
}

if (node.TryGetValue("type", ref tmpStr))
{
tmpStr = tmpStr.ToLower();
}
switch (tmpStr)
{
case "stock":
Type = CategoryType.Stock;
break;

case "mod":
Type = CategoryType.Mod;
break;

case "new":
default:
Type = CategoryType.New;
break;
catch {} // leave as default
}
if (node.TryGetValue("value", ref tmpStr))
{
Expand All @@ -141,10 +77,6 @@ public CategoryNode(ConfigNode node, LoadAndProcess data)
SubCategories.AddUnique(new SubCategoryItem(subcatName));
}
}
else
{
Behaviour = CategoryBehaviour.Add;
}
}
}

Expand All @@ -153,6 +85,54 @@ public bool HasSubCategories()
return (SubCategories?.Count ?? 0) > 0;
}

public static void LoadSubcategoryItems(ConfigNode[] nodes, List<SubCategoryItem> loadTo)
{
if (nodes == null || nodes.Length == 0)
{
return;
}
var stringList = new List<string>();
foreach (ConfigNode node in nodes)
{
stringList.AddUniqueRange(node.GetValues());
}
SubCategoryItem[] subs = new SubCategoryItem[1000];
var unorderedSubCats = new List<SubCategoryItem>();
foreach (string s in stringList)
{
int splitIndex = s.IndexOf(',');
if (splitIndex == -1 || splitIndex == s.Length) // just the category name
{
unorderedSubCats.AddUnique(new SubCategoryItem(s));
}
else // atleast two items
{
string firstSplit = s.Substring(0, splitIndex);
if (!int.TryParse(s.Substring(0, splitIndex), out int index)) // name + "dont template"
{
unorderedSubCats.AddUnique(new SubCategoryItem(s.Substring(0, splitIndex),
!string.Equals(s.Substring(splitIndex, s.Length - splitIndex - 1), "dont template", StringComparison.OrdinalIgnoreCase)));
}
else // has position index
{
int lastSplitIndex = s.LastIndexOf(',');
if (lastSplitIndex == splitIndex) // only 2 items, index + cat name
{
subs[index] = new SubCategoryItem(s.Substring(splitIndex + 1));
}
else // three items, index + name + "dont template"
{
subs[index] = new SubCategoryItem(s.Substring(splitIndex + 1, lastSplitIndex - splitIndex - 1),
!string.Equals(s.Substring(lastSplitIndex + 1), "dont template", StringComparison.OrdinalIgnoreCase));
}
}
}
}
loadTo = subs.Distinct().ToList(); // no duplicates and no gaps in a single line. Yay
loadTo.AddUniqueRange(unorderedSubCats); // tack unordered subcats on to the end
loadTo.RemoveAll(s => s == null);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
Expand Down Expand Up @@ -180,7 +160,6 @@ public bool Equals(CategoryNode C)
{
return true;
}

return CategoryName.Equals(C.CategoryName);
}

Expand Down
9 changes: 2 additions & 7 deletions FilterExtension/ConfigNodes/SubCategoryItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@ namespace FilterExtensions.ConfigNodes
{
public class SubCategoryItem : IEquatable<SubCategoryItem>
{
public string SubcategoryName { get; set; }
public bool ApplyTemplate { get; set; }

public SubCategoryItem()
{
ApplyTemplate = true;
}
public string SubcategoryName { get; }
public bool ApplyTemplate { get; }

public SubCategoryItem(string name, bool useTemplate = true)
{
Expand Down
4 changes: 2 additions & 2 deletions FilterExtension/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public IEnumerator EditorInit()

foreach (CategoryInstance c in LoadAndProcess.Categories) // all non mod specific FE categories
{
if ((c.Type == CategoryNode.CategoryType.New || c.Type == CategoryNode.CategoryType.Stock)
if ((c.Type == CategoryNode.CategoryType.NEW || c.Type == CategoryNode.CategoryType.STOCK)
&& (settings.replaceFbM || !string.Equals(c.Name, "Filter by Manufacturer", StringComparison.OrdinalIgnoreCase)))
{
c.Initialise();
Expand All @@ -56,7 +56,7 @@ public IEnumerator EditorInit()
// this is to be used for altering subcategories in a category added by another mod
foreach (CategoryInstance c in LoadAndProcess.Categories)
{
if (c.Type == CategoryNode.CategoryType.Mod)
if (c.Type == CategoryNode.CategoryType.MOD)
{
c.Initialise();
}
Expand Down
2 changes: 1 addition & 1 deletion FilterExtension/LoadAndProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private void ProcessFilterDefinitions()
}

CategoryNode Cat = CategoryNodes.Find(C => C.CategoryName == "Filter by Resource");
if (Cat != null && Cat.Type == CategoryNode.CategoryType.Stock)
if (Cat != null && Cat.Type == CategoryNode.CategoryType.STOCK)
{
foreach (string s in resources)
{
Expand Down
Binary file modified GameData/000_FilterExtensions/FilterExtensions.dll
Binary file not shown.

0 comments on commit c8e9ae5

Please sign in to comment.