ConfigEntry.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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; internal 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. wasChanged = wasChanged || !Equals(newValue, _convertedValue);
  88. if (wasChanged)
  89. {
  90. _convertedValue = newValue;
  91. if (fireEvent)
  92. OnSettingChanged(sender);
  93. }
  94. }
  95. /// <summary>
  96. /// Get the serialized representation of the value.
  97. /// </summary>
  98. public string GetSerializedValue()
  99. {
  100. if (_serializedValue != null)
  101. return _serializedValue;
  102. if (!IsDefined)
  103. return null;
  104. return TomlTypeConverter.ConvertToString(Value, SettingType);
  105. }
  106. /// <summary>
  107. /// Set the value by using its serialized form.
  108. /// </summary>
  109. public void SetSerializedValue(string newValue) => SetSerializedValue(newValue, true, this);
  110. internal void SetSerializedValue(string newValue, bool fireEvent, object sender)
  111. {
  112. string current = GetSerializedValue();
  113. if (string.Equals(current, newValue)) return;
  114. _serializedValue = newValue;
  115. if (!IsDefined) return;
  116. if (ProcessSerializedValue())
  117. {
  118. if (fireEvent)
  119. OnSettingChanged(sender);
  120. }
  121. }
  122. private bool ProcessSerializedValue()
  123. {
  124. if (!IsDefined)
  125. throw new InvalidOperationException("Can't get the value before the SettingType is specified");
  126. if (_serializedValue != null)
  127. {
  128. string value = _serializedValue;
  129. _serializedValue = null;
  130. if (value != "")
  131. {
  132. try
  133. {
  134. var newValue = TomlTypeConverter.ConvertToValue(value, SettingType);
  135. if (!Equals(newValue, _convertedValue))
  136. {
  137. _convertedValue = newValue;
  138. return true;
  139. }
  140. return false;
  141. }
  142. catch (Exception e)
  143. {
  144. Logger.Log(LogLevel.Warning, $"Config value of setting \"{Definition}\" could not be " +
  145. $"parsed and will be ignored. Reason: {e.Message}; Value: {value}");
  146. }
  147. }
  148. }
  149. if (_convertedValue == null && DefaultValue != null)
  150. {
  151. _convertedValue = DefaultValue;
  152. return true;
  153. }
  154. return false;
  155. }
  156. private void OnSettingChanged(object sender)
  157. {
  158. ConfigFile.OnSettingChanged(sender, this);
  159. }
  160. /// <summary>
  161. /// Write a description of this setting using all available metadata.
  162. /// </summary>
  163. public void WriteDescription(StreamWriter writer)
  164. {
  165. if (Description != null)
  166. writer.WriteLine(Description.ToSerializedString());
  167. if (SettingType != null)
  168. {
  169. writer.WriteLine("# Setting type: " + SettingType.Name);
  170. writer.WriteLine("# Default value: " + DefaultValue);
  171. // todo acceptable values
  172. if (SettingType.IsEnum && SettingType.GetCustomAttributes(typeof(FlagsAttribute), true).Any())
  173. writer.WriteLine("# Multiple values can be set at the same time by separating them with , (e.g. Debug, Warning)");
  174. }
  175. }
  176. }
  177. }