forked from Radfordhound/HedgeLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainFrm.cs
1336 lines (1152 loc) · 51.4 KB
/
MainFrm.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 HedgeLib.Archives;
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace HedgeArchiveEditor
{
public partial class MainFrm : Form
{
// Variables/Constants
public static string tempPath = Path.Combine(Path.GetTempPath(), "HedgeArchiveEditor\\");
public Dictionary<Archive, string> ArchiveFilePaths = new Dictionary<Archive, string>();
public Dictionary<Archive, ArchiveDirectory> ArchiveCurrentDir = new Dictionary<Archive, ArchiveDirectory>();
public List<Archive> Archives = new List<Archive>();
public Archive CurrentArchive
{
get
{
return (tabControl.SelectedIndex >= 0 &&
tabControl.SelectedIndex < Archives.Count) ?
Archives[tabControl.SelectedIndex] : null;
}
set
{
if (tabControl.SelectedIndex >= 0 && tabControl.SelectedIndex < Archives.Count)
Archives[tabControl.SelectedIndex] = value;
}
}
private bool extracting, extracted = false;
// Constructors
public MainFrm()
{
InitializeComponent();
UpdateTitle();
Directory.CreateDirectory(tempPath);
}
// Methods
public void UpdateTitle()
{
Text = ((tabControl.TabPages.Count > 0) ?
$"{tabControl.SelectedTab.Text} - " : "") + Program.ProgramName;
}
public string GetFilters(bool includeAllArchives)
{
string filters = "";
if (includeAllArchives)
{
filters += "All Supported Archives (*.ar, *.arl, *.pfd, *.pac, *.one";
// Addons
foreach (var addon in Addon.Addons)
foreach (var archive in addon.Archives)
foreach (string ext in archive.FileExtensions)
filters += $", *{ext}";
filters += ")|*.ar;*.arl;*.pfd;*.pac;*.one";
// Addons
foreach (var addon in Addon.Addons)
foreach (var archive in addon.Archives)
foreach (string ext in archive.FileExtensions)
filters += $";*{ext}";
}
// Generations/Unleashed
filters += "|Generations/Unleashed Archives (*.ar, *.arl, *.pfd)|*.ar;*.arl;*.pfd";
// Lost World
filters += "|Sonic Forces Archives (*.pac)|*.pac";
// StoryBooks
filters += "|StoryBook Series Archives (*.one)|*.one";
// Heroes/Shadow
filters += "|Heroes Archives (*.one)|*.one";
// Addons
filters += AddFiltersFromAddons();
// All Files
filters += "|All Files (*.*)|*.*";
if (!includeAllArchives)
filters = filters.Substring(1);
return filters;
}
public void OpenArchive(string filePath)
{
try
{
var archive = Program.LoadArchive(filePath);
Archives.Add(archive);
ArchiveFilePaths.Add(archive, filePath);
ArchiveCurrentDir.Add(archive, null);
archive.Saved = true;
AddTabPage(new FileInfo(filePath).Name);
}catch (Exception ex)
{
MessageBox.Show(ex.Message, Program.ProgramName,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
RefreshGUI();
}
public void SaveArchive(int index, bool saveAs)
{
string fileLocation = "";
int ArchiveType = -1;
var archive = Archives[index];
if (ArchiveFilePaths[archive] == null || saveAs)
{
var sfd = new SaveFileDialog()
{
Title = "Save Archive As...",
Filter = GetFilters(false)
};
if (sfd.ShowDialog() == DialogResult.OK)
{
ArchiveType = sfd.FilterIndex - 1;
fileLocation = sfd.FileName;
ArchiveFilePaths[archive] = fileLocation;
}
else return;
}
else
{
fileLocation = ArchiveFilePaths[archive];
// These checks may not work.
var type = Archives[index].GetType();
if (type == typeof(GensArchive)) ArchiveType = 0;
else if (type == typeof(ForcesArchive)) ArchiveType = 1;
else if (type == typeof(SBArchive)) ArchiveType = 2;
else if (type == typeof(ONEArchive)) ArchiveType = 3;
}
if (ArchiveType == -1)
{
if (fileLocation.EndsWith(GensArchive.ListExtension) ||
fileLocation.EndsWith(GensArchive.Extension) ||
fileLocation.EndsWith(GensArchive.SplitExtension) ||
fileLocation.EndsWith(GensArchive.PFDExtension))
ArchiveType = 0; // Generations/Unleashed
else if (fileLocation.EndsWith(ForcesArchive.Extension))
ArchiveType = 1; // Lost World
else if (fileLocation.EndsWith(SBArchive.Extension))
ArchiveType = 2; // Story Books
else if (fileLocation.EndsWith(ONEArchive.Extension)) // NOTE: This never gets called
ArchiveType = 3; // Heroes/Shadow
// Addons
if (ArchiveType == -1)
{
int i = 3;
foreach (var addon in Addon.Addons)
foreach (var addonArchive in addon.Archives)
{
i++;
if (addonArchive.FileExtensions.Contains
(Path.GetExtension(fileLocation.ToLower())))
ArchiveType = i;
}
}
}
var saveOptions = new SaveOptions(ArchiveType);
// Automatically set the Magic value if its an ONEArchive
if (archive.GetType() == typeof(ONEArchive))
saveOptions.NumericUpDown1.Value = ((ONEArchive)archive).Magic;
if (saveOptions.ShowDialog() == DialogResult.OK && saveOptions.ArchiveType != -1)
{
// This is a horrible way of checking this, I know
switch (saveOptions.ComboBox1.SelectedIndex)
{
// Generations/Unleashed
case 0:
uint? splitAmount = (saveOptions.CheckBox2.Checked) ?
(uint?)saveOptions.NumericUpDown2.Value : null;
var genArc = new GensArchive(archive)
{
Padding = (uint)saveOptions.NumericUpDown1.Value
};
if (saveOptions.CheckBox3.Checked && saveOptions.CheckBox2.Checked)
genArc.GetSplitArchivesList(fileLocation)
.ForEach(file => File.Delete(file));
genArc.Save(fileLocation, saveOptions.CheckBox1.Checked, splitAmount);
break;
// Forces
case 1:
var fArc = new ForcesArchive(archive);
fArc.Save(fileLocation, true);
break;
// Story Books
case 2:
var sbArc = new SBArchive(archive);
sbArc.Save(fileLocation, true);
break;
// Heroes/Shadow
case 3:
var oneArc = new ONEArchive(archive)
{
Magic = (uint)saveOptions.NumericUpDown1.Value
};
oneArc.Save(fileLocation, true);
break;
default:
int archiveIndex = saveOptions.ComboBox1.SelectedIndex - 4;
int i = 0;
foreach (var addon in Addon.Addons)
foreach (var addonArchive in addon.Archives)
{
if (i++ == archiveIndex)
{
var arc = Activator.CreateInstance(
addonArchive.ArchiveType) as Archive;
arc.Data = archive.Data;
arc.Save(fileLocation, true);
}
}
break;
}
archive.Saved = true;
}
RefreshTabPage(index, false);
}
public void CloseArchive(int index)
{
if (!Archives[index].Saved)
{
if (MessageBox.Show("Save Archive before closing?", Text,
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
try { SaveArchive(index, false); }
catch { return; }
}
}
ArchiveFilePaths.Remove(Archives[index]);
ArchiveCurrentDir.Remove(Archives[index]);
Archives.RemoveAt(index);
tabControl.TabPages.RemoveAt(index);
}
public void AddTabPage(string fileName)
{
tabControl.TabPages.Add(fileName);
int tabPageIndex = tabControl.TabPages.Count - 1;
var tabPage = tabControl.TabPages[tabPageIndex];
tabPage.Tag = fileName;
var listView = new ListViewSort()
{
Dock = DockStyle.Fill,
View = View.Details,
MultiSelect = true,
FullRowSelect = true,
AllowColumnReorder = true,
LabelEdit = true
};
listView.ContextMenuStrip = contextMenu;
// Mouse Events
listView.MouseMove += Lv_MouseMove;
listView.MouseUp += Lv_MouseUp;
listView.MouseDoubleClick += Lv_MouseDoubleClick;
// Other Events
listView.KeyPress += new KeyPressEventHandler(Lv_KeyPress);
listView.BeforeLabelEdit += new LabelEditEventHandler(Lv_BeforeLabelEdit);
listView.AfterLabelEdit += new LabelEditEventHandler(Lv_AfterLabelEdit);
// Columns
listView.Columns.Add("Name");
listView.Columns.Add("Extension");
listView.Columns.Add("Size");
tabPage.Controls.Add(listView);
RefreshTabPage(tabPageIndex);
tabControl.SelectedIndex = tabPageIndex;
}
public void RefreshTabPage(int index, bool refreshFileList = true)
{
if(index < 0)
return;
var tabPage = tabControl.TabPages[index];
var listView = tabPage.Controls[0] as ListView;
var archive = Archives[index];
// Update TabPage Text
tabPage.Text = (tabPage.Tag as string) + (archive.Saved ? "" : "*");
UpdateTitle();
// Update File List
if (!refreshFileList || listView == null) return;
var files = ArchiveCurrentDir[archive] == null ?
archive.Data : ArchiveCurrentDir[archive].Data;
UpdateList(files, ArchiveCurrentDir[archive] == null);
}
public void UpdateList(List<ArchiveData> dataInDirectory, bool isRoot)
{
var tabPage = tabControl.SelectedTab;
var listView = tabPage.Controls[0] as ListView;
var archive = CurrentArchive;
var files = archive.Data; // Files to add to list
var items = new List<ListViewItem>(); // List of Items to be added later
// Stops the ListView from drawing until we call EndUpdate
listView.BeginUpdate();
// If listView is set to Details View
if (listView.View == View.Details)
{
listView.SmallImageList = new ImageList();
listView.SmallImageList.ColorDepth = ColorDepth.Depth32Bit;
listView.SmallImageList.Images.Add("-", GetIconFromExtension("-"));
}
var imgList = listView.LargeImageList ?? listView.SmallImageList;
// Folder Icon
ExtractIconEx("shell32.dll", 4, out IntPtr largePointer, out IntPtr SmallPointer, 1);
imgList.Images.Add("-Directory",
Icon.FromHandle(largePointer));
// Clears/Removes all the items from the ListView.
listView.Items.Clear();
// Lengths
int longestNameLength = 0, longestExtensionLength = 0, longestSizeLength = 0;
if (!isRoot)
{
// Current Directory
var dir = ArchiveCurrentDir[archive];
files = dir.Data; // Change List to the Current Directory's List
var lvi = new ListViewItem("..");
lvi.Tag = dir.Parent; // Paent Directory
lvi.ImageKey = "-Directory"; // Directory Icon
if (lvi.Text.Length > longestNameLength)
longestNameLength = lvi.Text.Length;
items.Insert(0, lvi);
}
for (int i = 0; i < dataInDirectory.Count; ++i)
{
var data = dataInDirectory[i];
var lvi = new ListViewItem();
if (data is ArchiveFile file)
{
var fileInfo = new FileInfo(file.Name);
lvi = new ListViewItem(new string[]
{
fileInfo.Name,
fileInfo.Extension,
file.Data != null ?
ConvertSize(file.Data.LongLength) : null
});
try
{
// Sets the ImageKey to the current file.
if (fileInfo.Extension.Length == 0)
lvi.ImageKey = "-";
else
{
if (!imgList.Images.ContainsKey(fileInfo.Extension))
imgList.Images.Add(fileInfo.Extension, GetIconFromExtension(fileInfo.Extension));
lvi.ImageKey = fileInfo.Extension;
}
}
catch { }
if (lvi.Text.Length > longestNameLength)
longestNameLength = lvi.Text.Length;
if (lvi.SubItems[1].Text.Length > longestExtensionLength)
longestExtensionLength = lvi.SubItems[1].Text.Length;
if (lvi.SubItems[2].Text.Length > longestSizeLength)
longestSizeLength = lvi.SubItems[2].Text.Length;
}
else if (data is ArchiveDirectory directory)
{
lvi = new ListViewItem(directory.Name);
lvi.Tag = directory;
lvi.ImageKey = "-Directory";
if (lvi.Text.Length > longestNameLength)
longestNameLength = lvi.Text.Length;
}
else
continue; // Skip this object
lvi.Tag = data;
items.Add(lvi);
}
// Adds all the items into the ListView
listView.Items.AddRange(items.ToArray());
// Update the columns in the file list
listView.AutoResizeColumn(0, (longestNameLength > listView.Columns[0].Text.Length) ?
ColumnHeaderAutoResizeStyle.ColumnContent :
ColumnHeaderAutoResizeStyle.HeaderSize);
listView.AutoResizeColumn(1, (longestExtensionLength > listView.Columns[1].Text.Length) ?
ColumnHeaderAutoResizeStyle.ColumnContent :
ColumnHeaderAutoResizeStyle.HeaderSize);
listView.AutoResizeColumn(2, (longestExtensionLength > listView.Columns[2].Text.Length) ?
ColumnHeaderAutoResizeStyle.ColumnContent :
ColumnHeaderAutoResizeStyle.HeaderSize);
listView.EndUpdate();
}
public void RefreshGUI()
{
saveToolStripMenuItem.Enabled = saveAsToolStripMenuItem.Enabled =
addFilesToolStripMenuItem.Enabled = extractAllToolStripMenuItem.Enabled =
closeToolStripMenuItem.Enabled = tabControl.TabPages.Count > 0;
RefreshTabPage(tabControl.SelectedIndex);
if (tabControl.SelectedIndex > -1)
largeIconViewToolStripMenuItem.Checked =
(tabControl.TabPages[tabControl.SelectedIndex].Controls[0] as ListView).View == View.LargeIcon;
// TODO: Update status bar label.
UpdateTitle();
}
public void AddFilesToCurrentArchive(params string[] filePaths)
{
AddFilesToArchive(CurrentArchive, filePaths);
}
/// <summary>
///
/// </summary>
/// <param name="archive">The Archive you want to add the files to</param>
/// <param name="filePaths">An array of file paths</param>
public void AddFilesToArchive(Archive archive, params string[] filePaths)
{
var files = ArchiveCurrentDir[archive] == null ?
archive.Data : ArchiveCurrentDir[archive].Data;
foreach (var file in filePaths)
{
if (File.GetAttributes(file) != FileAttributes.Directory)
{ // File
var fileInfo = new FileInfo(file);
var archiveFile = files.Find(
t => t.Name.ToLower() == fileInfo.Name.ToLower());
if (archiveFile != null)
{
if (MessageBox.Show($"There's already a file called \"{fileInfo.Name}\".\n" +
$"Do you want to replace this file?", Text,
MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
continue;
files.Remove(archiveFile);
}
files.Add(new ArchiveFile(file));
archive.Saved = false;
}
else
{ // Directory
AddDirectoryToArchiveDirectory(archive, null, file);
archive.Saved = false;
}
}
}
public void AddDirectoryToArchiveDirectory(Archive archive, ArchiveDirectory directory, string directoryPath)
{
var files = directory == null ? archive.Data : directory.Data;
var fileInfo = new FileInfo(directoryPath);
var archiveFile = files.Find(
t => t.Name.ToLower() == fileInfo.Name.ToLower());
if (archiveFile != null)
{
if (MessageBox.Show($"There's already a directory called \"{fileInfo.Name}\".\n" +
$"Do you want to merge this directory?", Text,
MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
return;
}
var directoryInfo = new DirectoryInfo(directoryPath);
var newDirectory = new ArchiveDirectory(directoryInfo.Name);
newDirectory.Parent = directory;
foreach (string filePath in Directory.GetFiles(directoryPath))
newDirectory.Data.Add(new ArchiveFile(filePath));
foreach (string directoryPath2 in Directory.GetDirectories(directoryPath, "*",
SearchOption.TopDirectoryOnly))
AddDirectoryToArchiveDirectory(archive, newDirectory, directoryPath2);
files.Add(newDirectory);
}
public string AddFiltersFromAddons()
{
string s = "";
foreach (var addon in Addon.Addons)
{
foreach (var archive in addon.Archives)
{
s += $"|{archive.ArchiveName}|";
foreach (string ext in archive.FileExtensions)
s += $"*{ext};";
s = s.Substring(0, s.Length-1);
}
}
return s;
}
public static bool HasSupportedArchiveExtension(string fileName)
{
string fileExtension = Path.GetExtension(fileName).ToLower();
foreach (var addon in Addon.Addons)
foreach (var archive in addon.Archives)
if (archive.FileExtensions.Contains(fileExtension)) return true;
return (fileExtension == GensArchive.Extension || fileExtension == GensArchive.ListExtension
|| fileExtension == GensArchive.PFDExtension || fileExtension == GensArchive.SplitExtension
|| fileExtension == ForcesArchive.Extension
|| fileExtension == SBArchive.Extension || fileExtension == ONEArchive.Extension);
}
// GUI Events
private void TabControl_SelectedIndexChanged(object sender, EventArgs e)
{
RefreshGUI();
}
// TODO: Allow drag and drop into directories
private void TabControl_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop) &&
e.Data.GetData(DataFormats.FileDrop) is string[] files)
{
if (HasSupportedArchiveExtension(files[0]))
{
foreach (string fileName in files)
OpenArchive(fileName);
RefreshGUI();
}
else
{
var archive = CurrentArchive;
AddFilesToArchive(archive, files);
RefreshGUI();
}
}
}
private void TabControl_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop) &&
e.Data.GetData(DataFormats.FileDrop) is string[] files)
{
// Gets the Current Process PID.
string processId = Process.GetCurrentProcess().Id.ToString();
if (e.Data.GetDataPresent("SourcePID") && (e.Data.GetData("SourcePID") as string == processId))
return;
if (Archives.Count > 0)
e.Effect = DragDropEffects.Copy;
if (files.Length > 0 && HasSupportedArchiveExtension(files[0]))
e.Effect = DragDropEffects.Copy;
}
}
private void ContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
var listView = tabControl.SelectedTab.Controls[0] as ListView;
extractSelectedFilesToolStripMenuItem.Enabled =
removeSelectedFilesToolStripMenuItem.Enabled =
renameSelectedFileToolStripMenuItem.Enabled = listView.SelectedItems.Count > 0;
}
// GUI Events (ToolStripMenuItem)
private void LargeIconViewToolStripMenuItem_Click(object sender, EventArgs e)
{
// Checks if theres a selected tab.
if (tabControl.SelectedIndex >= 0)
{
var listView = tabControl.SelectedTab.Controls[0] as ListView;
if (listView == null) return;
if (listView.View == View.Details)
{ // Set to Large Icons.
listView.LargeImageList = new ImageList()
{
ImageSize = new Size(64, 64),
ColorDepth = ColorDepth.Depth32Bit
};
listView.View = View.LargeIcon;
largeIconViewToolStripMenuItem.CheckState = CheckState.Checked;
listView.LargeImageList.Images.Add("-", GetIconFromExtension("-"));
}
else
{ // Set to Details.
listView.LargeImageList = null;
listView.View = View.Details;
largeIconViewToolStripMenuItem.CheckState = CheckState.Unchecked;
}
// Refreshes the TabPage and ListView.
RefreshTabPage(tabControl.SelectedIndex);
}
}
private void CreateNewArchiveToolStripMenuItem_Click(object sender, EventArgs e)
{
var archive = new Archive();
Archives.Add(archive);
ArchiveFilePaths.Add(archive, null);
ArchiveCurrentDir.Add(archive, null);
AddTabPage("Untitled");
RefreshGUI();
}
private void OpenToolStripMenuItem_Click(object sender, EventArgs e)
{
var ofd = new OpenFileDialog()
{
Title = "Open Archive(s)...",
Filter = GetFilters(true),
Multiselect = true
};
if (ofd.ShowDialog() == DialogResult.OK)
{
try
{
foreach (string file in ofd.FileNames)
OpenArchive(file);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Program.ProgramName,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
RefreshGUI();
}
}
private void AddFilesToolStripMenuItem_Click(object sender, EventArgs e)
{
var ofd = new OpenFileDialog()
{
Title = "Add File(s)...",
Filter = "All Files (*.*)|*.*",
Multiselect = true
};
if (ofd.ShowDialog() == DialogResult.OK)
{
var archive = CurrentArchive;
AddFilesToArchive(archive, ofd.FileNames);
RefreshTabPage(tabControl.SelectedIndex);
}
}
private void NewFolderToolStripMenuItem_Click(object sender, EventArgs e)
{
var archive = CurrentArchive;
var files = ArchiveCurrentDir[archive] == null ?
archive.Data : ArchiveCurrentDir[archive].Data;
// TODO: Allow user to change the directory name at creation
var directory = new ArchiveDirectory("New Folder");
if (files.FindIndex(t => t.Name == "New Folder") != -1)
{
int index = 1;
while (files.FindIndex(t => t.Name == $"New Folder ({index})") != -1)
index++;
directory.Name = $"New Folder ({index})";
}
archive.Saved = false;
if (ArchiveCurrentDir[archive] != null)
directory.Parent = ArchiveCurrentDir[archive];
files.Add(directory);
RefreshTabPage(tabControl.SelectedIndex);
}
private void ExtractAllToolStripMenuItem_Click(object sender, EventArgs e)
{
var sfd = new SaveFileDialog()
{
Title = "Extract all files",
FileName = "Enter into a directory and press Save"
};
if (sfd.ShowDialog() == DialogResult.OK)
{
try
{
var archive = CurrentArchive;
var files = ArchiveCurrentDir[archive] == null ?
archive.Data : ArchiveCurrentDir[archive].Data;
var fileInfo = new FileInfo(sfd.FileName);
var pb = new ToolStripProgressBar();
new System.Threading.Thread(() =>
{
Invoke(new Action(() => Enabled = false));
Process.Start("explorer.exe", fileInfo.Directory.FullName);
statusStrip.Invoke(new Action(() => statusStrip.Items.AddRange(new ToolStripItem[] { pb })));
Invoke(new Action(() => pb.Maximum = files.Count));
foreach (var archiveFile in files)
{
archiveFile.Extract(Path.Combine(fileInfo.Directory.FullName, archiveFile.Name));
Invoke(new Action(() => ++pb.Value));
}
statusStrip.Invoke(new Action(() => statusStrip.Items.Remove(pb)));
Invoke(new Action(() => Enabled = true));
}).Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Program.ProgramName,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
RefreshGUI();
}
}
private void CloseToolStripMenuItem_Click(object sender, EventArgs e)
{
CloseArchive(tabControl.SelectedIndex);
}
private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void ExtractSelectedFilesToolStripMenuItem_Click(object sender, EventArgs e)
{
var sfd = new SaveFileDialog()
{
Title = "Extract selected files",
FileName = "Enter into a directory and press Save"
};
if (sfd.ShowDialog() == DialogResult.OK)
{
try
{
var archive = CurrentArchive;
var files = ArchiveCurrentDir[archive] == null ?
archive.Data : ArchiveCurrentDir[archive].Data;
var listView = tabControl.SelectedTab.Controls[0] as ListView;
var pb = new ToolStripProgressBar();
string directoryPath = new FileInfo(sfd.FileName).Directory.FullName;
new System.Threading.Thread(() =>
{
Invoke(new Action(() => Enabled = false));
statusStrip.Invoke(new Action(() => statusStrip.Items.AddRange(new ToolStripItem[] { pb })));
Process.Start("explorer.exe", directoryPath);
Invoke(new Action(() => pb.Maximum = listView.SelectedItems.Count));
Invoke(new Action(() =>
{
foreach (ListViewItem lvi in listView.SelectedItems)
foreach (var archiveFile in files)
if (archiveFile.Name == lvi.SubItems[0].Text)
{
archiveFile.Extract(Path.Combine(directoryPath, archiveFile.Name));
++pb.Value;
break;
}
}));
statusStrip.Invoke(new Action(() => statusStrip.Items.Remove(pb)));
Invoke(new Action(() => Enabled = true));
}).Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Program.ProgramName,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void RemoveSelectedFilesToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
var archive = CurrentArchive;
var files = ArchiveCurrentDir[archive] == null ?
archive.Data : ArchiveCurrentDir[archive].Data;
var listView = tabControl.SelectedTab.Controls[0] as ListView;
archive.Saved = false;
new System.Threading.Thread(() =>
{
Invoke(new Action(() => Enabled = false));
Invoke(new Action(() =>
{
foreach (ListViewItem lvi in listView.SelectedItems)
files.Remove(files.Find(t => t.Name == lvi.Text));
}));
Invoke(new Action(() => RefreshGUI()));
Invoke(new Action(() => RefreshTabPage(tabControl.SelectedIndex)));
Invoke(new Action(() => Enabled = true));
}).Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Program.ProgramName,
MessageBoxButtons.OK, MessageBoxIcon.Error);
RefreshGUI();
RefreshTabPage(tabControl.SelectedIndex);
}
}
private void CreateFromDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
{
var sfd = new SaveFileDialog()
{
Title = "Create Archive from Directory",
FileName = "Enter into a directory and press Save"
};
if (sfd.ShowDialog() == DialogResult.OK)
{
var fileInfo = new FileInfo(sfd.FileName);
var archive = new Archive();
ArchiveFilePaths.Add(archive, null);
ArchiveCurrentDir.Add(archive, null);
foreach (string filePath in Directory.GetFiles(fileInfo.DirectoryName))
archive.Data.Add(new ArchiveFile(filePath));
foreach (string directoryPath2 in Directory.GetDirectories(fileInfo.DirectoryName,
"*", SearchOption.TopDirectoryOnly))
AddDirectoryToArchiveDirectory(archive, null, directoryPath2);
Archives.Add(archive);
AddTabPage(Path.GetFileName(fileInfo.DirectoryName));
RefreshGUI();
}
}
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
SaveArchive(tabControl.SelectedIndex, false);
}
catch (Exception ex)
{
MessageBox.Show($"Failed to save archive!\n{ex}", Program.ProgramName,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
SaveArchive(tabControl.SelectedIndex, true);
}
catch (Exception ex)
{
MessageBox.Show($"Failed to save archive!\n{ex}", Program.ProgramName,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void RenameSelectedFileToolStripMenuItem_Click(object sender, EventArgs e)
{
var listView = tabControl.SelectedTab.Controls[0] as ListView;
if (listView == null || listView.SelectedItems.Count < 1) return;
listView.FocusedItem.BeginEdit();
}
private void SelectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in (tabControl.SelectedTab.Controls[0] as ListView).Items)
item.Selected = true;
}
private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
{
var archive = CurrentArchive;
var files = ArchiveCurrentDir[archive] == null ?
archive.Data : ArchiveCurrentDir[archive].Data;
var listView = tabControl.SelectedTab.Controls[0] as ListView;
var pb = new ToolStripProgressBar();
var fileList = new List<string>();
string path = Path.Combine(tempPath, "Extracted_Files\\");
if (listView == null) return;
Directory.CreateDirectory(path);
new System.Threading.Thread(() =>
{
Invoke(new Action(() => Enabled = false));
statusStrip.Invoke(new Action(() => statusStrip.Items.AddRange(new ToolStripItem[] { pb })));
Invoke(new Action(() => pb.Maximum = listView.SelectedItems.Count));
Invoke(new Action(() =>
{
foreach (ListViewItem lvi in listView.SelectedItems)
foreach (var archiveFile in files)
{
string filePath = Path.Combine(path, archiveFile.Name);
archiveFile.Extract(filePath);
fileList.Add(filePath);
++pb.Value;
}
}));
Invoke(new Action(() => Clipboard.SetData(DataFormats.FileDrop, fileList.ToArray())));
statusStrip.Invoke(new Action(() => statusStrip.Items.Remove(pb)));
Invoke(new Action(() => Enabled = true));
}).Start();
}
private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Clipboard.GetData(DataFormats.FileDrop) is string[] files)
{
var archive = CurrentArchive;
AddFilesToArchive(archive, files);
RefreshGUI();
RefreshTabPage(tabControl.SelectedIndex);
}
}
private void EditToolStripMenuItem_Opening(object sender, EventArgs e)
{
ListView lv = null;
if (Archives.Count > 0) lv = tabControl.SelectedTab.Controls[0] as ListView;
pasteToolStripMenuItem.Enabled = (lv != null && Clipboard.ContainsFileDropList());
renameToolStripMenuItem.Enabled = copyToolStripMenuItem.Enabled = deleteToolStripMenuItem.Enabled =
selectAllToolStripMenuItem.Enabled =
((lv != null) ? lv.SelectedItems.Count > 0 : false);
}
private void MainFrm_FormClosing(object sender, FormClosingEventArgs e)
{
for (int i = 0; i < Archives.Count; ++i)
{
var archive = Archives[i];
if (!archive.Saved)
{
var ArchiveName = Path.GetFileName(ArchiveFilePaths[archive] ?? "Archive");
var dialog = MessageBox.Show($"Save {ArchiveName} before closing?", Text,
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);