コード例 #1
0
        /// <summary>
        /// Saves the current settings into CONFIG_PATH.
        /// </summary>
        public static void Save()
        {
            List <String> lines = new List <String> ();

            SerializableSettingAttribute.GetSerializableFields(typeof(Settings)).All(property => {
                lines.Add(property.Name + "===" + property.GetValue(null));
                return(true);
            });
            File.WriteAllLines(CONFIG_PATH.ToString(), lines);
        }
コード例 #2
0
        /// <summary>
        /// Loads the settings from the defined constant value CONFIG_PATH.
        /// </summary>
        public static void Load()
        {
            Dictionary <string, string> settings = new Dictionary <string, string>();

            if (!CONFIG_PATH.Exists)
            {
                CONFIG_PATH.Create().Close();
            }
            if (!TEMP_PATH.Exists)
            {
                TEMP_PATH.Create();
            }
            string[] configLines = File.ReadAllLines(CONFIG_PATH.ToString());
            ReadConfigLines(configLines, (k, v) => settings.Add(k, v));

            IEnumerable <FieldInfo> fieldsToLoad = SerializableSettingAttribute.GetSerializableFields(typeof(Settings));

            fieldsToLoad.All(field => {
                if (field.FieldType == typeof(string))
                {
                    if (settings.ContainsKey(field.Name))
                    {
                        field.SetValue(null, settings[field.Name]);
                    }
                    else
                    {
                        field.SetValue(null, (string)field.GetCustomAttribute <SerializableSettingAttribute>(true).value);
                    }
                }
                else if (field.FieldType == typeof(bool))
                {
                    if (settings.ContainsKey(field.Name))
                    {
                        field.SetValue(null, settings[field.Name].ToLower() == "true");
                    }
                    else
                    {
                        field.SetValue(null, (bool)field.GetCustomAttribute <SerializableSettingAttribute>(true).value);
                    }
                }
                else if (field.FieldType == typeof(int))
                {
                    if (settings.ContainsKey(field.Name))
                    {
                        field.SetValue(null, int.Parse(settings[field.Name]));
                    }
                    else
                    {
                        field.SetValue(null, (int)field.GetCustomAttribute <SerializableSettingAttribute>(true).value);
                    }
                }
                else if (field.FieldType == typeof(float))
                {
                    if (settings.ContainsKey(field.Name))
                    {
                        field.SetValue(null, float.Parse(settings[field.Name]));
                    }
                    else
                    {
                        field.SetValue(null, (float)field.GetCustomAttribute <SerializableSettingAttribute>(true).value);
                    }
                }
                else
                {
                    Debug.WriteLine(string.Format("Setting {0} isn't of a supported type.", field.Name));
                }
                Debug.WriteLine(field.Name + ": " + field.GetValue(null));
                return(true);
            });
        }