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

114 lines
3.5 KiB
C++
Raw Normal View History

/*****************************************************************************
2019-03-17 08:16:15 +01:00
* Copyright (c) 2014-2019 OpenRCT2 developers
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2018-06-22 23:03:20 +02:00
#include "ImageTable.h"
#include "../OpenRCT2.h"
#include "../core/IStream.hpp"
#include "Object.h"
#include <algorithm>
#include <memory>
2018-01-02 19:23:22 +00:00
#include <stdexcept>
2016-06-25 10:48:54 +01:00
ImageTable::~ImageTable()
{
if (_data == nullptr)
{
2018-06-22 23:03:20 +02:00
for (auto& entry : _entries)
{
2018-06-22 23:03:20 +02:00
delete[] entry.offset;
}
}
2016-06-25 10:48:54 +01:00
}
2018-06-22 23:03:20 +02: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
{
uint32_t numImages = stream->ReadValue<uint32_t>();
uint32_t imageDataSize = stream->ReadValue<uint32_t>();
2016-06-25 10:48:54 +01:00
uint64_t headerTableSize = numImages * 16;
uint64_t remainingBytes = stream->GetLength() - stream->GetPosition() - headerTableSize;
if (remainingBytes > imageDataSize)
{
context->LogWarning(OBJECT_ERROR_BAD_IMAGE_TABLE, "Image table size longer than expected.");
imageDataSize = (uint32_t)remainingBytes;
}
auto dataSize = (size_t)imageDataSize;
auto data = std::make_unique<uint8_t[]>(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.get();
std::vector<rct_g1_element> newEntries;
for (uint32_t i = 0; i < numImages; i++)
2016-07-01 21:01:06 +01:00
{
rct_g1_element g1Element;
2016-06-25 10:48:54 +01:00
uintptr_t imageDataOffset = (uintptr_t)stream->ReadValue<uint32_t>();
g1Element.offset = (uint8_t*)(imageDataBase + imageDataOffset);
2016-07-01 21:01:06 +01:00
g1Element.width = stream->ReadValue<int16_t>();
g1Element.height = stream->ReadValue<int16_t>();
g1Element.x_offset = stream->ReadValue<int16_t>();
g1Element.y_offset = stream->ReadValue<int16_t>();
g1Element.flags = stream->ReadValue<uint16_t>();
g1Element.zoomed_offset = stream->ReadValue<uint16_t>();
2016-07-01 21:01:06 +01:00
newEntries.push_back(g1Element);
2016-07-01 21:01:06 +01:00
}
// Read g1 element data
size_t readBytes = (size_t)stream->TryRead(data.get(), dataSize);
2016-07-01 21:01:06 +01:00
// If data is shorter than expected (some custom objects are unfortunately like that)
size_t unreadBytes = dataSize - readBytes;
2016-07-01 21:01:06 +01:00
if (unreadBytes > 0)
{
std::fill_n(data.get() + readBytes, unreadBytes, 0);
2016-07-01 21:01:06 +01:00
context->LogWarning(OBJECT_ERROR_BAD_IMAGE_TABLE, "Image table size shorter than expected.");
}
_data = std::move(data);
_entries.insert(_entries.end(), newEntries.begin(), newEntries.end());
2016-06-25 10:48:54 +01:00
}
2018-06-22 23:03:20 +02: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
2018-06-22 23:03:20 +02: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 = nullptr;
2017-12-05 20:35:13 +00:00
}
else
{
newg1.offset = new uint8_t[length];
std::copy_n(g1->offset, length, newg1.offset);
2017-12-05 20:35:13 +00:00
}
2017-12-04 21:56:06 +00:00
_entries.push_back(newg1);
}