49 lines
1.4 KiB
C#
49 lines
1.4 KiB
C#
using System.IO;
|
|
using System.Text.Json;
|
|
using Futonizer.Models;
|
|
|
|
namespace Futonizer.Services;
|
|
|
|
/// <summary>
|
|
/// Loads and saves <see cref="AppSettings"/> as a JSON file stored next to
|
|
/// the application executable, so it travels with the app folder.
|
|
/// </summary>
|
|
public static class SettingsService
|
|
{
|
|
private static readonly string SettingsFilePath = Path.Combine(AppContext.BaseDirectory, "settings.json");
|
|
private static readonly JsonSerializerOptions Options = new() { WriteIndented = true };
|
|
|
|
public static AppSettings Load()
|
|
{
|
|
try
|
|
{
|
|
if (File.Exists(SettingsFilePath))
|
|
{
|
|
string json = File.ReadAllText(SettingsFilePath);
|
|
var settings = JsonSerializer.Deserialize<AppSettings>(json, Options);
|
|
if (settings is not null)
|
|
return settings;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Corrupt or unreadable settings file - fall back to defaults.
|
|
}
|
|
|
|
return new AppSettings();
|
|
}
|
|
|
|
public static void Save(AppSettings settings)
|
|
{
|
|
try
|
|
{
|
|
string json = JsonSerializer.Serialize(settings, Options);
|
|
File.WriteAllText(SettingsFilePath, json);
|
|
}
|
|
catch
|
|
{
|
|
// Best effort - ignore write failures (e.g. read-only install folder).
|
|
}
|
|
}
|
|
}
|