forked from technificentConsulting/TS4-SimRipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameFileHandler.cs
1150 lines (1055 loc) · 53.9 KB
/
GameFileHandler.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 System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ProtoBuf;
using s4pi.Package;
using s4pi.Interfaces;
using s4pi.ImageResource;
using System.Collections.Generic;
namespace TS4SimRipper
{
public partial class Form1 : Form
{
Package[] gamePackages = new Package[0];
string[] gamePackageNames = new string[0];
bool[] notBaseGame = new bool[0];
public Package TroubleshootPackageTuning = (Package)Package.NewPackage(0);
Package TroubleshootPackageBasic = (Package)Package.NewPackage(0);
Package TroubleshootPackageOutfit = (Package)Package.NewPackage(0);
public Dictionary<(uint, uint, ulong), (string, Package)> allMaxisInstances = new Dictionary<(uint, uint, ulong), (string, Package)>();
public Dictionary<(uint, uint, ulong), (string, Package)> allCCInstances = new Dictionary<(uint, uint, ulong), (string, Package)>();
public Dictionary<(uint, uint, ulong), (string, Package)> allInstances = new Dictionary<(uint, uint, ulong), (string, Package)>();
private bool DetectFilePaths()
{
try
{
if (String.Compare(Properties.Settings.Default.TS4Path, " ") <= 0)
{
string tmp = (string)Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Maxis\\The Sims 4", "Install Dir", null);
if (tmp != null) Properties.Settings.Default.TS4Path = tmp;
//MessageBox.Show(tmp);
Properties.Settings.Default.Save();
}
if (String.Compare(Properties.Settings.Default.TS4ModsPath, " ") <= 0)
{
string tmp = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\Electronic Arts\\The Sims 4\\Mods";
if (!Directory.Exists(tmp))
{
string[] tmp2 = Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\Electronic Arts\\", "*Sims 4*", SearchOption.AllDirectories);
if (tmp2.Length == 1)
{
// tmp = Path.GetDirectoryName(tmp2[0]) + Path.DirectorySeparatorChar;
tmp = tmp2[0] + Path.DirectorySeparatorChar + "Mods" + Path.DirectorySeparatorChar;
}
else if (tmp2.Length > 1)
{
if (tmp2[0].Length < tmp2[1].Length)
tmp = tmp2[0] + Path.DirectorySeparatorChar + "Mods" + Path.DirectorySeparatorChar;
//tmp = Path.GetDirectoryName(tmp2[0]) + Path.DirectorySeparatorChar;
else
tmp = tmp2[1] + Path.DirectorySeparatorChar + "Mods" + Path.DirectorySeparatorChar;
//tmp = Path.GetDirectoryName(tmp2[1]) + Path.DirectorySeparatorChar;
}
}
if (tmp != null) Properties.Settings.Default.TS4ModsPath = tmp;
}
if (String.Compare(Properties.Settings.Default.TS4SavesPath, " ") <= 0)
{
string tmp = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\Electronic Arts\\The Sims 4\\saves";
if (!Directory.Exists(tmp))
{
string[] tmp2 = Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\Electronic Arts\\", "*Sims 4*", SearchOption.AllDirectories);
if (tmp2.Length == 1)
{
// tmp = Path.GetDirectoryName(tmp2[0]) + Path.DirectorySeparatorChar;
tmp = tmp2[0] + Path.DirectorySeparatorChar + "saves";
}
else if (tmp2.Length > 1)
{
if (tmp2[0].Length < tmp2[1].Length)
tmp = tmp2[0] + Path.DirectorySeparatorChar + "saves";
//tmp = Path.GetDirectoryName(tmp2[0]) + Path.DirectorySeparatorChar;
else
tmp = tmp2[1] + Path.DirectorySeparatorChar + "saves";
//tmp = Path.GetDirectoryName(tmp2[1]) + Path.DirectorySeparatorChar;
}
}
if (tmp != null) Properties.Settings.Default.TS4SavesPath = tmp; Properties.Settings.Default.Save();
}
if (Properties.Settings.Default.TS4Path == null || Properties.Settings.Default.TS4ModsPath == null) return false;
}
catch (Exception e)
{
return false;
}
return (Directory.Exists(Properties.Settings.Default.TS4Path) & Directory.Exists(Properties.Settings.Default.TS4ModsPath));
}
private bool SetupGamePacks()
{
string TS4FilesPath = Properties.Settings.Default.TS4Path;
List<Package> gamePacks = new List<Package>();
List<string> paths = new List<string>();
List<bool> notBase = new List<bool>();
try
{
List<string> pathsSim = new List<string>(Directory.GetFiles(TS4FilesPath, "Simulation*Build*.package", SearchOption.AllDirectories));
List<string> pathsClient = new List<string>(Directory.GetFiles(TS4FilesPath, "Client*Build*.package", SearchOption.AllDirectories));
List<string> orderedPathsSim = new List<string>();
List<string> orderedPathsClient = new List<string>();
List<string> emptyList = new List<string>();
List<string> packPrefixes = new List<string>() { "EP", "GP", "SP", "FP" };
addPathsAccordingToPatchPriority(pathsClient, ref orderedPathsClient, "", "ClientDelta", "ClientFull", packPrefixes);
addPathsAccordingToPatchPriority(pathsSim, ref orderedPathsSim, "", "SimulationDelta", "SimulationFull", packPrefixes);
addPathsAccordingToPatchPriority(pathsClient, ref orderedPathsClient, "Client", "ClientDelta", "ClientFull", emptyList);
addPathsAccordingToPatchPriority(pathsSim, ref orderedPathsSim, "Simulation", "SimulationDelta", "SimulationFull", emptyList);
paths.AddRange(orderedPathsSim);
paths.AddRange(orderedPathsClient);
}
catch (DirectoryNotFoundException e)
{
MessageBox.Show("Your game packages path is invalid! Please go to File / Change Settings and correct it or make it blank to reset, then restart."
+ Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace);
return false;
}
catch (IOException e)
{
MessageBox.Show("Your game packages path is invalid or a network error has occurred! Please go to File / Change Settings and correct it or make it blank to reset, then restart."
+ Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace);
return false;
}
catch (ArgumentException e)
{
MessageBox.Show("Your game packages path is not specified correctly! Please go to File / Change Settings and correct it or make it blank to reset, then restart."
+ Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace);
return false;
}
catch (UnauthorizedAccessException e)
{
MessageBox.Show("You do not have the required permissions to access the game packages folder! Please restart with admin privileges."
+ Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace);
return false;
}
if (paths.Count == 0)
{
MessageBox.Show("Can't find game packages! Please go to File / Change Settings and correct the game packages path or make it blank to reset, then restart.");
return false;
}
Predicate<IResourceIndexEntry> testPred = r => r.ResourceType == (uint)ResourceTypes.Rig;
try
{
string err = "";
for (int i = 0; i < paths.Count; i++)
{
if (paths[i].Contains("Delta_LE"))
{
continue;
}
try
{
Package p = OpenPackage(paths[i], false);
if (p == null)
{
err += paths[i] + " is NULL" + Environment.NewLine;
paths[i] = null;
continue;
}
else
{
foreach (IResourceIndexEntry indexEntry in p.GetResourceList)
{
(uint ResourceType, uint ResourceGroup, ulong Instance) key = (indexEntry.ResourceType, indexEntry.ResourceGroup, indexEntry.Instance);
bool deleted = indexEntry.Compressed == 0xFFE0;
if (!deleted)
{
if (this.allMaxisInstances.ContainsKey(key))
{
this.allMaxisInstances.Remove(key);
}
if (this.allInstances.ContainsKey(key))
{
this.allInstances.Remove(key);
}
this.allMaxisInstances.Add(key, (paths[i], p));
this.allInstances.Add(key, (paths[i], p));
}
}
}
gamePacks.Add(p);
notBase.Add(!paths[i].Contains("\\Data\\"));
}
catch (Exception e)
{
err += paths[i] + " : " + e.Message + Environment.NewLine;
paths[i] = null;
}
}
if (err.Length > 0) MessageBox.Show("Unable to open the following game packages:" + Environment.NewLine + err);
List<Package> ccPacks = new List<Package>();
List<string> ccPaths = new List<string>();
List<bool> isCC = new List<bool>();
string[] localCC = null;
try
{
localCC = Directory.GetFiles(Properties.Settings.Default.TS4ModsPath,
"*.package", SearchOption.AllDirectories);
}
catch
{
MessageBox.Show("Either the path to your user Sims 4 folder in Documents is incorrect or you have no custom content." +
Environment.NewLine + "If the path is incorrect, go to File / Change Settings and correct it or make it blank to reset, then restart.");
}
if (localCC != null)
{
ccPaths = new List<string>(localCC);
// ccPaths.Sort((a, b) => b.CompareTo(a)); //descending sort
ccPaths.Sort();
err = "";
for (int j = 0; j < ccPaths.Count; j++)
{
try
{
Package p = OpenPackage(ccPaths[j], false);
if (p == null)
{
err += ccPaths[j] + " is NULL" + Environment.NewLine;
ccPaths[j] = null;
continue;
}
else
{
foreach (IResourceIndexEntry indexEntry in p.GetResourceList)
{
(uint ResourceType, uint ResourceGroup, ulong Instance) key = (indexEntry.ResourceType, indexEntry.ResourceGroup, indexEntry.Instance);
add_CC_keys(ccPaths, j, p, key);
if (indexEntry.ResourceGroup == 0x80000000)
{
(uint ResourceType, uint ResourceGroup, ulong Instance) zero_group_key = (indexEntry.ResourceType, 0, indexEntry.Instance);
add_CC_keys(ccPaths, j, p, zero_group_key);
}
}
}
ccPacks.Add(p);
isCC.Add(true);
}
catch (Exception e)
{
err += ccPaths[j] + " : " + e.Message + Environment.NewLine;
ccPaths[j] = null;
}
}
if (err.Length > 0) MessageBox.Show("Unable to open the following mod packages:" + Environment.NewLine + err);
}
ccPacks.AddRange(gamePacks);
ccPaths.AddRange(paths);
isCC.AddRange(notBase);
ccPaths.RemoveAll(item => item == null);
gamePackages = ccPacks.ToArray();
gamePackageNames = ccPaths.ToArray();
notBaseGame = isCC.ToArray();
}
catch (UnauthorizedAccessException e)
{
MessageBox.Show("You do not have the required permissions to open the game and/or mod packages! Please restart with admin privileges."
+ Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace);
return false;
}
catch (Exception e)
{
MessageBox.Show(e.Message + Environment.NewLine + e.StackTrace);
return false;
}
return true;
}
private static void addPathsAccordingToPatchPriority(List<string> inputPaths, ref List<string> paths, string prefix, string packDelta, string packFull, List<string> packTypes)
{
getPackageAndAddIfValid(inputPaths, ref paths, prefix, packFull, packTypes);
getPackageAndAddIfValid(inputPaths, ref paths, prefix, packDelta, packTypes);
}
private static void getPackageAndAddIfValid(List<string> inputPaths, ref List<string> paths, string prefix, string desiredPackagePrefix, List<string> packTypes)
{
for (int i = 0; i < inputPaths.Count; i++)
{
// Don't include legacy edition packages
if (inputPaths[i].Contains("Delta_LE"))
{
continue;
}
if (packTypes.Count == 0)
{
if (inputPaths[i].Contains(desiredPackagePrefix))
{
paths.Add(inputPaths[i]);
}
}
else
{
if (packTypes.Count != 0)
{
string[] directories = inputPaths[i].Split(Path.DirectorySeparatorChar);
bool isBGPackage = true;
foreach (string packType in packTypes)
{
if (directories[directories.Length - 2].Contains(packType))
{
isBGPackage = false;
break;
}
}
if (!isBGPackage & prefix == "" & inputPaths[i].Contains(desiredPackagePrefix))
{
paths.Add(inputPaths[i]);
}
}
}
}
}
private void add_CC_keys(List<string> paths, int j, Package p, (uint ResourceType, uint ResourceGroup, ulong Instance) key)
{
if (this.allCCInstances.ContainsKey(key))
{
this.allCCInstances.Remove(key);
}
if (this.allInstances.ContainsKey(key))
{
this.allInstances.Remove(key);
}
this.allCCInstances.Add(key, (paths[j], p));
this.allInstances.Add(key, (paths[j], p));
}
private void SaveStream(IResourceIndexEntry irie, BinaryReader br, Package pack)
{
Stream s = new MemoryStream();
br.BaseStream.CopyTo(s);
s.Position = 0;
pack.AddResource(irie, s, true);
}
private void SaveStream(TGI tgi, Stream r, Package pack)
{
Stream s = new MemoryStream();
r.CopyTo(s);
s.Position = 0;
TGIBlock tgiblock = new TGIBlock(1, null, tgi.Type, tgi.Group, tgi.Instance);
pack.AddResource(tgiblock, s, true);
}
private void SaveStream(IResourceIndexEntry irie, Stream r, Package pack)
{
Stream s = new MemoryStream();
r.CopyTo(s);
s.Position = 0;
pack.AddResource(irie, s, true);
}
private Sculpt FetchGameSculpt(TGI tgi, ref string errorMsg)
{
if (tgi.Instance == 0ul) return null;
(uint ResourceType, uint ResourceGroup, ulong Instance) key = (tgi.Type, tgi.Group, tgi.Instance);
Predicate<IResourceIndexEntry> pred = r => r.ResourceType == tgi.Type & r.Instance == tgi.Instance;
if (this.allInstances.ContainsKey(key))
{
Package p = this.allInstances[key].Item2;
IResourceIndexEntry irie = p.Find(pred);
if (irie != null)
{
using (BinaryReader br = new BinaryReader(p.GetResource(irie)))
{
if (this.allCCInstances.ContainsKey(key)) SaveStream(irie, br, TroubleshootPackageBasic);
try
{
Sculpt sculpt = new Sculpt(br);
return sculpt;
}
catch (Exception e)
{
errorMsg += "Can't read Sculpt " + tgi.ToString() + ", Package: " + Path.GetFileName(this.allInstances[key].Item1) + " : " + e.Message + Environment.NewLine;
return null;
}
}
}
}
errorMsg += "Can't find Sculpt " + tgi.ToString() + Environment.NewLine;
return null;
}
private SMOD FetchGameSMOD(TGI tgi, ref string errorMsg)
{
if (tgi.Instance == 0ul) return null;
(uint ResourceType, uint ResourceGroup, ulong Instance) key = (tgi.Type, tgi.Group, tgi.Instance);
if (this.allInstances.ContainsKey(key)) {
Predicate<IResourceIndexEntry> pred = r => r.ResourceType == tgi.Type & r.ResourceGroup == tgi.Group & r.Instance == tgi.Instance;
Package p = this.allInstances[key].Item2;
IResourceIndexEntry irie = p.Find(pred);
if (irie != null)
{
using (BinaryReader br = new BinaryReader(p.GetResource(irie)))
{
if (this.allCCInstances.ContainsKey(key)) SaveStream(irie, br, TroubleshootPackageBasic);
try
{
SMOD smod = new SMOD(br);
return smod;
}
catch (Exception e)
{
errorMsg += "Can't read SimModifier " + tgi.ToString() + ", Package: " + Path.GetFileName(this.allInstances[key].Item1) + " : " + e.Message + Environment.NewLine;
return null;
}
}
}
}
errorMsg += "Can't find SimModifier " + tgi.ToString() + Environment.NewLine;
return null;
}
private BGEO FetchGameBGEO(TGI tgi, ref string errorMsg)
{
if (tgi.Instance == 0ul) return null;
(uint ResourceType, uint ResourceGroup, ulong Instance) key = (tgi.Type, tgi.Group, tgi.Instance);
if (this.allInstances.ContainsKey(key))
{ Predicate<IResourceIndexEntry> pred = r => r.ResourceType == tgi.Type & r.Instance == tgi.Instance;
Package p = this.allInstances[key].Item2;
IResourceIndexEntry irie = p.Find(pred);
if (irie != null)
{
using (BinaryReader br = new BinaryReader(p.GetResource(irie)))
{
if (this.allCCInstances.ContainsKey(key)) SaveStream(irie, br, TroubleshootPackageBasic);
try
{
BGEO bgeo = new BGEO(br);
return bgeo;
}
catch (Exception e)
{
errorMsg += "Can't read BGEO " + tgi.ToString() + ", Package: " + Path.GetFileName(this.allInstances[key].Item1) + " : " + e.Message + Environment.NewLine;
return null;
}
}
}
}
errorMsg += "Can't find BGEO " + tgi.ToString() + Environment.NewLine;
return null;
}
private DMap FetchGameDMap(TGI tgi, ref string errorMsg)
{
if (tgi.Instance == 0ul) return null;
(uint ResourceType, uint ResourceGroup, ulong Instance) key = (tgi.Type, tgi.Group, tgi.Instance);
{
Predicate<IResourceIndexEntry> pred = r => r.ResourceType == tgi.Type & r.Instance == tgi.Instance;
if (this.allInstances.ContainsKey(key))
{
Package p = this.allInstances[key].Item2;
IResourceIndexEntry irie = p.Find(pred);
if (irie != null)
{
using (BinaryReader br = new BinaryReader(p.GetResource(irie)))
{
if (this.allCCInstances.ContainsKey(key)) SaveStream(irie, br, TroubleshootPackageBasic);
try
{
DMap dmap = new DMap(br);
return dmap;
}
catch (Exception e)
{
errorMsg += "Can't read DMap " + tgi.ToString() + ", Package: " + Path.GetFileName(this.allInstances[key].Item1) + " : " + e.Message + Environment.NewLine;
return null;
}
}
}
}
}
errorMsg += "Can't find DeformerMap " + tgi.ToString() + Environment.NewLine;
return null;
}
private BOND FetchGameBOND(TGI tgi, ref string errorMsg)
{
if (tgi.Instance == 0ul) return null;
(uint ResourceType, uint ResourceGroup, ulong Instance) key = (tgi.Type, tgi.Group, tgi.Instance);
if (this.allInstances.ContainsKey(key))
{
Predicate<IResourceIndexEntry> pred = r => r.ResourceType == tgi.Type & r.Instance == tgi.Instance;
Package p = this.allInstances[key].Item2;
IResourceIndexEntry irie = p.Find(pred);
if (irie != null)
{
using (BinaryReader br = new BinaryReader(p.GetResource(irie)))
{
if (this.allCCInstances.ContainsKey(key)) SaveStream(irie, br, TroubleshootPackageBasic);
try
{
BOND bond = new BOND(br);
return bond;
}
catch (Exception e)
{
errorMsg += "Can't read BOND " + tgi.ToString() + ", Package: " + Path.GetFileName(this.allInstances[key].Item1) + " : " + e.Message + Environment.NewLine;
return null;
}
}
}
}
errorMsg += "Can't find BoneDelta " + tgi.ToString() + Environment.NewLine;
return null;
}
private CASP FetchGameCASP(TGI tgi, out string packageName, BodyType partType, int outfitNumber, ref string errorMsg, bool saveme)
{
if (tgi.Instance == 0ul) { packageName = ""; return null; }
(uint ResourceType, uint ResourceGroup, ulong Instance) key = (tgi.Type, tgi.Group, tgi.Instance);
if (this.allInstances.ContainsKey(key))
{
Predicate<IResourceIndexEntry> pred = r => r.ResourceType == tgi.Type & r.Instance == tgi.Instance;
Package p = this.allInstances[key].Item2;
IResourceIndexEntry irie = p.Find(pred);
if (irie != null)
{
using (BinaryReader br = new BinaryReader(p.GetResource(irie)))
{
if (this.allCCInstances.ContainsKey(key) && saveme) SaveStream(irie, br, TroubleshootPackageOutfit);
try
{
CASP casp = new CASP(br);
casp.tgi = new TGI(irie.ResourceType, irie.ResourceGroup, irie.Instance);
casp.notBaseGame = this.allCCInstances.ContainsKey(key);
packageName = Path.GetFileName(allInstances[key].Item1);
return casp;
}
catch (Exception e)
{
errorMsg += "Can't read " + partType.ToString() + " CASP " + tgi.ToString() + ", Package: " + Path.GetFileName(allInstances[key].Item1) + " : " + e.Message + Environment.NewLine;
packageName = Path.GetFileName(this.allInstances[key].Item1);
if (this.allCCInstances.ContainsKey(key) && !saveme) SaveStream(irie, br, TroubleshootPackageOutfit);
return null;
}
}
}
}
errorMsg += "Can't find " + partType.ToString() + " CASP " + tgi.ToString() + Environment.NewLine;
packageName = "";
return null;
}
private GEOM FetchGameGEOM(TGI tgi, string packname, int outfitNumber, ref string errorMsg)
{
if (tgi.Instance == 0ul) return null;
(uint ResourceType, uint ResourceGroup, ulong Instance) key = (tgi.Type, tgi.Group, tgi.Instance);
if (this.allInstances.ContainsKey(key))
{
Predicate<IResourceIndexEntry> pred = r => r.ResourceType == tgi.Type & r.ResourceGroup == tgi.Group & r.Instance == tgi.Instance;
Package p = this.allInstances[key].Item2;
IResourceIndexEntry irie = p.Find(pred);
if (irie != null)
{
using (BinaryReader br = new BinaryReader(p.GetResource(irie)))
{
if (this.allCCInstances.ContainsKey(key)) SaveStream(irie, br, TroubleshootPackageOutfit);
try
{
GEOM geom = new GEOM(br);
geom.StandardizeFormat();
return geom;
}
catch (Exception e)
{
errorMsg += "Can't read GEOM " + tgi.ToString() + ", Package: " + this.allCCInstances.ContainsKey(key) + " : " + e.Message + Environment.NewLine;
return null;
}
}
}
}
errorMsg += "Can't find GEOM mesh " + tgi.ToString() + ", Linked from package: " + packname + Environment.NewLine;
return null;
}
private RegionMap FetchGameRMap(TGI tgi, string packname, int outfitNumber, ref string errorMsg)
{
if (tgi.Instance == 0ul) return null;
(uint ResourceType, uint ResourceGroup, ulong Instance) key = (tgi.Type, tgi.Group, tgi.Instance);
Predicate<IResourceIndexEntry> pred = r => r.ResourceType == tgi.Type & r.Instance == tgi.Instance;
if (this.allInstances.ContainsKey(key))
{
Package p = this.allInstances[key].Item2;
IResourceIndexEntry irie = p.Find(pred);
if (irie != null)
{
using (BinaryReader br = new BinaryReader(p.GetResource(irie)))
{
if (this.allCCInstances.ContainsKey(key)) SaveStream(irie, br, TroubleshootPackageOutfit);
try
{
RegionMap rmap = new RegionMap(br);
return rmap;
}
catch (Exception e)
{
errorMsg += "Can't read RMap " + tgi.ToString() + ", Package: " + this.allCCInstances.ContainsKey(key) + " : " + e.Message + Environment.NewLine;
return null;
}
}
}
}
errorMsg += "Can't find RegionMap " + tgi.ToString() + ", Linked from package: " + packname + Environment.NewLine;
return null;
}
private RLEResource FetchGameRLE(TGI tgi, int outfitNumber, ref string errorMsg)
{
if (tgi.Instance == 0ul) return null;
(uint ResourceType, uint ResourceGroup, ulong Instance) key = (tgi.Type, tgi.Group, tgi.Instance);
Predicate<IResourceIndexEntry> pred = r => r.ResourceType == tgi.Type & r.Instance == tgi.Instance;
if (this.allInstances.ContainsKey(key))
{
Package p = this.allInstances[key].Item2;
IResourceIndexEntry irie = p.Find(pred);
if (irie != null)
{
Stream s = p.GetResource(irie);
if (this.allCCInstances.ContainsKey(key)) SaveStream(irie, s, TroubleshootPackageOutfit);
try
{
RLEResource rle = new RLEResource(1, s);
return rle;
}
catch (Exception e)
{
errorMsg += "Can't read RLE " + tgi.ToString() + ", Package: " + Path.GetFileName(this.allInstances[key].Item1) + " : " + e.Message + Environment.NewLine;
}
}
}
errorMsg += "Can't find RLE texture " + tgi.ToString() + Environment.NewLine;
return null;
}
private Bitmap FetchGameImageFromRLE(TGI tgi, int outfitNumber, ref string errorMsg)
{
return FetchGameImageFromRLE(tgi, false, outfitNumber, ref errorMsg, true);
}
private Bitmap FetchGameImageFromRLE(TGI tgi, bool isSpecular, int outfitNumber, ref string errorMsg, bool writeLog)
{
if (tgi.Instance == 0ul) return null;
(uint ResourceType, uint ResourceGroup, ulong Instance) key = (tgi.Type, tgi.Group, tgi.Instance);
(uint ResourceType, uint ResourceGroup, ulong Instance) keyLrle = ((uint)ResourceTypes.LRLE, tgi.Group, tgi.Instance);
Predicate<IResourceIndexEntry> pred = r => r.ResourceType == tgi.Type & r.ResourceGroup == tgi.Group & r.Instance == tgi.Instance;
Predicate<IResourceIndexEntry> predLrle = r => r.ResourceType == (uint)ResourceTypes.LRLE & r.ResourceGroup == tgi.Group & r.Instance == tgi.Instance;
if (this.allInstances.ContainsKey(key) || this.allInstances.ContainsKey(keyLrle))
{
Package p = this.allInstances[key].Item2;
IResourceIndexEntry irie = p.Find(pred);
IResourceIndexEntry irieLrle = p.Find(predLrle);
if (irie != null || irieLrle != null)
{
try
{
Stream s = new MemoryStream();
if (irie != null)
{
s = p.GetResource(irie);
}
if (s.Length < 4)
{
if (irieLrle != null)
{
s = p.GetResource(irieLrle);
}
}
if (this.allCCInstances.ContainsKey(key)) SaveStream(irie, s, outfitNumber >= 0 ? TroubleshootPackageOutfit : TroubleshootPackageBasic);
using (RLEResource rle = new RLEResource(1, s))
{
if (rle != null && rle.AsBytes.Length > 0)
{
using (DdsFile dds = new DdsFile())
{
dds.Load(rle.ToDDS(), false);
Bitmap texture;
if (isSpecular)
{
Size specSize = new Size(currentSize.Width / 2, currentSize.Height / 2);
if (dds.Size == specSize) texture = new Bitmap(dds.Image);
else texture = new Bitmap(dds.Image, specSize);
}
else
{
Bitmap tmp = dds.Image;
if (tmp.Size == currentSize) texture = new Bitmap(tmp);
else texture = new Bitmap(tmp, currentSize);
}
return texture;
}
}
}
}
catch (Exception e)
{
if (isSpecular && writeLog)
{
errorMsg += "Can't read RLES image " + tgi.ToString() + ", Package: " + Path.GetFileName(this.allInstances[key].Item1) + " : " + e.Message + Environment.NewLine;
}
}
}
}
if (isSpecular)
{
if (writeLog) errorMsg += "Can't find RLES texture " + tgi.ToString() + Environment.NewLine;
}
else
{
if (writeLog) errorMsg += "Can't find RLE texture " + tgi.ToString() + Environment.NewLine;
}
return null;
}
private LRLE FetchGameLRLE(TGI tgi, int outfitNumber, ref string errorMsg)
{
if (tgi.Instance == 0ul) return null;
(uint ResourceType, uint ResourceGroup, ulong Instance) key = (tgi.Type, tgi.Group, tgi.Instance);
Predicate<IResourceIndexEntry> pred = r => r.ResourceType == tgi.Type & r.Instance == tgi.Instance;
if (this.allInstances.ContainsKey(key))
{
Package p = this.allInstances[key].Item2;
IResourceIndexEntry irie = p.Find(pred);
if (irie != null)
{
using (BinaryReader br = new BinaryReader(p.GetResource(irie)))
{
if (this.allCCInstances.ContainsKey(key)) SaveStream(irie, br, TroubleshootPackageOutfit);
try
{
LRLE lrle = new LRLE(br);
return lrle;
}
catch (Exception e)
{
errorMsg += "Can't read LRLE " + tgi.ToString() + ", Package: " + Path.GetFileName(this.allInstances[key].Item1) + " : " + e.Message + Environment.NewLine;
}
}
}
}
errorMsg += "Can't find LRLE texture " + tgi.ToString() + Environment.NewLine;
return null;
}
private Bitmap FetchGameImageFromLRLE(TGI tgi, int outfitNumber, ref string errorMsg)
{
return FetchGameImageFromLRLE(tgi, outfitNumber, ref errorMsg, true);
}
private Bitmap FetchGameImageFromLRLE(TGI tgi, int outfitNumber, ref string errorMsg, bool writeLog)
{
if (tgi.Instance == 0ul) return null;
(uint ResourceType, uint ResourceGroup, ulong Instance) key = (tgi.Type, tgi.Group, tgi.Instance);
Predicate<IResourceIndexEntry> pred = r => r.ResourceType == tgi.Type & r.Instance == tgi.Instance;
if (this.allInstances.ContainsKey(key))
{
Package p = this.allInstances[key].Item2;
IResourceIndexEntry irie = p.Find(pred);
if (irie != null)
{
Stream s = p.GetResource(irie);
if (this.allCCInstances.ContainsKey(key)) SaveStream(irie, s, outfitNumber >= 0 ? TroubleshootPackageOutfit : TroubleshootPackageBasic);
try
{
BinaryReader br = new BinaryReader(s);
LRLE lrle = new LRLE(br);
Bitmap tmp = lrle.image;
if (tmp.Size == currentSize) return new Bitmap(tmp);
else return new Bitmap(tmp, currentSize);
}
catch
{
try
{
using (RLEResource rle = new RLEResource(1, s))
{
if (rle != null && rle.AsBytes.Length > 0)
{
using (DdsFile dds = new DdsFile())
{
dds.Load(rle.ToDDS(), false);
Bitmap texture;
Bitmap tmp = dds.Image;
if (tmp.Size == currentSize) texture = new Bitmap(tmp);
else texture = new Bitmap(tmp, currentSize);
return texture;
}
}
}
}
catch (Exception e)
{
if (writeLog) errorMsg += "Can't read LRLE image " + tgi.ToString() + ", Package: " + Path.GetFileName(this.allInstances[key].Item1) + " : " + e.Message + Environment.NewLine;
}
}
}
}
//Predicate<IResourceIndexEntry> pred2 = r => r.ResourceType == (uint)ResourceTypes.RLE2 & r.ResourceGroup == tgi.Group & r.Instance == tgi.Instance;
//for (int i = 0; i < gamePackages.Length; i++)
//{
// Package p = gamePackages[i];
// IResourceIndexEntry irie = p.Find(pred2);
// if (irie != null)
// {
// if (isCCPackage[i] && !currentCCPackages.Any(x => x.package == p)) { currentCCPackages.Add(new CCPackage(p, Path.GetFileName(Path.GetFileName(this.allInstances[key].Item1)), outfitNumber)); }
// Stream s = p.GetResource(irie);
// try
// {
// using (RLEResource rle = new RLEResource(1, s))
// {
// if (rle != null && rle.AsBytes.Length > 0)
// {
// using (DdsFile dds = new DdsFile())
// {
// dds.Load(rle.ToDDS(), false);
// Bitmap texture;
// Bitmap tmp = dds.Image;
// if (tmp.Size == currentSize) texture = new Bitmap(tmp);
// else texture = new Bitmap(tmp, currentSize);
// return texture;
// }
// }
// }
// }
// catch
// {
// errorMsg += "Can't read RLE image " + tgi.ToString() + ", Package: " + Path.GetFileName(this.allInstances[key].Item1) + Environment.NewLine;
// }
// }
//}
if (writeLog) errorMsg += "Can't find LRLE texture " + tgi.ToString() + Environment.NewLine;
return null;
}
private Bitmap FetchGameTexture(ulong instance, int outfitNumber, ref string errorMsg, bool includeDDS) //Looks for LRLE, RLE2, and optionally DDS
{
return FetchGameTexture(new TGI((uint)ResourceTypes.LRLE, 0, instance), outfitNumber, ref errorMsg, includeDDS);
}
private Bitmap FetchGameTexture(TGI tgi, int outfitNumber, ref string errorMsg, bool includeDDS) //Looks for LRLE, RLE2, and optionally DDS
{
TGI lrleTGI = new TGI((uint)ResourceTypes.LRLE, tgi.Group, tgi.Instance);
TGI rle2TGI = new TGI((uint)ResourceTypes.RLE2, tgi.Group, tgi.Instance);
TGI ddsTGI = new TGI((uint)ResourceTypes.DDSuncompressed, tgi.Group, tgi.Instance);
Bitmap image = FetchGameImageFromLRLE(lrleTGI, outfitNumber, ref errorMsg, false);
if (image != null) return image;
image = FetchGameImageFromRLE(rle2TGI, false, outfitNumber, ref errorMsg, false);
if (image != null) return image;
if (includeDDS) image = FetchGameImageFromDDS(ddsTGI, outfitNumber, ref errorMsg, false);
if (image == null) errorMsg += "Can't find or read texture, instance: 0x" + tgi.Instance.ToString("X16") + Environment.NewLine;
return image;
}
private DSTResource FetchGameDST(TGI tgi, int outfitNumber, ref string errorMsg)
{
if (tgi.Instance == 0ul) return null;
(uint ResourceType, uint ResourceGroup, ulong Instance) key = (tgi.Type, tgi.Group, tgi.Instance);
if (this.allInstances.ContainsKey(key))
{
Predicate<IResourceIndexEntry> pred = r => r.ResourceType == tgi.Type & r.Instance == tgi.Instance;
Package p = this.allInstances[key].Item2;
IResourceIndexEntry irie = p.Find(pred);
if (irie != null)
{
Stream s = p.GetResource(irie);
if (this.allCCInstances.ContainsKey(key)) SaveStream(irie, s, TroubleshootPackageOutfit);
try
{
DSTResource dst = new DSTResource(1, s);
return dst;
}
catch (Exception e)
{
errorMsg += "Can't read DST " + tgi.ToString() + ", Package: " + Path.GetFileName(this.allInstances[key].Item1)+ " : " + e.Message + Environment.NewLine;
return null;
}
}
}
errorMsg += "Can't find DST texture " + tgi.ToString() + Environment.NewLine;
return null;
}
private Bitmap FetchGameImageFromDST(TGI tgi, int outfitNumber, ref string errorMsg)
{
if (tgi.Instance == 0ul) return null;
(uint ResourceType, uint ResourceGroup, ulong Instance) key = (tgi.Type, tgi.Group, tgi.Instance);
if (this.allInstances.ContainsKey(key))
{ Predicate<IResourceIndexEntry> pred = r => r.ResourceType == tgi.Type & r.Instance == tgi.Instance;
Package p = this.allInstances[key].Item2;
IResourceIndexEntry irie = p.Find(pred);
if (irie != null)
{
Stream s = p.GetResource(irie);
if (this.allCCInstances.ContainsKey(key)) SaveStream(irie, s, TroubleshootPackageOutfit);
try
{
using (DSTResource dst = new DSTResource(1, s))
{
if (dst != null)
{
using (DdsFile dds = new DdsFile())
{
dds.Load(dst.ToDDS(), false);
return new Bitmap(dds.Image);
}
}
}
}
catch (Exception e)
{
errorMsg += "Can't read DST image " + tgi.ToString() + ", Package: " + Path.GetFileName(this.allInstances[key].Item1) + " : " + e.Message + Environment.NewLine;
return null;
}
}
}
errorMsg += "Can't find DST texture " + tgi.ToString() + Environment.NewLine;
return null;
}
private DdsFile FetchGameDDS(TGI tgi, int outfitNumber, ref string errorMsg)
{
if (tgi.Instance == 0ul) return null;
(uint ResourceType, uint ResourceGroup, ulong Instance) key = (tgi.Type, tgi.Group, tgi.Instance);
if (this.allInstances.ContainsKey(key))
{
Predicate<IResourceIndexEntry> pred = r => r.ResourceType == tgi.Type & r.Instance == tgi.Instance;
Package p = this.allInstances[key].Item2;
IResourceIndexEntry irie = p.Find(pred);
if (irie != null)
{
Stream s = p.GetResource(irie);
if (this.allCCInstances.ContainsKey(key)) SaveStream(irie, s, TroubleshootPackageOutfit);
try
{
DdsFile dds = new DdsFile();
dds.Load(s, false);
return dds;
}
catch (Exception e)
{
errorMsg += "Can't read DDS " + tgi.ToString() + ", Package: " + Path.GetFileName(this.allInstances[key].Item1) + " : " + e.Message + Environment.NewLine;
return null;