From dcb7a44aa27c463ce737325888230d42c5663758 Mon Sep 17 00:00:00 2001 From: Pavlos Touboulidis Date: Sat, 12 Jul 2014 04:46:45 +0300 Subject: [PATCH] Use available TypeConverters to serialize and deserialize types --- OpenRA.Game/FieldLoader.cs | 17 +++++++++++++++++ OpenRA.Game/FieldSaver.cs | 14 ++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/OpenRA.Game/FieldLoader.cs b/OpenRA.Game/FieldLoader.cs index 4bacabc8b1..2cb685a30f 100644 --- a/OpenRA.Game/FieldLoader.cs +++ b/OpenRA.Game/FieldLoader.cs @@ -10,6 +10,7 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Drawing; using System.Globalization; using System.Linq; @@ -374,6 +375,22 @@ namespace OpenRA return InvalidValueAction(value, fieldType, fieldName); } + else + { + var conv = TypeDescriptor.GetConverter(fieldType); + if (conv.CanConvertFrom(typeof(string))) + { + try + { + return conv.ConvertFromInvariantString(value); + } + catch + { + return InvalidValueAction(value, fieldType, fieldName); + } + } + } + UnknownFieldAction("[Type] {0}".F(value), fieldType); return null; } diff --git a/OpenRA.Game/FieldSaver.cs b/OpenRA.Game/FieldSaver.cs index 3c401870c7..50da9af1c7 100644 --- a/OpenRA.Game/FieldSaver.cs +++ b/OpenRA.Game/FieldSaver.cs @@ -10,6 +10,7 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Drawing; using System.Globalization; using System.Linq; @@ -88,6 +89,19 @@ namespace OpenRA if (t == typeof(DateTime)) return ((DateTime)v).ToString("yyyy-MM-dd HH-mm-ss", CultureInfo.InvariantCulture); + // Try the TypeConverter + var conv = TypeDescriptor.GetConverter(t); + if (conv.CanConvertTo(typeof(string))) + { + try + { + return conv.ConvertToInvariantString(v); + } + catch + { + } + } + return v.ToString(); }