-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaveEditor.xaml.cs
87 lines (80 loc) · 2.8 KB
/
SaveEditor.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace RemnantWorldChanger
{
/// <summary>
/// Interaction logic for SaveEditor.xaml
/// </summary>
public partial class SaveEditor : Window
{
private DataPackage? Original { get; set; }
private BulkSave Saves { get; set; }
public DataPackage? Save { get; private set; }
public SaveEditor(BulkSave bs, DataPackage dp)
{
InitializeComponent();
this.Original = dp;
this.Saves = bs;
EditorWindow.Text = SerializeDataPackage(dp);
}
private string SerializeDataPackage(DataPackage dp)
{
return JsonSerializer.Serialize<DataPackage>(dp, new JsonSerializerOptions
{
WriteIndented = true,
Converters = { new JsonStringEnumConverter() }
});
}
private void ApplyEdit_Click(object sender, RoutedEventArgs e)
{
if (EditorWindow is null)
return;
if (EditorWindow.Text == "")
return;
Debug.WriteLine(EditorWindow.Text);
var ser = new JsonSerializerOptions();
ser.Converters.Add(new JsonStringEnumConverter());
DataPackage? temp = null;
try
{
temp = JsonSerializer.Deserialize<DataPackage>(EditorWindow.Text, ser);
}
catch (JsonException je)
{
var _ = MessageBox.Show(je.Message + "\n\nRevert Data?", "Json Deserialization Error", MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (_ == MessageBoxResult.Yes)
EditorWindow.Text = SerializeDataPackage(Original);
}
if (temp == null)
return;
MessageBoxResult result = MessageBoxResult.Yes;
if (Saves.SaveInfo.Contains(temp))
result = MessageBox.Show("An entry already exists with those parameters, Overwrite?", "Duplicate Entry", MessageBoxButton.YesNo, MessageBoxImage.Error);
if (result != MessageBoxResult.Yes)
return;
Save = temp;
Debug.WriteLine($"Updated: {Save}");
DialogResult = true;
this.Close();
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
this.Close();
}
}
}