ConfigWrapper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. public ConfigWrapper(string key, BaseUnityPlugin plugin, T @default = default(T))
  53. : this(key, @default)
  54. {
  55. Section = MetadataHelper.GetMetadata(plugin).GUID;
  56. }
  57. public ConfigWrapper(string key, BaseUnityPlugin plugin, Func<string, T> strToObj, Func<T, string> objToStr, T @default = default(T))
  58. : this(key, strToObj, objToStr, @default)
  59. {
  60. Section = MetadataHelper.GetMetadata(plugin).GUID;
  61. }
  62. public ConfigWrapper(string key, BaseUnityPlugin plugin, IConfigConverter<T> converter, T @default = default(T))
  63. : this(key, converter.ConvertFromString, converter.ConvertToString, @default)
  64. {
  65. Section = MetadataHelper.GetMetadata(plugin).GUID;
  66. }
  67. public ConfigWrapper(string key, string section, T @default = default(T))
  68. : this(key, @default)
  69. {
  70. Section = section;
  71. }
  72. public ConfigWrapper(string key, string section, Func<string, T> strToObj, Func<T, string> objToStr, T @default = default(T))
  73. : this(key, strToObj, objToStr, @default)
  74. {
  75. Section = section;
  76. }
  77. public ConfigWrapper(string key, string section, IConfigConverter<T> converter, T @default = default(T))
  78. : this(key, converter.ConvertFromString, converter.ConvertToString, @default)
  79. {
  80. Section = section;
  81. }
  82. protected virtual bool GetKeyExists()
  83. {
  84. return Config.HasEntry(Key, Section);
  85. }
  86. protected virtual T GetValue()
  87. {
  88. try
  89. {
  90. var strVal = Config.GetEntry(Key, _defaultStr, Section);
  91. var obj = _strToObj(strVal);
  92. // Always update in case config was changed from outside
  93. _lastValue = obj;
  94. _lastValueSet = true;
  95. return obj;
  96. }
  97. catch (Exception ex)
  98. {
  99. Logger.Log(LogLevel.Error, "ConfigWrapper Get Converter Exception: " + ex.Message);
  100. return _default;
  101. }
  102. }
  103. protected virtual void SetValue(T value)
  104. {
  105. try
  106. {
  107. // Always write just in case config was changed from outside
  108. var strVal = _objToStr(value);
  109. Config.SetEntry(Key, strVal, Section);
  110. if (_lastValueSet && Equals(_lastValue, value))
  111. return;
  112. _lastValue = value;
  113. _lastValueSet = true;
  114. OnSettingChanged();
  115. }
  116. catch (Exception ex)
  117. {
  118. Logger.Log(LogLevel.Error, "ConfigWrapper Set Converter Exception: " + ex.Message);
  119. }
  120. }
  121. public void Clear()
  122. {
  123. Config.UnsetEntry(Key, Section);
  124. _lastValueSet = false;
  125. OnSettingChanged();
  126. }
  127. /// <summary>
  128. /// Fired when the setting is changed. Does not detect changes made outside from this object.
  129. /// </summary>
  130. public event EventHandler SettingChanged;
  131. private void OnSettingChanged()
  132. {
  133. SettingChanged?.Invoke(this, EventArgs.Empty);
  134. }
  135. }
  136. }