2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2021-12-10 02:22:16 +00:00
|
|
|
#include "DiscIO/FileBlob.h"
|
|
|
|
|
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"
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
namespace DiscIO
|
|
|
|
{
|
2016-12-21 11:50:15 +00:00
|
|
|
PlainFileReader::PlainFileReader(File::IOFile file) : m_file(std::move(file))
|
2008-09-17 23:25:35 +00:00
|
|
|
{
|
2011-03-11 10:21:46 +00:00
|
|
|
m_size = m_file.GetSize();
|
2008-09-18 08:29:10 +00:00
|
|
|
}
|
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-09-18 08:29:10 +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-09-18 08:29:10 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2023-07-07 12:31:26 +00:00
|
|
|
std::unique_ptr<BlobReader> PlainFileReader::CopyReader() const
|
|
|
|
{
|
|
|
|
return Create(m_file.Duplicate("rb"));
|
|
|
|
}
|
|
|
|
|
2008-09-18 08:29:10 +00:00
|
|
|
bool PlainFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
|
|
|
|
{
|
2022-01-29 04:58:31 +00:00
|
|
|
if (m_file.Seek(offset, File::SeekOrigin::Begin) && m_file.ReadBytes(out_ptr, nbytes))
|
2014-12-14 12:16:21 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-29 05:01:03 +00:00
|
|
|
m_file.ClearError();
|
2014-12-14 12:16:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-09-18 08:29:10 +00:00
|
|
|
}
|
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
|
|
|
{
|
2022-08-01 09:53:30 +00:00
|
|
|
ASSERT(infile->GetDataSizeType() == DataSizeType::Accurate);
|
2020-04-04 19:47:28 +00:00
|
|
|
|
|
|
|
File::IOFile outfile(outfile_path, "wb");
|
|
|
|
if (!outfile)
|
|
|
|
{
|
2020-11-11 06:06:33 +00:00
|
|
|
PanicAlertFmtT(
|
2020-11-16 12:28:11 +00:00
|
|
|
"Failed to open the output file \"{0}\".\n"
|
2020-11-11 06:06:33 +00:00
|
|
|
"Check that you have permissions to write the target folder and that the media can "
|
|
|
|
"be written.",
|
|
|
|
outfile_path);
|
2020-04-04 19:47:28 +00:00
|
|
|
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
|
|
|
{
|
2020-11-16 12:28:11 +00:00
|
|
|
PanicAlertFmtT("Failed to read from the input file \"{0}\".", infile_path);
|
2020-04-04 19:47:28 +00:00
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!outfile.WriteBytes(buffer.data(), sz))
|
|
|
|
{
|
2020-11-16 12:28:11 +00:00
|
|
|
PanicAlertFmtT("Failed to write the output file \"{0}\".\n"
|
2020-11-11 06:06:33 +00:00
|
|
|
"Check that you have enough space available on the target drive.",
|
|
|
|
outfile_path);
|
2020-04-04 19:47:28 +00:00
|
|
|
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
|