コード例 #1
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;
                }
            }
        }
コード例 #2
0
ファイル: ConfigFile.cs プロジェクト: shuixi2013/BepInEx
        /// <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);
            }
        }