2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2020-04-04 19:47:28 +00:00
|
|
|
#include <algorithm>
|
2015-12-07 04:15:51 +00:00
|
|
|
#include <memory>
|
2014-03-12 19:33:41 +00:00
|
|
|
#include <string>
|
2016-12-21 11:50:15 +00:00
|
|
|
#include <utility>
|
2020-04-04 19:47:28 +00:00
|
|
|
#include <vector>
|
2016-12-21 11:50:15 +00:00
|
|
|
|
2020-04-04 19:47:28 +00:00
|
|
|
#include "Common/Assert.h"
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
2016-12-21 11:50:15 +00:00
|
|
|
#include "DiscIO/FileBlob.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
2016-12-21 11:50:15 +00:00
|
|
|
PlainFileReader::PlainFileReader(File::IOFile file) : m_file(std::move(file))
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2011-03-11 10:21:46 +00:00
|
|
|
m_size = m_file.GetSize();
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 11:50:15 +00:00
|
|
|
std::unique_ptr<PlainFileReader> PlainFileReader::Create(File::IOFile file)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-12-21 11:50:15 +00:00
|
|
|
if (file)
|
|
|
|
return std::unique_ptr<PlainFileReader>(new PlainFileReader(std::move(file)));
|
2015-12-07 04:15:51 +00:00
|
|
|
|
|
|
|
return nullptr;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PlainFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
|
|
|
|
{
|
2014-12-14 12:16:21 +00:00
|
|
|
if (m_file.Seek(offset, SEEK_SET) && m_file.ReadBytes(out_ptr, nbytes))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_file.Clear();
|
|
|
|
return false;
|
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 15:40:07 +00:00
|
|
|
bool ConvertToPlain(BlobReader* infile, const std::string& infile_path,
|
2020-06-26 16:35:09 +00:00
|
|
|
const std::string& outfile_path, CompressCB callback)
|
2020-04-04 19:47:28 +00:00
|
|
|
{
|
2020-04-10 15:40:07 +00:00
|
|
|
ASSERT(infile->IsDataSizeAccurate());
|
2020-04-04 19:47:28 +00:00
|
|
|
|
|
|
|
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;
|
2020-04-10 15:40:07 +00:00
|
|
|
u64 buffer_size = infile->GetBlockSize();
|
2020-04-04 19:47:28 +00:00
|
|
|
if (buffer_size == 0)
|
|
|
|
{
|
|
|
|
buffer_size = DESIRED_BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (buffer_size < DESIRED_BUFFER_SIZE)
|
|
|
|
buffer_size *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8> buffer(buffer_size);
|
2020-04-10 15:40:07 +00:00
|
|
|
const u64 num_buffers = (infile->GetDataSize() + buffer_size - 1) / buffer_size;
|
2020-04-04 19:47:28 +00:00
|
|
|
int progress_monitor = std::max<int>(1, num_buffers / 100);
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
for (u64 i = 0; i < num_buffers; i++)
|
|
|
|
{
|
|
|
|
if (i % progress_monitor == 0)
|
|
|
|
{
|
|
|
|
const bool was_cancelled =
|
2020-06-26 16:35:09 +00:00
|
|
|
!callback(Common::GetStringT("Unpacking"), (float)i / (float)num_buffers);
|
2020-04-04 19:47:28 +00:00
|
|
|
if (was_cancelled)
|
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const u64 inpos = i * buffer_size;
|
2020-04-10 15:40:07 +00:00
|
|
|
const u64 sz = std::min(buffer_size, infile->GetDataSize() - inpos);
|
|
|
|
if (!infile->Read(inpos, sz, buffer.data()))
|
2020-04-04 19:47:28 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace DiscIO
|