Esempio n. 1
0
        public async Task Reset()
        {
            log.Info("Writing default settings");

            IEnumerable <PropertyInfo> properties = typeof(Settings).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Where(prop => prop.CustomAttributes.Any());

            foreach (var propertyInfo in properties)
            {
                if (propertyInfo.PropertyType.Name == "KeyBindings")
                {
                    continue;
                }

                IniSection attribute = (IniSection)propertyInfo.GetCustomAttribute(typeof(IniSection), false);

                settingsIniFile.Write(propertyInfo.Name, attribute.DefaultValue.ToString(), attribute.Section);
            }

            log.Info("Resetting keybindings");
            Bindings.ResetKeybindings(settingsIniFile);
        }
Esempio n. 2
0
        public async Task <bool> ReadSettings()
        {
            bool valid = AllCategoriesPresent;

            IEnumerable <PropertyInfo> properties = typeof(Settings).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Where(prop => prop.CustomAttributes.Any());

            log.Info("Reading keybindings");
            Bindings.ReadKeybindings(settingsIniFile);

            // If valid is true here all sections must be present
            if (valid)
            {
                foreach (var propertyInfo in properties)
                {
                    IniSection attribute = (IniSection)propertyInfo.GetCustomAttribute(typeof(IniSection), false);

                    log.Debug($"Reading settings for {propertyInfo.Name}");

                    string raw = settingsIniFile.Read(propertyInfo.Name, attribute.Section);

                    if (propertyInfo.PropertyType.Name == "KeyBindings")
                    {
                        continue;
                    }

                    switch (attribute.Type.Name)
                    {
                    case "Single":
                    {
                        float value;

                        if (!float.TryParse(raw, out value))
                        {
                            log.Warn($"Invalid value '{raw}' for {propertyInfo.Name}");

                            valid = false;
                            break;
                        }

                        if (!(value >= (float)attribute.MinimumValue && value <= (float)attribute.MaximumValue))
                        {
                            valid = false;
                        }
                        else
                        {
                            propertyInfo.SetValue(this, value);
                        }


                        break;
                    }

                    case "Boolean":
                    {
                        bool value;

                        if (!bool.TryParse(raw, out value))
                        {
                            log.Warn($"Invalid value '{raw}' for {propertyInfo.Name}");
                            valid = false;
                        }
                        else
                        {
                            propertyInfo.SetValue(this, value);
                        }

                        break;
                    }

                    default:
                    {
                        valid = false;
                        break;
                    }
                    }

                    if (!valid)
                    {
                        break;
                    }
                }
            }

            if (!valid && MessageBox.Show("Invalid settings file. Press OK to reset to default settings.",
                                          "Settings Invalid",
                                          MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
            {
                await Reset();

                valid = true;
            }

            return(valid);
        }