mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-10 09:32:29 +01:00
Pass object repository as a dependency when loading objects
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
#include "../drawing/Drawing.h"
|
||||
#include "../localisation/Language.h"
|
||||
#include "../object/Object.h"
|
||||
#include "../object/ObjectRepository.h"
|
||||
#include "BannerObject.h"
|
||||
#include "ObjectJsonHelpers.h"
|
||||
#include "ObjectList.h"
|
||||
@@ -46,16 +47,20 @@ void BannerObject::ReadLegacy(IReadObjectContext * context, IStream * stream)
|
||||
|
||||
// Add banners to 'Signs and items for footpaths' group, rather than lumping them in the Miscellaneous tab.
|
||||
// Since this is already done the other way round for original items, avoid adding those to prevent duplicates.
|
||||
const std::string identifier = GetIdentifier();
|
||||
const rct_object_entry * objectEntry = object_list_find_by_name(identifier.c_str());
|
||||
static const rct_object_entry scgPathX = Object::GetScgPathXHeader();
|
||||
auto identifier = GetIdentifier();
|
||||
|
||||
if (objectEntry != nullptr &&
|
||||
(object_entry_get_source_game(objectEntry) == OBJECT_SOURCE_WACKY_WORLDS ||
|
||||
object_entry_get_source_game(objectEntry) == OBJECT_SOURCE_TIME_TWISTER ||
|
||||
object_entry_get_source_game(objectEntry) == OBJECT_SOURCE_CUSTOM))
|
||||
auto& objectRepository = context->GetObjectRepository();
|
||||
auto item = objectRepository.FindObject(identifier);
|
||||
if (item != nullptr)
|
||||
{
|
||||
SetPrimarySceneryGroup(&scgPathX);
|
||||
auto objectEntry = &item->ObjectEntry;
|
||||
if (object_entry_get_source_game(objectEntry) == OBJECT_SOURCE_WACKY_WORLDS ||
|
||||
object_entry_get_source_game(objectEntry) == OBJECT_SOURCE_TIME_TWISTER ||
|
||||
object_entry_get_source_game(objectEntry) == OBJECT_SOURCE_CUSTOM)
|
||||
{
|
||||
auto scgPathX = Object::GetScgPathXHeader();
|
||||
SetPrimarySceneryGroup(&scgPathX);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include "../interface/Cursors.h"
|
||||
#include "../localisation/Localisation.h"
|
||||
#include "../object/Object.h"
|
||||
#include "../object/ObjectRepository.h"
|
||||
#include "ObjectList.h"
|
||||
#include "FootpathItemObject.h"
|
||||
#include "ObjectJsonHelpers.h"
|
||||
@@ -49,16 +50,20 @@ void FootpathItemObject::ReadLegacy(IReadObjectContext * context, IStream * stre
|
||||
|
||||
// Add path bits to 'Signs and items for footpaths' group, rather than lumping them in the Miscellaneous tab.
|
||||
// Since this is already done the other way round for original items, avoid adding those to prevent duplicates.
|
||||
const std::string identifier = GetIdentifier();
|
||||
const rct_object_entry * objectEntry = object_list_find_by_name(identifier.c_str());
|
||||
static const rct_object_entry scgPathX = Object::GetScgPathXHeader();
|
||||
auto identifier = GetIdentifier();
|
||||
|
||||
if (objectEntry != nullptr &&
|
||||
(object_entry_get_source_game(objectEntry) == OBJECT_SOURCE_WACKY_WORLDS ||
|
||||
object_entry_get_source_game(objectEntry) == OBJECT_SOURCE_TIME_TWISTER ||
|
||||
object_entry_get_source_game(objectEntry) == OBJECT_SOURCE_CUSTOM))
|
||||
auto& objectRepository = context->GetObjectRepository();
|
||||
auto item = objectRepository.FindObject(identifier);
|
||||
if (item != nullptr)
|
||||
{
|
||||
SetPrimarySceneryGroup(&scgPathX);
|
||||
auto sourceGame = object_entry_get_source_game(&item->ObjectEntry);
|
||||
if (sourceGame == OBJECT_SOURCE_WACKY_WORLDS ||
|
||||
sourceGame == OBJECT_SOURCE_TIME_TWISTER ||
|
||||
sourceGame == OBJECT_SOURCE_CUSTOM)
|
||||
{
|
||||
auto scgPathX = Object::GetScgPathXHeader();
|
||||
SetPrimarySceneryGroup(&scgPathX);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -111,6 +111,7 @@ struct rct_object_filters {
|
||||
assert_struct_size(rct_object_filters, 3);
|
||||
#pragma pack(pop)
|
||||
|
||||
interface IObjectRepository;
|
||||
interface IStream;
|
||||
struct ObjectRepositoryItem;
|
||||
struct rct_drawpixelinfo;
|
||||
@@ -119,6 +120,7 @@ interface IReadObjectContext
|
||||
{
|
||||
virtual ~IReadObjectContext() = default;
|
||||
|
||||
virtual IObjectRepository& GetObjectRepository() abstract;
|
||||
virtual bool ShouldLoadImages() abstract;
|
||||
|
||||
virtual void LogWarning(uint32 code, const utf8 * text) abstract;
|
||||
@@ -206,7 +208,6 @@ sint32 object_calculate_checksum(const rct_object_entry * entry, const void * da
|
||||
bool find_object_in_entry_group(const rct_object_entry* entry, uint8* entry_type, uint8* entry_index);
|
||||
void object_create_identifier_name(char* string_buffer, size_t size, const rct_object_entry* object);
|
||||
|
||||
const rct_object_entry * object_list_find_by_name(const char *name);
|
||||
const rct_object_entry * object_list_find(rct_object_entry *entry);
|
||||
|
||||
void object_entry_get_name_fixed(utf8 * buffer, size_t bufferSize, const rct_object_entry * entry);
|
||||
|
||||
@@ -41,6 +41,8 @@
|
||||
class ReadObjectContext : public IReadObjectContext
|
||||
{
|
||||
private:
|
||||
IObjectRepository& _objectRepository;
|
||||
|
||||
std::string _objectName;
|
||||
bool _loadImages;
|
||||
bool _wasWarning = false;
|
||||
@@ -50,12 +52,18 @@ public:
|
||||
bool WasWarning() const { return _wasWarning; }
|
||||
bool WasError() const { return _wasError; }
|
||||
|
||||
ReadObjectContext(const std::string &objectName, bool loadImages)
|
||||
: _objectName(objectName),
|
||||
ReadObjectContext(IObjectRepository& objectRepository, const std::string &objectName, bool loadImages)
|
||||
: _objectRepository(objectRepository),
|
||||
_objectName(objectName),
|
||||
_loadImages(loadImages)
|
||||
{
|
||||
}
|
||||
|
||||
IObjectRepository& GetObjectRepository() override
|
||||
{
|
||||
return _objectRepository;
|
||||
}
|
||||
|
||||
bool ShouldLoadImages() override
|
||||
{
|
||||
return _loadImages;
|
||||
@@ -101,9 +109,9 @@ namespace ObjectFactory
|
||||
}
|
||||
}
|
||||
|
||||
Object * CreateObjectFromLegacyFile(const utf8 * path)
|
||||
Object * CreateObjectFromLegacyFile(IObjectRepository& objectRepository, const utf8 * path)
|
||||
{
|
||||
log_verbose("CreateObjectFromLegacyFile(\"%s\")", path);
|
||||
log_verbose("CreateObjectFromLegacyFile(..., \"%s\")", path);
|
||||
|
||||
Object * result = nullptr;
|
||||
try
|
||||
@@ -122,7 +130,7 @@ namespace ObjectFactory
|
||||
log_verbose(" size: %zu", chunk->GetLength());
|
||||
|
||||
auto chunkStream = MemoryStream(chunk->GetData(), chunk->GetLength());
|
||||
auto readContext = ReadObjectContext(objectName, !gOpenRCT2Headless);
|
||||
auto readContext = ReadObjectContext(objectRepository, objectName, !gOpenRCT2Headless);
|
||||
ReadObjectLegacy(result, &readContext, &chunkStream);
|
||||
if (readContext.WasError())
|
||||
{
|
||||
@@ -137,7 +145,7 @@ namespace ObjectFactory
|
||||
return result;
|
||||
}
|
||||
|
||||
Object * CreateObjectFromLegacyData(const rct_object_entry * entry, const void * data, size_t dataSize)
|
||||
Object * CreateObjectFromLegacyData(IObjectRepository& objectRepository, const rct_object_entry * entry, const void * data, size_t dataSize)
|
||||
{
|
||||
Guard::ArgumentNotNull(entry, GUARD_LINE);
|
||||
Guard::ArgumentNotNull(data, GUARD_LINE);
|
||||
@@ -148,7 +156,7 @@ namespace ObjectFactory
|
||||
utf8 objectName[DAT_NAME_LENGTH + 1];
|
||||
object_entry_get_name_fixed(objectName, sizeof(objectName), entry);
|
||||
|
||||
auto readContext = ReadObjectContext(objectName, !gOpenRCT2Headless);
|
||||
auto readContext = ReadObjectContext(objectRepository, objectName, !gOpenRCT2Headless);
|
||||
auto chunkStream = MemoryStream(data, dataSize);
|
||||
ReadObjectLegacy(result, &readContext, &chunkStream);
|
||||
|
||||
@@ -220,7 +228,7 @@ namespace ObjectFactory
|
||||
return 0xFF;
|
||||
}
|
||||
|
||||
Object * CreateObjectFromJsonFile(const std::string &path)
|
||||
Object * CreateObjectFromJsonFile(IObjectRepository& objectRepository, const std::string &path)
|
||||
{
|
||||
log_verbose("CreateObjectFromJsonFile(\"%s\")", path.c_str());
|
||||
|
||||
@@ -249,7 +257,7 @@ namespace ObjectFactory
|
||||
memcpy(entry.name, originalName.c_str(), minLength);
|
||||
|
||||
result = CreateObject(entry);
|
||||
auto readContext = ReadObjectContext(id, !gOpenRCT2Headless);
|
||||
auto readContext = ReadObjectContext(objectRepository, id, !gOpenRCT2Headless);
|
||||
result->ReadJson(&readContext, jRoot);
|
||||
if (readContext.WasError())
|
||||
{
|
||||
|
||||
@@ -18,14 +18,15 @@
|
||||
|
||||
#include "../common.h"
|
||||
|
||||
interface IObjectRepository;
|
||||
class Object;
|
||||
struct rct_object_entry;
|
||||
|
||||
namespace ObjectFactory
|
||||
{
|
||||
Object * CreateObjectFromLegacyFile(const utf8 * path);
|
||||
Object * CreateObjectFromLegacyData(const rct_object_entry * entry, const void * data, size_t dataSize);
|
||||
Object * CreateObjectFromLegacyFile(IObjectRepository& objectRepository, const utf8 * path);
|
||||
Object * CreateObjectFromLegacyData(IObjectRepository& objectRepository, const rct_object_entry * entry, const void * data, size_t dataSize);
|
||||
Object * CreateObject(const rct_object_entry &entry);
|
||||
|
||||
Object * CreateObjectFromJsonFile(const std::string &path);
|
||||
Object * CreateObjectFromJsonFile(IObjectRepository& objectRepository, const std::string &path);
|
||||
}
|
||||
|
||||
@@ -234,7 +234,7 @@ namespace ObjectJsonHelpers
|
||||
{
|
||||
std::vector<rct_g1_element> result;
|
||||
auto objectPath = FindLegacyObject(name);
|
||||
auto obj = ObjectFactory::CreateObjectFromLegacyFile(objectPath.c_str());
|
||||
auto obj = ObjectFactory::CreateObjectFromLegacyFile(context->GetObjectRepository(), objectPath.c_str());
|
||||
if (obj != nullptr)
|
||||
{
|
||||
auto &imgTable = static_cast<const Object *>(obj)->GetImageTable();
|
||||
|
||||
@@ -83,8 +83,10 @@ private:
|
||||
static constexpr uint16 VERSION = 17;
|
||||
static constexpr auto PATTERN = "*.dat;*.pob;*.json";
|
||||
|
||||
IObjectRepository& _objectRepository;
|
||||
|
||||
public:
|
||||
explicit ObjectFileIndex(IPlatformEnvironment * env) :
|
||||
explicit ObjectFileIndex(IObjectRepository& objectRepository, IPlatformEnvironment * env) :
|
||||
FileIndex("object index",
|
||||
MAGIC_NUMBER,
|
||||
VERSION,
|
||||
@@ -92,7 +94,8 @@ public:
|
||||
std::string(PATTERN),
|
||||
std::vector<std::string>({
|
||||
env->GetDirectoryPath(DIRBASE::OPENRCT2, DIRID::OBJECT),
|
||||
env->GetDirectoryPath(DIRBASE::USER, DIRID::OBJECT) }))
|
||||
env->GetDirectoryPath(DIRBASE::USER, DIRID::OBJECT) })),
|
||||
_objectRepository(objectRepository)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -102,7 +105,7 @@ public:
|
||||
auto extension = Path::GetExtension(path);
|
||||
if (String::Equals(extension, ".json", true))
|
||||
{
|
||||
auto object = ObjectFactory::CreateObjectFromJsonFile(path);
|
||||
auto object = ObjectFactory::CreateObjectFromJsonFile(_objectRepository, path);
|
||||
if (object != nullptr)
|
||||
{
|
||||
ObjectRepositoryItem item = { 0 };
|
||||
@@ -116,7 +119,7 @@ public:
|
||||
}
|
||||
else
|
||||
{
|
||||
auto object = ObjectFactory::CreateObjectFromLegacyFile(path.c_str());
|
||||
auto object = ObjectFactory::CreateObjectFromLegacyFile(_objectRepository, path.c_str());
|
||||
if (object != nullptr)
|
||||
{
|
||||
ObjectRepositoryItem item = { 0 };
|
||||
@@ -213,7 +216,7 @@ class ObjectRepository final : public IObjectRepository
|
||||
public:
|
||||
explicit ObjectRepository(IPlatformEnvironment * env)
|
||||
: _env(env),
|
||||
_fileIndex(env)
|
||||
_fileIndex(*this, env)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -279,11 +282,11 @@ public:
|
||||
auto extension = Path::GetExtension(ori->Path);
|
||||
if (String::Equals(extension, ".json", true))
|
||||
{
|
||||
return ObjectFactory::CreateObjectFromJsonFile(ori->Path);
|
||||
return ObjectFactory::CreateObjectFromJsonFile(*this, ori->Path);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ObjectFactory::CreateObjectFromLegacyFile(ori->Path);
|
||||
return ObjectFactory::CreateObjectFromLegacyFile(*this, ori->Path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -310,7 +313,7 @@ public:
|
||||
object_entry_get_name_fixed(objectName, sizeof(objectName), objectEntry);
|
||||
|
||||
// Check that the object is loadable before writing it
|
||||
Object * object = ObjectFactory::CreateObjectFromLegacyData(objectEntry, data, dataSize);
|
||||
Object * object = ObjectFactory::CreateObjectFromLegacyData(*this, objectEntry, data, dataSize);
|
||||
if (object == nullptr)
|
||||
{
|
||||
Console::Error::WriteLine("[%s] Unable to export object.", objectName);
|
||||
@@ -659,18 +662,6 @@ const rct_object_entry * object_list_find(rct_object_entry * entry)
|
||||
return result;
|
||||
}
|
||||
|
||||
const rct_object_entry * object_list_find_by_name(const char * name)
|
||||
{
|
||||
const rct_object_entry * result = nullptr;
|
||||
auto objRepo = GetContext()->GetObjectRepository();
|
||||
auto item = objRepo->FindObject(name);
|
||||
if (item != nullptr)
|
||||
{
|
||||
result = &item->ObjectEntry;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void object_list_load()
|
||||
{
|
||||
auto context = GetContext();
|
||||
|
||||
@@ -551,10 +551,11 @@ private:
|
||||
std::vector<const char *> objects = RCT1::GetSceneryObjects(sceneryTheme);
|
||||
for (const char * objectName : objects)
|
||||
{
|
||||
const rct_object_entry * foundEntry = object_list_find_by_name(objectName);
|
||||
if (foundEntry != nullptr)
|
||||
auto objectRepository = OpenRCT2::GetContext()->GetObjectRepository();
|
||||
auto foundObject = objectRepository->FindObject(objectName);
|
||||
if (foundObject != nullptr)
|
||||
{
|
||||
uint8 objectType = object_entry_get_type(foundEntry);
|
||||
uint8 objectType = object_entry_get_type(&foundObject->ObjectEntry);
|
||||
switch (objectType) {
|
||||
case OBJECT_TYPE_SMALL_SCENERY:
|
||||
case OBJECT_TYPE_LARGE_SCENERY:
|
||||
|
||||
Reference in New Issue
Block a user