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

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Crzyrndm committed Apr 23, 2016
1 parent c5b02a3 commit 34d0b6a
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 142 deletions.
8 changes: 4 additions & 4 deletions FilterExtension/ConfigNodes/Check.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,10 @@ public Check(Check c)

if (c.values != null)
values = (string[])c.values.Clone();
checks = new List<Check>();
if (c.checks != null)
{
checks = new List<Check>(c.checks.Count);
checks = new List<Check>();
for (int i = 0; i < c.checks.Count; ++i)
checks.Add(new Check(c.checks[i]));
}
Expand Down Expand Up @@ -303,16 +304,15 @@ public bool Equals(Check c2)
{
if (c2 == null)
return false;
if (this.type == c2.type && this.values == c2.values && this.invert == c2.invert && this.contains == c2.contains && this.checks == c2.checks && this.equality == c2.equality)
if (type == c2.type && values == c2.values && invert == c2.invert && contains == c2.contains && checks == c2.checks && equality == c2.equality)
return true;
else
return false;
}

public override int GetHashCode()
{
int checks = this.checks.Any() ? this.checks.GetHashCode() : 1;
return this.type.GetHashCode() * this.values.GetHashCode() * this.invert.GetHashCode() * this.contains.GetHashCode() * this.equality.GetHashCode() * checks;
return type.GetHashCode() ^ values.GetHashCode() ^ invert.GetHashCode() ^ contains.GetHashCode() ^ equality.GetHashCode() ^ (int)checks.Average(c => c.GetHashCode());
}
}
}
23 changes: 5 additions & 18 deletions FilterExtension/ConfigNodes/Filter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,41 +73,28 @@ public static bool compareFilterLists(List<Filter> fLA, List<Filter> fLB)
if (fLA.Count != fLB.Count && fLA.Count != 0)
return false;

foreach (Filter fA in fLA)
{
if (!fLB.Any(fB => fB.Equals(fA)))
return false;
}
return true;
return fLA.All(f => fLB.Contains(f));
}

public bool Equals(Filter f2)
{
if (f2 == null)
return false;

if (this.invert != f2.invert)
if (invert != f2.invert)
return false;
else
{
foreach (Check c1 in this.checks)
{
if (!f2.checks.Any(c2 => c1.Equals(c2)))
return false;
}
return true;
}
return checks.All(c => f2.checks.Contains(c));
}

public override int GetHashCode()
{
int hash = 0;
foreach (Check c in this.checks)
foreach (Check c in checks)
{
hash *= c.GetHashCode();
}

return hash * this.invert.GetHashCode();
return hash ^ invert.GetHashCode();
}
}
}
118 changes: 39 additions & 79 deletions FilterExtension/ConfigNodes/customCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public customCategory(ConfigNode node)

categoryName = node.GetValue("name");
iconName = node.GetValue("icon");
colour = convertToColor(node.GetValue("colour"));
colour = GUIUtils.convertToColor(node.GetValue("colour"));

makeTemplate(node);

Expand Down Expand Up @@ -180,61 +180,51 @@ public void initialise()
category.subcategories.Clear();
}

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++)
{
subCategoryItem subcategoryItem = subCategories[i];
if (subcategoryItem == null)
continue;

if (string.IsNullOrEmpty(subcategoryItem.subcategoryName))
continue;
customSubCategory subcategory = null;
if (!Core.Instance.subCategoriesDict.TryGetValue(subcategoryItem.subcategoryName, out subcategory))
{
Core.Log("subcategory {0} not found in subcategories Dictionary", subcategoryItem.subcategoryName);
continue;
}
initSubcategory(i, subCategories[i], category);
}

//List<string> conflictsList;
#warning subcategory conflicts are broken and doing stupid things
//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(subcategoryNames).ToList();
// // if there are any conflicts that show up in the subcategories list before this one
// if (conflicts.Any(c => subcategoryNames.IndexOf(c) < i))
// {
// Core.Log("Filters duplicated in category " + this.categoryName + " between subCategories:\r\n" + string.Join("\r\n", conflicts.ToArray()));
// continue;
// }
//}

customSubCategory sC = new customSubCategory(subcategory);
if (subcategoryItem.applyTemplate)
sC.template = templates;

try
{
if (Editor.subcategoriesChecked || sC.checkSubCategoryHasParts(categoryName))
sC.initialise(category);
}
catch (Exception ex)
public void initSubcategory(int index, subCategoryItem toInit, PartCategorizer.Category category)
{
if (toInit == null || string.IsNullOrEmpty(toInit.ToString()))
return;

List<string> conflictsList;
if (Core.Instance.conflictsDict.TryGetValue(toInit.ToString(), out conflictsList)) // if we have a conflict with some other subcategory
{
for (int j = 0; j < index; ++j) // iterate over the subcategories we've already added to see if it's one of them
{
// extended logging for errors
Core.Log(subCategories[i] + " failed to initialise");
Core.Log("Category:" + categoryName + ", filter:" + sC.hasFilters + ", Count:" + sC.filters.Count + ", Icon:" + Core.getIcon(sC.iconName));
Core.Log(ex.StackTrace);
if (conflictsList.Contains(subCategories[j].subcategoryName))
{
// if so, we skip this subcategory
Core.Log("Filters duplicated in category {0} between subCategories:\r\n{1} and {2}", categoryName, toInit.ToString(), subCategories[j].subcategoryName);
return;
}
}
}
}
customSubCategory subcategory = null;
if (!Core.Instance.subCategoriesDict.TryGetValue(toInit.ToString(), out subcategory))
{
Core.Log("subcategory {0} not found in subcategories Dictionary", toInit.ToString());
return;
}

private void typeSwitch(string Type, string Replace)
{
customSubCategory sC = new customSubCategory(subcategory);
if (toInit.applyTemplate)
sC.template = templates;

try
{
if (Editor.subcategoriesChecked || sC.checkSubCategoryHasParts(categoryName))
sC.initialise(category);
}
catch (Exception ex)
{
// extended logging for errors
Core.Log(subCategories[index] + " failed to initialise");
Core.Log("Category:" + categoryName + ", filter:" + sC.hasFilters + ", Count:" + sC.filters.Count + ", Icon:" + Core.getIcon(sC.iconName));
Core.Log(ex.StackTrace);
}
}

private void makeTemplate(ConfigNode node)
Expand All @@ -247,36 +237,6 @@ private void makeTemplate(ConfigNode node)
templates.Add(new Filter(n));
}

public static Color convertToColor(string hex_ARGB)
{
if (string.IsNullOrEmpty(hex_ARGB))
return Color.clear;

hex_ARGB = hex_ARGB.Replace("#", "").Replace("0x", ""); // remove any hexadecimal identifiers
if (System.Text.RegularExpressions.Regex.IsMatch(hex_ARGB, "[0-9a-fA-F]{6,8}")) // check it is valid hex
{
if (hex_ARGB.Length == 8)
{
Color c = new Color();
c.a = (float)byte.Parse(hex_ARGB.Substring(0, 2), System.Globalization.NumberStyles.HexNumber) / 255f;
c.r = (float)byte.Parse(hex_ARGB.Substring(2, 2), System.Globalization.NumberStyles.HexNumber) / 255f;
c.g = (float)byte.Parse(hex_ARGB.Substring(4, 2), System.Globalization.NumberStyles.HexNumber) / 255f;
c.b = (float)byte.Parse(hex_ARGB.Substring(6, 2), System.Globalization.NumberStyles.HexNumber) / 255f;
return c;
}
else // if (hex_ARGB.Length == 6)
{
Color c = new Color();
c.a = 1;
c.r = (float)byte.Parse(hex_ARGB.Substring(0, 2), System.Globalization.NumberStyles.HexNumber) / 255f;
c.g = (float)byte.Parse(hex_ARGB.Substring(2, 2), System.Globalization.NumberStyles.HexNumber) / 255f;
c.b = (float)byte.Parse(hex_ARGB.Substring(4, 2), System.Globalization.NumberStyles.HexNumber) / 255f;
return c;
}
}
return Color.clear;
}

public bool hasSubCategories()
{
return (subCategories != null && subCategories.Any());
Expand Down
4 changes: 2 additions & 2 deletions FilterExtension/ConfigNodes/customSubCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,15 @@ public bool Equals(customSubCategory sC2)
if (sC2 == null)
return false;

if (this.subCategoryTitle == sC2.subCategoryTitle)
if (subCategoryTitle == sC2.subCategoryTitle)
return true;

return false;
}

public override int GetHashCode()
{
return this.subCategoryTitle.GetHashCode();
return subCategoryTitle.GetHashCode();
}
}
}
19 changes: 9 additions & 10 deletions FilterExtension/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,35 +378,34 @@ private void checkAndMarkConflicts()
// Can't guarantee iteration order of dict will be the same each time so need a set of elements that have been processed
// to ensure conflicts are only checked against elements that are already checked
// by only checking against processed elements we know we're only adding checking for collisions between each pair once
HashSet<string> processedElements = new HashSet<string>();
List<string> processedElements = new List<string>();
foreach (KeyValuePair<string, customSubCategory> kvpOuter in subCategoriesDict)
{
foreach (KeyValuePair<string, customSubCategory> kvp in subCategoriesDict) // iterate through the already added sC's
foreach (string subcatName in processedElements)
{
if (kvp.Key == kvpOuter.Key || !processedElements.Contains(kvp.Key))
continue;
if (Filter.compareFilterLists(kvp.Value.filters, kvpOuter.Value.filters)) // check for duplicated filters
customSubCategory processedSubcat = subCategoriesDict[subcatName];
if (Filter.compareFilterLists(processedSubcat.filters, kvpOuter.Value.filters))
{
// add conflict entry for the already entered subCategory
List<string> conflicts;
if (conflictsDict.TryGetValue(kvp.Key, out conflicts))
if (conflictsDict.TryGetValue(subcatName, out conflicts))
conflicts.Add(kvpOuter.Key);
else
conflictsDict.Add(kvp.Key, new List<string>() { kvpOuter.Key});
conflictsDict.Add(subcatName, new List<string>() { kvpOuter.Key });

// add a conflict entry for the new subcategory
if (conflictsDict.TryGetValue(kvpOuter.Key, out conflicts))
conflicts.Add(kvp.Key);
conflicts.Add(subcatName);
else
conflictsDict.Add(kvpOuter.Key, new List<string>() { kvp.Key });
conflictsDict.Add(kvpOuter.Key, new List<string>() { subcatName });
}
}
processedElements.Add(kvpOuter.Key);
}
}

/// <summary>
/// loads all textures between 25 and 40 px in dimensions into a dictionary using the filename as a key
/// loads all textures that are 32x32px into a dictionary using the filename as a key
/// </summary>
private static void loadIcons()
{
Expand Down
57 changes: 28 additions & 29 deletions FilterExtension/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,35 +142,34 @@ public void namesAndIcons(PartCategorizer.Category category)
/// </summary>
public static void setSelectedCategory()
{
//try
//{
// PartCategorizer.Category Filter = PartCategorizer.Instance.filters.FirstOrDefault(f => f.button.activeButton.CurrentState == KSP.UI.UIRadioButton.State.True);
// if (Filter != null)
// Filter.button.activeButton.SetState(KSP.UI.UIRadioButton.State.False, KSP.UI.UIRadioButton.CallType.APPLICATIONSILENT, null);

// Filter = PartCategorizer.Instance.filters.FirstOrDefault(f => f.button.categoryName == Settings.categoryDefault);
// if (Filter != null)
// Filter.button.activeButton.SetState(KSP.UI.UIRadioButton.State.True, KSP.UI.UIRadioButton.CallType.APPLICATIONSILENT, null);
// else
// {
// Filter = PartCategorizer.Instance.filters[0];
// if (Filter != null)
// {
// Filter.button.activeButton.SetState(KSP.UI.UIRadioButton.State.True, KSP.UI.UIRadioButton.CallType.APPLICATIONSILENT, null);
// }
// }

// // set the subcategory button
// //Filter = Filter.subcategories.FirstOrDefault(sC => sC.button.categoryName == instance.subCategoryDefault);
// //if (Filter != null && Filter.button.activeButton.State != RUIToggleButtonTyped.ButtonState.TRUE)
// // Filter.button.activeButton.SetTrue(Filter.button.activeButton, RUIToggleButtonTyped.ClickType.FORCED);
//}
//catch (Exception e)
//{
// Core.Log("Category refresh failed");
// Core.Log(e.InnerException);
// Core.Log(e.StackTrace);
//}
try
{
PartCategorizer.Category cat;
if (Settings.categoryDefault != string.Empty)
{
cat = PartCategorizer.Instance.filters.FirstOrDefault(f => f.button.categoryName == Settings.categoryDefault);
if (cat != null)
cat.button.activeButton.SetState(KSP.UI.UIRadioButton.State.True, KSP.UI.UIRadioButton.CallType.APPLICATION, null, true);
}

if (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 == Settings.subCategoryDefault);
if (cat != null)
cat.button.activeButton.SetState(KSP.UI.UIRadioButton.State.True, KSP.UI.UIRadioButton.CallType.APPLICATION, null, true);
}
}
}
catch (Exception e)
{
Core.Log("Category refresh failed");
Core.Log(e.InnerException);
Core.Log(e.StackTrace);
}
}

/// <summary>
Expand Down
18 changes: 18 additions & 0 deletions FilterExtension/Utility/GUIUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,23 @@ public static void DrawLabelPlusBox(string label, ref string text)
text = GUILayout.TextField(text);
GUILayout.EndHorizontal();
}

public static Color convertToColor(string hex_ARGB)
{
if (string.IsNullOrEmpty(hex_ARGB))
return Color.clear;
hex_ARGB = hex_ARGB.Replace("#", "").Replace("0x", ""); // remove any hexadecimal identifiers

byte a = 255;
if (hex_ARGB.Length >= 8)
{
a = byte.Parse(hex_ARGB.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
hex_ARGB = hex_ARGB.Substring(2);
}
byte r = byte.Parse(hex_ARGB.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
byte g = byte.Parse(hex_ARGB.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
byte b = byte.Parse(hex_ARGB.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
return new Color32(r, g, b, a);
}
}
}
Binary file modified GameData/000_FilterExtensions/FilterExtensions.dll
Binary file not shown.

0 comments on commit 34d0b6a

Please sign in to comment.