Added exception handling to SerializeProperty.TrySetValue methods

This commit is contained in:
Joonas Rikkonen
2017-12-02 15:50:40 +02:00
parent ccac8de723
commit c66191ca94
@@ -130,7 +130,15 @@ namespace Barotrauma
DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj + "\" to " + value + " (not a valid " + propertyInfo.PropertyType + ")", e); DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj + "\" to " + value + " (not a valid " + propertyInfo.PropertyType + ")", e);
return false; return false;
} }
propertyInfo.SetValue(obj, enumVal); try
{
propertyInfo.SetValue(obj, enumVal);
}
catch (Exception e)
{
DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj.ToString() + "\" to " + value.ToString(), e);
return false;
}
} }
else else
{ {
@@ -141,45 +149,55 @@ namespace Barotrauma
} }
} }
switch (typeName) try
{ {
case "bool": switch (typeName)
propertyInfo.SetValue(obj, value.ToLowerInvariant() == "true", null); {
break; case "bool":
case "int": propertyInfo.SetValue(obj, value.ToLowerInvariant() == "true", null);
int intVal; break;
if (int.TryParse(value, out intVal)) case "int":
{ int intVal;
propertyInfo.SetValue(obj, intVal, null); if (int.TryParse(value, out intVal))
} {
break; propertyInfo.SetValue(obj, intVal, null);
case "float": }
float floatVal; break;
if (float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out floatVal)) case "float":
{ float floatVal;
propertyInfo.SetValue(obj, floatVal, null); if (float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out floatVal))
} {
break; propertyInfo.SetValue(obj, floatVal, null);
case "string": }
propertyInfo.SetValue(obj, value, null); break;
break; case "string":
case "vector2": propertyInfo.SetValue(obj, value, null);
propertyInfo.SetValue(obj, XMLExtensions.ParseVector2(value)); break;
break; case "vector2":
case "vector3": propertyInfo.SetValue(obj, XMLExtensions.ParseVector2(value));
propertyInfo.SetValue(obj, XMLExtensions.ParseVector3(value)); break;
break; case "vector3":
case "vector4": propertyInfo.SetValue(obj, XMLExtensions.ParseVector3(value));
propertyInfo.SetValue(obj, XMLExtensions.ParseVector4(value)); break;
break; case "vector4":
case "color": propertyInfo.SetValue(obj, XMLExtensions.ParseVector4(value));
propertyInfo.SetValue(obj, XMLExtensions.ParseColor(value)); break;
break; case "color":
case "rectangle": propertyInfo.SetValue(obj, XMLExtensions.ParseColor(value));
propertyInfo.SetValue(obj, XMLExtensions.ParseRect(value, true)); break;
break; case "rectangle":
propertyInfo.SetValue(obj, XMLExtensions.ParseRect(value, true));
break;
}
} }
catch (Exception e)
{
DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj.ToString() + "\" to " + value.ToString(), e);
return false;
}
return true; return true;
} }
@@ -215,42 +233,52 @@ namespace Barotrauma
} }
} }
if (value.GetType() == typeof(string)) try
{ {
switch (typeName) if (value.GetType() == typeof(string))
{ {
case "string": switch (typeName)
propertyInfo.SetValue(obj, value, null); {
return true; case "string":
case "vector2": propertyInfo.SetValue(obj, value, null);
propertyInfo.SetValue(obj, XMLExtensions.ParseVector2((string)value)); return true;
return true; case "vector2":
case "vector3": propertyInfo.SetValue(obj, XMLExtensions.ParseVector2((string)value));
propertyInfo.SetValue(obj, XMLExtensions.ParseVector3((string)value)); return true;
return true; case "vector3":
case "vector4": propertyInfo.SetValue(obj, XMLExtensions.ParseVector3((string)value));
propertyInfo.SetValue(obj, XMLExtensions.ParseVector4((string)value)); return true;
return true; case "vector4":
case "color": propertyInfo.SetValue(obj, XMLExtensions.ParseVector4((string)value));
propertyInfo.SetValue(obj, XMLExtensions.ParseColor((string)value)); return true;
return true; case "color":
case "rectangle": propertyInfo.SetValue(obj, XMLExtensions.ParseColor((string)value));
propertyInfo.SetValue(obj, XMLExtensions.ParseColor((string)value)); return true;
return true; case "rectangle":
default: propertyInfo.SetValue(obj, XMLExtensions.ParseColor((string)value));
DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj.ToString() + "\" to " + value.ToString()); return true;
DebugConsole.ThrowError("(Cannot convert a string to a " + propertyDescriptor.PropertyType.ToString() + ")"); default:
return false; DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj.ToString() + "\" to " + value.ToString());
DebugConsole.ThrowError("(Cannot convert a string to a " + propertyDescriptor.PropertyType.ToString() + ")");
return false;
}
} }
else if (propertyDescriptor.PropertyType != value.GetType())
{
DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj.ToString() + "\" to " + value.ToString());
DebugConsole.ThrowError("(Non-matching type, should be " + propertyDescriptor.PropertyType + " instead of " + value.GetType() + ")");
return false;
}
propertyInfo.SetValue(obj, value, null);
} }
else if (propertyDescriptor.PropertyType != value.GetType())
catch (Exception e)
{ {
DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj.ToString() + "\" to " + value.ToString()); DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj.ToString() + "\" to " + value.ToString(), e);
DebugConsole.ThrowError("(Non-matching type, should be " + propertyDescriptor.PropertyType + " instead of " + value.GetType() + ")");
return false; return false;
} }
propertyInfo.SetValue(obj, value, null);
return true; return true;
} }
catch catch