ConfigEntry.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 sealed class ConfigEntry
  12. {
  13. internal ConfigEntry(ConfigFile configFile, ConfigDefinition definition, Type settingType, object defaultValue) : this(configFile, definition)
  14. {
  15. SetTypeAndDefaultValue(settingType, defaultValue, true);
  16. }
  17. internal ConfigEntry(ConfigFile configFile, ConfigDefinition definition)
  18. {
  19. ConfigFile = configFile ?? throw new ArgumentNullException(nameof(configFile));
  20. Definition = definition ?? throw new ArgumentNullException(nameof(definition));
  21. }
  22. internal void SetTypeAndDefaultValue(Type settingType, object defaultValue, bool uniqueDefaultValue)
  23. {
  24. if (settingType == null) throw new ArgumentNullException(nameof(settingType));
  25. if (settingType == SettingType)
  26. {
  27. if (uniqueDefaultValue)
  28. DefaultValue = defaultValue;
  29. return;
  30. }
  31. if (SettingType != null)
  32. {
  33. throw new ArgumentException($"Tried to define setting \"{Definition}\" as type {settingType.Name} " +
  34. $"while it was already defined as type {SettingType.Name}. Use the same " +
  35. "Type for all Wrappers of a single setting.");
  36. }
  37. if (defaultValue == null && settingType.IsValueType)
  38. throw new ArgumentException("defaultValue is null while settingType is a value type");
  39. if (defaultValue != null && !settingType.IsInstanceOfType(defaultValue))
  40. throw new ArgumentException("defaultValue can not be assigned to type " + settingType.Name);
  41. SettingType = settingType;
  42. DefaultValue = defaultValue;
  43. }
  44. private object _convertedValue;
  45. private string _serializedValue;
  46. /// <summary>
  47. /// Config file this entry is a part of.
  48. /// </summary>
  49. public ConfigFile ConfigFile { get; }
  50. /// <summary>
  51. /// Category and name of this setting. Used as a unique key for identification within a <see cref="Configuration.ConfigFile"/>.
  52. /// </summary>
  53. public ConfigDefinition Definition { get; }
  54. /// <summary>
  55. /// Description / metadata of this setting.
  56. /// </summary>
  57. public ConfigDescription Description { get; private set; }
  58. /// <summary>
  59. /// Type of the <see cref="Value"/> that this setting holds.
  60. /// </summary>
  61. public Type SettingType { get; private set; }
  62. /// <summary>
  63. /// Default value of this setting (set only if the setting was not changed before).
  64. /// </summary>
  65. public object DefaultValue { get; private set; }
  66. /// <summary>
  67. /// Is the type of this setting defined, and by extension can <see cref="Value"/> of this setting be accessed.
  68. /// Setting is defined when any <see cref="ConfigWrapper{T}"/> objects reference it.
  69. /// </summary>
  70. public bool IsDefined => SettingType != null;
  71. /// <summary>
  72. /// Get or set the value of the setting.
  73. /// Can't be used when <see cref="IsDefined"/> is false.
  74. /// </summary>
  75. public object Value
  76. {
  77. get
  78. {
  79. ProcessSerializedValue();
  80. return _convertedValue;
  81. }
  82. set => SetValue(value, true, this);
  83. }
  84. internal void SetValue(object newValue, bool fireEvent, object sender)
  85. {
  86. bool wasChanged = ProcessSerializedValue();
  87. newValue = ClampValue(newValue);
  88. wasChanged = wasChanged || !Equals(newValue, _convertedValue);
  89. if (wasChanged)
  90. {
  91. _convertedValue = newValue;
  92. if (fireEvent)
  93. OnSettingChanged(sender);
  94. }
  95. }
  96. /// <summary>
  97. /// Get the serialized representation of the value.
  98. /// </summary>
  99. public string GetSerializedValue()
  100. {
  101. if (_serializedValue != null)
  102. return _serializedValue;
  103. if (!IsDefined)
  104. return null;
  105. return TomlTypeConverter.ConvertToString(Value, SettingType);
  106. }
  107. /// <summary>
  108. /// Set the value by using its serialized form.
  109. /// </summary>
  110. public void SetSerializedValue(string newValue) => SetSerializedValue(newValue, true, this);
  111. internal void SetSerializedValue(string newValue, bool fireEvent, object sender)
  112. {
  113. string current = GetSerializedValue();
  114. if (string.Equals(current, newValue)) return;
  115. _serializedValue = newValue;
  116. if (!IsDefined) return;
  117. if (ProcessSerializedValue())
  118. {
  119. if (fireEvent)
  120. OnSettingChanged(sender);
  121. }
  122. }
  123. private bool ProcessSerializedValue()
  124. {
  125. if (!IsDefined)
  126. throw new InvalidOperationException("Can't get the value before the SettingType is specified");
  127. if (_serializedValue != null)
  128. {
  129. string value = _serializedValue;
  130. _serializedValue = null;
  131. if (value != "")
  132. {
  133. try
  134. {
  135. var newValue = TomlTypeConverter.ConvertToValue(value, SettingType);
  136. newValue = ClampValue(newValue);
  137. if (!Equals(newValue, _convertedValue))
  138. {
  139. _convertedValue = newValue;
  140. return true;
  141. }
  142. return false;
  143. }
  144. catch (Exception e)
  145. {
  146. Logger.Log(LogLevel.Warning, $"Config value of setting \"{Definition}\" could not be " +
  147. $"parsed and will be ignored. Reason: {e.Message}; Value: {value}");
  148. }
  149. }
  150. }
  151. if (_convertedValue == null && DefaultValue != null)
  152. {
  153. _convertedValue = DefaultValue;
  154. return true;
  155. }
  156. return false;
  157. }
  158. internal void SetDescription(ConfigDescription configDescription)
  159. {
  160. Description = configDescription;
  161. SetValue(ClampValue(Value), true, this);
  162. }
  163. private object ClampValue(object value)
  164. {
  165. if (Description?.AcceptableValues != null)
  166. return Description.AcceptableValues.Clamp(value);
  167. return value;
  168. }
  169. private void OnSettingChanged(object sender)
  170. {
  171. ConfigFile.OnSettingChanged(sender, this);
  172. }
  173. /// <summary>
  174. /// Write a description of this setting using all available metadata.
  175. /// </summary>
  176. public void WriteDescription(StreamWriter writer)
  177. {
  178. bool hasDescription = Description != null;
  179. bool hasType = SettingType != null;
  180. if (hasDescription)
  181. writer.WriteLine(Description.ToSerializedString());
  182. if (hasType)
  183. {
  184. writer.WriteLine("# Setting type: " + SettingType.Name);
  185. if (SettingType.IsEnum && SettingType.GetCustomAttributes(typeof(FlagsAttribute), true).Any())
  186. writer.WriteLine("# Multiple values can be set at the same time by separating them with , (e.g. Debug, Warning)");
  187. writer.WriteLine("# Default value: " + DefaultValue);
  188. }
  189. if (hasDescription)
  190. {
  191. if (Description.AcceptableValues != null)
  192. {
  193. writer.WriteLine(Description.AcceptableValues.ToSerializedString());
  194. }
  195. else if (hasType)
  196. {
  197. if (SettingType == typeof(bool))
  198. writer.WriteLine("# Acceptable values: True, False");
  199. else if (SettingType.IsEnum)
  200. writer.WriteLine("# Acceptable values: " + string.Join(", ", Enum.GetNames(SettingType)));
  201. }
  202. }
  203. }
  204. }
  205. }