123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- using System;
- using System.IO;
- using System.Linq;
- using BepInEx.Logging;
- namespace BepInEx.Configuration
- {
- /// <summary>
- /// Container for a single setting of a <see cref="Configuration.ConfigFile"/>.
- /// Each config entry is linked to one config file.
- /// </summary>
- public abstract class ConfigEntryBase
- {
- /// <summary>
- /// Types of defaultValue and definition.AcceptableValues have to be the same as settingType.
- /// </summary>
- internal ConfigEntryBase(ConfigFile configFile, ConfigDefinition definition, Type settingType, object defaultValue)
- {
- ConfigFile = configFile ?? throw new ArgumentNullException(nameof(configFile));
- Definition = definition ?? throw new ArgumentNullException(nameof(definition));
- SettingType = settingType ?? throw new ArgumentNullException(nameof(settingType));
- // Free type check
- Value = defaultValue;
- DefaultValue = defaultValue;
- }
- /// <summary>
- /// Config file this entry is a part of.
- /// </summary>
- public ConfigFile ConfigFile { get; }
- /// <summary>
- /// Category and name of this setting. Used as a unique key for identification within a <see cref="Configuration.ConfigFile"/>.
- /// </summary>
- public ConfigDefinition Definition { get; }
- /// <summary>
- /// Description / metadata of this setting.
- /// </summary>
- public ConfigDescription Description { get; private set; }
- /// <summary>
- /// Type of the <see cref="Value"/> that this setting holds.
- /// </summary>
- public Type SettingType { get; }
- /// <summary>
- /// Default value of this setting (set only if the setting was not changed before).
- /// </summary>
- public object DefaultValue { get; }
- /// <summary>
- /// Get or set the value of the setting.
- /// </summary>
- public abstract object Value { get; set; }
- /// <summary>
- /// Get the serialized representation of the value.
- /// </summary>
- public string GetSerializedValue()
- {
- return TomlTypeConverter.ConvertToString(Value, SettingType);
- }
- /// <summary>
- /// Set the value by using its serialized form.
- /// </summary>
- public void SetSerializedValue(string value)
- {
- try
- {
- var newValue = TomlTypeConverter.ConvertToValue(value, SettingType);
- Value = newValue;
- }
- catch (Exception e)
- {
- Logging.Logger.Log(LogLevel.Warning, $"Config value of setting \"{Definition}\" could not be parsed and will be ignored. Reason: {e.Message}; Value: {value}");
- }
- }
- internal void SetDescription(ConfigDescription configDescription)
- {
- if (configDescription == null) throw new ArgumentNullException(nameof(configDescription));
- if (configDescription.AcceptableValues != null && !SettingType.IsAssignableFrom(configDescription.AcceptableValues.ValueType))
- throw new ArgumentException("configDescription.AcceptableValues is for a different type than the type of this setting");
- Description = configDescription;
- // Automatically calls ClampValue in case it changed
- Value = Value;
- }
- /// <summary>
- /// If necessary, clamp the value to acceptable value range. T has to be equal to settingType.
- /// </summary>
- protected T ClampValue<T>(T value)
- {
- if (Description?.AcceptableValues != null)
- return (T)Description.AcceptableValues.Clamp(value);
- return value;
- }
- /// <summary>
- /// Trigger setting changed event.
- /// </summary>
- protected void OnSettingChanged(object sender)
- {
- ConfigFile.OnSettingChanged(sender, this);
- }
- /// <summary>
- /// Write a description of this setting using all available metadata.
- /// </summary>
- public void WriteDescription(StreamWriter writer)
- {
- bool hasDescription = Description != null;
- if (hasDescription)
- writer.WriteLine(Description.ToSerializedString());
- writer.WriteLine("# Setting type: " + SettingType.Name);
- writer.WriteLine("# Default value: " + DefaultValue);
- if (hasDescription && Description.AcceptableValues != null)
- {
- writer.WriteLine(Description.AcceptableValues.ToSerializedString());
- }
- else if (SettingType.IsEnum)
- {
- writer.WriteLine("# Acceptable values: " + string.Join(", ", Enum.GetNames(SettingType)));
- if (SettingType.GetCustomAttributes(typeof(FlagsAttribute), true).Any())
- writer.WriteLine("# Multiple values can be set at the same time by separating them with , (e.g. Debug, Warning)");
- }
- }
- }
- }
|