From 0ace8d9dc9e7eb7895f358c12bb93faeebc7b9c3 Mon Sep 17 00:00:00 2001 From: milimoe Date: Sat, 15 Feb 2025 17:18:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BB=8E=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E5=8A=A0=E8=BD=BD=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Api/Utility/NovelConfig.cs | 103 ++++++++++++++++++++++++++++++++++--- 1 file changed, 95 insertions(+), 8 deletions(-) diff --git a/Api/Utility/NovelConfig.cs b/Api/Utility/NovelConfig.cs index 16f6786..ac20278 100644 --- a/Api/Utility/NovelConfig.cs +++ b/Api/Utility/NovelConfig.cs @@ -71,11 +71,58 @@ namespace Milimoe.FunGame.Core.Api.Utility } } + /// + /// 从指定路径加载配置文件,并根据其文件名,转换为本框架所需的文件 + /// 需要注意: 用于检查加载的文件名是否在配置文件目录中已经存在 + /// 如果不使用此检查,使用 时可能会覆盖原有文件(程序目录/(通常是 novels)//[所选的文件名].json) + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static NovelConfig LoadFrom(string path, string novelName, bool checkConflict = true, Dictionary>? predicates = null) + { + if (!File.Exists(path)) + { + throw new FileNotFoundException($"找不到文件:{path}"); + } + + string fileName = Path.GetFileNameWithoutExtension(path); + string dpath = $@"{AppDomain.CurrentDomain.BaseDirectory}{RootPath}/{novelName}"; + string fpath = $@"{dpath}/{fileName}.json"; + + if (checkConflict && File.Exists(fpath)) + { + throw new InvalidOperationException($"文件 {fileName}.json 已存在,请先重命名。"); + } + + // 确保目录存在 + ExistsDirectoryAndCreate(novelName); + + // 复制文件内容 + string json = File.ReadAllText(path, General.DefaultEncoding); + if (NetworkUtility.JsonDeserialize>(json) is null) + { + throw new InvalidDataException($"文件 {path} 内容为空或格式不正确。"); + } + File.WriteAllText(fpath, json, General.DefaultEncoding); + + // 从新文件加载配置 + NovelConfig config = new(novelName, fileName); + config.LoadConfig(predicates); + + return config; + } + /// /// 从配置文件中读取配置。 /// - /// 传入定义好的条件字典 - public void LoadConfig(Dictionary>? Predicates = null) + /// 传入定义好的条件字典 + public void LoadConfig(Dictionary>? predicates = null) { string dpath = $@"{AppDomain.CurrentDomain.BaseDirectory}{RootPath}/{NovelName}"; string fpath = $@"{dpath}/{FileName}.json"; @@ -102,13 +149,13 @@ namespace Milimoe.FunGame.Core.Api.Utility } } } - if (Predicates != null) + if (predicates != null) { if (obj.Values.TryGetValue(nameof(NovelNode.AndPredicates), out object? value2) && value2 is List aps) { foreach (string ap in aps) { - if (Predicates.TryGetValue(ap, out Func? value3) && value3 != null) + if (predicates.TryGetValue(ap, out Func? value3) && value3 != null) { obj.AndPredicates[ap] = value3; } @@ -118,7 +165,7 @@ namespace Milimoe.FunGame.Core.Api.Utility { foreach (string op in ops) { - if (Predicates.TryGetValue(op, out Func? value3) && value3 != null) + if (predicates.TryGetValue(op, out Func? value3) && value3 != null) { obj.OrPredicates[op] = value3; } @@ -137,13 +184,13 @@ namespace Milimoe.FunGame.Core.Api.Utility } } } - if (Predicates != null) + if (predicates != null) { if (option.Values.TryGetValue(nameof(NovelNode.AndPredicates), out object? value3) && value3 is List aps) { foreach (string ap in aps) { - if (Predicates.TryGetValue(ap, out Func? value4) && value4 != null) + if (predicates.TryGetValue(ap, out Func? value4) && value4 != null) { option.AndPredicates[ap] = value4; } @@ -153,7 +200,7 @@ namespace Milimoe.FunGame.Core.Api.Utility { foreach (string op in ops) { - if (Predicates.TryGetValue(op, out Func? value4) && value4 != null) + if (predicates.TryGetValue(op, out Func? value4) && value4 != null) { option.OrPredicates[op] = value4; } @@ -181,5 +228,45 @@ namespace Milimoe.FunGame.Core.Api.Utility writer.WriteLine(json); writer.Flush(); } + + /// + /// 检查配置文件目录是否存在 + /// + /// + /// + public static bool ExistsDirectory(string novelName) + { + string dpath = $@"{AppDomain.CurrentDomain.BaseDirectory}{RootPath}/{novelName}"; + return Directory.Exists(dpath); + } + + /// + /// 检查配置文件目录是否存在,不存在则创建 + /// + /// + /// + public static bool ExistsDirectoryAndCreate(string novelName) + { + string dpath = $@"{AppDomain.CurrentDomain.BaseDirectory}{RootPath}/{novelName}"; + bool result = Directory.Exists(dpath); + if (!result) + { + Directory.CreateDirectory(dpath); + } + return result; + } + + /// + /// 检查配置文件目录中是否存在指定文件 + /// + /// + /// + /// + public static bool ExistsFile(string novelName, string fileName) + { + string dpath = $@"{AppDomain.CurrentDomain.BaseDirectory}{RootPath}/{novelName}"; + string fpath = $@"{dpath}/{fileName}.json"; + return File.Exists(fpath); + } } }