Exemple #1
0
        /// <summary>
        /// Writes the default config file. If overwrite = true, it resets the old one.
        /// </summary>
        /// <param name="overwrite"></param>
        public static void ResetConfigs(bool overwrite = false)
        {
            if (overwrite && File.Exists(IOPathHelper.GetSettingsFile()))
            {
                File.Delete(IOPathHelper.GetSettingsFile());
            }

            if (!File.Exists(IOPathHelper.GetSettingsFile()))
            {
                // Write the default settings file.
                IOPathHelper.CreateDirectory(IOPathHelper.GetConfigsDirectory());

                var xmlWriterSettings = new XmlWriterSettings {
                    NewLineOnAttributes = true, Indent = true
                };
                using (XmlWriter writer = XmlWriter.Create(IOPathHelper.GetSettingsFile(), xmlWriterSettings))
                {
                    writer.WriteStartDocument();
                    writer.WriteStartElement("Settings");

                    foreach (var pair in _defaultSettings)
                    {
                        writer.WriteElementString(pair.Key, pair.Value);
                    }

                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Takes in a setting name plus value and updates an existing one.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public static void SaveSetting(string name, string value)
        {
            // Do something later if the setting is not valid. An empty value is fine.
            if (value != "Void" && !ValidateSetting(name, value))
            {
                Communication.InformUser("An irregularity was detected while trying to save your configuration.");
                return;
            }

            var setDoc         = XDocument.Load(IOPathHelper.GetSettingsFile());
            var setDocElements = setDoc.Root.Elements();

            foreach (var element in setDocElements)
            {
                if (element.Name == name)
                {
                    element.Value = value;
                }
            }

            setDoc.Save(IOPathHelper.GetSettingsFile());
        }
Exemple #3
0
        /// <summary>
        /// Gets the value of the given setting name. Returns defautl(T) if setting has no value right now => Void.
        /// Throws <see cref="FileLoadException"/> if a setting doesnt exist.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static T GetSettingValue <T>(string name)
        {
            if (!_wellKnownSettings.ContainsKey(name))
            {
                throw new FileLoadException($"The given setting does not exist: {name}");
            }

            var setDoc         = XDocument.Load(IOPathHelper.GetSettingsFile());
            var setDocElements = setDoc.Root.Elements();

            foreach (var element in setDocElements)
            {
                if (element.Name == name)
                {
                    if (element.Value == "Void")
                    {
                        return(default(T));
                    }

                    if (ValidateSetting(name, element.Value))
                    {
                        // Cast the value to the right type that is being given by the settingType
                        var normalString = element.Value.ToString();
                        return((T)Convert.ChangeType(normalString, typeof(T)));
                    }
                    else
                    {
                        Communication.InformUser
                            ("An irregularity was detected in your configuration file. " +
                            "If you are not aware of ever having changed that file directly, the Guardian suggests starting a scan within the fortress and your Antivirus-Software, " +
                            $"for it is possible that something malicious crawled through your files. The {TermHelper.GetDatabaseTerm()} however remains save.");
                    }
                }
            }
            return(default(T));
        }