Merge pull request #12 from usagirei/config-wrapper

Config Wrapper Class - Handles Automatic Type Conversion
This commit is contained in:
Bepis authored and GitHub committed 2018-03-02 13:52:09 +11:00
commit 25cc3609ab
2 files changed
+79

No files matched your search

+1
View File
@@ -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
View File
@@ -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);
}
}
}