ExtractCommand.cs 5.4 KB

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