2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:09:55 -06:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
2015-12-07 17:53:42 -07:00
|
|
|
#include <limits>
|
2015-12-06 21:15:51 -07:00
|
|
|
#include <memory>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <string>
|
|
|
|
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/CDUtils.h"
|
2014-02-20 17:47:53 -07:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
|
|
|
|
#include "DiscIO/Blob.h"
|
|
|
|
#include "DiscIO/CISOBlob.h"
|
|
|
|
#include "DiscIO/CompressedBlob.h"
|
|
|
|
#include "DiscIO/DriveBlob.h"
|
|
|
|
#include "DiscIO/FileBlob.h"
|
|
|
|
#include "DiscIO/WbfsBlob.h"
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
|
|
|
|
|
|
|
// Provides caching and split-operation-to-block-operations facilities.
|
|
|
|
// Used for compressed blob reading and direct drive reading.
|
|
|
|
|
|
|
|
void SectorReader::SetSectorSize(int blocksize)
|
|
|
|
{
|
2015-12-07 17:53:42 -07:00
|
|
|
for (auto& cache_entry : m_cache)
|
|
|
|
cache_entry.resize(blocksize);
|
|
|
|
|
|
|
|
m_cache_tags.fill(std::numeric_limits<u64>::max());
|
2008-12-07 22:30:24 -07:00
|
|
|
m_blocksize = blocksize;
|
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
SectorReader::~SectorReader()
|
|
|
|
{
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2015-12-07 17:53:42 -07:00
|
|
|
const std::vector<u8>& SectorReader::GetBlockData(u64 block_num)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2009-02-22 09:48:54 -07:00
|
|
|
// TODO : Expand usage of the cache to more than one block :P
|
2014-09-01 13:48:02 -06:00
|
|
|
if (m_cache_tags[0] == block_num)
|
|
|
|
return m_cache[0];
|
2015-12-07 17:53:42 -07:00
|
|
|
|
|
|
|
GetBlock(block_num, m_cache[0].data());
|
|
|
|
m_cache_tags[0] = block_num;
|
|
|
|
return m_cache[0];
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SectorReader::Read(u64 offset, u64 size, u8* out_ptr)
|
|
|
|
{
|
|
|
|
u64 startingBlock = offset / m_blocksize;
|
|
|
|
u64 remain = size;
|
|
|
|
|
|
|
|
int positionInBlock = (int)(offset % m_blocksize);
|
|
|
|
u64 block = startingBlock;
|
|
|
|
|
|
|
|
while (remain > 0)
|
|
|
|
{
|
2009-02-22 09:48:54 -07:00
|
|
|
// Check if we are ready to do a large block read. > instead of >= so we don't bother if remain is only one block.
|
2009-03-07 01:07:11 -07:00
|
|
|
if (positionInBlock == 0 && remain > (u64)m_blocksize)
|
2009-02-22 09:48:54 -07:00
|
|
|
{
|
|
|
|
u64 num_blocks = remain / m_blocksize;
|
|
|
|
ReadMultipleAlignedBlocks(block, num_blocks, out_ptr);
|
|
|
|
block += num_blocks;
|
|
|
|
out_ptr += num_blocks * m_blocksize;
|
|
|
|
remain -= num_blocks * m_blocksize;
|
2009-02-22 09:59:22 -07:00
|
|
|
continue;
|
2009-02-22 09:48:54 -07:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2015-12-07 17:53:42 -07:00
|
|
|
const std::vector<u8>& data = GetBlockData(block);
|
2015-09-16 13:06:54 -06:00
|
|
|
|
2015-12-07 17:53:42 -07:00
|
|
|
u32 to_copy = m_blocksize - positionInBlock;
|
|
|
|
if (to_copy >= remain)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2009-02-22 09:48:54 -07:00
|
|
|
// Yay, we are done!
|
2015-12-07 17:53:42 -07:00
|
|
|
std::copy(data.begin() + positionInBlock, data.begin() + positionInBlock + remain, out_ptr);
|
2008-12-07 22:30:24 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-07 17:53:42 -07:00
|
|
|
std::copy(data.begin() + positionInBlock, data.begin() + positionInBlock + to_copy, out_ptr);
|
|
|
|
out_ptr += to_copy;
|
|
|
|
remain -= to_copy;
|
2008-12-07 22:30:24 -07:00
|
|
|
positionInBlock = 0;
|
|
|
|
block++;
|
|
|
|
}
|
|
|
|
}
|
2014-03-12 13:33:41 -06:00
|
|
|
|
2008-12-07 22:30:24 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-07 17:53:42 -07:00
|
|
|
bool SectorReader::ReadMultipleAlignedBlocks(u64 block_num, u64 num_blocks, u8* out_ptr)
|
2009-02-22 09:48:54 -07:00
|
|
|
{
|
2009-03-07 01:07:11 -07:00
|
|
|
for (u64 i = 0; i < num_blocks; i++)
|
2009-02-22 09:48:54 -07:00
|
|
|
{
|
2015-12-07 17:53:42 -07:00
|
|
|
const std::vector<u8>& data = GetBlockData(block_num + i);
|
|
|
|
const u64 offset = i * m_blocksize;
|
|
|
|
|
|
|
|
std::copy(data.begin(), data.end(), out_ptr + offset);
|
2009-02-22 09:48:54 -07:00
|
|
|
}
|
2014-03-12 13:33:41 -06:00
|
|
|
|
2009-02-22 09:48:54 -07:00
|
|
|
return true;
|
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2015-12-06 21:15:51 -07:00
|
|
|
std::unique_ptr<IBlobReader> CreateBlobReader(const std::string& filename)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2014-03-12 13:33:41 -06:00
|
|
|
if (cdio_is_cdrom(filename))
|
2009-02-21 16:44:40 -07:00
|
|
|
return DriveReader::Create(filename);
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
if (!File::Exists(filename))
|
2014-03-09 14:14:26 -06:00
|
|
|
return nullptr;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2012-05-03 08:09:01 -06:00
|
|
|
if (IsWbfsBlob(filename))
|
|
|
|
return WbfsFileReader::Create(filename);
|
|
|
|
|
2015-09-26 06:46:53 -06:00
|
|
|
if (IsGCZBlob(filename))
|
2008-12-07 22:30:24 -07:00
|
|
|
return CompressedBlobReader::Create(filename);
|
|
|
|
|
2010-12-29 07:42:20 -07:00
|
|
|
if (IsCISOBlob(filename))
|
|
|
|
return CISOFileReader::Create(filename);
|
|
|
|
|
2009-02-24 12:57:29 -07:00
|
|
|
// Still here? Assume plain file - since we know it exists due to the File::Exists check above.
|
2008-12-07 22:30:24 -07:00
|
|
|
return PlainFileReader::Create(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|