From a42f33a8c1100611dc96d81086dc50bb0d316770 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com>
Date: Fri, 19 Jun 2026 01:23:31 +0800
Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E7=9A=84=20Config=20?=
=?UTF-8?q?=E5=AE=9E=E7=8E=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Shared/General/ConcurrentList.cs | 1 +
Shared/General/Config.cs | 57 ++++++++++++++++++++++++++++++++
2 files changed, 58 insertions(+)
create mode 100644 Shared/General/Config.cs
diff --git a/Shared/General/ConcurrentList.cs b/Shared/General/ConcurrentList.cs
index 01eca4d..371fdc2 100644
--- a/Shared/General/ConcurrentList.cs
+++ b/Shared/General/ConcurrentList.cs
@@ -2,6 +2,7 @@
///
/// 使用粗粒度锁的线程安全 实现。
+/// 通常建议使用 或其他线程安全集合,它们具有更好的性能。
///
public class ConcurrentList : IList, IList, IEnumerable, IEnumerable {
diff --git a/Shared/General/Config.cs b/Shared/General/Config.cs
new file mode 100644
index 0000000..f8ae7fb
--- /dev/null
+++ b/Shared/General/Config.cs
@@ -0,0 +1,57 @@
+namespace MeloongCore;
+
+public interface IConfigProvider {
+ void Write(string key, JToken value);
+ JToken? Read(string key, JToken? defaultValue = null);
+ bool HasValue(string key);
+ void Remove(string key);
+ void Save();
+}
+public class JsonConfigProvider(string filePath) : IConfigProvider {
+ public readonly Lazy Content = new(() => {
+ try {
+ return FileUtils.Exists(filePath) ? ((JObject?) FileUtils.ReadAsJson(filePath) ?? []) : [];
+ } catch (Exception ex) {
+ Logger.Error(ex, $"读取配置文件失败:{filePath}", LogBehavior.Toast);
+ return [];
+ }
+ });
+ public void Write(string key, JToken value)
+ => Content.Value[key] = value;
+ public JToken? Read(string key, JToken? defaultValue = null)
+ => Content.Value.TryGetValue(key, out var value) ? value : defaultValue;
+ public bool HasValue(string key)
+ => Content.Value.ContainsKey(key);
+ public void Remove(string key)
+ => Content.Value.Remove(key);
+ public void Save()
+ => FileUtils.Write(filePath, Content.Value.ToString(Formatting.Indented));
+}
+public static class Configs {
+ public static readonly JsonConfigProvider AppData = new(Path.Combine(Paths.AppDataThenName, "config.json"));
+
+ ///
+ /// 从 secret.json 中读取特定密钥,如果不存在则抛出异常。
+ ///
+ public static string GetSecret(string key) => secret.Read(key)?.ToString() ?? throw new KeyNotFoundException($"Secret not found: {key}");
+ private static readonly JsonConfigProvider secret = new(Path.Combine(Paths.AppData, "secret.json"));
+}
+
+public class ConfigEntry(string key, T? defaultValue, IConfigProvider? defaultProvider = null, bool encrypted = false) {
+ private readonly IConfigProvider defaultProvider = defaultProvider ?? Configs.AppData;
+ public string Key = key;
+ public bool Encrypted = encrypted;
+ public T? DefaultValue = defaultValue;
+
+ ///
+ /// 当设置项的值实际被改变时触发,参数为新值。
+ ///
+ public event Action? OnChanged;
+
+ public bool HasValue(IConfigProvider? provider = null)
+ => (provider ?? defaultProvider).HasValue(Key);
+ public void Remove(IConfigProvider? provider = null)
+ => (provider ?? defaultProvider).Remove(Key);
+ public void Save(IConfigProvider? provider = null)
+ => (provider ?? defaultProvider).Save();
+}
\ No newline at end of file