using Milimoe.FunGame.Core.Entity; using Milimoe.FunGame.Core.Library.Constant; namespace Milimoe.FunGame.Core.Api.Utility { /// /// 简易的实体模组配置文件生成器,适用范围:动态扩展技能和物品、保存玩家的存档 /// 仅支持继承了 的实体类型,每个 仅保存一种实体类型的数据 /// 文件会保存为:程序目录/configs//.json /// /// /// 新建一个配置文件,文件会保存为:程序目录/configs//.json /// /// /// public class EntityModuleConfig(string module_name, string file_name) : Dictionary where T : BaseEntity { /// /// 模组的名称 /// public string ModuleName { get; set; } = module_name; /// /// 配置文件的名称(后缀将是.json) /// public string FileName { get; set; } = file_name; /// /// 使用索引器给指定key赋值,不存在key会新增 /// /// /// public new T this[string key] { get => base[key]; set { if (value != null) Add(key, value); } } /// /// 获取指定key的value /// /// /// public T? Get(string key) { if (TryGetValue(key, out T? value) && value != null) { return value; } return null; } /// /// 添加一个配置,如果已存在key会覆盖 /// /// /// public new void Add(string key, T value) { if (value != null) { if (TryGetValue(key, out _)) base[key] = value; else base.Add(key, value); } } /// /// 从配置文件中读取配置。 /// public void LoadConfig() { string dpath = $@"{AppDomain.CurrentDomain.BaseDirectory}configs/{ModuleName}"; string fpath = $@"{dpath}/{FileName}.json"; if (Directory.Exists(dpath) && File.Exists(fpath)) { string json = File.ReadAllText(fpath, General.DefaultEncoding); Dictionary dict = NetworkUtility.JsonDeserialize>(json) ?? []; Clear(); foreach (string key in dict.Keys) { T obj = dict[key]; base.Add(key, obj); } } } /// /// 将配置保存到配置文件。调用此方法会覆盖原有的.json,请注意备份 /// public void SaveConfig() { string json = NetworkUtility.JsonSerialize((Dictionary)this); string dpath = $@"{AppDomain.CurrentDomain.BaseDirectory}configs/{ModuleName}"; string fpath = $@"{dpath}/{FileName}.json"; if (!Directory.Exists(dpath)) { Directory.CreateDirectory(dpath); } using StreamWriter writer = new(fpath, false, General.DefaultEncoding); writer.WriteLine(json); writer.Flush(); } } }