OpenRA.FieldSaver.SaveDifferences C# (CSharp) Method

SaveDifferences() public static method

public static SaveDifferences ( object o, object from, bool includePrivateByDefault = false ) : MiniYaml
o object
from object
includePrivateByDefault bool
return MiniYaml
        public static MiniYaml SaveDifferences(object o, object from, bool includePrivateByDefault = false)
        {
            if (o.GetType() != from.GetType())
                throw new InvalidOperationException("FieldLoader: can't diff objects of different types");

            var fields = FieldLoader.GetTypeLoadInfo(o.GetType(), includePrivateByDefault)
                .Where(info => FormatValue(o, info.Field) != FormatValue(from, info.Field));

            return new MiniYaml(
                null,
                fields.Select(info => new MiniYamlNode(info.YamlName, FormatValue(o, info.Field))).ToList());
        }

Usage Example

Example #1
0
File: Map.cs Project: test71/OpenRA
        public void Save(string toPath)
        {
            MapFormat = 5;

            var root   = new List <MiniYamlNode>();
            var fields = new []
            {
                "Selectable",
                "MapFormat",
                "RequiresMod",
                "Title",
                "Description",
                "Author",
                "Tileset",
                "MapSize",
                "Bounds",
                "UseAsShellmap",
                "Type",
            };

            foreach (var field in fields)
            {
                var f = this.GetType().GetField(field);
                if (f.GetValue(this) == null)
                {
                    continue;
                }
                root.Add(new MiniYamlNode(field, FieldSaver.FormatValue(this, f)));
            }

            root.Add(new MiniYamlNode("Players", null,
                                      Players.Select(p => new MiniYamlNode(
                                                         "PlayerReference@{0}".F(p.Key),
                                                         FieldSaver.SaveDifferences(p.Value, new PlayerReference()))).ToList()));

            root.Add(new MiniYamlNode("Actors", null,
                                      Actors.Value.Select(x => new MiniYamlNode(
                                                              x.Key,
                                                              x.Value.Save())).ToList()));

            root.Add(new MiniYamlNode("Smudges", MiniYaml.FromList <SmudgeReference>(Smudges.Value)));
            root.Add(new MiniYamlNode("Rules", null, Rules));
            root.Add(new MiniYamlNode("Sequences", null, Sequences));
            root.Add(new MiniYamlNode("Weapons", null, Weapons));
            root.Add(new MiniYamlNode("Voices", null, Voices));

            var entries = new Dictionary <string, byte[]>();

            entries.Add("map.bin", SaveBinaryData());
            var s = root.WriteToString();

            entries.Add("map.yaml", Encoding.UTF8.GetBytes(s));

            // Saving the map to a new location
            if (toPath != Path)
            {
                Path = toPath;

                // Create a new map package
                // TODO: Add other files (custom assets) to the entries list
                Container = FileSystem.CreatePackage(Path, int.MaxValue, entries);
            }

            // Update existing package
            Container.Write(entries);
        }
All Usage Examples Of OpenRA.FieldSaver::SaveDifferences