ZoneLoading: Simulate the vanilla edge case that the chunk size is in the end of the loadbuffer and cannot quite fit into it so the space is padded and the chunk size is loaded from the beginning of the buffer

This commit is contained in:
Jan 2019-12-24 17:21:58 +01:00
parent 31192e8372
commit d224eb8ce5
7 changed files with 78 additions and 22 deletions

View File

@ -28,6 +28,7 @@ const int ZoneLoaderFactoryT6::VERSION = 147;
const int ZoneLoaderFactoryT6::STREAM_COUNT = 4;
const int ZoneLoaderFactoryT6::XCHUNK_SIZE = 0x8000;
const int ZoneLoaderFactoryT6::VANILLA_BUFFER_SIZE = 0x80000;
const int ZoneLoaderFactoryT6::OFFSET_BLOCK_BIT_COUNT = 3;
const block_t ZoneLoaderFactoryT6::INSERT_BLOCK = T6::XFILE_BLOCK_VIRTUAL;
@ -259,7 +260,7 @@ class ZoneLoaderFactoryT6::ZoneLoaderFactoryT6Impl
static ISignatureDataProvider* AddXChunkProcessor(bool isEncrypted, ZoneLoader* zoneLoader, std::string& fileName)
{
ISignatureDataProvider* result = nullptr;
auto* xChunkProcessor = new ProcessorXChunks(STREAM_COUNT, XCHUNK_SIZE);
auto* xChunkProcessor = new ProcessorXChunks(STREAM_COUNT, XCHUNK_SIZE, VANILLA_BUFFER_SIZE);
if(isEncrypted)
{

View File

@ -13,6 +13,7 @@ class ZoneLoaderFactoryT6 final : public IZoneLoaderFactory
static const int STREAM_COUNT;
static const int XCHUNK_SIZE;
static const int VANILLA_BUFFER_SIZE;
static const int OFFSET_BLOCK_BIT_COUNT;
static const block_t INSERT_BLOCK;

View File

@ -1,9 +1,12 @@
#pragma once
#include <cstdint>
class ILoadingStream
{
public:
virtual ~ILoadingStream() = default;
virtual size_t Load(void* buffer, size_t length) = 0;
virtual int64_t Pos() = 0;
};

View File

@ -9,3 +9,8 @@ size_t LoadingFileStream::Load(void* buffer, const size_t length)
{
return m_file->Read(buffer, 1, length);
}
int64_t LoadingFileStream::Pos()
{
return m_file->Pos();
}

View File

@ -2,7 +2,7 @@
#include "ILoadingStream.h"
#include "Utils/FileAPI.h"
class LoadingFileStream : public ILoadingStream
class LoadingFileStream final : public ILoadingStream
{
FileAPI::File* m_file;
@ -10,4 +10,5 @@ public:
explicit LoadingFileStream(FileAPI::File* file);
size_t Load(void* buffer, size_t length) override;
int64_t Pos() override;
};

View File

@ -55,7 +55,8 @@ class DBLoadStream
}
public:
DBLoadStream(const int streamIndex, const size_t chunkSize, std::vector<IXChunkProcessor*>& chunkProcessors) : m_processors(chunkProcessors)
DBLoadStream(const int streamIndex, const size_t chunkSize,
std::vector<IXChunkProcessor*>& chunkProcessors) : m_processors(chunkProcessors)
{
m_index = streamIndex;
m_chunk_size = chunkSize;
@ -127,6 +128,7 @@ class ProcessorXChunks::ProcessorXChunksImpl
std::vector<DBLoadStream*> m_streams;
size_t m_chunk_size;
size_t m_vanilla_buffer_size;
std::vector<IXChunkProcessor*> m_chunk_processors;
bool m_initialized_streams;
@ -134,6 +136,7 @@ class ProcessorXChunks::ProcessorXChunksImpl
const uint8_t* m_current_chunk;
size_t m_current_chunk_size;
size_t m_current_chunk_offset;
size_t m_vanilla_buffer_offset;
bool m_eof_reached;
unsigned int m_eof_stream;
@ -146,7 +149,18 @@ class ProcessorXChunks::ProcessorXChunksImpl
return;
xchunk_size_t chunkSize;
const size_t readSize = m_base->m_base_stream->Load(&chunkSize, sizeof(chunkSize));
if (m_vanilla_buffer_size > 0)
{
if (m_vanilla_buffer_offset + sizeof chunkSize > m_vanilla_buffer_size)
{
m_base->m_base_stream->Load(&chunkSize, m_vanilla_buffer_size - m_vanilla_buffer_offset);
m_vanilla_buffer_offset = 0;
}
m_vanilla_buffer_offset = (m_vanilla_buffer_offset + sizeof chunkSize) % m_vanilla_buffer_size;
}
const size_t readSize = m_base->m_base_stream->Load(&chunkSize, sizeof chunkSize);
if (readSize == 0)
{
@ -168,6 +182,11 @@ class ProcessorXChunks::ProcessorXChunksImpl
throw InvalidChunkSizeException(chunkSize);
}
if (m_vanilla_buffer_size > 0)
{
m_vanilla_buffer_offset = (m_vanilla_buffer_offset + loadedChunkSize) % m_vanilla_buffer_size;
}
stream->StartLoading(loadedChunkSize);
}
@ -183,6 +202,7 @@ class ProcessorXChunks::ProcessorXChunksImpl
void InitStreams()
{
m_initialized_streams = true;
m_vanilla_buffer_offset = static_cast<size_t>(m_base->m_base_stream->Pos());
const unsigned int streamCount = m_streams.size();
for (unsigned int streamNum = 0; streamNum < streamCount; streamNum++)
@ -215,17 +235,25 @@ public:
}
m_chunk_size = xChunkSize;
m_vanilla_buffer_size = 0;
m_initialized_streams = false;
m_current_stream = 0;
m_current_chunk = nullptr;
m_current_chunk_size = 0;
m_current_chunk_offset = 0;
m_vanilla_buffer_offset = 0;
m_eof_reached = false;
m_eof_stream = 0;
}
ProcessorXChunksImpl(ProcessorXChunks* base, const int numStreams, const size_t xChunkSize,
const size_t vanillaBufferSize) : ProcessorXChunksImpl(base, numStreams, xChunkSize)
{
m_vanilla_buffer_size = vanillaBufferSize;
}
~ProcessorXChunksImpl()
{
for (auto* stream : m_streams)
@ -286,6 +314,11 @@ public:
return loadedSize;
}
int64_t Pos() const
{
return m_base->m_base_stream->Pos();
}
};
ProcessorXChunks::ProcessorXChunks(const int numStreams, const size_t xChunkSize)
@ -293,6 +326,11 @@ ProcessorXChunks::ProcessorXChunks(const int numStreams, const size_t xChunkSize
m_impl = new ProcessorXChunksImpl(this, numStreams, xChunkSize);
}
ProcessorXChunks::ProcessorXChunks(const int numStreams, const size_t xChunkSize, const size_t vanillaBufferSize)
{
m_impl = new ProcessorXChunksImpl(this, numStreams, xChunkSize, vanillaBufferSize);
}
ProcessorXChunks::~ProcessorXChunks()
{
delete m_impl;
@ -308,3 +346,8 @@ size_t ProcessorXChunks::Load(void* buffer, const size_t length)
{
return m_impl->Load(buffer, length);
}
int64_t ProcessorXChunks::Pos()
{
return m_impl->Pos();
}

View File

@ -9,9 +9,11 @@ class ProcessorXChunks : public StreamProcessor
public:
ProcessorXChunks(int numStreams, size_t xChunkSize);
ProcessorXChunks(int numStreams, size_t xChunkSize, size_t vanillaBufferSize);
~ProcessorXChunks() override;
size_t Load(void* buffer, size_t length) override;
int64_t Pos() override;
void AddChunkProcessor(IXChunkProcessor* chunkProcessor) const;
};