ConfigWrapper.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.ComponentModel;
  3. using BepInEx.Logging;
  4. namespace BepInEx
  5. {
  6. public interface IConfigConverter<T>
  7. {
  8. string ConvertToString(T value);
  9. T ConvertFromString(string str);
  10. }
  11. public class ConfigWrapper<T>
  12. {
  13. private readonly Func<string, T> _strToObj;
  14. private readonly Func<T, string> _objToStr;
  15. private readonly string _defaultStr;
  16. private readonly T _default;
  17. private T _lastValue;
  18. private bool _lastValueSet;
  19. public string Key { get; protected set; }
  20. public string Section { get; protected set; }
  21. public T Value
  22. {
  23. get { return GetValue(); }
  24. set { SetValue(value); }
  25. }
  26. public ConfigWrapper(string key, T @default = default(T))
  27. {
  28. var cvt = TypeDescriptor.GetConverter(typeof(T));
  29. if (!cvt.CanConvertFrom(typeof(string)))
  30. throw new ArgumentException("Default TypeConverter can't convert from String");
  31. if (!cvt.CanConvertTo(typeof(string)))
  32. throw new ArgumentException("Default TypeConverter can't convert to String");
  33. _strToObj = (str) => (T)cvt.ConvertFromInvariantString(str);
  34. _objToStr = (obj) => cvt.ConvertToInvariantString(obj);
  35. _defaultStr = _objToStr(@default);
  36. _default = @default;
  37. Key = key;
  38. }
  39. public ConfigWrapper(string key, Func<string, T> strToObj, Func<T, string> objToStr, T @default = default(T))
  40. {
  41. if (objToStr == null)
  42. throw new ArgumentNullException("objToStr");
  43. if (strToObj == null)
  44. throw new ArgumentNullException("strToObj");
  45. _strToObj = strToObj;
  46. _objToStr = objToStr;
  47. _defaultStr = _objToStr(@default);
  48. Key = key;
  49. }
  50. public ConfigWrapper(string key, IConfigConverter<T> converter, T @default = default(T))
  51. : this(key, converter.ConvertFromString, converter.ConvertToString, @default)
  52. {
  53. }
  54. public ConfigWrapper(string key, BaseUnityPlugin plugin, T @default = default(T))
  55. : this(key, @default)
  56. {
  57. Section = MetadataHelper.GetMetadata(plugin).GUID;
  58. }
  59. public ConfigWrapper(string key, BaseUnityPlugin plugin, Func<string, T> strToObj, Func<T, string> objToStr, T @default = default(T))
  60. : this(key, strToObj, objToStr, @default)
  61. {
  62. Section = MetadataHelper.GetMetadata(plugin).GUID;
  63. }
  64. public ConfigWrapper(string key, BaseUnityPlugin plugin, IConfigConverter<T> converter, T @default = default(T))
  65. : this(key, converter.ConvertFromString, converter.ConvertToString, @default)
  66. {
  67. Section = MetadataHelper.GetMetadata(plugin).GUID;
  68. }
  69. public ConfigWrapper(string key, string section, T @default = default(T))
  70. : this(key, @default)
  71. {
  72. Section = section;
  73. }
  74. public ConfigWrapper(string key, string section, Func<string, T> strToObj, Func<T, string> objToStr, T @default = default(T))
  75. : this(key, strToObj, objToStr, @default)
  76. {
  77. Section = section;
  78. }
  79. public ConfigWrapper(string key, string section, IConfigConverter<T> converter, T @default = default(T))
  80. : this(key, converter.ConvertFromString, converter.ConvertToString, @default)
  81. {
  82. Section = section;
  83. }
  84. protected virtual bool GetKeyExists()
  85. {
  86. return Config.HasEntry(Key, Section);
  87. }
  88. protected virtual T GetValue()
  89. {
  90. try
  91. {
  92. var strVal = Config.GetEntry(Key, _defaultStr, Section);
  93. var obj = _strToObj(strVal);
  94. // Always update in case config was changed from outside
  95. _lastValue = obj;
  96. _lastValueSet = true;
  97. return obj;
  98. }
  99. catch (Exception ex)
  100. {
  101. Logger.Log(LogLevel.Error, "ConfigWrapper Get Converter Exception: " + ex.Message);
  102. return _default;
  103. }
  104. }
  105. protected virtual void SetValue(T value)
  106. {
  107. try
  108. {
  109. // Always write just in case config was changed from outside
  110. var strVal = _objToStr(value);
  111. Config.SetEntry(Key, strVal, Section);
  112. if (_lastValueSet && Equals(_lastValue, value)) return;
  113. _lastValue = value;
  114. _lastValueSet = true;
  115. OnSettingChanged();
  116. }
  117. catch (Exception ex)
  118. {
  119. Logger.Log(LogLevel.Error, "ConfigWrapper Set Converter Exception: " + ex.Message);
  120. }
  121. }
  122. public void Clear()
  123. {
  124. Config.UnsetEntry(Key, Section);
  125. _lastValueSet = false;
  126. OnSettingChanged();
  127. }
  128. /// <summary>
  129. /// Fired when the setting is changed. Does not detect changes made outside from this object.
  130. /// </summary>
  131. public event EventHandler SettingChanged;
  132. private void OnSettingChanged()
  133. {
  134. SettingChanged?.Invoke(this, EventArgs.Empty);
  135. }
  136. }
  137. }