-
Notifications
You must be signed in to change notification settings - Fork 3
/
Form1.cs
912 lines (844 loc) · 38.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
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
using G1Tool.Formats;
using System;
using System.Drawing.Imaging;
using System.Windows.Forms;
using OpenTK.Graphics;
using OpenTK.Platform;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
namespace G1Tool
{
public partial class Form1 : Form
{
ContextMenu g1tMenu = new ContextMenu();
private Bin BinFile { get; set; }
private int FormatIndex { get; set; }
private ContextMenu ContextMenuBinDDS { get; set; }
private ContextMenu ContextMenuBinG1T { get; set; }
private ContextMenu ContextMenuBin { get; set; }
private ContextMenu ContextMenuDDS { get; set; }
private ContextMenu ContextMenuG1T { get; set; }
private new List<List<G1T>> BinFileList { get; set; } // I hate KT // FileList for storing format
private List<G1T> G1TFileList { get; set; }
private List<string> FilePathBinGZList { get; set; } // The string name of the files
private List<string> FilePathBinList { get; set; }
private List<string> FilePathG1TList { get; set; }
private int NodeIndexBin { get; set; }
private int NodeIndexG1T { get; set; }
public Form1()
{
InitializeComponent();
CreateOpenGLContext();
SetUpContextMenu();
PopulateCompressionComboBox();
}
public class ComboBoxItem
{
public string Text { get; set; }
public int Value { get; set; }
public ComboBoxItem(string text, int value = 0)
{
Text = text;
Value = value;
}
public override string ToString()
{
return Text;
}
}
private void PopulateCompressionComboBox()
{
comboBoxCompression.Items.Add(new ComboBoxItem("RGBA8", 0x1));
comboBoxCompression.Items.Add(new ComboBoxItem("RGBA8 (Warriors Orochi 4)", 0x2));
comboBoxCompression.Items.Add(new ComboBoxItem("DXT1 (FEW)", 0x6));
comboBoxCompression.Items.Add(new ComboBoxItem("DXT5 (FEW)", 0x8));
comboBoxCompression.Items.Add(new ComboBoxItem("DXT1 (3H)", 0x59));
comboBoxCompression.Items.Add(new ComboBoxItem("DXT5 (3H)", 0x5B));
}
private void CreateOpenGLContext()
{
IWindowInfo window = Utilities.CreateWindowsWindowInfo(this.Handle);
IGraphicsContext context = new GraphicsContext(GraphicsMode.Default, window);
context.MakeCurrent(window);
context.LoadAll();
}
private void SetUpContextMenu()
{
SetUpG1TMenu();
SetUpTextureContextMenu();
}
private void SetUpG1TMenu()
{
MenuItem add = new MenuItem("Add");
add.Click += Add_Click;
g1tMenu.MenuItems.Add(add);
}
private void Add_Click(object sender, EventArgs e)
{
/*using (var opendialog = new openFileDialog())
{
opendialog.Filter =
"DirectDraw Surface|*.dds";
opendialog.Multiselect = false; //For now at least, only one file at a time
if (opendialog.ShowDialog() == DialogResult.OK)
{
DDS temp = new DDS();
temp.Read(opendialog.FileName);
if (temp.MipMapCount >= 10)
{
MessageBox.Show("The amount of mipmaps for this texture is too big. Reducing it to 9.");
temp.MipMapCount = 9;
}
G1Texture texture = new G1Texture();
texture.Replace(temp);
textureListBox.Items.Add(texture);
textureListBox.SelectedItem = texture;
}
}
*/
}
private void SetUpTextureContextMenu()
{
// Apparently can't share the same object
// Bin
MenuItem replaceDDS = new MenuItem("Replace DDS");
MenuItem exportPNG = new MenuItem("Export as PNG");
replaceDDS.Click += ReplaceDDS_Click;
exportPNG.Click += ExportPNG_Click;
ContextMenuBinDDS = new ContextMenu();
ContextMenuBinDDS.MenuItems.Add(replaceDDS);
ContextMenuBinDDS.MenuItems.Add(exportPNG);
MenuItem replaceG1T = new MenuItem("Replace G1T");
MenuItem exportG1T = new MenuItem("Export G1T");
MenuItem batchExportPNG = new MenuItem("Batch export as PNG");
replaceG1T.Click += ReplaceG1T_Click;
exportG1T.Click += ExportG1T_Click;
batchExportPNG.Click += BatchExportPNG_Click;
ContextMenuBinG1T = new ContextMenu();
ContextMenuBinG1T.MenuItems.Add(replaceG1T);
ContextMenuBinG1T.MenuItems.Add(exportG1T);
ContextMenuBinG1T.MenuItems.Add(batchExportPNG);
MenuItem save = new MenuItem("Save");
MenuItem saveAs = new MenuItem("Save As...");
MenuItem binBatchExportG1T = new MenuItem("Batch export G1T");
MenuItem binBatchExportPNG = new MenuItem("Batch export G1T as PNG");
save.Click += Save_Click;
saveAs.Click += SaveAs_Click;
binBatchExportG1T.Click += BinBatchExportG1T_Click;
binBatchExportPNG.Click += BinBatchExportPNG_Click;
ContextMenuBin = new ContextMenu();
ContextMenuBin.MenuItems.Add(save);
ContextMenuBin.MenuItems.Add(saveAs);
ContextMenuBin.MenuItems.Add(binBatchExportG1T);
ContextMenuBin.MenuItems.Add(binBatchExportPNG);
// G1T
ContextMenuDDS = new ContextMenu();
replaceDDS = new MenuItem("Replace DDS");
exportPNG = new MenuItem("Export as PNG");
replaceDDS.Click += ReplaceDDS_Click;
exportPNG.Click += ExportPNG_Click;
ContextMenuDDS = new ContextMenu();
ContextMenuDDS.MenuItems.Add(replaceDDS);
ContextMenuDDS.MenuItems.Add(exportPNG);
save = new MenuItem("Save");
saveAs = new MenuItem("Save As...");
replaceG1T = new MenuItem("Replace G1T");
batchExportPNG = new MenuItem("Batch export as PNG");
save.Click += Save_Click;
saveAs.Click += SaveAs_Click;
replaceG1T.Click += ReplaceG1T_Click;
batchExportPNG.Click += BatchExportPNG_Click;
ContextMenuG1T = new ContextMenu();
ContextMenuG1T.MenuItems.Add(save);
ContextMenuG1T.MenuItems.Add(saveAs);
ContextMenuG1T.MenuItems.Add(replaceG1T);
ContextMenuG1T.MenuItems.Add(batchExportPNG);
}
private void ExportPNG_Click(object sender, EventArgs e)
{
using (var opendialog = new SaveFileDialog())
{
opendialog.Filter =
"Portable Network Graphics|*.png";
if (opendialog.ShowDialog() == DialogResult.OK)
{
switch (Path.GetExtension(opendialog.FileName))
{
case ".png":
{
pictureBox1.Image.Save(opendialog.FileName, ImageFormat.Png);
}
break;
}
}
}
}
private void BinBatchExportPNG_Click(object sender, EventArgs e)
{
using (var browserDialog = new FolderBrowserDialog())
{
if (browserDialog.ShowDialog() == DialogResult.OK)
{
for (int i = 0; i < BinFileList[treeView1.SelectedNode.Index].Count; i++)
{
for (int ii = 0; ii < BinFileList[treeView1.SelectedNode.Index][i].Textures.Count; ii++)
{
LoadImage(BinFileList[treeView1.SelectedNode.Index][i].Textures[ii]);
Directory.CreateDirectory(browserDialog.SelectedPath + "\\" + i.ToString());
pictureBox1.Image.Save(browserDialog.SelectedPath + "\\" + i.ToString() + "\\" + Path.GetFileNameWithoutExtension(treeView1.SelectedNode.Nodes[i].Nodes[ii].Text) + ".png", ImageFormat.Png);
}
}
}
}
}
private void BatchExportPNG_Click(object sender, EventArgs e)
{
using (var browserDialog = new FolderBrowserDialog())
{
if (browserDialog.ShowDialog() == DialogResult.OK)
{
if (FormatIndex == NodeIndexBin && treeView1.Nodes.ContainsKey("BIN"))
{
for (int i = 0; i < BinFileList[treeView1.SelectedNode.Parent.Index][treeView1.SelectedNode.Index].Textures.Count; i++)
{
LoadImage(BinFileList[treeView1.SelectedNode.Parent.Index][treeView1.SelectedNode.Index].Textures[i]);
pictureBox1.Image.Save(browserDialog.SelectedPath + "\\" + Path.GetFileNameWithoutExtension(treeView1.SelectedNode.Nodes[i].Text) + ".png", ImageFormat.Png); // Probably can just add @ later
MessageBox.Show(browserDialog.SelectedPath + "\\" + Path.GetFileNameWithoutExtension(treeView1.SelectedNode.Nodes[i].Text) + ".png");
}
}
if (FormatIndex == NodeIndexG1T && treeView1.Nodes.ContainsKey("G1T"))
{
for (int i = 0; i < G1TFileList[treeView1.SelectedNode.Index].Textures.Count; i++)
{
LoadImage(G1TFileList[treeView1.SelectedNode.Index].Textures[i]);
pictureBox1.Image.Save(browserDialog.SelectedPath + "\\" + Path.GetFileNameWithoutExtension(treeView1.SelectedNode.Nodes[i].Text) + ".png", ImageFormat.Png);
}
}
}
}
}
private void ReplaceDDS_Click(object sender, EventArgs e)
{
using (var opendialog = new OpenFileDialog())
{
opendialog.Filter =
"DirectDraw Surface|*.dds";
if (opendialog.ShowDialog() == DialogResult.OK)
{
G1Texture texture = new G1Texture();
DDS newDDS = new DDS();
newDDS.Read(opendialog.FileName);
if (newDDS.MipMapCount >= 10)
{
MessageBox.Show("The amount of mipmaps for this texture is too big. Reducing it to 9.");
newDDS.MipMapCount = 9;
}
if (FormatIndex == NodeIndexBin && treeView1.Nodes.ContainsKey("BIN"))
{
texture = BinFileList[treeView1.SelectedNode.Parent.Parent.Index][treeView1.SelectedNode.Parent.Index].Textures[treeView1.SelectedNode.Index];
texture.Replace(newDDS);
}
if (FormatIndex == NodeIndexG1T && treeView1.Nodes.ContainsKey("G1T"))
{
texture = G1TFileList[treeView1.SelectedNode.Parent.Index].Textures[treeView1.SelectedNode.Index];
texture.Replace(newDDS);
}
pictureBox1.Image = texture.Mipmap.GetBitmap();
}
}
}
private void ReplaceG1T_Click(object sender, EventArgs e)
{
using (var opendialog = new OpenFileDialog())
{
opendialog.Filter =
"Koei Tecmo texture archive|*.g1t";
if (opendialog.ShowDialog() == DialogResult.OK)
{
using (var fs = new FileStream(opendialog.FileName, FileMode.Open))
{
int fileSize = (int)new FileInfo(opendialog.FileName).Length;
byte[] buffer = new byte[fileSize];
fs.Read(buffer, 0, fileSize);
var newG1T = new G1T();
newG1T.Read(buffer);
treeView1.SelectedNode.Nodes.Clear();
for (int i = 0; i < newG1T.Textures.Count; i++)
{
treeView1.SelectedNode.Nodes.Add(i.ToString() + ".dds");
}
if (FormatIndex == NodeIndexBin && treeView1.Nodes.ContainsKey("BIN"))
{
BinFileList[treeView1.SelectedNode.Parent.Index][treeView1.SelectedNode.Index] = newG1T;
LoadImage(BinFileList[treeView1.SelectedNode.Parent.Index][treeView1.SelectedNode.Index].Textures[treeView1.SelectedNode.FirstNode.Index]);
}
if (FormatIndex == NodeIndexG1T && treeView1.Nodes.ContainsKey("G1T"))
{
G1TFileList[treeView1.SelectedNode.Parent.Index] = newG1T;
LoadImage(G1TFileList[treeView1.SelectedNode.Index].Textures[treeView1.SelectedNode.FirstNode.Index]);
}
}
}
}
}
private void ExportG1T_Click(object sender, EventArgs e)
{
using (var browserDialog = new FolderBrowserDialog())
{
if (browserDialog.ShowDialog() == DialogResult.OK)
{
using (var fs = new FileStream(browserDialog.SelectedPath + "\\" + treeView1.SelectedNode.Text, FileMode.CreateNew))
{
var g1tFile = BinFileList[treeView1.SelectedNode.Parent.Index][treeView1.SelectedNode.Index].Write();
fs.Write(g1tFile, 0x0, g1tFile.Length);
}
}
}
}
private void BinBatchExportG1T_Click(object sender, EventArgs e)
{
using (var browserDialog = new FolderBrowserDialog())
{
if (browserDialog.ShowDialog() == DialogResult.OK)
{
for (int i = 0; i < BinFileList[treeView1.SelectedNode.Parent.Index].Count; i++)
{
using (var fs = new FileStream(browserDialog.SelectedPath + "\\" + treeView1.SelectedNode.Nodes[i].Text, FileMode.CreateNew))
{
var g1tFile = BinFileList[treeView1.SelectedNode.Parent.Index][i].Write();
fs.Write(g1tFile, 0x0, g1tFile.Length);
}
}
}
}
}
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
treeView1.SelectedNode = e.Node;
}
if (FormatIndex == NodeIndexBin && treeView1.Nodes.ContainsKey("BIN"))
{
if (e.Node.Level == 1)
{
e.Node.ContextMenu = ContextMenuBin;
}
if (e.Node.Level == 2) // G1T
{
e.Node.ContextMenu = ContextMenuBinG1T;
}
if (e.Node.Level == 3) // DDS
{
e.Node.ContextMenu = ContextMenuBinDDS;
}
}
if (FormatIndex == NodeIndexG1T && treeView1.Nodes.ContainsKey("G1T"))
{
if (e.Node.Level == 1) // G1T
{
e.Node.ContextMenu = ContextMenuG1T;
}
if (e.Node.Level == 2) // DDS
{
e.Node.ContextMenu = ContextMenuDDS;
}
}
}
private void OpenToolStripMenuItem_Click(object sender, EventArgs e)
{
using (openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter =
"Koei Tecmo texture archive|*.g1t|" +
"Koei Tecmo binary archive|*.bin|" +
"Compressed Koei Tecmo texture archive|*.g1t.gz|" +
"DirectDraw Surface|*.dds|" +
"All supported files|*.g1t*;*.bin;*.g1t.gz;*.dds";
openFileDialog.FilterIndex = 5;
openFileDialog.Multiselect = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
FilePathBinGZList = new List<string>();
FilePathBinList = new List<string>();
FilePathG1TList = new List<string>();
treeView1.Nodes.Clear();
pictureBox1.Image = null;
for (int i = 0; i < openFileDialog.FileNames.Length; i++)
{
switch (Path.GetExtension(openFileDialog.FileNames[i]))
{
case (".bin"):
{
if (!treeView1.Nodes.ContainsKey("BIN"))
{
treeView1.Nodes.Add(new TreeNode("BIN") { Name = "BIN" });
NodeIndexBin = treeView1.Nodes.IndexOfKey("BIN");
}
FilePathBinGZList.Add(openFileDialog.FileNames[i]);
}
break;
case (".g1t"):
{
if (!treeView1.Nodes.ContainsKey("G1T"))
{
treeView1.Nodes.Add(new TreeNode("G1T") { Name = "G1T" });
NodeIndexG1T = treeView1.Nodes.IndexOfKey("G1T");
}
FilePathG1TList.Add(openFileDialog.FileNames[i]);
}
break;
case (".dds"):
{
}
break;
}
}
OpenFile();
}
}
}
private void OpenFile()
{
BinFileList = new List<List<G1T>>();
G1TFileList = new List<G1T>();
var imageList = new ImageList();
imageList.ColorDepth = ColorDepth.Depth32Bit;
#region BIN
for (int i = 0; i < FilePathBinGZList.Count; i++)
{
imageList.Images.Add(i.ToString(), Icon.ExtractAssociatedIcon(FilePathBinGZList[i]));
var node = new TreeNode()
{
Text = Path.GetFileName(FilePathBinGZList[i]),
ImageKey = i.ToString(),
SelectedImageKey = i.ToString(),
};
treeView1.Nodes[NodeIndexBin].Nodes.Add(node);
BinFile = new Bin();
var binEntries = new List<G1T>();
var dummyEntry = new G1T();
BinFile.Read(FilePathBinGZList[i]);
for (int ii = 0; ii < BinFile.FileList.Count; ii++)
{
if (BinFile.FileList[ii].Length != 0)
{
#region Compressed Entries
try
{
int magicID = BitConverter.ToInt32(GZip.Decompress(BinFile.FileList[ii]), 0x0);
switch (magicID)
{
case (0x47314D5F): // G1M_
{
treeView1.Nodes[NodeIndexBin].Nodes[i].Nodes.Add(new TreeNode(ii.ToString() + ".g1m"));
binEntries.Add(dummyEntry);
}
break;
case (0x47315447): // G1TG
{
treeView1.Nodes[NodeIndexBin].Nodes[i].Nodes.Add(new TreeNode(ii.ToString() + ".g1t"));
var g1t = new G1T();
g1t.Read(GZip.Decompress(BinFile.FileList[ii]));
for (int iii = 0; iii < g1t.Textures.Count; iii++)
{
treeView1.Nodes[NodeIndexBin].Nodes[i].Nodes[ii].Nodes.Add(iii.ToString() + ".dds");
}
binEntries.Add(g1t);
}
break;
default:
{
treeView1.Nodes[NodeIndexBin].Nodes[i].Nodes.Add(new TreeNode(ii.ToString() + ".bin"));
binEntries.Add(dummyEntry);
}
break;
}
}
#endregion
// Need a new list to make sure to not compress when saving
#region Uncompressed Entries
catch (Exception)
{
int magicID = BitConverter.ToInt32(BinFile.FileList[ii], 0x0);
switch (magicID)
{
case (0x47314D5F): // G1M_
{
treeView1.Nodes[NodeIndexBin].Nodes[i].Nodes.Add(new TreeNode(ii.ToString() + ".g1m"));
binEntries.Add(dummyEntry);
}
break;
case (0x47315447): // G1TG
{
var g1tList = new List<List<G1Texture>>();
treeView1.Nodes[NodeIndexBin].Nodes[i].Nodes.Add(new TreeNode(ii.ToString() + ".g1t"));
var g1t = new G1T();
g1t.Read(BinFile.FileList[ii]);
for (int iii = 0; iii < g1t.Textures.Count; iii++)
{
treeView1.Nodes[NodeIndexBin].Nodes[i].Nodes[ii].Nodes.Add(iii.ToString() + ".dds");
}
binEntries.Add(g1t);
}
break;
default:
{
treeView1.Nodes[NodeIndexBin].Nodes[i].Nodes.Add(new TreeNode(ii.ToString() + ".bin"));
binEntries.Add(dummyEntry);
}
break;
}
// New List for bin with uncompressed files
if (!FilePathBinList.Contains(FilePathBinGZList[i]))
{
FilePathBinList.Add(FilePathBinGZList[i]);
}
}
#endregion
}
#region Dummy Entries
else
{
treeView1.Nodes[NodeIndexBin].Nodes[i].Nodes.Add(new TreeNode(ii.ToString() + ".bin"));
binEntries.Add(dummyEntry);
}
}
#endregion
BinFileList.Add(binEntries);
}
// Remove bin with uncompressed files from binGZ
for (int i = 0; i < FilePathBinList.Count; i++)
{
FilePathBinGZList.RemoveAt(FilePathBinGZList.IndexOf(FilePathBinList[i]));
}
#endregion
#region G1T
for (int i = 0; i < FilePathG1TList.Count; i++)
{
imageList.Images.Add(i.ToString(), Icon.ExtractAssociatedIcon(FilePathG1TList[i]));
var node = new TreeNode()
{
Text = Path.GetFileName(FilePathG1TList[i]),
ImageKey = i.ToString(),
SelectedImageKey = i.ToString(),
};
treeView1.Nodes[NodeIndexG1T].Nodes.Add(node);
using (var fs = new FileStream(FilePathG1TList[i], FileMode.Open))
{
int fileSize = (int)new FileInfo(FilePathG1TList[i]).Length;
byte[] buffer = new byte[fileSize];
fs.Read(buffer, 0, fileSize);
var g1t = new G1T();
g1t.Read(buffer);
for (int ii = 0; ii < g1t.Textures.Count; ii++)
{
treeView1.Nodes[NodeIndexG1T].Nodes[i].Nodes.Add(new TreeNode(ii.ToString() + ".dds"));
}
G1TFileList.Add(g1t);
}
}
#endregion
/*
switch (Path.GetExtension(openFileDialog.FileNames[i]))
{
#region GZ
case ".gz":
{
using (var fs = new FileStream(openFileDialog.FileNames[i], FileMode.Open))
{
var ktGZip = new KTGZip();
int fileSize = (int)new FileInfo(openFileDialog.FileNames[i]).Length;
byte[] buffer = new byte[fileSize];
fs.Read(buffer, 0, fileSize);
currentG1T = new G1T();
currentG1T.Read(ktGZip.Decompress(buffer));
}
var list = new List<string>();
list.Add(Path.GetFileNameWithoutExtension(openFileDialog.FileNames[i]));
}
break;
#endregion
#region DDS
case ".dds":
{
treeView1.Nodes[3].Nodes.Add(node);
DDS temp = new DDS();
temp.Read(openFileDialog.FileNames[i]);
pictureBox1.Image = temp.Texture.GetBitmap();
}
break;
}
#endregion
*/
treeView1.ImageList = imageList;
}
private void openFolderToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private object GetComboBoxItemByValue(byte internalFormat)
{
foreach (ComboBoxItem item in comboBoxCompression.Items)
{
if (item.Value == internalFormat)
return item;
}
return null;
}
private void Save_Click(object sender, EventArgs e)
{
if (FormatIndex == NodeIndexBin && treeView1.Nodes.ContainsKey("BIN"))
{
var fileList = new List<byte[]>();
for (int i = 0; i < BinFileList[treeView1.SelectedNode.Index].Count; i++) // For Each G1T file of the selected bin file
{
fileList.Add(GZip.Compress(BinFileList[treeView1.SelectedNode.Index][i].Write()));
}
BinFile.Write(fileList, FilePathBinGZList[treeView1.SelectedNode.Index]);
}
if (FormatIndex == NodeIndexG1T && treeView1.Nodes.ContainsKey("G1T"))
{
using (var fs = new FileStream(FilePathG1TList[treeView1.SelectedNode.Index], FileMode.Create))
{
var g1tFile = G1TFileList[treeView1.SelectedNode.Index].Write();
fs.Write(g1tFile, 0x0, g1tFile.Length);
}
}
}
private void SaveAs_Click(object sender, EventArgs e)
{
if (FormatIndex == NodeIndexBin && treeView1.Nodes.ContainsKey("BIN"))
{
using (var savedialog = new SaveFileDialog())
{
savedialog.DefaultExt = ".bin";
savedialog.Filter = "Koei Tecmo Binary Archive|*.bin";
if (savedialog.ShowDialog() == DialogResult.OK)
{
var fileList = new List<byte[]>();
for (int i = 0; i < BinFileList[treeView1.SelectedNode.Index].Count; i++) // For Each G1T file of the selected bin file
{
fileList.Add(GZip.Compress(BinFileList[treeView1.SelectedNode.Index][i].Write()));
}
BinFile.Write(fileList, savedialog.FileName);
}
}
}
if (FormatIndex == NodeIndexG1T && treeView1.Nodes.ContainsKey("G1T"))
{
using (var savedialog = new SaveFileDialog())
{
savedialog.DefaultExt = ".g1t";
savedialog.Filter = "Koei Tecmo Texture Archive|*.g1t";
if (savedialog.ShowDialog() == DialogResult.OK)
{
using (var fs = new FileStream(savedialog.FileName, FileMode.Create))
{
var g1tFile = G1TFileList[treeView1.SelectedNode.Index].Write();
fs.Write(g1tFile, 0x0, g1tFile.Length);
}
}
}
}
}
private void NumericUpDownMipMap_ValueChanged(object sender, EventArgs e)
{
G1Texture tex = new G1Texture();
if (FormatIndex == NodeIndexBin && treeView1.Nodes.ContainsKey("BIN"))
{
if (treeView1.SelectedNode.Level == 2)
{
tex = BinFileList[treeView1.SelectedNode.Parent.Index][treeView1.SelectedNode.Index].Textures[treeView1.SelectedNode.FirstNode.Index];
}
if (treeView1.SelectedNode.Level == 3)
{
tex = BinFileList[treeView1.SelectedNode.Parent.Parent.Index][treeView1.SelectedNode.Parent.Index].Textures[treeView1.SelectedNode.Index];
}
}
if (FormatIndex == NodeIndexG1T && treeView1.Nodes.ContainsKey("G1T"))
{
if (treeView1.SelectedNode.Level == 1)
{
tex = G1TFileList[treeView1.SelectedNode.Index].Textures[treeView1.SelectedNode.FirstNode.Index];
}
if (treeView1.SelectedNode.Level == 2)
{
tex = G1TFileList[treeView1.SelectedNode.Parent.Index].Textures[treeView1.SelectedNode.Index];
}
}
tex.MipMapCount = (byte)numericUpDownMipMap.Value;
}
private void CheckBoxNormalMap_CheckedChanged(object sender, EventArgs e)
{
G1Texture tex = new G1Texture();
if (FormatIndex == NodeIndexBin && treeView1.Nodes.ContainsKey("BIN"))
{
if (treeView1.SelectedNode.Level == 2)
{
tex = BinFileList[treeView1.SelectedNode.Parent.Index][treeView1.SelectedNode.Index].Textures[treeView1.SelectedNode.FirstNode.Index];
}
if (treeView1.SelectedNode.Level == 3)
{
tex = BinFileList[treeView1.SelectedNode.Parent.Parent.Index][treeView1.SelectedNode.Parent.Index].Textures[treeView1.SelectedNode.Index];
}
}
if (FormatIndex == NodeIndexG1T && treeView1.Nodes.ContainsKey("G1T"))
{
if (treeView1.SelectedNode.Level == 1)
{
tex = G1TFileList[treeView1.SelectedNode.Index].Textures[treeView1.SelectedNode.FirstNode.Index];
}
if (treeView1.SelectedNode.Level == 2)
{
tex = G1TFileList[treeView1.SelectedNode.Parent.Index].Textures[treeView1.SelectedNode.Index];
}
}
if (checkBoxNormalMap.Checked)
tex.NormalMapFlags = 3;
else
tex.NormalMapFlags = 0;
}
private void ComboBoxCompression_SelectedIndexChanged(object sender, EventArgs e)
{
G1Texture tex = new G1Texture();
if (FormatIndex == NodeIndexBin && treeView1.Nodes.ContainsKey("BIN"))
{
if (treeView1.SelectedNode.Level == 2)
{
tex = BinFileList[treeView1.SelectedNode.Parent.Index][treeView1.SelectedNode.Index].Textures[treeView1.SelectedNode.FirstNode.Index];
}
if (treeView1.SelectedNode.Level == 3)
{
tex = BinFileList[treeView1.SelectedNode.Parent.Parent.Index][treeView1.SelectedNode.Parent.Index].Textures[treeView1.SelectedNode.Index];
}
}
if (FormatIndex == NodeIndexG1T && treeView1.Nodes.ContainsKey("G1T"))
{
if (treeView1.SelectedNode.Level == 1)
{
tex = G1TFileList[treeView1.SelectedNode.Index].Textures[treeView1.SelectedNode.FirstNode.Index];
}
if (treeView1.SelectedNode.Level == 2)
{
tex = G1TFileList[treeView1.SelectedNode.Parent.Index].Textures[treeView1.SelectedNode.Index];
}
}
tex.pixelInternalFormat = G1Texture.GetPixelInternalFormatForTextures((byte)((ComboBoxItem)comboBoxCompression.SelectedItem).Value);
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
var nodeRoot = treeView1.SelectedNode;
while (nodeRoot.Parent != null)
{
nodeRoot = nodeRoot.Parent;
}
if (FormatIndex != nodeRoot.Index)
{
FormatIndex = nodeRoot.Index;
}
if (treeView1.SelectedNode.Level == 0)
{
pictureBox1.Image = null;
}
if (FormatIndex == NodeIndexBin && treeView1.Nodes.ContainsKey("BIN"))
{
if (treeView1.SelectedNode.Level == 1)
{
pictureBox1.Image = null;
}
if (treeView1.SelectedNode.Level == 2)
{
if (Path.GetExtension(treeView1.SelectedNode.Text) == ".g1t")
{
LoadImage(BinFileList[treeView1.SelectedNode.Parent.Index][treeView1.SelectedNode.Index].Textures[treeView1.SelectedNode.FirstNode.Index]);
}
else
{
pictureBox1.Image = null;
}
}
if (treeView1.SelectedNode.Level == 3)
{
LoadImage(BinFileList[treeView1.SelectedNode.Parent.Parent.Index][treeView1.SelectedNode.Parent.Index].Textures[treeView1.SelectedNode.Index]);
}
}
if (FormatIndex == NodeIndexG1T && treeView1.Nodes.ContainsKey("G1T"))
{
if (treeView1.SelectedNode.Level == 1)
{
LoadImage(G1TFileList[treeView1.SelectedNode.Index].Textures[treeView1.SelectedNode.FirstNode.Index]);
}
if (treeView1.SelectedNode.Level == 2)
{
LoadImage(G1TFileList[treeView1.SelectedNode.Parent.Index].Textures[treeView1.SelectedNode.Index]);
}
}
}
/*
switch (Path.GetExtension(openFileDialog.FileNames[FormatIndex])) // FileIndex
{
case ".bin":
{
if (Path.GetExtension(treeView1.SelectedNode.Text) == ".g1t")
{
if (treeView1.SelectedNode.Level == 1)
{
LoadImage(BinEntries[FormatIndex][treeView1.SelectedNode.Index][treeView1.SelectedNode.FirstNode.Index][treeView1.SelectedNode.FirstNode.Index]);
}
}
if (treeView1.SelectedNode.Level == 2)
{
var node = treeView1.SelectedNode;
while (node.Parent.Level == 1)
{
node = node.Parent;
}
LoadImage(BinEntries[FormatIndex][node.Index][treeView1.SelectedNode.Index][treeView1.SelectedNode.Index]);
}
}
break;
case ".g1t":
{
if (treeView1.SelectedNode.Level == 0)
{
LoadImage(G1TEntries[FormatIndex][treeView1.SelectedNode.FirstNode.Index]);
}
if (treeView1.SelectedNode.Level == 1)
{
LoadImage(G1TEntries[FormatIndex][treeView1.SelectedNode.Index]);
}
}
break;
} */
private void LoadImage(G1Texture tex)
{
tex.Mipmap.Bind();
widthLabel.Text = $"Width: {tex.Mipmap.Width}";
heightLabel.Text = $"Height: {tex.Mipmap.Height}";
numericUpDownMipMap.Value = tex.MipMapCount;
if (tex.NormalMapFlags != 0)
checkBoxNormalMap.Checked = true;
else
checkBoxNormalMap.Checked = false;
checkBoxExHeader.Checked = tex.UsesExtraHeader;
checkBoxNormalMap.Text = $"Normal map ({tex.NormalMapFlags:X})";
pictureBox1.Image = tex.Mipmap.GetBitmap();
comboBoxCompression.SelectedItem = GetComboBoxItemByValue(tex.compression_format);
numericUpDownMipMap.Enabled = true;
checkBoxNormalMap.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
using (var colorDialog = new ColorDialog())
{
colorDialog.FullOpen = true;
if (colorDialog.ShowDialog() == DialogResult.OK)
{
panel1.BackColor = colorDialog.Color;
pictureBox1.BackColor = colorDialog.Color;
}
}
}
}
}