WIP: 使用Preferences查询
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace OCES.Resonance.Core;
|
||||
|
||||
/// <summary>
|
||||
/// 偏好设置管理器,负责平台路径解析、JSON 持久化以及 <see cref="Preferences"/> 实例的生命周期管理。
|
||||
/// </summary>
|
||||
public static class PreferencesManager
|
||||
{
|
||||
/// <summary>当前活动的偏好设置实例,<see cref="Load"/> 调用前为 null</summary>
|
||||
public static Preferences? CurrentPreferences { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 加载偏好设置。先尝试从硬盘反序列化;若文件不存在则创建带平台默认路径的新实例。
|
||||
/// </summary>
|
||||
public static void Load()
|
||||
{
|
||||
string preferencesPath = GetDefaultPreferencesPath();
|
||||
|
||||
// 尝试从硬盘加载
|
||||
Preferences? loaded = Deserialize(preferencesPath);
|
||||
if (loaded != null)
|
||||
{
|
||||
CurrentPreferences = loaded;
|
||||
return;
|
||||
}
|
||||
|
||||
// 文件不存在或反序列化失败 → 创建默认实例
|
||||
CurrentPreferences = new Preferences
|
||||
{
|
||||
DatabasePath = GetDefaultDatabasePath()
|
||||
};
|
||||
|
||||
// 立即落盘,确保下次启动能直接读取
|
||||
Serialize(CurrentPreferences);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将当前 <see cref="CurrentPreferences"/> 序列化到硬盘。
|
||||
/// </summary>
|
||||
public static void Save()
|
||||
{
|
||||
if (CurrentPreferences == null) return;
|
||||
Serialize(CurrentPreferences);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据运行时平台返回数据库文件存放目录。
|
||||
/// </summary>
|
||||
public static string GetDefaultDatabasePath()
|
||||
{
|
||||
if (OperatingSystem.IsMacOS())
|
||||
{
|
||||
string library = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
|
||||
return Path.Combine(library, "OCES", "Resonance", "Databases");
|
||||
}
|
||||
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
string localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
||||
return Path.Combine(localAppData, "OCES", "Resonance", "Databases");
|
||||
}
|
||||
|
||||
if (OperatingSystem.IsLinux())
|
||||
{
|
||||
string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
|
||||
string xdgDataHome = Environment.GetEnvironmentVariable("XDG_DATA_HOME")
|
||||
?? Path.Combine(home, ".local", "share");
|
||||
return Path.Combine(xdgDataHome, "Resonance", "Databases");
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据运行时平台返回偏好设置 JSON 文件的完整路径。
|
||||
/// </summary>
|
||||
public static string GetDefaultPreferencesPath()
|
||||
{
|
||||
if (OperatingSystem.IsMacOS())
|
||||
{
|
||||
string library = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
|
||||
return Path.Combine(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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 <see cref="Preferences"/> 实例序列化为 JSON 文件,保存到平台默认偏好设置路径。
|
||||
/// </summary>
|
||||
/// <returns>成功返回 true,失败返回 false</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从指定路径读取 JSON 文件并反序列化为 <see cref="Preferences"/> 实例。
|
||||
/// </summary>
|
||||
/// <returns>反序列化成功返回实例,失败返回 null</returns>
|
||||
static Preferences? Deserialize(string preferencesPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!File.Exists(preferencesPath))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
string json = File.ReadAllText(preferencesPath);
|
||||
return JsonConvert.DeserializeObject<Preferences>(json);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user