ConfigEntryBase.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. Logging.Logger.Log(LogLevel.Warning, $"Config value of setting \"{Definition}\" could not be 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. if (hasDescription)
  103. writer.WriteLine(Description.ToSerializedString());
  104. writer.WriteLine("# Setting type: " + SettingType.Name);
  105. writer.WriteLine("# Default value: " + DefaultValue);
  106. if (hasDescription && Description.AcceptableValues != null)
  107. {
  108. writer.WriteLine(Description.AcceptableValues.ToSerializedString());
  109. }
  110. else if (SettingType.IsEnum)
  111. {
  112. writer.WriteLine("# Acceptable values: " + string.Join(", ", Enum.GetNames(SettingType)));
  113. if (SettingType.GetCustomAttributes(typeof(FlagsAttribute), true).Any())
  114. writer.WriteLine("# Multiple values can be set at the same time by separating them with , (e.g. Debug, Warning)");
  115. }
  116. }
  117. }
  118. }