健壮性优化
This commit is contained in:
1 parent
fe9fc04c20
commit
0a3b21505a
2 files changed
+11
-3
No files matched your search
@@ -44,13 +44,17 @@ public class JsonConfigProvider : IConfigProvider {
|
|||||||
if (value is null) {
|
if (value is null) {
|
||||||
// 直接在 JSON 中表示为 null
|
// 直接在 JSON 中表示为 null
|
||||||
json.Value[key] = JValue.CreateNull();
|
json.Value[key] = JValue.CreateNull();
|
||||||
|
Logger.Trace($"配置已修改:{key} = null({filePath})");
|
||||||
} else if (encrypted) {
|
} else if (encrypted) {
|
||||||
// 需要加密,在 JSON 中保存密文字符串
|
// 需要加密,在 JSON 中保存密文字符串
|
||||||
throw new NotImplementedException("加密需要改为使用识别码,现在尚未实现。");
|
throw new NotImplementedException("加密需要改为使用识别码,现在尚未实现。");
|
||||||
|
Logger.Trace($"配置已修改:{key} = 已加密({filePath})");
|
||||||
json.Value[key] = CryptographyUtils.AesEncrypt(value is string str ? str : JsonConvert.SerializeObject(value));
|
json.Value[key] = CryptographyUtils.AesEncrypt(value is string str ? str : JsonConvert.SerializeObject(value));
|
||||||
} else {
|
} else {
|
||||||
// 用 JToken 保留原始结构
|
// 用 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();
|
MakeDirty();
|
||||||
@@ -125,7 +129,7 @@ public static class ConfigUtils {
|
|||||||
/// 从 secret.json 中读取特定密钥,如果未找到对应密钥则抛出异常。
|
/// 从 secret.json 中读取特定密钥,如果未找到对应密钥则抛出异常。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static string GetSecret(string key)
|
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"));
|
private static readonly JsonConfigProvider secret = new(Path.Combine(Paths.AppData, "secret.json"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ public abstract class RedoableWorkerBase<TOut>(Func<CancellationToken?, Progress
|
|||||||
public TOut LastResult {
|
public TOut LastResult {
|
||||||
get {
|
get {
|
||||||
lock (this) {
|
lock (this) {
|
||||||
|
if (typeof(TOut) == typeof(NoType)) throw new InvalidOperationException("该 Worker 没有返回值。");
|
||||||
if (!hasSucceeded) throw new InvalidOperationException("从未成功完成过。");
|
if (!hasSucceeded) throw new InvalidOperationException("从未成功完成过。");
|
||||||
return lastResult == null ? default! : (TOut) lastResult;
|
return lastResult == null ? default! : (TOut) lastResult;
|
||||||
}
|
}
|
||||||
@@ -196,7 +197,7 @@ public class RedoableWorker<TOut> : RedoableWorkerBase<TOut> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public class RedoableWorker : RedoableWorkerBase<object?> {
|
public class RedoableWorker : RedoableWorkerBase<NoType?> {
|
||||||
public RedoableWorker(Action<CancellationToken?, ProgressProvider?> workload, [CallerMemberName] string creatorMemberName = "")
|
public RedoableWorker(Action<CancellationToken?, ProgressProvider?> workload, [CallerMemberName] string creatorMemberName = "")
|
||||||
: base((c, p) => { workload(c, p); return null; }, creatorMemberName: creatorMemberName) { }
|
: base((c, p) => { workload(c, p); return null; }, creatorMemberName: creatorMemberName) { }
|
||||||
public RedoableWorker(Action<CancellationToken?> workload, [CallerMemberName] string 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 = "")
|
public RedoableWorker(Action workload, [CallerMemberName] string creatorMemberName = "")
|
||||||
: base((_, _) => { workload(); return null; }, creatorMemberName: creatorMemberName) { }
|
: base((_, _) => { workload(); return null; }, creatorMemberName: creatorMemberName) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>指示没有该类型,用于泛型参数。</summary>
|
||||||
|
public class NoType { }
|
||||||
Reference in new issue
Block a user