forked from project-redbud/FunGame-Core
parent
a72ccd5a54
commit
22371d431c
198
Api/Utility/PluginConfig.cs
Normal file
198
Api/Utility/PluginConfig.cs
Normal file
@ -0,0 +1,198 @@
|
|||||||
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
|
using Milimoe.FunGame.Core.Library.Constant;
|
||||||
|
|
||||||
|
namespace Milimoe.FunGame.Core.Api.Utility
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 简易的插件配置文件生成器<para/>
|
||||||
|
/// 仅支持部分基本类型(<see cref="long"/>, <see cref="decimal"/>, <see cref="string"/>, <see cref="bool"/>)及其数组(<see cref="List{T}">List<long>, List<decimal>, List<string>, List<bool></see>和<see cref="Array">long[], decimal[], string[], bool[]</see>)
|
||||||
|
/// <para/>文件会保存为:程序目录/configs/<see cref="PluginName"/>/<see cref="FileName"/>.json
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// 新建一个配置文件,文件会保存为:程序目录/configs/<see cref="PluginName"/>/<see cref="FileName"/>.json
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="plugin_name"></param>
|
||||||
|
/// <param name="file_name"></param>
|
||||||
|
public class PluginConfig(string plugin_name, string file_name) : Dictionary<string, object>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 插件的名称
|
||||||
|
/// </summary>
|
||||||
|
public string PluginName { get; set; } = plugin_name;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 配置文件的名称(后缀将是.json)
|
||||||
|
/// </summary>
|
||||||
|
public string FileName { get; set; } = file_name;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 使用索引器给指定key赋值,不存在key会新增
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public new object this[string key]
|
||||||
|
{
|
||||||
|
set => Add(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 如果保存了对象,请使用此方法转换
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="key"></param>
|
||||||
|
public void Parse<T>(string key)
|
||||||
|
{
|
||||||
|
if (TryGetValue(key, out object? value) && value != null)
|
||||||
|
{
|
||||||
|
T? instance = NetworkUtility.JsonDeserialize<T>(value.ToString() ?? "");
|
||||||
|
if (instance != null)
|
||||||
|
{
|
||||||
|
base[key] = instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 使用泛型获取指定key的value
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="key"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public T? Get<T>(string key)
|
||||||
|
{
|
||||||
|
if (TryGetValue(key, out object? value) && value != null)
|
||||||
|
{
|
||||||
|
return NetworkUtility.JsonDeserialize<T>(value.ToString() ?? "");
|
||||||
|
}
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 添加一个配置,如果已存在key会覆盖
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key"></param>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
public new void Add(string key, object value)
|
||||||
|
{
|
||||||
|
if (value != null)
|
||||||
|
{
|
||||||
|
if (TryGetValue(key, out _)) base[key] = value;
|
||||||
|
else base.Add(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 从配置文件中读取配置。
|
||||||
|
/// 注意:所有保存时为数组的对象都会变成<see cref="List{T}"/>对象,并且不支持<see cref="object"/>类型
|
||||||
|
/// </summary>
|
||||||
|
public void LoadConfig()
|
||||||
|
{
|
||||||
|
string dpath = $@"{AppDomain.CurrentDomain.BaseDirectory}configs/{PluginName}";
|
||||||
|
string fpath = $@"{dpath}/{FileName}.json";
|
||||||
|
if (!Directory.Exists(dpath))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(dpath);
|
||||||
|
}
|
||||||
|
if (File.Exists(fpath))
|
||||||
|
{
|
||||||
|
string json = File.ReadAllText(fpath, General.DefaultEncoding);
|
||||||
|
Dictionary<string, object> dict = NetworkUtility.JsonDeserialize<Dictionary<string, object>>(json) ?? [];
|
||||||
|
Clear();
|
||||||
|
foreach (string key in dict.Keys)
|
||||||
|
{
|
||||||
|
JsonElement obj = (JsonElement)dict[key];
|
||||||
|
AddValue(key, obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 将配置保存到配置文件。调用此方法会覆盖原有的.json,请注意备份
|
||||||
|
/// </summary>
|
||||||
|
public void SaveConfig()
|
||||||
|
{
|
||||||
|
string json = NetworkUtility.JsonSerialize((Dictionary<string, object>)this);
|
||||||
|
string dpath = $@"{AppDomain.CurrentDomain.BaseDirectory}configs/{PluginName}";
|
||||||
|
string fpath = $@"{dpath}/{FileName}.json";
|
||||||
|
if (!Directory.Exists(dpath))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(dpath);
|
||||||
|
}
|
||||||
|
using StreamWriter writer = new(fpath, false, Encoding.Unicode);
|
||||||
|
writer.WriteLine(json);
|
||||||
|
writer.Flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Json反序列化的方法
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key"></param>
|
||||||
|
/// <param name="obj"></param>
|
||||||
|
private void AddValue(string key, JsonElement obj)
|
||||||
|
{
|
||||||
|
switch (obj.ValueKind)
|
||||||
|
{
|
||||||
|
case JsonValueKind.Object:
|
||||||
|
base.Add(key, obj);
|
||||||
|
break;
|
||||||
|
case JsonValueKind.Number:
|
||||||
|
if (obj.ValueKind == JsonValueKind.Number && obj.TryGetInt64(out long longValue))
|
||||||
|
{
|
||||||
|
base.Add(key, longValue);
|
||||||
|
}
|
||||||
|
else if (obj.ValueKind == JsonValueKind.Number && obj.TryGetDecimal(out decimal decValue))
|
||||||
|
{
|
||||||
|
base.Add(key, decValue);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case JsonValueKind.String:
|
||||||
|
base.Add(key, obj.GetString() ?? "");
|
||||||
|
break;
|
||||||
|
case JsonValueKind.True:
|
||||||
|
case JsonValueKind.False:
|
||||||
|
base.Add(key, obj.GetBoolean());
|
||||||
|
break;
|
||||||
|
case JsonValueKind.Array:
|
||||||
|
AddValues(key, obj.EnumerateArray());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Json数组反序列化的方法。不支持<see cref="object"/>数组。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key"></param>
|
||||||
|
/// <param name="obj"></param>
|
||||||
|
private void AddValues(string key, JsonElement.ArrayEnumerator obj)
|
||||||
|
{
|
||||||
|
List<long> longList = [];
|
||||||
|
List<decimal> decList = [];
|
||||||
|
List<string> strList = [];
|
||||||
|
List<bool> bolList = [];
|
||||||
|
foreach (JsonElement array_e in obj)
|
||||||
|
{
|
||||||
|
if (array_e.ValueKind == JsonValueKind.Number && array_e.TryGetInt64(out long longValue))
|
||||||
|
{
|
||||||
|
longList.Add(longValue);
|
||||||
|
}
|
||||||
|
else if (array_e.ValueKind == JsonValueKind.Number && array_e.TryGetDecimal(out decimal decValue))
|
||||||
|
{
|
||||||
|
decList.Add(decValue);
|
||||||
|
}
|
||||||
|
else if (array_e.ValueKind == JsonValueKind.String)
|
||||||
|
{
|
||||||
|
strList.Add(array_e.GetString() ?? "");
|
||||||
|
}
|
||||||
|
else if (array_e.ValueKind == JsonValueKind.True || array_e.ValueKind == JsonValueKind.False)
|
||||||
|
{
|
||||||
|
bolList.Add(array_e.GetBoolean());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (longList.Count > 0) base.Add(key, longList);
|
||||||
|
if (decList.Count > 0) base.Add(key, decList);
|
||||||
|
if (strList.Count > 0) base.Add(key, strList);
|
||||||
|
if (bolList.Count > 0) base.Add(key, bolList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -780,6 +780,94 @@
|
|||||||
Private JsonSerializerOptions
|
Private JsonSerializerOptions
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="T:Milimoe.FunGame.Core.Api.Utility.PluginConfig">
|
||||||
|
<summary>
|
||||||
|
简易的插件配置文件生成器<para/>
|
||||||
|
仅支持部分基本类型(<see cref="T:System.Int64"/>, <see cref="T:System.Decimal"/>, <see cref="T:System.String"/>, <see cref="T:System.Boolean"/>)及其数组(<see cref="T:System.Collections.Generic.List`1">List<long>, List<decimal>, List<string>, List<bool></see>和<see cref="T:System.Array">long[], decimal[], string[], bool[]</see>)
|
||||||
|
<para/>文件会保存为:程序目录/configs/<see cref="P:Milimoe.FunGame.Core.Api.Utility.PluginConfig.PluginName"/>/<see cref="P:Milimoe.FunGame.Core.Api.Utility.PluginConfig.FileName"/>.json
|
||||||
|
</summary>
|
||||||
|
<remarks>
|
||||||
|
新建一个配置文件,文件会保存为:程序目录/configs/<see cref="P:Milimoe.FunGame.Core.Api.Utility.PluginConfig.PluginName"/>/<see cref="P:Milimoe.FunGame.Core.Api.Utility.PluginConfig.FileName"/>.json
|
||||||
|
</remarks>
|
||||||
|
<param name="plugin_name"></param>
|
||||||
|
<param name="file_name"></param>
|
||||||
|
</member>
|
||||||
|
<member name="M:Milimoe.FunGame.Core.Api.Utility.PluginConfig.#ctor(System.String,System.String)">
|
||||||
|
<summary>
|
||||||
|
简易的插件配置文件生成器<para/>
|
||||||
|
仅支持部分基本类型(<see cref="T:System.Int64"/>, <see cref="T:System.Decimal"/>, <see cref="T:System.String"/>, <see cref="T:System.Boolean"/>)及其数组(<see cref="T:System.Collections.Generic.List`1">List<long>, List<decimal>, List<string>, List<bool></see>和<see cref="T:System.Array">long[], decimal[], string[], bool[]</see>)
|
||||||
|
<para/>文件会保存为:程序目录/configs/<see cref="P:Milimoe.FunGame.Core.Api.Utility.PluginConfig.PluginName"/>/<see cref="P:Milimoe.FunGame.Core.Api.Utility.PluginConfig.FileName"/>.json
|
||||||
|
</summary>
|
||||||
|
<remarks>
|
||||||
|
新建一个配置文件,文件会保存为:程序目录/configs/<see cref="P:Milimoe.FunGame.Core.Api.Utility.PluginConfig.PluginName"/>/<see cref="P:Milimoe.FunGame.Core.Api.Utility.PluginConfig.FileName"/>.json
|
||||||
|
</remarks>
|
||||||
|
<param name="plugin_name"></param>
|
||||||
|
<param name="file_name"></param>
|
||||||
|
</member>
|
||||||
|
<member name="P:Milimoe.FunGame.Core.Api.Utility.PluginConfig.PluginName">
|
||||||
|
<summary>
|
||||||
|
插件的名称
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="P:Milimoe.FunGame.Core.Api.Utility.PluginConfig.FileName">
|
||||||
|
<summary>
|
||||||
|
配置文件的名称(后缀将是.json)
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="P:Milimoe.FunGame.Core.Api.Utility.PluginConfig.Item(System.String)">
|
||||||
|
<summary>
|
||||||
|
使用索引器给指定key赋值,不存在key会新增
|
||||||
|
</summary>
|
||||||
|
<param name="key"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:Milimoe.FunGame.Core.Api.Utility.PluginConfig.Parse``1(System.String)">
|
||||||
|
<summary>
|
||||||
|
如果保存了对象,请使用此方法转换
|
||||||
|
</summary>
|
||||||
|
<typeparam name="T"></typeparam>
|
||||||
|
<param name="key"></param>
|
||||||
|
</member>
|
||||||
|
<member name="M:Milimoe.FunGame.Core.Api.Utility.PluginConfig.Get``1(System.String)">
|
||||||
|
<summary>
|
||||||
|
使用泛型获取指定key的value
|
||||||
|
</summary>
|
||||||
|
<typeparam name="T"></typeparam>
|
||||||
|
<param name="key"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:Milimoe.FunGame.Core.Api.Utility.PluginConfig.Add(System.String,System.Object)">
|
||||||
|
<summary>
|
||||||
|
添加一个配置,如果已存在key会覆盖
|
||||||
|
</summary>
|
||||||
|
<param name="key"></param>
|
||||||
|
<param name="value"></param>
|
||||||
|
</member>
|
||||||
|
<member name="M:Milimoe.FunGame.Core.Api.Utility.PluginConfig.LoadConfig">
|
||||||
|
<summary>
|
||||||
|
从配置文件中读取配置。
|
||||||
|
注意:所有保存时为数组的对象都会变成<see cref="T:System.Collections.Generic.List`1"/>对象,并且不支持<see cref="T:System.Object"/>类型
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="M:Milimoe.FunGame.Core.Api.Utility.PluginConfig.SaveConfig">
|
||||||
|
<summary>
|
||||||
|
将配置保存到配置文件。调用此方法会覆盖原有的.json,请注意备份
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="M:Milimoe.FunGame.Core.Api.Utility.PluginConfig.AddValue(System.String,System.Text.Json.JsonElement)">
|
||||||
|
<summary>
|
||||||
|
Json反序列化的方法
|
||||||
|
</summary>
|
||||||
|
<param name="key"></param>
|
||||||
|
<param name="obj"></param>
|
||||||
|
</member>
|
||||||
|
<member name="M:Milimoe.FunGame.Core.Api.Utility.PluginConfig.AddValues(System.String,System.Text.Json.JsonElement.ArrayEnumerator)">
|
||||||
|
<summary>
|
||||||
|
Json数组反序列化的方法。不支持<see cref="T:System.Object"/>数组。
|
||||||
|
</summary>
|
||||||
|
<param name="key"></param>
|
||||||
|
<param name="obj"></param>
|
||||||
|
</member>
|
||||||
<member name="P:Milimoe.FunGame.Core.Api.Utility.PluginLoader.Plugins">
|
<member name="P:Milimoe.FunGame.Core.Api.Utility.PluginLoader.Plugins">
|
||||||
<summary>
|
<summary>
|
||||||
已读取的插件列表
|
已读取的插件列表
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user