示例#1
0
        public void Save(string toPath)
        {
            MapFormat = 6;

            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("Options", FieldSaver.SaveDifferences(Options, new MapOptions())));

            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, RuleDefinitions));
            root.Add(new MiniYamlNode("Sequences", null, SequenceDefinitions));
            root.Add(new MiniYamlNode("VoxelSequences", null, VoxelSequenceDefinitions));
            root.Add(new MiniYamlNode("Weapons", null, WeaponDefinitions));
            root.Add(new MiniYamlNode("Voices", null, VoiceDefinitions));
            root.Add(new MiniYamlNode("Notifications", null, NotificationDefinitions));
            root.Add(new MiniYamlNode("Translations", null, TranslationDefinitions));

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

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

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

            // Add any custom assets
            if (Container != null)
            {
                foreach (var file in Container.AllFileNames())
                {
                    if (file == "map.bin" || file == "map.yaml")
                    {
                        continue;
                    }

                    entries.Add(file, Container.GetContent(file).ReadAllBytes());
                }
            }

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

                // Create a new map package
                Container = GlobalFileSystem.CreatePackage(Path, int.MaxValue, entries);
            }

            // Update existing package
            Container.Write(entries);
        }