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

Commit

Permalink
Merge pull request #17 from Crzyrndm/master
Browse files Browse the repository at this point in the history
Update from parent.
  • Loading branch information
Kerbas-ad-astra committed May 7, 2016
2 parents 4fbb7fb + 82e9231 commit 4792c83
Show file tree
Hide file tree
Showing 78 changed files with 266 additions and 257 deletions.
90 changes: 30 additions & 60 deletions FilterExtension/ConfigNodes/Check.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,93 +167,63 @@ public Check(string Type, string Value, bool Invert = false, bool Contains = tru
checks = new List<Check>();
}

public bool checkPart(AvailablePart part, int depth = 0)
public bool checkResult(AvailablePart part, int depth = 0)
{
bool result = true;
switch (type.typeEnum)
{
case CheckType.moduleTitle: // check by module title
result = PartType.checkModuleTitle(part, values, contains);
break;
return invert ^ PartType.checkModuleTitle(part, values, contains);
case CheckType.moduleName:
result = PartType.checkModuleName(part, values, contains);
break;
return invert ^ PartType.checkModuleName(part, values, contains);
case CheckType.partName: // check by part name (cfg name)
result = PartType.checkName(part, values);
break;
return invert ^ PartType.checkName(part, values);
case CheckType.partTitle: // check by part title (in game name)
result = PartType.checkTitle(part, values);
break;
return invert ^ PartType.checkTitle(part, values);
case CheckType.resource: // check for a resource
result = PartType.checkResource(part, values, contains);
break;
return invert ^ PartType.checkResource(part, values, contains);
case CheckType.propellant: // check for engine propellant
result = PartType.checkPropellant(part, values, contains);
break;
return invert ^ PartType.checkPropellant(part, values, contains);
case CheckType.tech: // check by tech
result = PartType.checkTech(part, values);
break;
return invert ^ PartType.checkTech(part, values);
case CheckType.manufacturer: // check by manufacturer
result = PartType.checkManufacturer(part, values);
break;
return invert ^ PartType.checkManufacturer(part, values);
case CheckType.folder: // check by mod root folder
result = PartType.checkFolder(part, values);
break;
return invert ^ PartType.checkFolder(part, values);
case CheckType.path: // check by part folder location
result = PartType.checkPath(part, values);
break;
return invert ^ PartType.checkPath(part, values);
case CheckType.category:
result = PartType.checkCategory(part, values);
break;
return invert ^ PartType.checkCategory(part, values);
case CheckType.size: // check by largest stack node size
result = PartType.checkPartSize(part, values, contains, equality);
break;
return invert ^ PartType.checkPartSize(part, values, contains, equality);
case CheckType.crew:
result = PartType.checkCrewCapacity(part, values, equality);
break;
return invert ^ PartType.checkCrewCapacity(part, values, equality);
case CheckType.custom: // for when things get tricky
result = PartType.checkCustom(part, values);
break;
return invert ^ PartType.checkCustom(part, values);
case CheckType.mass:
result = PartType.checkMass(part, values, equality);
break;
return invert ^ PartType.checkMass(part, values, equality);
case CheckType.cost:
result = PartType.checkCost(part, values, equality);
break;
return invert ^ PartType.checkCost(part, values, equality);
case CheckType.crashTolerance:
result = PartType.checkCrashTolerance(part, values, equality);
break;
return invert ^ PartType.checkCrashTolerance(part, values, equality);
case CheckType.maxTemp:
result = PartType.checkTemperature(part, values, equality);
break;
return invert ^ PartType.checkTemperature(part, values, equality);
case CheckType.profile:
result = PartType.checkBulkHeadProfiles(part, values, contains);
break;
return invert ^ PartType.checkBulkHeadProfiles(part, values, contains);
case CheckType.subcategory:
return invert ^ PartType.checkSubcategory(part, values, depth);
case CheckType.tag:
return invert ^ PartType.checkTags(part, values, contains);
case CheckType.check:
for (int i = 0; i < checks.Count; i++ )
for (int i = 0; i < checks.Count; i++)
{
if (!checks[i].checkPart(part))
{
result = false;
break;
}
if (invert == checks[i].checkResult(part))
return false;
}
break;
case CheckType.subcategory:
result = PartType.checkSubcategory(part, values, depth);
break;
case CheckType.tag:
result = PartType.checkTags(part, values, contains);
break;
return true;
default:
Core.Log("invalid Check type specified");
result = false;
break;
Core.Log("invalid Check type specified", Core.LogLevel.Warn);
return invert ^ false;
}

if (invert)
return !result;
return result;
}

/// <summary>
Expand Down
9 changes: 7 additions & 2 deletions FilterExtension/ConfigNodes/Filter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,14 @@ public object Clone()
return new Filter(this);
}

internal bool checkFilter(AvailablePart part, int depth = 0)
public bool filterResult(AvailablePart part, int depth = 0)
{
return invert ? !checks.All(c => c.checkPart(part, depth)) : checks.All(c => c.checkPart(part, depth));
for(int i = 0; i < checks.Count; ++i )
{
if(!checks[i].checkResult(part, depth))
return invert;
}
return !invert;
}

/// <summary>
Expand Down
16 changes: 8 additions & 8 deletions FilterExtension/ConfigNodes/customCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ public void initialise()
{
if (string.IsNullOrEmpty(categoryName))
{
Core.Log("Category name is null or empty");
Core.Log("Category name is null or empty", Core.LogLevel.Warn);
return;
}
if (!hasSubCategories())
{
Core.Log(categoryName + " has no subcategories");
Core.Log(categoryName + " has no subcategories", Core.LogLevel.Warn);
return;
}
PartCategorizer.Category category;
Expand All @@ -173,7 +173,7 @@ public void initialise()
{
if (!PartCategorizer.Instance.filters.TryGetValue(c => c.button.categoryName == categoryName, out category))
{
Core.Log("No category of this name was found to manipulate: " + categoryName);
Core.Log("No category of this name was found to manipulate: " + categoryName, Core.LogLevel.Warn);
return;
}
else if (behaviour == categoryBehaviour.Replace)
Expand All @@ -197,15 +197,15 @@ public void initSubcategory(int index, subCategoryItem toInit, PartCategorizer.C
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);
Core.Log("Filters duplicated in category {0} between subCategories:\r\n{1} and {2}", Core.LogLevel.Warn, 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());
Core.Log("subcategory {0} not found in subcategories Dictionary", Core.LogLevel.Warn, toInit.ToString());
return;
}

Expand All @@ -221,9 +221,9 @@ public void initSubcategory(int index, subCategoryItem toInit, PartCategorizer.C
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);
Core.Log("{0} failed to initialise\r\nCategory: {1}, Subcategory: {2}, filter?: {3}, filter count: {4}, Icon: {5}\r\n{6}\r\n{7}",
Core.LogLevel.Error,
subCategories[index], categoryName, sC.hasFilters, sC.filters.Count, Core.getIcon(sC.iconName), ex.Message, ex.StackTrace);
}
}

Expand Down
71 changes: 56 additions & 15 deletions FilterExtension/ConfigNodes/customSubCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void initialise(PartCategorizer.Category cat)
if (cat == null)
return;
RUI.Icons.Selectable.Icon icon = Core.getIcon(iconName);
PartCategorizer.AddCustomSubcategoryFilter(cat, this.subCategoryTitle, icon, p => checkFilters(p));
PartCategorizer.AddCustomSubcategoryFilter(cat, this.subCategoryTitle, icon, p => checkPartFilters(p));
}

/// <summary>
Expand Down Expand Up @@ -101,7 +101,7 @@ public object Clone()
/// <param name="part"></param>
/// <param name="depth"></param>
/// <returns></returns>
public bool checkFilters(AvailablePart part, int depth = 0)
public bool checkPartFilters(AvailablePart part, int depth = 0)
{
if (Editor.blackListedParts != null)
{
Expand All @@ -120,7 +120,57 @@ public bool checkFilters(AvailablePart part, int depth = 0)
return false;
}

return ((!template.Any() || template.Any(t => t.checkFilter(part, depth))) && filters.Any(f => f.checkFilter(part, depth))); // part passed a template if present, and a subcategory filter
return checkTemplate(part, depth);
}

/// <summary>
/// Go through the category template filters. If the template list is empty or the part matches one of the template filters, go on to check against the filters of this subcategory
/// </summary>
/// <param name="ap"></param>
/// <param name="depth"></param>
/// <returns></returns>
private bool checkTemplate(AvailablePart ap, int depth = 0)
{
if (template.Count == 0)
return checkFilters(ap, depth);
else
{
Filter t;
for (int i = 0; i < template.Count; ++i)
{
t = template[i];
if (t.filterResult(ap, depth))
{
return checkFilters(ap, depth);
}
}
return false;
}
}

/// <summary>
/// Go through the filters of this subcategory. If the filter list is empty or the part matches one of the filters we can accept that part into this subcategory
/// Templates have already been checked at this point
/// if there is no template or filter, hasFilters property will be false and this subcategory will be removed prior to this point
/// </summary>
/// <param name="ap"></param>
/// <param name="depth"></param>
/// <returns></returns>
private bool checkFilters(AvailablePart ap, int depth = 0)
{
if (filters.Count == 0)
return true;
else
{
Filter f;
for (int i = 0; i < filters.Count; ++i)
{
f = filters[i];
if (f.filterResult(ap, depth))
return true;
}
return false;
}
}

/// <summary>
Expand All @@ -131,9 +181,6 @@ public bool checkFilters(AvailablePart part, int depth = 0)
/// <returns>true if the subcategory contains any parts</returns>
public bool checkSubCategoryHasParts(string category)
{
//if (Editor.subcategoriesChecked)
// return true;

PartModuleFilter pmf;
AvailablePart p;
for (int i = 0; i < PartLoader.Instance.parts.Count; i++)
Expand All @@ -147,22 +194,16 @@ public bool checkSubCategoryHasParts(string category)
if (pmf.CheckForForceBlock(subCategoryTitle))
return false;
}
if (checkFilters(PartLoader.Instance.parts[i]))
if (checkPartFilters(PartLoader.Instance.parts[i]))
return true;
}

#warning this is not working. Empty categories will show up if the one pass checking is enabled
// only need to do this the first time we hit the editor
customCategory C = Core.Instance.Categories.FirstOrDefault(c => c.categoryName == category);
if (C != null)
C.subCategories.RemoveAll(s => s.subcategoryName == subCategoryTitle);

if (Settings.debug)
{
if (!string.IsNullOrEmpty(category))
Core.Log(subCategoryTitle + " in category " + category + " has no valid parts and was not initialised");
Core.Log(subCategoryTitle + " in category " + category + " has no valid parts and was not initialised", Core.LogLevel.Warn);
else
Core.Log(subCategoryTitle + " has no valid parts and was not initialised");
Core.Log(subCategoryTitle + " has no valid parts and was not initialised", Core.LogLevel.Warn);
}
return false;
}
Expand Down
35 changes: 26 additions & 9 deletions FilterExtension/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace FilterExtensions
[KSPAddon(KSPAddon.Startup.MainMenu, true)]
public class Core : MonoBehaviour
{
public static readonly Version version = new Version(2, 5, 0, 0);
public static readonly Version version = new Version(2, 5, 1, 0);

private static Core instance;
public static Core Instance
Expand Down Expand Up @@ -58,7 +58,7 @@ void Awake()
{
instance = this;
DontDestroyOnLoad(this);
Log(string.Empty);
Log(string.Empty, LogLevel.Warn);

getConfigs();
getPartData();
Expand Down Expand Up @@ -141,7 +141,7 @@ private void getPartData()
if (!partPathDict.ContainsKey(p.name))
partPathDict.Add(p.name, p.partUrl);
else
Log(p.name + " duplicated part key in part path dictionary");
Log(p.name + " duplicated part key in part path dictionary", LogLevel.Warn);

if (PartType.isEngine(p))
processEnginePropellants(p);
Expand All @@ -152,8 +152,8 @@ private void getPartData()
resources.AddUnique(r.resourceName);
}
}
if (p.partPrefab.Modules.Contains("PartModuleFilter"))
filterModules.Add(p.name, (PartModuleFilter)p.partPrefab.Modules["PartModuleFilter"]);
if (p.partPrefab.Modules.Contains<PartModuleFilter>())
filterModules.Add(p.name, p.partPrefab.Modules.GetModule<PartModuleFilter>());
}
generateEngineTypes();

Expand Down Expand Up @@ -490,18 +490,35 @@ public void SetNameAndIcon(ref string name, ref string icon)
icon = tmp;
}

public enum LogLevel
{
Log,
Warn,
Error
}

/// <summary>
/// Debug.Log with FE id/version inserted
/// </summary>
/// <param name="o"></param>
internal static void Log(object o)
internal static void Log(object o, LogLevel level = LogLevel.Log)
{
Debug.Log(string.Format("[Filter Extensions {0}]: {1}", version, o));
if (level == LogLevel.Log)
Debug.LogFormat("[Filter Extensions {0}]: {1}", version, o);
else if (level == LogLevel.Warn)
Debug.LogWarningFormat("[Filter Extensions {0}]: {1}", version, o);
else
Debug.LogErrorFormat("[Filter Extensions {0}]: {1}", version, o);
}

internal static void Log(string format, params object[] o)
internal static void Log(string format, LogLevel level = LogLevel.Log, params object[] o)
{
Debug.Log(string.Format("[Filter Extensions {0}]: ", version) + string.Format(format, o));
if (level == LogLevel.Log)
Debug.LogFormat("[Filter Extensions {0}]: {1}", version, string.Format(format, o));
else if (level == LogLevel.Warn)
Debug.LogWarningFormat("[Filter Extensions {0}]: {1}", version, string.Format(format, o));
else
Debug.LogErrorFormat("[Filter Extensions {0}]: {1}", version, string.Format(format, o));
}
}
}
Loading

0 comments on commit 4792c83

Please sign in to comment.