2008-09-17 23:25:35 +00:00
|
|
|
// Copyright (C) 2003-2008 Dolphin Project.
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
#include "Common.h"
|
|
|
|
#include "FileUtil.h"
|
|
|
|
#include "Blob.h"
|
|
|
|
#include "CompressedBlob.h"
|
|
|
|
#include "FileBlob.h"
|
|
|
|
#include "DriveBlob.h"
|
|
|
|
#include "MappedFile.h"
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
namespace DiscIO
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
// Provides caching and split-operation-to-block-operations facilities.
|
|
|
|
// Used for compressed blob reading and direct drive reading.
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
void SectorReader::SetSectorSize(int blocksize)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-09-17 23:25:35 +00:00
|
|
|
for (int i = 0; i < CACHE_SIZE; i++)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-09-17 23:25:35 +00:00
|
|
|
cache[i] = new u8[blocksize];
|
|
|
|
cache_tags[i] = (u64)(s64) - 1;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
2008-09-17 23:25:35 +00:00
|
|
|
m_blocksize = blocksize;
|
|
|
|
}
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
SectorReader::~SectorReader() {
|
|
|
|
for (int i = 0; i < CACHE_SIZE; i++)
|
|
|
|
delete[] cache[i];
|
|
|
|
}
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
const u8 *SectorReader::GetBlockData(u64 block_num)
|
|
|
|
{
|
|
|
|
if (cache_tags[0] == block_num)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-09-17 23:25:35 +00:00
|
|
|
return cache[0];
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
2008-09-17 23:25:35 +00:00
|
|
|
else
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-09-17 23:25:35 +00:00
|
|
|
GetBlock(block_num, cache[0]);
|
|
|
|
cache_tags[0] = block_num;
|
|
|
|
return cache[0];
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
2008-09-17 23:25:35 +00:00
|
|
|
}
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
bool SectorReader::Read(u64 offset, u64 size, u8* out_ptr)
|
|
|
|
{
|
|
|
|
u64 startingBlock = offset / m_blocksize;
|
|
|
|
u64 remain = size;
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
int positionInBlock = (int)(offset % m_blocksize);
|
|
|
|
u64 block = startingBlock;
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
while (remain > 0)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-09-17 23:25:35 +00:00
|
|
|
const u8* data = GetBlockData(block);
|
|
|
|
if (!data)
|
|
|
|
return false;
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
u32 toCopy = m_blocksize - positionInBlock;
|
|
|
|
if (toCopy >= remain)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-09-17 23:25:35 +00:00
|
|
|
// yay, we are done!
|
|
|
|
memcpy(out_ptr, data + positionInBlock, (size_t)remain);
|
|
|
|
return true;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-17 23:25:35 +00:00
|
|
|
memcpy(out_ptr, data + positionInBlock, toCopy);
|
|
|
|
out_ptr += toCopy;
|
|
|
|
remain -= toCopy;
|
|
|
|
positionInBlock = 0;
|
|
|
|
block++;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-17 23:25:35 +00:00
|
|
|
return true;
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
IBlobReader* CreateBlobReader(const char* filename)
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-09-17 23:25:35 +00:00
|
|
|
//if (strlen(filename) < 4 && filename[1] == ':') // Drive, for sure.
|
|
|
|
// return DriveReader::Create(filename);
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
if (!File::Exists(filename))
|
|
|
|
return 0;
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
if (IsCompressedBlob(filename))
|
|
|
|
return CompressedBlobReader::Create(filename);
|
2008-07-12 17:40:22 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
// Still here? Assume plain file.
|
|
|
|
return PlainFileReader::Create(filename);
|
2008-07-12 17:40:22 +00:00
|
|
|
}
|
2008-08-26 12:40:09 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
} // namespace
|