新增信号交互进程插件
This commit is contained in:
@@ -6,9 +6,18 @@ using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace StandardScene.Signal
|
||||
{
|
||||
/// <summary>信号插件 JSON 配置的路径解析、种子拷贝与读写。</summary>
|
||||
/// <summary>
|
||||
/// 信号插件三张 JSON 的路径解析与读写。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 只使用 SimpleLite 工作目录 <c>Config/Signal/*.json</c>,不读、不拷 <c>plugins/Config/Signal</c>。
|
||||
/// 文件不存在时 Load 返回空表;Save 会创建目录。
|
||||
/// 迷毂数据中心走同一套文件;保存后需在本进程点「重新加载配置」。
|
||||
/// 枚举按名字序列化(<see cref="StringEnumConverter"/>),与样例里 <c>"上线机构"</c> 一致。
|
||||
/// </remarks>
|
||||
public static class SignalConfigStore
|
||||
{
|
||||
/// <summary>保存时串行写盘,避免 CycleGUI 与迷毂 API 同时 Save 互相覆盖一半。</summary>
|
||||
private static readonly object FileLock = new object();
|
||||
|
||||
private static readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings
|
||||
@@ -19,8 +28,8 @@ namespace StandardScene.Signal
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// 相对路径依次尝试:工作目录、插件目录旁的 Config。
|
||||
/// 工作目录文件不存在时,若插件目录有样例则拷过去。
|
||||
/// 相对路径固定落到工作目录(SimpleLite.exe 所在目录)下的 <c>Config/Signal</c>。
|
||||
/// 已是绝对路径则原样返回。文件不存在也返回该路径,不回退插件目录。
|
||||
/// </summary>
|
||||
public static string Resolve(string configuredPath)
|
||||
{
|
||||
@@ -30,31 +39,12 @@ namespace StandardScene.Signal
|
||||
if (Path.IsPathRooted(configuredPath))
|
||||
return configuredPath;
|
||||
|
||||
var working = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configuredPath);
|
||||
if (File.Exists(working))
|
||||
return working;
|
||||
|
||||
var sample = Path.Combine(
|
||||
Path.GetDirectoryName(typeof(SignalConfigStore).Assembly.Location) ?? "",
|
||||
configuredPath);
|
||||
if (File.Exists(sample))
|
||||
{
|
||||
try
|
||||
{
|
||||
var dir = Path.GetDirectoryName(working);
|
||||
if (!string.IsNullOrEmpty(dir))
|
||||
Directory.CreateDirectory(dir);
|
||||
File.Copy(sample, working, overwrite: false);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return File.Exists(working) ? working : sample;
|
||||
}
|
||||
}
|
||||
|
||||
return working;
|
||||
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configuredPath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化为列表。文件不存在、空、或 JSON 损坏时返回空列表,不抛给启动流程。
|
||||
/// </summary>
|
||||
public static List<T> Load<T>(string path) where T : class
|
||||
{
|
||||
try
|
||||
@@ -72,6 +62,7 @@ namespace StandardScene.Signal
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>缩进写入;自动建目录。与 Load 共用枚举按名序列化。</summary>
|
||||
public static void Save<T>(string path, IReadOnlyList<T> items)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(items ?? Array.Empty<T>(), JsonSettings);
|
||||
|
||||
Reference in New Issue
Block a user