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

643 lines
22 KiB
C++
Raw Normal View History

/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 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"
2020-08-12 19:44:59 +01:00
#include "../Context.h"
2024-07-09 16:23:31 +02:00
#include "../Diagnostic.h"
2018-06-22 23:03:20 +02:00
#include "../OpenRCT2.h"
2020-08-12 19:44:59 +01:00
#include "../PlatformEnvironment.h"
2025-02-13 10:51:46 -05:00
#include "../SpriteIds.h"
2020-08-12 19:44:59 +01:00
#include "../core/File.h"
#include "../core/FileScanner.h"
#include "../core/Guard.hpp"
2018-06-22 23:03:20 +02:00
#include "../core/IStream.hpp"
#include "../core/Json.hpp"
2020-08-12 19:44:59 +01:00
#include "../core/Path.hpp"
#include "../core/String.hpp"
#include "../drawing/ImageImporter.h"
2018-06-22 23:03:20 +02:00
#include "Object.h"
2020-08-12 19:44:59 +01:00
#include "ObjectFactory.h"
2018-06-22 23:03:20 +02:00
#include <memory>
2018-01-02 19:23:22 +00:00
#include <stdexcept>
2020-08-12 19:44:59 +01:00
using namespace OpenRCT2;
using namespace OpenRCT2::Drawing;
2022-09-03 23:34:34 +02:00
static thread_local std::map<u8string, std::unique_ptr<Object>> _objDataCache = {};
2020-08-12 19:44:59 +01:00
struct ImageTable::RequiredImage
{
G1Element g1{};
2020-08-12 19:44:59 +01:00
std::unique_ptr<RequiredImage> next_zoom;
bool HasData() const
{
return g1.offset != nullptr;
}
RequiredImage() = default;
RequiredImage(const RequiredImage&) = delete;
RequiredImage(const G1Element& orig)
2020-08-12 19:44:59 +01:00
{
auto length = G1CalculateDataSize(&orig);
2020-08-12 19:44:59 +01:00
g1 = orig;
g1.offset = new uint8_t[length];
std::memcpy(g1.offset, orig.offset, length);
g1.flags &= ~G1_FLAG_HAS_ZOOM_SPRITE;
}
RequiredImage(uint32_t idx, std::function<const G1Element*(uint32_t)> getter)
2020-08-12 19:44:59 +01:00
{
auto orig = getter(idx);
if (orig != nullptr)
{
auto length = G1CalculateDataSize(orig);
2020-08-12 19:44:59 +01:00
g1 = *orig;
g1.offset = new uint8_t[length];
std::memcpy(g1.offset, orig->offset, length);
if ((g1.flags & G1_FLAG_HAS_ZOOM_SPRITE) && g1.zoomed_offset != 0)
{
// Fetch image for next zoom level
next_zoom = std::make_unique<RequiredImage>(static_cast<uint32_t>(idx - g1.zoomed_offset), getter);
if (!next_zoom->HasData())
{
next_zoom = nullptr;
g1.flags &= ~G1_FLAG_HAS_ZOOM_SPRITE;
}
}
}
}
~RequiredImage()
{
delete[] g1.offset;
}
};
std::vector<std::unique_ptr<ImageTable::RequiredImage>> ImageTable::ParseImages(IReadObjectContext* context, std::string s)
{
std::vector<std::unique_ptr<RequiredImage>> result;
if (s.empty())
{
result.push_back(std::make_unique<RequiredImage>());
}
2024-12-11 22:28:31 +01:00
else if (String::startsWith(s, "$CSG"))
2020-08-12 19:44:59 +01:00
{
auto rangeStart = s.find('[');
auto rangeEnd = s.find(']');
if (rangeStart != std::string::npos && rangeEnd != std::string::npos)
2020-08-12 19:44:59 +01:00
{
auto rangeString = s.substr(rangeStart, rangeEnd - rangeStart + 1);
auto range = ParseRange(rangeString);
if (IsCsgLoaded())
2020-08-12 19:44:59 +01:00
{
for (auto i : range)
{
result.push_back(std::make_unique<RequiredImage>(
static_cast<uint32_t>(SPR_CSG_BEGIN + i),
[](uint32_t idx) -> const G1Element* { return GfxGetG1Element(idx); }));
2020-08-12 19:44:59 +01:00
}
}
else
{
std::string id(context->GetObjectIdentifier());
LOG_WARNING("CSG not loaded inserting placeholder images for %s", id.c_str());
result.resize(range.size());
for (auto& res : result)
{
res = std::make_unique<RequiredImage>();
}
}
2020-08-12 19:44:59 +01:00
}
}
2024-12-11 22:28:31 +01:00
else if (String::startsWith(s, "$G1"))
2020-08-12 19:44:59 +01:00
{
auto rangeStart = s.find('[');
auto rangeEnd = s.find(']');
if (rangeStart != std::string::npos && rangeEnd != std::string::npos)
2020-08-12 19:44:59 +01:00
{
auto rangeString = s.substr(rangeStart, rangeEnd - rangeStart + 1);
auto range = ParseRange(rangeString);
2020-08-12 19:44:59 +01:00
for (auto i : range)
{
result.push_back(std::make_unique<RequiredImage>(
static_cast<uint32_t>(i), [](uint32_t idx) -> const G1Element* { return GfxGetG1Element(idx); }));
2020-08-12 19:44:59 +01:00
}
}
}
2024-12-11 22:28:31 +01:00
else if (String::startsWith(s, "$RCT2:OBJDATA/"))
2020-08-12 19:44:59 +01:00
{
auto name = s.substr(14);
auto rangeStart = name.find('[');
auto rangeEnd = name.find(']');
if (rangeStart != std::string::npos && rangeEnd != std::string::npos)
2020-08-12 19:44:59 +01:00
{
auto rangeString = name.substr(rangeStart, rangeEnd - rangeStart + 1);
auto range = ParseRange(rangeString);
2020-08-12 19:44:59 +01:00
name = name.substr(0, rangeStart);
result = LoadObjectImages(context, name, range);
}
}
2024-12-11 22:28:31 +01:00
else if (String::startsWith(s, "$LGX:"))
2022-03-19 11:02:02 -07:00
{
auto name = s.substr(5);
auto rangeStart = name.find('[');
auto rangeEnd = name.find(']');
if (rangeStart != std::string::npos && rangeEnd != std::string::npos)
2022-03-19 11:02:02 -07:00
{
auto rangeString = name.substr(rangeStart, rangeEnd - rangeStart + 1);
auto range = ParseRange(rangeString);
2022-03-19 11:02:02 -07:00
name = name.substr(0, rangeStart);
result = LoadImageArchiveImages(context, name, range);
}
else
{
result = LoadImageArchiveImages(context, name);
}
}
2020-08-12 19:44:59 +01:00
else
{
try
{
auto imageData = context->GetData(s);
2021-10-20 02:10:56 +03:00
auto image = Imaging::ReadFromBuffer(imageData);
auto meta = ImageImportMeta{};
2020-08-12 19:44:59 +01:00
ImageImporter importer;
auto importResult = importer.Import(image, meta);
2020-08-12 19:44:59 +01:00
result.push_back(std::make_unique<RequiredImage>(importResult.Element));
}
catch (const std::exception& e)
{
2024-12-11 22:28:31 +01:00
auto msg = String::stdFormat("Unable to load image '%s': %s", s.c_str(), e.what());
context->LogWarning(ObjectError::BadImageTable, msg.c_str());
2020-08-12 19:44:59 +01:00
result.push_back(std::make_unique<RequiredImage>());
}
}
return result;
}
2021-10-20 02:10:56 +03:00
std::vector<std::unique_ptr<ImageTable::RequiredImage>> ImageTable::ParseImages(
IReadObjectContext* context, std::vector<std::pair<std::string, Image>>& imageSources, json_t& el)
2020-08-12 19:44:59 +01:00
{
Guard::Assert(el.is_object(), "ImageTable::ParseImages expects parameter el to be object");
auto path = Json::GetString(el["path"]);
auto meta = createImageImportMetaFromJson(el);
2020-08-12 19:44:59 +01:00
std::vector<std::unique_ptr<RequiredImage>> result;
try
{
2021-10-20 02:10:56 +03:00
auto itSource = std::find_if(
imageSources.begin(), imageSources.end(),
[&path](const std::pair<std::string, Image>& item) { return item.first == path; });
if (itSource == imageSources.end())
{
throw std::runtime_error("Unable to find image in image source list.");
}
auto& image = itSource->second;
2020-08-12 19:44:59 +01:00
ImageImporter importer;
auto importResult = importer.Import(image, meta);
2021-10-20 02:10:56 +03:00
auto g1element = importResult.Element;
result.push_back(std::make_unique<RequiredImage>(g1element));
2020-08-12 19:44:59 +01:00
}
catch (const std::exception& e)
{
2024-12-11 22:28:31 +01:00
auto msg = String::stdFormat("Unable to load image '%s': %s", path.c_str(), e.what());
context->LogWarning(ObjectError::BadImageTable, msg.c_str());
2020-08-12 19:44:59 +01:00
result.push_back(std::make_unique<RequiredImage>());
}
return result;
}
2022-03-19 11:02:02 -07:00
std::vector<std::unique_ptr<ImageTable::RequiredImage>> ImageTable::LoadImageArchiveImages(
IReadObjectContext* context, const std::string& path, const std::vector<int32_t>& range)
{
std::vector<std::unique_ptr<RequiredImage>> result;
auto gxRaw = context->GetData(path);
std::optional<Gx> gxData = GfxLoadGx(gxRaw);
2022-03-19 11:02:02 -07:00
if (gxData.has_value())
{
// Fix entry data offsets
for (uint32_t i = 0; i < gxData->header.num_entries; i++)
{
if (gxData->elements[i].offset == nullptr)
{
gxData->elements[i].offset = gxData->data.get();
}
else
{
gxData->elements[i].offset += reinterpret_cast<uintptr_t>(gxData->data.get());
}
2022-03-19 11:02:02 -07:00
}
if (range.size() > 0)
{
size_t placeHoldersAdded = 0;
for (auto i : range)
{
if (i >= 0 && (i < static_cast<int32_t>(gxData->header.num_entries)))
{
result.push_back(std::make_unique<RequiredImage>(gxData->elements[i]));
}
else
{
result.push_back(std::make_unique<RequiredImage>());
placeHoldersAdded++;
}
}
// Log place holder information
if (placeHoldersAdded > 0)
{
std::string msg = "Adding " + std::to_string(placeHoldersAdded) + " placeholders";
context->LogWarning(ObjectError::InvalidProperty, msg.c_str());
}
}
else
{
for (int i = 0; i < static_cast<int32_t>(gxData->header.num_entries); i++)
result.push_back(std::make_unique<RequiredImage>(gxData->elements[i]));
}
}
else
{
2024-12-11 22:28:31 +01:00
auto msg = String::stdFormat("Unable to load Gx '%s'", path.c_str());
2022-03-19 11:02:02 -07:00
context->LogWarning(ObjectError::BadImageTable, msg.c_str());
for (size_t i = 0; i < range.size(); i++)
{
result.push_back(std::make_unique<RequiredImage>());
}
}
return result;
}
2020-08-12 19:44:59 +01:00
std::vector<std::unique_ptr<ImageTable::RequiredImage>> ImageTable::LoadObjectImages(
IReadObjectContext* context, const std::string& name, const std::vector<int32_t>& range)
{
std::vector<std::unique_ptr<RequiredImage>> result;
2022-09-03 23:34:34 +02:00
Object* obj;
auto cached = _objDataCache.find(name);
if (cached != _objDataCache.end())
{
obj = cached->second.get();
}
else
{
auto objectPath = FindLegacyObject(name);
auto tmp = ObjectFactory::CreateObjectFromLegacyFile(
context->GetObjectRepository(), objectPath.c_str(), !gOpenRCT2NoGraphics);
auto inserted = _objDataCache.insert({ name, std::move(tmp) });
obj = inserted.first->second.get();
}
2020-08-12 19:44:59 +01:00
if (obj != nullptr)
{
2022-09-03 23:34:34 +02:00
auto& imgTable = static_cast<const Object*>(obj)->GetImageTable();
2020-08-12 19:44:59 +01:00
auto numImages = static_cast<int32_t>(imgTable.GetCount());
auto images = imgTable.GetImages();
size_t placeHoldersAdded = 0;
for (auto i : range)
{
if (i >= 0 && i < numImages)
{
result.push_back(std::make_unique<RequiredImage>(
static_cast<uint32_t>(i), [images](uint32_t idx) -> const G1Element* { return &images[idx]; }));
2020-08-12 19:44:59 +01:00
}
else
{
result.push_back(std::make_unique<RequiredImage>());
placeHoldersAdded++;
}
}
// Log place holder information
if (placeHoldersAdded > 0)
{
std::string msg = "Adding " + std::to_string(placeHoldersAdded) + " placeholders";
context->LogWarning(ObjectError::InvalidProperty, msg.c_str());
2020-08-12 19:44:59 +01:00
}
}
else
{
2022-09-03 23:34:34 +02:00
std::string msg = "Unable to open '" + name + "'";
context->LogWarning(ObjectError::InvalidProperty, msg.c_str());
2020-08-12 19:44:59 +01:00
for (size_t i = 0; i < range.size(); i++)
{
result.push_back(std::make_unique<RequiredImage>());
}
}
return result;
}
std::vector<int32_t> ImageTable::ParseRange(std::string s)
{
// Currently only supports [###] or [###..###]
std::vector<int32_t> result = {};
if (s.length() >= 3 && s[0] == '[' && s[s.length() - 1] == ']')
{
s = s.substr(1, s.length() - 2);
2024-12-11 22:28:31 +01:00
auto parts = String::split(s, "..");
2020-08-12 19:44:59 +01:00
if (parts.size() == 1)
{
result.push_back(std::stoi(parts[0]));
}
else
{
auto left = std::stoi(parts[0]);
auto right = std::stoi(parts[1]);
if (left <= right)
{
for (auto i = left; i <= right; i++)
{
result.push_back(i);
}
}
else
{
for (auto i = right; i >= left; i--)
{
result.push_back(i);
}
}
}
}
return result;
}
std::string ImageTable::FindLegacyObject(const std::string& name)
{
const auto& env = GetContext()->GetPlatformEnvironment();
auto objectsPath = env.GetDirectoryPath(DirBase::rct2, DirId::objects);
2020-08-12 19:44:59 +01:00
auto objectPath = Path::Combine(objectsPath, name);
if (File::Exists(objectPath))
{
return objectPath;
}
std::string altName = name;
auto rangeStart = name.find(".DAT");
if (rangeStart != std::string::npos)
{
altName.replace(rangeStart, 4, ".POB");
}
objectPath = Path::Combine(objectsPath, altName);
if (File::Exists(objectPath))
{
return objectPath;
}
2020-08-12 19:44:59 +01:00
if (!File::Exists(objectPath))
{
// Search recursively for any file with the target name (case insensitive)
2022-02-27 22:23:35 +01:00
auto filter = Path::Combine(objectsPath, u8"*.dat;*.pob");
auto scanner = Path::ScanDirectory(filter, true);
2020-08-12 19:44:59 +01:00
while (scanner->Next())
{
auto currentName = Path::GetFileName(scanner->GetPathRelative());
2024-12-11 22:28:31 +01:00
if (String::iequals(currentName, name) || String::iequals(currentName, altName))
2020-08-12 19:44:59 +01:00
{
objectPath = scanner->GetPath();
break;
}
}
}
return objectPath;
}
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
}
void ImageTable::Read(IReadObjectContext* context, OpenRCT2::IStream* stream)
{
2017-06-07 23:17:19 +01:00
if (gOpenRCT2NoGraphics)
{
return;
}
try
2016-06-25 10:48:54 +01:00
{
uint32_t numImages = stream->ReadValue<uint32_t>();
uint32_t imageDataSize = stream->ReadValue<uint32_t>();
uint64_t headerTableSize = numImages * 16;
uint64_t remainingBytes = stream->GetLength() - stream->GetPosition() - headerTableSize;
if (remainingBytes > imageDataSize)
{
context->LogVerbose(ObjectError::BadImageTable, "Image table size longer than expected.");
imageDataSize = static_cast<uint32_t>(remainingBytes);
}
auto dataSize = static_cast<size_t>(imageDataSize);
auto data = std::make_unique<uint8_t[]>(dataSize);
if (data == nullptr)
{
context->LogError(ObjectError::BadImageTable, "Image table too large.");
throw std::runtime_error("Image table too large.");
}
// Read g1 element headers
uintptr_t imageDataBase = reinterpret_cast<uintptr_t>(data.get());
std::vector<G1Element> newEntries;
for (uint32_t i = 0; i < numImages; i++)
{
G1Element g1Element{};
uintptr_t imageDataOffset = static_cast<uintptr_t>(stream->ReadValue<uint32_t>());
g1Element.offset = reinterpret_cast<uint8_t*>(imageDataBase + imageDataOffset);
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>();
newEntries.push_back(std::move(g1Element));
}
// Read g1 element data
size_t readBytes = static_cast<size_t>(stream->TryRead(data.get(), dataSize));
// If data is shorter than expected (some custom objects are unfortunately like that)
size_t unreadBytes = dataSize - readBytes;
if (unreadBytes > 0)
{
std::fill_n(data.get() + readBytes, unreadBytes, 0);
context->LogWarning(ObjectError::BadImageTable, "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
}
catch (const std::exception&)
2016-06-28 21:16:09 +01:00
{
context->LogError(ObjectError::BadImageTable, "Bad image table.");
2016-07-01 21:01:06 +01:00
throw;
2016-06-28 21:16:09 +01:00
}
}
2017-12-04 21:56:06 +00:00
2021-10-20 02:10:56 +03:00
std::vector<std::pair<std::string, Image>> ImageTable::GetImageSources(IReadObjectContext* context, json_t& jsonImages)
{
std::vector<std::pair<std::string, Image>> result;
for (auto& jsonImage : jsonImages)
{
if (jsonImage.is_object() && jsonImage.contains("path"))
2021-10-20 02:10:56 +03:00
{
auto path = Json::GetString(jsonImage["path"]);
auto keepPalette = Json::GetString(jsonImage["palette"]) == "keep";
auto itSource = std::find_if(result.begin(), result.end(), [&path](const std::pair<std::string, Image>& item) {
return item.first == path;
});
if (itSource == result.end())
{
auto imageData = context->GetData(path);
2025-03-26 11:59:12 +01:00
auto imageFormat = keepPalette ? ImageFormat::png : ImageFormat::png32;
2021-10-20 02:10:56 +03:00
auto image = Imaging::ReadFromBuffer(imageData, imageFormat);
auto pair = std::make_pair<std::string, Image>(std::move(path), std::move(image));
result.push_back(std::move(pair));
}
}
}
return result;
}
2021-11-30 19:20:09 +01:00
bool ImageTable::ReadJson(IReadObjectContext* context, json_t& root)
2020-08-12 19:44:59 +01:00
{
Guard::Assert(root.is_object(), "ImageTable::ReadJson expects parameter root to be object");
2021-11-30 19:20:09 +01:00
bool usesFallbackSprites = false;
2020-08-12 19:44:59 +01:00
if (context->ShouldLoadImages())
{
// First gather all the required images from inspecting the JSON
std::vector<std::unique_ptr<RequiredImage>> allImages;
auto jsonImages = root["images"];
if (!IsCsgLoaded() && root.contains("noCsgImages"))
2021-11-30 19:20:09 +01:00
{
jsonImages = root["noCsgImages"];
usesFallbackSprites = true;
}
2021-10-20 02:10:56 +03:00
auto imageSources = GetImageSources(context, jsonImages);
2020-08-12 19:44:59 +01:00
for (auto& jsonImage : jsonImages)
{
if (jsonImage.is_string())
{
auto strImage = jsonImage.get<std::string>();
auto images = ParseImages(context, strImage);
allImages.insert(
allImages.end(), std::make_move_iterator(images.begin()), std::make_move_iterator(images.end()));
}
else if (jsonImage.is_object())
{
if (jsonImage.contains("gx"))
{
auto xOverride = Json::GetNumber<int16_t>(jsonImage["x"], std::numeric_limits<int16_t>::max());
auto yOverride = Json::GetNumber<int16_t>(jsonImage["y"], std::numeric_limits<int16_t>::max());
const bool hasXOverride = xOverride != std::numeric_limits<int16_t>::max();
const bool hasYOverride = yOverride != std::numeric_limits<int16_t>::max();
auto strImage = jsonImage["gx"].get<std::string>();
auto images = ParseImages(context, strImage);
if (hasXOverride || hasYOverride)
{
for (auto& image : images)
{
if (hasXOverride)
image->g1.x_offset = xOverride;
if (hasYOverride)
image->g1.y_offset = yOverride;
}
}
allImages.insert(
allImages.end(), std::make_move_iterator(images.begin()), std::make_move_iterator(images.end()));
}
else
{
auto images = ParseImages(context, imageSources, jsonImage);
allImages.insert(
allImages.end(), std::make_move_iterator(images.begin()), std::make_move_iterator(images.end()));
}
2020-08-12 19:44:59 +01:00
}
}
// Now add all the images to the image table
auto imagesStartIndex = GetCount();
for (const auto& img : allImages)
{
const auto& g1 = img->g1;
AddImage(&g1);
}
// Add all the zoom images at the very end of the image table.
// This way it should not affect the offsets used within the object logic.
for (size_t j = 0; j < allImages.size(); j++)
{
const auto tableIndex = imagesStartIndex + j;
const auto* img = allImages[j].get();
if (img->next_zoom != nullptr)
{
img = img->next_zoom.get();
// Set old image zoom offset to zoom image which we are about to add
auto g1a = const_cast<G1Element*>(&GetImages()[tableIndex]);
2020-08-12 19:44:59 +01:00
g1a->zoomed_offset = static_cast<int32_t>(tableIndex) - static_cast<int32_t>(GetCount());
while (img != nullptr)
{
auto g1b = img->g1;
if (img->next_zoom != nullptr)
{
g1b.zoomed_offset = -1;
}
AddImage(&g1b);
img = img->next_zoom.get();
}
}
}
}
2021-11-30 19:20:09 +01:00
2022-09-03 23:34:34 +02:00
_objDataCache.clear();
2021-11-30 19:20:09 +01:00
return usesFallbackSprites;
2020-08-12 19:44:59 +01:00
}
void ImageTable::AddImage(const G1Element* g1)
2017-12-04 21:56:06 +00:00
{
G1Element newg1 = *g1;
auto length = G1CalculateDataSize(g1);
2017-12-05 20:35:13 +00:00
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
}
_entries.push_back(std::move(newg1));
2017-12-04 21:56:06 +00:00
}