-
Notifications
You must be signed in to change notification settings - Fork 0
/
resForm.cs
728 lines (617 loc) · 31.7 KB
/
resForm.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
using ImageMagick;
using Shrek_2_team_action_tools.DDS;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
namespace Shrek_2_team_action_tools
{
public partial class resForm : Form
{
public resForm()
{
InitializeComponent();
}
void AddNewReport(string report)
{
if (listBoxRES.InvokeRequired)
{
listBoxRES.Invoke(new ReportHandler(AddNewReport), report);
}
else
{
listBoxRES.Items.Add(report);
listBoxRES.SelectedIndex = listBoxRES.Items.Count - 1;
listBoxRES.SelectedIndex = -1;
}
}
struct xproFormat
{
public int fileSize;
public uint textureOffset;
public int textureSize;
public int kratnostWidth;
public int kratnostHeight;
public int width;
public int height;
public int textureFormat;
public byte[] data;
}
struct texturePackage
{
public int index;
public int offset;
}
private xproFormat getData(Stream stream, int offset)
{
xproFormat format = new xproFormat();
BinaryReader br = new BinaryReader(stream);
br.BaseStream.Seek(offset + 4, SeekOrigin.Begin);
format.fileSize = br.ReadInt32();
format.textureOffset = br.ReadUInt32();
br.BaseStream.Seek(offset + 25, SeekOrigin.Begin);
format.textureFormat = 0;
byte tf = br.ReadByte();
byte wk = br.ReadByte();
byte hk = br.ReadByte();
format.textureFormat = (int)tf;
format.kratnostWidth = (int)((wk & 0xF0) >> 4);
format.kratnostHeight = (int)hk;
br.BaseStream.Seek(offset + 30, SeekOrigin.Begin);
byte[] tmp = br.ReadBytes(1);
byte[] tmpVal = new byte[4];
tmpVal[0] = tmp[0];
format.height = BitConverter.ToInt32(tmpVal, 0) == 0 && format.textureFormat < 0x12 ? -1 : (BitConverter.ToInt32(tmpVal, 0) + 1) * 16;
tmp = br.ReadBytes(1);
tmpVal = new byte[4];
tmpVal[0] = tmp[0];
format.width = BitConverter.ToInt32(tmpVal, 0) == 0 && format.textureFormat < 0x12 ? -1 : (BitConverter.ToInt32(tmpVal, 0) + 1) * 16;
if (format.kratnostWidth > 0 && format.kratnostHeight > 0 && (format.width == -1 || format.height == -1))
{
format.width = (int)Math.Pow(2, format.kratnostWidth);
format.height = (int)Math.Pow(2, format.kratnostHeight);
format.textureFormat = (int)tf;
}
int kratnost = 0;
OtherMethods.getSizeAndKratnost(format.width, format.height, format.textureFormat, ref format.textureSize, ref kratnost);
br.BaseStream.Seek(offset + format.textureOffset, SeekOrigin.Begin);
format.data = br.ReadBytes(format.textureSize);
if (format.textureFormat < 0x12)
{
byte[] header = TextureWorker.GenHeader((uint)format.textureFormat, format.width, format.height, (uint)format.textureSize, 1);
tmp = new byte[header.Length + format.textureSize];
Array.Copy(header, 0, tmp, 0, header.Length);
Array.Copy(format.data, 0, tmp, header.Length, format.data.Length);
format.data = new byte[tmp.Length];
Array.Copy(tmp, 0, format.data, 0, tmp.Length);
MagickImage img = new MagickImage(format.data);
img.Settings.Compression = CompressionMethod.NoCompression;
MemoryStream ms = new MemoryStream();
img.Flip();
img.Write(ms, MagickFormat.Bmp);
Image dd = Image.FromStream(ms);
Bitmap bmp = new Bitmap(format.width, format.height, PixelFormat.Format32bppArgb);
using(Graphics graph = Graphics.FromImage(bmp))
{
graph.DrawImage(dd, new Rectangle(0, 0, format.width, format.height));
}
ms.Close();
ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Bmp);
tmp = ms.ToArray();
ms.Close();
int tmpOff = 0;
byte[] t = new byte[4];
Array.Copy(tmp, 10, t, 0, 4);
tmpOff = BitConverter.ToInt32(t, 0);
format.data = new byte[tmp.Length - tmpOff];
Array.Copy(tmp, tmpOff, format.data, 0, format.data.Length);
}
br.Close();
return format;
}
private xproFormat getData(string fileName)
{
xproFormat format = new xproFormat();
FileStream fs = new FileStream(fileName, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
br.BaseStream.Seek(4, SeekOrigin.Begin);
format.fileSize = br.ReadInt32();
format.textureOffset = br.ReadUInt32();
br.BaseStream.Seek(25, SeekOrigin.Begin);
format.textureFormat = 0;
byte tf = br.ReadByte();
byte wk = br.ReadByte();
byte hk = br.ReadByte();
format.textureFormat = (int)tf;
format.kratnostWidth = (int)((wk & 0xF0) >> 4);
format.kratnostHeight = (int)hk;
br.BaseStream.Seek(30, SeekOrigin.Begin);
byte[] tmp = br.ReadBytes(1);
byte[] tmpVal = new byte[4];
tmpVal[0] = tmp[0];
format.height = BitConverter.ToInt32(tmpVal, 0) == 0 && format.textureFormat < 0x12 ? -1 : (BitConverter.ToInt32(tmpVal, 0) + 1) * 16;
tmp = br.ReadBytes(1);
tmpVal = new byte[4];
tmpVal[0] = tmp[0];
format.width = BitConverter.ToInt32(tmpVal, 0) == 0 && format.textureFormat < 0x12 ? -1 : (BitConverter.ToInt32(tmpVal, 0) + 1) * 16;
if (format.kratnostWidth > 0 && format.kratnostHeight > 0 && (format.width == -1 || format.height == -1))
{
format.width = (int)Math.Pow(2, format.kratnostWidth);
format.height = (int)Math.Pow(2, format.kratnostHeight);
format.textureFormat = (int)tf;
}
int kratnost = 0;
OtherMethods.getSizeAndKratnost(format.width, format.height, format.textureFormat, ref format.textureSize, ref kratnost);
br.BaseStream.Seek(format.textureOffset, SeekOrigin.Begin);
format.data = br.ReadBytes(format.textureSize);
if(format.textureFormat < 0x12)
{
byte[] header = TextureWorker.GenHeader((uint)format.textureFormat, format.width, format.height, (uint)format.textureSize, 1);
tmp = new byte[header.Length + format.textureSize];
Array.Copy(header, 0, tmp, 0, header.Length);
Array.Copy(format.data, 0, tmp, header.Length, format.data.Length);
format.data = new byte[tmp.Length];
Array.Copy(tmp, 0, format.data, 0, tmp.Length);
MagickImage img = new MagickImage(format.data);
img.Settings.Compression = CompressionMethod.NoCompression;
MemoryStream ms = new MemoryStream();
img.Flip();
img.Flop();
img.Write(ms, MagickFormat.Bmp);
tmp = ms.ToArray();
ms.Close();
int tmpOff = 0;
byte[] t = new byte[4];
Array.Copy(tmp, 10, t, 0, 4);
tmpOff = BitConverter.ToInt32(t, 0);
format.data = new byte[tmp.Length - tmpOff];
Array.Copy(tmp, tmpOff, format.data, 0, format.data.Length);
}
br.Close();
fs.Close();
return format;
}
private int getType(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open);
byte[] tmp = new byte[4];
fs.Read(tmp, 0, 4);
fs.Close();
if (Encoding.ASCII.GetString(tmp) == "XPR0") return 0;
if (Encoding.ASCII.GetString(tmp) == "DDS ") return 1;
return -1;
}
private void extractDDS(FileInfo fi)
{
byte[] ddsContent = File.ReadAllBytes(fi.FullName);
if (File.Exists(MainForm.settings.outputPath + Path.DirectorySeparatorChar + fi.Name.Remove(fi.Name.Length - 3, 3) + "dds")) File.Delete(MainForm.settings.outputPath + Path.DirectorySeparatorChar + fi.Name.Remove(fi.Name.Length - 3, 3) + "dds");
File.WriteAllBytes(MainForm.settings.outputPath + Path.DirectorySeparatorChar + fi.Name.Remove(fi.Name.Length - 3, 3) + "dds", ddsContent);
AddNewReport("File " + fi.Name + " successfully extracted as DDS file.");
}
private void extractXPR0(string fileName, xproFormat format, int index = -1)
{
if (format.width != -1 && format.height != -1)
{
Bitmap bmp = new Bitmap(format.width, format.height, PixelFormat.Format32bppArgb);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
IntPtr bmpPtr = bmpData.Scan0;
Marshal.Copy(format.data, 0, bmpPtr, format.data.Length);
bmp.UnlockBits(bmpData);
string outputName = MainForm.settings.outputPath + Path.DirectorySeparatorChar + fileName.Remove(fileName.Length - 4, 4);
outputName += index != -1 ? "_" + Convert.ToString(index) + ".png" : ".png";
bmp.Save(outputName);
AddNewReport("File " + fileName + " successfully extracted as PNG.");
}
else
{
AddNewReport("File " + fileName + " probably doesn't have an image.");
}
}
private void extractBtn_Click(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo(MainForm.settings.inputPath);
string[] extensions = new[] { "*.res", "*.tpk" };
FileInfo[] fi = extensions.SelectMany(x => di.GetFiles(x, SearchOption.AllDirectories)).ToArray();
if(fi.Length > 0)
{
listBoxRES.Items.Clear();
for(int i = 0; i < fi.Length; i++)
{
if (fi[i].Extension.ToLower() == ".res")
{
int type = getType(fi[i].FullName);
switch (type)
{
case 0:
xproFormat format = getData(fi[i].FullName);
extractXPR0(fi[i].Name, format);
break;
case 1:
extractDDS(fi[i]);
break;
default:
AddNewReport("File " + fi[i].Name + ": unknown type");
break;
}
}
else
{
//Check if tpk file is bmp or dds
FileStream fs = new FileStream(fi[i].FullName, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] tmp = br.ReadBytes(4);
br.Close();
fs.Close();
if (Encoding.ASCII.GetString(tmp) == "DDS ")
{
extractDDS(fi[i]);
}
else
{
fs = new FileStream(fi[i].FullName, FileMode.Open);
br = new BinaryReader(fs);
tmp = br.ReadBytes(2);
int checkSize = br.ReadInt32();
int fileSize = (int)fs.Length;
br.Close();
fs.Close();
if (Encoding.ASCII.GetString(tmp) == "BM" && checkSize == fileSize)
{
tmp = File.ReadAllBytes(fi[i].FullName);
File.WriteAllBytes(MainForm.settings.outputPath + Path.DirectorySeparatorChar + fi[i].Name.Remove(fi[i].Name.Length - 3, 3) + "bmp", tmp);
AddNewReport("File " + fi[i].Name + " successfully extracted as BMP.");
}
else
{
fs = new FileStream(fi[i].FullName, FileMode.Open);
br = new BinaryReader(fs);
int count = br.ReadInt32();
texturePackage[] tp = new texturePackage[count];
for (int c = 0; c < count; c++)
{
tp[c].index = br.ReadInt32();
tp[c].offset = br.ReadInt32();
}
br.Close();
fs.Close();
for (int c = 0; c < count; c++)
{
fs = new FileStream(fi[i].FullName, FileMode.Open);
br = new BinaryReader(fs);
xproFormat format = getData(fs, tp[c].offset);
extractXPR0(fi[i].Name, format, c);
br.Close();
fs.Close();
}
}
}
}
}
}
}
private void importXPR0(xproFormat format, FileStream fsw, FileInfo tpkFI, string pngFileName, int tpkOffset, int tpkSize, ref int writeOffset)
{
FileStream fs = new FileStream(tpkFI.FullName, FileMode.Open);
fs.Seek(tpkOffset, SeekOrigin.Begin);
byte[] file = new byte[tpkSize];
fs.Read(file, 0, file.Length);
fs.Close();
byte[] rawData = null;
int size = 0;
if (pngFileName != "")
{
FileInfo pngFI = new FileInfo(pngFileName);
Image img = Bitmap.FromFile(pngFI.FullName);
if (img.PixelFormat != PixelFormat.Format32bppArgb)
{
fsw.Write(file, 0, file.Length);
writeOffset += file.Length;
AddNewReport("File " + tpkFI.Name + ": " + pngFI.Name + ": bitmap must be ARGB8888! Imported original block from " + tpkFI.Name);
return;
}
if (img.Width != format.width || img.Height != format.height)
{
fsw.Write(file, 0, file.Length);
writeOffset += file.Length;
AddNewReport("File " + tpkFI.Name + ": " + pngFI.Name + " has incorrect width and height! Imported original block from " + tpkFI.Name);
return;
}
using (MemoryStream ms = new MemoryStream())
{
img.RotateFlip(RotateFlipType.RotateNoneFlipY);
img.Save(ms, ImageFormat.Bmp);
rawData = ms.ToArray();
byte[] tmp = new byte[4];
Array.Copy(rawData, 10, tmp, 0, tmp.Length);
int offset = BitConverter.ToInt32(tmp, 0);
size = rawData.Length - offset;
format.data = new byte[size];
Array.Copy(rawData, offset, format.data, 0, size);
}
if (format.textureFormat != 0 && format.textureFormat < 0x12)
{
MagickImage mgImg = new MagickImage(rawData);
switch (format.textureFormat)
{
case 12:
mgImg.Settings.Compression = CompressionMethod.DXT1;
break;
case 15:
mgImg.Settings.Compression = CompressionMethod.DXT5;
break;
}
using (MemoryStream ms = new MemoryStream())
{
mgImg.Flip();
mgImg.Write(ms, MagickFormat.Dds);
rawData = ms.ToArray();
int texSize = 0;
int kratnost = 0;
OtherMethods.getSizeAndKratnost(format.width, format.height, format.textureFormat, ref texSize, ref kratnost);
byte[] tmpImg = new byte[texSize];
Array.Copy(rawData, 128, tmpImg, 0, tmpImg.Length);
format.data = new byte[tmpImg.Length];
Array.Copy(tmpImg, 0, format.data, 0, format.data.Length);
size = format.data.Length;
}
mgImg.Dispose();
img.Dispose();
}
if ((size > format.fileSize) || (size < 0))
{
fsw.Write(file, 0, file.Length);
writeOffset += file.Length;
AddNewReport("File " + tpkFI.Name + " has incorrect size compare with " + pngFI.Name + ". Imported original file from " + tpkFI.Name);
return;
}
img.Dispose();
Array.Copy(format.data, 0, file, format.textureOffset, format.data.Length);
}
writeOffset += file.Length;
fsw.Write(file, 0, file.Length);
}
private void importXPR0(xproFormat format, FileInfo resFI, FileInfo pngFI)
{
byte[] file = File.ReadAllBytes(resFI.FullName);
byte[] rawData = null;
int size = 0;
Image img = Bitmap.FromFile(pngFI.FullName);
if(img.PixelFormat != PixelFormat.Format32bppArgb)
{
AddNewReport("File " + resFI.Name + ": " + pngFI.Name + ": bitmap must be ARGB8888!");
return;
}
if(img.Width != format.width || img.Height != format.height)
{
AddNewReport("File " + resFI.Name + ": " + pngFI.Name + " has incorrect width and height!");
return;
}
using (MemoryStream ms = new MemoryStream())
{
img.RotateFlip(RotateFlipType.RotateNoneFlipY);
img.Save(ms, ImageFormat.Bmp);
rawData = ms.ToArray();
byte[] tmp = new byte[4];
Array.Copy(rawData, 10, tmp, 0, tmp.Length);
int offset = BitConverter.ToInt32(tmp, 0);
size = rawData.Length - offset;
format.data = new byte[size];
Array.Copy(rawData, offset, format.data, 0, size);
}
if (format.textureFormat != 0 && format.textureFormat < 0x12)
{
MagickImage mgImg = new MagickImage(rawData);
switch(format.textureFormat)
{
case 12:
mgImg.Settings.Compression = CompressionMethod.DXT1;
break;
case 15:
mgImg.Settings.Compression = CompressionMethod.DXT5;
break;
}
using(MemoryStream ms = new MemoryStream())
{
mgImg.Write(ms, MagickFormat.Dds);
rawData = ms.ToArray();
int texSize = 0;
int kratnost = 0;
OtherMethods.getSizeAndKratnost(format.width, format.height, format.textureFormat, ref texSize, ref kratnost);
byte[] tmpImg = new byte[texSize];
Array.Copy(rawData, 128, tmpImg, 0, tmpImg.Length);
format.data = new byte[tmpImg.Length];
Array.Copy(tmpImg, 0, format.data, 0, format.data.Length);
size = format.data.Length;
}
mgImg.Dispose();
img.Dispose();
}
//if(size != format.fileSize - format.textureOffset)
if ((size > format.fileSize) || (size < 0))
{
AddNewReport("File " + resFI.Name + " has incorrect size compare with " + pngFI.Name);
return;
}
img.Dispose();
if (File.Exists(MainForm.settings.outputPath + Path.DirectorySeparatorChar + resFI.Name)) File.Delete(MainForm.settings.outputPath + Path.DirectorySeparatorChar + resFI.Name);
Array.Copy(format.data, 0, file, format.textureOffset, format.data.Length);
File.WriteAllBytes(MainForm.settings.outputPath + Path.DirectorySeparatorChar + resFI.Name, file);
AddNewReport("File " + resFI + " successfully imported.");
}
private void importDDS(FileInfo ddsFI)
{
File.Copy(ddsFI.FullName, MainForm.settings.outputPath + Path.DirectorySeparatorChar + ddsFI.Name.Remove(ddsFI.Name.Length - 3, 3) + "res");
AddNewReport("File " + ddsFI.Name.Remove(ddsFI.Name.Length - 3, 3) + ".res successfully imported");
}
private void importBtn_Click(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo(MainForm.settings.inputPath);
var extensions = new[] { "*.png", "*.dds" };
var file_extensions = new[] { "*.res", "*.tpk" };
FileInfo[] fi = file_extensions.SelectMany(im_ext => di.GetFiles(im_ext, SearchOption.AllDirectories)).ToArray();
FileInfo[] fi2 = extensions.SelectMany(ext => di.GetFiles(ext, SearchOption.AllDirectories)).ToArray();
if (fi.Length > 0 && fi2.Length > 0)
{
listBoxRES.Items.Clear();
for (int i = 0; i < fi.Length; i++)
{
if (fi[i].Extension.ToLower() == ".res")
{
for (int j = 0; j < fi2.Length; j++)
{
if (fi2[j].Name.Remove(fi2[j].Name.Length - 4, 4) == fi[i].Name.Remove(fi[i].Name.Length - 4, 4))
{
int type = getType(fi[i].FullName);
switch (type)
{
case 0:
xproFormat format = getData(fi[i].FullName);
if (fi2[j].Extension.ToLower() == ".png")
{
importXPR0(format, fi[i], fi2[j]);
}
else
{
AddNewReport("File " + fi[i].Name + " needs a png file to import.");
}
break;
case 1:
if (fi2[j].Extension.ToLower() == ".dds")
{
importDDS(fi2[j]);
}
else
{
AddNewReport("File " + fi[i].Name + " needs a dds file to import.");
}
break;
default:
AddNewReport("File " + fi[i].Name + ": unknown type");
break;
}
}
}
}
else
{
//Check if tpk file is bmp or dds
FileStream fs = new FileStream(fi[i].FullName, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] tmp = br.ReadBytes(4);
br.Close();
fs.Close();
if (Encoding.ASCII.GetString(tmp) == "DDS ")
{
foreach(FileInfo ddsFI in fi2)
{
if(ddsFI.Name.Remove(ddsFI.Name.Length - 4) == fi[i].Name.Remove(fi[i].Name.Length - 4))
{
if (ddsFI.Extension.ToLower() == ".dds")
{
importDDS(ddsFI);
}
else
{
AddNewReport("File " + fi[i].Name + " needs a dds file to import.");
}
}
}
}
else if(Encoding.ASCII.GetString(tmp).Substring(0, 2) == "BM") //Check for probably bmp file
{
fs = new FileStream(fi[i].FullName, FileMode.Open);
br = new BinaryReader(fs);
tmp = br.ReadBytes(2);
int checkSize = br.ReadInt32();
int fileSize = (int)fs.Length;
br.Close();
fs.Close();
if (Encoding.ASCII.GetString(tmp) == "BM" && checkSize == fileSize)
{
foreach (FileInfo bmpFI in fi2)
{
if (bmpFI.Name.Remove(bmpFI.Name.Length - 4) == fi[i].Name.Remove(fi[i].Name.Length - 4))
{
if (bmpFI.Extension.ToLower() == ".bmp")
{
tmp = File.ReadAllBytes(bmpFI.FullName);
File.WriteAllBytes(MainForm.settings.outputPath + Path.DirectorySeparatorChar + fi[i].Name, tmp);
AddNewReport("File " + fi[i].Name + " successfully imported.");
}
else
{
AddNewReport("File " + fi[i].Name + " needs a bmp file to import.");
}
}
}
}
}
else //Try import files
{
bool modified = false;
fs = new FileStream(fi[i].FullName, FileMode.Open);
br = new BinaryReader(fs);
int endOffset = (int)fs.Length;
int count = br.ReadInt32();
texturePackage[] tp = new texturePackage[count];
for (int c = 0; c < count; c++)
{
tp[c].index = br.ReadInt32();
tp[c].offset = br.ReadInt32();
}
int offset = tp[0].offset;
br.Close();
fs.Close();
if (File.Exists(MainForm.settings.outputPath + Path.DirectorySeparatorChar + fi[i].Name)) File.Delete(MainForm.settings.outputPath + Path.DirectorySeparatorChar + fi[i].Name);
FileStream fsw = new FileStream(MainForm.settings.outputPath + Path.DirectorySeparatorChar + fi[i].Name, FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fsw);
tmp = new byte[8 * count];
bw.Write(count);
bw.Write(tmp);
tmp = new byte[tp[0].offset - (4 + (8 * count))];
tmp = OtherMethods.fillPadded(tmp, tmp.Length, 0);
bw.Write(tmp);
int tmpOff = 0;
int tmpHeadOff = 4;
for (int c = 0; c < count; c++)
{
fs = new FileStream(fi[i].FullName, FileMode.Open);
br = new BinaryReader(fs);
int blockSize = c + 1 < count ? tp[c + 1].offset - tp[c].offset : endOffset - tp[c].offset;
string pathName = File.Exists(fi[i].FullName.Remove(fi[i].FullName.Length - 4) + "_" + Convert.ToString(c) + ".png") ? fi[i].FullName.Remove(fi[i].FullName.Length - 4) + "_" + Convert.ToString(c) + ".png" : "";
if(pathName != "" && !modified) modified = true;
xproFormat format = getData(fs, tp[c].offset);
importXPR0(format, fsw, fi[i], pathName, tp[c].offset, blockSize, ref offset);
if (c + 1 < count) tp[c + 1].offset = offset;
tmpOff = (int)bw.BaseStream.Position;
bw.BaseStream.Seek(tmpHeadOff, SeekOrigin.Begin);
bw.Write(tp[c].index);
bw.Write(tp[c].offset);
tmpHeadOff += 8;
bw.BaseStream.Seek(tmpOff, SeekOrigin.Begin);
br.Close();
fs.Close();
}
bw.Close();
fsw.Close();
if (modified)
{
AddNewReport("File " + fi[i].Name + " successfully imported.");
}
else
{
AddNewReport("File " + fi[i].Name + " just copied. Nothing to modify.");
}
}
}
}
}
}
}
}