feat: 实现preferences类
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.72" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
||||
<PackageReference Include="System.Data.SQLite" Version="2.0.3" />
|
||||
<PackageReference Include="z440.atl.core" Version="7.13.0" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace OCES.Resonance.Core;
|
||||
|
||||
/// <summary>
|
||||
/// 应用偏好设置,单例模式,负责平台路径解析与 JSON 持久化。
|
||||
/// </summary>
|
||||
public class Preferences
|
||||
{
|
||||
private static readonly Lazy<Preferences> _instance = new(() => new Preferences());
|
||||
|
||||
/// <summary>单例访问入口</summary>
|
||||
public static Preferences Instance
|
||||
{
|
||||
get { return _instance.Value; }
|
||||
}
|
||||
|
||||
/// <summary>数据库文件存放目录</summary>
|
||||
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.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");
|
||||
}
|
||||
}
|
||||
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user