From 42f6913bccdafcb8a32e5d82bd54947062cfd0a9 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sat, 4 Apr 2020 21:47:28 +0200 Subject: Move DiscIO::ConvertToPlain to FileBlob.cpp There is no longer anything GCZ specific about it. --- Source/Core/DiscIO/FileBlob.cpp | 84 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) (limited to 'Source/Core/DiscIO/FileBlob.cpp') diff --git a/Source/Core/DiscIO/FileBlob.cpp b/Source/Core/DiscIO/FileBlob.cpp index 9e0486d969..95beeb1bc3 100644 --- a/Source/Core/DiscIO/FileBlob.cpp +++ b/Source/Core/DiscIO/FileBlob.cpp @@ -2,10 +2,15 @@ // Licensed under GPLv2+ // Refer to the license.txt file included. +#include #include #include #include +#include +#include "Common/Assert.h" +#include "Common/FileUtil.h" +#include "Common/MsgHandler.h" #include "DiscIO/FileBlob.h" namespace DiscIO @@ -36,4 +41,83 @@ bool PlainFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr) } } +bool ConvertToPlain(const std::string& infile_path, const std::string& outfile_path, + CompressCB callback, void* arg) +{ + std::unique_ptr reader = CreateBlobReader(infile_path); + if (!reader) + { + PanicAlertT("Failed to open the input file \"%s\".", infile_path.c_str()); + return false; + } + + ASSERT(reader->IsDataSizeAccurate()); + + File::IOFile outfile(outfile_path, "wb"); + if (!outfile) + { + PanicAlertT("Failed to open the output file \"%s\".\n" + "Check that you have permissions to write the target folder and that the media can " + "be written.", + outfile_path.c_str()); + return false; + } + + constexpr size_t DESIRED_BUFFER_SIZE = 0x80000; + u64 buffer_size = reader->GetBlockSize(); + if (buffer_size == 0) + { + buffer_size = DESIRED_BUFFER_SIZE; + } + else + { + while (buffer_size < DESIRED_BUFFER_SIZE) + buffer_size *= 2; + } + + std::vector buffer(buffer_size); + const u64 num_buffers = (reader->GetDataSize() + buffer_size - 1) / buffer_size; + int progress_monitor = std::max(1, num_buffers / 100); + bool success = true; + + for (u64 i = 0; i < num_buffers; i++) + { + if (i % progress_monitor == 0) + { + const bool was_cancelled = + !callback(Common::GetStringT("Unpacking"), (float)i / (float)num_buffers, arg); + if (was_cancelled) + { + success = false; + break; + } + } + const u64 inpos = i * buffer_size; + const u64 sz = std::min(buffer_size, reader->GetDataSize() - inpos); + if (!reader->Read(inpos, sz, buffer.data())) + { + PanicAlertT("Failed to read from the input file \"%s\".", infile_path.c_str()); + success = false; + break; + } + if (!outfile.WriteBytes(buffer.data(), sz)) + { + PanicAlertT("Failed to write the output file \"%s\".\n" + "Check that you have enough space available on the target drive.", + outfile_path.c_str()); + success = false; + break; + } + } + + if (!success) + { + // Remove the incomplete output file. + outfile.Close(); + File::Delete(outfile_path); + } + + return success; +} + } // namespace DiscIO -- cgit v1.2.3 From 6ffcbcee70c043a0d158d87458f99d20eddbb939 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Fri, 10 Apr 2020 17:40:07 +0200 Subject: DiscIO: Move scrubbing code out of ConvertToGCZ This way, scrubbing can also be performed when converting to other formats. --- Source/Core/DiscIO/FileBlob.cpp | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) (limited to 'Source/Core/DiscIO/FileBlob.cpp') diff --git a/Source/Core/DiscIO/FileBlob.cpp b/Source/Core/DiscIO/FileBlob.cpp index 95beeb1bc3..ca0aa2cfd0 100644 --- a/Source/Core/DiscIO/FileBlob.cpp +++ b/Source/Core/DiscIO/FileBlob.cpp @@ -41,17 +41,10 @@ bool PlainFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr) } } -bool ConvertToPlain(const std::string& infile_path, const std::string& outfile_path, - CompressCB callback, void* arg) +bool ConvertToPlain(BlobReader* infile, const std::string& infile_path, + const std::string& outfile_path, CompressCB callback, void* arg) { - std::unique_ptr reader = CreateBlobReader(infile_path); - if (!reader) - { - PanicAlertT("Failed to open the input file \"%s\".", infile_path.c_str()); - return false; - } - - ASSERT(reader->IsDataSizeAccurate()); + ASSERT(infile->IsDataSizeAccurate()); File::IOFile outfile(outfile_path, "wb"); if (!outfile) @@ -64,7 +57,7 @@ bool ConvertToPlain(const std::string& infile_path, const std::string& outfile_p } constexpr size_t DESIRED_BUFFER_SIZE = 0x80000; - u64 buffer_size = reader->GetBlockSize(); + u64 buffer_size = infile->GetBlockSize(); if (buffer_size == 0) { buffer_size = DESIRED_BUFFER_SIZE; @@ -76,7 +69,7 @@ bool ConvertToPlain(const std::string& infile_path, const std::string& outfile_p } std::vector buffer(buffer_size); - const u64 num_buffers = (reader->GetDataSize() + buffer_size - 1) / buffer_size; + const u64 num_buffers = (infile->GetDataSize() + buffer_size - 1) / buffer_size; int progress_monitor = std::max(1, num_buffers / 100); bool success = true; @@ -93,8 +86,8 @@ bool ConvertToPlain(const std::string& infile_path, const std::string& outfile_p } } const u64 inpos = i * buffer_size; - const u64 sz = std::min(buffer_size, reader->GetDataSize() - inpos); - if (!reader->Read(inpos, sz, buffer.data())) + const u64 sz = std::min(buffer_size, infile->GetDataSize() - inpos); + if (!infile->Read(inpos, sz, buffer.data())) { PanicAlertT("Failed to read from the input file \"%s\".", infile_path.c_str()); success = false; -- cgit v1.2.3