using System.Text.Json;
using Milimoe.FunGame.Core.Library.Constant;
namespace Milimoe.FunGame.Core.Api.Utility
{
///
/// 简易的插件配置文件生成器
/// 仅支持部分基本类型(, , , , )及其数组(List<long>, List<double>, List<decimal>, List<string>, List<bool>和long[], double[], decimal[], string[], bool[])
/// 文件会保存为:程序目录/(通常是 configs)//.json
///
///
/// 新建一个配置文件,文件会保存为:程序目录/(通常是 configs)//.json
///
///
///
public class PluginConfig(string plugin_name, string file_name) : Dictionary
{
///
/// 配置文件存放的根目录
///
public static string RootPath { get; set; } = "configs";
///
/// 插件的名称
///
public string PluginName { get; set; } = plugin_name;
///
/// 配置文件的名称(后缀将是.json)
///
public string FileName { get; set; } = file_name;
///
/// 使用索引器给指定key赋值,不存在key会新增
///
///
///
public new object this[string key]
{
get => base[key];
set
{
if (value != null) Add(key, value);
}
}
///
/// 如果保存了对象,请使用此方法转换
///
///
///
public void Parse(string key)
{
if (TryGetValue(key, out object? value) && value != null)
{
T? instance = NetworkUtility.JsonDeserialize(value.ToString() ?? "");
if (instance != null)
{
base[key] = instance;
}
}
}
///
/// 获取指定key的value
///
///
///
public object? GetValue(string key)
{
if (base.TryGetValue(key, out object? value) && value != null)
{
return value;
}
return null;
}
///
/// 使用泛型获取指定key的value
///
///
///
///
public T? Get(string key)
{
if (TryGetValue(key, out object? value) && value != null)
{
if (value is T typeValue)
{
return typeValue;
}
if (value is List