示例#1
0
        public static async Task SetValues(ConfigValues values)
        {
            EnsureInitialized();
            _configValues = values;
            await Save(_filePath);

            Reload();
        }
示例#2
0
        public static void Reload()
        {
            string filePath = Properties.Settings.Default.ConfigPath;

            if (Path.GetFileName(filePath).ToLower() == "config.json" &&
                File.Exists(Path.Combine(Path.GetDirectoryName(filePath), "JsonViewer.exe")) &&
                filePath.ToLower().Contains(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).ToLower()))
            {
                filePath = null;
            }

            if (!File.Exists(filePath))
            {
                filePath = null;
            }

            if (LoadConfig(filePath))
            {
                PropertyChanged?.Invoke("Values");
            }
            else
            {
                MessageBoxResult dr = MessageBox.Show("Unable to load your config.  Do you want to use the default?", "Default config?", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (dr == MessageBoxResult.Yes)
                {
                    Properties.Settings.Default.ConfigPath = string.Empty;
                    Properties.Settings.Default.Save();
                    Reload();
                }
                else
                {
                    _configValues = new ConfigValues();
                    _filePath     = string.Empty;
                    PropertyChanged?.Invoke("Values");
                    PropertyChanged?.Invoke("FilePath");
                }
            }
        }
示例#3
0
        public static bool LoadConfig(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                filePath = GetDefaultConfigPath();
            }

            StringBuilder sb = new StringBuilder();

            sb.Append("Error processing config:\r\n");

            List <string>          exceptionMessages      = new List <string>();
            JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings
            {
                Error = new EventHandler <Newtonsoft.Json.Serialization.ErrorEventArgs>(
                    (obj, args) =>
                {
                    exceptionMessages.Add(args.ErrorContext.Error.ToString());
                    args.ErrorContext.Handled = true;
                }),
                CheckAdditionalContent = true,
                MaxDepth = 100,
                MissingMemberHandling = MissingMemberHandling.Error
            };

            string errorMessage = string.Empty;

            try
            {
                ConfigValues values = JsonConvert.DeserializeObject <ConfigValues>(File.ReadAllText(filePath), jsonSerializerSettings);

                if (exceptionMessages.Count > 0)
                {
                    errorMessage  = "Exceptions hit while processing config:\r\n\r\n";
                    errorMessage += string.Join("\r\n\r\n-------------------------\r\n\r\n", exceptionMessages.ToArray());
                }
                else
                {
                    if (Properties.Settings.Default.ConfigPath != filePath)
                    {
                        Properties.Settings.Default.ConfigPath = filePath;
                        Properties.Settings.Default.Save();
                    }

                    _filePath     = filePath;
                    _configValues = values;
                    return(true);
                }
            }
            catch
            {
                if (!string.IsNullOrEmpty(filePath))
                {
                    errorMessage = "Unable to process the config file :\r\n" + filePath;
                }
            }

            FileLogger.Assert(!string.IsNullOrEmpty(errorMessage));
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MessageBox.Show(errorMessage, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            return(false);
        }
示例#4
0
 public EditableRuleSet(ConfigValues configValues)
 {
     _configValues = configValues;
 }