WIP: 使用Preferences查询
This commit is contained in:
@@ -11,7 +11,7 @@
|
|||||||
### Mac
|
### Mac
|
||||||
|
|
||||||
数据库: ~/Library/Application Support/Resonance/Databases
|
数据库: ~/Library/Application Support/Resonance/Databases
|
||||||
偏好设置: ~/Library/Preferences/com.oces.Resonance.plist
|
偏好设置: ~/Library/Preferences/com.oces.Resonance.json
|
||||||
缓存数据: ~/Library/Caches/com.oces.Resonance
|
缓存数据: ~/Library/Caches/com.oces.Resonance
|
||||||
|
|
||||||
### Windows
|
### Windows
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ public static class Database
|
|||||||
/// <returns>数据库连接</returns>
|
/// <returns>数据库连接</returns>
|
||||||
internal static IDbConnection GetConnection(string dbName = "default")
|
internal static IDbConnection GetConnection(string dbName = "default")
|
||||||
{
|
{
|
||||||
string connectionString = $"Data Source={dbName}.rdb;Version=3;";
|
string connectionString = $"Data Source={Path.Combine(PreferencesManager.GetDefaultDatabasePath(), dbName)}.rdb;Version=3;";
|
||||||
return new SQLiteConnection(connectionString);
|
return new SQLiteConnection(connectionString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-99
@@ -1,108 +1,10 @@
|
|||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace OCES.Resonance.Core;
|
namespace OCES.Resonance.Core;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 应用偏好设置,单例模式,负责平台路径解析与 JSON 持久化。
|
/// 应用偏好设置数据模型,由 <see cref="PreferencesManager"/> 统一管理生命周期。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Preferences
|
public class Preferences
|
||||||
{
|
{
|
||||||
private static readonly Lazy<Preferences> _instance = new(() => new Preferences());
|
|
||||||
|
|
||||||
/// <summary>单例访问入口</summary>
|
|
||||||
public static Preferences Instance
|
|
||||||
{
|
|
||||||
get { return _instance.Value; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>数据库文件存放目录</summary>
|
/// <summary>数据库文件存放目录</summary>
|
||||||
public string DatabasePath { get; set; } = string.Empty;
|
public string DatabasePath { get; set; } = string.Empty;
|
||||||
|
|
||||||
/// <summary>偏好设置 JSON 文件的完整路径</summary>
|
|
||||||
public string PreferencesPath { get; set; } = string.Empty;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 私有构造函数 —— 根据运行时平台设置 <see cref="DatabasePath"/> 和 <see cref="PreferencesPath"/>。
|
|
||||||
/// JSON 反序列化时也会调用此构造函数,随后由 JSON 属性覆盖路径值。
|
|
||||||
/// </summary>
|
|
||||||
[JsonConstructor]
|
|
||||||
Preferences()
|
|
||||||
{
|
|
||||||
if (OperatingSystem.IsMacOS())
|
|
||||||
{
|
|
||||||
string home = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
|
|
||||||
DatabasePath = Path.Combine(home, "OCES","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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 将传入的 <see cref="Preferences"/> 实例序列化为 JSON 文件,保存到 <see cref="PreferencesPath"/>。
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="preferences">待序列化的偏好设置实例</param>
|
|
||||||
/// <returns>成功返回 true,失败返回 false</returns>
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 从指定路径读取 JSON 文件并反序列化为 <see cref="Preferences"/> 实例。
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="preferencesPath">JSON 文件的完整路径</param>
|
|
||||||
/// <returns>反序列化成功返回 Preferences 实例,失败返回 null</returns>
|
|
||||||
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<Preferences>(json, settings);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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