ConfigWrapper.cs 5.2 KB

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