Changed PropertyConditional operators from strings to enums

This commit is contained in:
Joonas Rikkonen
2018-01-09 16:18:05 +02:00
parent e21959d0a2
commit 0671dba1b4
2 changed files with 32 additions and 23 deletions
@@ -15,9 +15,19 @@ namespace Barotrauma
HasStatusTag HasStatusTag
} }
public enum OperatorType
{
Equals,
NotEquals,
LessThan,
LessThanEquals,
GreaterThan,
GreaterThanEquals
}
public readonly ConditionType Type; public readonly ConditionType Type;
public readonly string PropertyName; public readonly string PropertyName;
public readonly string Operator; public readonly OperatorType Operator;
public readonly string Value; public readonly string Value;
public PropertyConditional(XAttribute attribute) public PropertyConditional(XAttribute attribute)
@@ -25,7 +35,6 @@ namespace Barotrauma
string attributeString = attribute.Value.ToString(); string attributeString = attribute.Value.ToString();
string atStr = attributeString; string atStr = attributeString;
string[] splitString = atStr.Split(' '); string[] splitString = atStr.Split(' ');
string op = splitString[0];
if (splitString.Length > 0) if (splitString.Length > 0)
{ {
for (int i = 1; i < splitString.Length; i++) for (int i = 1; i < splitString.Length; i++)
@@ -34,12 +43,13 @@ namespace Barotrauma
} }
} }
//thanks xml for not letting me use < or > in attributes :( //thanks xml for not letting me use < or > in attributes :(
string op = splitString[0];
switch (op) switch (op)
{ {
case "e": case "e":
case "eq": case "eq":
case "equals": case "equals":
op = "=="; Operator = OperatorType.Equals;
break; break;
case "ne": case "ne":
case "neq": case "neq":
@@ -48,31 +58,31 @@ namespace Barotrauma
case "!e": case "!e":
case "!eq": case "!eq":
case "!equals": case "!equals":
op = "!="; Operator = OperatorType.NotEquals;
break; break;
case "gt": case "gt":
case "greaterthan": case "greaterthan":
op = ">"; Operator = OperatorType.GreaterThan;
break; break;
case "lt": case "lt":
case "lessthan": case "lessthan":
op = "<"; Operator = OperatorType.LessThan;
break; break;
case "gte": case "gte":
case "gteq": case "gteq":
case "greaterthanequals": case "greaterthanequals":
op = ">="; Operator = OperatorType.GreaterThanEquals;
break; break;
case "lte": case "lte":
case "lteq": case "lteq":
case "lessthanequals": case "lessthanequals":
op = "<="; Operator = OperatorType.LessThanEquals;
break; break;
default: default:
if (op != "==" && op != "!=" && op != ">" && op != "<" && op != ">=" && op != "<=") //Didn't use escape strings or anything if (op != "==" && op != "!=" && op != ">" && op != "<" && op != ">=" && op != "<=") //Didn't use escape strings or anything
{ {
atStr = attributeString; //We probably don't even have an operator atStr = attributeString; //We probably don't even have an operator
op = "=="; DebugConsole.ThrowError("Error in property conditional - \"" + op + "\" is not a valid operator.");
} }
break; break;
} }
@@ -83,7 +93,6 @@ namespace Barotrauma
Type = ConditionType.PropertyValue; Type = ConditionType.PropertyValue;
} }
Operator = op;
Value = atStr; Value = atStr;
} }
@@ -111,7 +120,7 @@ namespace Barotrauma
switch (Operator) switch (Operator)
{ {
case "==": case OperatorType.Equals:
if (floatValue == null) if (floatValue == null)
{ {
return property.GetValue().Equals(floatValue); return property.GetValue().Equals(floatValue);
@@ -120,7 +129,7 @@ namespace Barotrauma
{ {
return property.GetValue().Equals(Value); return property.GetValue().Equals(Value);
} }
case "!=": case OperatorType.NotEquals:
if (floatValue == null) if (floatValue == null)
{ {
return !property.GetValue().Equals(floatValue); return !property.GetValue().Equals(floatValue);
@@ -129,7 +138,7 @@ namespace Barotrauma
{ {
return !property.GetValue().Equals(Value); return !property.GetValue().Equals(Value);
} }
case ">": case OperatorType.GreaterThan:
if (floatValue == null) if (floatValue == null)
{ {
DebugConsole.ThrowError("Couldn't compare " + Value.ToString() + " (" + Value.GetType() + ") to property \"" + property.Name + "\" (" + type + ")! " DebugConsole.ThrowError("Couldn't compare " + Value.ToString() + " (" + Value.GetType() + ") to property \"" + property.Name + "\" (" + type + ")! "
@@ -138,7 +147,7 @@ namespace Barotrauma
else if (floatProperty > floatValue) else if (floatProperty > floatValue)
return true; return true;
break; break;
case "<": case OperatorType.LessThan:
if (floatValue == null) if (floatValue == null)
{ {
DebugConsole.ThrowError("Couldn't compare " + Value.ToString() + " (" + Value.GetType() + ") to property \"" + property.Name + "\" (" + type + ")! " DebugConsole.ThrowError("Couldn't compare " + Value.ToString() + " (" + Value.GetType() + ") to property \"" + property.Name + "\" (" + type + ")! "
@@ -147,7 +156,7 @@ namespace Barotrauma
else if (floatProperty < floatValue) else if (floatProperty < floatValue)
return true; return true;
break; break;
case ">=": case OperatorType.GreaterThanEquals:
if (floatValue == null) if (floatValue == null)
{ {
DebugConsole.ThrowError("Couldn't compare " + Value.ToString() + " (" + Value.GetType() + ") to property \"" + property.Name + "\" (" + type + ")! " DebugConsole.ThrowError("Couldn't compare " + Value.ToString() + " (" + Value.GetType() + ") to property \"" + property.Name + "\" (" + type + ")! "
@@ -156,7 +165,7 @@ namespace Barotrauma
else if (floatProperty >= floatValue) else if (floatProperty >= floatValue)
return true; return true;
break; break;
case "<=": case OperatorType.LessThanEquals:
if (floatValue == null) if (floatValue == null)
{ {
DebugConsole.ThrowError("Couldn't compare " + Value.ToString() + " (" + Value.GetType() + ") to property \"" + property.Name + "\" (" + type + ")! " DebugConsole.ThrowError("Couldn't compare " + Value.ToString() + " (" + Value.GetType() + ") to property \"" + property.Name + "\" (" + type + ")! "
@@ -285,7 +285,7 @@ namespace Barotrauma
} }
return false; return false;
case PropertyConditional.ConditionType.Name: case PropertyConditional.ConditionType.Name:
return pc.Operator == "==" ? target.Name == valStr : target.Name != valStr; return (pc.Operator == PropertyConditional.OperatorType.Equals) == (target.Name == valStr);
case PropertyConditional.ConditionType.HasTag: case PropertyConditional.ConditionType.HasTag:
{ {
string[] readTags = valStr.Split(','); string[] readTags = valStr.Split(',');
@@ -294,7 +294,7 @@ namespace Barotrauma
if (((Item)target).HasTag(tag)) matches++; if (((Item)target).HasTag(tag)) matches++;
//If operator is == then it needs to match everything, otherwise if its != there must be zero 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; return pc.Operator == PropertyConditional.OperatorType.Equals ? matches >= readTags.Length : matches <= 0;
} }
case PropertyConditional.ConditionType.HasStatusTag: case PropertyConditional.ConditionType.HasStatusTag:
List<DurationListElement> durations = DurationList.FindAll(d => d.Targets.Contains(target)); List<DurationListElement> durations = DurationList.FindAll(d => d.Targets.Contains(target));
@@ -310,7 +310,7 @@ namespace Barotrauma
foreach (string tag in readTags) foreach (string tag in readTags)
if (duration.Parent.HasTag(tag)) matches++; if (duration.Parent.HasTag(tag)) matches++;
success = pc.Operator == "==" ? matches >= readTags.Length : matches <= 0; success = pc.Operator == PropertyConditional.OperatorType.Equals ? matches >= readTags.Length : matches <= 0;
if (cancelStatusEffect > 0 && success) if (cancelStatusEffect > 0 && success)
DurationList.Remove(duration); DurationList.Remove(duration);
if (cancelStatusEffect != 2) //cancelStatusEffect 1 = only cancel once, cancelStatusEffect 2 = cancel all of matching tags if (cancelStatusEffect != 2) //cancelStatusEffect 1 = only cancel once, cancelStatusEffect 2 = cancel all of matching tags
@@ -322,7 +322,7 @@ namespace Barotrauma
foreach (string tag in readTags) foreach (string tag in readTags)
if (delay.Parent.HasTag(tag)) matches++; if (delay.Parent.HasTag(tag)) matches++;
success = pc.Operator == "==" ? matches >= readTags.Length : matches <= 0; success = pc.Operator == PropertyConditional.OperatorType.Equals ? matches >= readTags.Length : matches <= 0;
if (cancelStatusEffect > 0 && success) if (cancelStatusEffect > 0 && success)
DelayedEffect.DelayList.Remove(delay); DelayedEffect.DelayList.Remove(delay);
if (cancelStatusEffect != 2) //ditto if (cancelStatusEffect != 2) //ditto
@@ -331,7 +331,7 @@ namespace Barotrauma
} }
return success; return success;
case PropertyConditional.ConditionType.SpeciesName: case PropertyConditional.ConditionType.SpeciesName:
return pc.Operator == "==" ? ((Character)target).SpeciesName == valStr : ((Character)target).SpeciesName != valStr; return (pc.Operator == PropertyConditional.OperatorType.Equals) == (((Character)target).SpeciesName == valStr);
} }
} }
} }