-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMainWindow.xaml.cs
519 lines (460 loc) · 15.3 KB
/
MainWindow.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Navigation;
using nsZip.Properties;
namespace nsZip
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private readonly Output Out;
private readonly OpenFileDialog SelectNspXciDialog = new OpenFileDialog();
private readonly OpenFileDialog SelectNspzDialog = new OpenFileDialog();
private readonly OpenFileDialog SelectInputFileDialog = new OpenFileDialog();
private readonly FolderBrowserDialog SelectOutputDictionaryDialog = new FolderBrowserDialog();
private readonly FolderBrowserDialog SelectTempDictionaryDialog = new FolderBrowserDialog();
private int BlockSize = 262144;
private bool CheckForUpdates;
private bool KeepTempFilesAfterTask;
private string OutputFolderPath;
private enum TaskDonePowerState
{
None,
Suspend,
Hibernate
};
private TaskDonePowerState StandByWhenTaskDone;
private string TempFolderPath;
private bool VerifyHashes = true;
private int ZstdLevel = 18;
public MainWindow()
{
InitializeComponent();
Out = new Output();
MainGrid.Visibility = Visibility.Visible;
MainGridBusy.Visibility = Visibility.Hidden;
SelectNspzDialog.Filter =
"Compressed Switch File (*.nspz)|*.nspz|" +
"XCIZ to not-installable NSP (*.xciz)|*.xciz";
SelectNspzDialog.Multiselect = true;
SelectNspzDialog.Title = "Select input nspz files...";
SelectInputFileDialog.Filter =
"All Switch Games (*.nsp;*.xci;*.nspz;*.xciz)|*.nsp;*.xci;*.nspz;*.xciz|" +
"Switch Package (*.nsp)|*.nsp|Switch Cartridge (*.xci)|*.xci|" +
"Compressed Switch File (*.nspz)|*.nspz|" +
"Compressed Switch Cart (*.xciz)|*.xciz";
SelectInputFileDialog.Multiselect = true;
SelectInputFileDialog.Title = "Select input files...";
SelectNspXciDialog.Filter =
"Switch Games (*.nsp;*.xci)|*.nsp;*.xci|" +
"Switch Package (*.nsp)|*.ns|Switch Cartridge (*.xci)|*.xci";
SelectNspXciDialog.Multiselect = true;
SelectNspXciDialog.Title = "Select input NSP files...";
SelectOutputDictionaryDialog.RootFolder = Environment.SpecialFolder.MyComputer;
OutputFolderTextBox.Text = Settings.Default.OutputFolder != ""
? Settings.Default.OutputFolder
: Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
VerificationComboBox.SelectedIndex = Settings.Default.Verification ? 0 : 1;
CheckForUpdatesComboBox.SelectedIndex = Settings.Default.CheckForUpdates ? 0 : 1;
foreach (var item in CompressionLevelComboBox.Items.Cast<ComboBoxItem>())
{
if ((int) item.Tag == Settings.Default.CompressionLevel)
{
CompressionLevelComboBox.SelectedItem = item;
break;
}
}
foreach (var item in BlockSizeComboBox.Items.Cast<ComboBoxItem>())
{
if ((int) item.Tag == Settings.Default.BlockSize)
{
BlockSizeComboBox.SelectedItem = item;
break;
}
}
SelectTempDictionaryDialog.RootFolder = Environment.SpecialFolder.MyComputer;
if (Settings.Default.TempFolder != "")
{
TempFolderTextBox.Text = Settings.Default.TempFolder;
}
else
{
TempFolderTextBox.Text = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
}
KeepTempFilesAfterTaskComboBox.SelectedIndex = Settings.Default.KeepTempFiles ? 0 : 1;
StandByWhenTaskDoneComboBox.SelectedIndex = Settings.Default.StandbyWhenDone;
try
{
LicenseTextBox.Text = File.ReadAllText(@"LICENSE");
}
catch (Exception ex)
{
Out.Log("LICENSE file not found!\r\n");
}
Out.Log("nsZip initialized\r\n");
}
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(e.Uri.ToString());
}
private void SelectFileToCompressButton_Click(object sender, RoutedEventArgs e)
{
if (SelectNspXciDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
foreach (var filename in SelectNspXciDialog.FileNames)
{
TaskQueue.Items.Add(filename);
}
}
}
private void SelectNspFileToDecompressButton_Click(object sender, RoutedEventArgs e)
{
if (SelectNspzDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
foreach (var filename in SelectNspzDialog.FileNames)
{
TaskQueue.Items.Add(filename);
}
}
}
private void RunButton_Click(object sender, RoutedEventArgs e)
{
var t = Task.Run(() => { RunTask(); });
}
private void RunTask()
{
if (TaskQueue.Items.Count == 0)
{
Out.Log("Nothing to do - TaskQueue empty! Please add an NSP or NSPZ!\r\n");
return;
}
Dispatcher.Invoke(() =>
{
BusyTextBlock.Text = "Working...";
MainGrid.Visibility = Visibility.Hidden;
MainGridBusy.Visibility = Visibility.Visible;
});
try
{
do
{
var inFile = (string) TaskQueue.Items[0];
var infileLowerCase = inFile.ToLower();
var inFileNoExtension = Path.GetFileNameWithoutExtension(inFile);
Dispatcher.Invoke(() =>
{
BusyTextBlock.Text =
$"Task \"{Path.GetFileNameWithoutExtension(inFile)}\" in progress...\r\nThis might take quite some time.\r\n" +
$"Please take a look at the console window for more information.";
TaskQueue.Items.RemoveAt(0);
});
var tl = new TaskLogic(OutputFolderPath, TempFolderPath, VerifyHashes, BlockSize, ZstdLevel, Out);
if (tl.checkIfAlreadyExist(inFile))
{
continue;
}
tl.cleanFolders();
try
{
if (infileLowerCase.EndsWith("nsp"))
{
tl.CompressNSP(inFile);
}
else if (infileLowerCase.EndsWith("xci"))
{
tl.CompressXCI(inFile);
}
else if (infileLowerCase.EndsWith("nspz"))
{
tl.DecompressNSPZ(inFile);
}
else if (infileLowerCase.EndsWith("xciz"))
{
tl.DecompressXCIZ(inFile);
}
else
{
throw new InvalidDataException($"Invalid file type {inFile}");
}
}
catch (Exception ex)
{
Out.Error(ex.StackTrace + "\r\n");
Out.Error(ex.Message + "\r\n\r\n");
}
finally
{
if (!KeepTempFilesAfterTask && tl != null)
{
tl.cleanFolders();
}
}
} while (TaskQueue.Items.Count > 0);
}
catch (Exception ex)
{
Out.Log(ex.StackTrace + "\r\n");
Out.Log(ex.Message);
throw ex;
}
finally
{
Dispatcher.Invoke(() =>
{
MainGrid.Visibility = Visibility.Visible;
MainGridBusy.Visibility = Visibility.Hidden;
});
switch (StandByWhenTaskDone)
{
case TaskDonePowerState.Suspend:
Out.Log("Activate standby mode...\r\n");
Thread.Sleep(1000);
System.Windows.Forms.Application.SetSuspendState(PowerState.Suspend, false, false);
break;
case TaskDonePowerState.Hibernate:
Out.Log("Activate hibernate mode...\r\n");
Thread.Sleep(1000);
System.Windows.Forms.Application.SetSuspendState(PowerState.Hibernate, false, false);
break;
}
}
}
private void VerificationComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
VerifyHashes = VerificationComboBox.SelectedIndex != 1;
Settings.Default.Verification = VerifyHashes;
Settings.Default.Save();
Out.Log($"Set VerifyHashes to {VerifyHashes}\r\n");
}
private void CheckForUpdatesComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
CheckForUpdates = CheckForUpdatesComboBox.SelectedIndex != 1;
Settings.Default.CheckForUpdates = CheckForUpdates;
Settings.Default.Save();
Out.Log($"Set CheckForUpdates to {CheckForUpdates}\r\n");
}
private void CompressionLevelComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ZstdLevel = (int) ((ComboBoxItem) CompressionLevelComboBox.SelectedItem).Tag;
Settings.Default.CompressionLevel = ZstdLevel;
Settings.Default.Save();
Out.Log($"Set ZstdLevel to {ZstdLevel}\r\n");
}
private void BlockSizeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
BlockSize = (int) ((ComboBoxItem) BlockSizeComboBox.SelectedItem).Tag;
Settings.Default.BlockSize = BlockSize;
Settings.Default.Save();
Out.Log($"Set BlockSize to {BlockSize} bytes\r\n");
}
private void OutputFolderTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
OutputFolderPath = OutputFolderTextBox.Text;
Settings.Default.OutputFolder = OutputFolderPath;
Settings.Default.Save();
Out.Log($"Set OutputFolderPath to {OutputFolderPath}\r\n");
}
private void OutputFolderButton_Click(object sender, RoutedEventArgs e)
{
SelectOutputDictionaryDialog.SelectedPath = OutputFolderPath;
if (SelectOutputDictionaryDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK
&& !string.IsNullOrWhiteSpace(SelectOutputDictionaryDialog.SelectedPath))
{
OutputFolderTextBox.Text = SelectOutputDictionaryDialog.SelectedPath;
}
}
private void TempFolderTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TempFolderPath = TempFolderTextBox.Text;
Settings.Default.TempFolder = TempFolderPath;
Settings.Default.Save();
Out.Log($"Set TempFolderPath to {TempFolderPath}\r\n");
}
private void TempFolderButton_Click(object sender, RoutedEventArgs e)
{
SelectTempDictionaryDialog.SelectedPath = TempFolderPath;
if (SelectTempDictionaryDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK
&& !string.IsNullOrWhiteSpace(SelectTempDictionaryDialog.SelectedPath))
{
TempFolderTextBox.Text = SelectTempDictionaryDialog.SelectedPath;
}
}
private void KeepTempFilesAfterTaskComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
KeepTempFilesAfterTask = KeepTempFilesAfterTaskComboBox.SelectedIndex != 1;
Settings.Default.KeepTempFiles = KeepTempFilesAfterTask;
Settings.Default.Save();
Out.Log($"Set KeepTempFilesAfterTask to {KeepTempFilesAfterTask}\r\n");
}
private void StandByWhenTaskDoneComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
switch (StandByWhenTaskDoneComboBox.SelectedIndex)
{
case 0:
StandByWhenTaskDone = TaskDonePowerState.None;
break;
case 1:
StandByWhenTaskDone = TaskDonePowerState.Suspend;
break;
case 2:
StandByWhenTaskDone = TaskDonePowerState.Hibernate;
break;
}
Settings.Default.StandbyWhenDone = StandByWhenTaskDoneComboBox.SelectedIndex;
Settings.Default.Save();
Out.Log($"Set StandByWhenTaskDone to {StandByWhenTaskDone}\r\n");
}
private void SelectInputFilesButton_Click(object sender, RoutedEventArgs e)
{
if (SelectInputFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
foreach (var filename in SelectInputFileDialog.FileNames)
{
ToolsTaskQueue.Items.Add(filename);
}
}
}
enum ToolsTaskType {
ExtractTitlekeys,
ExtractTickets,
ExtractPfsHfs,
ExtractRomFS
}
private void ExtractTitlekeys_Click(object sender, RoutedEventArgs e)
{
var t = Task.Run(() => { RunToolsTask(ToolsTaskType.ExtractTitlekeys); });
}
private void ExtractTickets_Click(object sender, RoutedEventArgs e)
{
var t = Task.Run(() => { RunToolsTask(ToolsTaskType.ExtractTickets); });
}
private void ExtractPfsHfs_Click(object sender, RoutedEventArgs e)
{
var t = Task.Run(() => { RunToolsTask(ToolsTaskType.ExtractPfsHfs); });
}
private void ExtractRomFS_Click(object sender, RoutedEventArgs e)
{
var t = Task.Run(() => { RunToolsTask(ToolsTaskType.ExtractRomFS); });
}
private void RunToolsTask(ToolsTaskType toolsTaskType)
{
if (ToolsTaskQueue.Items.Count == 0)
{
Out.Log("Nothing to do - ToolsTaskQueue empty! Please select any input file first!\r\n");
return;
}
Dispatcher.Invoke(() =>
{
BusyTextBlock.Text = "Working...";
MainGrid.Visibility = Visibility.Hidden;
MainGridBusy.Visibility = Visibility.Visible;
});
var extractedTitleKeys = new LibHac.Keyset();
var keyset = new LibHac.Keyset();
if (toolsTaskType == ToolsTaskType.ExtractRomFS)
{
keyset = LibHacControl.ProcessKeyset.OpenKeyset();
}
try
{
var TitlekeysOutputFilePath = $"{OutputFolderPath}/titlekeys.txt";
var TicketOutputPath = $"{OutputFolderPath}/Tickets";
if (toolsTaskType == ToolsTaskType.ExtractTickets && !Directory.Exists(TicketOutputPath))
{
Directory.CreateDirectory(TicketOutputPath);
}
do
{
var inFilePath = (string)ToolsTaskQueue.Items[0];
var inFile = Path.GetFileName(inFilePath);
var ToolsTaskText = $"ToolsTask {toolsTaskType.ToString()} \"{inFile}\" in progress...";
Out.Event($"{ToolsTaskText}\r\n");
Dispatcher.Invoke(() =>
{
BusyTextBlock.Text =
$"{ToolsTaskText}\r\nThis might take quite some time.\r\n" +
$"Please take a look at the console window for more information.";
ToolsTaskQueue.Items.RemoveAt(0);
});
// Sleep 10 ms for the BusyTextBlock be longer visible for a large amount of very short tasks like ExtractTitlekeys
// It’s here so he sleeps even if the ToolsTask was skipped
Thread.Sleep(10);
switch(toolsTaskType)
{
case ToolsTaskType.ExtractTitlekeys:
FileTools.File2Titlekey(inFilePath, extractedTitleKeys, Out);
break;
case ToolsTaskType.ExtractTickets:
FileTools.File2Tickets(inFilePath, TicketOutputPath, keyset, Out);
break;
case ToolsTaskType.ExtractPfsHfs:
var PfsHfsOutputPath = $"{OutputFolderPath}/{inFile}_Extracted";
if (!Directory.Exists(PfsHfsOutputPath))
{
Directory.CreateDirectory(PfsHfsOutputPath);
FileTools.ExtractPfsHfs(inFilePath, PfsHfsOutputPath, keyset, Out);
}
else
{
Out.Event($"ToolsTask {toolsTaskType.ToString()} \"{inFile}\" skipped as it already exists in the output directory\r\n");
continue;
}
break;
case ToolsTaskType.ExtractRomFS:
var RomFsOutputPath = $"{OutputFolderPath}/{inFile}_RomFS";
if (!Directory.Exists(RomFsOutputPath))
{
Directory.CreateDirectory(RomFsOutputPath);
FileTools.ExtractRomFS(inFilePath, RomFsOutputPath, keyset, Out);
}
else
{
Out.Event($"ToolsTask {toolsTaskType.ToString()} \"{inFile}\" skipped as it already exists in the output directory\r\n");
continue;
}
break;
default:
throw new NotImplementedException($"Unknown ToolsTaskType: {toolsTaskType}!");
}
Out.Event($"ToolsTask {toolsTaskType.ToString()} \"{inFile}\" completed!\r\n");
} while (ToolsTaskQueue.Items.Count > 0);
if (toolsTaskType == ToolsTaskType.ExtractTitlekeys)
{
Out.Log($"Writing to {TitlekeysOutputFilePath}\r\n");
using (var titlekeys = new StreamWriter(File.Open(TitlekeysOutputFilePath, FileMode.Create), Encoding.ASCII))
{
foreach (var entry in extractedTitleKeys.TitleKeys)
{
var line = $"{Utils.BytesToString(entry.Key)},{Utils.BytesToString(entry.Value)}\r\n";
titlekeys.Write(line);
Out.Log(line);
}
}
}
Out.Event($"All ToolsTasks completed!\r\n");
}
catch (Exception ex)
{
Out.LogException(ex);
}
finally
{
Dispatcher.Invoke(() =>
{
MainGrid.Visibility = Visibility.Visible;
MainGridBusy.Visibility = Visibility.Hidden;
});
}
}
}
}