mirror of
https://github.com/SabreTools/MPF.git
synced 2026-07-02 17:24:48 +00:00
Add save and load for new options type
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -200,6 +200,31 @@ namespace MPF.Frontend.Tools
|
||||
return new Options(settings);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load the current set of options from the application configuration
|
||||
/// </summary>
|
||||
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<SegmentedOptions>(reader) ?? new SegmentedOptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Save the current set of options to the application configuration
|
||||
/// </summary>
|
||||
@@ -233,6 +258,23 @@ namespace MPF.Frontend.Tools
|
||||
serializer.Serialize(writer, options.Settings, typeof(Dictionary<string, string>));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Save the current set of options to the application configuration
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempt to determine the configuration path
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user