Skip to content
Permalink
Browse files
Merge pull request #10252 from ssdsnake/feature_dolphintool
DolphinTool: Add CLI tool subsystem + commands for verifying and converting RVZ/ISO/WIA/GCZ
  • Loading branch information
JosJuice committed Dec 9, 2021
2 parents 85e5070 + 1aa8a4d commit d5d21c6
Show file tree
Hide file tree
Showing 19 changed files with 968 additions and 26 deletions.
@@ -32,6 +32,10 @@ if(NOT WIN32 AND NOT APPLE AND NOT HAIKU)
option(ENABLE_EGL "Enables EGL OpenGL Interface" ON)
endif()

if(NOT ANDROID)
option(ENABLE_CLI_TOOL "Enable dolphin-tool, a CLI-based utility for functions such as managing disc images" ON)
endif()

option(USE_SHARED_ENET "Use shared libenet if found rather than Dolphin's soon-to-compatibly-diverge version" OFF)
option(USE_UPNP "Enables UPnP port mapping support" ON)
option(ENABLE_NOGUI "Enable NoGUI frontend" ON)
@@ -11,6 +11,10 @@ if(ENABLE_NOGUI)
add_subdirectory(DolphinNoGUI)
endif()

if(ENABLE_CLI_TOOL)
add_subdirectory(DolphinTool)
endif()

if(ENABLE_QT)
add_subdirectory(DolphinQt)
endif()
@@ -23,7 +27,6 @@ if (APPLE)
add_subdirectory(MacUpdater)
endif()


if (WIN32)
add_subdirectory(WinUpdater)
endif()
@@ -12,6 +12,8 @@
#include <fmt/format.h>

#include "Common/CommonTypes.h"
#include "Common/MathUtil.h"
#include "DiscIO/Blob.h"
#include "DiscIO/Filesystem.h"
#include "DiscIO/Volume.h"

@@ -198,4 +200,49 @@ u64 GetBiggestReferencedOffset(const Volume& volume, const std::vector<Partition
return biggest_offset;
}

bool IsGCZBlockSizeLegacyCompatible(int block_size, u64 file_size)
{
// In order for versions of Dolphin prior to 5.0-11893 to be able to convert a GCZ file
// to ISO without messing up the final part of the file in some way, the file size
// must be an integer multiple of the block size (fixed in 3aa463c) and must not be
// an integer multiple of the block size multiplied by 32 (fixed in 26b21e3).
return file_size % block_size == 0 && file_size % (block_size * 32) != 0;
}

bool IsDiscImageBlockSizeValid(int block_size, DiscIO::BlobType format)
{
switch (format)
{
case DiscIO::BlobType::GCZ:
// Block size "must" be a power of 2
if (!MathUtil::IsPow2(block_size))
return false;

break;
case DiscIO::BlobType::WIA:
// Block size must not be less than the minimum, and must be a multiple of it
if (block_size < WIA_MIN_BLOCK_SIZE || block_size % WIA_MIN_BLOCK_SIZE != 0)
return false;

break;
case DiscIO::BlobType::RVZ:
// Block size must not be smaller than the minimum
// Block sizes smaller than the large block size threshold must be a power of 2
// Block sizes larger than that threshold must be a multiple of the threshold
if (block_size < RVZ_MIN_BLOCK_SIZE ||
(block_size < RVZ_BIG_BLOCK_SIZE_LCM && !MathUtil::IsPow2(block_size)) ||
(block_size > RVZ_BIG_BLOCK_SIZE_LCM && block_size % RVZ_BIG_BLOCK_SIZE_LCM != 0))
{
return false;
}

break;
default:
ASSERT(false);
break;
}

return true;
}

} // namespace DiscIO
@@ -8,6 +8,7 @@
#include <vector>

#include "Common/CommonTypes.h"
#include "DiscIO/Blob.h"

namespace DiscIO
{
@@ -49,6 +50,31 @@ constexpr u32 WII_NONPARTITION_DISCHEADER_SIZE = 0x100;
constexpr u32 WII_REGION_DATA_ADDRESS = 0x4E000;
constexpr u32 WII_REGION_DATA_SIZE = 0x20;

// 128 KiB (0x20000) is the default block size for GCZ/RVZ images
constexpr int GCZ_RVZ_PREFERRED_BLOCK_SIZE = 0x20000;

// 32 KiB (0x8000) was picked because DVD timings are emulated as if we can't read less than
// an entire ECC block at once. Therefore, little reason to choose a smaller block size.
constexpr int PREFERRED_MIN_BLOCK_SIZE = 0x8000;

// 2 MiB (0x200000) was picked because it is the smallest block size supported by WIA.
// For performance reasons, blocks shouldn't be too large.
constexpr int PREFERRED_MAX_BLOCK_SIZE = 0x200000;

// If we didn't find a good GCZ block size, pick the block size which was hardcoded
// in legacy versions. That way, at least we're not worse than older versions.
// 16 KiB (0x4000) for supporting GCZs in versions of Dolphin prior to 5.0-11893
constexpr int GCZ_FALLBACK_BLOCK_SIZE = 0x4000;

// 2 MiB (0x200000) is the smallest block size supported by WIA.
constexpr int WIA_MIN_BLOCK_SIZE = 0x200000;

// 32 KiB (0x8000) is the smallest block size supported by RVZ.
constexpr int RVZ_MIN_BLOCK_SIZE = 0x8000;

// 2 MiB (0x200000): for RVZ, block sizes larger than 2 MiB must be an integer multiple of 2 MiB.
constexpr int RVZ_BIG_BLOCK_SIZE_LCM = 0x200000;

std::string NameForPartitionType(u32 partition_type, bool include_prefix);

std::optional<u64> GetApploaderSize(const Volume& volume, const Partition& partition);
@@ -59,4 +85,7 @@ std::optional<u64> GetFSTSize(const Volume& volume, const Partition& partition);

u64 GetBiggestReferencedOffset(const Volume& volume);
u64 GetBiggestReferencedOffset(const Volume& volume, const std::vector<Partition>& partitions);

bool IsGCZBlockSizeLegacyCompatible(int block_size, u64 file_size);
bool IsDiscImageBlockSizeValid(int block_size, DiscIO::BlobType format);
} // namespace DiscIO
@@ -23,6 +23,7 @@
#include "Common/Assert.h"
#include "Common/Logging/Log.h"
#include "DiscIO/Blob.h"
#include "DiscIO/DiscUtils.h"
#include "DiscIO/ScrubbedBlob.h"
#include "DiscIO/WIABlob.h"
#include "DolphinQt/QtUtils/DolphinFileDialog.h"
@@ -118,10 +119,9 @@ void ConvertDialog::AddToBlockSizeComboBox(int size)
{
m_block_size->addItem(QString::fromStdString(UICommon::FormatSize(size, 0)), size);

// Select 128 KiB by default, or if it is not available, the size closest to it.
// Select the default, or if it is not available, the size closest to it.
// This code assumes that sizes get added to the combo box in increasing order.
constexpr int DEFAULT_SIZE = 0x20000;
if (size <= DEFAULT_SIZE)
if (size <= DiscIO::GCZ_RVZ_PREFERRED_BLOCK_SIZE)
m_block_size->setCurrentIndex(m_block_size->count() - 1);
}

@@ -138,14 +138,6 @@ void ConvertDialog::AddToCompressionLevelComboBox(int level)

void ConvertDialog::OnFormatChanged()
{
// Because DVD timings are emulated as if we can't read less than an entire ECC block at once
// (32 KiB - 0x8000), there is little reason to use a block size smaller than that.
constexpr int MIN_BLOCK_SIZE = 0x8000;

// For performance reasons, blocks shouldn't be too large.
// 2 MiB (0x200000) was picked because it is the smallest block size supported by WIA.
constexpr int MAX_BLOCK_SIZE = 0x200000;

const DiscIO::BlobType format = static_cast<DiscIO::BlobType>(m_format->currentData().toInt());

m_block_size->clear();
@@ -156,21 +148,17 @@ void ConvertDialog::OnFormatChanged()
{
case DiscIO::BlobType::GCZ:
{
// In order for versions of Dolphin prior to 5.0-11893 to be able to convert a GCZ file
// to ISO without messing up the final part of the file in some way, the file size
// must be an integer multiple of the block size (fixed in 3aa463c) and must not be
// an integer multiple of the block size multiplied by 32 (fixed in 26b21e3).

// To support legacy versions of dolphin, we have to check the GCZ block size
// See DiscIO::IsGCZBlockSizeLegacyCompatible() for details
const auto block_size_ok = [this](int block_size) {
return std::all_of(m_files.begin(), m_files.end(), [block_size](const auto& file) {
constexpr u64 BLOCKS_PER_BUFFER = 32;
const u64 file_size = file->GetVolumeSize();
return file_size % block_size == 0 && file_size % (block_size * BLOCKS_PER_BUFFER) != 0;
return DiscIO::IsGCZBlockSizeLegacyCompatible(block_size, file->GetVolumeSize());
});
};

// Add all block sizes in the normal range that do not cause problems
for (int block_size = MIN_BLOCK_SIZE; block_size <= MAX_BLOCK_SIZE; block_size *= 2)
for (int block_size = DiscIO::PREFERRED_MIN_BLOCK_SIZE;
block_size <= DiscIO::PREFERRED_MAX_BLOCK_SIZE; block_size *= 2)
{
if (block_size_ok(block_size))
AddToBlockSizeComboBox(block_size);
@@ -180,13 +168,12 @@ void ConvertDialog::OnFormatChanged()
// in older versions of Dolphin. That way, at least we're not worse than older versions.
if (m_block_size->count() == 0)
{
constexpr int FALLBACK_BLOCK_SIZE = 0x4000;
if (!block_size_ok(FALLBACK_BLOCK_SIZE))
if (!block_size_ok(DiscIO::GCZ_FALLBACK_BLOCK_SIZE))
{
ERROR_LOG_FMT(MASTER_LOG, "Failed to find a block size which does not cause problems "
"when decompressing using an old version of Dolphin");
}
AddToBlockSizeComboBox(FALLBACK_BLOCK_SIZE);
AddToBlockSizeComboBox(DiscIO::GCZ_FALLBACK_BLOCK_SIZE);
}

break;
@@ -195,13 +182,14 @@ void ConvertDialog::OnFormatChanged()
m_block_size->setEnabled(true);

// This is the smallest block size supported by WIA. For performance, larger sizes are avoided.
AddToBlockSizeComboBox(0x200000);
AddToBlockSizeComboBox(DiscIO::WIA_MIN_BLOCK_SIZE);

break;
case DiscIO::BlobType::RVZ:
m_block_size->setEnabled(true);

for (int block_size = MIN_BLOCK_SIZE; block_size <= MAX_BLOCK_SIZE; block_size *= 2)
for (int block_size = DiscIO::PREFERRED_MIN_BLOCK_SIZE;
block_size <= DiscIO::PREFERRED_MAX_BLOCK_SIZE; block_size *= 2)
AddToBlockSizeComboBox(block_size);

break;
@@ -257,6 +245,7 @@ void ConvertDialog::OnFormatChanged()
m_block_size->setEnabled(m_block_size->count() > 1);
m_compression->setEnabled(m_compression->count() > 1);

// Block scrubbing of RVZ containers and Datel discs
const bool scrubbing_allowed =
format != DiscIO::BlobType::RVZ &&
std::none_of(m_files.begin(), m_files.end(), std::mem_fn(&UICommon::GameFile::IsDatelDisc));
@@ -0,0 +1,22 @@
add_executable(dolphin-tool
ToolHeadlessPlatform.cpp
Command.h
ConvertCommand.cpp
ConvertCommand.h
VerifyCommand.cpp
VerifyCommand.h
ToolMain.cpp
)

set_target_properties(dolphin-tool PROPERTIES OUTPUT_NAME dolphin-tool)

target_link_libraries(dolphin-tool
PRIVATE
core
discio
videocommon
cpp-optparse
)

set(CPACK_PACKAGE_EXECUTABLES ${CPACK_PACKAGE_EXECUTABLES} dolphin-tool)
install(TARGETS dolphin-tool RUNTIME DESTINATION ${bindir})
@@ -0,0 +1,19 @@
// Copyright 2021 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <string>
#include <vector>

namespace DolphinTool
{
class Command
{
public:
Command() {}
virtual ~Command() {}
virtual int Main(const std::vector<std::string>& args) = 0;
};

} // namespace DolphinTool

0 comments on commit d5d21c6

Please sign in to comment.