FormatValue() public static method

public static FormatValue ( object v ) : string
v object
return string
Beispiel #1
0
        public void Serialize(Map map, List <MiniYamlNode> nodes)
        {
            var value = field != null?field.GetValue(map) : property.GetValue(map, null);

            if (type == Type.NodeList)
            {
                var listValue = (List <MiniYamlNode>)value;
                if (required || listValue.Any())
                {
                    nodes.Add(new MiniYamlNode(key, null, listValue));
                }
            }
            else if (type == Type.MiniYaml)
            {
                var yamlValue = (MiniYaml)value;
                if (required || (yamlValue != null && (yamlValue.Value != null || yamlValue.Nodes.Any())))
                {
                    nodes.Add(new MiniYamlNode(key, yamlValue));
                }
            }
            else
            {
                var formattedValue = FieldSaver.FormatValue(value);
                if (required || formattedValue != ignoreIfValue)
                {
                    nodes.Add(new MiniYamlNode(key, formattedValue));
                }
            }
        }
Beispiel #2
0
        public void Save(string filepath)
        {
            var root = new List <MiniYamlNode>();
            var gen  = new List <MiniYamlNode>();

            foreach (var field in Fields)
            {
                var f = this.GetType().GetField(field);
                if (f.GetValue(this) == null)
                {
                    continue;
                }

                gen.Add(new MiniYamlNode(field, FieldSaver.FormatValue(this, f)));
            }

            root.Add(new MiniYamlNode("General", null, gen));

            root.Add(new MiniYamlNode("Terrain", null,
                                      Terrain.Select(t => new MiniYamlNode("TerrainType@{0}".F(t.Value.Type), t.Value.Save())).ToList()));

            root.Add(new MiniYamlNode("Templates", null,
                                      Templates.Select(t => new MiniYamlNode("Template@{0}".F(t.Value.Id), t.Value.Save())).ToList()));
            root.WriteToFile(filepath);
        }
Beispiel #3
0
        public void Save()
        {
            foreach (var kv in Sections)
            {
                var sectionYaml = yamlCache.FirstOrDefault(x => x.Key == kv.Key);
                if (sectionYaml == null)
                {
                    sectionYaml = new MiniYamlNode(kv.Key, new MiniYaml(""));
                    yamlCache.Add(sectionYaml);
                }

                var defaultValues = Activator.CreateInstance(kv.Value.GetType());
                var fields        = FieldLoader.GetTypeLoadInfo(kv.Value.GetType());
                foreach (var fli in fields)
                {
                    var serialized        = FieldSaver.FormatValue(kv.Value, fli.Field);
                    var defaultSerialized = FieldSaver.FormatValue(defaultValues, fli.Field);

                    // Fields with their default value are not saved in the settings yaml
                    // Make sure that we erase any previously defined custom values
                    if (serialized == defaultSerialized)
                    {
                        sectionYaml.Value.Nodes.RemoveAll(n => n.Key == fli.YamlName);
                    }
                    else
                    {
                        // Update or add the custom value
                        var fieldYaml = sectionYaml.Value.Nodes.FirstOrDefault(n => n.Key == fli.YamlName);
                        if (fieldYaml != null)
                        {
                            fieldYaml.Value.Value = serialized;
                        }
                        else
                        {
                            sectionYaml.Value.Nodes.Add(new MiniYamlNode(fli.YamlName, new MiniYaml(serialized)));
                        }
                    }
                }
            }

            var keysYaml = yamlCache.FirstOrDefault(x => x.Key == "Keys");

            if (keysYaml == null)
            {
                keysYaml = new MiniYamlNode("Keys", new MiniYaml(""));
                yamlCache.Add(keysYaml);
            }

            keysYaml.Value.Nodes.Clear();
            foreach (var kv in Keys)
            {
                keysYaml.Value.Nodes.Add(new MiniYamlNode(kv.Key, FieldSaver.FormatValue(kv.Value)));
            }

            yamlCache.WriteToFile(settingsFile);
        }
Beispiel #4
0
        public MiniYaml Save()
        {
            var root = new List <MiniYamlNode>();

            foreach (var field in Fields)
            {
                FieldInfo f = this.GetType().GetField(field);
                if (f.GetValue(this) == null)
                {
                    continue;
                }

                root.Add(new MiniYamlNode(field, FieldSaver.FormatValue(this, f)));
            }

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

            return(new MiniYaml(null, root));
        }
Beispiel #5
0
        public MiniYaml Save(TileSet tileSet)
        {
            var root = new List <MiniYamlNode>();

            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("Tiles", null,
                                      tiles.Select((terrainTypeIndex, templateIndex) => new MiniYamlNode(templateIndex.ToString(), tileSet[terrainTypeIndex].Type)).ToList()));

            return(new MiniYaml(null, root));
        }
Beispiel #6
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);
        }
Beispiel #7
0
 public override MiniYaml Save()
 {
     return(new MiniYaml(FieldSaver.FormatValue(value)));
 }
Beispiel #8
0
        public void Save(string toPath)
        {
            MapFormat = 7;

            var root   = new List <MiniYamlNode>();
            var fields = new[]
            {
                "MapFormat",
                "RequiresMod",
                "Title",
                "Description",
                "Author",
                "Tileset",
                "MapSize",
                "Bounds",
                "Visibility",
                "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("Videos", FieldSaver.SaveDifferences(Videos, new MapVideos())));

            root.Add(new MiniYamlNode("Options", FieldSaver.SaveDifferences(Options, new MapOptions())));

            root.Add(new MiniYamlNode("Players", null, PlayerDefinitions));

            root.Add(new MiniYamlNode("Actors", null, ActorDefinitions));
            root.Add(new MiniYamlNode("Smudges", null, SmudgeDefinitions));
            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("Music", null, MusicDefinitions));
            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 || Container == null)
            {
                Path = toPath;

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

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

            // Update UID to match the newly saved data
            Uid = ComputeHash();
        }