diff --git a/CHANGELIST.md b/CHANGELIST.md index dd949524..55987a3a 100644 --- a/CHANGELIST.md +++ b/CHANGELIST.md @@ -20,6 +20,7 @@ - Add currently-unused new options class - Allow Check and CLI to check for updates on startup - Check should not default to update checking +- Add save and load for new options type ### 3.6.0 (2025-11-28) diff --git a/MPF.Frontend/Tools/OptionsLoader.cs b/MPF.Frontend/Tools/OptionsLoader.cs index ebacd1bb..8478343e 100644 --- a/MPF.Frontend/Tools/OptionsLoader.cs +++ b/MPF.Frontend/Tools/OptionsLoader.cs @@ -200,6 +200,31 @@ namespace MPF.Frontend.Tools return new Options(settings); } + /// + /// Load the current set of options from the application configuration + /// + public static SegmentedOptions LoadFromConfigSegmented() + { + // If no options path can be found + if (string.IsNullOrEmpty(ConfigurationPath)) + return new SegmentedOptions(); + + // If the file does not exist + if (!File.Exists(ConfigurationPath) || new FileInfo(ConfigurationPath).Length == 0) + return new SegmentedOptions(); + + var serializer = JsonSerializer.Create(); + serializer.DefaultValueHandling = DefaultValueHandling.Ignore; + serializer.MissingMemberHandling = MissingMemberHandling.Ignore; + serializer.NullValueHandling = NullValueHandling.Ignore; + + var stream = File.Open(ConfigurationPath, FileMode.Open, FileAccess.Read, FileShare.None); + using var sr = new StreamReader(stream); + var reader = new JsonTextReader(sr); + + return serializer.Deserialize(reader) ?? new SegmentedOptions(); + } + /// /// Save the current set of options to the application configuration /// @@ -233,6 +258,23 @@ namespace MPF.Frontend.Tools serializer.Serialize(writer, options.Settings, typeof(Dictionary)); } + /// + /// Save the current set of options to the application configuration + /// + public static void SaveToConfig(SegmentedOptions options) + { + // If no options path can be found + if (string.IsNullOrEmpty(ConfigurationPath)) + return; + + var serializer = JsonSerializer.Create(); + var stream = File.Open(ConfigurationPath, FileMode.Create, FileAccess.Write, FileShare.None); + using var sw = new StreamWriter(stream) { AutoFlush = true }; + var writer = new JsonTextWriter(sw) { Formatting = Formatting.Indented }; + + serializer.Serialize(writer, options); + } + /// /// Attempt to determine the configuration path ///