Add ability to import FastFlags from JSON

This commit is contained in:
pizzaboxer 2023-07-27 22:56:11 +01:00
parent e0fcbaab2f
commit b651be7024
No known key found for this signature in database
GPG Key ID: 59D4A1DBAD0F2BA8
2 changed files with 60 additions and 0 deletions

View File

@ -34,6 +34,7 @@
</ui:Button.Style>
</ui:Button>
<ToggleButton x:Name="TogglePresetsButton" Content="Show preset flags" Click="ToggleButton_Click" Margin="12,0,0,0" />
<ui:Button Icon="ArrowImport24" Content="Import JSON" Margin="12,0,0,0" Click="ImportJSONButton_Click" />
</StackPanel>
<ui:TextBox x:Name="SearchTextBox" Grid.Row="2" Margin="0,0,0,16" Icon="Search32" PlaceholderText="Search" TextChanged="SearchTextBox_TextChanged" />

View File

@ -3,6 +3,8 @@ using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using Microsoft.Win32;
using Wpf.Ui.Mvvm.Contracts;
using Bloxstrap.UI.Elements.Dialogs;
@ -211,6 +213,63 @@ namespace Bloxstrap.UI.Elements.Menu.Pages
ReloadList();
}
private void ImportJSONButton_Click(object sender, RoutedEventArgs e)
{
var dialog = new OpenFileDialog
{
Filter = "JSON files|*.json|All files|*.*"
};
if (dialog.ShowDialog() != true)
return;
try
{
var list = JsonSerializer.Deserialize<Dictionary<string, object>>(File.ReadAllText(dialog.FileName));
if (list is null)
throw new Exception("JSON deserialization returned null");
var conflictingFlags = App.FastFlags.Prop.Where(x => list.ContainsKey(x.Key)).Select(x => x.Key);
bool overwriteConflicting = false;
if (conflictingFlags.Any())
{
var result = Controls.ShowMessageBox(
"Some of the flags you are attempting to import already have set values. Would you like to overwrite their current values with the ones defined in the import?\n" +
"\n" +
"Conflicting flags:\n" +
String.Join(", ", conflictingFlags),
MessageBoxImage.Question,
MessageBoxButton.YesNo
);
overwriteConflicting = result == MessageBoxResult.Yes;
}
foreach (var pair in list)
{
if (App.FastFlags.Prop.ContainsKey(pair.Key) && !overwriteConflicting)
continue;
App.FastFlags.SetValue(pair.Key, pair.Value);
}
_searchFilter = "";
ReloadList();
}
catch (Exception ex)
{
Controls.ShowMessageBox(
"The file you've selected does not appear to be valid JSON. Please double check the file contents and try again.\n" +
"\n" +
"More information:\n" +
$"{ex.Message}",
MessageBoxImage.Error
);
}
}
private void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (sender is not TextBox textbox)