-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.axaml.cs
139 lines (116 loc) · 4.13 KB
/
MainWindow.axaml.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.PanAndZoom;
using Avalonia.Markup.Xaml;
using SMM2SaveEditor;
using Avalonia.Platform.Storage;
using Avalonia.Interactivity;
using Kaitai;
using SMM2SaveEditor.Utility;
using System.IO;
using System.Diagnostics;
using SMM2SaveEditor.Utility.EditorHelpers;
using System;
using Avalonia.Media.Imaging;
namespace SMM2SaveEditor
{
public partial class MainWindow : Window
{
public static MainWindow? Instance { get; private set; }
private Level level;
private EntityEditor entityEditor;
private ZoomBorder? zoomBorder;
private IStorageBookmarkFile? storageBookmarkFile;
public MainWindow()
{
Instance = this;
this.InitializeComponent();
level = this.Find<Level>("Level");
// lol
//level.LoadFromStream(new KaitaiStream(new byte[0x5BFD0]));
entityEditor = new();
this.Find<Grid>("EditingArea")?.Children.Add(entityEditor);
Grid.SetColumn(entityEditor, 2);
zoomBorder = this.Find<ZoomBorder>("ZoomBorder");
if (zoomBorder == null) throw new MissingMemberException("No zoom border found!");
zoomBorder.KeyDown += (s, e) =>
{
if (e.Key == Avalonia.Input.Key.Space) zoomBorder.UniformToFill();
};
Icon = new(new Bitmap(
#if RELEASE
"./Assets/smm2saveeditor.ico"
#else
"../../../Assets/smm2saveeditor.ico"
#endif
));
Debug.WriteLine("Launched application!");
}
private void InitializeComponent()
{
Debug.WriteLine("Initializing window...");
AvaloniaXamlLoader.Load(this);
}
private async void OnOpenLevel(object sender, RoutedEventArgs e)
{
var picked = await StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions()
{
Title = "Open Level",
AllowMultiple = false,
FileTypeFilter = new[]
{
new FilePickerFileType("")
{
Patterns = new[] { "*.bcd" }
}
}
});
if (picked.Count == 0) return;
storageBookmarkFile = (IStorageBookmarkFile)picked[0];
LoadFromFile(storageBookmarkFile.Path.LocalPath);
}
private async void OnExportLevel(object sender, RoutedEventArgs e)
{
IStorageFile? picked = await StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions()
{
Title = "Export Level",
DefaultExtension = (storageBookmarkFile != null) ? storageBookmarkFile.Name : "course_data_000.bcd",
FileTypeChoices = new[]
{
new FilePickerFileType("")
{
Patterns = new[] { "*.bcd" }
}
},
ShowOverwritePrompt = true
});
if (picked == null) return;
byte[] encrypted = LevelCrypto.EncryptLevel(level.GetBytes());
File.WriteAllBytes(picked.Path.LocalPath, encrypted);
Debug.WriteLine("Completed export. Happy trolling!");
}
private async void OnSaveLevel(object sender, RoutedEventArgs e)
{
if (storageBookmarkFile == null)
{
OnExportLevel(sender, e);
return;
}
await File.WriteAllBytesAsync(storageBookmarkFile.Path.LocalPath, level.GetBytes());
}
public async void LoadFromFile(string path)
{
Debug.WriteLine("Attempting to load level from " + path);
try
{
byte[] bytes = await File.ReadAllBytesAsync(path);
bytes = LevelCrypto.DecryptLevel(bytes);
level.LoadFromStream(new KaitaiStream(bytes));
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}
}