This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathForm1.cs
466 lines (424 loc) · 13.9 KB
/
Form1.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
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using Microsoft.WindowsAPICodePack.Dialogs;
using System.Data;
using System.Windows.Forms;
using IGAE_GUI.IGA;
using IGAE_GUI.IGZ;
namespace IGAE_GUI
{
public partial class Form_igArchiveExtractor : Form
{
private CommonOpenFileDialog cofdSelectExtractOutputDir = new CommonOpenFileDialog();
private CommonOpenFileDialog cofdSelectInputDir = new CommonOpenFileDialog();
List<IGA_File> files = new List<IGA_File>();
const int prgBarMax = 100000;
public Form_igArchiveExtractor(Config config)
{
InitializeComponent();
ApplySettings(config);
cofdSelectExtractOutputDir.IsFolderPicker = true;
cofdSelectInputDir.IsFolderPicker = true;
prgProgressBar.Minimum = 0;
prgProgressBar.Maximum = prgBarMax;
lblComplete.Visible = false;
tmsi_ExtractAll.Enabled = false;
tmsi_ExtractFile.Enabled = false;
}
private void OpenIGAFile(IGA_Version version)
{
SelectIGAFile.Filter = "Supported game files|*.arc;*.bld;*.pak;*.iga;*.igz;*.lang|All files (*.*)|*.*";
if(SelectIGAFile.ShowDialog() == DialogResult.OK)
{
prgProgressBar.Value = 0;
lblComplete.Visible = false;
if(files != null)
{
if(files.Count > 0)
{
for(int i = 0; i < files.Count; i++)
{
files[i].Close();
}
}
}
files = new List<IGA_File>();
try
{
files.Add(new IGA_File(SelectIGAFile.FileName, version));
}
catch(Exception)
{
IGZ_File igz = new IGZ_File(new FileStream(SelectIGAFile.FileName, FileMode.Open, FileAccess.ReadWrite));
IGZ_GeneralForm igzForm = new IGZ_GeneralForm(igz);
igzForm.Show();
return;
}
tmsi_ExtractAll.Enabled = true;
treeLocalFiles.Nodes.Clear();
List<string> containedFiles = new List<string>();
for (uint i = 0; i < files[0].numberOfFiles; i++)
{
containedFiles.Add(files[0].names[i]);
prgProgressBar.Value = (int)(((float)i / (float)files[0].numberOfFiles) * prgBarMax);
}
MakeTreeFromPaths(containedFiles);
treeLocalFiles.Sort();
lstLog.Items.Add($"Opened IGA file \"{SelectIGAFile.FileName}\"");
lblComplete.Visible = true;
prgProgressBar.Value = prgBarMax;
tmsi_ExtractFile.Enabled = false;
}
}
private void treeLocalFiles_AfterSelect(object sender, EventArgs e)
{
lblSelectedFile.Text = $"Selected: {treeLocalFiles.SelectedNode.Text}";
string type;
if(treeLocalFiles.SelectedNode.Nodes.Count > 0)
{
tmsi_ExtractFile.Enabled = false;
type = "Directory";
lblSize.Text = $"Size: N/A";
lblIndex.Text = $"Index: N/A";
}
else
{
string[] parts = treeLocalFiles.SelectedNode.Text.Split('.');
string extension = parts[parts.Length - 1];
switch (extension)
{
case "enc":
type = "FSB Audio File";
break;
default:
type = $"{extension.ToUpper()} File";
break;
}
for (int i = 0; i < files.Count; i++)
{
if(files[i].localFileHeaders.Any(x => x.path.EndsWith(treeLocalFiles.SelectedNode.Text)))
{
IGA_Descriptor selected = files[i].localFileHeaders.First(x => x.path.EndsWith(treeLocalFiles.SelectedNode.Text));
lblSize.Text = $"Size: {selected.size} bytes";
lblIndex.Text = $"Index: {selected.index}";
tmsi_ExtractFile.Enabled = true;
}
}
}
lblType.Text = $"Type: {type}";
}
private void ExtractAllFiles(object sender, EventArgs e)
{
cofdSelectExtractOutputDir.Title = "Select Output Folder";
uint totalFiles = 0;
for (int j = 0; j < files.Count; j++)
{
for (uint i = 0; i < files[j].numberOfFiles; i++)
{
totalFiles += files[j].numberOfFiles;
}
}
uint currFiles = 0;
if (cofdSelectExtractOutputDir.ShowDialog() == CommonFileDialogResult.Ok)
{
lblComplete.Visible = false;
prgProgressBar.Value = 0;
for (int i = 0; i < files.Count; i++)
{
for (uint j = 0; j < files[i].numberOfFiles; j++)
{
files[i].ExtractFile(j, cofdSelectExtractOutputDir.FileName, out int res);
lstLog.Items.Add($"Extracting file {j} {(res == 0 ? "succeeded" : "failed due to: unsupported compression")}...");
currFiles++;
prgProgressBar.Value = (int)(((float)currFiles / totalFiles) * prgBarMax);
}
}
prgProgressBar.Value = prgBarMax;
lblComplete.Visible = true;
}
}
private void ExtractSingleFile(object sender, EventArgs e)
{
uint index = 0;
int igaIndex;
for (igaIndex = 0; igaIndex < files.Count; igaIndex++)
{
try
{
index = files[igaIndex].localFileHeaders.First(x => x.path.Contains(treeLocalFiles.SelectedNode.Text)).index;
break;
}
catch (InvalidOperationException)
{
continue;
}
}
cofdSelectExtractOutputDir.Title = "Select Output Folder";
if (cofdSelectExtractOutputDir.ShowDialog() == CommonFileDialogResult.Ok)
{
prgProgressBar.Value = 0;
lblComplete.Visible = false;
files[igaIndex].ExtractFile(index, cofdSelectExtractOutputDir.FileName, out int res);
prgProgressBar.Value = prgBarMax;
lblComplete.Visible = true;
}
}
private void PreviewFile(object sender, EventArgs e)
{
if(treeLocalFiles.SelectedNode.Nodes.Count != 0)
{
return;
}
int index = -1;
int igaIndex;
for (igaIndex = 0; igaIndex < files.Count; igaIndex++)
{
try
{
index = (int)files[igaIndex].localFileHeaders.First(x => x.path.EndsWith(treeLocalFiles.SelectedNode.Text)).index;
break;
}
catch (InvalidOperationException)
{
continue;
}
}
if(index == -1) return;
MemoryStream igzms = new MemoryStream((int)files[igaIndex].localFileHeaders[index].size);
files[igaIndex].ExtractFile((uint)index, igzms, out int res, true);
IGZ_File igz = new IGZ_File(igzms);
//if(igz.version != 0x06 && igz.version != 0x08 && igz.version != 0x09 ) return; //temporary line of code cos these two definitely work and other ones are buggy as hell
IGZ_GeneralForm igzForm = new IGZ_GeneralForm(igz);
igzForm.Show();
}
private void ExitApplication(object sender, EventArgs e)
{
Application.Exit();
}
private void btnClearLog_Click(object sender, EventArgs e)
{
lstLog.Items.Clear();
}
private void OpenFolder(IGA_Version version)
{
prgProgressBar.Value = 0;
lblComplete.Visible = false;
cofdSelectInputDir.Title = "Select Folder To Load";
if (cofdSelectInputDir.ShowDialog() == CommonFileDialogResult.Ok)
{
treeLocalFiles.Nodes.Clear();
//I don't like this line of code either
string[] igaFiles = Directory.GetFiles(cofdSelectInputDir.FileName, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".arc", StringComparison.OrdinalIgnoreCase) || s.EndsWith(".bld", StringComparison.OrdinalIgnoreCase) || s.EndsWith(".pak", StringComparison.OrdinalIgnoreCase) || s.EndsWith(".iga", StringComparison.OrdinalIgnoreCase)).ToArray();
if (igaFiles.Length == 0) throw new InvalidOperationException("There are no IGA type files in this directory");
List<string> containedFiles = new List<string>();
if(files != null)
{
if(files.Count > 0)
{
for(int i = 0; i < files.Count; i++)
{
files[i].Close();
}
}
}
files = new List<IGA_File>();
for (int i = 0; i < igaFiles.Length; i++)
{
prgProgressBar.Value = (int)(((float)i / igaFiles.Length) * prgBarMax);
//Some arc files on 3DS are 0 bytes, this line of code prevents them from loading
if (new FileInfo(igaFiles[i]).Length == 0) continue;
//Most BLD files contain bld and pak files which are not iga
if (Path.GetFileName(igaFiles[i]) == "level.bld") continue;
if (Path.GetFileName(igaFiles[i]) == "ENGLISH.pak") continue;
if (Path.GetFileName(igaFiles[i]) == "FRENCH.pak") continue;
if (Path.GetFileName(igaFiles[i]) == "SPANISH.pak") continue;
if (Path.GetFileName(igaFiles[i]) == "PORTUGUESE.pak") continue;
if (Path.GetFileName(igaFiles[i]) == "SWEDISH.pak") continue;
if (Path.GetFileName(igaFiles[i]) == "FINNISH.pak") continue;
if (Path.GetFileName(igaFiles[i]) == "DUTCH.pak") continue;
if (Path.GetFileName(igaFiles[i]) == "NORWEGIAN.pak") continue;
if (Path.GetFileName(igaFiles[i]) == "DANISH.pak") continue;
if (Path.GetFileName(igaFiles[i]) == "GERMAN.pak") continue;
if (Path.GetFileName(igaFiles[i]) == "HISPANIC.pak") continue;
if (Path.GetFileName(igaFiles[i]) == "ITALIAN.pak") continue;
if (Path.GetFileName(igaFiles[i]) == "JAPANESE.pak") continue;
files.Add(new IGA_File(igaFiles[i], version));
bool isBLD = igaFiles[i].EndsWith("bld", StringComparison.OrdinalIgnoreCase);
for (uint j = 0; j < files.Last().numberOfFiles; j++)
{
if(isBLD)
{
//Done to prevent a newly loaded bld overwriting previously loaded ones
containedFiles.Add($"{Path.GetFileNameWithoutExtension(igaFiles[i])}/{files.Last().names[j]}");
}
else
{
containedFiles.Add(files.Last().names[j]);
}
}
lstLog.Items.Add($"Opened IGA file \"{igaFiles[i]}\"");
}
MakeTreeFromPaths(containedFiles);
treeLocalFiles.Sort();
tmsi_ExtractAll.Enabled = true;
}
tmsi_ExtractFile.Enabled = false;
prgProgressBar.Value = prgBarMax;
lblComplete.Visible = true;
}
//Stolen from ykm29's reply to https://stackoverflow.com/questions/1155977/populate-treeview-from-a-list-of-path with some alterations
void MakeTreeFromPaths(List<string> paths)
{
for (int i = 0; i < paths.Count; i++)
{
TreeNode currentNode = null;
string[] items = paths[i].Split(new char[]{'/', '\\'});
foreach(string item in items)
{
if(currentNode == null)
{
TreeNode[] rootNodes = treeLocalFiles.Nodes.Cast<TreeNode>().ToArray();
if(rootNodes.Any(x => x.Text.Equals(item, StringComparison.OrdinalIgnoreCase)))
{
currentNode = rootNodes.First(x => x.Text.Equals(item, StringComparison.OrdinalIgnoreCase));
}
else
{
currentNode = treeLocalFiles.Nodes.Add(item);
}
}
else
{
TreeNode[] currentNodes = currentNode.Nodes.Cast<TreeNode>().ToArray();
if(currentNodes.Any(x => x.Text.Equals(item, StringComparison.OrdinalIgnoreCase)))
{
currentNode = currentNodes.First(x => x.Text.Equals(item, StringComparison.OrdinalIgnoreCase));
}
else
{
currentNode = currentNode.Nodes.Add(item);
}
}
}
}
}
private void OpenSettings(object sender, EventArgs e)
{
SettingsMenu menu = new SettingsMenu(Config.Read());
menu.Show();
menu.btnSaveSettings.Click += (_, __) => ApplySettings(menu.config);
}
void ApplySettings(Config config)
{
if (config.darkMode)
{
Themes.SetWindowControlToDark(this);
foreach(Control control in Controls)
{
Themes.SetControlToDark(control);
}
}
else
{
foreach(Control control in Controls)
{
Themes.SetControlToLight(control);
}
Themes.SetControlToLight(this);
}
}
void SaveAs(object sender, EventArgs e)
{
IGA_BuildForm buildForm;
if(files.Count != 1)
{
buildForm = new IGA_BuildForm(null);
}
else
{
buildForm = new IGA_BuildForm(files[0]);
}
buildForm.Show();
return;
//Console.WriteLine("Opening save dialogue");
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
saveFileDialog.Filter = "iga files|*.arc;*.bld;*.pak|All files (*.*)|*.*";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog() == DialogResult.OK) //If the user selects a file
{
files[0].Build(saveFileDialog.FileName);
}
}
}
#region Version ToolStripMenuItems
private void OpenFile_SSAWii_3DS_ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenIGAFile(IGA_Version.SkylandersSpyrosAdventureWii);
}
//Sg and ssa wii u are the same
private void OpenFile_SG_SSAWiiU_ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenIGAFile(IGA_Version.SkylandersSpyrosAdventureWiiU);
}
private void OpenFile_SSF_ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenIGAFile(IGA_Version.SkylandersSwapForce);
}
private void OpenFile_STT_ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenIGAFile(IGA_Version.SkylandersTrapTeam);
}
//They're the same, also for clarity it's: SSC, SI (PS3, Xbox 360, Wii U)
private void OpenFile_SSC_SI_PS3_X360_WiiU_ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenIGAFile(IGA_Version.SkylandersSuperChargers);
}
//I'm mad
private void OpenFile_SI_PS4_ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenIGAFile(IGA_Version.SkylandersImaginatorsPS4);
}
//Lost islands
private void OpenFile_SLI_ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenIGAFile(IGA_Version.SkylandersLostIslands);
}
private void OpenFolder_SSAWii_3DS_ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFolder(IGA_Version.SkylandersSpyrosAdventureWii);
}
//Sg and ssa wii u are the same
private void OpenFolder_SG_SSAWiiU_ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFolder(IGA_Version.SkylandersSpyrosAdventureWiiU);
}
private void OpenFolder_SSF_ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFolder(IGA_Version.SkylandersSwapForce);
}
private void OpenFolder_STT_ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFolder(IGA_Version.SkylandersTrapTeam);
}
//They're the same, also for clarity it's: SSC, SI (PS3, Xbox 360, Wii U)
private void OpenFolder_SSC_SI_PS3_X360_WiiU_ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFolder(IGA_Version.SkylandersSuperChargers);
}
//I'm mad
private void OpenFolder_SI_PS4_ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFolder(IGA_Version.SkylandersImaginatorsPS4);
}
//Lost islands
private void OpenFolder_SLI_ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFolder(IGA_Version.SkylandersLostIslands);
}
#endregion
}
}