1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-15 11:03:00 +01:00

refactor object factory loading

This commit is contained in:
Ted John
2016-07-01 21:22:18 +01:00
parent 8ce9a66286
commit 72f7f0f329
3 changed files with 58 additions and 46 deletions

View File

@@ -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;

View File

@@ -42,7 +42,7 @@ public:
MemoryStream();
MemoryStream(const MemoryStream &copy);
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();

View File

@@ -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<void>(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<void>(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);