Ejemplo n.º 1
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);
         }
     };
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Create a new setting. The setting is saved to drive and loaded automatically.
 /// Each section and key pair 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="section">Section/category/group of the setting. Settings are grouped by this.</param>
 /// <param name="key">Name 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>(string section, string key, T defaultValue, ConfigDescription configDescription = null)
 => AddSetting(new ConfigDefinition(section, key), defaultValue, configDescription);
Ejemplo n.º 3
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;
                }
            }
        }
Ejemplo n.º 4
0
 public ConfigEntry <T> AddSetting <T>(ConfigDefinition configDefinition, T defaultValue, ConfigDescription configDescription = null)
 => Bind(configDefinition, defaultValue, configDescription);
Ejemplo n.º 5
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> Bind <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.TryGetValue(configDefinition, out var rawEntry))
                {
                    return((ConfigEntry <T>)rawEntry);
                }

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

                Entries[configDefinition] = entry;

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

                if (SaveOnConfigSet)
                {
                    Save();
                }

                return(entry);
            }
        }
Ejemplo n.º 6
0
 internal ConfigEntry(ConfigFile configFile,
                      ConfigDefinition definition,
                      T defaultValue,
                      ConfigDescription configDescription) : base(configFile, definition, typeof(T),
                                                                  defaultValue, configDescription) =>