ConfigWrapper.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.ComponentModel;
  3. namespace BepInEx
  4. {
  5. public class ConfigWrapper<T>
  6. {
  7. private readonly Func<string, T> _strToObj;
  8. private readonly Func<T, string> _objToStr;
  9. private readonly string _defaultStr;
  10. private readonly T _default;
  11. public string Key { get; protected set; }
  12. public string Section { get; protected set; }
  13. public T Value
  14. {
  15. get { return GetValue(); }
  16. set { SetValue(value); }
  17. }
  18. public ConfigWrapper(string key, T @default = default(T))
  19. {
  20. var cvt = TypeDescriptor.GetConverter(typeof(T));
  21. if (!cvt.CanConvertFrom(typeof(string)))
  22. throw new ArgumentException("Default TypeConverter can't convert from String");
  23. if (!cvt.CanConvertTo(typeof(string)))
  24. throw new ArgumentException("Default TypeConverter can't convert to String");
  25. _strToObj = (str) => (T)cvt.ConvertFromInvariantString(str);
  26. _objToStr = (obj) => cvt.ConvertToInvariantString(obj);
  27. _defaultStr = _objToStr(@default);
  28. _default = @default;
  29. Key = key;
  30. }
  31. public ConfigWrapper(string key, Func<string, T> strToObj, Func<T, string> objToStr, T @default = default(T))
  32. {
  33. if (objToStr == null)
  34. throw new ArgumentNullException("objToStr");
  35. if (strToObj == null)
  36. throw new ArgumentNullException("strToObj");
  37. _strToObj = strToObj;
  38. _objToStr = objToStr;
  39. _defaultStr = _objToStr(@default);
  40. Key = key;
  41. }
  42. public ConfigWrapper(string key, BaseUnityPlugin plugin, T @default = default(T))
  43. : this(key, @default)
  44. {
  45. Section = TypeLoader.GetMetadata(plugin).GUID;
  46. }
  47. public ConfigWrapper(string key, BaseUnityPlugin plugin, Func<string, T> strToObj, Func<T, string> objToStr, T @default = default(T))
  48. : this(key, strToObj, objToStr, @default)
  49. {
  50. Section = TypeLoader.GetMetadata(plugin).GUID;
  51. }
  52. public ConfigWrapper(string key, string section, T @default = default(T))
  53. : this(key, @default)
  54. {
  55. Section = section;
  56. }
  57. public ConfigWrapper(string key, string section, Func<string, T> strToObj, Func<T, string> objToStr, T @default = default(T))
  58. : this(key, strToObj, objToStr, @default)
  59. {
  60. Section = section;
  61. }
  62. protected virtual bool GetKeyExists()
  63. {
  64. return Config.HasEntry(Key, Section);
  65. }
  66. protected virtual T GetValue()
  67. {
  68. try
  69. {
  70. var strVal = Config.GetEntry(Key, _defaultStr, Section);
  71. return _strToObj(strVal);
  72. }
  73. catch (Exception ex)
  74. {
  75. BepInLogger.Log("ConfigWrapper Get Converter Exception: " + ex.Message);
  76. return _default;
  77. }
  78. }
  79. protected virtual void SetValue(T value)
  80. {
  81. try
  82. {
  83. var strVal = _objToStr(value);
  84. Config.SetEntry(Key, strVal, Section);
  85. }
  86. catch (Exception ex)
  87. {
  88. BepInLogger.Log("ConfigWrapper Set Converter Exception: " + ex.Message);
  89. }
  90. }
  91. public void Clear()
  92. {
  93. Config.UnsetEntry(Key, Section);
  94. }
  95. }
  96. }