ConfigEntryBase.cs 4.5 KB

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