健壮性优化

This commit is contained in:
龙腾猫跃 committed 2026-06-28 23:57:37 +08:00
1 parent fe9fc04c20
commit 0a3b21505a
2 files changed
+11 -3

No files matched your search

+6 -2
View File
@@ -44,13 +44,17 @@ public class JsonConfigProvider : IConfigProvider {
if (value is null) {
// 直接在 JSON 中表示为 null
json.Value[key] = JValue.CreateNull();
Logger.Trace($"配置已修改:{key} = null({filePath})");
} else if (encrypted) {
// 需要加密,在 JSON 中保存密文字符串
throw new NotImplementedException("加密需要改为使用识别码,现在尚未实现。");
Logger.Trace($"配置已修改:{key} = 已加密({filePath})");
json.Value[key] = CryptographyUtils.AesEncrypt(value is string str ? str : JsonConvert.SerializeObject(value));
} else {
// 用 JToken 保留原始结构
json.Value[key] = JToken.FromObject(value);
JToken token = JToken.FromObject(value);
json.Value[key] = token;
Logger.Trace($"配置已修改:{key} = {token.ToString(Formatting.None)}({filePath})");
}
}
MakeDirty();
@@ -125,7 +129,7 @@ public static class ConfigUtils {
/// 从 secret.json 中读取特定密钥,如果未找到对应密钥则抛出异常。
/// </summary>
public static string GetSecret(string key)
=> secret.Read<string>(key, null, false) ?? throw new KeyNotFoundException($"Secret not found: {key}");
=> secret.Read<string>(key, null, false) ?? throw new KeyNotFoundException($"未找到密钥:{key}");
private static readonly JsonConfigProvider secret = new(Path.Combine(Paths.AppData, "secret.json"));
}
+5 -1
View File
@@ -68,6 +68,7 @@ public abstract class RedoableWorkerBase<TOut>(Func<CancellationToken?, Progress
public TOut LastResult {
get {
lock (this) {
if (typeof(TOut) == typeof(NoType)) throw new InvalidOperationException("该 Worker 没有返回值。");
if (!hasSucceeded) throw new InvalidOperationException("从未成功完成过。");
return lastResult == null ? default! : (TOut) lastResult;
}
@@ -196,7 +197,7 @@ public class RedoableWorker<TOut> : RedoableWorkerBase<TOut> {
}
/// <inheritdoc />
public class RedoableWorker : RedoableWorkerBase<object?> {
public class RedoableWorker : RedoableWorkerBase<NoType?> {
public RedoableWorker(Action<CancellationToken?, ProgressProvider?> workload, [CallerMemberName] string creatorMemberName = "")
: base((c, p) => { workload(c, p); return null; }, creatorMemberName: creatorMemberName) { }
public RedoableWorker(Action<CancellationToken?> workload, [CallerMemberName] string creatorMemberName = "")
@@ -204,3 +205,6 @@ public class RedoableWorker : RedoableWorkerBase<object?> {
public RedoableWorker(Action workload, [CallerMemberName] string creatorMemberName = "")
: base((_, _) => { workload(); return null; }, creatorMemberName: creatorMemberName) { }
}
/// <summary>指示没有该类型,用于泛型参数。</summary>
public class NoType { }