Skip to content

Commit

Permalink
Merge pull request #10491 from Minty-Meeo/dolphin-tool-more-stuff
Browse files Browse the repository at this point in the history
More dolphin-tool features and options
  • Loading branch information
AdmiralCurtiss committed Mar 20, 2022
2 parents 8368b87 + deba9ce commit 7b0d5f2
Show file tree
Hide file tree
Showing 20 changed files with 167 additions and 8 deletions.
1 change: 1 addition & 0 deletions Source/Core/DiscIO/Blob.h
Expand Up @@ -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<int> GetCompressionLevel() const = 0;

// NOT thread-safe - can't call this from multiple threads.
virtual bool Read(u64 offset, u64 size, u8* out_ptr) = 0;
Expand Down
1 change: 1 addition & 0 deletions Source/Core/DiscIO/CISOBlob.h
Expand Up @@ -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<int> GetCompressionLevel() const override { return std::nullopt; }

bool Read(u64 offset, u64 nbytes, u8* out_ptr) override;

Expand Down
1 change: 1 addition & 0 deletions Source/Core/DiscIO/CompressedBlob.h
Expand Up @@ -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<int> GetCompressionLevel() const override { return std::nullopt; }

u64 GetBlockCompressedSize(u64 block_num) const;
bool GetBlock(u64 block_num, u8* out_ptr) override;
Expand Down
1 change: 1 addition & 0 deletions Source/Core/DiscIO/DirectoryBlob.h
Expand Up @@ -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<int> GetCompressionLevel() const override { return std::nullopt; }

private:
struct PartitionWithType
Expand Down
1 change: 1 addition & 0 deletions Source/Core/DiscIO/DriveBlob.h
Expand Up @@ -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<int> GetCompressionLevel() const override { return std::nullopt; }

private:
DriveReader(const std::string& drive);
Expand Down
1 change: 1 addition & 0 deletions Source/Core/DiscIO/FileBlob.h
Expand Up @@ -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<int> GetCompressionLevel() const override { return std::nullopt; }

bool Read(u64 offset, u64 nbytes, u8* out_ptr) override;

Expand Down
4 changes: 4 additions & 0 deletions Source/Core/DiscIO/ScrubbedBlob.h
Expand Up @@ -33,6 +33,10 @@ class ScrubbedBlob : public BlobReader
{
return m_blob_reader->GetCompressionMethod();
}
std::optional<int> GetCompressionLevel() const override
{
return m_blob_reader->GetCompressionLevel();
}

bool Read(u64 offset, u64 size, u8* out_ptr) override;

Expand Down
1 change: 1 addition & 0 deletions Source/Core/DiscIO/TGCBlob.h
Expand Up @@ -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<int> GetCompressionLevel() const override { return std::nullopt; }

bool Read(u64 offset, u64 nbytes, u8* out_ptr) override;

Expand Down
5 changes: 5 additions & 0 deletions Source/Core/DiscIO/VolumeFileBlobReader.cpp
Expand Up @@ -58,6 +58,11 @@ std::string VolumeFileBlobReader::GetCompressionMethod() const
return m_volume.GetBlobReader().GetCompressionMethod();
}

std::optional<int> 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())
Expand Down
1 change: 1 addition & 0 deletions Source/Core/DiscIO/VolumeFileBlobReader.h
Expand Up @@ -30,6 +30,7 @@ class VolumeFileBlobReader final : public BlobReader
u64 GetBlockSize() const override;
bool HasFastRandomAccessInBlock() const override;
std::string GetCompressionMethod() const override;
std::optional<int> GetCompressionLevel() const override;

bool Read(u64 offset, u64 length, u8* out_ptr) override;

Expand Down
10 changes: 7 additions & 3 deletions Source/Core/DiscIO/WIABlob.cpp
Expand Up @@ -56,7 +56,7 @@ static void PushBack(std::vector<u8>* vector, const T& x)
PushBack(vector, x_ptr, x_ptr + sizeof(T));
}

std::pair<int, int> GetAllowedCompressionLevels(WIARVZCompressionType compression_type)
std::pair<int, int> GetAllowedCompressionLevels(WIARVZCompressionType compression_type, bool gui)
{
switch (compression_type)
{
Expand All @@ -68,7 +68,10 @@ std::pair<int, int> 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};
}
Expand Down Expand Up @@ -1985,7 +1988,8 @@ WIARVZFileReader<RVZ>::Convert(BlobReader* infile, const VolumeDisc* infile_volu

header_2.disc_type = Common::swap32(disc_type);
header_2.compression_type = Common::swap32(static_cast<u32>(compression_type));
header_2.compression_level = Common::swap32(static_cast<u32>(compression_level));
header_2.compression_level =
static_cast<s32>(Common::swap32(static_cast<u32>(compression_level)));
header_2.chunk_size = Common::swap32(static_cast<u32>(chunk_size));

header_2.number_of_partition_entries = Common::swap32(static_cast<u32>(partition_entries.size()));
Expand Down
8 changes: 6 additions & 2 deletions Source/Core/DiscIO/WIABlob.h
Expand Up @@ -34,7 +34,7 @@ enum class WIARVZCompressionType : u32
Zstd = 5,
};

std::pair<int, int> GetAllowedCompressionLevels(WIARVZCompressionType compression_type);
std::pair<int, int> 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)
Expand All @@ -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<int> GetCompressionLevel() const override
{
return static_cast<int>(static_cast<s32>(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;
Expand Down Expand Up @@ -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<u8, 0x80> disc_header;
Expand Down
1 change: 1 addition & 0 deletions Source/Core/DiscIO/WbfsBlob.h
Expand Up @@ -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<int> GetCompressionLevel() const override { return std::nullopt; }

bool Read(u64 offset, u64 nbytes, u8* out_ptr) override;

Expand Down
2 changes: 1 addition & 1 deletion Source/Core/DolphinQt/ConvertDialog.cpp
Expand Up @@ -262,7 +262,7 @@ void ConvertDialog::OnCompressionChanged()
const auto compression_type =
static_cast<DiscIO::WIARVZCompressionType>(m_compression->currentData().toInt());

const std::pair<int, int> range = DiscIO::GetAllowedCompressionLevels(compression_type);
const std::pair<int, int> range = DiscIO::GetAllowedCompressionLevels(compression_type, true);

for (int i = range.first; i <= range.second; ++i)
{
Expand Down
2 changes: 2 additions & 0 deletions Source/Core/DolphinTool/CMakeLists.txt
Expand Up @@ -5,6 +5,8 @@ add_executable(dolphin-tool
ConvertCommand.h
VerifyCommand.cpp
VerifyCommand.h
HeaderCommand.cpp
HeaderCommand.h
ToolMain.cpp
)

Expand Down
3 changes: 2 additions & 1 deletion Source/Core/DolphinTool/ConvertCommand.cpp
Expand Up @@ -220,7 +220,8 @@ int ConvertCommand::Main(const std::vector<std::string>& args)
return 1;
}

const std::pair<int, int> range = DiscIO::GetAllowedCompressionLevels(compression_o.value());
const std::pair<int, int> 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;
Expand Down
2 changes: 2 additions & 0 deletions Source/Core/DolphinTool/DolphinTool.vcxproj
Expand Up @@ -38,6 +38,7 @@
<ItemGroup>
<ClCompile Include="ConvertCommand.cpp" />
<ClCompile Include="VerifyCommand.cpp" />
<ClCompile Include="HeaderCommand.cpp" />
<ClCompile Include="ToolHeadlessPlatform.cpp" />
<ClCompile Include="ToolMain.cpp" />
</ItemGroup>
Expand All @@ -52,6 +53,7 @@
<ClInclude Include="Command.h" />
<ClInclude Include="ConvertCommand.h" />
<ClInclude Include="VerifyCommand.h" />
<ClInclude Include="HeaderCommand.h" />
</ItemGroup>
<ItemGroup>
<Manifest Include="DolphinTool.exe.manifest" />
Expand Down
106 changes: 106 additions & 0 deletions 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 <OptionParser.h>
#include <optional>

namespace DolphinTool
{
int HeaderCommand::Main(const std::vector<std::string>& args)
{
auto parser = std::make_unique<optparse::OptionParser>();

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<const char*>(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<DiscIO::BlobReader> 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
19 changes: 19 additions & 0 deletions 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 <string>
#include <vector>

#include "DolphinTool/Command.h"

namespace DolphinTool
{
class HeaderCommand final : public Command
{
public:
int Main(const std::vector<std::string>& args) override;
};

} // namespace DolphinTool
5 changes: 4 additions & 1 deletion Source/Core/DolphinTool/ToolMain.cpp
Expand Up @@ -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;
}
Expand All @@ -42,6 +43,8 @@ int main(int argc, char* argv[])
command = std::make_unique<DolphinTool::ConvertCommand>();
else if (command_str == "verify")
command = std::make_unique<DolphinTool::VerifyCommand>();
else if (command_str == "header")
command = std::make_unique<DolphinTool::HeaderCommand>();
else
return PrintUsage(1);

Expand Down

0 comments on commit 7b0d5f2

Please sign in to comment.