| 1 |
// Copyright 2022 Dolphin Emulator Project |
| 2 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 3 |
|
| 4 |
#include "DolphinTool/HeaderCommand.h" |
| 5 |
|
| 6 |
#include <cstdlib> |
| 7 |
#include <optional> |
| 8 |
#include <string> |
| 9 |
#include <vector> |
| 10 |
|
| 11 |
#include <OptionParser.h> |
| 12 |
#include <fmt/ostream.h> |
| 13 |
#include <picojson.h> |
| 14 |
|
| 15 |
#include "DiscIO/Blob.h" |
| 16 |
#include "DiscIO/Volume.h" |
| 17 |
|
| 18 |
namespace DolphinTool |
| 19 |
{ |
| 20 |
int HeaderCommand(const std::vector<std::string>& args) |
| 21 |
{ |
| 22 |
optparse::OptionParser parser; |
| 23 |
|
| 24 |
parser.usage("usage: header [options]..."); |
| 25 |
|
| 26 |
parser.add_option("-i", "--input") |
| 27 |
.type("string") |
| 28 |
.action("store") |
| 29 |
.help("Path to disc image FILE.") |
| 30 |
.metavar("FILE"); |
| 31 |
|
| 32 |
parser.add_option("-j", "--json") |
| 33 |
.action("store_true") |
| 34 |
.help("Optional. Print the information as JSON, then exit. Overrides other print options."); |
| 35 |
|
| 36 |
parser.add_option("-b", "--block_size") |
| 37 |
.action("store_true") |
| 38 |
.help("Optional. Print the block size of GCZ/WIA/RVZ formats, then exit."); |
| 39 |
|
| 40 |
parser.add_option("-c", "--compression") |
| 41 |
.action("store_true") |
| 42 |
.help("Optional. Print the compression method of GCZ/WIA/RVZ formats, then exit."); |
| 43 |
|
| 44 |
parser.add_option("-l", "--compression_level") |
| 45 |
.action("store_true") |
| 46 |
.help("Optional. Print the level of compression for WIA/RVZ formats, then exit."); |
| 47 |
|
| 48 |
const optparse::Values& options = parser.parse_args(args); |
| 49 |
|
| 50 |
// Validate options |
| 51 |
const std::string& input_file_path = options["input"]; |
| 52 |
if (input_file_path.empty()) |
| 53 |
{ |
| 54 |
fmt::print(std::cerr, "Error: No input set\n"); |
| 55 |
return EXIT_FAILURE; |
| 56 |
} |
| 57 |
|
| 58 |
const bool enable_json = options.is_set_by_user("json"); |
| 59 |
const bool enable_block_size = options.is_set_by_user("block_size"); |
| 60 |
const bool enable_compression_method = options.is_set_by_user("compression"); |
| 61 |
const bool enable_compression_level = options.is_set_by_user("compression_level"); |
| 62 |
|
| 63 |
// Open the blob reader |
| 64 |
const std::unique_ptr<DiscIO::BlobReader> blob_reader = DiscIO::CreateBlobReader(input_file_path); |
| 65 |
if (!blob_reader) |
| 66 |
{ |
| 67 |
fmt::print(std::cerr, "Error: Unable to open disc image\n"); |
| 68 |
return EXIT_FAILURE; |
| 69 |
} |
| 70 |
// Open the volume |
| 71 |
const std::unique_ptr<DiscIO::Volume> volume = DiscIO::CreateVolume(blob_reader->CopyReader()); |
| 72 |
|
| 73 |
if (enable_json) |
| 74 |
{ |
| 75 |
auto json = picojson::object(); |
| 76 |
|
| 77 |
// File data |
| 78 |
if (const u64 block_size = blob_reader->GetBlockSize()) |
| 79 |
json["block_size"] = picojson::value((double)block_size); |
| 80 |
|
| 81 |
const std::string compression_method = blob_reader->GetCompressionMethod(); |
| 82 |
if (compression_method != "") |
| 83 |
json["compression_method"] = picojson::value(compression_method); |
| 84 |
|
| 85 |
if (const std::optional<int> compression_level = blob_reader->GetCompressionLevel()) |
| 86 |
json["compression_level"] = picojson::value((double)compression_level.value()); |
| 87 |
|
| 88 |
// Game data |
| 89 |
if (volume) |
| 90 |
{ |
| 91 |
json["internal_name"] = picojson::value(volume->GetInternalName()); |
| 92 |
|
| 93 |
if (const std::optional<u16> revision = volume->GetRevision()) |
| 94 |
json["revision"] = picojson::value((double)revision.value()); |
| 95 |
|
| 96 |
json["game_id"] = picojson::value(volume->GetGameID()); |
| 97 |
|
| 98 |
if (const std::optional<u64> title_id = volume->GetTitleID()) |
| 99 |
json["title_id"] = picojson::value((double)title_id.value()); |
| 100 |
|
| 101 |
json["region"] = picojson::value(DiscIO::GetName(volume->GetRegion(), false)); |
| 102 |
|
| 103 |
json["country"] = picojson::value(DiscIO::GetName(volume->GetCountry(), false)); |
| 104 |
} |
| 105 |
|
| 106 |
// Print |
| 107 |
std::cout << picojson::value(json) << '\n'; |
| 108 |
} |
| 109 |
else if (enable_block_size || enable_compression_method || enable_compression_level) |
| 110 |
{ |
| 111 |
if (enable_block_size) |
| 112 |
{ |
| 113 |
const auto block_size = blob_reader->GetBlockSize(); |
| 114 |
if (block_size == 0) |
| 115 |
fmt::print(std::cout, "N/A\n"); |
| 116 |
else |
| 117 |
fmt::print(std::cout, "{}\n", block_size); |
| 118 |
} |
| 119 |
if (enable_compression_method) |
| 120 |
{ |
| 121 |
const auto compression_method = blob_reader->GetCompressionMethod(); |
| 122 |
if (compression_method == "") |
| 123 |
fmt::print(std::cout, "N/A\n"); |
| 124 |
else |
| 125 |
fmt::print(std::cout, "{}\n", compression_method); |
| 126 |
} |
| 127 |
if (enable_compression_level) |
| 128 |
{ |
| 129 |
const auto compression_level_o = blob_reader->GetCompressionLevel(); |
| 130 |
if (compression_level_o == std::nullopt) |
| 131 |
fmt::print(std::cout, "N/A\n"); |
| 132 |
else |
| 133 |
fmt::print(std::cout, "{}\n", compression_level_o.value()); |
| 134 |
} |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
// File data |
| 139 |
if (const u64 block_size = blob_reader->GetBlockSize()) |
| 140 |
fmt::print(std::cout, "Block Size: {}\n", block_size); |
| 141 |
|
| 142 |
const std::string compression_method = blob_reader->GetCompressionMethod(); |
| 143 |
if (compression_method != "") |
| 144 |
fmt::print(std::cout, "Compression Method: {}\n", compression_method); |
| 145 |
|
| 146 |
if (const std::optional<int> compression_level = blob_reader->GetCompressionLevel()) |
| 147 |
fmt::print(std::cout, "Compression Level: {}\n", compression_level.value()); |
| 148 |
|
| 149 |
// Game data |
| 150 |
if (volume) |
| 151 |
{ |
| 152 |
fmt::print(std::cout, "Internal Name: {}\n", volume->GetInternalName()); |
| 153 |
|
| 154 |
if (const std::optional<u16> revision = volume->GetRevision()) |
| 155 |
fmt::print(std::cout, "Revision: {}\n", revision.value()); |
| 156 |
|
| 157 |
fmt::print(std::cout, "Game ID: {}\n", volume->GetGameID()); |
| 158 |
|
| 159 |
if (const std::optional<u64> title_id = volume->GetTitleID()) |
| 160 |
fmt::print(std::cout, "Title ID: {:016x}\n", title_id.value()); |
| 161 |
|
| 162 |
fmt::print(std::cout, "Region: {}\n", DiscIO::GetName(volume->GetRegion(), false)); |
| 163 |
|
| 164 |
fmt::print(std::cout, "Country: {}\n", DiscIO::GetName(volume->GetCountry(), false)); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
return EXIT_SUCCESS; |
| 169 |
} |
| 170 |
} // namespace DolphinTool |