diff --git a/README.md b/README.md index 5d2a69e..3d57afb 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ pwsh -File tools/Publish-Avalonia.ps1 `--import-recipes 原数据根目录` 可以导入已有配方,`--check-standalone` / `--check-runtime` 检查本地手部模型和使用模拟 AI 的步骤门控,`--check-gpu` 额外要求手部推理使用 GPU;这些本地诊断不代表真实 D3 接口联调成功。 -主界面每个步骤显示正常次数和异常次数:正常完成计入正常,异常跳过计入异常;按配方累计本次运行的结果,复位、停止或重新应用配方不会清零,重启软件后重新统计。 +主界面每个步骤显示正常次数和异常次数:正常完成计入正常,异常跳过计入异常;按配方保存在 `workspace/step-counts.json`,复位、停止、重新应用配方或重启软件均保留;仅点击“重置步骤”后的“重置计数”按钮,才会清零当前已应用配方的计数,不影响步骤进度及其他配方。 ## 验证 diff --git a/src/SopHelper.Avalonia/Models/StepCountsStore.cs b/src/SopHelper.Avalonia/Models/StepCountsStore.cs new file mode 100644 index 0000000..61f170a --- /dev/null +++ b/src/SopHelper.Avalonia/Models/StepCountsStore.cs @@ -0,0 +1,51 @@ +using System.IO; +using System.Text.Json; +using SopHelper.Engine.Sop; +using SopHelper.Presentation; + +namespace SopHelper.Models; + +/// 按配方持久保存步骤累计结果,独立于运行参数和配方编辑。 +public sealed class StepCountsStore(string root) : IStepCountStore +{ + private static readonly object FileGate = new(); + public static StepCountsStore Current => new(DesktopPlatform.WorkspaceRoot); + public string FilePath => Path.Combine(root, "workspace", "step-counts.json"); + + private Dictionary> Read() + { + if (!File.Exists(FilePath)) return new(StringComparer.OrdinalIgnoreCase); + var all = JsonSerializer.Deserialize>>(File.ReadAllText(FilePath)) + ?? throw new InvalidDataException("累计次数文件内容为空"); + if (all.Values.Any(steps => steps is null || steps.Any(pair => pair.Key <= 0 + || pair.Value.NormalCount < 0 || pair.Value.AbnormalCount < 0))) + throw new InvalidDataException("累计次数文件包含无效步骤或负数"); + return new(all, StringComparer.OrdinalIgnoreCase); + } + + public IReadOnlyDictionary Load(string recipeName) + { + lock (FileGate) return Read().GetValueOrDefault(recipeName) ?? []; + } + + public void Save(string recipeName, IReadOnlyDictionary counts) + { + lock (FileGate) + { + var all = Read(); + all[recipeName] = new(counts); + Directory.CreateDirectory(Path.GetDirectoryName(FilePath)!); + var temporary = FilePath + ".tmp"; + try + { + using (var stream = new FileStream(temporary, FileMode.Create, FileAccess.Write, FileShare.None)) + { + JsonSerializer.Serialize(stream, all, new JsonSerializerOptions { WriteIndented = true }); + stream.Flush(flushToDisk: true); + } + File.Move(temporary, FilePath, overwrite: true); + } + finally { if (File.Exists(temporary)) File.Delete(temporary); } + } + } +} diff --git a/src/SopHelper.Avalonia/VVMs/SopPage/SopPage.axaml b/src/SopHelper.Avalonia/VVMs/SopPage/SopPage.axaml index c3a2adc..627bfc2 100644 --- a/src/SopHelper.Avalonia/VVMs/SopPage/SopPage.axaml +++ b/src/SopHelper.Avalonia/VVMs/SopPage/SopPage.axaml @@ -94,6 +94,7 @@