ConfigWrapper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. public string Key { get; protected set; }
  18. public string Section { get; protected set; }
  19. public T Value
  20. {
  21. get { return GetValue(); }
  22. set { SetValue(value); }
  23. }
  24. public ConfigWrapper(string key, T @default = default(T))
  25. {
  26. var cvt = TypeDescriptor.GetConverter(typeof(T));
  27. if (!cvt.CanConvertFrom(typeof(string)))
  28. throw new ArgumentException("Default TypeConverter can't convert from String");
  29. if (!cvt.CanConvertTo(typeof(string)))
  30. throw new ArgumentException("Default TypeConverter can't convert to String");
  31. _strToObj = (str) => (T)cvt.ConvertFromInvariantString(str);
  32. _objToStr = (obj) => cvt.ConvertToInvariantString(obj);
  33. _defaultStr = _objToStr(@default);
  34. _default = @default;
  35. Key = key;
  36. }
  37. public ConfigWrapper(string key, Func<string, T> strToObj, Func<T, string> objToStr, T @default = default(T))
  38. {
  39. if (objToStr == null)
  40. throw new ArgumentNullException("objToStr");
  41. if (strToObj == null)
  42. throw new ArgumentNullException("strToObj");
  43. _strToObj = strToObj;
  44. _objToStr = objToStr;
  45. _defaultStr = _objToStr(@default);
  46. Key = key;
  47. }
  48. public ConfigWrapper(string key, IConfigConverter<T> converter, T @default = default(T))
  49. : this(key, converter.ConvertFromString, converter.ConvertToString, @default)
  50. {
  51. }
  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. return _strToObj(strVal);
  92. }
  93. catch (Exception ex)
  94. {
  95. Logger.Log(LogLevel.Error, "ConfigWrapper Get Converter Exception: " + ex.Message);
  96. return _default;
  97. }
  98. }
  99. protected virtual void SetValue(T value)
  100. {
  101. try
  102. {
  103. var strVal = _objToStr(value);
  104. Config.SetEntry(Key, strVal, Section);
  105. }
  106. catch (Exception ex)
  107. {
  108. Logger.Log(LogLevel.Error, "ConfigWrapper Set Converter Exception: " + ex.Message);
  109. }
  110. }
  111. public void Clear()
  112. {
  113. Config.UnsetEntry(Key, Section);
  114. }
  115. }
  116. }