ConfigEntryBase.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using BepInEx.Logging;
  5. using UnityEngine;
  6. namespace BepInEx.Configuration
  7. {
  8. /// <summary>
  9. /// Container for a single setting of a <see cref="Configuration.ConfigFile"/>.
  10. /// Each config entry is linked to one config file.
  11. /// </summary>
  12. public abstract class ConfigEntryBase
  13. {
  14. /// <summary>
  15. /// Types of defaultValue and definition.AcceptableValues have to be the same as settingType.
  16. /// </summary>
  17. internal ConfigEntryBase(ConfigFile configFile, ConfigDefinition definition, Type settingType, object defaultValue)
  18. {
  19. ConfigFile = configFile ?? throw new ArgumentNullException(nameof(configFile));
  20. Definition = definition ?? throw new ArgumentNullException(nameof(definition));
  21. SettingType = settingType ?? throw new ArgumentNullException(nameof(settingType));
  22. // Free type check
  23. Value = defaultValue;
  24. DefaultValue = defaultValue;
  25. }
  26. /// <summary>
  27. /// Config file this entry is a part of.
  28. /// </summary>
  29. public ConfigFile ConfigFile { get; }
  30. /// <summary>
  31. /// Category and name of this setting. Used as a unique key for identification within a <see cref="Configuration.ConfigFile"/>.
  32. /// </summary>
  33. public ConfigDefinition Definition { get; }
  34. /// <summary>
  35. /// Description / metadata of this setting.
  36. /// </summary>
  37. public ConfigDescription Description { get; private set; }
  38. /// <summary>
  39. /// Type of the <see cref="Value"/> that this setting holds.
  40. /// </summary>
  41. public Type SettingType { get; }
  42. /// <summary>
  43. /// Default value of this setting (set only if the setting was not changed before).
  44. /// </summary>
  45. public object DefaultValue { get; }
  46. /// <summary>
  47. /// Get or set the value of the setting.
  48. /// </summary>
  49. public abstract object Value { get; set; }
  50. /// <summary>
  51. /// Get the serialized representation of the value.
  52. /// </summary>
  53. public string GetSerializedValue()
  54. {
  55. return TomlTypeConverter.ConvertToString(Value, SettingType);
  56. }
  57. /// <summary>
  58. /// Set the value by using its serialized form.
  59. /// </summary>
  60. public void SetSerializedValue(string value)
  61. {
  62. try
  63. {
  64. var newValue = TomlTypeConverter.ConvertToValue(value, SettingType);
  65. Value = newValue;
  66. }
  67. catch (Exception e)
  68. {
  69. Logging.Logger.Log(LogLevel.Warning, $"Config value of setting \"{Definition}\" could not be 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. if (hasDescription)
  104. writer.WriteLine(Description.ToSerializedString());
  105. writer.WriteLine("# Setting type: " + SettingType.Name);
  106. writer.WriteLine("# Default value: " + DefaultValue);
  107. if (hasDescription && Description.AcceptableValues != null)
  108. {
  109. writer.WriteLine(Description.AcceptableValues.ToSerializedString());
  110. }
  111. else
  112. {
  113. if (SettingType.IsEnum)
  114. {
  115. writer.WriteLine("# Acceptable values: " + string.Join(", ", Enum.GetNames(SettingType)));
  116. if (SettingType.GetCustomAttributes(typeof(FlagsAttribute), true).Any())
  117. writer.WriteLine("# Multiple values can be set at the same time by separating them with , (e.g. Debug, Warning)");
  118. }
  119. else if (SettingType == typeof(bool))
  120. writer.WriteLine("# Acceptable values: True, False");
  121. else if (SettingType == typeof(Color))
  122. writer.WriteLine("# Acceptable values: Hex HTML color codes");
  123. }
  124. }
  125. }
  126. }