38f1ddb...178a853: v0.8.9.1, removed content folder
This commit is contained in:
@@ -2,9 +2,15 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Xml.Linq;
|
||||
using System.Linq;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
// TODO: This class should be refactored:
|
||||
// - Use XElement instead of XAttribute in the constructor
|
||||
// - Simplify, remove unnecessary conversions
|
||||
// - Improve the flow so that the logic is undestandable.
|
||||
// - Maybe ass some test cases for the operators?
|
||||
class PropertyConditional
|
||||
{
|
||||
public enum ConditionType
|
||||
@@ -13,7 +19,8 @@ namespace Barotrauma
|
||||
Name,
|
||||
SpeciesName,
|
||||
HasTag,
|
||||
HasStatusTag
|
||||
HasStatusTag,
|
||||
Affliction
|
||||
}
|
||||
|
||||
public enum OperatorType
|
||||
@@ -27,22 +34,34 @@ namespace Barotrauma
|
||||
}
|
||||
|
||||
public readonly ConditionType Type;
|
||||
public readonly string PropertyName;
|
||||
public readonly OperatorType Operator;
|
||||
public readonly string Value;
|
||||
public readonly string AttributeName;
|
||||
public readonly string AttributeValue;
|
||||
public readonly float? FloatValue;
|
||||
|
||||
public readonly string TargetItemComponentName;
|
||||
|
||||
private readonly string[] afflictionNames = new string[] { "internaldamage", "bleeding", "burn", "oxygenlow", "bloodloss", "pressure", "stun", "husk", "afflictionhusk" };
|
||||
|
||||
private readonly int cancelStatusEffect;
|
||||
|
||||
// TODO: use XElement instead of XAttribute
|
||||
public PropertyConditional(XAttribute attribute)
|
||||
{
|
||||
string attributeString = attribute.Value.ToString();
|
||||
string atStr = attributeString;
|
||||
string[] splitString = atStr.Split(' ');
|
||||
AttributeName = attribute.Name.ToString().ToLowerInvariant();
|
||||
string attributeValueString = attribute.Value.ToString();
|
||||
if (string.IsNullOrWhiteSpace(attributeValueString))
|
||||
{
|
||||
DebugConsole.ThrowError($"Conditional attribute value is empty: {attribute.Parent.ToString()}");
|
||||
return;
|
||||
}
|
||||
string valueString = attributeValueString;
|
||||
string[] splitString = valueString.Split(' ');
|
||||
if (splitString.Length > 0)
|
||||
{
|
||||
for (int i = 1; i < splitString.Length; i++)
|
||||
{
|
||||
atStr = splitString[i] + (i > 1 && i < splitString.Length ? " " : "");
|
||||
valueString = splitString[i] + (i > 1 && i < splitString.Length ? " " : "");
|
||||
}
|
||||
}
|
||||
//thanks xml for not letting me use < or > in attributes :(
|
||||
@@ -84,11 +103,13 @@ namespace Barotrauma
|
||||
default:
|
||||
if (op != "==" && op != "!=" && op != ">" && op != "<" && op != ">=" && op != "<=") //Didn't use escape strings or anything
|
||||
{
|
||||
atStr = attributeString; //We probably don't even have an operator
|
||||
valueString = attributeValueString; //We probably don't even have an operator
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
TargetItemComponentName = attribute.Parent.GetAttributeString("targetitemcomponent", "");
|
||||
|
||||
foreach (XElement subElement in attribute.Parent.Elements())
|
||||
{
|
||||
switch (subElement.Name.ToString().ToLowerInvariant())
|
||||
@@ -103,24 +124,34 @@ namespace Barotrauma
|
||||
}
|
||||
}
|
||||
|
||||
if (!Enum.TryParse(attribute.Name.ToString(), true, out Type))
|
||||
if (!Enum.TryParse(AttributeName, true, out Type))
|
||||
{
|
||||
PropertyName = attribute.Name.ToString();
|
||||
Type = ConditionType.PropertyValue;
|
||||
if (afflictionNames.Any(n => n == AttributeName))
|
||||
{
|
||||
Type = ConditionType.Affliction;
|
||||
}
|
||||
else
|
||||
{
|
||||
Type = ConditionType.PropertyValue;
|
||||
}
|
||||
}
|
||||
|
||||
Value = atStr;
|
||||
AttributeValue = valueString;
|
||||
if (float.TryParse(AttributeValue, NumberStyles.Float, CultureInfo.InvariantCulture, out float value))
|
||||
{
|
||||
FloatValue = value;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Matches(ISerializableEntity target)
|
||||
{
|
||||
string valStr = Value.ToString();
|
||||
string valStr = AttributeValue.ToString();
|
||||
|
||||
switch (Type)
|
||||
{
|
||||
case ConditionType.PropertyValue:
|
||||
SerializableProperty property;
|
||||
if (target.SerializableProperties.TryGetValue(PropertyName.ToLowerInvariant(), out property))
|
||||
if (target.SerializableProperties.TryGetValue(AttributeName, out property))
|
||||
{
|
||||
return Matches(property);
|
||||
}
|
||||
@@ -175,31 +206,52 @@ namespace Barotrauma
|
||||
Character targetCharacter = target as Character;
|
||||
if (targetCharacter == null) return false;
|
||||
return (Operator == OperatorType.Equals) == (targetCharacter.SpeciesName == valStr);
|
||||
case ConditionType.Affliction:
|
||||
if (target is Character targetChar)
|
||||
{
|
||||
var affliction = targetChar.CharacterHealth.GetAffliction(AttributeName);
|
||||
if (affliction == null) { return false; }
|
||||
if (FloatValue.HasValue)
|
||||
{
|
||||
float value = FloatValue.Value;
|
||||
switch (Operator)
|
||||
{
|
||||
case OperatorType.Equals:
|
||||
return affliction.Strength == value;
|
||||
case OperatorType.GreaterThan:
|
||||
return affliction.Strength > value;
|
||||
case OperatorType.GreaterThanEquals:
|
||||
return affliction.Strength >= value;
|
||||
case OperatorType.LessThan:
|
||||
return affliction.Strength < value;
|
||||
case OperatorType.LessThanEquals:
|
||||
return affliction.Strength <= value;
|
||||
case OperatorType.NotEquals:
|
||||
return affliction.Strength != value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: refactor and add tests
|
||||
private bool Matches(SerializableProperty property)
|
||||
{
|
||||
object propertyValue = property.GetValue();
|
||||
|
||||
if (propertyValue == null)
|
||||
{
|
||||
DebugConsole.ThrowError("Couldn't compare " + Value.ToString() + " (" + Value.GetType() + ") to property \"" + property.Name + "\" - property.GetValue() returns null!");
|
||||
DebugConsole.ThrowError("Couldn't compare " + AttributeValue.ToString() + " (" + AttributeValue.GetType() + ") to property \"" + property.Name + "\" - property.GetValue() returns null!");
|
||||
return false;
|
||||
}
|
||||
|
||||
Type type = propertyValue.GetType();
|
||||
float? floatValue = null;
|
||||
float? floatProperty = null;
|
||||
if (type == typeof(float) || type == typeof(int))
|
||||
{
|
||||
float parsedFloat;
|
||||
if (Single.TryParse(Value, NumberStyles.Float, CultureInfo.InvariantCulture, out parsedFloat))
|
||||
{
|
||||
floatValue = parsedFloat;
|
||||
}
|
||||
floatProperty = (float)propertyValue;
|
||||
}
|
||||
|
||||
@@ -208,64 +260,72 @@ namespace Barotrauma
|
||||
case OperatorType.Equals:
|
||||
if (type == typeof(bool))
|
||||
{
|
||||
return ((bool)propertyValue) == (Value.ToLowerInvariant() == "true");
|
||||
return ((bool)propertyValue) == (AttributeValue == "true");
|
||||
}
|
||||
else if (floatValue == null)
|
||||
else if (FloatValue == null)
|
||||
{
|
||||
return propertyValue.ToString().Equals(Value);
|
||||
return propertyValue.ToString().Equals(AttributeValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
return propertyValue.Equals(floatValue);
|
||||
return propertyValue.Equals(FloatValue);
|
||||
}
|
||||
case OperatorType.NotEquals:
|
||||
if (type == typeof(bool))
|
||||
{
|
||||
return ((bool)propertyValue) != (Value.ToLowerInvariant() == "true");
|
||||
return ((bool)propertyValue) != (AttributeValue == "true");
|
||||
}
|
||||
else if (floatValue == null)
|
||||
else if (FloatValue == null)
|
||||
{
|
||||
return !propertyValue.ToString().Equals(Value);
|
||||
return !propertyValue.ToString().Equals(AttributeValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
return !propertyValue.Equals(floatValue);
|
||||
return !propertyValue.Equals(FloatValue);
|
||||
}
|
||||
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 " + AttributeValue.ToString() + " (" + AttributeValue.GetType() + ") to property \"" + property.Name + "\" (" + type + ")! "
|
||||
+ "Make sure the type of the value set in the config files matches the type of the property.");
|
||||
}
|
||||
else if (floatProperty > floatValue)
|
||||
else if (floatProperty > FloatValue)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
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 " + AttributeValue.ToString() + " (" + AttributeValue.GetType() + ") to property \"" + property.Name + "\" (" + type + ")! "
|
||||
+ "Make sure the type of the value set in the config files matches the type of the property.");
|
||||
}
|
||||
else if (floatProperty < floatValue)
|
||||
else if (floatProperty < FloatValue)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
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 " + AttributeValue.ToString() + " (" + AttributeValue.GetType() + ") to property \"" + property.Name + "\" (" + type + ")! "
|
||||
+ "Make sure the type of the value set in the config files matches the type of the property.");
|
||||
}
|
||||
else if (floatProperty >= floatValue)
|
||||
else if (floatProperty >= FloatValue)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
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 " + AttributeValue.ToString() + " (" + AttributeValue.GetType() + ") to property \"" + property.Name + "\" (" + type + ")! "
|
||||
+ "Make sure the type of the value set in the config files matches the type of the property.");
|
||||
}
|
||||
else if (floatProperty <= floatValue)
|
||||
else if (floatProperty <= FloatValue)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user