diff --git a/OpenRA.FileFormats/FieldLoader.cs b/OpenRA.FileFormats/FieldLoader.cs index 6698a60d89..0b1eeacaa5 100755 --- a/OpenRA.FileFormats/FieldLoader.cs +++ b/OpenRA.FileFormats/FieldLoader.cs @@ -1,6 +1,6 @@ #region Copyright & License Information /* - * Copyright 2007-2011 The OpenRA Developers (see AUTHORS) + * Copyright 2007-2013 The OpenRA Developers (see AUTHORS) * This file is part of OpenRA, which is free software. It is made * available to you under the terms of the GNU General Public License * as published by the Free Software Foundation. For more information, @@ -344,80 +344,6 @@ namespace OpenRA.FileFormats } } - public static class FieldSaver - { - public static MiniYaml Save(object o) - { - var nodes = new List(); - string root = null; - - foreach (var f in o.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance)) - { - if (f.HasAttribute()) - root = FormatValue(o, f); - else - nodes.Add(new MiniYamlNode(f.Name, FormatValue(o, f))); - } - - return new MiniYaml(root, nodes); - } - - public static MiniYaml SaveDifferences(object o, object from) - { - if (o.GetType() != from.GetType()) - throw new InvalidOperationException("FieldLoader: can't diff objects of different types"); - - var fields = o.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance) - .Where(f => FormatValue(o, f) != FormatValue(from, f)); - - return new MiniYaml(null, fields.Select(f => new MiniYamlNode(f.Name, FormatValue(o, f))).ToList()); - } - - public static MiniYamlNode SaveField(object o, string field) - { - return new MiniYamlNode(field, FieldSaver.FormatValue(o, o.GetType().GetField(field))); - } - - public static string FormatValue(object v, Type t) - { - if (v == null) - return ""; - - // Color.ToString() does the wrong thing; force it to format as an array - if (t == typeof(Color)) - { - var c = (Color)v; - return "{0},{1},{2},{3}".F(((int)c.A).Clamp(0, 255), - ((int)c.R).Clamp(0, 255), - ((int)c.G).Clamp(0, 255), - ((int)c.B).Clamp(0, 255)); - } - - // Don't save floats in settings.yaml using country-specific decimal separators which can be misunderstood as group seperators. - if (t == typeof(float)) - return ((float)v).ToString(CultureInfo.InvariantCulture); - - if (t == typeof(Rectangle)) - { - var r = (Rectangle)v; - return "{0},{1},{2},{3}".F(r.X, r.Y, r.Width, r.Height); - } - - if (t.IsArray) - { - var elems = ((Array)v).OfType(); - return elems.JoinWith(","); - } - - return v.ToString(); - } - - public static string FormatValue(object o, FieldInfo f) - { - return FormatValue(f.GetValue(o), f.FieldType); - } - } - public class FieldFromYamlKeyAttribute : Attribute { } // mirrors DescriptionAttribute from System.ComponentModel but we dont want to have to use that everywhere. diff --git a/OpenRA.FileFormats/FieldSaver.cs b/OpenRA.FileFormats/FieldSaver.cs new file mode 100644 index 0000000000..65208da2b5 --- /dev/null +++ b/OpenRA.FileFormats/FieldSaver.cs @@ -0,0 +1,93 @@ +#region Copyright & License Information +/* + * Copyright 2007-2013 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see COPYING. + */ +#endregion + +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Globalization; +using System.Linq; +using System.Reflection; + +namespace OpenRA.FileFormats +{ + public static class FieldSaver + { + public static MiniYaml Save(object o) + { + var nodes = new List(); + string root = null; + + foreach (var f in o.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance)) + { + if (f.HasAttribute()) + root = FormatValue(o, f); + else + nodes.Add(new MiniYamlNode(f.Name, FormatValue(o, f))); + } + + return new MiniYaml(root, nodes); + } + + public static MiniYaml SaveDifferences(object o, object from) + { + if (o.GetType() != from.GetType()) + throw new InvalidOperationException("FieldLoader: can't diff objects of different types"); + + var fields = o.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance) + .Where(f => FormatValue(o, f) != FormatValue(from, f)); + + return new MiniYaml(null, fields.Select(f => new MiniYamlNode(f.Name, FormatValue(o, f))).ToList()); + } + + public static MiniYamlNode SaveField(object o, string field) + { + return new MiniYamlNode(field, FieldSaver.FormatValue(o, o.GetType().GetField(field))); + } + + public static string FormatValue(object v, Type t) + { + if (v == null) + return ""; + + // Color.ToString() does the wrong thing; force it to format as an array + if (t == typeof(Color)) + { + var c = (Color)v; + return "{0},{1},{2},{3}".F(((int)c.A).Clamp(0, 255), + ((int)c.R).Clamp(0, 255), + ((int)c.G).Clamp(0, 255), + ((int)c.B).Clamp(0, 255)); + } + + // Don't save floats in settings.yaml using country-specific decimal separators which can be misunderstood as group seperators. + if (t == typeof(float)) + return ((float)v).ToString(CultureInfo.InvariantCulture); + + if (t == typeof(Rectangle)) + { + var r = (Rectangle)v; + return "{0},{1},{2},{3}".F(r.X, r.Y, r.Width, r.Height); + } + + if (t.IsArray) + { + var elems = ((Array)v).OfType(); + return elems.JoinWith(","); + } + + return v.ToString(); + } + + public static string FormatValue(object o, FieldInfo f) + { + return FormatValue(f.GetValue(o), f.FieldType); + } + } +} diff --git a/OpenRA.FileFormats/OpenRA.FileFormats.csproj b/OpenRA.FileFormats/OpenRA.FileFormats.csproj index 95e2f659fa..09c4962242 100644 --- a/OpenRA.FileFormats/OpenRA.FileFormats.csproj +++ b/OpenRA.FileFormats/OpenRA.FileFormats.csproj @@ -80,6 +80,7 @@ +