|
@@ -1,17 +1,20 @@
|
|
|
-using System.ComponentModel;
|
|
|
+using System;
|
|
|
+using System.ComponentModel;
|
|
|
|
|
|
namespace BepInEx
|
|
|
{
|
|
|
+ public interface IConfigConverter<T>
|
|
|
+ {
|
|
|
+ string ConvertToString(T value);
|
|
|
+ T ConvertFromString(string str);
|
|
|
+ }
|
|
|
+
|
|
|
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;
|
|
|
+ private readonly T _default;
|
|
|
|
|
|
public string Key { get; protected set; }
|
|
|
|
|
@@ -25,49 +28,110 @@ 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);
|
|
|
+ _default = @default;
|
|
|
Key = key;
|
|
|
}
|
|
|
|
|
|
- public ConfigWrapper(string key, BaseUnityPlugin plugin, T @default = default(T)) : this(key, @default)
|
|
|
+ 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, 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)
|
|
|
{
|
|
|
Section = TypeLoader.GetMetadata(plugin).GUID;
|
|
|
}
|
|
|
|
|
|
- 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 = section;
|
|
|
+ Section = TypeLoader.GetMetadata(plugin).GUID;
|
|
|
}
|
|
|
|
|
|
- protected virtual bool GetKeyExists()
|
|
|
+ public ConfigWrapper(string key, BaseUnityPlugin plugin, IConfigConverter<T> converter, T @default = default(T))
|
|
|
+ : this(key, converter.ConvertFromString, converter.ConvertToString, @default)
|
|
|
{
|
|
|
- return Config.HasEntry(Key, Section);
|
|
|
+ Section = TypeLoader.GetMetadata(plugin).GUID;
|
|
|
}
|
|
|
|
|
|
- protected virtual T GetValue()
|
|
|
+ public ConfigWrapper(string key, string section, T @default = default(T))
|
|
|
+ : this(key, @default)
|
|
|
{
|
|
|
- if (_converter == null)
|
|
|
- _converter = TypeDescriptor.GetConverter(typeof(T));
|
|
|
+ Section = section;
|
|
|
+ }
|
|
|
|
|
|
- if (!Exists)
|
|
|
- return Default;
|
|
|
+ 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;
|
|
|
+ }
|
|
|
|
|
|
- var strVal = Config.GetEntry(Key, null, Section);
|
|
|
- return (T)_converter.ConvertFrom(strVal);
|
|
|
+ public ConfigWrapper(string key, string section, IConfigConverter<T> converter, T @default = default(T))
|
|
|
+ : this(key, converter.ConvertFromString, converter.ConvertToString, @default)
|
|
|
+ {
|
|
|
+ Section = section;
|
|
|
}
|
|
|
|
|
|
- protected virtual void SetValue(T value)
|
|
|
+ protected virtual bool GetKeyExists()
|
|
|
{
|
|
|
- if (_converter == null)
|
|
|
- _converter = TypeDescriptor.GetConverter(typeof(T));
|
|
|
+ return Config.HasEntry(Key, Section);
|
|
|
+ }
|
|
|
|
|
|
- var strVal = _converter.ConvertToString(value);
|
|
|
- Config.SetEntry(Key, strVal, Section);
|
|
|
+ protected virtual T GetValue()
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var strVal = Config.GetEntry(Key, _defaultStr, Section);
|
|
|
+ return _strToObj(strVal);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ BepInLogger.Log("ConfigWrapper Get Converter Exception: " + ex.Message);
|
|
|
+ return _default;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public static void RegisterTypeConverter<TC>() where TC : TypeConverter
|
|
|
+ protected virtual void SetValue(T value)
|
|
|
{
|
|
|
- TypeDescriptor.AddAttributes(typeof(T), new TypeConverterAttribute(typeof(TC)));
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var strVal = _objToStr(value);
|
|
|
+ Config.SetEntry(Key, strVal, Section);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ BepInLogger.Log("ConfigWrapper Set Converter Exception: " + ex.Message);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public void Clear()
|