Beispiel #1
0
        // IO
        public static void Load()
        {
            var path = SavePath;

            var settings = new IniSettings();

            settings.Load(path);

            foreach (var prop in Properties.Values)
            {
                if (prop.PropertyType == typeof(string))
                {
                    prop.SetValue(null, settings.GetString(prop.Name, (string)prop.GetValue(null)));
                }
                else if (prop.PropertyType == typeof(int))
                {
                    prop.SetValue(null, settings.GetInt(prop.Name, (int)prop.GetValue(null)));
                }
                else if (prop.PropertyType == typeof(double))
                {
                    prop.SetValue(null, settings.GetDouble(prop.Name, (double)prop.GetValue(null)));
                }
                else if (prop.PropertyType == typeof(bool))
                {
                    prop.SetValue(null, settings.GetBool(prop.Name, (bool)prop.GetValue(null)));
                }
                else if (prop.PropertyType == typeof(string[]))
                {
                    string[] vals;
                    if (settings.TryGetStrings(prop.Name, out vals))
                    {
                        prop.SetValue(null, vals);
                    }
                }
                else if (prop.PropertyType == typeof(ConcurrentDictionary <string, object>))
                {
                    var dict = (ConcurrentDictionary <string, object>)prop.GetValue(null);

                    dict.Clear();

                    string[] vals;
                    if (settings.TryGetStrings(prop.Name, out vals))
                    {
                        foreach (var s in vals)
                        {
                            dict[s] = null;
                        }
                    }
                }
                else if (prop.PropertyType == typeof(List <int>))
                {
                    int xd;
                    prop.SetValue(null, settings.GetString(prop.Name, "").Split(',').Select(x => x.Trim())
                                  .Where(x => int.TryParse(x, out xd)).Select(x => int.Parse(x)).ToList());
                }
            }
        }
Beispiel #2
0
        public static void Save()
        {
            var path = SavePath;

            var settings = new IniSettings();

            foreach (var prop in Properties.Values)
            {
                if (prop.PropertyType == typeof(string))
                {
                    settings.Set(prop.Name, (string)prop.GetValue(null));
                }
                else if (prop.PropertyType == typeof(int))
                {
                    settings.Set(prop.Name, (int)prop.GetValue(null));
                }
                else if (prop.PropertyType == typeof(double))
                {
                    settings.Set(prop.Name, (double)prop.GetValue(null));
                }
                else if (prop.PropertyType == typeof(bool))
                {
                    settings.Set(prop.Name, (bool)prop.GetValue(null));
                }
                else if (prop.PropertyType == typeof(string[]))
                {
                    settings.Set(prop.Name, (string[])prop.GetValue(null));
                }
                else if (prop.PropertyType == typeof(ConcurrentDictionary <string, object>))
                {
                    settings.Set(prop.Name, ((ConcurrentDictionary <string, object>)prop.GetValue(null)).Keys);
                }
                else if (prop.PropertyType == typeof(ConcurrentDictionary <string, string>))
                {
                    settings.Set(prop.Name, JsonConvert.SerializeObject(((ConcurrentDictionary <string, string>)prop.GetValue(null))));
                }
                else if (prop.PropertyType == typeof(List <int>))
                {
                    settings.Set(prop.Name, string.Join(",", (List <int>)prop.GetValue(null)));
                }
            }

            settings.Save(path);
        }
Beispiel #3
0
        // IO
        public static void Load()
        {
            var path = SavePath;

            var settings = new IniSettings();

            settings.Load(path);
            var parser = new JsonParser();


            foreach (var prop in Properties.Values)
            {
                if (prop.PropertyType == typeof(string))
                {
                    prop.SetValue(null, settings.GetString(prop.Name, (string)prop.GetValue(null)));
                }
                else if (prop.PropertyType == typeof(int))
                {
                    prop.SetValue(null, settings.GetInt(prop.Name, (int)prop.GetValue(null)));
                }
                else if (prop.PropertyType == typeof(double))
                {
                    prop.SetValue(null, settings.GetDouble(prop.Name, (double)prop.GetValue(null)));
                }
                else if (prop.PropertyType == typeof(bool))
                {
                    prop.SetValue(null, settings.GetBool(prop.Name, (bool)prop.GetValue(null)));
                }
                else if (prop.PropertyType == typeof(string[]))
                {
                    string[] vals;
                    if (settings.TryGetStrings(prop.Name, out vals))
                    {
                        prop.SetValue(null, vals);
                    }
                }
                else if (prop.PropertyType == typeof(ConcurrentDictionary <string, object>))
                {
                    var dict = (ConcurrentDictionary <string, object>)prop.GetValue(null);

                    dict.Clear();

                    string[] vals;
                    if (settings.TryGetStrings(prop.Name, out vals))
                    {
                        foreach (var s in vals)
                        {
                            dict[s] = null;
                        }
                    }
                }
                else if (prop.PropertyType == typeof(ConcurrentDictionary <string, string>))
                {
                    var dict = (ConcurrentDictionary <string, string>)prop.GetValue(null);

                    dict.Clear();

                    string val;
                    val = settings.GetString(prop.Name, "");
                    if (!String.IsNullOrEmpty(val))
                    {
                        try {
                            var newdict = JsonConvert.DeserializeObject <ConcurrentDictionary <string, string> >(val);
                            if (newdict != null)
                            {
                                prop.SetValue(null, newdict);
                            }
                        } catch {}
                    }
                }
                else if (prop.PropertyType == typeof(List <int>))
                {
                    int xd;
                    prop.SetValue(null, settings.GetString(prop.Name, "").Split(',').Select(x => x.Trim())
                                  .Where(x => int.TryParse(x, out xd)).Select(x => int.Parse(x)).ToList());
                }
            }
        }