瀏覽代碼

Add Interface-Based Converter Overloads

Usagirei 6 年之前
父節點
當前提交
070ab70d49
共有 1 個文件被更改,包括 25 次插入0 次删除
  1. 25 0
      BepInEx/ConfigWrapper.cs

+ 25 - 0
BepInEx/ConfigWrapper.cs

@@ -3,6 +3,12 @@ using System.ComponentModel;
 
 namespace BepInEx
 {
+    public interface IConfigConverter<T>
+    {
+        string ConvertToString<T>(T value);
+        T ConvertFromString(string str);
+    }
+    
     public class ConfigWrapper<T>
     {
         private readonly Func<string, T> _strToObj;
@@ -53,6 +59,13 @@ namespace BepInEx
             Key = key;
         }
 
+        public ConfigWrapper(string key, IConfigConverter<T> converter, T @default = default(T))
+            : this(key, converter.ConvertFromString, converter.ConvertToString, @default)
+        {
+
+        }
+
+
         public ConfigWrapper(string key, BaseUnityPlugin plugin, T @default = default(T))
             : this(key, @default)
         {
@@ -65,6 +78,12 @@ namespace BepInEx
             Section = TypeLoader.GetMetadata(plugin).GUID;
         }
 
+        public ConfigWrapper(string key, BaseUnityPlugin plugin, IConfigConverter<T> converter, T @default = default(T))
+          : this(key, converter.ConvertFromString, converter.ConvertToString, @default)
+        {
+            Section = TypeLoader.GetMetadata(plugin).GUID;
+        }
+
         public ConfigWrapper(string key, string section, T @default = default(T))
             : this(key, @default)
         {
@@ -77,6 +96,12 @@ namespace BepInEx
             Section = section;
         }
 
+        public ConfigWrapper(string key, string section, IConfigConverter<T> converter, T @default = default(T))
+           : this(key, converter.ConvertFromString, converter.ConvertToString, @default)
+        {
+            Section = section;
+        }
+
         protected virtual bool GetKeyExists()
         {
             return Config.HasEntry(Key, Section);