Browse Source

ConfigWrapper Changes

Usagirei 6 years ago
parent
commit
5e5d061688
2 changed files with 53 additions and 29 deletions
  1. 2 0
      BepInEx/Config.cs
  2. 51 29
      BepInEx/ConfigWrapper.cs

+ 2 - 0
BepInEx/Config.cs

@@ -66,7 +66,9 @@ namespace BepInEx
             }
 
             if (subdict.TryGetValue(key, out string value))
+            {
                 return value;
+            }
             else
             {
                 SetEntry(key, defaultValue, section);

+ 51 - 29
BepInEx/ConfigWrapper.cs

@@ -1,17 +1,13 @@
-using System.ComponentModel;
+using System;
+using System.ComponentModel;
 
 namespace BepInEx
 {
     public class ConfigWrapper<T>
     {
-        private TypeConverter _converter;
-
-        public T Default { get; protected set; }
-
-        public bool Exists
-        {
-            get { return GetKeyExists(); }
-        }
+        private readonly Func<string, T> _strToObj;
+        private readonly Func<T, string> _objToStr;
+        private readonly string _defaultStr;
 
         public string Key { get; protected set; }
 
@@ -25,16 +21,56 @@ namespace BepInEx
 
         public ConfigWrapper(string key, T @default = default(T))
         {
-            Default = @default;
+            var cvt = TypeDescriptor.GetConverter(typeof(T));
+
+            if (!cvt.CanConvertFrom(typeof(string)))
+                throw new ArgumentException("Default TypeConverter can't convert from String");
+
+            if (!cvt.CanConvertTo(typeof(string)))
+                throw new ArgumentException("Default TypeConverter can't convert to String");
+
+            _strToObj = (str) => (T)cvt.ConvertFromInvariantString(str);
+            _objToStr = (obj) => cvt.ConvertToInvariantString(obj);
+
+            _defaultStr = _objToStr(@default);
+            Key = key;
+        }
+
+        public ConfigWrapper(string key, Func<string, T> strToObj, Func<T, string> objToStr, T @default = default(T))
+        {
+            if (objToStr == null)
+                throw new ArgumentNullException("objToStr");
+
+            if (strToObj == null)
+                throw new ArgumentNullException("strToObj");
+
+            _strToObj = strToObj;
+            _objToStr = objToStr;
+
+            _defaultStr = _objToStr(@default);
             Key = key;
         }
 
-        public ConfigWrapper(string key, BaseUnityPlugin plugin, T @default = default(T)) : this(key, @default)
+        public ConfigWrapper(string key, BaseUnityPlugin plugin, T @default = default(T))
+            : this(key, @default)
         {
             Section = plugin.ID;
         }
 
-        public ConfigWrapper(string key, string section, T @default = default(T)) : this(key, @default)
+        public ConfigWrapper(string key, BaseUnityPlugin plugin, Func<string, T> strToObj, Func<T, string> objToStr, T @default = default(T))
+          : this(key, strToObj, objToStr, @default)
+        {
+            Section = plugin.ID;
+        }
+
+        public ConfigWrapper(string key, string section, T @default = default(T))
+            : this(key, @default)
+        {
+            Section = section;
+        }
+
+        public ConfigWrapper(string key, string section, Func<string, T> strToObj, Func<T, string> objToStr, T @default = default(T))
+           : this(key, strToObj, objToStr, @default)
         {
             Section = section;
         }
@@ -46,30 +82,16 @@ namespace BepInEx
 
         protected virtual T GetValue()
         {
-            if (_converter == null)
-                _converter = TypeDescriptor.GetConverter(typeof(T));
-
-            if (!Exists)
-                return Default;
-
-            var strVal = Config.GetEntry(Key, null, Section);
-            return (T)_converter.ConvertFrom(strVal);
+            var strVal = Config.GetEntry(Key, _defaultStr, Section);
+            return _strToObj(strVal);
         }
 
         protected virtual void SetValue(T value)
         {
-            if (_converter == null)
-                _converter = TypeDescriptor.GetConverter(typeof(T));
-
-            var strVal = _converter.ConvertToString(value);
+            var strVal = _objToStr(value);
             Config.SetEntry(Key, strVal, Section);
         }
 
-        public static void RegisterTypeConverter<TC>() where TC : TypeConverter
-        {
-            TypeDescriptor.AddAttributes(typeof(T), new TypeConverterAttribute(typeof(TC)));
-        }
-
         public void Clear()
         {
             Config.UnsetEntry(Key, Section);