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

Commit

Permalink
first pass at part module based filtering. Needs testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Crzyrndm committed Feb 20, 2016
1 parent 1cdd62b commit 66b0166
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 22 deletions.
10 changes: 10 additions & 0 deletions FilterExtension/ConfigNodes/customSubCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ public bool checkFilters(AvailablePart part, int depth = 0)
}
if (!unPurchasedOverride && Core.Instance.hideUnpurchased && !ResearchAndDevelopment.PartModelPurchased(part) && !ResearchAndDevelopment.IsExperimentalPart(part))
return false;

PartModuleFilter pmf;
if (Core.Instance.filterModules.TryGetValue(part.name, out pmf))
{
if (pmf.CheckForForceAdd(subCategoryTitle))
return true;
if (pmf.CheckForForceBlock(subCategoryTitle))
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
}

Expand Down
40 changes: 22 additions & 18 deletions FilterExtension/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ public class Core : MonoBehaviour
public List<List<string>> propellantCombos = new List<List<string>>();
// entry for each unique resource
public List<string> resources = new List<string>();

// Dictionary of icons created on entering the main menu
public Dictionary<string, RUI.Icons.Selectable.Icon> iconDict = new Dictionary<string, RUI.Icons.Selectable.Icon>();
// Dictionary of all filtering part modules by part name
public Dictionary<string, PartModuleFilter> filterModules = new Dictionary<string, PartModuleFilter>();


// Config has options to disable the FbM replacement, and the default Category/SC and sort method
public bool hideUnpurchased = true;
Expand All @@ -59,7 +61,7 @@ void Awake()
{
instance = this;
DontDestroyOnLoad(this);
Log("Version 2.4.1");
Log("Version 2.5.0");

getConfigs();
getPartData();
Expand Down Expand Up @@ -154,26 +156,28 @@ private void getPartData()
RepairAvailablePartUrl(p);

// if the url is still borked, can't associate a mod to the part
if (string.IsNullOrEmpty(p.partUrl))
continue;

// list of GameData folders
modNames.AddUnique(p.partUrl.Split(new char[] { '/', '\\' })[0]);
if (!string.IsNullOrEmpty(p.partUrl))
{
// list of GameData folders
modNames.AddUnique(p.partUrl.Split(new char[] { '/', '\\' })[0]);

// associate the path to the part
if (!partPathDict.ContainsKey(p.name))
partPathDict.Add(p.name, p.partUrl);
else
Log(p.name + " duplicated part key in part path dictionary");
// associate the path to the part
if (!partPathDict.ContainsKey(p.name))
partPathDict.Add(p.name, p.partUrl);
else
Log(p.name + " duplicated part key in part path dictionary");

if (PartType.isEngine(p))
processEnginePropellants(p);
if (PartType.isEngine(p))
processEnginePropellants(p);

if (p.partPrefab.Resources != null)
{
foreach (PartResource r in p.partPrefab.Resources)
resources.AddUnique(r.resourceName);
if (p.partPrefab.Resources != null)
{
foreach (PartResource r in p.partPrefab.Resources)
resources.AddUnique(r.resourceName);
}
}
if (p.partPrefab.Modules.Contains("PartModuleFilter"))
filterModules.Add(p.name, (PartModuleFilter)p.partPrefab.Modules["PartModuleFilter"]);
}

if (replaceFbM)
Expand Down
6 changes: 4 additions & 2 deletions FilterExtension/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ IEnumerator editorInit()
/// </summary>
void findPartsToBlock()
{
PartModuleFilter pmf;
// all parts that may not be visible
List<AvailablePart> partsToCheck = PartLoader.Instance.parts.FindAll(ap => ap.category == PartCategories.none);
List<AvailablePart> partsToCheck = PartLoader.Instance.parts.FindAll(ap => ap.category == PartCategories.none
&& !(Core.Instance.filterModules.TryGetValue(ap.name, out pmf) && pmf.hasForceAdd()));
// Only checking the category which should be Filter by Function (should I find FbF explcitly?)
PartCategorizer.Category mainCat = PartCategorizer.Instance.filters[0];
// has a reference to all the subcats that FE added to the category
Expand All @@ -131,7 +133,7 @@ void findPartsToBlock()
{
PartCategorizer.Category subCat = mainCat.subcategories[i];
// if the name is an FE subcat and the category should have that FE subcat and it's not the duplicate of one already seen created by another mod, mark it seen and move on
if (Core.Instance.subCategoriesDict.ContainsKey(subCat.button.categoryName) && customMainCat.subCategories.Any(subItem => string.Equals(subItem.subcategoryName, subCat.button.categoryName, StringComparison.CurrentCulture)) && !subCatsSeen.Contains(subCat.button.categoryName))
if (Core.Instance.subCategoriesDict.ContainsKey(subCat.button.categoryName) && customMainCat.subCategories.Any(subItem => string.Equals(subItem.subcategoryName, subCat.button.categoryName, StringComparison.CurrentCulture)))
subCatsSeen.Add(subCat.button.categoryName);
else // subcat created by another mod
{
Expand Down
1 change: 1 addition & 0 deletions FilterExtension/FilterExtensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<Compile Include="ConfigNodes\customCategory.cs" />
<Compile Include="ConfigNodes\Check.cs" />
<Compile Include="Core.cs" />
<Compile Include="PartModuleFilter.cs" />
<Compile Include="Settings.cs" />
<Compile Include="Utility\Extensions.cs" />
<Compile Include="Utility\PartType.cs" />
Expand Down
69 changes: 69 additions & 0 deletions FilterExtension/PartModuleFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace FilterExtensions
{
public class PartModuleFilter : PartModule
{
/// <summary>
/// list names of FE categories to push this part into even if it doesn't otherwise match the filters
/// </summary>
[KSPField]
public string filterAdd;
public HashSet<string> subcategoriesToAdd;

/// <summary>
/// list names of FE categories to never let this part be added to
/// </summary>
[KSPField]
public string filterBlock;
public HashSet<string> subcategoriesToBlock;

public bool CheckForForceAdd(string subcategory)
{
addStringToSet();
return subcategoriesToAdd.Contains(subcategory);
}

public bool CheckForForceBlock(string subcategory)
{
blockStringToSet();
return subcategoriesToBlock.Contains(subcategory);
}

void addStringToSet()
{
if (subcategoriesToAdd == null)
{
subcategoriesToAdd = new HashSet<string>();
string[] temp = filterAdd.Split(',');
for (int i = 0; i < temp.Length; ++i)
subcategoriesToAdd.Add(temp[i].Trim());
}
}

void blockStringToSet()
{
if (subcategoriesToBlock == null)
{
subcategoriesToBlock = new HashSet<string>();
string[] temp = filterBlock.Split(',');
for (int i = 0; i < temp.Length; ++i)
subcategoriesToBlock.Add(temp[i].Trim());
}
}

public bool hasForceAdd()
{
addStringToSet();
return subcategoriesToAdd.Any();
}

public bool hasForceBlock()
{
blockStringToSet();
return subcategoriesToBlock.Any();
}
}
}
4 changes: 2 additions & 2 deletions FilterExtension/Utility/PartType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ public static bool checkModuleName(AvailablePart part, string[] value, bool cont
if (part.partPrefab == null || part.partPrefab.Modules == null)
return false;
if (contains)
return value.Any(s => part.partPrefab.Modules.Contains(s) || checkModuleNameType(part, s));
return value.Any(s => checkModuleNameType(part, s) || part.partPrefab.Modules.Contains(s));
else
{
foreach (PartModule module in part.partPrefab.Modules)
{
foreach (string s in value)
{
if (s != module.ClassName)
if (s != module.ClassName.Replace('.', '_'))
return true;
}
}
Expand Down
Binary file modified GameData/000_FilterExtensions/FilterExtensions.dll
Binary file not shown.
Binary file added Icons.zip
Binary file not shown.

0 comments on commit 66b0166

Please sign in to comment.