InfoCommand.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using ArcToolkitCLI.Commands.Options;
  7. using ArcToolkitCLI.Util;
  8. using COM3D2.Toolkit.Arc;
  9. using CommandLine;
  10. namespace ArcToolkitCLI.Commands
  11. {
  12. [Verb("info", HelpText = "Display information about the given ARC files")]
  13. public class InfoCommand : IInputOptions, ICommand
  14. {
  15. [Option("only-key",
  16. HelpText = "If the archive is a WARC file, output only the decryption key as a base64 string",
  17. Required = false)]
  18. public bool OnlyKey { get; set; }
  19. public int Run()
  20. {
  21. var first = true;
  22. foreach (var file in Glob.EnumerateFiles(Input))
  23. {
  24. if (!file.Exists)
  25. return Errors.Error(Errors.ErrorCodes.NotAFile, file.Name);
  26. if (!first && !OnlyKey)
  27. Console.WriteLine();
  28. first = false;
  29. using (var stream = File.OpenRead(file.FullName))
  30. {
  31. var header = new byte[4];
  32. stream.Read(header, 0, header.Length);
  33. var headerString = Encoding.ASCII.GetString(header);
  34. stream.Position = 0;
  35. if (headerString == "warc")
  36. {
  37. var key = new byte[2048];
  38. stream.Read(key, 0, key.Length);
  39. var keyBase64 = Convert.ToBase64String(key);
  40. if (OnlyKey)
  41. {
  42. Console.WriteLine($"{Path.GetFileName(file.FullName)}:{keyBase64}");
  43. continue;
  44. }
  45. stream.Position = 0;
  46. using (var warc = new WarcArc(stream))
  47. {
  48. Console.WriteLine($"File name: {file.Name}");
  49. Console.WriteLine("ARC type: WARC");
  50. Console.WriteLine($"ARC Name: {warc.Name}");
  51. Console.WriteLine($"File count: {warc.Entries.Count()}");
  52. Console.WriteLine($"Decryption key: {keyBase64}");
  53. }
  54. }
  55. else if (headerString == "warp")
  56. {
  57. if (OnlyKey)
  58. continue;
  59. Console.WriteLine($"File name: {file.Name}");
  60. Console.WriteLine("ARC type: WARP");
  61. using (var br = new BinaryReader(stream))
  62. {
  63. Console.WriteLine(
  64. $"Needs a decryption key from the following ARC: {WarpArc.GetKeyWarpName(br)}");
  65. }
  66. }
  67. else
  68. {
  69. return Errors.Error(Errors.ErrorCodes.UnknownArcType, file.Name);
  70. }
  71. }
  72. }
  73. return 0;
  74. }
  75. public IEnumerable<string> Input { get; set; }
  76. }
  77. }