/
MainWindow.xaml.cs
1093 lines (988 loc) · 40.4 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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Microsoft.Win32;
using System.Windows;
using gablibela;
using gablibela.arc;
using gablibela.cmpr;
using System.Linq;
using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Input;
using System.Collections.Generic;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Collections.Specialized;
using System.Net;
using System.Text.Json;
namespace ArchiveExplorer
{
public partial class MainWindow : Window
{
public static readonly string AppTag = "AE";
public static readonly string Version = "v0.24";
public MainWindow()
{
InitializeComponent();
this.AllowDrop= true;
this.Drop += MainWindow_Drop;
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(ExceptionHandler);
string[] args = Environment.GetCommandLineArgs();
if(args.Length > 1)
{
TryOpenFile(args[1]);
}
CheckForUpdate();
#if DEBUG
AllocConsole();
Console.WriteLine($"RANWr ArchiveExplorer {AppTag}.{Version} Debug Build - {Properties.Resources.BuildDate}");
#endif
}
[DllImport("Kernel32")]
public static extern void AllocConsole();
[DllImport("Kernel32")]
public static extern void FreeConsole();
private void CheckForUpdate()
{
string html = HTMLGetRequest("https://api.github.com/repos/Gabriela-Orzechowska/RANWr/releases");
github_releases[] releases = null;
try
{
releases = JsonSerializer.Deserialize<github_releases[]>(html);
}
catch
{
return;
}
if (releases == null) return;
int baseVersion = int.Parse(Version.Replace(".", "").Substring(1));
int version = baseVersion;
string versionUri = string.Empty;
string downloadUri = string.Empty;
string _body = "";
foreach (var release in releases)
{
if (release.tag_name.StartsWith(AppTag + "."))
{
var newVersion = int.Parse(release.tag_name.Replace(".", "").Substring(AppTag.Length + 2));
if (newVersion > version)
{
version = newVersion;
versionUri = release.html_url;
_body = release.body;
if (release.assets.Length > 0)
downloadUri = release.assets[0].browser_download_url;
}
}
}
if (version != baseVersion)
{
MessageBoxResult result = MessageBox.Show($"There's a new version available! Would you like to go to the download page?\n\nChangelog:\n{_body}", "New Version!", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
OpenLinkInBrowser(downloadUri);
}
}
}
public struct github_releases
{
public string html_url { get; set; }
public string tag_name { get; set; }
public string name { get; set; }
public string body { get; set; }
public github_assets[] assets { get; set; }
}
public struct github_assets
{
public string url { get; set; }
public string name { get; set; }
public string browser_download_url { get; set; }
}
public string HTMLGetRequest(string uri)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
request.Headers["User-Agent"] = "RANWr Auto-Updater";
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
catch
{
return "";
}
}
static void ExceptionHandler(object sender, UnhandledExceptionEventArgs args)
{
var exception = args.ExceptionObject is Exception ? ((Exception)args.ExceptionObject).Message + ((Exception)args.ExceptionObject).StackTrace : string.Empty;
MessageBox.Show("Whoops! Please contact the developers with the following information:\r\n\r\n" + exception,
"Fatal Error", MessageBoxButton.OK, MessageBoxImage.Stop);
}
public Dictionary<TreeViewItem, DARCH.Node> nodeConnects = new();
public DARCH currentFile;
public DARCH.Node currentNode;
public string currentFilePath;
public Dictionary<string, string> fileTypes = new Dictionary<string, string>()
{
//Effects
{ ".breft", "Binary Revolution Effect Textures" },
{ ".breff", "Binary Revolution Effect File" },
//Menu Files
{ ".bmg", "Binary Message Group" },
{ ".brctr", "Binary Revolution Control" },
{ ".brfnt", "Binary Revolution Font" },
{ ".brlan", "Binary Revolution Layout Animation" },
{ ".brlyt", "Binary Revolution Layout" },
{ ".thp", "THP Movie File" },
{ ".tpl", "Texture Palette Library" },
//Sound
{ ".brsar", "Binary Revolution Sound Archive" },
{ ".brwar", "Binary Revolution Wave Archive" },
{ ".brstm", "Binary Revolution Stream Sound" },
{ ".brseq", "Binary Revolution Sequence" },
{ ".brwsd", "Binary Revolution Wave Sound Data" },
{ ".brasd", "Binary Revolution Animation Sound Data" },
//Resources
{ ".brres", "Binary Revolution Resource" },
//All BRRES Formats ffs
{ ".brmdl", "Binary Revolution Model Resource" },
{ ".brtex", "Binary Revolution Texture Resource" },
{ ".brcha", "Binary Revolution Character Animation Resource" },
{ ".brcla", "Binary Revolution Color Animation Resource" },
{ ".brplt", "Binary Revolution Palette Resource" },
{ ".brsca", "Binary Revolution Scene Animation Resource" },
{ ".brsha", "Binary Revolution Shape Animation Resource" },
{ ".brtpa", "Binary Revolution Texture Pattern Animation Resource" },
{ ".brtsa", "Binary Revolution Texture SRT Animation Resource" },
{ ".brvia", "Binary Revolution Visibility Animation Resource" },
//Mario Kart Wii Files
{ ".kcl", "Mario Kart Wii Collision File" },
{ ".kmp", "Mario Kart Wii Map Parameters" },
{ ".krm", "Mario Kart Wii Rumble" },
{ ".bsp", "Binary Settings and Physics" },
{ ".btiEnv", "Binary Texture Information" },
{ ".bti", "Binary Texture Information" },
{ ".btiMat", "Binary Texture Information" },
{ ".rkc", "Mario Kart Wii Competition File" },
{ ".rkg", "Mario Kart Wii Ghost File" },
{ ".ikp", "Inverse Kinematics Parameters" },
{ ".bcp", "Binary Camera Parameters" },
{ ".bmm", "Binary Mii Material" },
//Mario Kart Wii PostEffects
{ ".bdof", "Binary Depth of Field" },
{ ".bblm", "Binary Bloom" },
{ ".blmap", "Binary Light Map" },
{ ".bfg", "Binary Fog" },
{ ".blight", "Binary Lighting" },
};
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if(Keyboard.Modifiers == ModifierKeys.Control)
{
if(Keyboard.Modifiers == ModifierKeys.Shift)
{
switch(e.Key)
{
case Key.S:
saveFile(); break;
}
}
else
{
switch (e.Key)
{
case Key.S:
saveFile(2); break;
case Key.C:
CopyFiles(); break;
case Key.V:
PasteFiles(); break;
case Key.D:
DuplicateFile(); break;
case Key.N:
CreateNewFile(); break;
case Key.O:
OpenFileDialog(); break;
case Key.I:
ImportFileDialog(); break;
case Key.R:
ReplaceFileDialog(); break;
}
}
}
else if(e.Key == Key.Delete)
{
if (currentFile == null) return;
if (FileView.SelectedItems.Count > 0)
{
List<FileListItem> items = new();
foreach(var selItem in FileView.SelectedItems)
{
items.Add(selItem as FileListItem);
}
DeleteFile(items.ToArray());
return;
}
}
else if(e.Key == Key.Enter)
{
if (currentFile == null) return;
FileListItem item = FileView.SelectedItem as FileListItem;
if(Keyboard.Modifiers == ModifierKeys.Shift)
OpenCurrentSelectedWith(item);
else
OpenCurrentSelected(item);
}
}
private void CopyFiles()
{
if (FileView.SelectedItems.Count == 0) return;
StringCollection paths = new StringCollection();
foreach (FileListItem item in FileView.SelectedItems)
{
DARCH.Node node = item.Node;
string nodePath = currentFile.GetNodePath(node);
string exportPath = currentFile.PathCombine(currentFile.TemporaryPath, nodePath);
paths.Add(exportPath);
}
Clipboard.SetFileDropList(paths);
}
private void PasteFiles()
{
if (Clipboard.ContainsFileDropList())
{
var files = Clipboard.GetFileDropList();
foreach (var file in files)
{
TryImportFile(file);
}
}
}
private void DuplicateFile()
{
FileListItem item = FileView.SelectedItem as FileListItem;
if (item == null) return;
DARCH.Node node = item.Node;
string nodePath = currentFile.GetNodePath(node);
string exportPath = currentFile.PathCombine(currentFile.TemporaryPath, nodePath);
TryImportFile(exportPath, true);
}
private void FileView_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
if (currentFile == null)
{
e.Handled = true;
FileView.ContextMenu.IsOpen = false;
}
}
private void NewFile_Click(object sender, RoutedEventArgs e) => CreateNewFile();
private void CreateNewFile()
{
if (currentFile != null)
{
MessageBoxResult result = MessageBox.Show("Do you want to save before quitting?", "Warning", MessageBoxButton.YesNoCancel);
if (result == MessageBoxResult.Yes)
saveFile();
else if (result == MessageBoxResult.Cancel) return;
currentFile.FreeArchive();
}
DARCH darch = new();
darch.name = "untitled.arc";
currentFile = darch;
UpdateTreeView();
currentNode = currentFile.structure;
UpdateListView(currentFile.structure);
UpdatePath();
}
private void OpenFile_Click(object sender, RoutedEventArgs e) => OpenFileDialog();
private void OpenFileDialog()
{
var dialog = new OpenFileDialog();
dialog.Filter = "Nintendo DARCH Archive|*.arc;*.szs;*.u8";
bool? result = dialog.ShowDialog();
if (result == true)
{
TryOpenFile(dialog.FileName);
}
}
private void saveFile(byte level = 10)
{
if (currentFile == null) return;
currentFile.UpdateAllNodeData();
byte[] saveData = currentFile.EncodeDARCH();
if(Path.GetExtension(currentFilePath) == ".szs") saveData = YAZ0.Compress(saveData, level);
File.WriteAllBytes(currentFilePath, saveData);
}
private void QuickSave_Click(object sender, RoutedEventArgs e)
{
if (currentFile == null) return;
if (currentFilePath != null) saveFile(2);
else
{
QuickSaveAs_Click(sender, e);
}
}
private void QuickSaveAs_Click(object sender, RoutedEventArgs e)
{
var dialog = new SaveFileDialog();
dialog.Filter = "All supported files|*.szs;*.arc;*.u8|Compressed Archive|*.szs|Nintendo DARCH Archive|*.arc;*.u8";
bool? result = dialog.ShowDialog();
if (result == true)
{
currentFilePath = dialog.FileName;
saveFile(2);
currentFile.name = Path.GetFileName(currentFilePath);
UpdateTreeView();
}
}
private void FullSave_Click(object sender, RoutedEventArgs e)
{
if (currentFile == null) return;
if (currentFilePath != null) saveFile();
else
{
FullSaveAs_Click(sender, e);
}
}
private void FullSaveAs_Click(object sender, RoutedEventArgs e)
{
var dialog = new SaveFileDialog();
dialog.Filter = "All supported files|*.szs;*.arc;*.u8|Compressed Archive|*.szs|Nintendo DARCH Archive|*.arc;*.u8";
bool? result = dialog.ShowDialog();
if (result == true)
{
currentFilePath = dialog.FileName;
saveFile();
currentFile.name = Path.GetFileName(currentFilePath);
UpdateTreeView();
}
}
private void ImportFile_Click(object sender, RoutedEventArgs e) => ImportFileDialog();
private void ImportFileDialog()
{
if(currentFile == null) return;
var dialog = new OpenFileDialog();
dialog.Multiselect = true;
bool? result = dialog.ShowDialog();
if (result == true)
{
foreach (var file in dialog.FileNames)
{
var extension = Path.GetExtension(file);
if (extension == "*.szs" || extension == "*.arc" || extension == "*.u8") continue;
TryImportFile(file,false,false);
}
}
}
private void ReplaceFile_Click(object sender, RoutedEventArgs e)
{
ReplaceFileDialog();
}
private void ReplaceFileDialog()
{
if (currentFile == null) return;
var dialog = new OpenFileDialog();
dialog.Multiselect = false;
bool? result = dialog.ShowDialog();
if (result == true)
{
var file = dialog.FileName;
var extension = Path.GetExtension(file);
if (extension == "*.szs" || extension == "*.arc" || extension == "*.u8") return;
var currentItem = FileView.SelectedItem as FileListItem;
if (currentItem == null) return;
TryImportFile(file, false, false, currentItem.Text, true);
}
}
public void MainWindow_Drop(object sender, DragEventArgs e)
{
if (e.Data is DataObject && ((DataObject)e.Data).ContainsFileDropList())
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (var file in files)
{
TryImportFile(file);
}
}
}
private void GoBack_Click(object sender, RoutedEventArgs e)
{
if(currentNode.Parent != null)
{
currentNode = currentNode.Parent;
}
UpdatePath();
UpdateListView(currentNode);
}
private void FolderView_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
TreeViewItem item = (sender as TreeView).SelectedItem as TreeViewItem;
if (item == null) return;
if (nodeConnects.TryGetValue(item, out DARCH.Node node))
{
if (node == null) return;
currentNode = node;
UpdatePath();
UpdateListView(currentNode);
}
}
private void FileView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
FileListItem item = ((FrameworkElement)e.OriginalSource).DataContext as FileListItem;
if (e.ChangedButton != MouseButton.Left) return;
OpenCurrentSelected(item);
}
private void OpenCurrentSelected(FileListItem item)
{
if (item == null) return;
DARCH.Node node = item.Node;
if (node.Type == DARCH.Node.NodeType.File)
{
currentFile.OpenNode(node);
}
else
{
currentNode = node;
UpdatePath();
UpdateListView(currentNode);
}
}
private void OpenMenuItem_Click(object sender, RoutedEventArgs e)
{
FileListItem item = ((MenuItem)sender).DataContext as FileListItem;
if (item == null) return;
DARCH.Node node = item.Node;
if (node.Type == DARCH.Node.NodeType.File)
{
currentFile.OpenNode(node);
}
else
{
currentNode = node;
UpdatePath();
UpdateListView(currentNode);
}
}
private void OpenWithMenuItem_Click(object sender, RoutedEventArgs e)
{
FileListItem item = ((MenuItem)sender).DataContext as FileListItem;
OpenCurrentSelectedWith(item);
}
private void OpenCurrentSelectedWith(FileListItem item)
{
if (item == null) return;
DARCH.Node node = item.Node;
if (node.Type == DARCH.Node.NodeType.File)
{
currentFile.OpenWithNode(node);
}
}
StackPanel lastListViewItemPanel = null;
FileListItem lastItem = null;
string originalName = null;
private void FileItem_MouseEnter(object sender, MouseEventArgs e)
{
lastListViewItemPanel = sender as StackPanel;
}
private void FileView_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
FileListItem item = ((ListView)sender).SelectedItem as FileListItem;
if(item == null) return;
}
private void RenameItem_Click(object sender, RoutedEventArgs e)
{
lastItem = ((MenuItem)sender).DataContext as FileListItem;
RenameFile();
}
private void RenameFile()
{
foreach (var child in lastListViewItemPanel.Children)
{
if (child is TextBox)
{
TextBox textBox = (TextBox)child;
originalName = textBox.Text;
textBox.IsReadOnly = false;
textBox.Focusable = true;
textBox.Focus();
textBox.CaretBrush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 255, 255, 255));
textBox.Cursor = Cursors.IBeam;
textBox.BorderThickness = new Thickness(1);
textBox.SelectAll();
}
}
}
private void CreateFolderMenuItem_Click(object sender, RoutedEventArgs e)
{
string newFolderName = currentFile.GetFirstNameAvailable("New Folder", currentNode);
DARCH.Node node = currentFile.AddNode(newFolderName, DARCH.Node.NodeType.Directory, currentNode, true);
currentFile.ExportNode(node);
UpdateListView(currentNode);
UpdateTreeView();
}
private void CopyItem_Click(object sender, RoutedEventArgs e)
{
CopyFiles();
}
private void PasteItem_Click(object sender, RoutedEventArgs e)
{
PasteFiles();
}
private void DeleteItem_Click(object sender, RoutedEventArgs e)
{
if (FileView.SelectedItems.Count > 0)
{
List<FileListItem> items = new();
foreach (var selItem in FileView.SelectedItems)
{
items.Add(selItem as FileListItem);
}
DeleteFile(items.ToArray());
return;
}
}
private void DeleteFile(FileListItem[] items = null)
{
if (items == null) return;
string messageBoxText = "Are you sure to delete these files?";
if (items.Length == 1) messageBoxText = "Are you sure to delete this file?";
MessageBoxResult result = MessageBox.Show(messageBoxText, "Delete file?", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
foreach(var item in items)
currentFile.RemoveNode(item.Node);
UpdateListView(currentNode);
UpdateTreeView();
}
}
private void DeleteFile(FileListItem item = null)
{
if (item == null) return;
MessageBoxResult result = MessageBox.Show("Are you sure to delete this file?", "Delete file?", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
currentFile.RemoveNode(item.Node);
UpdateListView(currentNode);
}
UpdateTreeView();
}
private void FileTextBox_KeyDown(object sender, KeyEventArgs e)
{
TextBox box = (TextBox)sender;
if (e.Key == Key.Enter)
{
if (!box.IsReadOnly)
{
box.IsReadOnly = true;
box.BorderThickness = new Thickness(0);
if (originalName == box.Text) return;
RenameFile(lastItem, box.Text, box);
box.CaretBrush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0,0,0,0));
box.Cursor = Cursors.Arrow;
box.Focusable = false;
}
}
else if (e.Key == Key.Escape)
{
if (!box.IsReadOnly)
{
box.IsReadOnly = true;
box.BorderThickness = new Thickness(0);
box.Text = originalName;
box.CaretBrush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0, 0, 0, 0));
box.Cursor = Cursors.Arrow;
box.Focusable = false;
}
}
UpdateTreeView();
}
private void FileTextBox_LostFocus(object sender, RoutedEventArgs e)
{
TextBox box = (TextBox)sender;
if (!box.IsReadOnly)
{
box.IsReadOnly = true;
box.BorderThickness = new Thickness(0);
box.CaretBrush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0, 0, 0, 0));
box.Cursor = Cursors.Arrow;
box.Focusable = false;
if (originalName == box.Text) return;
RenameFile(lastItem, box.Text, box);
}
}
private void RenameFile(FileListItem item, string newName, TextBox box)
{
string newerName = currentFile.GetFirstNameAvailable(newName, currentNode);
currentFile.RenameNode(item.Node, newName);
if(newName != newerName)
{
box.Text = newerName;
}
currentFile.RecalculateStructureIndexes();
UpdateListView(currentNode);
UpdateTreeView();
}
private void TryImportFile(string filepath, bool forceDuplicate = false, bool importDARCH = true, string originalPath = null, bool forceOverride = false)
{
bool isDirectory = File.GetAttributes(filepath).HasFlag(FileAttributes.Directory);
byte[] data;
string name = Path.GetFileName(filepath);
if(originalPath != null) name = Path.GetFileName(originalPath);
if (!isDirectory)
{
data = File.ReadAllBytes(filepath);
var signature = BitConverter.ToUInt32(data.Take(4).Reverse().ToArray());
if (signature == DARCH.Signature || signature == YAZ0.SignatureHex)
{
if(importDARCH) TryOpenFile(filepath);
return;
}
if (currentFile == null) return;
var curFolderPath = Path.Combine(currentFile.TemporaryPath, currentFile.GetNodePath(currentNode));
var folderPath = Path.Combine(curFolderPath, name);
if(Path.Exists(folderPath))
{
if (forceDuplicate)
{
string newName = currentFile.GetFirstNameAvailable(name, currentNode);
folderPath = Path.Combine(curFolderPath, newName);
File.Copy(filepath, folderPath, true);
}
else if (forceOverride)
{
if (folderPath == filepath) return;
File.Copy(filepath, folderPath, true);
}
else
{
MessageBoxResult result = MessageBox.Show($"File {Path.GetFileName(folderPath)} already exists, replace it?", "Warning", MessageBoxButton.YesNoCancel);
if (result == MessageBoxResult.Yes)
{
if (folderPath == filepath) return;
File.Copy(filepath, folderPath, true);
}
else if (result == MessageBoxResult.No)
{
string newName = currentFile.GetFirstNameAvailable(name, currentNode);
folderPath = Path.Combine(curFolderPath, newName);
File.Copy(filepath, folderPath, true);
}
}
}
else
File.Copy(filepath, folderPath, true);
}
else
{
if (currentFile == null) return;
var curFolderPath = Path.Combine(currentFile.TemporaryPath, currentFile.GetNodePath(currentNode));
var folderPath = Path.Combine(curFolderPath, name);
bool somethingExists = false;
if (Path.Exists(folderPath))
{
List<string> allfiles = Directory.GetFileSystemEntries(folderPath, "*", SearchOption.AllDirectories).ToList();
foreach (string file in allfiles)
{
if (Path.Exists(file)) somethingExists = true; break;
}
}
Directory.CreateDirectory(folderPath);
if (somethingExists)
{
MessageBoxResult result = MessageBox.Show($"Replace files?", "Warning", MessageBoxButton.YesNo);
CopyFilesRecursively(filepath, folderPath, result == MessageBoxResult.Yes);
}
else
{
CopyFilesRecursively(filepath, folderPath, true);
}
}
currentFile.UpdateAllNodeData();
UpdateListView(currentNode);
UpdateTreeView();
}
private void TryOpenFile(string filepath)
{
if(currentFile != null)
{
MessageBoxResult result = MessageBox.Show("Do you want to save before quitting?", "Warning", MessageBoxButton.YesNoCancel);
if (result == MessageBoxResult.Yes)
{
if (currentFilePath != null) saveFile();
else
{
var dialog = new SaveFileDialog();
dialog.Filter = "All supported files|*.szs;*.arc;*.u8|Compressed Archive|*.szs|Nintendo DARCH Archive|*.arc;*.u8";
bool? newResult = dialog.ShowDialog();
if (newResult == true)
{
currentFilePath = dialog.FileName;
saveFile();
}
}
}
else if (result == MessageBoxResult.Cancel) return;
currentFile.FreeArchive();
}
DARCH darch = GetArchive(filepath, Path.GetFileName(filepath));
if (darch == null) return;
currentFilePath = filepath;
currentFile = darch;
darch.ExportAllNodes();
UpdateTreeView();
UpdateListView(currentFile.structure);
currentNode = currentFile.structure;
UpdatePath();
}
private void UpdatePath()
{
string path = currentFile.GetNodePath(currentNode);
PathBar.Text = path;
}
private void UpdateTreeView()
{
if (currentFile == null) return;
nodeConnects.Clear();
FolderView.Items.Clear();
TreeViewItem archiveNode = GetNodeItem(currentFile.structure);
archiveNode.Tag = currentFile.name;
archiveNode.Header = currentFile.name;
archiveNode.IsExpanded = true;
FolderView.Items.Add(archiveNode);
nodeConnects.Add(archiveNode, currentFile.structure);
}
private static void CopyFilesRecursively(string sourcePath, string targetPath, bool replace = true)
{
//Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
{
Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath));
}
//Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories))
{
File.Copy(newPath, newPath.Replace(sourcePath, targetPath), replace);
}
}
private void UpdateListView(DARCH.Node node)
{
List<DARCH.Node> nodes = node.Children.OrderByDescending(x => x.Type).ToList();
FileView.Items.Clear();
foreach(var child in nodes)
{
FileListItem item = new FileListItem();
if (child.Type == DARCH.Node.NodeType.Directory)
{
var uri = new Uri("/Icons/FolderOpened.png", UriKind.Relative);
item.Icon = new BitmapImage(uri);
item.Size = "";
}
else
{
item.Icon = IconManager.FindIconForFilename(child.Name, true);
item.Size = FormatFileSize(child.DataSize);
}
item.Node = child;
item.Text = child.Name;
item.Type = "";
var digits = new[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
var extension = Path.GetExtension(child.Name);
if (fileTypes.TryGetValue(extension.TrimEnd(digits), out var newName))
{
item.Type = newName;
}
item.isDir = child.Type == DARCH.Node.NodeType.Directory;
item.isFile = !item.isDir;
FileView.Items.Add(item);
}
}
public class FileListItem
{
public ImageSource Icon { get; set; }
public string Text { get; set; }
public string Size { get; set; }
public DARCH.Node Node { get; set; }
public string Type { get; set; }
public bool isDir { get; set; }
public bool isFile { get; set; }
}
public static string FormatFileSize(long bytes)
{
var unit = 1024;
if (bytes < unit) { return $"{bytes} B"; }
var exp = (int)(Math.Log(bytes) / Math.Log(unit));
return $"{bytes / Math.Pow(unit, exp):F2} {("KMGTPE")[exp - 1]}B";
}
private TreeViewItem GetNodeItem(DARCH.Node node)
{
TreeViewItem curNodeItem = new();
curNodeItem.IsExpanded = true;
if(node.Children.Count > 0)
{
foreach(var child in node.Children)
{
if (child.Type == DARCH.Node.NodeType.File) continue;
TreeViewItem childNodeItem = GetNodeItem(child);
childNodeItem.IsExpanded = true;
childNodeItem.Header = child.Name;
curNodeItem.Items.Add(childNodeItem);
nodeConnects.Add(childNodeItem, child);
}
}
return curNodeItem;
}
protected override void OnClosing(CancelEventArgs e)
{
if (currentFile != null)
{
MessageBoxResult result = MessageBox.Show("Do you want to save before quitting?", "Warning", MessageBoxButton.YesNoCancel);
if (result == MessageBoxResult.Yes)
{
if(currentFilePath != null) saveFile();
else
{
var dialog = new SaveFileDialog();
dialog.Filter = "All supported files|*.szs;*.arc;*.u8|Compressed Archive|*.szs|Nintendo DARCH Archive|*.arc;*.u8";
bool? newResult = dialog.ShowDialog();
if (newResult == true)
{
currentFilePath = dialog.FileName;
saveFile();
}
}
}
else if (result == MessageBoxResult.Cancel)
{
e.Cancel = true;
return;
}
}
base.OnClosing(e);
}
protected override void OnClosed(EventArgs e)
{
if(currentFile != null) currentFile.FreeArchive();
base.OnClosed(e);
}
public static DARCH GetArchive(string path, string filename) => GetArchive(File.ReadAllBytes(path), filename);
public static DARCH GetArchive(byte[] data, string filename)
{
var signature = BitConverter.ToUInt32(data.Take(4).Reverse().ToArray());
if (signature == YAZ0.SignatureHex)
{
data = YAZ0.Decode(data);
signature = BitConverter.ToUInt32(data.Take(4).Reverse().ToArray());
}
if (signature != DARCH.Signature) return null;
return new(data, filename);
}
private void AboutItem_Click(object sender, RoutedEventArgs e) => OpenLinkInBrowser("https://www.youtube.com/watch?v=9cX17CeYKt0");
private void GithubAboutItem_Click(object sender, RoutedEventArgs e) => OpenLinkInBrowser("https://github.com/Gabriela-Orzechowska/RANWr");
private void OpenLinkInBrowser(string uri)
{
var psi = new System.Diagnostics.ProcessStartInfo();
psi.UseShellExecute = true;
psi.FileName = uri;
System.Diagnostics.Process.Start(psi);
}
}
public static class IconManager