示例#1
0
 /// <summary>
 /// Access one of the existing settings. If the setting has not been added yet, null is returned.
 /// If the setting exists but has a different type than T, an exception is thrown.
 /// New settings should be added with <see cref="AddSetting{T}(ConfigDefinition,T,ConfigDescription)"/>.
 /// </summary>
 /// <typeparam name="T">Type of the value contained in this setting.</typeparam>
 /// <param name="configDefinition">Section and Key of the setting.</param>
 public ConfigEntry <T> GetSetting <T>(ConfigDefinition configDefinition)
 {
     lock (_ioLock)
     {
         Entries.TryGetValue(configDefinition, out var entry);
         return((ConfigEntry <T>)entry);
     }
 }
示例#2
0
        /// <summary>
        /// Reloads the config from disk. Unsaved changes are lost.
        /// </summary>
        public void Reload()
        {
            lock (_ioLock)
            {
                HomelessEntries.Clear();

                try
                {
                    _disableSaving = true;

                    string currentSection = string.Empty;

                    foreach (string rawLine in File.ReadAllLines(ConfigFilePath))
                    {
                        string line = rawLine.Trim();

                        if (line.StartsWith("#"))                         //comment
                        {
                            continue;
                        }

                        if (line.StartsWith("[") && line.EndsWith("]"))                         //section
                        {
                            currentSection = line.Substring(1, line.Length - 2);
                            continue;
                        }

                        string[] split = line.Split('=');                         //actual config line
                        if (split.Length != 2)
                        {
                            continue;                             //empty/invalid line
                        }
                        string currentKey   = split[0].Trim();
                        string currentValue = split[1].Trim();

                        var definition = new ConfigDefinition(currentSection, currentKey);

                        Entries.TryGetValue(definition, out ConfigEntryBase entry);

                        if (entry != null)
                        {
                            entry.SetSerializedValue(currentValue);
                        }
                        else
                        {
                            HomelessEntries[definition] = currentValue;
                        }
                    }
                }
                finally
                {
                    _disableSaving = false;
                }
            }

            OnConfigReloaded();
        }
示例#3
0
 public ConfigWrapper <T> Wrap <T>(string section, string key, string description = null, T defaultValue = default(T))
 {
     lock (_ioLock)
     {
         var definition = new ConfigDefinition(section, key, description);
         var setting    = GetSetting <T>(definition) ?? AddSetting(definition, defaultValue, string.IsNullOrEmpty(description) ? null : new ConfigDescription(description));
         return(new ConfigWrapper <T>(setting));
     }
 }
示例#4
0
 internal ConfigEntry(ConfigFile configFile,
                      ConfigDefinition definition,
                      T defaultValue,
                      ConfigDescription configDescription) : base(configFile, definition, typeof(T),
                                                                  defaultValue, configDescription)
 {
     configFile.SettingChanged += (sender, args) =>
     {
         if (args.ChangedSetting == this)
         {
             SettingChanged?.Invoke(sender, args);
         }
     };
 }
示例#5
0
        /// <summary>
        /// Reloads the config from disk. Unsaved changes are lost.
        /// </summary>
        public void Reload()
        {
            lock (_ioLock)
            {
                Dictionary <ConfigDefinition, string> descriptions = Cache.ToDictionary(x => x.Key, x => x.Key.Description);

                string currentSection = "";

                foreach (string rawLine in File.ReadAllLines(ConfigFilePath))
                {
                    string line = rawLine.Trim();

                    if (line.StartsWith("#"))                     //comment
                    {
                        continue;
                    }

                    if (line.StartsWith("[") && line.EndsWith("]"))                     //section
                    {
                        currentSection = line.Substring(1, line.Length - 2);
                        continue;
                    }

                    string[] split = line.Split('=');                     //actual config line
                    if (split.Length != 2)
                    {
                        continue;                         //empty/invalid line
                    }
                    string currentKey   = split[0].Trim();
                    string currentValue = split[1].Trim();

                    var definition = new ConfigDefinition(currentSection, currentKey);

                    if (descriptions.ContainsKey(definition))
                    {
                        definition.Description = descriptions[definition];
                    }

                    Cache[definition] = currentValue;
                }

                ConfigReloaded?.Invoke(this, EventArgs.Empty);
            }
        }
示例#6
0
        public ConfigWrapper <T> Wrap <T>(ConfigDefinition configDefinition, T defaultValue = default(T))
        {
            if (!Cache.ContainsKey(configDefinition))
            {
                Cache.Add(configDefinition, TomlTypeConverter.ConvertToString(defaultValue));
                Save();
            }
            else
            {
                var original = Cache.Keys.First(x => x.Equals(configDefinition));

                if (original.Description != configDefinition.Description)
                {
                    original.Description = configDefinition.Description;
                    Save();
                }
            }

            return(new ConfigWrapper <T>(this, configDefinition));
        }
示例#7
0
        /// <summary>
        /// Create a new setting. The setting is saved to drive and loaded automatically.
        /// Each definition can be used to add only one setting, trying to add a second setting will throw an exception.
        /// </summary>
        /// <typeparam name="T">Type of the value contained in this setting.</typeparam>
        /// <param name="configDefinition">Section and Key of the setting.</param>
        /// <param name="defaultValue">Value of the setting if the setting was not created yet.</param>
        /// <param name="configDescription">Description of the setting shown to the user and other metadata.</param>
        public ConfigEntry <T> AddSetting <T>(ConfigDefinition configDefinition, T defaultValue, ConfigDescription configDescription = null)
        {
            if (!TomlTypeConverter.CanConvert(typeof(T)))
            {
                throw new ArgumentException($"Type {typeof(T)} is not supported by the config system. Supported types: {string.Join(", ", TomlTypeConverter.GetSupportedTypes().Select(x => x.Name).ToArray())}");
            }

            lock (_ioLock)
            {
                if (Entries.ContainsKey(configDefinition))
                {
                    throw new ArgumentException("The setting " + configDefinition + " has already been created. Use GetSetting to get it.");
                }

                try
                {
                    _disableSaving = true;

                    var entry = new ConfigEntry <T>(this, configDefinition, defaultValue, configDescription);
                    Entries[configDefinition] = entry;

                    if (HomelessEntries.TryGetValue(configDefinition, out string homelessValue))
                    {
                        entry.SetSerializedValue(homelessValue);
                        HomelessEntries.Remove(configDefinition);
                    }

                    _disableSaving = false;
                    if (SaveOnConfigSet)
                    {
                        Save();
                    }

                    return(entry);
                }
                finally
                {
                    _disableSaving = false;
                }
            }
        }
示例#8
0
 public ConfigWrapper <T> Wrap <T>(ConfigDefinition configDefinition, T defaultValue = default(T))
 => Wrap(configDefinition.Section, configDefinition.Key, null, defaultValue);
示例#9
0
 internal ConfigEntry(ConfigFile configFile,
                      ConfigDefinition definition,
                      T defaultValue,
                      ConfigDescription configDescription) : base(configFile, definition, typeof(T),
                                                                  defaultValue, configDescription) =>