Some PropertyConditional refactoring & fixes:

- Changed the "attribute" field from a string to a more descriptively named enum (TODO: do the same for the operators).
- Changed the "value" field from an object to a string because the value is always a string anyway (might want to make it an object that holds the actual value we're comparing against so there's no need to do any parsing in the Matches method).
- Fixed "!=" operator checking for equality.
- Removed unnecessary property value getting from the Matches method (reflection ain't cheap!)
This commit is contained in:
Joonas Rikkonen
2018-01-09 16:10:07 +02:00
parent c502d9545c
commit e21959d0a2
2 changed files with 99 additions and 74 deletions
@@ -274,64 +274,65 @@ namespace Barotrauma
{
if (target == null || target.SerializableProperties == null) continue;
if (!target.SerializableProperties.TryGetValue(pc.Attribute, out SerializableProperty property))
string valStr = pc.Value.ToString();
switch (pc.Type)
{
//Do special conditional checks
string valStr = pc.Value.ToString();
if (pc.Attribute == "name")
case PropertyConditional.ConditionType.PropertyValue:
if (target.SerializableProperties.TryGetValue(pc.PropertyName.ToLowerInvariant(), out SerializableProperty property))
{
return pc.Matches(property);
}
return false;
case PropertyConditional.ConditionType.Name:
return pc.Operator == "==" ? target.Name == valStr : target.Name != valStr;
case PropertyConditional.ConditionType.HasTag:
{
string[] readTags = valStr.Split(',');
int matches = 0;
foreach (string tag in readTags)
if (((Item)target).HasTag(tag)) matches++;
if (pc.Attribute == "speciesname" && target is Character)
//If operator is == then it needs to match everything, otherwise if its != there must be zero matches.
return pc.Operator == "==" ? matches >= readTags.Length : matches <= 0;
}
case PropertyConditional.ConditionType.HasStatusTag:
List<DurationListElement> durations = DurationList.FindAll(d => d.Targets.Contains(target));
List<DelayedListElement> delays = DelayedEffect.DelayList.FindAll(d => d.Targets.Contains(target));
bool success = false;
if (durations.Any() || delays.Any())
{
string[] readTags = valStr.Split(',');
foreach (DurationListElement duration in durations)
{
int matches = 0;
foreach (string tag in readTags)
if (duration.Parent.HasTag(tag)) matches++;
success = pc.Operator == "==" ? matches >= readTags.Length : matches <= 0;
if (cancelStatusEffect > 0 && success)
DurationList.Remove(duration);
if (cancelStatusEffect != 2) //cancelStatusEffect 1 = only cancel once, cancelStatusEffect 2 = cancel all of matching tags
return success;
}
foreach (DelayedListElement delay in delays)
{
int matches = 0;
foreach (string tag in readTags)
if (delay.Parent.HasTag(tag)) matches++;
success = pc.Operator == "==" ? matches >= readTags.Length : matches <= 0;
if (cancelStatusEffect > 0 && success)
DelayedEffect.DelayList.Remove(delay);
if (cancelStatusEffect != 2) //ditto
return success;
}
}
return success;
case PropertyConditional.ConditionType.SpeciesName:
return pc.Operator == "==" ? ((Character)target).SpeciesName == valStr : ((Character)target).SpeciesName != valStr;
if ((pc.Attribute == "hastag" || pc.Attribute == "hastags") && target is Item)
{
string[] readTags = valStr.Split(',');
int matches = 0;
foreach (string tag in readTags)
if (((Item)target).HasTag(tag)) matches++;
//If operator is == then it needs to match everything, otherwise if its != there must be zero matches.
return pc.Operator == "==" ? matches >= readTags.Length : matches <= 0;
}
List<DurationListElement> durations = DurationList.FindAll(d => d.Targets.Contains(target));
List<DelayedListElement> delays = DelayedEffect.DelayList.FindAll(d => d.Targets.Contains(target));
bool success = false;
if (pc.Attribute == "hasstatustag" || pc.Attribute == "hasstatustags" && (durations.Any() || delays.Any()))
{
string[] readTags = valStr.Split(',');
foreach (DurationListElement duration in durations)
{
int matches = 0;
foreach (string tag in readTags)
if (duration.Parent.HasTag(tag)) matches++;
success = pc.Operator == "==" ? matches >= readTags.Length : matches <= 0;
if (cancelStatusEffect > 0 && success)
DurationList.Remove(duration);
if (cancelStatusEffect != 2) //cancelStatusEffect 1 = only cancel once, cancelStatusEffect 2 = cancel all of matching tags
return success;
}
foreach (DelayedListElement delay in delays)
{
int matches = 0;
foreach (string tag in readTags)
if (delay.Parent.HasTag(tag)) matches++;
success = pc.Operator == "==" ? matches >= readTags.Length : matches <= 0;
if (cancelStatusEffect > 0 && success)
DelayedEffect.DelayList.Remove(delay);
if (cancelStatusEffect != 2) //ditto
return success;
}
}
return success;
}
else if (!pc.Matches(property))
return false;
}
}
return true;