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);
+ }
}
}