From 72f7f0f329c5453345c06fbf7e2e97f73e7dccf9 Mon Sep 17 00:00:00 2001 From: Ted John Date: Fri, 1 Jul 2016 21:22:18 +0100 Subject: [PATCH] refactor object factory loading --- src/core/MemoryStream.cpp | 2 +- src/core/MemoryStream.h | 2 +- src/object/ObjectFactory.cpp | 100 ++++++++++++++++++++--------------- 3 files changed, 58 insertions(+), 46 deletions(-) diff --git a/src/core/MemoryStream.cpp b/src/core/MemoryStream.cpp index 35364fab0f..785d1e5ce4 100644 --- a/src/core/MemoryStream.cpp +++ b/src/core/MemoryStream.cpp @@ -53,7 +53,7 @@ MemoryStream::MemoryStream(size_t capacity) _position = _data; } -MemoryStream::MemoryStream(void * data, size_t dataSize, MEMORY_ACCESS access) +MemoryStream::MemoryStream(void * data, size_t dataSize, uint32 access) { _access = access; _dataCapacity = dataSize; diff --git a/src/core/MemoryStream.h b/src/core/MemoryStream.h index 82a2fcfe5e..762944075b 100644 --- a/src/core/MemoryStream.h +++ b/src/core/MemoryStream.h @@ -42,7 +42,7 @@ public: MemoryStream(); MemoryStream(const MemoryStream ©); explicit MemoryStream(size_t capacity); - MemoryStream(void * data, size_t dataSize, MEMORY_ACCESS access = MEMORY_ACCESS_READ); + MemoryStream(void * data, size_t dataSize, uint32 access = MEMORY_ACCESS_READ); MemoryStream(const void * data, size_t dataSize); virtual ~MemoryStream(); diff --git a/src/object/ObjectFactory.cpp b/src/object/ObjectFactory.cpp index 4aa8eec4bb..0fa7a36fdd 100644 --- a/src/object/ObjectFactory.cpp +++ b/src/object/ObjectFactory.cpp @@ -87,6 +87,47 @@ public: namespace ObjectFactory { + void ReadObjectLegacy(Object * object, IReadObjectContext * context, IStream * stream) + { + try + { + object->ReadLegacy(context, stream); + } + catch (IOException ex) + { + // TODO check that ex is really EOF and not some other error + context->LogError(OBJECT_ERROR_UNEXPECTED_EOF, "Unexpectedly reached end of file."); + } + catch (Exception ex) + { + context->LogError(OBJECT_ERROR_UNKNOWN, nullptr); + } + } + + MemoryStream * GetDecodedChunkStream(IReadObjectContext * context, SDL_RWops * file) + { + size_t bufferSize = 0x600000; + void * buffer = Memory::Allocate(bufferSize); + if (buffer == nullptr) + { + log_error("Unable to allocate data buffer."); + return nullptr; + } + + bufferSize = sawyercoding_read_chunk_with_size(file, (uint8 *)buffer, bufferSize); + if (bufferSize == SIZE_MAX) + { + context->LogError(OBJECT_ERROR_BAD_ENCODING, "Unable to decode chunk."); + Memory::Free(buffer); + return nullptr; + } + else + { + buffer = Memory::Reallocate(buffer, bufferSize); + return new MemoryStream(buffer, bufferSize, MEMORY_ACCESS_READ | MEMORY_ACCESS_OWNER); + } + } + Object * CreateObjectFromLegacyFile(const utf8 * path) { Object * result = nullptr; @@ -100,54 +141,25 @@ namespace ObjectFactory result = CreateObject(entry); if (result != nullptr) { - size_t bufferSize = 0x600000; - void * buffer = Memory::Allocate(bufferSize); - if (buffer == nullptr) + utf8 objectName[9] = { 0 }; + Memory::Copy(objectName, entry.name, 8); + + auto readContext = ReadObjectContext(objectName); + auto chunkStream = GetDecodedChunkStream(&readContext, file); + if (chunkStream != nullptr) { - log_error("Unable to allocate data buffer."); + ReadObjectLegacy(result, &readContext, chunkStream); + delete chunkStream; + } + + if (readContext.WasError()) + { + Console::Error::WriteFormat("Error reading object: '%s'", path); + Console::Error::WriteLine(); + delete result; result = nullptr; } - else - { - utf8 objectName[9] = { 0 }; - Memory::Copy(objectName, entry.name, 8); - - auto readContext = ReadObjectContext(objectName); - try - { - bufferSize = sawyercoding_read_chunk_with_size(file, (uint8 *)buffer, bufferSize); - if (bufferSize == SIZE_MAX) - { - readContext.LogError(OBJECT_ERROR_BAD_ENCODING, "Unable to decode chunk."); - } - else - { - buffer = Memory::Reallocate(buffer, bufferSize); - auto ms = MemoryStream(buffer, bufferSize); - result->ReadLegacy(&readContext, &ms); - } - } - catch (IOException ex) - { - // TODO check that ex is really EOF and not some other error - readContext.LogError(OBJECT_ERROR_UNEXPECTED_EOF, "Unexpectedly reached end of file."); - } - catch (Exception ex) - { - readContext.LogError(OBJECT_ERROR_UNKNOWN, nullptr); - } - - Memory::Free(buffer); - if (readContext.WasError()) - { - Console::Error::WriteFormat("Error reading object: '%s'", path); - Console::Error::WriteLine(); - - delete result; - result = nullptr; - } - } } } SDL_RWclose(file);