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

404 lines
9.9 KiB
C++
Raw Normal View History

/*****************************************************************************
2023-01-01 04:58:01 -06:00
* Copyright (c) 2014-2023 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 "Object.h"
#include "../Context.h"
2021-01-09 20:54:02 +00:00
#include "../core/File.h"
2021-01-20 20:42:03 +00:00
#include "../core/FileStream.h"
2016-07-04 18:53:35 +01:00
#include "../core/Memory.hpp"
#include "../core/String.hpp"
2021-09-11 19:06:17 +03:00
#include "../core/ZipStream.hpp"
#include "../drawing/Image.h"
2018-01-06 18:32:25 +01:00
#include "../localisation/Language.h"
#include "../localisation/LocalisationService.h"
2018-01-06 18:32:25 +01:00
#include "../localisation/StringIds.h"
2020-03-18 16:22:08 +01:00
#include "../world/Scenery.h"
2018-01-02 20:36:42 +01:00
#include "ObjectLimits.h"
#include "ObjectRepository.h"
2018-06-22 23:03:20 +02:00
#include <algorithm>
#include <cstring>
#include <stdexcept>
2018-06-22 23:03:20 +02:00
2021-01-20 20:42:03 +00:00
using namespace OpenRCT2;
ObjectEntryDescriptor::ObjectEntryDescriptor(const RCTObjectEntry& newEntry)
{
2021-09-12 21:25:22 +02:00
if (!newEntry.IsEmpty())
{
Generation = ObjectGeneration::DAT;
Entry = newEntry;
}
}
ObjectEntryDescriptor::ObjectEntryDescriptor(std::string_view newIdentifier)
{
Generation = ObjectGeneration::JSON;
Identifier = std::string(newIdentifier);
}
ObjectEntryDescriptor::ObjectEntryDescriptor(ObjectType type, std::string_view newIdentifier)
{
Generation = ObjectGeneration::JSON;
Identifier = std::string(newIdentifier);
Type = type;
}
ObjectEntryDescriptor::ObjectEntryDescriptor(const ObjectRepositoryItem& ori)
{
if (!ori.Identifier.empty())
{
Generation = ObjectGeneration::JSON;
Identifier = std::string(ori.Identifier);
}
else
{
Generation = ObjectGeneration::DAT;
Entry = ori.ObjectEntry;
}
}
bool ObjectEntryDescriptor::HasValue() const
{
return Generation != ObjectGeneration::JSON || !Identifier.empty();
};
ObjectType ObjectEntryDescriptor::GetType() const
{
return Generation == ObjectGeneration::JSON ? Type : Entry.GetType();
}
std::string_view ObjectEntryDescriptor::GetName() const
{
return Generation == ObjectGeneration::JSON ? Identifier : Entry.GetName();
}
bool ObjectEntryDescriptor::operator==(const ObjectEntryDescriptor& rhs) const
{
if (Generation != rhs.Generation)
return false;
if (Generation == ObjectGeneration::DAT)
{
return Entry == rhs.Entry;
}
2021-11-14 16:44:04 +00:00
else
{
return Type == rhs.Type && Identifier == rhs.Identifier;
}
2021-09-12 21:25:22 +02:00
}
bool ObjectEntryDescriptor::operator!=(const ObjectEntryDescriptor& rhs) const
{
return !(*this == rhs);
2016-07-04 18:53:35 +01:00
}
void* Object::GetLegacyData()
{
throw std::runtime_error("Not supported.");
}
void Object::ReadLegacy(IReadObjectContext* context, OpenRCT2::IStream* stream)
{
throw std::runtime_error("Not supported.");
}
2020-08-12 19:44:59 +01:00
void Object::PopulateTablesFromJson(IReadObjectContext* context, json_t& root)
{
_stringTable.ReadJson(root);
2021-11-30 19:20:09 +01:00
_usesFallbackImages = _imageTable.ReadJson(context, root);
2020-08-12 19:44:59 +01:00
}
std::string Object::GetOverrideString(uint8_t index) const
2016-07-04 18:53:35 +01:00
{
auto legacyIdentifier = GetLegacyIdentifier();
const auto& localisationService = OpenRCT2::GetContext()->GetLocalisationService();
auto stringId = localisationService.GetObjectOverrideStringId(legacyIdentifier, index);
2016-07-04 18:53:35 +01:00
2018-06-22 23:03:20 +02:00
const utf8* result = nullptr;
2016-07-04 18:53:35 +01:00
if (stringId != STR_NONE)
{
result = LanguageGetString(stringId);
2016-07-04 18:53:35 +01:00
}
2017-12-05 12:55:33 +00:00
return String::ToStd(result);
2016-07-04 18:53:35 +01:00
}
std::string Object::GetString(ObjectStringID index) const
2016-07-04 18:53:35 +01:00
{
auto sz = GetOverrideString(static_cast<uint8_t>(index));
2017-12-22 15:08:00 +01:00
if (sz.empty())
2016-07-04 18:53:35 +01:00
{
2017-12-11 13:06:59 +00:00
sz = GetStringTable().GetString(index);
2016-07-04 18:53:35 +01:00
}
2017-12-05 12:55:33 +00:00
return sz;
2016-07-04 18:53:35 +01:00
}
std::string Object::GetString(int32_t language, ObjectStringID index) const
2018-04-27 18:23:39 +01:00
{
return GetStringTable().GetString(language, index);
}
2021-03-12 11:04:04 +01:00
ObjectEntryDescriptor Object::GetScgPathXHeader() const
{
2021-10-27 14:21:14 +02:00
return ObjectEntryDescriptor("rct2.scenery_group.scgpathx");
}
RCTObjectEntry Object::CreateHeader(const char name[DAT_NAME_LENGTH + 1], uint32_t flags, uint32_t checksum)
{
RCTObjectEntry header = {};
header.flags = flags;
2018-02-01 12:52:39 +00:00
std::copy_n(name, DAT_NAME_LENGTH, header.name);
header.checksum = checksum;
return header;
}
std::vector<ObjectSourceGame> Object::GetSourceGames()
{
2018-07-06 15:15:56 +02:00
return _sourceGames;
}
void Object::SetSourceGames(const std::vector<ObjectSourceGame>& sourceGames)
2018-01-31 13:07:20 +01:00
{
2018-07-06 15:15:56 +02:00
_sourceGames = sourceGames;
2018-06-29 16:03:42 +02:00
}
#ifdef __WARN_SUGGEST_FINAL_METHODS__
2018-07-21 16:17:06 +02:00
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wsuggest-final-methods"
#endif
2017-12-05 12:55:33 +00:00
std::string Object::GetName() const
2016-07-04 18:53:35 +01:00
{
return GetString(ObjectStringID::NAME);
}
std::string Object::GetName(int32_t language) const
2018-04-27 18:23:39 +01:00
{
return GetString(language, ObjectStringID::NAME);
2018-04-27 18:23:39 +01:00
}
ImageIndex Object::LoadImages()
{
if (_baseImageId == ImageIndexUndefined)
{
_baseImageId = GfxObjectAllocateImages(GetImageTable().GetImages(), GetImageTable().GetCount());
}
return _baseImageId;
}
void Object::UnloadImages()
{
if (_baseImageId != ImageIndexUndefined)
{
GfxObjectFreeImages(_baseImageId, GetImageTable().GetCount());
}
}
void RCTObjectEntry::SetName(std::string_view value)
{
std::memset(name, ' ', sizeof(name));
std::memcpy(name, value.data(), std::min(sizeof(name), value.size()));
}
const std::vector<std::string>& Object::GetAuthors() const
{
return _authors;
}
void Object::SetAuthors(std::vector<std::string>&& authors)
{
_authors = std::move(authors);
}
bool Object::IsCompatibilityObject() const
{
return _isCompatibilityObject;
}
void Object::SetIsCompatibilityObject(const bool on)
{
_isCompatibilityObject = on;
}
bool RCTObjectEntry::IsEmpty() const
2021-09-12 21:25:22 +02:00
{
uint64_t a, b;
std::memcpy(&a, reinterpret_cast<const uint8_t*>(this), 8);
std::memcpy(&b, reinterpret_cast<const uint8_t*>(this) + 8, 8);
if (a == 0xFFFFFFFFFFFFFFFF && b == 0xFFFFFFFFFFFFFFFF)
return true;
if (a == 0 && b == 0)
return true;
return false;
}
bool RCTObjectEntry::operator==(const RCTObjectEntry& rhs) const
2021-09-12 21:25:22 +02:00
{
const auto a = this;
const auto b = &rhs;
// If an official object don't bother checking checksum
if ((a->flags & 0xF0) || (b->flags & 0xF0))
{
if (a->GetType() != b->GetType())
{
return false;
}
int32_t match = memcmp(a->name, b->name, 8);
if (match)
{
return false;
}
}
else
{
if (a->flags != b->flags)
{
return false;
}
int32_t match = memcmp(a->name, b->name, 8);
if (match)
{
return false;
}
if (a->checksum != b->checksum)
{
return false;
}
}
return true;
}
bool RCTObjectEntry::operator!=(const RCTObjectEntry& rhs) const
2021-09-12 21:25:22 +02:00
{
return !(*this == rhs);
}
2021-01-09 20:54:02 +00:00
bool ObjectAsset::IsAvailable() const
{
if (_zipPath.empty())
{
return File::Exists(_path);
}
2021-09-15 22:22:15 +02:00
auto zipArchive = Zip::TryOpen(_zipPath, ZIP_ACCESS::READ);
return zipArchive != nullptr && zipArchive->Exists(_path);
2021-01-09 20:54:02 +00:00
}
uint64_t ObjectAsset::GetSize() const
2021-01-09 20:54:02 +00:00
{
if (_zipPath.empty())
{
return File::GetSize(_path);
2021-01-09 20:54:02 +00:00
}
2021-09-15 22:22:15 +02:00
auto zipArchive = Zip::TryOpen(_zipPath, ZIP_ACCESS::READ);
if (zipArchive != nullptr)
2021-01-09 20:54:02 +00:00
{
2021-09-15 22:22:15 +02:00
auto index = zipArchive->GetIndexFromPath(_path);
if (index.has_value())
2021-01-09 20:54:02 +00:00
{
2021-09-15 22:22:15 +02:00
auto size = zipArchive->GetFileSize(index.value());
return size;
2021-01-09 20:54:02 +00:00
}
}
return 0;
}
2022-05-09 18:34:38 +01:00
std::vector<uint8_t> ObjectAsset::GetData() const
{
if (_zipPath.empty())
{
return File::ReadAllBytes(_path);
}
auto zipArchive = Zip::TryOpen(_zipPath, ZIP_ACCESS::READ);
if (zipArchive != nullptr)
{
return zipArchive->GetFileData(_path);
}
return {};
}
2021-01-20 20:42:03 +00:00
std::unique_ptr<IStream> ObjectAsset::GetStream() const
2021-01-09 20:54:02 +00:00
{
2022-05-13 01:04:39 +01:00
try
2021-01-09 20:54:02 +00:00
{
2022-05-13 01:04:39 +01:00
if (_zipPath.empty())
2021-01-09 20:54:02 +00:00
{
2022-05-13 01:04:39 +01:00
return std::make_unique<FileStream>(_path, FILE_MODE_OPEN);
2021-01-09 20:54:02 +00:00
}
2022-05-13 01:04:39 +01:00
auto zipArchive = Zip::TryOpen(_zipPath, ZIP_ACCESS::READ);
if (zipArchive != nullptr)
{
auto stream = zipArchive->GetFileStream(_path);
if (stream != nullptr)
{
return std::make_unique<ZipStreamWrapper>(std::move(zipArchive), std::move(stream));
}
}
}
catch (...)
{
2021-01-09 20:54:02 +00:00
}
return {};
}
2023-01-07 14:25:39 -08:00
u8string VersionString(const ObjectVersion& version)
{
return std::to_string(std::get<0>(version)) + "." + std::to_string(std::get<1>(version)) + "."
+ std::to_string(std::get<2>(version));
}
ObjectVersion VersionTuple(std::string_view version)
{
if (version.empty())
{
return std::make_tuple(0, 0, 0);
}
auto nums = String::Split(version, ".");
uint16_t versions[VersionNumFields] = {};
if (nums.size() > VersionNumFields)
{
LOG_WARNING("%i fields found in version string '%s', expected X.Y.Z", nums.size(), version);
2023-01-07 14:25:39 -08:00
}
if (nums.size() == 0)
{
LOG_WARNING("No fields found in version string '%s', expected X.Y.Z", version);
2023-01-07 14:25:39 -08:00
return std::make_tuple(0, 0, 0);
}
try
{
size_t highestIndex = std::min(nums.size(), VersionNumFields);
for (size_t i = 0; i < highestIndex; i++)
{
auto value = stoi(nums.at(i));
constexpr auto maxValue = std::numeric_limits<uint16_t>().max();
if (value > maxValue)
{
LOG_WARNING(
2023-01-07 14:25:39 -08:00
"Version value too high in version string '%s', version value will be capped to %i.", version, maxValue);
value = maxValue;
}
versions[i] = value;
}
}
catch (const std::exception&)
{
LOG_WARNING("Malformed version string '%s', expected X.Y.Z", version);
2023-01-07 14:25:39 -08:00
}
return std::make_tuple(versions[0], versions[1], versions[2]);
}
#ifdef __WARN_SUGGEST_FINAL_METHODS__
2018-07-21 16:17:06 +02:00
# pragma GCC diagnostic pop
#endif