2022-02-24 10:51:52 +00:00
|
|
|
// Copyright 2022 Dolphin Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
#include "DolphinTool/HeaderCommand.h"
|
2023-06-14 23:33:11 +00:00
|
|
|
|
2023-06-15 04:27:14 +00:00
|
|
|
#include <cstdlib>
|
2023-06-14 23:33:11 +00:00
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <OptionParser.h>
|
2023-06-16 23:59:30 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
#include <fmt/ostream.h>
|
2023-10-11 08:58:42 +00:00
|
|
|
#include <picojson.h>
|
2023-06-14 23:33:11 +00:00
|
|
|
|
2022-02-24 10:51:52 +00:00
|
|
|
#include "DiscIO/Blob.h"
|
|
|
|
#include "DiscIO/Volume.h"
|
|
|
|
#include "DiscIO/VolumeDisc.h"
|
|
|
|
|
|
|
|
namespace DolphinTool
|
|
|
|
{
|
2023-06-14 23:33:11 +00:00
|
|
|
int HeaderCommand(const std::vector<std::string>& args)
|
2022-02-24 10:51:52 +00:00
|
|
|
{
|
2023-06-14 20:14:03 +00:00
|
|
|
optparse::OptionParser parser;
|
2022-02-24 10:51:52 +00:00
|
|
|
|
2023-06-14 20:14:03 +00:00
|
|
|
parser.usage("usage: header [options]...");
|
2022-02-24 10:51:52 +00:00
|
|
|
|
2023-06-14 20:14:03 +00:00
|
|
|
parser.add_option("-i", "--input")
|
2022-02-24 10:51:52 +00:00
|
|
|
.type("string")
|
|
|
|
.action("store")
|
|
|
|
.help("Path to disc image FILE.")
|
|
|
|
.metavar("FILE");
|
|
|
|
|
2023-10-11 08:58:42 +00:00
|
|
|
parser.add_option("-j", "--json")
|
|
|
|
.action("store_true")
|
|
|
|
.help("Optional. Print the information as JSON, then exit. Overrides other print options.");
|
|
|
|
|
2023-06-14 20:14:03 +00:00
|
|
|
parser.add_option("-b", "--block_size")
|
2022-02-24 10:51:52 +00:00
|
|
|
.action("store_true")
|
|
|
|
.help("Optional. Print the block size of GCZ/WIA/RVZ formats, then exit.");
|
|
|
|
|
2023-06-14 20:14:03 +00:00
|
|
|
parser.add_option("-c", "--compression")
|
2022-02-24 10:51:52 +00:00
|
|
|
.action("store_true")
|
|
|
|
.help("Optional. Print the compression method of GCZ/WIA/RVZ formats, then exit.");
|
|
|
|
|
2023-06-14 20:14:03 +00:00
|
|
|
parser.add_option("-l", "--compression_level")
|
2022-02-24 10:51:52 +00:00
|
|
|
.action("store_true")
|
|
|
|
.help("Optional. Print the level of compression for WIA/RVZ formats, then exit.");
|
|
|
|
|
2023-06-14 20:14:03 +00:00
|
|
|
const optparse::Values& options = parser.parse_args(args);
|
2022-02-24 10:51:52 +00:00
|
|
|
|
|
|
|
// Validate options
|
2023-06-17 00:33:38 +00:00
|
|
|
const std::string& input_file_path = options["input"];
|
2022-02-24 10:51:52 +00:00
|
|
|
if (input_file_path.empty())
|
|
|
|
{
|
2023-06-16 23:59:30 +00:00
|
|
|
fmt::print(std::cerr, "Error: No input set\n");
|
2023-06-15 04:27:14 +00:00
|
|
|
return EXIT_FAILURE;
|
2022-02-24 10:51:52 +00:00
|
|
|
}
|
|
|
|
|
2023-10-11 08:58:42 +00:00
|
|
|
const bool enable_json = options.is_set_by_user("json");
|
2023-06-14 20:14:03 +00:00
|
|
|
const bool enable_block_size = options.is_set_by_user("block_size");
|
|
|
|
const bool enable_compression_method = options.is_set_by_user("compression");
|
|
|
|
const bool enable_compression_level = options.is_set_by_user("compression_level");
|
2022-02-24 10:51:52 +00:00
|
|
|
|
2023-10-11 08:58:42 +00:00
|
|
|
// Open the blob reader
|
2023-06-14 20:14:03 +00:00
|
|
|
const std::unique_ptr<DiscIO::BlobReader> blob_reader = DiscIO::CreateBlobReader(input_file_path);
|
2022-02-24 10:51:52 +00:00
|
|
|
if (!blob_reader)
|
|
|
|
{
|
2023-06-16 23:59:30 +00:00
|
|
|
fmt::print(std::cerr, "Error: Unable to open disc image\n");
|
2023-06-15 04:27:14 +00:00
|
|
|
return EXIT_FAILURE;
|
2022-02-24 10:51:52 +00:00
|
|
|
}
|
2023-10-11 08:58:42 +00:00
|
|
|
// Open the volume
|
|
|
|
const std::unique_ptr<DiscIO::Volume> volume = DiscIO::CreateVolume(blob_reader->CopyReader());
|
|
|
|
|
|
|
|
if (enable_json)
|
|
|
|
{
|
|
|
|
auto json = picojson::object();
|
|
|
|
|
|
|
|
// File data
|
|
|
|
if (const u64 block_size = blob_reader->GetBlockSize())
|
|
|
|
json["block_size"] = picojson::value((double)block_size);
|
|
|
|
|
|
|
|
const std::string compression_method = blob_reader->GetCompressionMethod();
|
|
|
|
if (compression_method != "")
|
|
|
|
json["compression_method"] = picojson::value(compression_method);
|
|
|
|
|
|
|
|
if (const std::optional<int> compression_level = blob_reader->GetCompressionLevel())
|
|
|
|
json["compression_level"] = picojson::value((double)compression_level.value());
|
|
|
|
|
|
|
|
// Game data
|
|
|
|
if (volume)
|
|
|
|
{
|
|
|
|
json["internal_name"] = picojson::value(volume->GetInternalName());
|
|
|
|
|
|
|
|
if (const std::optional<u64> revision = volume->GetRevision())
|
|
|
|
json["revision"] = picojson::value((double)revision.value());
|
|
|
|
|
|
|
|
json["game_id"] = picojson::value(volume->GetGameID());
|
|
|
|
|
|
|
|
if (const std::optional<u64> title_id = volume->GetTitleID())
|
|
|
|
json["title_id"] = picojson::value((double)title_id.value());
|
|
|
|
|
|
|
|
json["region"] = picojson::value(DiscIO::GetName(volume->GetRegion(), false));
|
2022-02-24 10:51:52 +00:00
|
|
|
|
2023-10-11 08:58:42 +00:00
|
|
|
json["country"] = picojson::value(DiscIO::GetName(volume->GetCountry(), false));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print
|
|
|
|
std::cout << picojson::value(json) << '\n';
|
|
|
|
}
|
|
|
|
else if (enable_block_size || enable_compression_method || enable_compression_level)
|
2022-02-24 10:51:52 +00:00
|
|
|
{
|
|
|
|
if (enable_block_size)
|
|
|
|
{
|
|
|
|
const auto block_size = blob_reader->GetBlockSize();
|
|
|
|
if (block_size == 0)
|
2023-06-16 23:59:30 +00:00
|
|
|
fmt::print(std::cout, "N/A\n");
|
2022-02-24 10:51:52 +00:00
|
|
|
else
|
2023-06-16 23:59:30 +00:00
|
|
|
fmt::print(std::cout, "{}\n", block_size);
|
2022-02-24 10:51:52 +00:00
|
|
|
}
|
|
|
|
if (enable_compression_method)
|
|
|
|
{
|
|
|
|
const auto compression_method = blob_reader->GetCompressionMethod();
|
|
|
|
if (compression_method == "")
|
2023-06-16 23:59:30 +00:00
|
|
|
fmt::print(std::cout, "N/A\n");
|
2022-02-24 10:51:52 +00:00
|
|
|
else
|
2023-06-16 23:59:30 +00:00
|
|
|
fmt::print(std::cout, "{}\n", compression_method);
|
2022-02-24 10:51:52 +00:00
|
|
|
}
|
|
|
|
if (enable_compression_level)
|
|
|
|
{
|
|
|
|
const auto compression_level_o = blob_reader->GetCompressionLevel();
|
|
|
|
if (compression_level_o == std::nullopt)
|
2023-06-16 23:59:30 +00:00
|
|
|
fmt::print(std::cout, "N/A\n");
|
2022-02-24 10:51:52 +00:00
|
|
|
else
|
2023-06-16 23:59:30 +00:00
|
|
|
fmt::print(std::cout, "{}\n", compression_level_o.value());
|
2022-02-24 10:51:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-11 08:58:42 +00:00
|
|
|
// File data
|
|
|
|
if (const u64 block_size = blob_reader->GetBlockSize())
|
|
|
|
fmt::print(std::cout, "Block Size: {}\n", block_size);
|
|
|
|
|
|
|
|
const std::string compression_method = blob_reader->GetCompressionMethod();
|
|
|
|
if (compression_method != "")
|
|
|
|
fmt::print(std::cout, "Compression Method: {}\n", compression_method);
|
|
|
|
|
|
|
|
if (const std::optional<int> compression_level = blob_reader->GetCompressionLevel())
|
|
|
|
fmt::print(std::cout, "Compression Level: {}\n", compression_level.value());
|
|
|
|
|
|
|
|
// Game data
|
|
|
|
if (volume)
|
2022-02-24 10:51:52 +00:00
|
|
|
{
|
2023-10-11 08:58:42 +00:00
|
|
|
fmt::print(std::cout, "Internal Name: {}\n", volume->GetInternalName());
|
|
|
|
|
|
|
|
if (const std::optional<u64> revision = volume->GetRevision())
|
|
|
|
fmt::print(std::cout, "Revision: {}\n", revision.value());
|
|
|
|
|
|
|
|
fmt::print(std::cout, "Game ID: {}\n", volume->GetGameID());
|
|
|
|
|
|
|
|
if (const std::optional<u64> title_id = volume->GetTitleID())
|
|
|
|
fmt::print(std::cout, "Title ID: {}\n", title_id.value());
|
|
|
|
|
|
|
|
fmt::print(std::cout, "Region: {}\n", DiscIO::GetName(volume->GetRegion(), false));
|
|
|
|
|
|
|
|
fmt::print(std::cout, "Country: {}\n", DiscIO::GetName(volume->GetCountry(), false));
|
2022-02-24 10:51:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-15 04:27:14 +00:00
|
|
|
return EXIT_SUCCESS;
|
2022-02-24 10:51:52 +00:00
|
|
|
}
|
|
|
|
} // namespace DolphinTool
|