feat(desktop): 保存配置时不再重写整个 config.yaml

桌面端从共享的 DI 容器拿配置对象,再用 YamlDotNet 的序列化器整份写回。这样
一轮往返会把文件重写一遍:仓库里那 60 行、注释众多的 config.yaml 会变成 40
行机器生成的块,注释全没了,多行值的引号风格也变了。只要在设置页动一个开关,
注释就丢了。

SettingsStore 不再重新序列化整个文档,只补丁 UI 真正拥有的那几个键:

- PatchText 按行扫描 YAML,用缩进栈跟踪每个受管键的路径
  (networkOptions.isEnable、provider.lyric.plugin[NetEase].priority …),
  只替换那个标量,其余每一个字节 —— 注释、空行、列对齐 —— 保持原样。带
  引号的值保持原引号风格(QuotedScalarEventEmitter 沿用原文样式)。
- 补丁后的文本会被重新解析,并逐字段与预期值比对,通过才落盘。往返不一致
  就拒绝写入,磁盘上的文件保持不动。另有重新生成整份文件的兜底路径,用于
  原文件已被替换成补丁器无法跟随的内容时。

同一提交里还有:

- AppPaths:可执行文件所在目录可写时配置就放那里(便携安装),否则回退到
  %AppData%/ZonyLrcTools。写入走临时文件 + File.Move,崩溃不会留下半份配置。
- NetEaseSessionStore / KugouDatabaseStore:两个小 JSON,分别存网易云登录
  cookie 和用户手动指定的 KGMusicV3.db 路径。刻意与 config.yaml 分开 ——
  它们是机器状态而不是用户偏好,也不该出现在设置的 diff 里。
- ConfigurationServiceCollectionExtensions:构造与 CLI 相同的 GlobalOptions,
  但基于上面解析出的路径,而不是固定的 AppContext.BaseDirectory。

Common 为此一行未改:CLI 照旧从自己所在目录读 config.yaml。
This commit is contained in:
aL1CesT committed 2026-11-09 04:27:42 +08:00
1 parent 4ec67b9ee0
commit 9cda87dbfe
7 files changed
+972

No files matched your search

@@ -0,0 +1,39 @@
using ZonyLrcTools.Common.Configuration;
namespace ZonyLrcTools.Desktop.Infrastructure.Configuration;
/// <summary>
/// Result of a settings persist operation.
/// </summary>
/// <param name="IsSuccess">Whether the settings were written to disk.</param>
/// <param name="FilePath">The file that was written, when the write succeeded.</param>
/// <param name="ErrorMessage">Human readable reason when the write failed.</param>
public readonly record struct SettingsSaveResult(bool IsSuccess, string? FilePath, string? ErrorMessage)
{
public static SettingsSaveResult Success(string filePath) => new(true, filePath, null);
public static SettingsSaveResult Failure(string errorMessage) => new(false, null, errorMessage);
}
/// <summary>
/// Single owner of the settings file: reads the bound <see cref="GlobalOptions"/> and writes it back
/// to <see cref="AppPaths.ConfigFilePath"/>.
/// </summary>
public interface ISettingsStore
{
/// <summary>
/// The live options instance shared by the whole application. Mutating it takes effect
/// immediately for components that read it per operation.
/// </summary>
GlobalOptions Options { get; }
/// <summary>
/// Absolute path of the settings file currently in use.
/// </summary>
string ConfigFilePath { get; }
/// <summary>
/// Serialize <see cref="Options"/> to YAML and replace the settings file atomically.
/// </summary>
Task<SettingsSaveResult> SaveAsync(CancellationToken cancellationToken = default);
}