Skip to content

Commit

Permalink
Change PartValidation so that all checks in a MODULE node apply to th…
Browse files Browse the repository at this point in the history
…e same module (as you'd expect). Also support using `title` instead of `label` which will be the entire loc string, rather than <label> = <whatever internal value was specified>. Add special handling for isEnabled = True / False which will check pm.IsEnabled rather than look for a basefield. (#26)
  • Loading branch information
NathanKell authored Oct 8, 2023
1 parent b41e808 commit 0a5838c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 22 deletions.
2 changes: 2 additions & 0 deletions GameData/ContractConfigurator/Localization/en-us.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ Localization
#cc.param.PartValidation.withModuleType = with module type: <<1>>
// <<With/All have/None have>> <<2>>: <<3>>
#cc.param.PartValidation.generic = <<1>> <<c:2>>: <<3>>
#cc.param.PartValidation.generic.moduleRoot = <<1>> <<2>>
#cc.param.PartValidation.generic.moduleSub = <<1>>: <<2>>
#cc.param.PartValidation.type = <<1>> type: <<2>>
#cc.param.PartValidation.module = <<1>> module: <<2>>
#cc.param.PartValidation.moduleType = <<1>> module type: <<2>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected void CreateDelegates()
foreach (List<Tuple<string, string, string>> list in filter.partModuleExtended)
{
ContractParameter wrapperParam = AddParameter(new AllParameterDelegate<Part>(Localizer.Format("#cc.param.PartValidation.moduleShort", filter.type.Prefix()), filter.type));

List<Tuple<string, string, string>> newTuples = new List<Tuple<string, string, string>>();
foreach (Tuple<string, string, string> v in list)
{
string name = v.Item1;
Expand All @@ -151,10 +151,27 @@ protected void CreateDelegates()
label = v.Item2;
}
string value = name == "name" ? ModuleName(v.Item3) : v.Item3;
newTuples.Add(new Tuple<string, string, string>(name, label, value));
}

ParameterDelegateMatchType childFilter = ParameterDelegateMatchType.FILTER;
wrapperParam.AddParameter(new ParameterDelegate<Part>(Localizer.Format("#cc.param.PartValidation.generic", childFilter.Prefix(), label, value), p => PartModuleCheck(p, v), childFilter));
ParameterDelegateMatchType childFilter = ParameterDelegateMatchType.FILTER;
string loc;
if (newTuples.Count == 1)
{
var t = newTuples[0];
if (t.Item2.Length > 0 && t.Item2[0] == '¶')
loc = Localizer.Format("#cc.param.PartValidation.generic.moduleRoot", childFilter.Prefix(), t.Item2.Substring(1));
else
loc = Localizer.Format("#cc.param.PartValidation.generic", childFilter.Prefix(), t.Item2, t.Item3);
}
else
{
List<string> subStrings = new List<string>();
foreach (var v in newTuples)
subStrings.Add(v.Item2.Length > 0 && v.Item2[0] == '¶' ? v.Item2.Substring(1) : Localizer.Format("#cc.param.PartValidation.generic.moduleSub", v.Item2, v.Item3));
loc = Localizer.Format("#cc.param.PartValidation.generic.moduleRoot", childFilter.Prefix(), Localizer.Format($"<<and(1,{subStrings.Count})>>", subStrings.ToArray()));
}
AddParameter(new ParameterDelegate<Part>(loc, p => PartModuleCheck(p, newTuples), childFilter));
}

// Filter by category
Expand Down Expand Up @@ -204,7 +221,7 @@ public static string ModuleName(string partModule)
public static string ModuleTypeName(string partModule)
{
// We don't have a reliable way to translate these, so we just do hardcoded mappings
switch(partModule)
switch (partModule)
{
case "Antenna":
return Localizer.GetStringByTag("#autoLOC_234196");
Expand Down Expand Up @@ -240,29 +257,40 @@ private bool PartHasObjective(Part p, string contractObjective)
return p.HasValidContractObjective(contractObjective);
}

private bool PartModuleCheck(Part p, Tuple<string, string, string> v)
private bool PartModuleCheck(Part p, List<Tuple<string, string, string>> tuples)
{
foreach (PartModule pm in p.Modules)
{
if (v.Item1 == "name")
{
if (pm.moduleName == v.Item3)
{
return true;
}
}
else if (v.Item1 == "EngineType")
bool match = true;
foreach (var v in tuples)
{
ModuleEngines me = pm as ModuleEngines;
return me != null && me.engineType.ToString() == v.Item3;
match &= ModuleCheck(pm, v);
}
if (match)
return true;
}
return false;
}

foreach (BaseField field in pm.Fields)
private static bool ModuleCheck(PartModule pm, Tuple<string, string, string> v)
{
if (v.Item1 == "name")
return pm.moduleName == v.Item3;

if (v.Item1 == "isEnabled")
return pm.isEnabled.ToString() == v.Item3;

if (v.Item1 == "EngineType")
{
ModuleEngines me = pm as ModuleEngines;
return me != null && me.engineType.ToString() == v.Item3;
}

foreach (BaseField field in pm.Fields)
{
if (field != null && field.name == v.Item1 && field.originalValue != null && field.originalValue.ToString() == v.Item3)
{
if (field != null && field.name == v.Item1 && field.originalValue != null && field.originalValue.ToString() == v.Item3)
{
return true;
}
return true;
}
}
return false;
Expand Down Expand Up @@ -317,7 +345,10 @@ protected override void OnParameterSave(ConfigNode node)
{
if (!String.IsNullOrEmpty(v.Item2))
{
moduleNode.AddValue("label", v.Item2);
if (v.Item2[0] == '¶')
moduleNode.AddValue("title", v.Item2.Substring(1));
else
moduleNode.AddValue("label", v.Item2);
}
moduleNode.AddValue(v.Item1, v.Item3);
}
Expand Down Expand Up @@ -377,6 +408,10 @@ protected override void OnParameterLoad(ConfigNode node)
{
nextLabel = v.value;
}
else if (v.name == "title")
{
nextLabel = "¶" + v.value;
}
else
{
tmp.Add(new Tuple<string, string, string>(v.name, nextLabel, v.value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override bool Load(ConfigNode configNode)
}

// Standard definition
if (configNode.HasValue("part") || configNode.HasValue("partModule") || configNode.HasValue("partModuleType") || configNode.HasValue("category") || configNode.HasValue("manufacturer"))
if (configNode.HasValue("part") || configNode.HasValue("partModule") || configNode.HasValue("partModuleType") || configNode.HasValue("category") || configNode.HasValue("manufacturer") || configNode.HasNode("MODULE"))
{
PartValidation.Filter filter = new PartValidation.Filter(defaultMatch);
valid &= ConfigNodeUtil.ParseValue<List<AvailablePart>>(configNode, "part", x => filter.parts = x, this, new List<AvailablePart>());
Expand All @@ -61,6 +61,10 @@ public override bool Load(ConfigNode configNode)
{
nextLabel = v.value;
}
else if (v.name == "title")
{
nextLabel = "¶" + v.value;
}
else
{
tmp.Add(new Tuple<string, string, string>(v.name, nextLabel, v.value));
Expand Down Expand Up @@ -126,6 +130,10 @@ public override bool Load(ConfigNode configNode)
{
nextLabel = v.value;
}
else if (v.name == "title")
{
nextLabel = "¶" + v.value;
}
else
{
tmp.Add(new Tuple<string, string, string>(v.name, nextLabel, v.value));
Expand Down

0 comments on commit 0a5838c

Please sign in to comment.