mirror of
https://github.com/denglihong2007/CRSim
synced 2026-08-09 19:15:59 +08:00
feat: Refactor SettingsService to use JSON storage
Replaced the Windows Registry-based settings implementation with a JSON file (`settings.json`) stored in the application's data path (`CRSim.Core/Utils/AppPaths.cs`). - Handles `IApi` interface serialization by storing the API name. - Includes error handling for corrupted JSON files to prevent crashes.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using CRSim.Core.Abstractions;
|
||||
using CRSim.Core.Services;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace CRSim.Core.Models
|
||||
{
|
||||
@@ -20,7 +21,9 @@ namespace CRSim.Core.Models
|
||||
public TimeSpan StopDisplayFromArrivalDuration { get; set; } = TimeSpan.FromMinutes(10);
|
||||
|
||||
public TimeSpan StopCheckInAdvanceDuration { get; set; } = TimeSpan.FromMinutes(2);
|
||||
[JsonIgnore]
|
||||
public IApi Api { get; set; } = new ApiFactory().CreateApi("镜像站");
|
||||
public string ApiName { get; set; } = "镜像站";
|
||||
public int MaxPages { get; set; } = 3;
|
||||
public int SwitchPageSeconds { get; set; } = 20;
|
||||
public string UserKey { get; set; } = "";
|
||||
|
||||
@@ -1,51 +1,56 @@
|
||||
using CRSim.Core.Abstractions;
|
||||
using CRSim.Core.Models;
|
||||
using Microsoft.Win32;
|
||||
using CRSim.Core.Utils;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace CRSim.Core.Services
|
||||
{
|
||||
public class SettingsService : ISettingsService
|
||||
{
|
||||
private Settings _settings;
|
||||
private RegistryKey _key = Registry.CurrentUser.OpenSubKey(@"Software\CRSim\Settings",true);
|
||||
private readonly string _settingsFilePath;
|
||||
|
||||
public SettingsService()
|
||||
{
|
||||
_settingsFilePath = Path.Combine(AppPaths.AppDataPath, "settings.json");
|
||||
}
|
||||
|
||||
public void SaveSettings()
|
||||
{
|
||||
_key.SetValue("SwitchPageSeconds", _settings.SwitchPageSeconds);
|
||||
_key.SetValue("Api", _settings.Api.Name);
|
||||
_key.SetValue("MaxPages", _settings.MaxPages);
|
||||
_key.SetValue("StopCheckInAdvanceDuration", (int)_settings.StopCheckInAdvanceDuration.TotalMinutes);
|
||||
_key.SetValue("StopDisplayUntilDepartureDuration", (int)_settings.StopDisplayUntilDepartureDuration.TotalMinutes);
|
||||
_key.SetValue("StopDisplayFromArrivalDuration", (int)_settings.StopDisplayFromArrivalDuration.TotalMinutes);
|
||||
_key.SetValue("PassingCheckInAdvanceDuration", (int)_settings.PassingCheckInAdvanceDuration.TotalMinutes);
|
||||
_key.SetValue("DepartureCheckInAdvanceDuration", (int)_settings.DepartureCheckInAdvanceDuration.TotalMinutes);
|
||||
_key.SetValue("UserKey", _settings.UserKey);
|
||||
_key.SetValue("LoadTodayOnly", _settings.LoadTodayOnly);
|
||||
_key.SetValue("ReopenUnclosedScreensOnLoad", _settings.ReopenUnclosedScreensOnLoad);
|
||||
// Ensure the directory exists
|
||||
Directory.CreateDirectory(AppPaths.AppDataPath);
|
||||
|
||||
_settings.ApiName = _settings.Api.Name;
|
||||
|
||||
var options = new JsonSerializerOptions { WriteIndented = true };
|
||||
string jsonString = JsonSerializer.Serialize(_settings, options);
|
||||
File.WriteAllText(_settingsFilePath, jsonString);
|
||||
}
|
||||
|
||||
public void LoadSettings()
|
||||
{
|
||||
if (_key==null)
|
||||
try
|
||||
{
|
||||
Registry.CurrentUser.CreateSubKey(@"Software\CRSim\Settings");
|
||||
_key = Registry.CurrentUser.OpenSubKey(@"Software\CRSim\Settings",true);
|
||||
if (File.Exists(_settingsFilePath))
|
||||
{
|
||||
string jsonString = File.ReadAllText(_settingsFilePath);
|
||||
_settings = JsonSerializer.Deserialize<Settings>(jsonString);
|
||||
_settings.Api = new ApiFactory().CreateApi(_settings.ApiName);
|
||||
}
|
||||
else
|
||||
{
|
||||
_settings = new Settings();
|
||||
SaveSettings();
|
||||
}
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
// Handle the case where the json is corrupted, load default settings
|
||||
_settings = new Settings();
|
||||
SaveSettings();
|
||||
}
|
||||
else
|
||||
{
|
||||
_settings = new Settings();
|
||||
if (_key.GetValue("SwitchPageSeconds") != null) _settings.SwitchPageSeconds = (int)_key.GetValue("SwitchPageSeconds");
|
||||
_settings.Api = new ApiFactory().CreateApi((string)_key.GetValue("Api"));
|
||||
if (_key.GetValue("MaxPages") != null) _settings.MaxPages = (int)_key.GetValue("MaxPages");
|
||||
if (_key.GetValue("StopCheckInAdvanceDuration") != null) _settings.StopCheckInAdvanceDuration = TimeSpan.FromMinutes((int)_key.GetValue("StopCheckInAdvanceDuration"));
|
||||
if (_key.GetValue("StopDisplayUntilDepartureDuration") != null) _settings.StopDisplayUntilDepartureDuration = TimeSpan.FromMinutes((int)_key.GetValue("StopDisplayUntilDepartureDuration"));
|
||||
if (_key.GetValue("StopDisplayFromArrivalDuration") != null) _settings.StopDisplayFromArrivalDuration = TimeSpan.FromMinutes((int)_key.GetValue("StopDisplayFromArrivalDuration"));
|
||||
if (_key.GetValue("PassingCheckInAdvanceDuration") != null) _settings.PassingCheckInAdvanceDuration = TimeSpan.FromMinutes((int)_key.GetValue("PassingCheckInAdvanceDuration"));
|
||||
if (_key.GetValue("DepartureCheckInAdvanceDuration") != null) _settings.DepartureCheckInAdvanceDuration = TimeSpan.FromMinutes((int)_key.GetValue("DepartureCheckInAdvanceDuration"));
|
||||
if (_key.GetValue("UserKey") != null) _settings.UserKey = (string)_key.GetValue("UserKey");
|
||||
if (_key.GetValue("LoadTodayOnly") != null) _settings.LoadTodayOnly = bool.Parse((string)_key.GetValue("LoadTodayOnly"));
|
||||
if (_key.GetValue("ReopenUnclosedScreensOnLoad") != null) _settings.ReopenUnclosedScreensOnLoad = bool.Parse((string)_key.GetValue("ReopenUnclosedScreensOnLoad"));
|
||||
}
|
||||
}
|
||||
|
||||
public Settings GetSettings()
|
||||
|
||||
Reference in New Issue
Block a user