diff --git a/FilterExtension/CategoryInstance.cs b/FilterExtension/CategoryInstance.cs index 78b36d68..3c48a1f9 100644 --- a/FilterExtension/CategoryInstance.cs +++ b/FilterExtension/CategoryInstance.cs @@ -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); diff --git a/FilterExtension/ConfigNodes/CategoryNode.cs b/FilterExtension/ConfigNodes/CategoryNode.cs index ed6b4b1d..6f65e1ee 100644 --- a/FilterExtension/ConfigNodes/CategoryNode.cs +++ b/FilterExtension/ConfigNodes/CategoryNode.cs @@ -11,119 +11,55 @@ public class CategoryNode : IEquatable { 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 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 SubCategories { get; } = new List(); // array of subcategories public List 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(); - foreach (ConfigNode n in filtNodes) + if (filtNodes != null) { - Templates.Add(new FilterNode(n)); + Templates = new List(); + 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(); - if (subcategoryList != null) + LoadSubcategoryItems(node.GetNodes("SUBCATEGORIES"), SubCategories); + string tmpStr = string.Empty; + if (node.TryGetValue("type", ref tmpStr)) { - var unorderedSubCats = new List(); - var stringList = new List(); - 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)) { @@ -141,10 +77,6 @@ public CategoryNode(ConfigNode node, LoadAndProcess data) SubCategories.AddUnique(new SubCategoryItem(subcatName)); } } - else - { - Behaviour = CategoryBehaviour.Add; - } } } @@ -153,6 +85,54 @@ public bool HasSubCategories() return (SubCategories?.Count ?? 0) > 0; } + public static void LoadSubcategoryItems(ConfigNode[] nodes, List loadTo) + { + if (nodes == null || nodes.Length == 0) + { + return; + } + var stringList = new List(); + foreach (ConfigNode node in nodes) + { + stringList.AddUniqueRange(node.GetValues()); + } + SubCategoryItem[] subs = new SubCategoryItem[1000]; + var unorderedSubCats = new List(); + 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)) @@ -180,7 +160,6 @@ public bool Equals(CategoryNode C) { return true; } - return CategoryName.Equals(C.CategoryName); } diff --git a/FilterExtension/ConfigNodes/SubCategoryItem.cs b/FilterExtension/ConfigNodes/SubCategoryItem.cs index 40e98636..14740236 100644 --- a/FilterExtension/ConfigNodes/SubCategoryItem.cs +++ b/FilterExtension/ConfigNodes/SubCategoryItem.cs @@ -4,13 +4,8 @@ namespace FilterExtensions.ConfigNodes { public class SubCategoryItem : IEquatable { - 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) { diff --git a/FilterExtension/Editor.cs b/FilterExtension/Editor.cs index 8ad727c5..55a6291d 100644 --- a/FilterExtension/Editor.cs +++ b/FilterExtension/Editor.cs @@ -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(); @@ -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(); } diff --git a/FilterExtension/LoadAndProcess.cs b/FilterExtension/LoadAndProcess.cs index 21ea9395..66a06c9e 100644 --- a/FilterExtension/LoadAndProcess.cs +++ b/FilterExtension/LoadAndProcess.cs @@ -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) { diff --git a/GameData/000_FilterExtensions/FilterExtensions.dll b/GameData/000_FilterExtensions/FilterExtensions.dll index 21fe372a..3a855173 100644 Binary files a/GameData/000_FilterExtensions/FilterExtensions.dll and b/GameData/000_FilterExtensions/FilterExtensions.dll differ