using Newtonsoft.Json; namespace OCES.Resonance.Core; /// /// 偏好设置管理器,负责平台路径解析、JSON 持久化以及 实例的生命周期管理。 /// public static class PreferencesManager { /// 当前活动的偏好设置实例, 调用前为 null public static Preferences? CurrentPreferences { get; set; } /// /// 加载偏好设置。先尝试从硬盘反序列化;若文件不存在则创建带平台默认路径的新实例。 /// public static void Load() { string preferencesPath = GetDefaultPreferencesPath(); // 尝试从硬盘加载 Preferences? loaded = Deserialize(preferencesPath); if (loaded != null) { CurrentPreferences = loaded; return; } // 文件不存在或反序列化失败 → 创建默认实例 CurrentPreferences = new Preferences { DatabasePath = GetDefaultDatabasePath(), }; // 立即落盘,确保下次启动能直接读取 Serialize(CurrentPreferences); } /// /// 将当前 序列化到硬盘。 /// public static void Save() { if (CurrentPreferences == null) return; Serialize(CurrentPreferences); } /// /// 根据运行时平台返回数据库文件存放目录。 /// public static string GetDefaultDatabasePath() { if (OperatingSystem.IsMacOS()) { string applicationSupport = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); DirectoryInfo databasePath = new(Path.Combine(applicationSupport, "OCES", "Resonance", "Databases")); if (!databasePath.Exists) Directory.CreateDirectory(databasePath.FullName); return databasePath.FullName; } if (OperatingSystem.IsWindows()) { string localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); DirectoryInfo databasePath = new(Path.Combine(localAppData, "OCES", "Resonance", "Databases")); if (!databasePath.Exists) Directory.CreateDirectory(databasePath.FullName); return databasePath.FullName; } if (OperatingSystem.IsLinux()) { string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); string xdgDataHome = Environment.GetEnvironmentVariable("XDG_DATA_HOME") ?? Path.Combine(home, ".local", "share"); DirectoryInfo databasePath = new(Path.Combine(xdgDataHome, "Resonance", "Databases")); if (!databasePath.Exists) Directory.CreateDirectory(databasePath.FullName); return databasePath.FullName; } return string.Empty; } /// /// 根据运行时平台返回偏好设置 JSON 文件的完整路径。 /// public static string GetDefaultPreferencesPath() { if (OperatingSystem.IsMacOS()) { string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); return Path.Combine(home, "Library", "Preferences", "com.oces.Resonance.json"); } if (OperatingSystem.IsWindows()) { string roamingAppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); return Path.Combine(roamingAppData, "OCES", "Resonance", "preferences.json"); } if (OperatingSystem.IsLinux()) { string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); string xdgConfigHome = Environment.GetEnvironmentVariable("XDG_CONFIG_HOME") ?? Path.Combine(home, ".config"); return Path.Combine(xdgConfigHome, "Resonance", "preferences.json"); } return string.Empty; } /// /// 将 实例序列化为 JSON 文件,保存到平台默认偏好设置路径。 /// /// 成功返回 true,失败返回 false static bool Serialize(Preferences preferences) { try { string path = GetDefaultPreferencesPath(); string? directory = Path.GetDirectoryName(path); if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) { Directory.CreateDirectory(directory); } string json = JsonConvert.SerializeObject(preferences, Formatting.Indented); File.WriteAllText(path, json); return true; } catch { // 调用方根据自己的情况处理 false return false; } } /// /// 从指定路径读取 JSON 文件并反序列化为 实例。 /// /// 反序列化成功返回实例,失败返回 null static Preferences? Deserialize(string preferencesPath) { try { if (!File.Exists(preferencesPath)) { return null; } string json = File.ReadAllText(preferencesPath); return JsonConvert.DeserializeObject(json); } catch { return null; } } }