ExtractCommand.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using ArcToolkitCLI.Commands.Options;
  6. using ArcToolkitCLI.Util;
  7. using COM3D2.Toolkit.Arc;
  8. using COM3D2.Toolkit.Arc.Files;
  9. using CommandLine;
  10. using Glob = GlobExpressions.Glob;
  11. namespace ArcToolkitCLI.Commands
  12. {
  13. [Verb("extract", HelpText = "Extract the contents of the given ARC files")]
  14. public class ExtractCommand : ICommand, IInputOptions, IDecryptionOptions, IOutputOptions
  15. {
  16. private Glob extractGlob;
  17. [Option('s', "skip-errors", Required = false,
  18. HelpText = "If specified, skips ARC files that failed to extract.", Default = false)]
  19. public bool SkipErrors { get; set; }
  20. [Option('p', "pattern", Required = false, HelpText = "Pattern to select which files to extract", Default = "*")]
  21. public string ExtractFilePattern { get; set; }
  22. private Glob ExtractGlob => extractGlob ?? (extractGlob = new Glob(ExtractFilePattern));
  23. public int Run()
  24. {
  25. Dictionary<string, byte[]> keys = null;
  26. Directory.CreateDirectory(Output);
  27. foreach (var file in Util.Glob.EnumerateFiles(Input))
  28. {
  29. if (!file.Exists)
  30. return Errors.Error(Errors.ErrorCodes.NotAFile, file.Name);
  31. using var stream = File.OpenRead(file.FullName);
  32. var header = new byte[4];
  33. stream.Read(header, 0, header.Length);
  34. var headerString = Encoding.ASCII.GetString(header);
  35. stream.Position = 0;
  36. WarcArc arc = null;
  37. if (headerString == "warp")
  38. {
  39. if (keys == null)
  40. {
  41. var err = Encryption.GetDecryptionKeys(this, out keys);
  42. if (err != 0)
  43. {
  44. if (SkipErrors)
  45. continue;
  46. return err;
  47. }
  48. arc = new WarpArc(stream, requestedFile =>
  49. {
  50. if (keys.TryGetValue("*", out var key))
  51. return key;
  52. if (keys.TryGetValue(requestedFile, out key))
  53. return key;
  54. if (Directory.Exists(ArcDirectory) &&
  55. File.Exists(Path.Combine(ArcDirectory, requestedFile)))
  56. return Encryption.ReadKeyFromFile(Path.Combine(ArcDirectory, requestedFile));
  57. if (File.Exists(WarcFile))
  58. return Encryption.ReadKeyFromFile(WarcFile);
  59. throw new FileNotFoundException("No key found for the requested ARC", requestedFile);
  60. });
  61. }
  62. }
  63. else if (headerString == "warc")
  64. arc = new WarcArc(stream);
  65. if (arc == null)
  66. {
  67. var err = Errors.Error(Errors.ErrorCodes.UnknownArcType, file.Name);
  68. if (SkipErrors)
  69. continue;
  70. return err;
  71. }
  72. Console.WriteLine($"Extracting {arc.Name}");
  73. var arcDir = Path.Combine(Output, Path.GetFileNameWithoutExtension(arc.Name));
  74. Directory.CreateDirectory(arcDir);
  75. foreach (var arcEntry in arc.Entries)
  76. ExtractArchive(arcEntry, arcDir);
  77. arc.Dispose();
  78. }
  79. return 0;
  80. }
  81. public string ArcDirectory { get; set; }
  82. public string DecryptionKey { get; set; }
  83. public string KeyFile { get; set; }
  84. public string WarcFile { get; set; }
  85. public IEnumerable<string> Input { get; set; }
  86. public string Output { get; set; }
  87. private void ExtractArchive(ArcEntry entry, string path, string internalPath = ".")
  88. {
  89. switch (entry)
  90. {
  91. case ArcFileEntry fe:
  92. var fullName = Path.Combine(path, fe.Name);
  93. var fullInternalName = Path.Combine(internalPath, fe.Name);
  94. if (!ExtractGlob.IsMatch(fullInternalName))
  95. return;
  96. Console.WriteLine(fullName);
  97. Directory.CreateDirectory(Path.GetDirectoryName(fullName));
  98. using (var file = File.Create(fullName))
  99. {
  100. using var data = fe.GetDataStream();
  101. data.CopyTo(file);
  102. }
  103. break;
  104. case ArcDirectoryEntry de:
  105. var subfolderPath = Path.Combine(path, de.Name);
  106. var realSubfolderPath = Path.Combine(internalPath, de.Name);
  107. foreach (var arcEntry in de.Children)
  108. ExtractArchive(arcEntry, subfolderPath, realSubfolderPath);
  109. break;
  110. }
  111. }
  112. }
  113. }