Browse Source

Config Wrapper Class

Usagirei 7 years ago
parent
commit
17eece6838
2 changed files with 79 additions and 0 deletions
  1. 1 0
      BepInEx/BepInEx.csproj
  2. 78 0
      BepInEx/ConfigWrapper.cs

+ 1 - 0
BepInEx/BepInEx.csproj

@@ -60,6 +60,7 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Config.cs" />
+    <Compile Include="ConfigWrapper.cs" />
     <Compile Include="ConsoleUtil\ConsoleEncoding\ConsoleEncoding.Buffers.cs" />
     <Compile Include="ConsoleUtil\ConsoleEncoding\ConsoleEncoding.cs" />
     <Compile Include="ConsoleUtil\ConsoleEncoding\ConsoleEncoding.PInvoke.cs" />

+ 78 - 0
BepInEx/ConfigWrapper.cs

@@ -0,0 +1,78 @@
+using System.ComponentModel;
+
+namespace BepInEx
+{
+    public class ConfigWrapper<T>
+    {
+        private TypeConverter _converter;
+
+        public T Default { get; protected set; }
+
+        public bool Exists
+        {
+            get { return GetKeyExists(); }
+        }
+
+        public string Key { get; protected set; }
+
+        public string Section { get; protected set; }
+
+        public T Value
+        {
+            get { return GetValue(); }
+            set { SetValue(value); }
+        }
+
+        public ConfigWrapper(string key, T @default = default(T))
+        {
+            Default = @default;
+            Key = key;
+        }
+
+        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)
+        {
+            Section = section;
+        }
+
+        protected virtual bool GetKeyExists()
+        {
+            return Config.HasEntry(Key, Section);
+        }
+
+        protected virtual T GetValue()
+        {
+            if (_converter == null)
+                _converter = TypeDescriptor.GetConverter(typeof(T));
+
+            if (!Exists)
+                return Default;
+
+            var strVal = Config.GetEntry(Key, Section);
+            return (T)_converter.ConvertFrom(strVal);
+        }
+
+        protected virtual void SetValue(T value)
+        {
+            if (_converter == null)
+                _converter = TypeDescriptor.GetConverter(typeof(T));
+
+            var strVal = _converter.ConvertToString(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);
+        }
+    }
+}