ConfigWrapper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. public string Key { get; protected set; }
  17. public string Section { get; protected set; }
  18. public T Value
  19. {
  20. get { return GetValue(); }
  21. set { SetValue(value); }
  22. }
  23. public ConfigWrapper(string key, T @default = default(T))
  24. {
  25. var cvt = TypeDescriptor.GetConverter(typeof(T));
  26. if (!cvt.CanConvertFrom(typeof(string)))
  27. throw new ArgumentException("Default TypeConverter can't convert from String");
  28. if (!cvt.CanConvertTo(typeof(string)))
  29. throw new ArgumentException("Default TypeConverter can't convert to String");
  30. _strToObj = (str) => (T)cvt.ConvertFromInvariantString(str);
  31. _objToStr = (obj) => cvt.ConvertToInvariantString(obj);
  32. _defaultStr = _objToStr(@default);
  33. _default = @default;
  34. Key = key;
  35. }
  36. public ConfigWrapper(string key, Func<string, T> strToObj, Func<T, string> objToStr, T @default = default(T))
  37. {
  38. if (objToStr == null)
  39. throw new ArgumentNullException("objToStr");
  40. if (strToObj == null)
  41. throw new ArgumentNullException("strToObj");
  42. _strToObj = strToObj;
  43. _objToStr = objToStr;
  44. _defaultStr = _objToStr(@default);
  45. Key = key;
  46. }
  47. public ConfigWrapper(string key, IConfigConverter<T> converter, T @default = default(T))
  48. : this(key, converter.ConvertFromString, converter.ConvertToString, @default)
  49. {
  50. }
  51. public ConfigWrapper(string key, BaseUnityPlugin plugin, T @default = default(T))
  52. : this(key, @default)
  53. {
  54. Section = MetadataHelper.GetMetadata(plugin).GUID;
  55. }
  56. public ConfigWrapper(string key, BaseUnityPlugin plugin, Func<string, T> strToObj, Func<T, string> objToStr, T @default = default(T))
  57. : this(key, strToObj, objToStr, @default)
  58. {
  59. Section = MetadataHelper.GetMetadata(plugin).GUID;
  60. }
  61. public ConfigWrapper(string key, BaseUnityPlugin plugin, IConfigConverter<T> converter, T @default = default(T))
  62. : this(key, converter.ConvertFromString, converter.ConvertToString, @default)
  63. {
  64. Section = MetadataHelper.GetMetadata(plugin).GUID;
  65. }
  66. public ConfigWrapper(string key, string section, T @default = default(T))
  67. : this(key, @default)
  68. {
  69. Section = section;
  70. }
  71. public ConfigWrapper(string key, string section, Func<string, T> strToObj, Func<T, string> objToStr, T @default = default(T))
  72. : this(key, strToObj, objToStr, @default)
  73. {
  74. Section = section;
  75. }
  76. public ConfigWrapper(string key, string section, IConfigConverter<T> converter, T @default = default(T))
  77. : this(key, converter.ConvertFromString, converter.ConvertToString, @default)
  78. {
  79. Section = section;
  80. }
  81. protected virtual bool GetKeyExists()
  82. {
  83. return Config.HasEntry(Key, Section);
  84. }
  85. protected virtual T GetValue()
  86. {
  87. try
  88. {
  89. var strVal = Config.GetEntry(Key, _defaultStr, Section);
  90. return _strToObj(strVal);
  91. }
  92. catch (Exception ex)
  93. {
  94. BepInLogger.Log("ConfigWrapper Get Converter Exception: " + ex.Message);
  95. return _default;
  96. }
  97. }
  98. protected virtual void SetValue(T value)
  99. {
  100. try
  101. {
  102. var strVal = _objToStr(value);
  103. Config.SetEntry(Key, strVal, Section);
  104. }
  105. catch (Exception ex)
  106. {
  107. BepInLogger.Log("ConfigWrapper Set Converter Exception: " + ex.Message);
  108. }
  109. }
  110. public void Clear()
  111. {
  112. Config.UnsetEntry(Key, Section);
  113. }
  114. }
  115. }