| 1 | // Copyright 2021 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinTool/ConvertCommand.h" |
| 5 | |
| 6 | #include <cstdlib> |
| 7 | #include <iostream> |
| 8 | #include <limits> |
| 9 | #include <optional> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include <OptionParser.h> |
| 14 | #include <fmt/ostream.h> |
| 15 | |
| 16 | #include "Common/CommonTypes.h" |
| 17 | #include "DiscIO/Blob.h" |
| 18 | #include "DiscIO/DiscUtils.h" |
| 19 | #include "DiscIO/ScrubbedBlob.h" |
| 20 | #include "DiscIO/Volume.h" |
| 21 | #include "DiscIO/WIABlob.h" |
| 22 | #include "UICommon/UICommon.h" |
| 23 | |
| 24 | namespace DolphinTool |
| 25 | { |
| 26 | static std::optional<DiscIO::WIARVZCompressionType> |
| 27 | ParseCompressionTypeString(const std::string& compression_str) |
| 28 | { |
| 29 | if (compression_str == "none") |
| 30 | return DiscIO::WIARVZCompressionType::None; |
| 31 | else if (compression_str == "purge") |
| 32 | return DiscIO::WIARVZCompressionType::Purge; |
| 33 | else if (compression_str == "bzip2") |
| 34 | return DiscIO::WIARVZCompressionType::Bzip2; |
| 35 | else if (compression_str == "lzma") |
| 36 | return DiscIO::WIARVZCompressionType::LZMA; |
| 37 | else if (compression_str == "lzma2") |
| 38 | return DiscIO::WIARVZCompressionType::LZMA2; |
| 39 | else if (compression_str == "zstd") |
| 40 | return DiscIO::WIARVZCompressionType::Zstd; |
| 41 | return std::nullopt; |
| 42 | } |
| 43 | |
| 44 | static std::optional<DiscIO::BlobType> ParseFormatString(const std::string& format_str) |
| 45 | { |
| 46 | if (format_str == "iso") |
| 47 | return DiscIO::BlobType::PLAIN; |
| 48 | else if (format_str == "gcz") |
| 49 | return DiscIO::BlobType::GCZ; |
| 50 | else if (format_str == "wia") |
| 51 | return DiscIO::BlobType::WIA; |
| 52 | else if (format_str == "rvz") |
| 53 | return DiscIO::BlobType::RVZ; |
| 54 | return std::nullopt; |
| 55 | } |
| 56 | |
| 57 | int ConvertCommand(const std::vector<std::string>& args) |
| 58 | { |
| 59 | optparse::OptionParser parser; |
| 60 | |
| 61 | parser.usage("usage: convert [options]... [FILE]..."); |
| 62 | |
| 63 | parser.add_option("-u", "--user") |
| 64 | .type("string") |
| 65 | .action("store") |
| 66 | .help("User folder path, required for temporary processing files. " |
| 67 | "Will be automatically created if this option is not set.") |
| 68 | .set_default(""); |
| 69 | |
| 70 | parser.add_option("-i", "--input") |
| 71 | .type("string") |
| 72 | .action("store") |
| 73 | .help("Path to disc image FILE.") |
| 74 | .metavar("FILE"); |
| 75 | |
| 76 | parser.add_option("-o", "--output") |
| 77 | .type("string") |
| 78 | .action("store") |
| 79 | .help("Path to the destination FILE.") |
| 80 | .metavar("FILE"); |
| 81 | |
| 82 | parser.add_option("-f", "--format") |
| 83 | .type("string") |
| 84 | .action("store") |
| 85 | .help("Container format to use. Default is RVZ. [%choices]") |
| 86 | .choices({"iso", "gcz", "wia", "rvz"}); |
| 87 | |
| 88 | parser.add_option("-s", "--scrub") |
| 89 | .action("store_true") |
| 90 | .help("Scrub junk data as part of conversion."); |
| 91 | |
| 92 | parser.add_option("-b", "--block_size") |
| 93 | .type("int") |
| 94 | .action("store") |
| 95 | .help("Block size for GCZ/WIA/RVZ formats, as an integer. Suggested value for RVZ: 131072 " |
| 96 | "(128 KiB)"); |
| 97 | |
| 98 | parser.add_option("-c", "--compression") |
| 99 | .type("string") |
| 100 | .action("store") |
| 101 | .help("Compression method to use when converting to WIA/RVZ. Suggested value for RVZ: zstd " |
| 102 | "[%choices]") |
| 103 | .choices({"none", "zstd", "bzip2", "lzma", "lzma2"}); |
| 104 | |
| 105 | parser.add_option("-l", "--compression_level") |
| 106 | .type("int") |
| 107 | .action("store") |
| 108 | .help("Level of compression for the selected method. Ignored if 'none'. Suggested value for " |
| 109 | "zstd: 5"); |
| 110 | |
| 111 | const optparse::Values& options = parser.parse_args(args); |
| 112 | |
| 113 | // Initialize the dolphin user directory, required for temporary processing files |
| 114 | // If this is not set, destructive file operations could occur due to path confusion |
| 115 | UICommon::SetUserDirectory(options["user"]); |
| 116 | UICommon::Init(); |
| 117 | |
| 118 | // Validate options |
| 119 | |
| 120 | // --input |
| 121 | if (!options.is_set("input")) |
| 122 | { |
| 123 | fmt::print(std::cerr, "Error: No input set\n"); |
| 124 | return EXIT_FAILURE; |
| 125 | } |
| 126 | const std::string& input_file_path = options["input"]; |
| 127 | |
| 128 | // --output |
| 129 | if (!options.is_set("output")) |
| 130 | { |
| 131 | fmt::print(std::cerr, "Error: No output set\n"); |
| 132 | return EXIT_FAILURE; |
| 133 | } |
| 134 | const std::string& output_file_path = options["output"]; |
| 135 | |
| 136 | // --format |
| 137 | const std::optional<DiscIO::BlobType> format_o = ParseFormatString(options["format"]); |
| 138 | if (!format_o.has_value()) |
| 139 | { |
| 140 | fmt::print(std::cerr, "Error: No output format set\n"); |
| 141 | return EXIT_FAILURE; |
| 142 | } |
| 143 | const DiscIO::BlobType format = format_o.value(); |
| 144 | |
| 145 | // Open the blob reader |
| 146 | std::unique_ptr<DiscIO::BlobReader> blob_reader = DiscIO::CreateBlobReader(input_file_path); |
| 147 | if (!blob_reader) |
| 148 | { |
| 149 | fmt::print(std::cerr, "Error: The input file could not be opened.\n"); |
| 150 | return EXIT_FAILURE; |
| 151 | } |
| 152 | |
| 153 | // --scrub |
| 154 | const bool scrub = static_cast<bool>(options.get("scrub")); |
| 155 | |
| 156 | // Open the volume |
| 157 | const std::unique_ptr<DiscIO::Volume> volume = DiscIO::CreateDisc(input_file_path); |
| 158 | if (!volume) |
| 159 | { |
| 160 | if (scrub) |
| 161 | { |
| 162 | fmt::print(std::cerr, "Error: Scrubbing is only supported for GC/Wii disc images.\n"); |
| 163 | return EXIT_FAILURE; |
| 164 | } |
| 165 | |
| 166 | fmt::print(std::cerr, |
| 167 | "Warning: The input file is not a GC/Wii disc image. Continuing anyway.\n"); |
| 168 | } |
| 169 | |
| 170 | if (scrub) |
| 171 | { |
| 172 | if (volume->IsDatelDisc()) |
| 173 | { |
| 174 | fmt::print(std::cerr, "Error: Scrubbing a Datel disc is not supported.\n"); |
| 175 | return EXIT_FAILURE; |
| 176 | } |
| 177 | |
| 178 | blob_reader = DiscIO::ScrubbedBlob::Create(input_file_path); |
| 179 | |
| 180 | if (!blob_reader) |
| 181 | { |
| 182 | fmt::print(std::cerr, "Error: Unable to process disc image. Try again without --scrub.\n"); |
| 183 | return EXIT_FAILURE; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if (scrub && format == DiscIO::BlobType::RVZ) |
| 188 | { |
| 189 | fmt::print(std::cerr, "Warning: Scrubbing an RVZ container does not offer significant space " |
| 190 | "advantages. Continuing anyway.\n"); |
| 191 | } |
| 192 | |
| 193 | if (scrub && format == DiscIO::BlobType::PLAIN) |
| 194 | { |
| 195 | fmt::print(std::cerr, "Warning: Scrubbing does not save space when converting to ISO unless " |
| 196 | "using external compression. Continuing anyway.\n"); |
| 197 | } |
| 198 | |
| 199 | if (!scrub && format == DiscIO::BlobType::GCZ && volume && |
| 200 | volume->GetVolumeType() == DiscIO::Platform::WiiDisc && !volume->IsDatelDisc()) |
| 201 | { |
| 202 | fmt::print(std::cerr, "Warning: Converting Wii disc images to GCZ without scrubbing may not " |
| 203 | "offer space advantages over ISO. Continuing anyway.\n"); |
| 204 | } |
| 205 | |
| 206 | if (volume && volume->IsNKit()) |
| 207 | { |
| 208 | fmt::print(std::cerr, |
| 209 | "Warning: Converting an NKit file, output will still be NKit! Continuing anyway.\n"); |
| 210 | } |
| 211 | |
| 212 | // --block_size |
| 213 | std::optional<int> block_size_o; |
| 214 | if (options.is_set("block_size")) |
| 215 | block_size_o = static_cast<int>(options.get("block_size")); |
| 216 | |
| 217 | if (format == DiscIO::BlobType::GCZ || format == DiscIO::BlobType::WIA || |
| 218 | format == DiscIO::BlobType::RVZ) |
| 219 | { |
| 220 | if (!block_size_o.has_value()) |
| 221 | { |
| 222 | fmt::print(std::cerr, "Error: Block size must be set for GCZ/RVZ/WIA\n"); |
| 223 | return EXIT_FAILURE; |
| 224 | } |
| 225 | |
| 226 | if (!DiscIO::IsDiscImageBlockSizeValid(block_size_o.value(), format)) |
| 227 | { |
| 228 | fmt::print(std::cerr, "Error: Block size is not valid for this format\n"); |
| 229 | return EXIT_FAILURE; |
| 230 | } |
| 231 | |
| 232 | if (block_size_o.value() < DiscIO::PREFERRED_MIN_BLOCK_SIZE || |
| 233 | block_size_o.value() > DiscIO::PREFERRED_MAX_BLOCK_SIZE) |
| 234 | { |
| 235 | fmt::print(std::cerr, |
| 236 | "Warning: Block size is not ideal for performance. Continuing anyway.\n"); |
| 237 | } |
| 238 | |
| 239 | if (format == DiscIO::BlobType::GCZ && volume && |
| 240 | !DiscIO::IsGCZBlockSizeLegacyCompatible(block_size_o.value(), volume->GetDataSize())) |
| 241 | { |
| 242 | fmt::print(std::cerr, |
| 243 | "Warning: For GCZs to be compatible with Dolphin < 5.0-11893, the file size " |
| 244 | "must be an integer multiple of the block size and must not be an integer " |
| 245 | "multiple of the block size multiplied by 32. Continuing anyway.\n"); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // --compress, --compress_level |
| 250 | const std::optional<DiscIO::WIARVZCompressionType> compression_o = |
| 251 | ParseCompressionTypeString(options["compression"]); |
| 252 | |
| 253 | std::optional<int> compression_level_o; |
| 254 | if (options.is_set("compression_level")) |
| 255 | compression_level_o = static_cast<int>(options.get("compression_level")); |
| 256 | |
| 257 | if (format == DiscIO::BlobType::WIA || format == DiscIO::BlobType::RVZ) |
| 258 | { |
| 259 | if (!compression_o.has_value()) |
| 260 | { |
| 261 | fmt::print(std::cerr, "Error: Compression method must be set for WIA or RVZ\n"); |
| 262 | return EXIT_FAILURE; |
| 263 | } |
| 264 | |
| 265 | if ((format == DiscIO::BlobType::WIA && |
| 266 | compression_o.value() == DiscIO::WIARVZCompressionType::Zstd) || |
| 267 | (format == DiscIO::BlobType::RVZ && |
| 268 | compression_o.value() == DiscIO::WIARVZCompressionType::Purge)) |
| 269 | { |
| 270 | fmt::print(std::cerr, "Error: Compression type is not supported for the container format\n"); |
| 271 | return EXIT_FAILURE; |
| 272 | } |
| 273 | |
| 274 | if (compression_o.value() == DiscIO::WIARVZCompressionType::None) |
| 275 | { |
| 276 | compression_level_o = 0; |
| 277 | } |
| 278 | else |
| 279 | { |
| 280 | if (!compression_level_o.has_value()) |
| 281 | { |
| 282 | fmt::print(std::cerr, |
| 283 | "Error: Compression level must be set when compression type is not 'none'\n"); |
| 284 | return EXIT_FAILURE; |
| 285 | } |
| 286 | |
| 287 | const std::pair<int, int> range = |
| 288 | DiscIO::GetAllowedCompressionLevels(compression_o.value(), false); |
| 289 | if (compression_level_o.value() < range.first || compression_level_o.value() > range.second) |
| 290 | { |
| 291 | fmt::print(std::cerr, "Error: Compression level not in acceptable range\n"); |
| 292 | return EXIT_FAILURE; |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | // Perform the conversion |
| 298 | const auto NOOP_STATUS_CALLBACK = [](const std::string& text, float percent) { return true; }; |
| 299 | |
| 300 | bool success = false; |
| 301 | |
| 302 | switch (format) |
| 303 | { |
| 304 | case DiscIO::BlobType::PLAIN: |
| 305 | { |
| 306 | success = DiscIO::ConvertToPlain(blob_reader.get(), input_file_path, output_file_path, |
| 307 | NOOP_STATUS_CALLBACK); |
| 308 | break; |
| 309 | } |
| 310 | |
| 311 | case DiscIO::BlobType::GCZ: |
| 312 | { |
| 313 | u32 sub_type = std::numeric_limits<u32>::max(); |
| 314 | if (volume) |
| 315 | { |
| 316 | if (volume->GetVolumeType() == DiscIO::Platform::GameCubeDisc) |
| 317 | sub_type = 0; |
| 318 | else if (volume->GetVolumeType() == DiscIO::Platform::WiiDisc) |
| 319 | sub_type = 1; |
| 320 | } |
| 321 | success = DiscIO::ConvertToGCZ(blob_reader.get(), input_file_path, output_file_path, sub_type, |
| 322 | block_size_o.value(), NOOP_STATUS_CALLBACK); |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | case DiscIO::BlobType::WIA: |
| 327 | case DiscIO::BlobType::RVZ: |
| 328 | { |
| 329 | success = DiscIO::ConvertToWIAOrRVZ(blob_reader.get(), input_file_path, output_file_path, |
| 330 | format == DiscIO::BlobType::RVZ, compression_o.value(), |
| 331 | compression_level_o.value(), block_size_o.value(), |
| 332 | NOOP_STATUS_CALLBACK); |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | default: |
| 337 | { |
| 338 | ASSERT(false); |
| 339 | break; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | if (!success) |
| 344 | { |
| 345 | fmt::print(std::cerr, "Error: Conversion failed\n"); |
| 346 | return EXIT_FAILURE; |
| 347 | } |
| 348 | |
| 349 | return EXIT_SUCCESS; |
| 350 | } |
| 351 | } // namespace DolphinTool |