Files
FinalToken/src/FinalToken.Core/Settings.cs
T
Florian Schmidt 9f33dd8896
agent-review agent review: fail
core: keep setting defaults for properties missing from the file
With init-only properties, the source-generated reader set a property
missing from settings.json to its default value, not its initializer,
so the file failed validation and every setting fell back to defaults.
Any release that adds a setting would have reset all user settings.
Settings use settable properties now, and the snapshot's version field
too; the other persisted types were checked and keep their defaults.

Refs #1
2026-10-14 21:23:04 +02:00

67 lines
3.4 KiB
C#

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);
}
}