예제 #1
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);
        }
예제 #2
0
        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()));
        }
예제 #3
0
        public static MiniYaml Save(object o, bool includePrivateByDefault = false)
        {
            var    nodes = new List <MiniYamlNode>();
            string root  = null;

            foreach (var info in FieldLoader.GetTypeLoadInfo(o.GetType(), includePrivateByDefault))
            {
                if (info.Attribute.FromYamlKey)
                {
                    root = FormatValue(o, info.Field);
                }
                else
                {
                    nodes.Add(new MiniYamlNode(info.YamlName, FormatValue(o, info.Field)));
                }
            }

            return(new MiniYaml(root, nodes));
        }