2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2021-12-09 19:22:16 -07:00
|
|
|
#include "DiscIO/FileBlob.h"
|
|
|
|
|
2020-04-04 13:47:28 -06:00
|
|
|
#include <algorithm>
|
2015-12-06 21:15:51 -07:00
|
|
|
#include <memory>
|
2014-03-12 13:33:41 -06:00
|
|
|
#include <string>
|
2016-12-21 04:50:15 -07:00
|
|
|
#include <utility>
|
2020-04-04 13:47:28 -06:00
|
|
|
#include <vector>
|
2016-12-21 04:50:15 -07:00
|
|
|
|
2020-04-04 13:47:28 -06:00
|
|
|
#include "Common/Assert.h"
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2008-09-17 17:25:35 -06:00
|
|
|
namespace DiscIO
|
|
|
|
{
|
2016-12-21 04:50:15 -07:00
|
|
|
PlainFileReader::PlainFileReader(File::IOFile file) : m_file(std::move(file))
|
2008-09-17 17:25:35 -06:00
|
|
|
{
|
2011-03-11 03:21:46 -07:00
|
|
|
m_size = m_file.GetSize();
|
2008-09-18 02:29:10 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2016-12-21 04:50:15 -07:00
|
|
|
std::unique_ptr<PlainFileReader> PlainFileReader::Create(File::IOFile file)
|
2008-09-18 02:29:10 -06:00
|
|
|
{
|
2016-12-21 04:50:15 -07:00
|
|
|
if (file)
|
|
|
|
return std::unique_ptr<PlainFileReader>(new PlainFileReader(std::move(file)));
|
2015-12-06 21:15:51 -07:00
|
|
|
|
|
|
|
return nullptr;
|
2008-09-18 02:29:10 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2008-09-18 02:29:10 -06:00
|
|
|
bool PlainFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
|
|
|
|
{
|
2022-01-28 21:58:31 -07:00
|
|
|
if (m_file.Seek(offset, File::SeekOrigin::Begin) && m_file.ReadBytes(out_ptr, nbytes))
|
2014-12-14 05:16:21 -07:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-28 22:01:03 -07:00
|
|
|
m_file.ClearError();
|
2014-12-14 05:16:21 -07:00
|
|
|
return false;
|
|
|
|
}
|
2008-09-18 02:29:10 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2020-04-10 09:40:07 -06:00
|
|
|
bool ConvertToPlain(BlobReader* infile, const std::string& infile_path,
|
2020-06-26 10:35:09 -06:00
|
|
|
const std::string& outfile_path, CompressCB callback)
|
2020-04-04 13:47:28 -06:00
|
|
|
{
|
2022-08-01 03:53:30 -06:00
|
|
|
ASSERT(infile->GetDataSizeType() == DataSizeType::Accurate);
|
2020-04-04 13:47:28 -06:00
|
|
|
|
|
|
|
File::IOFile outfile(outfile_path, "wb");
|
|
|
|
if (!outfile)
|
|
|
|
{
|
2020-11-10 23:06:33 -07:00
|
|
|
PanicAlertFmtT(
|
2020-11-16 05:28:11 -07:00
|
|
|
"Failed to open the output file \"{0}\".\n"
|
2020-11-10 23:06:33 -07:00
|
|
|
"Check that you have permissions to write the target folder and that the media can "
|
|
|
|
"be written.",
|
|
|
|
outfile_path);
|
2020-04-04 13:47:28 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr size_t DESIRED_BUFFER_SIZE = 0x80000;
|
2020-04-10 09:40:07 -06:00
|
|
|
u64 buffer_size = infile->GetBlockSize();
|
2020-04-04 13:47:28 -06: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 09:40:07 -06:00
|
|
|
const u64 num_buffers = (infile->GetDataSize() + buffer_size - 1) / buffer_size;
|
2020-04-04 13:47:28 -06: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 10:35:09 -06:00
|
|
|
!callback(Common::GetStringT("Unpacking"), (float)i / (float)num_buffers);
|
2020-04-04 13:47:28 -06:00
|
|
|
if (was_cancelled)
|
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const u64 inpos = i * buffer_size;
|
2020-04-10 09:40:07 -06:00
|
|
|
const u64 sz = std::min(buffer_size, infile->GetDataSize() - inpos);
|
|
|
|
if (!infile->Read(inpos, sz, buffer.data()))
|
2020-04-04 13:47:28 -06:00
|
|
|
{
|
2020-11-16 05:28:11 -07:00
|
|
|
PanicAlertFmtT("Failed to read from the input file \"{0}\".", infile_path);
|
2020-04-04 13:47:28 -06:00
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!outfile.WriteBytes(buffer.data(), sz))
|
|
|
|
{
|
2020-11-16 05:28:11 -07:00
|
|
|
PanicAlertFmtT("Failed to write the output file \"{0}\".\n"
|
2020-11-10 23:06:33 -07:00
|
|
|
"Check that you have enough space available on the target drive.",
|
|
|
|
outfile_path);
|
2020-04-04 13:47:28 -06:00
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
// Remove the incomplete output file.
|
|
|
|
outfile.Close();
|
|
|
|
File::Delete(outfile_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2019-05-05 17:48:12 -06:00
|
|
|
} // namespace DiscIO
|