mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-02-01 10:31:31 +02:00
DiscIO: Use std::function for compression callback
This commit is contained in:
parent
a7b9e6857b
commit
ca46028cde
@ -15,6 +15,7 @@
|
||||
// automatically do the right thing.
|
||||
|
||||
#include <array>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
@ -175,17 +176,16 @@ private:
|
||||
// Factory function - examines the path to choose the right type of BlobReader, and returns one.
|
||||
std::unique_ptr<BlobReader> CreateBlobReader(const std::string& filename);
|
||||
|
||||
typedef bool (*CompressCB)(const std::string& text, float percent, void* arg);
|
||||
using CompressCB = std::function<bool(const std::string& text, float percent)>;
|
||||
|
||||
bool ConvertToGCZ(BlobReader* infile, const std::string& infile_path,
|
||||
const std::string& outfile_path, u32 sub_type, int sector_size = 16384,
|
||||
CompressCB callback = nullptr, void* arg = nullptr);
|
||||
const std::string& outfile_path, u32 sub_type, int sector_size,
|
||||
CompressCB callback);
|
||||
bool ConvertToPlain(BlobReader* infile, const std::string& infile_path,
|
||||
const std::string& outfile_path, CompressCB callback = nullptr,
|
||||
void* arg = nullptr);
|
||||
const std::string& outfile_path, CompressCB callback);
|
||||
bool ConvertToWIAOrRVZ(BlobReader* infile, const std::string& infile_path,
|
||||
const std::string& outfile_path, bool rvz,
|
||||
WIARVZCompressionType compression_type, int compression_level,
|
||||
int chunk_size, CompressCB callback = nullptr, void* arg = nullptr);
|
||||
int chunk_size, CompressCB callback);
|
||||
|
||||
} // namespace DiscIO
|
||||
|
@ -238,7 +238,7 @@ static ConversionResult<OutputParameters> Compress(CompressThreadState* state,
|
||||
|
||||
static ConversionResultCode Output(OutputParameters parameters, File::IOFile* outfile,
|
||||
u64* position, std::vector<u64>* offsets, int progress_monitor,
|
||||
u32 num_blocks, CompressCB callback, void* arg)
|
||||
u32 num_blocks, CompressCB callback)
|
||||
{
|
||||
u64 offset = *position;
|
||||
if (!parameters.compressed)
|
||||
@ -261,7 +261,7 @@ static ConversionResultCode Output(OutputParameters parameters, File::IOFile* ou
|
||||
|
||||
const float completion = static_cast<float>(parameters.block_number) / num_blocks;
|
||||
|
||||
if (!callback(text, completion, arg))
|
||||
if (!callback(text, completion))
|
||||
return ConversionResultCode::Canceled;
|
||||
}
|
||||
|
||||
@ -270,7 +270,7 @@ static ConversionResultCode Output(OutputParameters parameters, File::IOFile* ou
|
||||
|
||||
bool ConvertToGCZ(BlobReader* infile, const std::string& infile_path,
|
||||
const std::string& outfile_path, u32 sub_type, int block_size,
|
||||
CompressCB callback, void* arg)
|
||||
CompressCB callback)
|
||||
{
|
||||
ASSERT(infile->IsDataSizeAccurate());
|
||||
|
||||
@ -284,7 +284,7 @@ bool ConvertToGCZ(BlobReader* infile, const std::string& infile_path,
|
||||
return false;
|
||||
}
|
||||
|
||||
callback(Common::GetStringT("Files opened, ready to compress."), 0, arg);
|
||||
callback(Common::GetStringT("Files opened, ready to compress."), 0);
|
||||
|
||||
CompressedBlobHeader header;
|
||||
header.magic_cookie = GCZ_MAGIC;
|
||||
@ -317,7 +317,7 @@ bool ConvertToGCZ(BlobReader* infile, const std::string& infile_path,
|
||||
|
||||
const auto output = [&](OutputParameters parameters) {
|
||||
return Output(std::move(parameters), &outfile, &position, &offsets, progress_monitor,
|
||||
header.num_blocks, callback, arg);
|
||||
header.num_blocks, callback);
|
||||
};
|
||||
|
||||
MultithreadedCompressor<CompressThreadState, CompressParameters, OutputParameters> compressor(
|
||||
@ -364,7 +364,7 @@ bool ConvertToGCZ(BlobReader* infile, const std::string& infile_path,
|
||||
outfile.WriteArray(offsets.data(), header.num_blocks);
|
||||
outfile.WriteArray(hashes.data(), header.num_blocks);
|
||||
|
||||
callback(Common::GetStringT("Done compressing disc image."), 1.0f, arg);
|
||||
callback(Common::GetStringT("Done compressing disc image."), 1.0f);
|
||||
}
|
||||
|
||||
if (result == ConversionResultCode::ReadFailed)
|
||||
|
@ -42,7 +42,7 @@ bool PlainFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
|
||||
}
|
||||
|
||||
bool ConvertToPlain(BlobReader* infile, const std::string& infile_path,
|
||||
const std::string& outfile_path, CompressCB callback, void* arg)
|
||||
const std::string& outfile_path, CompressCB callback)
|
||||
{
|
||||
ASSERT(infile->IsDataSizeAccurate());
|
||||
|
||||
@ -78,7 +78,7 @@ bool ConvertToPlain(BlobReader* infile, const std::string& infile_path,
|
||||
if (i % progress_monitor == 0)
|
||||
{
|
||||
const bool was_cancelled =
|
||||
!callback(Common::GetStringT("Unpacking"), (float)i / (float)num_buffers, arg);
|
||||
!callback(Common::GetStringT("Unpacking"), (float)i / (float)num_buffers);
|
||||
if (was_cancelled)
|
||||
{
|
||||
success = false;
|
||||
|
@ -1686,9 +1686,9 @@ ConversionResultCode WIARVZFileReader<RVZ>::Output(std::vector<OutputParametersE
|
||||
}
|
||||
|
||||
template <bool RVZ>
|
||||
ConversionResultCode
|
||||
WIARVZFileReader<RVZ>::RunCallback(size_t groups_written, u64 bytes_read, u64 bytes_written,
|
||||
u32 total_groups, u64 iso_size, CompressCB callback, void* arg)
|
||||
ConversionResultCode WIARVZFileReader<RVZ>::RunCallback(size_t groups_written, u64 bytes_read,
|
||||
u64 bytes_written, u32 total_groups,
|
||||
u64 iso_size, CompressCB callback)
|
||||
{
|
||||
int ratio = 0;
|
||||
if (bytes_read != 0)
|
||||
@ -1700,8 +1700,8 @@ WIARVZFileReader<RVZ>::RunCallback(size_t groups_written, u64 bytes_read, u64 by
|
||||
|
||||
const float completion = static_cast<float>(bytes_read) / iso_size;
|
||||
|
||||
return callback(text, completion, arg) ? ConversionResultCode::Success :
|
||||
ConversionResultCode::Canceled;
|
||||
return callback(text, completion) ? ConversionResultCode::Success :
|
||||
ConversionResultCode::Canceled;
|
||||
}
|
||||
|
||||
template <bool RVZ>
|
||||
@ -1729,8 +1729,7 @@ template <bool RVZ>
|
||||
ConversionResultCode
|
||||
WIARVZFileReader<RVZ>::Convert(BlobReader* infile, const VolumeDisc* infile_volume,
|
||||
File::IOFile* outfile, WIARVZCompressionType compression_type,
|
||||
int compression_level, int chunk_size, CompressCB callback,
|
||||
void* arg)
|
||||
int compression_level, int chunk_size, CompressCB callback)
|
||||
{
|
||||
ASSERT(infile->IsDataSizeAccurate());
|
||||
ASSERT(chunk_size > 0);
|
||||
@ -1832,7 +1831,7 @@ WIARVZFileReader<RVZ>::Convert(BlobReader* infile, const VolumeDisc* infile_volu
|
||||
return result;
|
||||
|
||||
return RunCallback(parameters.group_index + parameters.entries.size(), parameters.bytes_read,
|
||||
bytes_written, total_groups, iso_size, callback, arg);
|
||||
bytes_written, total_groups, iso_size, callback);
|
||||
};
|
||||
|
||||
MultithreadedCompressor<CompressThreadState, CompressParameters, OutputParameters> mt_compressor(
|
||||
@ -2030,7 +2029,7 @@ WIARVZFileReader<RVZ>::Convert(BlobReader* infile, const VolumeDisc* infile_volu
|
||||
bool ConvertToWIAOrRVZ(BlobReader* infile, const std::string& infile_path,
|
||||
const std::string& outfile_path, bool rvz,
|
||||
WIARVZCompressionType compression_type, int compression_level,
|
||||
int chunk_size, CompressCB callback, void* arg)
|
||||
int chunk_size, CompressCB callback)
|
||||
{
|
||||
File::IOFile outfile(outfile_path, "wb");
|
||||
if (!outfile)
|
||||
@ -2047,7 +2046,7 @@ bool ConvertToWIAOrRVZ(BlobReader* infile, const std::string& infile_path,
|
||||
const auto convert = rvz ? RVZFileReader::Convert : WIAFileReader::Convert;
|
||||
const ConversionResultCode result =
|
||||
convert(infile, infile_volume.get(), &outfile, compression_type, compression_level,
|
||||
chunk_size, callback, arg);
|
||||
chunk_size, callback);
|
||||
|
||||
if (result == ConversionResultCode::ReadFailed)
|
||||
PanicAlertT("Failed to read from the input file \"%s\".", infile_path.c_str());
|
||||
|
@ -64,8 +64,7 @@ public:
|
||||
|
||||
static ConversionResultCode Convert(BlobReader* infile, const VolumeDisc* infile_volume,
|
||||
File::IOFile* outfile, WIARVZCompressionType compression_type,
|
||||
int compression_level, int chunk_size, CompressCB callback,
|
||||
void* arg);
|
||||
int compression_level, int chunk_size, CompressCB callback);
|
||||
|
||||
private:
|
||||
using SHA1 = std::array<u8, 20>;
|
||||
@ -351,8 +350,7 @@ private:
|
||||
std::mutex* reusable_groups_mutex, GroupEntry* group_entry,
|
||||
u64* bytes_written);
|
||||
static ConversionResultCode RunCallback(size_t groups_written, u64 bytes_read, u64 bytes_written,
|
||||
u32 total_groups, u64 iso_size, CompressCB callback,
|
||||
void* arg);
|
||||
u32 total_groups, u64 iso_size, CompressCB callback);
|
||||
|
||||
bool m_valid;
|
||||
WIARVZCompressionType m_compression_type;
|
||||
|
@ -32,17 +32,6 @@
|
||||
#include "UICommon/GameFile.h"
|
||||
#include "UICommon/UICommon.h"
|
||||
|
||||
static bool CompressCB(const std::string& text, float percent, void* ptr)
|
||||
{
|
||||
if (ptr == nullptr)
|
||||
return false;
|
||||
|
||||
auto* progress_dialog = static_cast<ParallelProgressDialog*>(ptr);
|
||||
|
||||
progress_dialog->SetValue(percent * 100);
|
||||
return !progress_dialog->WasCanceled();
|
||||
}
|
||||
|
||||
ConvertDialog::ConvertDialog(QList<std::shared_ptr<const UICommon::GameFile>> files,
|
||||
QWidget* parent)
|
||||
: QDialog(parent), m_files(std::move(files))
|
||||
@ -463,15 +452,19 @@ void ConvertDialog::Convert()
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto callback = [&progress_dialog](const std::string& text, float percent) {
|
||||
progress_dialog.SetValue(percent * 100);
|
||||
return !progress_dialog.WasCanceled();
|
||||
};
|
||||
|
||||
std::future<bool> success;
|
||||
|
||||
switch (format)
|
||||
{
|
||||
case DiscIO::BlobType::PLAIN:
|
||||
success = std::async(std::launch::async, [&] {
|
||||
const bool good =
|
||||
DiscIO::ConvertToPlain(blob_reader.get(), original_path, dst_path.toStdString(),
|
||||
&CompressCB, &progress_dialog);
|
||||
const bool good = DiscIO::ConvertToPlain(blob_reader.get(), original_path,
|
||||
dst_path.toStdString(), callback);
|
||||
progress_dialog.Reset();
|
||||
return good;
|
||||
});
|
||||
@ -479,10 +472,9 @@ void ConvertDialog::Convert()
|
||||
|
||||
case DiscIO::BlobType::GCZ:
|
||||
success = std::async(std::launch::async, [&] {
|
||||
const bool good =
|
||||
DiscIO::ConvertToGCZ(blob_reader.get(), original_path, dst_path.toStdString(),
|
||||
file->GetPlatform() == DiscIO::Platform::WiiDisc ? 1 : 0,
|
||||
block_size, &CompressCB, &progress_dialog);
|
||||
const bool good = DiscIO::ConvertToGCZ(
|
||||
blob_reader.get(), original_path, dst_path.toStdString(),
|
||||
file->GetPlatform() == DiscIO::Platform::WiiDisc ? 1 : 0, block_size, callback);
|
||||
progress_dialog.Reset();
|
||||
return good;
|
||||
});
|
||||
@ -491,10 +483,10 @@ void ConvertDialog::Convert()
|
||||
case DiscIO::BlobType::WIA:
|
||||
case DiscIO::BlobType::RVZ:
|
||||
success = std::async(std::launch::async, [&] {
|
||||
const bool good = DiscIO::ConvertToWIAOrRVZ(
|
||||
blob_reader.get(), original_path, dst_path.toStdString(),
|
||||
format == DiscIO::BlobType::RVZ, compression, compression_level, block_size,
|
||||
&CompressCB, &progress_dialog);
|
||||
const bool good =
|
||||
DiscIO::ConvertToWIAOrRVZ(blob_reader.get(), original_path, dst_path.toStdString(),
|
||||
format == DiscIO::BlobType::RVZ, compression,
|
||||
compression_level, block_size, callback);
|
||||
progress_dialog.Reset();
|
||||
return good;
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user