ExtractCommand.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // Copyright 2024 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "DolphinTool/ExtractCommand.h"
  4. #include <filesystem>
  5. #include <future>
  6. #include <iostream>
  7. #include <fmt/format.h>
  8. #include <fmt/ostream.h>
  9. #include <OptionParser.h>
  10. #include "Common/FileUtil.h"
  11. #include "DiscIO/DiscExtractor.h"
  12. #include "DiscIO/DiscUtils.h"
  13. #include "DiscIO/Filesystem.h"
  14. #include "DiscIO/Volume.h"
  15. namespace DolphinTool
  16. {
  17. static void ExtractFile(const DiscIO::Volume& disc_volume, const DiscIO::Partition& partition,
  18. const std::string& path, const std::string& out)
  19. {
  20. const DiscIO::FileSystem* filesystem = disc_volume.GetFileSystem(partition);
  21. if (!filesystem)
  22. return;
  23. ExportFile(disc_volume, partition, filesystem->FindFileInfo(path).get(), out);
  24. }
  25. static std::unique_ptr<DiscIO::FileInfo> GetFileInfo(const DiscIO::Volume& disc_volume,
  26. const DiscIO::Partition& partition,
  27. const std::string& path)
  28. {
  29. const DiscIO::FileSystem* filesystem = disc_volume.GetFileSystem(partition);
  30. if (!filesystem)
  31. return nullptr;
  32. return filesystem->FindFileInfo(path);
  33. }
  34. static bool VolumeSupported(const DiscIO::Volume& disc_volume)
  35. {
  36. switch (disc_volume.GetVolumeType())
  37. {
  38. case DiscIO::Platform::WiiWAD:
  39. fmt::println(std::cerr, "Error: Wii WADs are not supported.");
  40. return false;
  41. case DiscIO::Platform::ELFOrDOL:
  42. fmt::println(std::cerr,
  43. "Error: *.elf or *.dol have no filesystem and are therefore not supported.");
  44. return false;
  45. case DiscIO::Platform::WiiDisc:
  46. case DiscIO::Platform::GameCubeDisc:
  47. return true;
  48. default:
  49. fmt::println(std::cerr, "Error: Unknown volume type.");
  50. return false;
  51. }
  52. }
  53. static void ExtractDirectory(const DiscIO::Volume& disc_volume, const DiscIO::Partition& partition,
  54. const std::string& path, const std::string& out, bool quiet)
  55. {
  56. const DiscIO::FileSystem* filesystem = disc_volume.GetFileSystem(partition);
  57. if (!filesystem)
  58. return;
  59. const std::unique_ptr<DiscIO::FileInfo> info = filesystem->FindFileInfo(path);
  60. u32 size = info->GetTotalChildren();
  61. u32 files = 0;
  62. ExportDirectory(
  63. disc_volume, partition, *info, true, "", out,
  64. [&files, &size, &quiet](const std::string& current) {
  65. files++;
  66. const float progress = static_cast<float>(files) / static_cast<float>(size) * 100;
  67. if (!quiet)
  68. fmt::println(std::cerr, "Extracting: {} | {}%", current, static_cast<int>(progress));
  69. return false;
  70. });
  71. }
  72. static bool ExtractSystemData(const DiscIO::Volume& disc_volume, const DiscIO::Partition& partition,
  73. const std::string& out)
  74. {
  75. return ExportSystemData(disc_volume, partition, out);
  76. }
  77. static void ExtractPartition(const DiscIO::Volume& disc_volume, const DiscIO::Partition& partition,
  78. const std::string& out, bool quiet)
  79. {
  80. ExtractDirectory(disc_volume, partition, "", out + "/files", quiet);
  81. ExtractSystemData(disc_volume, partition, out);
  82. }
  83. static void ListRecursively(const std::string& path, const DiscIO::FileInfo& info,
  84. std::string* result_text)
  85. {
  86. // Don't print the root.
  87. if (!path.empty())
  88. {
  89. const std::string line = fmt::format("{}\n", path);
  90. fmt::print("{}", line);
  91. result_text->append(line);
  92. }
  93. for (const DiscIO::FileInfo& child_info : info)
  94. {
  95. std::string child_path = path + child_info.GetName();
  96. if (child_info.IsDirectory())
  97. child_path += '/';
  98. ListRecursively(child_path, child_info, result_text);
  99. }
  100. }
  101. static bool ListPartition(const DiscIO::Volume& disc_volume, const DiscIO::Partition& partition,
  102. const std::string& partition_name, const std::string& path,
  103. std::string* result_text)
  104. {
  105. const DiscIO::FileSystem* filesystem = disc_volume.GetFileSystem(partition);
  106. const std::unique_ptr<DiscIO::FileInfo> info = filesystem->FindFileInfo(path);
  107. if (!info)
  108. {
  109. if (!partition_name.empty())
  110. {
  111. fmt::println(std::cerr, "Warning: {} does not exist in this partition.", path);
  112. }
  113. return false;
  114. }
  115. // Canonicalize user-provided path by reconstructing it using GetPath().
  116. ListRecursively(info->GetPath(), *info, result_text);
  117. return true;
  118. }
  119. static bool ListVolume(const DiscIO::Volume& disc_volume, const std::string& path,
  120. const std::string& specific_partition_name, bool quiet,
  121. std::string* result_text)
  122. {
  123. if (disc_volume.GetPartitions().empty())
  124. {
  125. return ListPartition(disc_volume, DiscIO::PARTITION_NONE, specific_partition_name, path,
  126. result_text);
  127. }
  128. bool success = false;
  129. for (DiscIO::Partition& p : disc_volume.GetPartitions())
  130. {
  131. const std::optional<u32> partition_type = disc_volume.GetPartitionType(p);
  132. if (!partition_type)
  133. {
  134. fmt::println(std::cerr, "Error: Could not get partition type.");
  135. return false;
  136. }
  137. const std::string partition_name = DiscIO::NameForPartitionType(*partition_type, true);
  138. if (!specific_partition_name.empty() &&
  139. !Common::CaseInsensitiveEquals(partition_name, specific_partition_name))
  140. {
  141. continue;
  142. }
  143. const std::string partition_start =
  144. fmt::format("/// PARTITION: {} <{}> ///\n", partition_name, path);
  145. fmt::print(std::cout, "{}", partition_start);
  146. result_text->append(partition_start);
  147. success |= ListPartition(disc_volume, p, specific_partition_name, path, result_text);
  148. }
  149. return success;
  150. }
  151. static bool HandleExtractPartition(const std::string& output, const std::string& single_file_path,
  152. const std::string& partition_name,
  153. const DiscIO::Volume& disc_volume,
  154. const DiscIO::Partition& partition, bool quiet, bool single)
  155. {
  156. std::string file;
  157. file.append(output).append("/");
  158. file.append(partition_name).append("/");
  159. if (!single)
  160. {
  161. ExtractPartition(disc_volume, partition, file, quiet);
  162. return true;
  163. }
  164. const auto file_info = GetFileInfo(disc_volume, partition, single_file_path);
  165. if (file_info != nullptr)
  166. {
  167. file.append("files/").append(single_file_path);
  168. File::CreateFullPath(file);
  169. if (file_info->IsDirectory())
  170. {
  171. file = PathToString(StringToPath(file).remove_filename());
  172. ExtractDirectory(disc_volume, partition, single_file_path, file, quiet);
  173. }
  174. else
  175. {
  176. ExtractFile(disc_volume, partition, single_file_path, file);
  177. }
  178. return true;
  179. }
  180. return false;
  181. }
  182. int Extract(const std::vector<std::string>& args)
  183. {
  184. optparse::OptionParser parser;
  185. parser.usage("usage: extract [options]...");
  186. parser.add_option("-i", "--input")
  187. .type("string")
  188. .action("store")
  189. .help("Path to disc image FILE.")
  190. .metavar("FILE");
  191. parser.add_option("-o", "--output")
  192. .type("string")
  193. .action("store")
  194. .help("Path to the destination FOLDER.")
  195. .metavar("FOLDER");
  196. parser.add_option("-p", "--partition")
  197. .type("string")
  198. .action("store")
  199. .help("Which specific partition you want to extract.");
  200. parser.add_option("-s", "--single")
  201. .type("string")
  202. .action("store")
  203. .help("Which specific file/directory you want to extract.");
  204. parser.add_option("-l", "--list")
  205. .action("store_true")
  206. .help("List all files in volume/partition. Will print the directory/file specified with "
  207. "--single if defined.");
  208. parser.add_option("-q", "--quiet")
  209. .action("store_true")
  210. .help("Mute all messages except for errors.");
  211. parser.add_option("-g", "--gameonly")
  212. .action("store_true")
  213. .help("Only extracts the DATA partition.");
  214. const optparse::Values& options = parser.parse_args(args);
  215. const bool quiet = options.is_set("quiet");
  216. const bool gameonly = options.is_set("gameonly");
  217. if (!options.is_set("input"))
  218. {
  219. fmt::println(std::cerr, "Error: No input image set");
  220. return EXIT_FAILURE;
  221. }
  222. const std::string& input_file_path = options["input"];
  223. const std::string& output_folder_path = options["output"];
  224. if (!options.is_set("output") && !options.is_set("list"))
  225. {
  226. fmt::println(std::cerr, "Error: No output folder set");
  227. return EXIT_FAILURE;
  228. }
  229. const std::string& single_file_path = options["single"];
  230. std::string specific_partition = options["partition"];
  231. if (options.is_set("output") && !options.is_set("list"))
  232. File::CreateDirs(output_folder_path);
  233. if (gameonly)
  234. specific_partition = std::string("data");
  235. if (const std::unique_ptr<DiscIO::BlobReader> blob_reader =
  236. DiscIO::CreateBlobReader(input_file_path);
  237. !blob_reader)
  238. {
  239. fmt::println(std::cerr, "Error: Unable to open disc image");
  240. return EXIT_FAILURE;
  241. }
  242. const std::unique_ptr<DiscIO::Volume> disc_volume = DiscIO::CreateVolume(input_file_path);
  243. if (!disc_volume)
  244. {
  245. fmt::println(std::cerr, "Error: Unable to open volume");
  246. return EXIT_FAILURE;
  247. }
  248. if (!VolumeSupported(*disc_volume))
  249. return EXIT_FAILURE;
  250. if (options.is_set("list"))
  251. {
  252. std::string list_path = options.is_set("single") ? single_file_path : "/";
  253. if (quiet && !options.is_set("output"))
  254. {
  255. fmt::println(std::cerr, "Error: --quiet is set but no output file provided. Please either "
  256. "remove the --quiet flag or specify --output");
  257. return EXIT_FAILURE;
  258. }
  259. std::string text;
  260. if (!ListVolume(*disc_volume, list_path, specific_partition, quiet, &text))
  261. {
  262. fmt::println(std::cerr, "Error: Found nothing to list");
  263. return EXIT_FAILURE;
  264. }
  265. if (options.is_set("output"))
  266. {
  267. File::CreateFullPath(output_folder_path);
  268. std::ofstream output_file;
  269. output_file.open(output_folder_path);
  270. if (!output_file.is_open())
  271. {
  272. fmt::println(std::cerr, "Error: Unable to open output file");
  273. return EXIT_FAILURE;
  274. }
  275. output_file << text;
  276. }
  277. return EXIT_SUCCESS;
  278. }
  279. bool extracted_one = false;
  280. if (disc_volume->GetPartitions().empty())
  281. {
  282. if (options.is_set("partition"))
  283. {
  284. fmt::println(
  285. std::cerr,
  286. "Warning: --partition has a value even though this image doesn't have any partitions.");
  287. }
  288. extracted_one = HandleExtractPartition(output_folder_path, single_file_path, "", *disc_volume,
  289. DiscIO::PARTITION_NONE, quiet, options.is_set("single"));
  290. }
  291. else
  292. {
  293. for (DiscIO::Partition& p : disc_volume->GetPartitions())
  294. {
  295. if (const std::optional<u32> partition_type = disc_volume->GetPartitionType(p))
  296. {
  297. const std::string partition_name = DiscIO::NameForPartitionType(*partition_type, true);
  298. if (!specific_partition.empty() &&
  299. !Common::CaseInsensitiveEquals(specific_partition, partition_name))
  300. {
  301. continue;
  302. }
  303. extracted_one |=
  304. HandleExtractPartition(output_folder_path, single_file_path, partition_name,
  305. *disc_volume, p, quiet, options.is_set("single"));
  306. }
  307. }
  308. }
  309. if (!extracted_one)
  310. {
  311. if (options.is_set("single"))
  312. fmt::print(std::cerr, "Error: No file/folder was extracted.");
  313. else
  314. fmt::print(std::cerr, "Error: No partitions were extracted.");
  315. if (options.is_set("partition"))
  316. fmt::println(std::cerr, " Maybe you misspelled your specified partition?");
  317. fmt::println(std::cerr, "\n");
  318. return EXIT_FAILURE;
  319. }
  320. if (!quiet)
  321. fmt::println(std::cerr, "Finished Successfully!");
  322. return EXIT_SUCCESS;
  323. }
  324. } // namespace DolphinTool