using System.Text.Json; namespace FinalToken.Core; public sealed record ProviderSettings { // Setters let source-generated JSON construct defaults before assigning present properties. public bool Enabled { get; set; } = true; public bool PopupsEnabled { get; set; } = true; public bool PopupAhead { get; set; } = true; public bool PopupBehind { get; set; } = true; public string? CliPath { get; set; } } public sealed record Settings { // Preserve initializers when an older file lacks newly added properties (D5). public TimeSpan RefreshInterval { get; set; } = TimeSpan.FromSeconds(60); public TimeSpan ProbeInterval { get; set; } = TimeSpan.FromMinutes(5); public double PaceThreshold { get; set; } = 10; public bool PopupAhead { get; set; } = true; public bool PopupBehind { get; set; } = true; public InstallMode StreamDeckInstallMode { get; set; } = InstallMode.Ask; public string? DismissedPluginVersion { get; set; } public bool StartWithOs { get; set; } public bool CloseToTray { get; set; } = true; public ProviderSettings Claude { get; set; } = new(); public ProviderSettings Codex { get; set; } = new(); public ProviderSettings For(Provider provider) => provider == Provider.Claude ? Claude : Codex; [System.Text.Json.Serialization.JsonIgnore] public bool IsValid => RefreshInterval > TimeSpan.Zero && RefreshInterval <= TimeSpan.FromSeconds(60) && ProbeInterval >= TimeSpan.FromMinutes(1) && ProbeInterval <= TimeSpan.FromMinutes(60) && double.IsFinite(PaceThreshold) && PaceThreshold is > 0 and <= 100 && Claude is not null && Codex is not null && Enum.IsDefined(StreamDeckInstallMode); } public static class DataPaths { public static string DataDirectory => Environment.GetEnvironmentVariable("FINALTOKEN_DATA_DIR") is { Length: > 0 } path ? Path.GetFullPath(path) : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "FinalToken"); public static string ClaudeDirectory => Environment.GetEnvironmentVariable("CLAUDE_CONFIG_DIR") is { Length: > 0 } path ? Path.GetFullPath(path) : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".claude"); public static string CodexDirectory => Environment.GetEnvironmentVariable("CODEX_HOME") is { Length: > 0 } path ? Path.GetFullPath(path) : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".codex"); } public static class SettingsFile { public static Settings Load(string directory, DiagnosticLog diagnostics) { try { var value = JsonSerializer.Deserialize(SharedFile.ReadAllText(Path.Combine(directory, "settings.json")), CoreJsonContext.Default.Settings); if (value is { IsValid: true }) return value; } catch (Exception e) when (e is IOException or UnauthorizedAccessException or JsonException) { } diagnostics.Add(null, "settings-defaults", "Settings missing or invalid; using defaults."); return new(); } public static Task SaveAsync(string directory, Settings settings, CancellationToken cancellationToken = default) { if (!settings.IsValid) throw new ArgumentException("Invalid settings.", nameof(settings)); return AtomicFile.WriteAsync(Path.Combine(directory, "settings.json"), JsonSerializer.Serialize(settings, CoreJsonContext.Default.Settings), cancellationToken); } }