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

Commit

Permalink
All changes for KSP 1.3 release (v3.0.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
Crzyrndm committed May 27, 2017
1 parent c8e9ae5 commit e42b530
Show file tree
Hide file tree
Showing 18 changed files with 181 additions and 156 deletions.
7 changes: 5 additions & 2 deletions FE_Testing/FE_Test_Script.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public void AddSubCat()
Debug.Log("[FE_Testing Testing] Test subcat created");
PartCategorizer.AddCustomSubcategoryFilter(
PartCategorizer.Instance.filters.Find(c => string.Equals(c.button.categoryName, "Filter by Function", StringComparison.OrdinalIgnoreCase))
, "testing", new RUI.Icons.Selectable.Icon("abc", null, null, true), p => allowedParts.Contains(p.name));
, "testing"
, Localizer.Format("testing")
, new RUI.Icons.Selectable.Icon("abc", null, null, true)
, p => allowedParts.Contains(p.name));
}
}

Expand Down Expand Up @@ -73,7 +76,7 @@ IEnumerator CheckTestSubcategories()
{
yield return new WaitForSeconds(5); // wait for the editor to complete startup

PartCategorizer.Category testCat = PartCategorizer.Instance.filters.FirstOrDefault(C => string.Equals(Localization.Format(C.button.categoryName), testCatName));
PartCategorizer.Category testCat = PartCategorizer.Instance.filters.FirstOrDefault(C => string.Equals(Localizer.Format(C.button.categoryName), testCatName));
if (testCat == null)
{
LogTestResult($"Category named \"{testCatName}\" found", false);
Expand Down
25 changes: 12 additions & 13 deletions FilterExtension/CategoryInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ public class CategoryInstance

public CategoryInstance(CategoryNode protoC, Dictionary<string, SubcategoryNode> allSubCats)
{
Name = Localization.Format(protoC.CategoryName);
Name = Localizer.Format(protoC.CategoryName);
if (string.IsNullOrEmpty(Name))
{
Logger.Log("Category name is blank", Logger.LogLevel.Warn);
return;
}
Icon = protoC.IconName;
Colour = protoC.Colour;
Type = protoC.Type;
Expand All @@ -47,27 +52,21 @@ public CategoryInstance(CategoryNode protoC, Dictionary<string, SubcategoryNode>
}

public void Initialise()
{
if (string.IsNullOrEmpty(Name))
{
LoadAndProcess.Log("Category name is null or empty", LoadAndProcess.LogLevel.Warn);
return;
}

{
if (Type == CategoryNode.CategoryType.NEW)
{
RUI.Icons.Selectable.Icon icon = LoadAndProcess.GetIcon(Icon);
PartCategorizer.Category category = PartCategorizer.AddCustomFilter(Name, icon, Colour);
PartCategorizer.Category category = PartCategorizer.AddCustomFilter(Name, Localizer.Format(Name), icon, Colour);
category.displayType = EditorPartList.State.PartsList;
category.exclusionFilter = PartCategorizer.Instance.filterGenericNothing;
InstanceSubcategories(category);
}
else
{
if (!PartCategorizer.Instance.filters.TryGetValue(c => string.Equals(Localization.Format(c.button.categoryName), Name, StringComparison.OrdinalIgnoreCase),
if (!PartCategorizer.Instance.filters.TryGetValue(c => string.Equals(Localizer.Format(c.button.categoryName), Name, StringComparison.OrdinalIgnoreCase),
out PartCategorizer.Category category))
{
LoadAndProcess.Log("No category of this name was found to manipulate: " + Name, LoadAndProcess.LogLevel.Warn);
Logger.Log($"No category of this name was found to manipulate: {Name}", Logger.LogLevel.Warn);
return;
}
else if (Behaviour == CategoryNode.CategoryBehaviour.Replace)
Expand Down Expand Up @@ -97,12 +96,12 @@ void InstanceSubcategories(PartCategorizer.Category category)
}
catch (Exception ex)
{
LoadAndProcess.Log($"{sc} failed to initialise"
Logger.Log($"{sc} failed to initialise"
+ $"\r\nCategory: {Name}"
+ $"\r\nSubcategory Valid: {sc.Valid}"
+ $"\r\n{ex.Message}"
+ $"\r\n{ex.StackTrace}",
LoadAndProcess.LogLevel.Error);
Logger.LogLevel.Error);
}
}
}
Expand Down
22 changes: 13 additions & 9 deletions FilterExtension/ConfigNodes/CategoryNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public enum CategoryBehaviour
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<SubCategoryItem> SubCategories { get; } // 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)
{
CategoryName = node.GetValue("name");
CategoryName = node.GetValue("name").Trim();
IconName = node.GetValue("icon");
Colour = GUIUtils.ConvertToColor(node.GetValue("colour"));

Expand All @@ -51,7 +51,8 @@ public CategoryNode(ConfigNode node, LoadAndProcess data)
{
All = tmpBool;
}
LoadSubcategoryItems(node.GetNodes("SUBCATEGORIES"), SubCategories);
SubCategories = LoadSubcategoryItems(node.GetNodes("SUBCATEGORIES"));

string tmpStr = string.Empty;
if (node.TryGetValue("type", ref tmpStr))
{
Expand Down Expand Up @@ -85,16 +86,18 @@ public bool HasSubCategories()
return (SubCategories?.Count ?? 0) > 0;
}

public static void LoadSubcategoryItems(ConfigNode[] nodes, List<SubCategoryItem> loadTo)
public static List<SubCategoryItem> LoadSubcategoryItems(ConfigNode[] nodes)
{
var loadTo = new List<SubCategoryItem>();
if (nodes == null || nodes.Length == 0)
{
return;
return loadTo;
}

var stringList = new List<string>();
foreach (ConfigNode node in nodes)
{
stringList.AddUniqueRange(node.GetValues());
stringList.AddRange(node.GetValues());
}
SubCategoryItem[] subs = new SubCategoryItem[1000];
var unorderedSubCats = new List<SubCategoryItem>();
Expand All @@ -103,14 +106,14 @@ public static void LoadSubcategoryItems(ConfigNode[] nodes, List<SubCategoryItem
int splitIndex = s.IndexOf(',');
if (splitIndex == -1 || splitIndex == s.Length) // just the category name
{
unorderedSubCats.AddUnique(new SubCategoryItem(s));
unorderedSubCats.Add(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),
unorderedSubCats.Add(new SubCategoryItem(s.Substring(0, splitIndex),
!string.Equals(s.Substring(splitIndex, s.Length - splitIndex - 1), "dont template", StringComparison.OrdinalIgnoreCase)));
}
else // has position index
Expand All @@ -129,8 +132,9 @@ public static void LoadSubcategoryItems(ConfigNode[] nodes, List<SubCategoryItem
}
}
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.AddRange(unorderedSubCats); // tack unordered subcats on to the end
loadTo.RemoveAll(s => s == null);
return loadTo;
}

public override bool Equals(object obj)
Expand Down
2 changes: 1 addition & 1 deletion FilterExtension/ConfigNodes/CheckNodes/CheckNodeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static CheckNode MakeCheck(ConfigNode node)
case CheckCost.ID:
return new CheckCost(node);
default:
LoadAndProcess.Log($"unknown check type {checkID}", LoadAndProcess.LogLevel.Error);
Logger.Log($"unknown check type {checkID}", Logger.LogLevel.Error);
return null;
}
}
Expand Down
2 changes: 1 addition & 1 deletion FilterExtension/ConfigNodes/SubCategoryItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class SubCategoryItem : IEquatable<SubCategoryItem>

public SubCategoryItem(string name, bool useTemplate = true)
{
SubcategoryName = name;
SubcategoryName = name.Trim();
ApplyTemplate = useTemplate;
}

Expand Down
9 changes: 5 additions & 4 deletions FilterExtension/ConfigNodes/SubcategoryNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace FilterExtensions.ConfigNodes
{
using System.Diagnostics;
using FilterExtensions.ConfigNodes.CheckNodes;

public class SubcategoryNode : IEquatable<SubcategoryNode>
Expand Down Expand Up @@ -42,6 +43,7 @@ public SubcategoryNode(ConfigNode node)

public SubcategoryNode(SubcategoryNode cloneFrom, CategoryNode category)
{
Debug.Assert(cloneFrom != null, "subcategory cloned from null");
SubCategoryTitle = cloneFrom.SubCategoryTitle;
IconName = cloneFrom.IconName;
Filters = cloneFrom.Filters;
Expand Down Expand Up @@ -115,7 +117,6 @@ public bool CheckPartFilters(AvailablePart part, int depth = 0)
return false;
}
}

return CheckFilters(part, depth);
}

Expand All @@ -129,7 +130,7 @@ public bool CheckPartFilters(AvailablePart part, int depth = 0)
/// <returns></returns>
private bool CheckFilters(AvailablePart ap, int depth = 0)
{
if (Filters.Count == 0 && (Category == null || Category.Templates.Count == 0))
if (Filters == null || Filters.Count == 0)
{
return true;
}
Expand All @@ -149,11 +150,11 @@ private bool CheckFilters(AvailablePart ap, int depth = 0)

private bool CheckCategoryFilter(AvailablePart ap, int depth = 0)
{
if (Category == null)
if (Category == null || Category.Templates.Count == 0)
{
return true;
}
foreach (FilterNode f in Filters)
foreach (FilterNode f in Category.Templates)
{
if (f.FilterResult(ap, depth))
{
Expand Down
8 changes: 6 additions & 2 deletions FilterExtension/ConfigNodes/SubcategoryNodeModifier.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

Expand All @@ -10,13 +11,14 @@ public static class SubcategoryNodeModifier
static readonly string[] splitter = new string[] { "=>" };
public static void MakeRenamers(ConfigNode node, Dictionary<string, string> renames)
{
Debug.Assert(renames != null, $"{nameof(renames)} dictionary is assumed to never be null");
foreach (string s in node.GetValues("name"))
{
string[] split = s.Split(splitter, StringSplitOptions.RemoveEmptyEntries)
.Select(str => str.Trim()).ToArray();
if (split.Length != 2)
{
LoadAndProcess.Log($"bad length in rename string {s}", LoadAndProcess.LogLevel.Error);
Logger.Log($"bad length in rename string {s}", Logger.LogLevel.Error);
continue;
}
if (!renames.ContainsKey(split[0]))
Expand All @@ -28,12 +30,13 @@ public static void MakeRenamers(ConfigNode node, Dictionary<string, string> rena

public static void MakeIconChangers(ConfigNode node, Dictionary<string, string> icons)
{
Debug.Assert(icons != null, $"{nameof(icons)} dictionary is assumed to never be null");
foreach (string s in node.GetValues("icon"))
{
string[] split = s.Split(splitter, StringSplitOptions.RemoveEmptyEntries).Select(str => str.Trim()).ToArray();
if (split.Length != 2)
{
LoadAndProcess.Log($"bad length in set icon string {s}", LoadAndProcess.LogLevel.Error);
Logger.Log($"bad length in set icon string {s}", Logger.LogLevel.Error);
continue;
}
if (icons.ContainsKey(split[0]))
Expand All @@ -45,6 +48,7 @@ public static void MakeIconChangers(ConfigNode node, Dictionary<string, string>

public static void MakeDeleters(ConfigNode node, HashSet<string> deleters)
{
Debug.Assert(deleters != null, $"{nameof(deleters)} hashset is assumed to never be null");
foreach (string s in node.GetValues("remove"))
{
string str = s.Trim();
Expand Down
47 changes: 13 additions & 34 deletions FilterExtension/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ public void StartEditor()
public IEnumerator EditorInit()
{
Settings settings = HighLogic.CurrentGame.Parameters.CustomParams<Settings>();
if (settings.debug)
{
LoadAndProcess.Log("Starting on Stock Filters", LoadAndProcess.LogLevel.Log);
LoadAndProcess.Log("Starting on general categories", LoadAndProcess.LogLevel.Log);
}

Logger.Log("Starting on general categories", Logger.LogLevel.Debug);
foreach (CategoryInstance c in LoadAndProcess.Categories) // all non mod specific FE categories
{
if ((c.Type == CategoryNode.CategoryType.NEW || c.Type == CategoryNode.CategoryType.STOCK)
Expand All @@ -48,11 +43,7 @@ public IEnumerator EditorInit()
}

yield return null;
if (settings.debug)
{
LoadAndProcess.Log("Starting on late categories", LoadAndProcess.LogLevel.Log);
}

Logger.Log("Starting on late categories", Logger.LogLevel.Debug);
// this is to be used for altering subcategories in a category added by another mod
foreach (CategoryInstance c in LoadAndProcess.Categories)
{
Expand All @@ -62,12 +53,9 @@ public IEnumerator EditorInit()
}
}

// Remove any category with no subCategories (causes major breakages if selected).
yield return null;
if (settings.debug)
{
LoadAndProcess.Log("Starting on removing categories", LoadAndProcess.LogLevel.Log);
}
Logger.Log("Starting on removing categories", Logger.LogLevel.Debug);
// Remove any category with no subCategories (causes major breakages if selected).
for (int i = PartCategorizer.Instance.filters.Count - 1; i >= 0; --i)
{
if (PartCategorizer.Instance.filters[i].subcategories.Count == 0)
Expand All @@ -83,10 +71,7 @@ public IEnumerator EditorInit()
}

yield return null;
if (HighLogic.CurrentGame.Parameters.CustomParams<Settings>().debug)
{
LoadAndProcess.Log("Refreshing parts list", LoadAndProcess.LogLevel.Log);
}
Logger.Log("Refreshing parts list", Logger.LogLevel.Debug);
SetSelectedCategory();
}

Expand All @@ -98,10 +83,9 @@ public static void SetSelectedCategory()
{
try
{
PartCategorizer.Category cat;
if (HighLogic.CurrentGame.Parameters.CustomParams<Settings>().categoryDefault != string.Empty)
{
cat = PartCategorizer.Instance.filters.FirstOrDefault(f => f.button.categoryName == HighLogic.CurrentGame.Parameters.CustomParams<Settings>().categoryDefault);
PartCategorizer.Category cat = PartCategorizer.Instance.filters.FirstOrDefault(f => f.button.categoryName == HighLogic.CurrentGame.Parameters.CustomParams<Settings>().categoryDefault);
if (cat != null)
{
cat.button.activeButton.SetState(KSP.UI.UIRadioButton.State.True, KSP.UI.UIRadioButton.CallType.APPLICATION, null, true);
Expand All @@ -111,28 +95,23 @@ public static void SetSelectedCategory()
if (HighLogic.CurrentGame.Parameters.CustomParams<Settings>().subCategoryDefault != string.Empty)
{
// set the subcategory button
cat = PartCategorizer.Instance.filters.FirstOrDefault(f => f.button.activeButton.Value);
if (cat != null)
{
cat = cat.subcategories.FirstOrDefault(sC => sC.button.categoryName == HighLogic.CurrentGame.Parameters.CustomParams<Settings>().subCategoryDefault);
if (cat != null)
{
cat.button.activeButton.SetState(KSP.UI.UIRadioButton.State.True, KSP.UI.UIRadioButton.CallType.APPLICATION, null, true);
}
}
KSP.UI.UIRadioButton but = PartCategorizer.Instance.filters.FirstOrDefault(f => f.button.activeButton.Value)?.subcategories.FirstOrDefault(sC => {
return sC.button.categoryName == HighLogic.CurrentGame.Parameters.CustomParams<Settings>().subCategoryDefault;
})?.button.activeButton;
but.SetState(KSP.UI.UIRadioButton.State.True, KSP.UI.UIRadioButton.CallType.APPLICATION, null, true);
}
}
catch (Exception e)
{
LoadAndProcess.Log($"Category refresh failed\r\n{e.InnerException}\r\n{e.StackTrace}", LoadAndProcess.LogLevel.Error);
Logger.Log($"Category refresh failed\r\n{e.InnerException}\r\n{e.StackTrace}", Logger.LogLevel.Error);
}
}

private bool CheckPartVisible(AvailablePart part, PartCategorizer.Category category)
{
for (int i = 0; i < category.subcategories.Count; ++i)
foreach (PartCategorizer.Category subcat in category.subcategories)
{
if (category.subcategories[i].exclusionFilter.FilterCriteria.Invoke(part))
if (subcat.exclusionFilter.FilterCriteria(part))
{
return true;
}
Expand Down
Loading

0 comments on commit e42b530

Please sign in to comment.