2016-06-25 02:52:58 +01:00
|
|
|
/*****************************************************************************
|
2020-05-07 19:35:30 +01:00
|
|
|
* Copyright (c) 2014-2020 OpenRCT2 developers
|
2016-06-25 02:52:58 +01:00
|
|
|
*
|
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
|
2016-06-25 02:52:58 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2016-06-25 02:52:58 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
#include "Object.h"
|
|
|
|
|
|
2020-05-07 19:35:30 +01:00
|
|
|
#include "../Context.h"
|
2021-01-09 20:54:02 +00:00
|
|
|
#include "../core/File.h"
|
2016-07-04 18:53:35 +01:00
|
|
|
#include "../core/Memory.hpp"
|
|
|
|
|
#include "../core/String.hpp"
|
2021-01-09 20:54:02 +00:00
|
|
|
#include "../core/Zip.h"
|
2018-01-06 18:32:25 +01:00
|
|
|
#include "../localisation/Language.h"
|
2020-05-07 19:35:30 +01:00
|
|
|
#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"
|
2016-06-25 02:52:58 +01:00
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
#include <algorithm>
|
2020-05-07 19:35:30 +01:00
|
|
|
#include <cstring>
|
2018-12-05 20:33:45 +00:00
|
|
|
#include <stdexcept>
|
2018-06-22 23:03:20 +02:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
Object::Object(const rct_object_entry& entry)
|
2016-06-25 02:52:58 +01:00
|
|
|
{
|
2016-06-25 14:44:04 +01:00
|
|
|
_objectEntry = entry;
|
2016-07-04 18:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
2018-12-05 20:33:45 +00:00
|
|
|
void* Object::GetLegacyData()
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("Not supported.");
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-30 23:01:22 +02:00
|
|
|
void Object::ReadLegacy(IReadObjectContext* context, OpenRCT2::IStream* stream)
|
2018-12-05 20:33:45 +00:00
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
_imageTable.ReadJson(context, root);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
std::string Object::GetOverrideString(uint8_t index) const
|
2016-07-04 18:53:35 +01:00
|
|
|
{
|
2020-05-07 19:35:30 +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
|
|
|
}
|
|
|
|
|
|
2020-08-29 04:36:58 +02:00
|
|
|
std::string Object::GetString(ObjectStringID index) const
|
2016-07-04 18:53:35 +01:00
|
|
|
{
|
2020-08-29 04:36:58 +02: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
|
|
|
}
|
|
|
|
|
|
2020-08-29 04:36:58 +02:00
|
|
|
std::string Object::GetString(int32_t language, ObjectStringID index) const
|
2018-04-27 18:23:39 +01:00
|
|
|
{
|
|
|
|
|
return GetStringTable().GetString(language, index);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-01 11:54:25 +01:00
|
|
|
rct_object_entry Object::GetScgWallsHeader()
|
|
|
|
|
{
|
|
|
|
|
return Object::CreateHeader("SCGWALLS", 207140231, 3518650219);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rct_object_entry Object::GetScgPathXHeader()
|
|
|
|
|
{
|
|
|
|
|
return Object::CreateHeader("SCGPATHX", 207140231, 890227440);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
rct_object_entry Object::CreateHeader(const char name[DAT_NAME_LENGTH + 1], uint32_t flags, uint32_t checksum)
|
2017-12-01 11:54:25 +01:00
|
|
|
{
|
2018-06-04 19:50:46 +02:00
|
|
|
rct_object_entry header = {};
|
2017-12-01 11:54:25 +01:00
|
|
|
header.flags = flags;
|
2018-02-01 12:52:39 +00:00
|
|
|
std::copy_n(name, DAT_NAME_LENGTH, header.name);
|
2017-12-01 11:54:25 +01:00
|
|
|
header.checksum = checksum;
|
|
|
|
|
return header;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-17 13:27:02 -07:00
|
|
|
std::vector<ObjectSourceGame> Object::GetSourceGames()
|
2018-07-03 13:46:45 +02:00
|
|
|
{
|
2018-07-06 15:15:56 +02:00
|
|
|
return _sourceGames;
|
2018-07-03 13:46:45 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-17 13:27:02 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2017-11-23 10:14:28 +01: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"
|
2017-11-23 10:14:28 +01:00
|
|
|
#endif
|
|
|
|
|
|
2017-12-05 12:55:33 +00:00
|
|
|
std::string Object::GetName() const
|
2016-07-04 18:53:35 +01:00
|
|
|
{
|
2020-08-29 04:36:58 +02:00
|
|
|
return GetString(ObjectStringID::NAME);
|
2016-06-25 02:52:58 +01:00
|
|
|
}
|
2017-11-23 10:14:28 +01:00
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
std::string Object::GetName(int32_t language) const
|
2018-04-27 18:23:39 +01:00
|
|
|
{
|
2020-08-29 04:36:58 +02: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)
|
2020-05-07 19:35:30 +01:00
|
|
|
{
|
|
|
|
|
std::memset(name, ' ', sizeof(name));
|
|
|
|
|
std::memcpy(name, value.data(), std::min(sizeof(name), value.size()));
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 06:23:28 +01:00
|
|
|
const std::vector<std::string>& Object::GetAuthors() const
|
|
|
|
|
{
|
|
|
|
|
return _authors;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 06:14:15 +09:00
|
|
|
void Object::SetAuthors(std::vector<std::string>&& authors)
|
2020-08-09 06:23:28 +01:00
|
|
|
{
|
2021-01-12 06:14:15 +09:00
|
|
|
_authors = std::move(authors);
|
2020-08-09 06:23:28 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-18 16:22:08 +01:00
|
|
|
std::optional<uint8_t> rct_object_entry::GetSceneryType() const
|
|
|
|
|
{
|
|
|
|
|
switch (GetType())
|
|
|
|
|
{
|
2020-11-09 10:36:37 +01:00
|
|
|
case ObjectType::SmallScenery:
|
2020-03-18 16:22:08 +01:00
|
|
|
return SCENERY_TYPE_SMALL;
|
2020-11-09 10:36:37 +01:00
|
|
|
case ObjectType::LargeScenery:
|
2020-03-18 16:22:08 +01:00
|
|
|
return SCENERY_TYPE_LARGE;
|
2020-11-09 10:36:37 +01:00
|
|
|
case ObjectType::Walls:
|
2020-03-18 16:22:08 +01:00
|
|
|
return SCENERY_TYPE_WALL;
|
2020-11-09 10:36:37 +01:00
|
|
|
case ObjectType::Banners:
|
2020-03-18 16:22:08 +01:00
|
|
|
return SCENERY_TYPE_BANNER;
|
2020-11-09 10:36:37 +01:00
|
|
|
case ObjectType::PathBits:
|
2020-03-18 16:22:08 +01:00
|
|
|
return SCENERY_TYPE_PATH_ITEM;
|
|
|
|
|
default:
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-09 20:54:02 +00:00
|
|
|
class zipstreamwrapper final : public std::istream
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
std::unique_ptr<IZipArchive> _zipArchive;
|
|
|
|
|
std::unique_ptr<std::istream> _base;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
zipstreamwrapper(std::unique_ptr<IZipArchive> zipArchive, std::unique_ptr<std::istream> base)
|
|
|
|
|
: std::istream(base->rdbuf())
|
|
|
|
|
, _zipArchive(std::move(zipArchive))
|
|
|
|
|
, _base(std::move(base))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bool ObjectAsset::IsAvailable() const
|
|
|
|
|
{
|
|
|
|
|
if (_zipPath.empty())
|
|
|
|
|
{
|
|
|
|
|
return File::Exists(_path);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
auto zipArchive = Zip::TryOpen(_zipPath, ZIP_ACCESS::READ);
|
|
|
|
|
return zipArchive != nullptr && zipArchive->Exists(_path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t ObjectAsset::GetLength() const
|
|
|
|
|
{
|
|
|
|
|
if (_zipPath.empty())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return File::ReadAllBytes(_path).size();
|
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
auto zipArchive = Zip::TryOpen(_zipPath, ZIP_ACCESS::READ);
|
|
|
|
|
if (zipArchive != nullptr)
|
|
|
|
|
{
|
|
|
|
|
auto index = zipArchive->GetIndexFromPath(_path);
|
|
|
|
|
if (index)
|
|
|
|
|
{
|
|
|
|
|
auto size = zipArchive->GetFileSize(*index);
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<std::istream> ObjectAsset::GetStream() const
|
|
|
|
|
{
|
|
|
|
|
if (_zipPath.empty())
|
|
|
|
|
{
|
|
|
|
|
return std::make_unique<std::ifstream>(_path, std::ios::binary);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-23 10:14:28 +01:00
|
|
|
#ifdef __WARN_SUGGEST_FINAL_METHODS__
|
2018-07-21 16:17:06 +02:00
|
|
|
# pragma GCC diagnostic pop
|
2017-11-23 10:14:28 +01:00
|
|
|
#endif
|