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

342 lines
8.3 KiB
C++
Raw Normal View History

/*****************************************************************************
* Copyright (c) 2014-2021 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"
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;
2020-11-11 10:51:10 +01:00
ObjectType& operator++(ObjectType& d, int)
{
return d = (d == ObjectType::Count) ? ObjectType::Ride : static_cast<ObjectType>(static_cast<uint8_t>(d) + 1);
}
2021-09-12 21:25:22 +02:00
ObjectEntryDescriptor::ObjectEntryDescriptor(const rct_object_entry& 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
}
rct_object_entry Object::ParseObjectEntry(const std::string& s)
{
rct_object_entry entry = {};
std::fill_n(entry.name, sizeof(entry.name), ' ');
auto copyLen = std::min<size_t>(8, s.size());
std::copy_n(s.c_str(), copyLen, entry.name);
return entry;
}
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 = language_get_string(stringId);
}
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::GetScgWallsHeader() const
{
2021-10-27 14:21:14 +02:00
return ObjectEntryDescriptor("rct2.scenery_group.scgwalls");
}
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");
}
rct_object_entry Object::CreateHeader(const char name[DAT_NAME_LENGTH + 1], uint32_t flags, uint32_t checksum)
{
rct_object_entry 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
}
2021-01-07 01:22:22 +09:00
void rct_object_entry::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);
}
2021-09-12 21:25:22 +02:00
bool rct_object_entry::IsEmpty() const
{
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 rct_object_entry::operator==(const rct_object_entry& rhs) const
{
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 rct_object_entry::operator!=(const rct_object_entry& rhs) const
{
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
{
if (_zipPath.empty())
{
2021-01-20 20:42:03 +00:00
return std::make_unique<FileStream>(_path, FILE_MODE_OPEN);
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 stream = zipArchive->GetFileStream(_path);
if (stream != nullptr)
2021-01-09 20:54:02 +00:00
{
2021-09-15 22:22:15 +02:00
return std::make_unique<ZipStreamWrapper>(std::move(zipArchive), std::move(stream));
2021-01-09 20:54:02 +00:00
}
}
return {};
}
#ifdef __WARN_SUGGEST_FINAL_METHODS__
2018-07-21 16:17:06 +02:00
# pragma GCC diagnostic pop
#endif