-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
1084 lines (952 loc) · 46.6 KB
/
Program.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.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using CommandLine;
using LibIRD;
using SabreTools.Hashing;
using SabreTools.RedumpLib.Web;
namespace IRDKit
{
internal class Program
{
#region Options
/// <summary>
/// IRD Creation command
/// </summary>
[Verb("create", HelpText = "Create an IRD from an ISO")]
public class CreateOptions
{
[Value(0, Required = true, HelpText = "Path to ISO file(s), or directory of ISO files")]
public IEnumerable<string> ISOPath { get; set; }
[Option('o', "output", HelpText = "Path to the IRD file to be created (will overwrite)")]
public string IRDPath { get; set; }
[Option('b', "layerbreak", HelpText = "Layerbreak value in bytes (use with BD-Video hybrid discs). Default: 12219392")]
public long? Layerbreak { get; set; }
[Option('k', "key", HelpText = "Hexadecimal representation of the disc key")]
public string Key { get; set; }
[Option('l', "getkey-log", HelpText = "Path to a .getkey.log file")]
public string GetKeyLog { get; set; }
[Option('f', "key-file", HelpText = "Path to a redump .key file")]
public string KeyFile { get; set; }
[Option('r', "recurse", HelpText = "Recurse through all subdirectories and generate IRDs for all ISOs")]
public bool Recurse { get; set; }
[Option('v', "verbose", HelpText = "Print more information during IRD creation")]
public bool Verbose { get; set; }
}
/// <summary>
/// IRD or ISO information command
/// </summary>
[Verb("info", HelpText = "Print information from an IRD or ISO")]
public class InfoOptions
{
[Value(0, Required = true, HelpText = "Path to IRD/ISO file(s), or directory of IRD/ISO files")]
public IEnumerable<string> InPath { get; set; }
[Option('o', "output", HelpText = "Path to the text or json file to be created (will overwrite)")]
public string OutPath { get; set; }
[Option('j', "json", HelpText = "Print IRD or ISO information as a JSON object")]
public bool Json { get; set; }
[Option('r', "recurse", HelpText = "Recurse through all subdirectories and print information for all ISOs and IRDs")]
public bool Recurse { get; set; }
}
/// <summary>
/// IRD diff command
/// </summary>
[Verb("diff", HelpText = "Compare two IRDs and print their differences")]
public class DiffOptions
{
[Value(0, Required = true, HelpText = "Path to the first IRD to compare against")]
public string InPath1 { get; set; }
[Value(1, Required = true, HelpText = "Path to the second IRD file to compare")]
public string InPath2 { get; set; }
[Option('o', "output", HelpText = "Path to the text or json file to be created (will overwrite)")]
public string OutPath { get; set; }
}
/// <summary>
/// IRD rename command
/// </summary>
[Verb("rename", HelpText = "Rename one or more IRD files according to the redump PS3 DAT")]
public class RenameOptions
{
[Value(0, Required = true, HelpText = "Path to IRD file(s), or directory of IRD files")]
public IEnumerable<string> IRDPath { get; set; }
[Option('d', "datfile", Required = true, HelpText = "Path to the redump PS3 Datfile")]
public string DATPath { get; set; }
[Option('s', "serial", HelpText = "Appends disc serial to new IRD filename")]
public bool Serial { get; set; }
[Option('e', "version", HelpText = "Appends disc version to new IRD filename")]
public bool Ver { get; set; }
[Option('c', "crc", HelpText = "Appends ISO CRC to new IRD filename")]
public bool CRC { get; set; }
[Option('r', "recurse", HelpText = "Recurse through all subdirectories and rename all IRDs")]
public bool Recurse { get; set; }
[Option('v', "verbose", HelpText = "Print more information about the renaming")]
public bool Verbose { get; set; }
}
#endregion
#region Program
/// <summary>
/// Parse command line arguments
/// </summary>
/// <param name="args">Command line arguments</param>
public static void Main(string[] args)
{
// Ensure console prints foreign characters properly
Console.OutputEncoding = Encoding.UTF8;
// Parse arguments
var result = Parser.Default.ParseArguments<CreateOptions, InfoOptions, DiffOptions, RenameOptions>(args).WithParsed(Run);
}
/// <summary>
/// Parse arguments
/// </summary>
/// <param name="args">Command-line arguments</param>
/// <exception cref="ArgumentException"></exception>
private static void Run(object args)
{
switch (args)
{
// Process options from a `create` command
case CreateOptions opt:
// Validate ISO paths
if (opt.ISOPath == null || !opt.ISOPath.Any())
{
Console.Error.WriteLine("Provide a valid ISO path to create an IRD");
return;
}
foreach (string isoPath in opt.ISOPath)
{
// Validate ISO path
if (string.IsNullOrEmpty(isoPath))
continue;
// If directory, search for all ISOs in current directory
if (Directory.Exists(isoPath))
{
// If recurse option enabled, search recursively
IEnumerable<string> isoFiles;
if (opt.Recurse)
{
if (opt.Verbose && isoPath == ".")
Console.WriteLine($"Recursively searching for ISOs in current directory");
else if (opt.Verbose)
Console.WriteLine($"Recursively searching for ISOs in {isoPath}");
isoFiles = Directory.EnumerateFiles(isoPath, "*.iso", SearchOption.AllDirectories);
}
else
{
if (opt.Verbose && isoPath == ".")
Console.WriteLine($"Searching for ISOs in current directory");
else if (opt.Verbose)
Console.WriteLine($"Searching for ISOs in {isoPath}");
isoFiles = Directory.EnumerateFiles(isoPath, "*.iso", SearchOption.TopDirectoryOnly);
}
// Warn if no files are found
if (!isoFiles.Any())
{
if (opt.Recurse)
Console.Error.WriteLine($"No ISOs found in {isoPath} (ensure .iso extension)");
else
Console.Error.WriteLine($"No ISOs found in {isoPath} (ensure .iso extension, or try use -r)");
continue;
}
// Determine output IRD folder
string outputPath = opt.IRDPath;
if (File.Exists(opt.IRDPath))
outputPath = Path.GetDirectoryName(opt.IRDPath);
// Create an IRD file for all ISO files found
foreach (string file in isoFiles.OrderBy(x => x))
ISO2IRD(file, irdPath: outputPath, keyPath: opt.KeyFile, verbose: opt.Verbose);
}
else
{
// Check that given file exists
if (!File.Exists(isoPath))
{
Console.Error.WriteLine($"ISO not found: {isoPath}");
continue;
}
string irdPath;
// Save to given output path and filename, if only 1 IRD is being created
if (opt.ISOPath.Count() == 1)
irdPath = ISO2IRD(isoPath, irdPath: opt.IRDPath, hexKey: opt.Key, keyPath: opt.KeyFile, getKeyLog: opt.GetKeyLog, layerbreak: opt.Layerbreak, verbose: opt.Verbose);
// Save to given output path, if more than 1 IRD is being created
else
irdPath = ISO2IRD(isoPath, irdPath: Path.GetDirectoryName(opt.IRDPath), verbose: opt.Verbose);
if (irdPath != null)
Console.WriteLine($"IRD saved to {irdPath}");
}
}
break;
// Process options from an `info` command
case InfoOptions opt:
// Validate required parameter
if (opt.InPath == null || !opt.InPath.Any())
{
Console.Error.WriteLine("Provide a valid ISO or IRD path to print info about");
return;
}
// Clear the output file path if it exists
if (opt.OutPath != null && opt.OutPath != "")
File.Delete(opt.OutPath);
foreach (string filePath in opt.InPath)
{
// Validate path
if (string.IsNullOrEmpty(filePath))
continue;
// If directory, search for all ISOs in current directory
if (Directory.Exists(filePath))
{
// If recurse option enabled, search recursively
IEnumerable<string> irdFiles;
IEnumerable<string> isoFiles;
if (opt.Recurse)
{
if (filePath == ".")
Console.WriteLine($"Recursively searching for IRDs and ISOs in current directory...\n");
else
Console.WriteLine($"Recursively searching for IRDs and ISOs in {filePath}...\n");
irdFiles = Directory.EnumerateFiles(filePath, "*.ird", SearchOption.AllDirectories);
isoFiles = Directory.EnumerateFiles(filePath, "*.iso", SearchOption.AllDirectories);
}
else
{
if (filePath == ".")
Console.WriteLine($"Searching for IRDs and ISOs in current directory...\n");
else
Console.WriteLine($"Searching for IRDs and ISOs in {filePath}...\n");
irdFiles = Directory.EnumerateFiles(filePath, "*.ird", SearchOption.TopDirectoryOnly);
isoFiles = Directory.EnumerateFiles(filePath, "*.iso", SearchOption.TopDirectoryOnly);
}
// Warn if no files are found
if (!isoFiles.Any() && !irdFiles.Any())
{
Console.Error.WriteLine("No IRDs or ISOs found (ensure .ird and .iso extensions)");
return;
}
// Open JSON object
if (opt.Json)
{
if (opt.OutPath != null && opt.OutPath != "")
File.AppendAllText(opt.OutPath, "{\n");
else
Console.WriteLine('{');
}
// Print info from all IRDs
bool noISO = !isoFiles.Any();
string lastIRD = irdFiles.Last();
foreach (string file in irdFiles)
{
PrintInfo(file, opt.Json, (noISO && file.Equals(lastIRD)), opt.OutPath);
}
// Print info from all ISOs
string lastISO = isoFiles.Last();
foreach (string file in isoFiles)
{
try
{
PrintISO(file, opt.Json, file.Equals(lastISO), opt.OutPath);
}
catch (IOException)
{
// Not a valid ISO file despite extension, assume file is an IRD
if (!opt.Json)
Console.Error.WriteLine($"{file} is not a valid ISO file\n");
}
}
// Close JSON object
if (opt.Json)
{
if (opt.OutPath != null && opt.OutPath != "")
File.AppendAllText(opt.OutPath, "}\n");
else
Console.WriteLine('}');
}
if (opt.OutPath != null && opt.OutPath != "")
Console.WriteLine($"Info saved to {opt.OutPath}");
}
else
{
// Check that given file exists
if (!File.Exists(filePath))
{
Console.Error.WriteLine($"{filePath} is not a valid file");
continue;
}
// Print info from given file
PrintInfo(filePath, opt.Json, true, opt.OutPath);
if (opt.OutPath != null && opt.OutPath != "")
Console.WriteLine($"Info saved to {opt.OutPath}");
}
}
break;
// Process options from a `diff` command
case DiffOptions opt:
// Validate required parameters
if (opt.InPath1 == null || opt.InPath2 == null || !File.Exists(opt.InPath1) || !File.Exists(opt.InPath2))
{
Console.Error.WriteLine("Provide two paths to IRDs to compare");
return;
}
// Clear the output file path if it exists
if (opt.OutPath != null && opt.OutPath != "")
File.Delete(opt.OutPath);
// Compare the two IRDs
PrintDiff(opt.InPath1, opt.InPath2, opt.OutPath);
if (opt.OutPath != null && opt.OutPath != "")
Console.WriteLine($"Diff saved to {opt.OutPath}");
break;
// Process options from a 'rename' command
case RenameOptions opt:
// Validate required parameters
if (opt.IRDPath == null || !opt.IRDPath.Any())
{
Console.Error.WriteLine("Provide a valid IRD path to rename");
return;
}
// Read DAT file
XDocument datfile = DatParser(opt.DATPath);
if (datfile == null)
{
Console.Error.WriteLine("Unable to parse DAT file");
return;
}
foreach (string irdPath in opt.IRDPath)
{
// Validate IRD path
if (string.IsNullOrEmpty(irdPath))
continue;
// If directory, search for all ISOs in current directory
if (Directory.Exists(irdPath))
{
// If recurse option enabled, search recursively
IEnumerable<string> irdFiles;
if (opt.Recurse)
{
if (opt.Verbose && irdPath == ".")
Console.WriteLine($"Recursively renaming IRDs in current directory");
else if (opt.Verbose)
Console.WriteLine($"Recursively renaming IRDs in {irdPath}");
irdFiles = Directory.EnumerateFiles(irdPath, "*.ird", SearchOption.AllDirectories);
}
else
{
if (opt.Verbose && irdPath == ".")
Console.WriteLine($"Renaming IRDs in current directory");
else if (opt.Verbose)
Console.WriteLine($"Renaming IRDs in {irdPath}");
irdFiles = Directory.EnumerateFiles(irdPath, "*.ird", SearchOption.TopDirectoryOnly);
}
// Warn if no files are found
if (!irdFiles.Any())
{
if (opt.Recurse)
Console.Error.WriteLine($"No IRDs found in {irdPath} (ensure .ird extension)");
else
Console.Error.WriteLine($"No IRDs found in {irdPath} (ensure .ird extension, or try use -r)");
continue;
}
// Rename all IRD files found
foreach (string file in irdFiles.OrderBy(x => x))
{
try
{
RenameIRD(file, datfile, serial: opt.Serial, ver: opt.Ver, crc: opt.CRC, verbose: opt.Verbose);
}
catch (Exception e)
{
Console.Error.WriteLine(e);
}
}
}
else
{
// Check that given file exists
if (!File.Exists(irdPath))
{
Console.Error.WriteLine($"IRD not found: {irdPath}");
continue;
}
// Rename provided IRD path
try
{
RenameIRD(irdPath, datfile, serial: opt.Serial, ver: opt.Ver, crc: opt.CRC, verbose: opt.Verbose);
}
catch (Exception e)
{
Console.Error.WriteLine(e);
}
}
}
break;
// Unknown command
default:
break;
}
}
#endregion
#region Functionality
/// <summary>
/// Prints info about a file
/// </summary>
/// <param name="inPath">File to retrieve info from</param>
/// <param name="json">Whether to format output as JSON (true) or plain text (false)</param>
/// <param name="outPath">File to output info to</param>
public static void PrintInfo(string inPath, bool json, bool single = true, string outPath = null)
{
// Check if file is an ISO
bool isISO = String.Compare(Path.GetExtension(inPath), ".iso", StringComparison.OrdinalIgnoreCase) == 0;
if (isISO)
{
try
{
PrintISO(inPath, json, single, outPath);
return;
}
catch (IOException)
{
// Not a valid ISO file despite extension, try open as IRD
}
}
// Assume it is an IRD file
try
{
if (json)
{
IRD ird = IRD.Read(inPath);
if (outPath != null)
File.AppendAllText(outPath, $"\"{Path.GetFileName(inPath)}\": ");
else
Console.Write($"\"{Path.GetFileName(inPath)}\": ");
ird.PrintJson(outPath, single);
}
else
IRD.Read(inPath).Print(outPath, Path.GetFileName(inPath));
if (json)
return;
return;
}
catch (InvalidDataException)
{
// Not a valid IRD file despite extension, give up
if (json)
return;
if (isISO)
Console.Error.WriteLine($"{inPath} is not a valid ISO file\n");
else
Console.Error.WriteLine($"{inPath} is not a valid IRD file\n");
}
}
/// <summary>
/// Print information about ISO file
/// </summary>
/// <param name="isoPath">Path to ISO file</param>
/// <param name="json">Whether to format output as JSON (true) or plain text (false)</param>
/// <param name="outPath">File to output info to</param>
/// <exception cref="FileNotFoundException"></exception>
/// <exception cref="InvalidFileSystemException"></exception>
public static void PrintISO(string isoPath, bool json, bool single = true, string outPath = null)
{
// Open ISO file for reading
using FileStream fs = new(isoPath, FileMode.Open, FileAccess.Read);
// Validate ISO file stream
if (fs == null || !LibIRD.DiscUtils.Iso9660.CDReader.Detect(fs))
{
Console.Error.WriteLine($"{isoPath} is not a valid ISO file");
return;
}
// Create new ISO reader
using LibIRD.DiscUtils.Iso9660.CDReader reader = new(fs);
// Write PS3_DISC.SFB info
try
{
using LibIRD.DiscUtils.Streams.SparseStream s = reader.OpenFile("\\PS3_DISC.SFB", FileMode.Open, FileAccess.Read);
PS3_DiscSFB ps3_DiscSFB = new(s);
if (json)
{
// Begin JSON object
if (json)
{
if (outPath != null)
File.AppendAllText(outPath, $"\"{Path.GetFileName(isoPath)}\": {{\n");
else
Console.WriteLine($"\"{Path.GetFileName(isoPath)}\": {{");
}
// Print PS3_DISC.SFB info
if (outPath != null)
File.AppendAllText(outPath, "\"PS3_DISC.SFB\": ");
else
Console.Write("\"PS3_DISC.SFB\": ");
ps3_DiscSFB.PrintJson(outPath);
if (outPath != null)
File.AppendAllText(outPath, ",\n");
else
Console.WriteLine(',');
}
else
ps3_DiscSFB.Print(outPath, Path.GetFileName(isoPath));
}
catch (FileNotFoundException)
{
if (!json)
Console.Error.WriteLine($"{isoPath} is not a valid PS3 ISO file\n");
return;
}
// Write PARAM.SFO info
try
{
using LibIRD.DiscUtils.Streams.SparseStream s = reader.OpenFile("\\PS3_GAME\\PARAM.SFO", FileMode.Open, FileAccess.Read);
ParamSFO paramSFO = new(s);
if (json)
{
if (outPath != null)
File.AppendAllText(outPath, "\"PARAM.SFO\": ");
else
Console.Write("\"PARAM.SFO\": ");
paramSFO.PrintJson(outPath);
}
else
paramSFO.Print(outPath, Path.GetFileName(isoPath));
}
catch (FileNotFoundException)
{
if (!json)
Console.Error.WriteLine($"\\PS3_GAME\\PARAM.SFO not found in {isoPath}\n");
}
// End JSON object
if (json)
{
if (single)
{
if (outPath != null)
File.AppendAllText(outPath, "\n}\n");
else
Console.WriteLine("\n}");
}
else
{
if (outPath != null)
File.AppendAllText(outPath, "\n},\n");
else
Console.WriteLine("\n},");
}
}
}
/// <summary>
/// Prints the differences between two IRD files
/// </summary>
/// <param name="irdPath1">First IRD path to compare against</param>
/// <param name="irdPath2">Second IRD path to compare against</param>
/// <param name="outPath">File to write comparison to, null if print to Console</param>
public static void PrintDiff(string irdPath1, string irdPath2, string outPath = null)
{
// Check they are different IRDs
if (Path.GetFullPath(irdPath1) == Path.GetFullPath(irdPath2))
{
Console.Error.WriteLine("Provide two different IRDs for a diff");
return;
}
// Parse each IRD
IRD IRD1 = IRD.Read(irdPath1);
IRD IRD2 = IRD.Read(irdPath2);
// Build a formatted diff
StringBuilder printText = new();
// Print any version difference
if (IRD1.Version != IRD2.Version)
printText.AppendLine($"Version: {IRD1.Version} vs {IRD2.Version}");
// Print any title ID difference
if (IRD1.TitleID != IRD2.TitleID)
printText.AppendLine($"TitleID: {IRD1.TitleID} vs {IRD2.TitleID}");
// Print any title difference
if (IRD1.Title != IRD2.Title)
printText.AppendLine($"Title: \"{IRD1.Title}\" vs \"{IRD2.Title}\"");
// Print any system version difference
if (IRD1.SystemVersion != IRD2.SystemVersion)
printText.AppendLine($"PUP Version: {IRD1.SystemVersion} vs {IRD2.SystemVersion}");
// Print any disc version difference
if (IRD1.DiscVersion != IRD2.DiscVersion)
printText.AppendLine($"Disc Version: {IRD1.DiscVersion} vs {IRD2.DiscVersion}");
// Print any app version difference
if (IRD1.AppVersion != IRD2.AppVersion)
printText.AppendLine($"App Version: {IRD1.AppVersion} vs {IRD2.AppVersion}");
// Un-gzip the headers to compare them
byte[] header1 = Decompress(IRD1.Header);
byte[] header2 = Decompress(IRD2.Header);
// Print the difference in header length, if not 0
if (header1.Length != header2.Length)
printText.AppendLine($"Header Length: {header1.Length} vs {header2.Length}");
// Print number of bytes that the headers differ by, if not 0
int headerDiff;
if (header1.Length < header2.Length)
headerDiff = header2.Length - header1.Length + header1.Where((x, i) => x != header2[i]).Count();
else
headerDiff = header1.Length - header2.Length + header2.Where((x, i) => x != header1[i]).Count();
if (headerDiff != 0)
printText.AppendLine($"Header: Differs by {headerDiff} bytes");
// Un-gzip the footers to compare them
byte[] footer1 = Decompress(IRD1.Footer);
byte[] footer2 = Decompress(IRD2.Footer);
// Print the difference in footer length, if not 0
if (footer1.Length != footer2.Length)
printText.AppendLine($"Footer Length: {footer1.Length} vs {footer2.Length}");
// Print number of bytes that the footers differ by, if not 0
int footerDiff;
if (footer1.Length < footer2.Length)
footerDiff = footer2.Length - footer1.Length + footer1.Where((x, i) => x != footer2[i]).Count();
else
footerDiff = footer1.Length - footer2.Length + footer2.Where((x, i) => x != footer1[i]).Count();
if (footerDiff != 0)
printText.AppendLine($"Footer: Differs by {footerDiff} bytes");
// Print the difference in number of regions, if not 0
if (IRD1.RegionCount != IRD2.RegionCount)
printText.AppendLine($"Region Count: {IRD1.RegionCount} vs {IRD2.RegionCount}");
// Print any differences in region hashes
int regionCount = IRD2.RegionCount < IRD1.RegionCount ? IRD2.RegionCount : IRD1.RegionCount;
if (regionCount > IRD1.RegionHashes.Length)
regionCount = IRD1.RegionHashes.Length;
if (regionCount > IRD2.RegionHashes.Length)
regionCount = IRD2.RegionHashes.Length;
for (int i = 0; i < regionCount; i++)
{
if (!IRD1.RegionHashes[i].SequenceEqual(IRD2.RegionHashes[i]))
printText.AppendLine($"Region {i} Hash: {LibIRD.IRD.ByteArrayToHexString(IRD1.RegionHashes[i])} vs {LibIRD.IRD.ByteArrayToHexString(IRD2.RegionHashes[i])}");
}
// Print the difference in number of files, if not 0
if (IRD1.FileCount != IRD2.FileCount)
printText.AppendLine($"File Count: {IRD1.FileCount} vs {IRD2.FileCount}");
// Print the mismatch file hashes, for each file offset at which they differ
List<long> missingOffsets1 = [];
List<long> missingOffsets2 = [];
for (int i = 0; i < IRD1.FileKeys.Length; i++)
{
int j = Array.FindIndex(IRD2.FileKeys, element => element == IRD1.FileKeys[i]);
if (j == -1)
missingOffsets2.Add(IRD1.FileKeys[i]);
if (j != -1 && !IRD1.FileHashes[i].SequenceEqual(IRD2.FileHashes[j]))
printText.AppendLine($"File Hash at Offset {IRD1.FileKeys[i]}: {LibIRD.IRD.ByteArrayToHexString(IRD1.FileHashes[i])} vs {LibIRD.IRD.ByteArrayToHexString(IRD2.FileHashes[j])}");
}
for (int i = 0; i < IRD2.FileKeys.Length; i++)
{
int j = Array.FindIndex(IRD1.FileKeys, element => element == IRD2.FileKeys[i]);
if (j == -1)
missingOffsets1.Add(IRD2.FileKeys[i]);
}
// Print the file offsets that differ
if (missingOffsets1.Count > 0)
printText.AppendLine($"File Offsets not Present in {irdPath1}: {string.Join(", ", missingOffsets1)}");
if (missingOffsets2.Count > 0)
printText.AppendLine($"File Offsets not Present in {irdPath2}: {string.Join(", ", missingOffsets2)}");
// Print any extra config data difference
if (IRD1.ExtraConfig != IRD2.ExtraConfig)
printText.AppendLine($"Extra Config: {IRD1.ExtraConfig:X4} vs {IRD2.ExtraConfig:X4}");
// Print any attachments data difference
if (IRD1.Attachments != IRD2.Attachments)
printText.AppendLine($"Attachments: {IRD1.Attachments:X4} vs {IRD2.Attachments:X4}");
// Print any unique ID difference
if (IRD1.UID != IRD2.UID)
printText.AppendLine($"Unique ID: {IRD1.UID:X8} vs {IRD2.UID:X8}");
// Print any data 1 key difference
if (!IRD1.Data1Key.SequenceEqual(IRD2.Data1Key))
printText.AppendLine($"Data 1 Key: {LibIRD.IRD.ByteArrayToHexString(IRD1.Data1Key)} vs {LibIRD.IRD.ByteArrayToHexString(IRD2.Data1Key)}");
// Print any data 2 key difference
if (!IRD1.Data2Key.SequenceEqual(IRD2.Data2Key))
printText.AppendLine($"Data 2 Key: {LibIRD.IRD.ByteArrayToHexString(IRD1.Data2Key)} vs {LibIRD.IRD.ByteArrayToHexString(IRD2.Data2Key)}");
// Print any PIC difference
if (!IRD1.PIC.SequenceEqual(IRD2.PIC))
printText.AppendLine($"PIC: {LibIRD.IRD.ByteArrayToHexString(IRD1.PIC)} vs {LibIRD.IRD.ByteArrayToHexString(IRD2.PIC)}");
// Write formatted string to file if output path provided, otherwise to console
if (outPath != null)
File.AppendAllText(outPath, printText.ToString());
else
Console.WriteLine(printText.ToString());
}
/// <summary>
/// Creates an IRD file from an ISO file
/// </summary>
/// <param name="isoPath">Path to an ISO file</param>
/// <param name="irdPath">Path to IRD file to be created (optional)</param>
/// <param name="hexKey">Hex string disc key</param>
/// <param name="keyPath">Disc key file (overridden by hex string if present)</param>
/// <param name="getKeyLog">GetKey log file (overridden by disc key or key file if present)</param>
/// <param name="layerbreak">Layerbreak value of disc</param>
public static string ISO2IRD(string isoPath, string irdPath = null, string hexKey = null, string keyPath = null, string getKeyLog = null, long? layerbreak = null, bool verbose = false)
{
// Check file exists
FileInfo iso;
try
{
iso = new(isoPath);
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message + ", failed to create IRD");
return null;
}
if (!iso.Exists)
{
Console.Error.WriteLine($"{nameof(isoPath)} is not a valid file or directory");
return null;
}
// Determine IRD path if only folder given
if (Directory.Exists(irdPath))
irdPath = Path.Combine(irdPath, Path.GetFileName(Path.ChangeExtension(isoPath, ".ird")));
// Determine IRD path if none given
if (irdPath == string.Empty)
irdPath = Path.GetFileName(Path.ChangeExtension(isoPath, ".ird"));
irdPath ??= Path.ChangeExtension(isoPath, ".ird");
// Create new reproducible redump-style IRD with a given hex key
if (hexKey != null)
{
try
{
// Get disc key from hex string
byte[] discKey = LibIRD.IRD.HexStringToByteArray(hexKey);
if (discKey == null || discKey.Length != 16)
Console.Error.WriteLine($"{hexKey} is not a valid key, detecting key automatically...");
else
{
Console.WriteLine($"Creating {irdPath} with Key: {hexKey}");
IRD ird1 = new ReIRD(isoPath, discKey, layerbreak);
ird1.Write(irdPath);
if (verbose)
ird1.Print();
return irdPath;
}
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message + ", failed to create IRD");
return null;
}
}
// Create new reproducible redump-style IRD with a given key file
if (keyPath != null)
{
try
{
// If key directory was given, append filename and .key
if (Directory.Exists(keyPath))
{
keyPath = Path.Combine(keyPath, Path.ChangeExtension(Path.GetFileName(isoPath), ".key"));
}
// Read key from .key file
byte[] discKey = File.ReadAllBytes(keyPath);
if (discKey == null || discKey.Length != 16)
Console.Error.WriteLine($"{hexKey} is not a valid key, detecting key automatically...");
else
{
Console.WriteLine($"Creating {irdPath} with Key: {LibIRD.IRD.ByteArrayToHexString(discKey)}");
IRD ird1 = new ReIRD(isoPath, discKey, layerbreak);
ird1.Write(irdPath);
if (verbose)
ird1.Print();
return irdPath;
}
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message + ", failed to create IRD");
return null;
}
}
// Create new reproducible redump-style IRD with a given GetKey log
if (getKeyLog != null)
{
try
{
Console.WriteLine($"Creating {irdPath} with key from: {getKeyLog}");
IRD ird1 = new ReIRD(isoPath, getKeyLog);
ird1.Write(irdPath);
if (verbose)
ird1.Print();
return irdPath;
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message + ", failed to create IRD");
return null;
}
}
// No key provided, try search for .key file
string keyfilePath = Path.ChangeExtension(isoPath, ".key");
FileInfo keyFile = new(keyfilePath);
if (keyFile.Exists)
{
// Found .key file, try use it
try
{
// Read key from .key file
byte[] discKey = File.ReadAllBytes(keyfilePath);
if (discKey == null || discKey.Length != 16)
Console.Error.WriteLine($"{hexKey} is not a valid key, detecting key automatically...");
else
{
Console.WriteLine($"Creating {irdPath} with Key: {LibIRD.IRD.ByteArrayToHexString(discKey)}");
IRD ird1 = new ReIRD(isoPath, discKey, layerbreak);
ird1.Write(irdPath);
if (verbose)
ird1.Print();
return irdPath;
}
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message + ", failed to create IRD");
return null;
}
}
// No key provided, try search for .getkey.log file
string logfilePath = Path.ChangeExtension(isoPath, ".getkey.log");
FileInfo logfile = new(logfilePath);
if (logfile.Exists)
{
// Found .getkey.log file, check it is valid
try
{
Console.WriteLine($"Creating {irdPath} with key from: {logfilePath}");
IRD ird1 = new ReIRD(isoPath, logfilePath);
ird1.Write(irdPath);
if (verbose)
ird1.Print();
return irdPath;
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message + ", failed to create IRD");
return null;
}
}
// No key provided, try get key from redump.org
if (verbose)
Console.WriteLine("No key provided... Searching for key on redump.org");
// Compute CRC32 hash
byte[] crc32 = HashTool.GetFileHashArray(isoPath, HashType.CRC32);
string crc32String = LibIRD.IRD.ByteArrayToHexString(crc32).ToLowerInvariant();
// Convert to UInt for use as UID in IRD
Array.Reverse(crc32);
uint crc32UInt = BitConverter.ToUInt32(crc32, 0);
// Search for ISO on redump.org
RedumpClient redump = new();
List<int> ids = redump.CheckSingleSitePage("http://redump.org/discs/system/ps3/quicksearch/" + crc32String).ConfigureAwait(false).GetAwaiter().GetResult();
int id;
if (ids.Count == 0)
{
Console.Error.WriteLine("ISO not found in redump and no valid key provided, cannot create IRD");
return null;
}
else if (ids.Count > 1)
{
// More than one result for the CRC32 hash, compute SHA1 hash instead
string sha1String = HashTool.GetFileHash(isoPath, HashType.SHA1);
// Search redump.org for SHA1 hash
List<int> ids2 = redump.CheckSingleSitePage("http://redump.org/discs/system/ps3/quicksearch/" + sha1String).ConfigureAwait(false).GetAwaiter().GetResult();
if (ids2.Count == 0)
{
Console.Error.WriteLine("ISO not found in redump and no valid key provided, cannot create IRD");
return null;
}
else if (ids2.Count > 1)
{
Console.Error.WriteLine("Cannot automatically get key from redump. Please search redump.org and run again with -k");
return null;
}
id = ids2[0];
}
else
{
// One result found, assume it is the PS3 ISO
id = ids[0];
}
// Download key file from redump.org
byte[] key = redump.DownloadData($"http://redump.org/disc/{id}/key").ConfigureAwait(false).GetAwaiter().GetResult();
if (key == null || key.Length != 16)
{
Console.Error.WriteLine("Invalid key obtained from redump and no valid key provided, cannot create IRD");
return null;
}
// Create IRD with key from redump
Console.WriteLine($"Creating {irdPath} with Key from redump.org: {LibIRD.IRD.ByteArrayToHexString(key)}");
try
{
IRD ird = new ReIRD(isoPath, key, layerbreak, crc32UInt);
ird.Write(irdPath);
if (verbose)
ird.Print();
return irdPath;
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message + ", failed to create IRD");