diff --git a/Source/Core/DiscIO/Blob.h b/Source/Core/DiscIO/Blob.h index 0a7cf096645f..03a8644de5b9 100644 --- a/Source/Core/DiscIO/Blob.h +++ b/Source/Core/DiscIO/Blob.h @@ -59,6 +59,7 @@ class BlobReader virtual u64 GetBlockSize() const = 0; virtual bool HasFastRandomAccessInBlock() const = 0; virtual std::string GetCompressionMethod() const = 0; + virtual std::optional GetCompressionLevel() const = 0; // NOT thread-safe - can't call this from multiple threads. virtual bool Read(u64 offset, u64 size, u8* out_ptr) = 0; diff --git a/Source/Core/DiscIO/CISOBlob.h b/Source/Core/DiscIO/CISOBlob.h index 48acdb0b59a8..e7dacb3647a4 100644 --- a/Source/Core/DiscIO/CISOBlob.h +++ b/Source/Core/DiscIO/CISOBlob.h @@ -46,6 +46,7 @@ class CISOFileReader : public BlobReader u64 GetBlockSize() const override { return m_block_size; } bool HasFastRandomAccessInBlock() const override { return true; } std::string GetCompressionMethod() const override { return {}; } + std::optional GetCompressionLevel() const override { return std::nullopt; } bool Read(u64 offset, u64 nbytes, u8* out_ptr) override; diff --git a/Source/Core/DiscIO/CompressedBlob.h b/Source/Core/DiscIO/CompressedBlob.h index c7e81bf2d6f3..e32cc2569d4f 100644 --- a/Source/Core/DiscIO/CompressedBlob.h +++ b/Source/Core/DiscIO/CompressedBlob.h @@ -58,6 +58,7 @@ class CompressedBlobReader : public SectorReader u64 GetBlockSize() const override { return m_header.block_size; } bool HasFastRandomAccessInBlock() const override { return false; } std::string GetCompressionMethod() const override { return "Deflate"; } + std::optional GetCompressionLevel() const override { return std::nullopt; } u64 GetBlockCompressedSize(u64 block_num) const; bool GetBlock(u64 block_num, u8* out_ptr) override; diff --git a/Source/Core/DiscIO/DirectoryBlob.h b/Source/Core/DiscIO/DirectoryBlob.h index 766ead1a7a01..3e8c4d929238 100644 --- a/Source/Core/DiscIO/DirectoryBlob.h +++ b/Source/Core/DiscIO/DirectoryBlob.h @@ -280,6 +280,7 @@ class DirectoryBlobReader : public BlobReader u64 GetBlockSize() const override { return 0; } bool HasFastRandomAccessInBlock() const override { return true; } std::string GetCompressionMethod() const override { return {}; } + std::optional GetCompressionLevel() const override { return std::nullopt; } private: struct PartitionWithType diff --git a/Source/Core/DiscIO/DriveBlob.h b/Source/Core/DiscIO/DriveBlob.h index 3710fd0f39b7..afe3dc8953d8 100644 --- a/Source/Core/DiscIO/DriveBlob.h +++ b/Source/Core/DiscIO/DriveBlob.h @@ -32,6 +32,7 @@ class DriveReader : public SectorReader u64 GetBlockSize() const override { return ECC_BLOCK_SIZE; } bool HasFastRandomAccessInBlock() const override { return false; } std::string GetCompressionMethod() const override { return {}; } + std::optional GetCompressionLevel() const override { return std::nullopt; } private: DriveReader(const std::string& drive); diff --git a/Source/Core/DiscIO/FileBlob.h b/Source/Core/DiscIO/FileBlob.h index eb944e6d06b7..ea0bd55b8924 100644 --- a/Source/Core/DiscIO/FileBlob.h +++ b/Source/Core/DiscIO/FileBlob.h @@ -27,6 +27,7 @@ class PlainFileReader : public BlobReader u64 GetBlockSize() const override { return 0; } bool HasFastRandomAccessInBlock() const override { return true; } std::string GetCompressionMethod() const override { return {}; } + std::optional GetCompressionLevel() const override { return std::nullopt; } bool Read(u64 offset, u64 nbytes, u8* out_ptr) override; diff --git a/Source/Core/DiscIO/ScrubbedBlob.h b/Source/Core/DiscIO/ScrubbedBlob.h index 2b046bec8618..6530860afe94 100644 --- a/Source/Core/DiscIO/ScrubbedBlob.h +++ b/Source/Core/DiscIO/ScrubbedBlob.h @@ -33,6 +33,10 @@ class ScrubbedBlob : public BlobReader { return m_blob_reader->GetCompressionMethod(); } + std::optional GetCompressionLevel() const override + { + return m_blob_reader->GetCompressionLevel(); + } bool Read(u64 offset, u64 size, u8* out_ptr) override; diff --git a/Source/Core/DiscIO/TGCBlob.h b/Source/Core/DiscIO/TGCBlob.h index caa9bc8f021a..b6d393ea7d53 100644 --- a/Source/Core/DiscIO/TGCBlob.h +++ b/Source/Core/DiscIO/TGCBlob.h @@ -50,6 +50,7 @@ class TGCFileReader final : public BlobReader u64 GetBlockSize() const override { return 0; } bool HasFastRandomAccessInBlock() const override { return true; } std::string GetCompressionMethod() const override { return {}; } + std::optional GetCompressionLevel() const override { return std::nullopt; } bool Read(u64 offset, u64 nbytes, u8* out_ptr) override; diff --git a/Source/Core/DiscIO/VolumeFileBlobReader.cpp b/Source/Core/DiscIO/VolumeFileBlobReader.cpp index 098871b5965c..f6be7e3a34e5 100644 --- a/Source/Core/DiscIO/VolumeFileBlobReader.cpp +++ b/Source/Core/DiscIO/VolumeFileBlobReader.cpp @@ -58,6 +58,11 @@ std::string VolumeFileBlobReader::GetCompressionMethod() const return m_volume.GetBlobReader().GetCompressionMethod(); } +std::optional VolumeFileBlobReader::GetCompressionLevel() const +{ + return m_volume.GetBlobReader().GetCompressionLevel(); +} + bool VolumeFileBlobReader::Read(u64 offset, u64 length, u8* out_ptr) { if (offset + length > m_file_info->GetSize()) diff --git a/Source/Core/DiscIO/VolumeFileBlobReader.h b/Source/Core/DiscIO/VolumeFileBlobReader.h index 0489158577e0..88b0929cbc56 100644 --- a/Source/Core/DiscIO/VolumeFileBlobReader.h +++ b/Source/Core/DiscIO/VolumeFileBlobReader.h @@ -30,6 +30,7 @@ class VolumeFileBlobReader final : public BlobReader u64 GetBlockSize() const override; bool HasFastRandomAccessInBlock() const override; std::string GetCompressionMethod() const override; + std::optional GetCompressionLevel() const override; bool Read(u64 offset, u64 length, u8* out_ptr) override; diff --git a/Source/Core/DiscIO/WIABlob.cpp b/Source/Core/DiscIO/WIABlob.cpp index fa83e73043bf..8ef5a3f6b0b2 100644 --- a/Source/Core/DiscIO/WIABlob.cpp +++ b/Source/Core/DiscIO/WIABlob.cpp @@ -56,7 +56,7 @@ static void PushBack(std::vector* vector, const T& x) PushBack(vector, x_ptr, x_ptr + sizeof(T)); } -std::pair GetAllowedCompressionLevels(WIARVZCompressionType compression_type) +std::pair GetAllowedCompressionLevels(WIARVZCompressionType compression_type, bool gui) { switch (compression_type) { @@ -68,7 +68,10 @@ std::pair GetAllowedCompressionLevels(WIARVZCompressionType compressio // The actual minimum level can be gotten by calling ZSTD_minCLevel(). However, returning that // would make the UI rather weird, because it is a negative number with very large magnitude. // Note: Level 0 is a special number which means "default level" (level 3 as of this writing). - return {1, ZSTD_maxCLevel()}; + if (gui) + return {1, ZSTD_maxCLevel()}; + else + return {ZSTD_minCLevel(), ZSTD_maxCLevel()}; default: return {0, -1}; } @@ -1985,7 +1988,8 @@ WIARVZFileReader::Convert(BlobReader* infile, const VolumeDisc* infile_volu header_2.disc_type = Common::swap32(disc_type); header_2.compression_type = Common::swap32(static_cast(compression_type)); - header_2.compression_level = Common::swap32(static_cast(compression_level)); + header_2.compression_level = + static_cast(Common::swap32(static_cast(compression_level))); header_2.chunk_size = Common::swap32(static_cast(chunk_size)); header_2.number_of_partition_entries = Common::swap32(static_cast(partition_entries.size())); diff --git a/Source/Core/DiscIO/WIABlob.h b/Source/Core/DiscIO/WIABlob.h index d13476762b64..abfa829397c5 100644 --- a/Source/Core/DiscIO/WIABlob.h +++ b/Source/Core/DiscIO/WIABlob.h @@ -34,7 +34,7 @@ enum class WIARVZCompressionType : u32 Zstd = 5, }; -std::pair GetAllowedCompressionLevels(WIARVZCompressionType compression_type); +std::pair GetAllowedCompressionLevels(WIARVZCompressionType compression_type, bool gui); constexpr u32 WIA_MAGIC = 0x01414957; // "WIA\x1" (byteswapped to little endian) constexpr u32 RVZ_MAGIC = 0x015A5652; // "RVZ\x1" (byteswapped to little endian) @@ -56,6 +56,10 @@ class WIARVZFileReader : public BlobReader u64 GetBlockSize() const override { return Common::swap32(m_header_2.chunk_size); } bool HasFastRandomAccessInBlock() const override { return false; } std::string GetCompressionMethod() const override; + std::optional GetCompressionLevel() const override + { + return static_cast(static_cast(Common::swap32(m_header_2.compression_level))); + } bool Read(u64 offset, u64 size, u8* out_ptr) override; bool SupportsReadWiiDecrypted(u64 offset, u64 size, u64 partition_data_offset) const override; @@ -89,7 +93,7 @@ class WIARVZFileReader : public BlobReader { u32 disc_type; u32 compression_type; - u32 compression_level; // Informative only + s32 compression_level; // Informative only u32 chunk_size; std::array disc_header; diff --git a/Source/Core/DiscIO/WbfsBlob.h b/Source/Core/DiscIO/WbfsBlob.h index 5260003592c5..30db2a7d4cfa 100644 --- a/Source/Core/DiscIO/WbfsBlob.h +++ b/Source/Core/DiscIO/WbfsBlob.h @@ -34,6 +34,7 @@ class WbfsFileReader : public BlobReader u64 GetBlockSize() const override { return m_wbfs_sector_size; } bool HasFastRandomAccessInBlock() const override { return true; } std::string GetCompressionMethod() const override { return {}; } + std::optional GetCompressionLevel() const override { return std::nullopt; } bool Read(u64 offset, u64 nbytes, u8* out_ptr) override; diff --git a/Source/Core/DolphinQt/ConvertDialog.cpp b/Source/Core/DolphinQt/ConvertDialog.cpp index afc0f6e87c39..c5bb6d6346cb 100644 --- a/Source/Core/DolphinQt/ConvertDialog.cpp +++ b/Source/Core/DolphinQt/ConvertDialog.cpp @@ -262,7 +262,7 @@ void ConvertDialog::OnCompressionChanged() const auto compression_type = static_cast(m_compression->currentData().toInt()); - const std::pair range = DiscIO::GetAllowedCompressionLevels(compression_type); + const std::pair range = DiscIO::GetAllowedCompressionLevels(compression_type, true); for (int i = range.first; i <= range.second; ++i) { diff --git a/Source/Core/DolphinTool/CMakeLists.txt b/Source/Core/DolphinTool/CMakeLists.txt index e5ccada861f8..cba3b0218ab5 100644 --- a/Source/Core/DolphinTool/CMakeLists.txt +++ b/Source/Core/DolphinTool/CMakeLists.txt @@ -5,6 +5,8 @@ add_executable(dolphin-tool ConvertCommand.h VerifyCommand.cpp VerifyCommand.h + HeaderCommand.cpp + HeaderCommand.h ToolMain.cpp ) diff --git a/Source/Core/DolphinTool/ConvertCommand.cpp b/Source/Core/DolphinTool/ConvertCommand.cpp index 96e645ed7cd6..717f63122a16 100644 --- a/Source/Core/DolphinTool/ConvertCommand.cpp +++ b/Source/Core/DolphinTool/ConvertCommand.cpp @@ -220,7 +220,8 @@ int ConvertCommand::Main(const std::vector& args) return 1; } - const std::pair range = DiscIO::GetAllowedCompressionLevels(compression_o.value()); + const std::pair range = + DiscIO::GetAllowedCompressionLevels(compression_o.value(), false); if (compression_level_o.value() < range.first || compression_level_o.value() > range.second) { std::cerr << "Error: Compression level not in acceptable range" << std::endl; diff --git a/Source/Core/DolphinTool/DolphinTool.vcxproj b/Source/Core/DolphinTool/DolphinTool.vcxproj index 393438764e55..d0831aad8614 100644 --- a/Source/Core/DolphinTool/DolphinTool.vcxproj +++ b/Source/Core/DolphinTool/DolphinTool.vcxproj @@ -38,6 +38,7 @@ + @@ -52,6 +53,7 @@ + diff --git a/Source/Core/DolphinTool/HeaderCommand.cpp b/Source/Core/DolphinTool/HeaderCommand.cpp new file mode 100644 index 000000000000..856299b25d4c --- /dev/null +++ b/Source/Core/DolphinTool/HeaderCommand.cpp @@ -0,0 +1,106 @@ +// Copyright 2022 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "DolphinTool/HeaderCommand.h" +#include "DiscIO/Blob.h" +#include "DiscIO/Volume.h" +#include "DiscIO/VolumeDisc.h" + +#include +#include + +namespace DolphinTool +{ +int HeaderCommand::Main(const std::vector& args) +{ + auto parser = std::make_unique(); + + parser->usage("usage: header [options]..."); + + parser->add_option("-i", "--input") + .type("string") + .action("store") + .help("Path to disc image FILE.") + .metavar("FILE"); + + parser->add_option("-b", "--block_size") + .action("store_true") + .help("Optional. Print the block size of GCZ/WIA/RVZ formats, then exit."); + + parser->add_option("-c", "--compression") + .action("store_true") + .help("Optional. Print the compression method of GCZ/WIA/RVZ formats, then exit."); + + parser->add_option("-l", "--compression_level") + .action("store_true") + .help("Optional. Print the level of compression for WIA/RVZ formats, then exit."); + + const optparse::Values& options = parser->parse_args(args); + + // Validate options + const std::string input_file_path = static_cast(options.get("input")); + if (input_file_path.empty()) + { + std::cerr << "Error: No input set" << std::endl; + return 1; + } + + bool enable_block_size = options.is_set_by_user("block_size"); + bool enable_compression_method = options.is_set_by_user("compression"); + bool enable_compression_level = options.is_set_by_user("compression_level"); + + // Open the blob reader, plus get blob type + std::shared_ptr blob_reader = DiscIO::CreateBlobReader(input_file_path); + if (!blob_reader) + { + std::cerr << "Error: Unable to open disc image" << std::endl; + return 1; + } + const DiscIO::BlobType blob_type = blob_reader->GetBlobType(); + + if (enable_block_size || enable_compression_method || enable_compression_level) + { + if (enable_block_size) + { + const auto block_size = blob_reader->GetBlockSize(); + if (block_size == 0) + std::cout << "N/A" << std::endl; + else + std::cout << block_size << std::endl; + } + if (enable_compression_method) + { + const auto compression_method = blob_reader->GetCompressionMethod(); + if (compression_method == "") + std::cout << "N/A" << std::endl; + else + std::cout << compression_method << std::endl; + } + if (enable_compression_level) + { + const auto compression_level_o = blob_reader->GetCompressionLevel(); + if (compression_level_o == std::nullopt) + std::cout << "N/A" << std::endl; + else + std::cout << compression_level_o.value() << std::endl; + } + } + else + { + if (blob_type == DiscIO::BlobType::GCZ) + { + std::cout << "Block Size: " << blob_reader->GetBlockSize() << std::endl; + std::cout << "Compression Method: " << blob_reader->GetCompressionMethod() << std::endl; + } + if (blob_type == DiscIO::BlobType::WIA || blob_type == DiscIO::BlobType::RVZ) + { + std::cout << "Block Size: " << blob_reader->GetBlockSize() << std::endl; + std::cout << "Compression Method: " << blob_reader->GetCompressionMethod() << std::endl; + std::cout << "Compression Level: " << blob_reader->GetCompressionLevel().value() << std::endl; + } + } + + return 0; +} + +} // namespace DolphinTool diff --git a/Source/Core/DolphinTool/HeaderCommand.h b/Source/Core/DolphinTool/HeaderCommand.h new file mode 100644 index 000000000000..d79af110015e --- /dev/null +++ b/Source/Core/DolphinTool/HeaderCommand.h @@ -0,0 +1,19 @@ +// Copyright 2022 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include + +#include "DolphinTool/Command.h" + +namespace DolphinTool +{ +class HeaderCommand final : public Command +{ +public: + int Main(const std::vector& args) override; +}; + +} // namespace DolphinTool diff --git a/Source/Core/DolphinTool/ToolMain.cpp b/Source/Core/DolphinTool/ToolMain.cpp index ccaf56ea9942..15b27ab95a75 100644 --- a/Source/Core/DolphinTool/ToolMain.cpp +++ b/Source/Core/DolphinTool/ToolMain.cpp @@ -10,12 +10,13 @@ #include "Common/Version.h" #include "DolphinTool/Command.h" #include "DolphinTool/ConvertCommand.h" +#include "DolphinTool/HeaderCommand.h" #include "DolphinTool/VerifyCommand.h" static int PrintUsage(int code) { std::cerr << "usage: dolphin-tool COMMAND -h" << std::endl << std::endl; - std::cerr << "commands supported: [convert, verify]" << std::endl; + std::cerr << "commands supported: [convert, verify, header]" << std::endl; return code; } @@ -42,6 +43,8 @@ int main(int argc, char* argv[]) command = std::make_unique(); else if (command_str == "verify") command = std::make_unique(); + else if (command_str == "header") + command = std::make_unique(); else return PrintUsage(1);