ConfigEntry.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using BepInEx.Logging;
  5. namespace BepInEx.Configuration
  6. {
  7. /// <summary>
  8. /// Container for a single setting of a <see cref="Configuration.ConfigFile"/>.
  9. /// Each config entry is linked to one config file.
  10. /// </summary>
  11. public abstract class ConfigEntryBase
  12. {
  13. /// <summary>
  14. /// Types of defaultValue and definition.AcceptableValues have to be the same as settingType.
  15. /// </summary>
  16. internal ConfigEntryBase(ConfigFile configFile, ConfigDefinition definition, Type settingType, object defaultValue)
  17. {
  18. ConfigFile = configFile ?? throw new ArgumentNullException(nameof(configFile));
  19. Definition = definition ?? throw new ArgumentNullException(nameof(definition));
  20. SettingType = settingType ?? throw new ArgumentNullException(nameof(settingType));
  21. // Free type check
  22. Value = defaultValue;
  23. DefaultValue = defaultValue;
  24. }
  25. /// <summary>
  26. /// Config file this entry is a part of.
  27. /// </summary>
  28. public ConfigFile ConfigFile { get; }
  29. /// <summary>
  30. /// Category and name of this setting. Used as a unique key for identification within a <see cref="Configuration.ConfigFile"/>.
  31. /// </summary>
  32. public ConfigDefinition Definition { get; }
  33. /// <summary>
  34. /// Description / metadata of this setting.
  35. /// </summary>
  36. public ConfigDescription Description { get; private set; }
  37. /// <summary>
  38. /// Type of the <see cref="Value"/> that this setting holds.
  39. /// </summary>
  40. public Type SettingType { get; }
  41. /// <summary>
  42. /// Default value of this setting (set only if the setting was not changed before).
  43. /// </summary>
  44. public object DefaultValue { get; }
  45. /// <summary>
  46. /// Get or set the value of the setting.
  47. /// </summary>
  48. public abstract object Value { get; set; }
  49. /// <summary>
  50. /// Get the serialized representation of the value.
  51. /// </summary>
  52. public string GetSerializedValue()
  53. {
  54. return TomlTypeConverter.ConvertToString(Value, SettingType);
  55. }
  56. /// <summary>
  57. /// Set the value by using its serialized form.
  58. /// </summary>
  59. public void SetSerializedValue(string value)
  60. {
  61. try
  62. {
  63. var newValue = TomlTypeConverter.ConvertToValue(value, SettingType);
  64. Value = newValue;
  65. }
  66. catch (Exception e)
  67. {
  68. Logger.Log(LogLevel.Warning, $"Config value of setting \"{Definition}\" could not be " +
  69. $"parsed and will be ignored. Reason: {e.Message}; Value: {value}");
  70. }
  71. }
  72. internal void SetDescription(ConfigDescription configDescription)
  73. {
  74. if (configDescription == null) throw new ArgumentNullException(nameof(configDescription));
  75. if (configDescription.AcceptableValues != null && !SettingType.IsAssignableFrom(configDescription.AcceptableValues.ValueType))
  76. throw new ArgumentException("configDescription.AcceptableValues is for a different type than the type of this setting");
  77. Description = configDescription;
  78. // Automatically calls ClampValue in case it changed
  79. Value = Value;
  80. }
  81. /// <summary>
  82. /// If necessary, clamp the value to acceptable value range. T has to be equal to settingType.
  83. /// </summary>
  84. protected T ClampValue<T>(T value)
  85. {
  86. if (Description?.AcceptableValues != null)
  87. return (T)Description.AcceptableValues.Clamp(value);
  88. return value;
  89. }
  90. /// <summary>
  91. /// Trigger setting changed event.
  92. /// </summary>
  93. protected void OnSettingChanged(object sender)
  94. {
  95. ConfigFile.OnSettingChanged(sender, this);
  96. }
  97. /// <summary>
  98. /// Write a description of this setting using all available metadata.
  99. /// </summary>
  100. public void WriteDescription(StreamWriter writer)
  101. {
  102. bool hasDescription = Description != null;
  103. bool hasType = SettingType != null;
  104. if (hasDescription)
  105. writer.WriteLine(Description.ToSerializedString());
  106. if (hasType)
  107. {
  108. if (SettingType.IsEnum && SettingType.GetCustomAttributes(typeof(FlagsAttribute), true).Any())
  109. writer.WriteLine("# Multiple values can be set at the same time by separating them with , (e.g. Debug, Warning)");
  110. writer.WriteLine("# Setting type: " + SettingType.Name);
  111. writer.WriteLine("# Default value: " + DefaultValue);
  112. }
  113. if (hasDescription && Description.AcceptableValues != null)
  114. {
  115. writer.WriteLine(Description.AcceptableValues.ToSerializedString());
  116. }
  117. else if (hasType)
  118. {
  119. /*if (SettingType == typeof(bool))
  120. writer.WriteLine("# Acceptable values: True, False");
  121. else*/
  122. if (SettingType.IsEnum)
  123. writer.WriteLine("# Acceptable values: " + string.Join(", ", Enum.GetNames(SettingType)));
  124. }
  125. }
  126. }
  127. /// <inheritdoc />
  128. public class ConfigEntry<T> : ConfigEntryBase
  129. {
  130. private T _typedValue;
  131. internal ConfigEntry(ConfigFile configFile, ConfigDefinition definition, T defaultValue) : base(configFile, definition, typeof(T), defaultValue) { }
  132. /// <summary>
  133. /// Get or set the value of the setting without boxing.
  134. /// </summary>
  135. public T TypedValue
  136. {
  137. get => _typedValue;
  138. set
  139. {
  140. value = ClampValue(value);
  141. if (Equals(_typedValue, value))
  142. return;
  143. _typedValue = value;
  144. OnSettingChanged(this);
  145. }
  146. }
  147. /// <inheritdoc />
  148. public override object Value
  149. {
  150. get => TypedValue;
  151. set => TypedValue = (T)value;
  152. }
  153. }
  154. }