-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
73 lines (62 loc) · 3.06 KB
/
MainWindow.xaml.cs
File metadata and controls
73 lines (62 loc) · 3.06 KB
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
using Google.Protobuf;
using Microsoft.Win32;
using System.IO;
using System.Windows;
namespace StaticDataConverter
{
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void BrowseDat(object sender, RoutedEventArgs e) {
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true) {
InputDat.Text = openFileDialog.FileName;
}
}
private void BrowseJSON(object sender, RoutedEventArgs e) {
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true) {
InputJSON.Text = openFileDialog.FileName;
}
}
private void ConvertJsonToDat(object sender, RoutedEventArgs e) {
try {
string json = File.ReadAllText(InputJSON.Text);
// Parse JSON into a StaticData protobuf object
var parser = new JsonParser(JsonParser.Settings.Default.WithIgnoreUnknownFields(true));
var staticData = parser.Parse<StaticData.StaticData>(json);
// Serialize to .dat file
string outputFilePath = Path.ChangeExtension(InputJSON.Text, ".dat");
using (var output = File.Create(outputFilePath)) {
staticData.WriteTo(output);
}
MessageBox.Show("Conversion from JSON to .dat completed!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
} catch (Exception ex) {
MessageBox.Show($"Error during conversion: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void ConvertDatToJson(object sender, RoutedEventArgs e) {
try {
// Read .dat file into a StaticData protobuf object
var staticData = new StaticData.StaticData();
using (var input = File.OpenRead(InputDat.Text)) {
staticData.MergeFrom(input);
}
// Convert protobuf object to JSON using Google.Protobuf.JsonFormatter
var json = JsonFormatter.Default.Format(staticData);
// Format JSON using Newtonsoft.Json for pretty printing
var formattedJson = Newtonsoft.Json.JsonConvert.SerializeObject(
Newtonsoft.Json.JsonConvert.DeserializeObject(json),
Newtonsoft.Json.Formatting.Indented
);
// Write the formatted JSON to a file
string outputFilePath = Path.ChangeExtension(InputDat.Text, ".json");
File.WriteAllText(outputFilePath, formattedJson);
MessageBox.Show("Conversion from .dat to JSON completed!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
} catch (Exception ex) {
MessageBox.Show($"Error during conversion: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}