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

571 lines
19 KiB
C++
Raw Normal View History

2016-06-25 12:30:20 +01:00
/*****************************************************************************
2023-01-01 04:58:01 -06:00
* Copyright (c) 2014-2023 OpenRCT2 developers
2016-06-25 12:30:20 +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 12:30:20 +01:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2016-06-25 12:30:20 +01:00
*****************************************************************************/
2018-06-22 23:03:20 +02:00
#include "ObjectFactory.h"
#include "../OpenRCT2.h"
#include "../audio/audio.h"
2016-06-26 21:55:14 +01:00
#include "../core/Console.hpp"
2018-05-04 22:35:36 +01:00
#include "../core/File.h"
#include "../core/FileStream.h"
2017-12-04 21:56:06 +00:00
#include "../core/Json.hpp"
2016-06-25 12:30:20 +01:00
#include "../core/Memory.hpp"
#include "../core/MemoryStream.h"
2018-05-04 22:35:36 +01:00
#include "../core/Path.hpp"
#include "../core/String.hpp"
2018-05-03 12:43:53 +01:00
#include "../core/Zip.h"
#include "../rct12/SawyerChunkReader.h"
2022-05-09 18:34:38 +01:00
#include "AudioObject.h"
2016-06-25 23:43:30 +01:00
#include "BannerObject.h"
2016-06-25 12:30:20 +01:00
#include "EntranceObject.h"
2016-06-25 18:05:24 +01:00
#include "FootpathItemObject.h"
2016-06-25 17:09:56 +01:00
#include "FootpathObject.h"
#include "FootpathRailingsObject.h"
#include "FootpathSurfaceObject.h"
2016-06-25 22:47:09 +01:00
#include "LargeSceneryObject.h"
2021-01-09 20:54:02 +00:00
#include "MusicObject.h"
2016-06-25 12:30:20 +01:00
#include "Object.h"
2018-02-13 15:20:55 +01:00
#include "ObjectLimits.h"
#include "ObjectList.h"
2016-06-25 22:07:01 +01:00
#include "RideObject.h"
2016-06-26 00:06:55 +01:00
#include "SceneryGroupObject.h"
2016-06-25 18:58:04 +01:00
#include "SmallSceneryObject.h"
#include "StationObject.h"
#include "TerrainEdgeObject.h"
#include "TerrainSurfaceObject.h"
2016-06-25 22:59:24 +01:00
#include "WallObject.h"
2016-06-26 00:31:16 +01:00
#include "WaterObject.h"
2018-07-30 22:44:59 +02:00
2018-08-12 12:50:40 +01:00
#include <algorithm>
2022-07-29 18:45:10 +02:00
#include <memory>
2018-06-29 16:03:42 +02:00
#include <unordered_map>
2016-06-25 12:30:20 +01:00
struct IFileDataRetriever
2018-05-12 16:11:51 +01:00
{
virtual ~IFileDataRetriever() = default;
2021-01-07 01:22:22 +09:00
virtual std::vector<uint8_t> GetData(std::string_view path) const abstract;
2021-01-09 20:54:02 +00:00
virtual ObjectAsset GetAsset(std::string_view path) const abstract;
2018-05-12 16:11:51 +01:00
};
class FileSystemDataRetriever : public IFileDataRetriever
{
private:
std::string _basePath;
public:
2021-01-07 01:22:22 +09:00
FileSystemDataRetriever(std::string_view basePath)
2018-05-12 16:11:51 +01:00
: _basePath(basePath)
{
}
2021-01-07 01:22:22 +09:00
std::vector<uint8_t> GetData(std::string_view path) const override
2018-05-12 16:11:51 +01:00
{
2021-01-09 20:54:02 +00:00
auto absolutePath = Path::Combine(_basePath, path);
2018-05-12 16:11:51 +01:00
return File::ReadAllBytes(absolutePath);
}
2021-01-09 20:54:02 +00:00
ObjectAsset GetAsset(std::string_view path) const override
{
2022-05-09 18:34:38 +01:00
if (Path::IsAbsolute(path))
{
return ObjectAsset(path);
}
else
{
auto absolutePath = Path::Combine(_basePath, path);
return ObjectAsset(absolutePath);
}
2021-01-09 20:54:02 +00:00
}
2018-05-12 16:11:51 +01:00
};
class ZipDataRetriever : public IFileDataRetriever
{
private:
2021-01-09 20:54:02 +00:00
const std::string _path;
2018-05-12 16:11:51 +01:00
const IZipArchive& _zipArchive;
public:
2021-01-09 20:54:02 +00:00
ZipDataRetriever(std::string_view path, const IZipArchive& zipArchive)
: _path(path)
, _zipArchive(zipArchive)
2018-05-12 16:11:51 +01:00
{
}
2021-01-07 01:22:22 +09:00
std::vector<uint8_t> GetData(std::string_view path) const override
2018-05-12 16:11:51 +01:00
{
return _zipArchive.GetFileData(path);
}
2021-01-09 20:54:02 +00:00
ObjectAsset GetAsset(std::string_view path) const override
{
return ObjectAsset(_path, path);
}
2018-05-12 16:11:51 +01:00
};
2016-07-01 21:01:06 +01:00
class ReadObjectContext : public IReadObjectContext
{
private:
IObjectRepository& _objectRepository;
2018-06-22 23:03:20 +02:00
const IFileDataRetriever* _fileDataRetriever;
std::string _identifier;
2018-06-22 23:03:20 +02:00
bool _loadImages;
2018-05-04 22:35:36 +01:00
std::string _basePath;
bool _wasVerbose = false;
2018-06-22 23:03:20 +02:00
bool _wasWarning = false;
bool _wasError = false;
2016-07-01 21:01:06 +01:00
public:
bool WasVerbose() const
{
return _wasVerbose;
}
2018-06-22 23:03:20 +02:00
bool WasWarning() const
{
return _wasWarning;
}
bool WasError() const
{
return _wasError;
}
2016-07-01 21:01:06 +01:00
2018-05-12 16:11:51 +01:00
ReadObjectContext(
IObjectRepository& objectRepository, const std::string& identifier, bool loadImages,
2018-06-22 23:03:20 +02:00
const IFileDataRetriever* fileDataRetriever)
: _objectRepository(objectRepository)
, _fileDataRetriever(fileDataRetriever)
, _identifier(identifier)
2018-06-22 23:03:20 +02:00
, _loadImages(loadImages)
2016-07-01 21:01:06 +01:00
{
}
std::string_view GetObjectIdentifier() override
{
return _identifier;
}
IObjectRepository& GetObjectRepository() override
{
return _objectRepository;
}
bool ShouldLoadImages() override
2016-07-01 21:01:06 +01:00
{
return _loadImages;
2016-07-01 21:01:06 +01:00
}
2021-01-07 01:22:22 +09:00
std::vector<uint8_t> GetData(std::string_view path) override
2018-05-04 22:35:36 +01:00
{
2018-05-12 16:11:51 +01:00
if (_fileDataRetriever != nullptr)
{
return _fileDataRetriever->GetData(path);
}
return {};
2018-05-04 22:35:36 +01:00
}
2021-01-09 20:54:02 +00:00
ObjectAsset GetAsset(std::string_view path) override
{
if (_fileDataRetriever != nullptr)
{
return _fileDataRetriever->GetAsset(path);
}
return {};
}
void LogVerbose(ObjectError code, const utf8* text) override
{
_wasVerbose = true;
if (!String::IsNullOrEmpty(text))
{
log_verbose("[%s] Info (%d): %s", _identifier.c_str(), code, text);
}
}
void LogWarning(ObjectError code, const utf8* text) override
2016-07-01 21:01:06 +01:00
{
_wasWarning = true;
if (!String::IsNullOrEmpty(text))
{
Console::Error::WriteLine("[%s] Warning (%d): %s", _identifier.c_str(), code, text);
2016-07-01 21:01:06 +01:00
}
}
void LogError(ObjectError code, const utf8* text) override
2016-07-01 21:01:06 +01:00
{
_wasError = true;
if (!String::IsNullOrEmpty(text))
{
Console::Error::WriteLine("[%s] Error (%d): %s", _identifier.c_str(), code, text);
2016-07-01 21:01:06 +01:00
}
}
};
2016-06-25 12:30:20 +01:00
namespace ObjectFactory
{
/**
* @param jRoot Must be JSON node of type object
* @note jRoot is deliberately left non-const: json_t behaviour changes when const
*/
static std::unique_ptr<Object> CreateObjectFromJson(
IObjectRepository& objectRepository, json_t& jRoot, const IFileDataRetriever* fileRetriever, bool loadImageTable);
2018-05-03 12:43:53 +01:00
static ObjectSourceGame ParseSourceGame(const std::string& s)
2018-06-29 16:03:42 +02:00
{
static const std::unordered_map<std::string, ObjectSourceGame> LookupTable{
{ "rct1", ObjectSourceGame::RCT1 },
{ "rct1aa", ObjectSourceGame::AddedAttractions },
{ "rct1ll", ObjectSourceGame::LoopyLandscapes },
{ "rct2", ObjectSourceGame::RCT2 },
{ "rct2ww", ObjectSourceGame::WackyWorlds },
{ "rct2tt", ObjectSourceGame::TimeTwister },
{ "official", ObjectSourceGame::OpenRCT2Official },
{ "custom", ObjectSourceGame::Custom },
2018-06-29 16:03:42 +02:00
};
auto result = LookupTable.find(s);
return (result != LookupTable.end()) ? result->second : ObjectSourceGame::Custom;
2018-06-29 16:03:42 +02:00
}
static void ReadObjectLegacy(Object& object, IReadObjectContext* context, OpenRCT2::IStream* stream)
2016-07-01 21:22:18 +01:00
{
try
{
object.ReadLegacy(context, stream);
2016-07-01 21:22:18 +01:00
}
2018-06-22 23:03:20 +02:00
catch (const IOException&)
2016-07-01 21:22:18 +01:00
{
// TODO check that ex is really EOF and not some other error
context->LogError(ObjectError::UnexpectedEOF, "Unexpectedly reached end of file.");
2016-07-01 21:22:18 +01:00
}
2018-06-22 23:03:20 +02:00
catch (const std::exception&)
2016-07-01 21:22:18 +01:00
{
context->LogError(ObjectError::Unknown, nullptr);
2016-07-01 21:22:18 +01:00
}
}
std::unique_ptr<Object> CreateObjectFromLegacyFile(IObjectRepository& objectRepository, const utf8* path, bool loadImages)
2016-06-25 12:30:20 +01:00
{
log_verbose("CreateObjectFromLegacyFile(..., \"%s\")", path);
std::unique_ptr<Object> result;
try
2016-06-25 12:30:20 +01:00
{
auto fs = OpenRCT2::FileStream(path, OpenRCT2::FILE_MODE_OPEN);
auto chunkReader = SawyerChunkReader(&fs);
rct_object_entry entry = fs.ReadValue<rct_object_entry>();
2020-11-09 10:36:37 +01:00
if (entry.GetType() != ObjectType::ScenarioText)
2016-06-25 12:30:20 +01:00
{
2021-09-12 21:25:22 +02:00
result = CreateObject(entry.GetType());
result->SetDescriptor(ObjectEntryDescriptor(entry));
2018-08-29 23:29:45 +02:00
utf8 objectName[DAT_NAME_LENGTH + 1] = { 0 };
object_entry_get_name_fixed(objectName, sizeof(objectName), &entry);
log_verbose(" entry: { 0x%08X, \"%s\", 0x%08X }", entry.flags, objectName, entry.checksum);
auto chunk = chunkReader.ReadChunk();
log_verbose(" size: %zu", chunk->GetLength());
auto chunkStream = OpenRCT2::MemoryStream(chunk->GetData(), chunk->GetLength());
auto readContext = ReadObjectContext(objectRepository, objectName, loadImages, nullptr);
ReadObjectLegacy(*result, &readContext, &chunkStream);
2018-08-29 23:29:45 +02:00
if (readContext.WasError())
{
throw std::runtime_error("Object has errors");
}
2020-10-17 13:27:02 -07:00
result->SetSourceGames({ entry.GetSourceGame() });
2016-06-25 12:30:20 +01:00
}
}
2018-12-05 23:12:22 +01:00
catch (const std::exception& e)
{
2018-12-05 23:12:22 +01:00
log_error("Error: %s when processing object %s", e.what(), path);
2016-06-25 12:30:20 +01:00
}
return result;
}
std::unique_ptr<Object> CreateObjectFromLegacyData(
2018-06-22 23:03:20 +02:00
IObjectRepository& objectRepository, const rct_object_entry* entry, const void* data, size_t dataSize)
2016-07-09 14:52:56 +01:00
{
2016-07-16 14:17:36 +01:00
Guard::ArgumentNotNull(entry, GUARD_LINE);
Guard::ArgumentNotNull(data, GUARD_LINE);
2016-07-09 14:52:56 +01:00
2021-09-12 21:25:22 +02:00
auto result = CreateObject(entry->GetType());
2016-07-09 14:52:56 +01:00
if (result != nullptr)
{
2021-09-12 21:25:22 +02:00
result->SetDescriptor(ObjectEntryDescriptor(*entry));
2018-01-02 20:36:42 +01:00
utf8 objectName[DAT_NAME_LENGTH + 1];
2016-07-09 15:24:00 +01:00
object_entry_get_name_fixed(objectName, sizeof(objectName), entry);
2016-07-09 14:52:56 +01:00
auto readContext = ReadObjectContext(objectRepository, objectName, !gOpenRCT2NoGraphics, nullptr);
auto chunkStream = OpenRCT2::MemoryStream(data, dataSize);
ReadObjectLegacy(*result, &readContext, &chunkStream);
2016-07-09 14:52:56 +01:00
if (readContext.WasError())
{
log_error("Error when processing object.");
2016-07-09 14:52:56 +01:00
}
else
{
2020-10-17 13:27:02 -07:00
result->SetSourceGames({ entry->GetSourceGame() });
}
2016-07-09 14:52:56 +01:00
}
return result;
}
2021-09-12 21:25:22 +02:00
std::unique_ptr<Object> CreateObject(ObjectType type)
2016-06-25 12:30:20 +01:00
{
std::unique_ptr<Object> result;
2021-09-12 21:25:22 +02:00
switch (type)
2018-06-22 23:03:20 +02:00
{
2020-11-09 10:36:37 +01:00
case ObjectType::Ride:
2021-09-12 21:25:22 +02:00
result = std::make_unique<RideObject>();
2018-06-22 23:03:20 +02:00
break;
2020-11-09 10:36:37 +01:00
case ObjectType::SmallScenery:
2021-09-12 21:25:22 +02:00
result = std::make_unique<SmallSceneryObject>();
2018-06-22 23:03:20 +02:00
break;
2020-11-09 10:36:37 +01:00
case ObjectType::LargeScenery:
2021-09-12 21:25:22 +02:00
result = std::make_unique<LargeSceneryObject>();
2018-06-22 23:03:20 +02:00
break;
2020-11-09 10:36:37 +01:00
case ObjectType::Walls:
2021-09-12 21:25:22 +02:00
result = std::make_unique<WallObject>();
2018-06-22 23:03:20 +02:00
break;
2020-11-09 10:36:37 +01:00
case ObjectType::Banners:
2021-09-12 21:25:22 +02:00
result = std::make_unique<BannerObject>();
2018-06-22 23:03:20 +02:00
break;
2020-11-09 10:36:37 +01:00
case ObjectType::Paths:
2021-09-12 21:25:22 +02:00
result = std::make_unique<FootpathObject>();
2018-06-22 23:03:20 +02:00
break;
2020-11-09 10:36:37 +01:00
case ObjectType::PathBits:
2021-09-12 21:25:22 +02:00
result = std::make_unique<FootpathItemObject>();
2018-06-22 23:03:20 +02:00
break;
2020-11-09 10:36:37 +01:00
case ObjectType::SceneryGroup:
2021-09-12 21:25:22 +02:00
result = std::make_unique<SceneryGroupObject>();
2018-06-22 23:03:20 +02:00
break;
2020-11-09 10:36:37 +01:00
case ObjectType::ParkEntrance:
2021-09-12 21:25:22 +02:00
result = std::make_unique<EntranceObject>();
2018-06-22 23:03:20 +02:00
break;
2020-11-09 10:36:37 +01:00
case ObjectType::Water:
2021-09-12 21:25:22 +02:00
result = std::make_unique<WaterObject>();
2018-06-22 23:03:20 +02:00
break;
2020-11-09 10:36:37 +01:00
case ObjectType::ScenarioText:
2018-06-22 23:03:20 +02:00
break;
2020-11-09 10:36:37 +01:00
case ObjectType::TerrainSurface:
2021-09-12 21:25:22 +02:00
result = std::make_unique<TerrainSurfaceObject>();
break;
2020-11-09 10:36:37 +01:00
case ObjectType::TerrainEdge:
2021-09-12 21:25:22 +02:00
result = std::make_unique<TerrainEdgeObject>();
break;
2020-11-09 10:36:37 +01:00
case ObjectType::Station:
2021-09-12 21:25:22 +02:00
result = std::make_unique<StationObject>();
break;
2021-01-09 20:54:02 +00:00
case ObjectType::Music:
2021-09-12 21:25:22 +02:00
result = std::make_unique<MusicObject>();
2021-01-09 20:54:02 +00:00
break;
case ObjectType::FootpathSurface:
2021-09-12 21:25:22 +02:00
result = std::make_unique<FootpathSurfaceObject>();
break;
case ObjectType::FootpathRailings:
2021-09-12 21:25:22 +02:00
result = std::make_unique<FootpathRailingsObject>();
break;
2022-05-09 18:34:38 +01:00
case ObjectType::Audio:
result = std::make_unique<AudioObject>();
break;
2018-06-22 23:03:20 +02:00
default:
throw std::runtime_error("Invalid object type");
2016-06-25 12:30:20 +01:00
}
return result;
}
2017-12-04 21:56:06 +00:00
static ObjectType ParseObjectType(const std::string& s)
2017-12-07 23:50:44 +00:00
{
2018-06-22 23:03:20 +02:00
if (s == "ride")
2020-11-09 10:36:37 +01:00
return ObjectType::Ride;
2018-06-22 23:03:20 +02:00
if (s == "footpath_banner")
2020-11-09 10:36:37 +01:00
return ObjectType::Banners;
2018-06-22 23:03:20 +02:00
if (s == "footpath_item")
2020-11-09 10:36:37 +01:00
return ObjectType::PathBits;
2018-06-22 23:03:20 +02:00
if (s == "scenery_small")
2020-11-09 10:36:37 +01:00
return ObjectType::SmallScenery;
2018-06-22 23:03:20 +02:00
if (s == "scenery_large")
2020-11-09 10:36:37 +01:00
return ObjectType::LargeScenery;
2018-06-22 23:03:20 +02:00
if (s == "scenery_wall")
2020-11-09 10:36:37 +01:00
return ObjectType::Walls;
2018-06-22 23:03:20 +02:00
if (s == "scenery_group")
2020-11-09 10:36:37 +01:00
return ObjectType::SceneryGroup;
2018-06-22 23:03:20 +02:00
if (s == "park_entrance")
2020-11-09 10:36:37 +01:00
return ObjectType::ParkEntrance;
2018-06-22 23:03:20 +02:00
if (s == "water")
2020-11-09 10:36:37 +01:00
return ObjectType::Water;
if (s == "terrain_surface")
2020-11-09 10:36:37 +01:00
return ObjectType::TerrainSurface;
if (s == "terrain_edge")
2020-11-09 10:36:37 +01:00
return ObjectType::TerrainEdge;
if (s == "station")
2020-11-09 10:36:37 +01:00
return ObjectType::Station;
2021-01-09 20:54:02 +00:00
if (s == "music")
return ObjectType::Music;
if (s == "footpath_surface")
return ObjectType::FootpathSurface;
if (s == "footpath_railings")
return ObjectType::FootpathRailings;
2022-05-09 18:34:38 +01:00
if (s == "audio")
return ObjectType::Audio;
return ObjectType::None;
2017-12-07 23:50:44 +00:00
}
std::unique_ptr<Object> CreateObjectFromZipFile(IObjectRepository& objectRepository, std::string_view path, bool loadImages)
2018-05-03 12:43:53 +01:00
{
try
{
2018-05-12 16:11:51 +01:00
auto archive = Zip::Open(path, ZIP_ACCESS::READ);
2018-05-03 12:43:53 +01:00
auto jsonBytes = archive->GetFileData("object.json");
if (jsonBytes.empty())
{
throw std::runtime_error("Unable to open object.json.");
}
json_t jRoot = Json::FromVector(jsonBytes);
if (jRoot.is_object())
2018-05-03 12:43:53 +01:00
{
2021-01-09 20:54:02 +00:00
auto fileDataRetriever = ZipDataRetriever(path, *archive);
return CreateObjectFromJson(objectRepository, jRoot, &fileDataRetriever, loadImages);
2018-05-03 12:43:53 +01:00
}
}
catch (const std::exception& e)
{
2021-01-07 01:22:22 +09:00
Console::Error::WriteLine("Unable to open or read '%s': %s", std::string(path).c_str(), e.what());
2018-05-03 12:43:53 +01:00
}
return nullptr;
2018-05-03 12:43:53 +01:00
}
std::unique_ptr<Object> CreateObjectFromJsonFile(
IObjectRepository& objectRepository, const std::string& path, bool loadImages)
2017-12-04 21:56:06 +00:00
{
log_verbose("CreateObjectFromJsonFile(\"%s\")", path.c_str());
try
{
json_t jRoot = Json::ReadFromFile(path.c_str());
2018-05-12 16:11:51 +01:00
auto fileDataRetriever = FileSystemDataRetriever(Path::GetDirectory(path));
return CreateObjectFromJson(objectRepository, jRoot, &fileDataRetriever, loadImages);
2017-12-04 21:56:06 +00:00
}
2018-06-22 23:03:20 +02:00
catch (const std::runtime_error& err)
2017-12-04 21:56:06 +00:00
{
2018-03-27 20:43:20 +02:00
Console::Error::WriteLine("Unable to open or read '%s': %s", path.c_str(), err.what());
2017-12-04 21:56:06 +00:00
}
return nullptr;
2017-12-04 21:56:06 +00:00
}
2018-05-03 12:43:53 +01:00
static void ExtractSourceGames(const std::string& id, json_t& jRoot, Object& result)
{
auto sourceGames = jRoot["sourceGame"];
if (sourceGames.is_array() || sourceGames.is_string())
{
std::vector<ObjectSourceGame> sourceGameVector;
for (const auto& jSourceGame : sourceGames)
{
sourceGameVector.push_back(ParseSourceGame(Json::GetString(jSourceGame)));
}
if (!sourceGameVector.empty())
{
result.SetSourceGames(sourceGameVector);
}
else
{
log_error("Object %s has an incorrect sourceGame parameter.", id.c_str());
result.SetSourceGames({ ObjectSourceGame::Custom });
}
}
// >90% of objects are custom, so allow omitting the parameter without displaying an error.
else if (sourceGames.is_null())
{
result.SetSourceGames({ ObjectSourceGame::Custom });
}
else
{
log_error("Object %s has an incorrect sourceGame parameter.", id.c_str());
result.SetSourceGames({ ObjectSourceGame::Custom });
}
}
std::unique_ptr<Object> CreateObjectFromJson(
IObjectRepository& objectRepository, json_t& jRoot, const IFileDataRetriever* fileRetriever, bool loadImageTable)
2018-05-03 12:43:53 +01:00
{
2022-08-29 10:50:42 +01:00
if (!jRoot.is_object())
{
throw std::runtime_error("Object JSON root was not an object");
}
2018-05-03 12:43:53 +01:00
log_verbose("CreateObjectFromJson(...)");
std::unique_ptr<Object> result;
auto objectType = ParseObjectType(Json::GetString(jRoot["objectType"]));
2020-11-09 12:07:38 +01:00
if (objectType != ObjectType::None)
2018-05-03 12:43:53 +01:00
{
auto id = Json::GetString(jRoot["id"]);
// HACK Disguise RCT Classic audio as RCT2 audio so asset packs override correctly
if (id == OpenRCT2::Audio::AudioObjectIdentifiers::Rct2cBase)
id = OpenRCT2::Audio::AudioObjectIdentifiers::Rct2Base;
2023-01-07 14:25:39 -08:00
auto version = VersionTuple(Json::GetString(jRoot["version"]));
2021-09-12 21:25:22 +02:00
ObjectEntryDescriptor descriptor;
auto originalId = Json::GetString(jRoot["originalId"]);
if (originalId.length() == 8 + 1 + 8 + 1 + 8)
2018-05-03 12:43:53 +01:00
{
2021-09-12 21:25:22 +02:00
auto originalName = originalId.substr(9, 8);
2018-05-03 12:43:53 +01:00
2021-09-12 21:25:22 +02:00
rct_object_entry entry = {};
entry.flags = std::stoul(originalId.substr(0, 8), nullptr, 16);
entry.checksum = std::stoul(originalId.substr(18, 8), nullptr, 16);
entry.SetType(objectType);
auto minLength = std::min<size_t>(8, originalName.length());
std::memcpy(entry.name, originalName.c_str(), minLength);
descriptor = ObjectEntryDescriptor(entry);
}
else
{
descriptor = ObjectEntryDescriptor(objectType, id);
}
2022-11-23 14:20:44 -08:00
descriptor.Version = version;
2021-09-12 21:25:22 +02:00
result = CreateObject(objectType);
2022-11-23 14:20:44 -08:00
result->SetVersion(version);
result->SetIdentifier(id);
2021-09-12 21:25:22 +02:00
result->SetDescriptor(descriptor);
result->MarkAsJsonObject();
auto readContext = ReadObjectContext(objectRepository, id, loadImageTable, fileRetriever);
result->ReadJson(&readContext, jRoot);
if (readContext.WasError())
{
throw std::runtime_error("Object has errors");
}
2018-05-03 12:43:53 +01:00
auto jAuthors = jRoot["authors"];
std::vector<std::string> authorVector;
for (const auto& jAuthor : jAuthors)
{
if (jAuthor.is_string())
2018-05-03 12:43:53 +01:00
{
authorVector.emplace_back(Json::GetString(jAuthor));
2018-05-03 12:43:53 +01:00
}
}
result->SetAuthors(std::move(authorVector));
ExtractSourceGames(id, jRoot, *result);
2018-05-03 12:43:53 +01:00
}
return result;
}
2018-06-22 23:03:20 +02:00
} // namespace ObjectFactory