Files
OpenRCT2-Web/src/openrct2/object/ImageTable.cpp
T

119 lines
3.9 KiB
C++
Raw Normal View History

2017-06-01 21:55:10 +02:00
#pragma region Copyright (c) 2014-2017 OpenRCT2 Developers
/*****************************************************************************
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
*
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 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, either version 3 of the License, or
* (at your option) any later version.
*
* A full copy of the GNU General Public License can be found in licence.txt
*****************************************************************************/
#pragma endregion
#include <algorithm>
2018-01-02 19:23:22 +00:00
#include <stdexcept>
2016-06-25 10:48:54 +01:00
#include "../core/IStream.hpp"
#include "../core/Memory.hpp"
2017-06-07 23:17:19 +01:00
#include "../OpenRCT2.h"
#include "ImageTable.h"
2016-07-01 21:01:06 +01:00
#include "Object.h"
2016-06-25 10:48:54 +01:00
ImageTable::~ImageTable()
{
Memory::Free(_data);
_data = nullptr;
_dataSize = 0;
}
2016-07-01 21:01:06 +01:00
void ImageTable::Read(IReadObjectContext * context, IStream * stream)
{
2017-06-07 23:17:19 +01:00
if (gOpenRCT2NoGraphics)
{
return;
}
2016-07-01 21:01:06 +01:00
try
2016-06-25 10:48:54 +01:00
{
2016-07-01 21:01:06 +01:00
uint32 numImages = stream->ReadValue<uint32>();
uint32 imageDataSize = stream->ReadValue<uint32>();
2016-06-25 10:48:54 +01:00
uint64 headerTableSize = numImages * 16;
uint64 remainingBytes = stream->GetLength() - stream->GetPosition() - headerTableSize;
if (remainingBytes > imageDataSize)
{
context->LogWarning(OBJECT_ERROR_BAD_IMAGE_TABLE, "Image table size longer than expected.");
imageDataSize = (uint32)remainingBytes;
}
2016-07-01 21:01:06 +01:00
_dataSize = imageDataSize;
_data = Memory::Reallocate(_data, _dataSize);
if (_data == nullptr)
{
context->LogError(OBJECT_ERROR_BAD_IMAGE_TABLE, "Image table too large.");
2018-01-02 19:23:22 +00:00
throw std::runtime_error("Image table too large.");
}
2016-06-25 10:48:54 +01:00
2016-07-01 21:01:06 +01:00
// Read g1 element headers
uintptr_t imageDataBase = (uintptr_t)_data;
for (uint32 i = 0; i < numImages; i++)
{
rct_g1_element g1Element;
2016-06-25 10:48:54 +01:00
2016-07-01 21:01:06 +01:00
uintptr_t imageDataOffset = (uintptr_t)stream->ReadValue<uint32>();
g1Element.offset = (uint8*)(imageDataBase + imageDataOffset);
g1Element.width = stream->ReadValue<sint16>();
g1Element.height = stream->ReadValue<sint16>();
g1Element.x_offset = stream->ReadValue<sint16>();
g1Element.y_offset = stream->ReadValue<sint16>();
g1Element.flags = stream->ReadValue<uint16>();
g1Element.zoomed_offset = stream->ReadValue<uint16>();
_entries.push_back(g1Element);
}
// Read g1 element data
size_t readBytes = (size_t)stream->TryRead(_data, _dataSize);
// If data is shorter than expected (some custom objects are unfortunately like that)
size_t unreadBytes = _dataSize - readBytes;
if (unreadBytes > 0)
{
uint8 * ptr = (uint8*)(((uintptr_t)_data) + readBytes);
std::fill_n(ptr, unreadBytes, 0);
2016-07-01 21:01:06 +01:00
context->LogWarning(OBJECT_ERROR_BAD_IMAGE_TABLE, "Image table size shorter than expected.");
}
// TODO validate the image data to prevent crashes in-game
2016-06-25 10:48:54 +01:00
}
2018-01-02 19:23:22 +00:00
catch (const std::exception &)
2016-06-28 21:16:09 +01:00
{
2016-07-01 21:01:06 +01:00
context->LogError(OBJECT_ERROR_BAD_IMAGE_TABLE, "Bad image table.");
throw;
2016-06-28 21:16:09 +01:00
}
}
2017-12-04 21:56:06 +00:00
2017-12-05 20:08:47 +00:00
void ImageTable::AddImage(const rct_g1_element * g1)
2017-12-04 21:56:06 +00:00
{
rct_g1_element newg1 = *g1;
2017-12-05 20:35:13 +00:00
auto length = g1_calculate_data_size(g1);
if (length == 0)
{
newg1.offset = 0;
}
else
{
auto dstOffset = _dataSize;
_dataSize += length;
_data = Memory::Reallocate(_data, _dataSize);
auto dst = (uint8 *)((size_t)_data + dstOffset);
Memory::Copy(dst, g1->offset, length);
newg1.offset = dst;
}
2017-12-04 21:56:06 +00:00
_entries.push_back(newg1);
}