From 7a952a2e5e337ad5807f13f9ec42bec53057e616 Mon Sep 17 00:00:00 2001
From: MaxED <j.maxed@gmail.com>
Date: Mon, 19 Nov 2012 09:44:05 +0000
Subject: [PATCH] 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.

---
 Source/Core/IO/UniversalEntry.cs        |  5 ++++
 Source/Core/IO/UniversalStreamReader.cs | 38 ++++++++++++++++++-------
 2 files changed, 32 insertions(+), 11 deletions(-)

diff --git a/Source/Core/IO/UniversalEntry.cs b/Source/Core/IO/UniversalEntry.cs
index 2e35a3f69..8e925db16 100644
--- a/Source/Core/IO/UniversalEntry.cs
+++ b/Source/Core/IO/UniversalEntry.cs
@@ -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
 	}
diff --git a/Source/Core/IO/UniversalStreamReader.cs b/Source/Core/IO/UniversalStreamReader.cs
index 2eb6cf05c..665a3a2a8 100644
--- a/Source/Core/IO/UniversalStreamReader.cs
+++ b/Source/Core/IO/UniversalStreamReader.cs
@@ -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);
-
-					// Make custom field
-					element.Fields[e.Key] = new UniValue(type, e.Value);
+                    if (setknowncustomtypes) {
+                        type = General.Map.Options.GetUniversalFieldType(elementname, e.Key, type);
+
+                        //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);
+                    }
 				}
 			}
 		}
-- 
GitLab