Added some boilerplate to UDMF map format reader. If a property value is Integer and expected property type is Float, a value will be casted to float. In other cases of type mismatch, the property will be ignored and a warning will be added to Errors and Warnings window.

This commit is contained in:
MaxED 2012-11-19 09:44:05 +00:00
parent f731000db3
commit 7a952a2e5e
2 changed files with 31 additions and 10 deletions

View file

@ -67,6 +67,11 @@ namespace CodeImp.DoomBuilder.IO
{
if(value.GetType() != t) throw new Exception("The value of entry '" + key + "' is of incompatible type (expected " + t.Name + ")");
}
//mxd
public bool IsValidType(Type t) {
return value.GetType() == t;
}
#endregion
}

View file

@ -46,6 +46,7 @@ namespace CodeImp.DoomBuilder.IO
private Configuration config;
private bool setknowncustomtypes;
private bool strictchecking = true;
private Dictionary<string, UniversalFieldInfo> defaultFields; //mxd. Type checks
#endregion
@ -418,18 +419,33 @@ namespace CodeImp.DoomBuilder.IO
{
int type = (int)UniversalType.Integer;
// Determine default type
if(e.Value.GetType() == typeof(int)) type = (int)UniversalType.Integer;
else if(e.Value.GetType() == typeof(float)) type = (int)UniversalType.Float;
else if(e.Value.GetType() == typeof(bool)) type = (int)UniversalType.Boolean;
else if(e.Value.GetType() == typeof(string)) type = (int)UniversalType.String;
// Try to find the type from configuration
if(setknowncustomtypes)
type = General.Map.Options.GetUniversalFieldType(elementname, e.Key, type);
if (setknowncustomtypes) {
type = General.Map.Options.GetUniversalFieldType(elementname, e.Key, type);
// Make custom field
element.Fields[e.Key] = new UniValue(type, e.Value);
//mxd. Check type
object value = e.Value;
// Let's be kind and cast any int to a float if needed
if (type == (int)UniversalType.Float && e.Value.GetType() == typeof(int)) {
value = (float)(int)e.Value;
} else if (!e.IsValidType(e.Value.GetType())) {
General.ErrorLogger.Add(ErrorType.Warning, element.ToString() + ": the value of entry '" + e.Key + "' is of incompatible type (expected " + e.GetType().Name + ", but got " + e.Value.GetType().Name + "). If you save the map, this value will be ignored.");
continue;
}
// Make custom field
element.Fields[e.Key] = new UniValue(type, value);
} else {
// Determine default type
if (e.Value.GetType() == typeof(int)) type = (int)UniversalType.Integer;
else if (e.Value.GetType() == typeof(float)) type = (int)UniversalType.Float;
else if (e.Value.GetType() == typeof(bool)) type = (int)UniversalType.Boolean;
else if (e.Value.GetType() == typeof(string)) type = (int)UniversalType.String;
// Make custom field
element.Fields[e.Key] = new UniValue(type, e.Value);
}
}
}
}