using System; using System.IO; using System.Text.Json; using RedBookPlayer.GUI; namespace RedBookPlayer { public class Settings { /// /// Indicates if discs should start playing on load /// public bool AutoPlay { get; set; } = false; /// /// Indicates if an index change can trigger a track change /// public bool IndexButtonChangeTrack { get; set; } = false; /// /// Indicates if the index 0 of track 1 is treated like a hidden track /// public bool AllowSkipHiddenTrack { get; set; } = false; /// /// Indicates if data tracks should be played like old, non-compliant players /// public bool PlayDataTracks { get; set; } = false; /// /// Generate a TOC if the disc is missing one /// public bool GenerateMissingTOC { get; set; } = true; /// /// Indicates the default playback volume /// public int Volume { get; set; } = 100; /// /// Indicates the currently selected theme /// public string SelectedTheme { get; set; } = "default"; /// /// Path to the settings file /// private string _filePath; public Settings() {} public Settings(string filePath) => _filePath = filePath; /// /// Load settings from a file /// /// Path to the settings JSON file /// Settings derived from the input file, if possible public static Settings Load(string filePath) { if(File.Exists(filePath)) { try { Settings settings = JsonSerializer.Deserialize(File.ReadAllText(filePath)); settings._filePath = filePath; MainWindow.ApplyTheme(settings.SelectedTheme); return settings; } catch(JsonException) { Console.WriteLine("Couldn't parse settings, reverting to default"); return new Settings(filePath); } } return new Settings(filePath); } /// /// Save settings to a file /// public void Save() { var options = new JsonSerializerOptions { WriteIndented = true }; string json = JsonSerializer.Serialize(this, options); File.WriteAllText(_filePath, json); } } }