mirror of
https://github.com/BepInEx/BepInEx.git
synced 2026-10-20 01:46:13 +00:00
Merge branch 'master' of https://github.com/bbepis/BepInEx
This commit is contained in:
commit
4dbedd98a1
2 files changed
+79
No files matched your search
@@ -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" />
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user