using System;
using System.IO;
using System.Linq;
using BepInEx.Logging;
namespace BepInEx.Configuration
{
///
/// Provides access to a single setting inside of a .
///
/// Type of the setting.
public sealed class ConfigEntry : ConfigEntryBase
{
///
/// Fired when the setting is changed. Does not detect changes made outside from this object.
///
public event EventHandler SettingChanged;
private T _typedValue;
///
/// Value of this setting.
///
public T Value
{
get => _typedValue;
set
{
value = ClampValue(value);
if (Equals(_typedValue, value))
return;
_typedValue = value;
OnSettingChanged(this);
}
}
///
public override object BoxedValue
{
get => Value;
set => Value = (T)value;
}
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);
};
}
}
///
/// Container for a single setting of a .
/// Each config entry is linked to one config file.
///
public abstract class ConfigEntryBase
{
///
/// Types of defaultValue and definition.AcceptableValues have to be the same as settingType.
///
internal ConfigEntryBase(ConfigFile configFile, ConfigDefinition definition, Type settingType, object defaultValue, ConfigDescription configDescription)
{
ConfigFile = configFile ?? throw new ArgumentNullException(nameof(configFile));
Definition = definition ?? throw new ArgumentNullException(nameof(definition));
SettingType = settingType ?? throw new ArgumentNullException(nameof(settingType));
Description = configDescription ?? ConfigDescription.Empty;
if (Description.AcceptableValues != null && !SettingType.IsAssignableFrom(Description.AcceptableValues.ValueType))
throw new ArgumentException("configDescription.AcceptableValues is for a different type than the type of this setting");
DefaultValue = defaultValue;
// Free type check and automatically calls ClampValue in case AcceptableValues were provided
BoxedValue = defaultValue;
}
///
/// Config file this entry is a part of.
///
public ConfigFile ConfigFile { get; }
///
/// Category and name of this setting. Used as a unique key for identification within a .
///
public ConfigDefinition Definition { get; }
///
/// Description / metadata of this setting.
///
public ConfigDescription Description { get; }
///
/// Type of the that this setting holds.
///
public Type SettingType { get; }
///
/// Default value of this setting (set only if the setting was not changed before).
///
public object DefaultValue { get; }
///
/// Get or set the value of the setting.
///
public abstract object BoxedValue { get; set; }
///
/// Get the serialized representation of the value.
///
public string GetSerializedValue()
{
return TomlTypeConverter.ConvertToString(BoxedValue, SettingType);
}
///
/// Set the value by using its serialized form.
///
public void SetSerializedValue(string value)
{
try
{
var newValue = TomlTypeConverter.ConvertToValue(value, SettingType);
BoxedValue = newValue;
}
catch (Exception e)
{
Logger.Log(LogLevel.Warning, $"Config value of setting \"{Definition}\" could not be parsed and will be ignored. Reason: {e.Message}; Value: {value}");
}
}
///
/// If necessary, clamp the value to acceptable value range. T has to be equal to settingType.
///
protected T ClampValue(T value)
{
if (Description.AcceptableValues != null)
return (T)Description.AcceptableValues.Clamp(value);
return value;
}
///
/// Trigger setting changed event.
///
protected void OnSettingChanged(object sender)
{
ConfigFile.OnSettingChanged(sender, this);
}
///
/// Write a description of this setting using all available metadata.
///
public void WriteDescription(StreamWriter writer)
{
if (!string.IsNullOrEmpty(Description.Description))
writer.WriteLine($"## {Description.Description.Replace("\n", "\n## ")}");
writer.WriteLine("# Setting type: " + SettingType.Name);
writer.WriteLine("# Default value: " + DefaultValue);
if (Description.AcceptableValues != null)
{
writer.WriteLine(Description.AcceptableValues.ToDescriptionString());
}
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)");
}
}
}
}