diff --git a/src/AtlFieldExtractor/AtlFieldExtractor.csproj b/src/AtlFieldExtractor/AtlFieldExtractor.csproj index b93a434..30d11b9 100755 --- a/src/AtlFieldExtractor/AtlFieldExtractor.csproj +++ b/src/AtlFieldExtractor/AtlFieldExtractor.csproj @@ -8,6 +8,7 @@ + diff --git a/src/Core.Tests/Core.Tests.csproj b/src/Core.Tests/Core.Tests.csproj index 9daf4aa..07a72ed 100755 --- a/src/Core.Tests/Core.Tests.csproj +++ b/src/Core.Tests/Core.Tests.csproj @@ -13,6 +13,7 @@ + diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 147373c..dc4b4cb 100755 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -8,6 +8,7 @@ + diff --git a/src/Core/Preferences.cs b/src/Core/Preferences.cs new file mode 100644 index 0000000..c73cfd2 --- /dev/null +++ b/src/Core/Preferences.cs @@ -0,0 +1,108 @@ +using Newtonsoft.Json; + +namespace OCES.Resonance.Core; + +/// +/// 应用偏好设置,单例模式,负责平台路径解析与 JSON 持久化。 +/// +public class Preferences +{ + private static readonly Lazy _instance = new(() => new Preferences()); + + /// 单例访问入口 + public static Preferences Instance + { + get { return _instance.Value; } + } + + /// 数据库文件存放目录 + public string DatabasePath { get; set; } = string.Empty; + + /// 偏好设置 JSON 文件的完整路径 + public string PreferencesPath { get; set; } = string.Empty; + + /// + /// 私有构造函数 —— 根据运行时平台设置 。 + /// JSON 反序列化时也会调用此构造函数,随后由 JSON 属性覆盖路径值。 + /// + [JsonConstructor] + Preferences() + { + if (OperatingSystem.IsMacOS()) + { + string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + DatabasePath = Path.Combine(home, "Library", "Application Support", "Resonance", "Databases"); + PreferencesPath = Path.Combine(home, "Library", "Preferences", "com.oces.Resonance.json"); + } + else if (OperatingSystem.IsWindows()) + { + string localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); + string roamingAppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); + DatabasePath = Path.Combine(localAppData, "OCES", "Resonance", "Databases"); + PreferencesPath = Path.Combine(roamingAppData, "OCES", "Resonance", "preferences.json"); + } + else if (OperatingSystem.IsLinux()) + { + string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + string xdgDataHome = Environment.GetEnvironmentVariable("XDG_DATA_HOME") + ?? Path.Combine(home, ".local", "share"); + string xdgConfigHome = Environment.GetEnvironmentVariable("XDG_CONFIG_HOME") + ?? Path.Combine(home, ".config"); + DatabasePath = Path.Combine(xdgDataHome, "Resonance", "Databases"); + PreferencesPath = Path.Combine(xdgConfigHome, "Resonance", "preferences.json"); + } + } + + /// + /// 将传入的 实例序列化为 JSON 文件,保存到 。 + /// + /// 待序列化的偏好设置实例 + /// 成功返回 true,失败返回 false + public bool Serialize(Preferences preferences) + { + try + { + string? directory = Path.GetDirectoryName(PreferencesPath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + { + Directory.CreateDirectory(directory); + } + + string json = JsonConvert.SerializeObject(preferences, Formatting.Indented); + File.WriteAllText(PreferencesPath, json); + return true; + } + catch + { + //调用房根据自己的情况,自己处理接到false该怎么办。 + return false; + } + } + + /// + /// 从指定路径读取 JSON 文件并反序列化为 实例。 + /// + /// JSON 文件的完整路径 + /// 反序列化成功返回 Preferences 实例,失败返回 null + public Preferences? Deserialize(string preferencesPath) + { + try + { + if (!File.Exists(preferencesPath)) + { + return null; + } + + string json = File.ReadAllText(preferencesPath); + JsonSerializerSettings settings = new() + { + ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor, + }; + return JsonConvert.DeserializeObject(json, settings); + } + catch + { + return null; + } + } +}