diff --git a/src/openrct2/AssetPack.cpp b/src/openrct2/AssetPack.cpp index e7c2ec07ca..c843cf652a 100644 --- a/src/openrct2/AssetPack.cpp +++ b/src/openrct2/AssetPack.cpp @@ -122,11 +122,6 @@ namespace OpenRCT2 throw std::runtime_error("Not implemented"); } - IObjectRepository& GetObjectRepository() override - { - throw std::runtime_error("Not implemented"); - } - bool ShouldLoadImages() override { return true; diff --git a/src/openrct2/CommandLineSprite.cpp b/src/openrct2/CommandLineSprite.cpp index eb87ebaad2..73a105a034 100644 --- a/src/openrct2/CommandLineSprite.cpp +++ b/src/openrct2/CommandLineSprite.cpp @@ -584,8 +584,7 @@ namespace OpenRCT2 auto context = CreateContext(); context->Initialise(); - auto& objectRepository = GetContext()->GetObjectRepository(); - std::unique_ptr metaObject = OpenRCT2::ObjectFactory::CreateObjectFromFile(objectRepository, objectPath, true); + std::unique_ptr metaObject = OpenRCT2::ObjectFactory::CreateObjectFromFile(objectPath, true); if (metaObject == nullptr) { fprintf(stderr, "Could not load the object.\n"); diff --git a/src/openrct2/object/BannerObject.cpp b/src/openrct2/object/BannerObject.cpp index 873b33504e..5118dfa346 100644 --- a/src/openrct2/object/BannerObject.cpp +++ b/src/openrct2/object/BannerObject.cpp @@ -44,18 +44,11 @@ namespace OpenRCT2 // 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. - - auto& objectRepository = context->GetObjectRepository(); - auto item = objectRepository.FindObject(GetDescriptor()); - if (item != nullptr) + auto firstSourceGame = GetFirstSourceGame(); + if (firstSourceGame == ObjectSourceGame::Custom) { - auto sourceGame = item->GetFirstSourceGame(); - if (sourceGame == ObjectSourceGame::WackyWorlds || sourceGame == ObjectSourceGame::TimeTwister - || sourceGame == ObjectSourceGame::Custom) - { - auto scgPathX = Object::GetScgPathXHeader(); - SetPrimarySceneryGroup(scgPathX); - } + auto scgPathX = Object::GetScgPathXHeader(); + SetPrimarySceneryGroup(scgPathX); } } diff --git a/src/openrct2/object/ImageTable.cpp b/src/openrct2/object/ImageTable.cpp index cb0ba7631f..d4630b444a 100644 --- a/src/openrct2/object/ImageTable.cpp +++ b/src/openrct2/object/ImageTable.cpp @@ -298,8 +298,7 @@ namespace OpenRCT2 else { auto objectPath = FindLegacyObject(name); - auto tmp = ObjectFactory::CreateObjectFromLegacyFile( - context->GetObjectRepository(), objectPath.c_str(), !gOpenRCT2NoGraphics); + auto tmp = ObjectFactory::CreateObjectFromLegacyFile(objectPath.c_str(), !gOpenRCT2NoGraphics); auto inserted = _objDataCache.insert({ name, std::move(tmp) }); obj = inserted.first->second.get(); } diff --git a/src/openrct2/object/Object.cpp b/src/openrct2/object/Object.cpp index 0707764277..b4cac59e9b 100644 --- a/src/openrct2/object/Object.cpp +++ b/src/openrct2/object/Object.cpp @@ -192,6 +192,14 @@ namespace OpenRCT2 return _sourceGames; } + ObjectSourceGame Object::GetFirstSourceGame() const + { + if (_sourceGames.size() == 0) + return ObjectSourceGame::Custom; + + return _sourceGames[0]; + } + void Object::SetSourceGames(const std::vector& sourceGames) { _sourceGames = sourceGames; diff --git a/src/openrct2/object/Object.h b/src/openrct2/object/Object.h index 1143a8f42f..568c550cea 100644 --- a/src/openrct2/object/Object.h +++ b/src/openrct2/object/Object.h @@ -168,7 +168,6 @@ namespace OpenRCT2 virtual ~IReadObjectContext() = default; virtual std::string_view GetObjectIdentifier() = 0; - virtual IObjectRepository& GetObjectRepository() = 0; virtual bool ShouldLoadImages() = 0; virtual std::vector GetData(std::string_view path) = 0; virtual ObjectAsset GetAsset(std::string_view path) = 0; @@ -304,6 +303,7 @@ namespace OpenRCT2 { } std::vector GetSourceGames(); + ObjectSourceGame GetFirstSourceGame() const; void SetSourceGames(const std::vector& sourceGames); const std::vector& GetAuthors() const; diff --git a/src/openrct2/object/ObjectFactory.cpp b/src/openrct2/object/ObjectFactory.cpp index 65c1acd24e..c1cc83dba9 100644 --- a/src/openrct2/object/ObjectFactory.cpp +++ b/src/openrct2/object/ObjectFactory.cpp @@ -122,7 +122,6 @@ namespace OpenRCT2 class ReadObjectContext : public IReadObjectContext { private: - IObjectRepository& _objectRepository; const IFileDataRetriever* _fileDataRetriever; std::string _identifier; @@ -146,11 +145,8 @@ namespace OpenRCT2 return _wasError; } - ReadObjectContext( - IObjectRepository& objectRepository, const std::string& identifier, bool loadImages, - const IFileDataRetriever* fileDataRetriever) - : _objectRepository(objectRepository) - , _fileDataRetriever(fileDataRetriever) + ReadObjectContext(const std::string& identifier, bool loadImages, const IFileDataRetriever* fileDataRetriever) + : _fileDataRetriever(fileDataRetriever) , _identifier(identifier) , _loadImages(loadImages) { @@ -161,11 +157,6 @@ namespace OpenRCT2 return _identifier; } - IObjectRepository& GetObjectRepository() override - { - return _objectRepository; - } - bool ShouldLoadImages() override { return _loadImages; @@ -228,8 +219,7 @@ namespace OpenRCT2::ObjectFactory * @note jRoot is deliberately left non-const: json_t behaviour changes when const */ static std::unique_ptr CreateObjectFromJson( - IObjectRepository& objectRepository, json_t& jRoot, const IFileDataRetriever* fileRetriever, bool loadImageTable, - const std::string_view path); + json_t& jRoot, const IFileDataRetriever* fileRetriever, bool loadImageTable, const std::string_view path); static ObjectSourceGame ParseSourceGame(const std::string_view s) { @@ -264,29 +254,29 @@ namespace OpenRCT2::ObjectFactory } } - std::unique_ptr CreateObjectFromFile(IObjectRepository& objectRepository, u8string_view path, bool loadImages) + std::unique_ptr CreateObjectFromFile(u8string_view path, bool loadImages) { std::unique_ptr object; auto extension = Path::GetExtension(path); if (String::iequals(extension, ".json")) { auto pathStr = u8string(path); - object = ObjectFactory::CreateObjectFromJsonFile(objectRepository, pathStr, loadImages); + object = ObjectFactory::CreateObjectFromJsonFile(pathStr, loadImages); } else if (String::iequals(extension, ".parkobj")) { - object = ObjectFactory::CreateObjectFromZipFile(objectRepository, path, loadImages); + object = ObjectFactory::CreateObjectFromZipFile(path, loadImages); } else { auto pathStr = u8string(path); - object = ObjectFactory::CreateObjectFromLegacyFile(objectRepository, pathStr.c_str(), loadImages); + object = ObjectFactory::CreateObjectFromLegacyFile(pathStr.c_str(), loadImages); } return object; } - std::unique_ptr CreateObjectFromLegacyFile(IObjectRepository& objectRepository, const utf8* path, bool loadImages) + std::unique_ptr CreateObjectFromLegacyFile(const utf8* path, bool loadImages) { LOG_VERBOSE("CreateObjectFromLegacyFile(..., \"%s\")", path); @@ -312,7 +302,7 @@ namespace OpenRCT2::ObjectFactory LOG_VERBOSE(" size: %zu", chunk->GetLength()); auto chunkStream = OpenRCT2::MemoryStream(chunk->GetData(), chunk->GetLength()); - auto readContext = ReadObjectContext(objectRepository, objectName, loadImages, nullptr); + auto readContext = ReadObjectContext(objectName, loadImages, nullptr); ReadObjectLegacy(*result, &readContext, &chunkStream); if (readContext.WasError()) { @@ -328,8 +318,7 @@ namespace OpenRCT2::ObjectFactory return result; } - std::unique_ptr CreateObjectFromLegacyData( - IObjectRepository& objectRepository, const RCTObjectEntry* entry, const void* data, size_t dataSize) + std::unique_ptr CreateObjectFromLegacyData(const RCTObjectEntry* entry, const void* data, size_t dataSize) { Guard::ArgumentNotNull(entry, GUARD_LINE); Guard::ArgumentNotNull(data, GUARD_LINE); @@ -342,7 +331,7 @@ namespace OpenRCT2::ObjectFactory utf8 objectName[kDatNameLength + 1]; ObjectEntryGetNameFixed(objectName, sizeof(objectName), entry); - auto readContext = ReadObjectContext(objectRepository, objectName, !gOpenRCT2NoGraphics, nullptr); + auto readContext = ReadObjectContext(objectName, !gOpenRCT2NoGraphics, nullptr); auto chunkStream = OpenRCT2::MemoryStream(data, dataSize); ReadObjectLegacy(*result, &readContext, &chunkStream); @@ -456,7 +445,7 @@ namespace OpenRCT2::ObjectFactory { "climate", ObjectType::climate }, }; - std::unique_ptr CreateObjectFromZipFile(IObjectRepository& objectRepository, std::string_view path, bool loadImages) + std::unique_ptr CreateObjectFromZipFile(std::string_view path, bool loadImages) { try { @@ -472,7 +461,7 @@ namespace OpenRCT2::ObjectFactory if (jRoot.is_object()) { auto fileDataRetriever = ZipDataRetriever(path, *archive); - return CreateObjectFromJson(objectRepository, jRoot, &fileDataRetriever, loadImages, path); + return CreateObjectFromJson(jRoot, &fileDataRetriever, loadImages, path); } } catch (const std::exception& e) @@ -482,8 +471,7 @@ namespace OpenRCT2::ObjectFactory return nullptr; } - std::unique_ptr CreateObjectFromJsonFile( - IObjectRepository& objectRepository, const std::string& path, bool loadImages) + std::unique_ptr CreateObjectFromJsonFile(const std::string& path, bool loadImages) { LOG_VERBOSE("CreateObjectFromJsonFile(\"%s\")", path.c_str()); @@ -491,7 +479,7 @@ namespace OpenRCT2::ObjectFactory { json_t jRoot = Json::ReadFromFile(path.c_str()); auto fileDataRetriever = FileSystemDataRetriever(Path::GetDirectory(path)); - return CreateObjectFromJson(objectRepository, jRoot, &fileDataRetriever, loadImages, path); + return CreateObjectFromJson(jRoot, &fileDataRetriever, loadImages, path); } catch (const std::runtime_error& err) { @@ -540,8 +528,7 @@ namespace OpenRCT2::ObjectFactory } std::unique_ptr CreateObjectFromJson( - IObjectRepository& objectRepository, json_t& jRoot, const IFileDataRetriever* fileRetriever, bool loadImageTable, - const std::string_view path) + json_t& jRoot, const IFileDataRetriever* fileRetriever, bool loadImageTable, const std::string_view path) { if (!jRoot.is_object()) { @@ -602,7 +589,7 @@ namespace OpenRCT2::ObjectFactory result->SetDescriptor(descriptor); result->SetFileName(OpenRCT2::Path::GetFileNameWithoutExtension(path)); result->MarkAsJsonObject(); - auto readContext = ReadObjectContext(objectRepository, id, loadImageTable, fileRetriever); + auto readContext = ReadObjectContext(id, loadImageTable, fileRetriever); result->ReadJson(&readContext, jRoot); if (readContext.WasError()) { diff --git a/src/openrct2/object/ObjectFactory.h b/src/openrct2/object/ObjectFactory.h index 64f4421cd7..4b2be14cf2 100644 --- a/src/openrct2/object/ObjectFactory.h +++ b/src/openrct2/object/ObjectFactory.h @@ -24,16 +24,12 @@ namespace OpenRCT2 namespace OpenRCT2::ObjectFactory { - [[nodiscard]] std::unique_ptr CreateObjectFromFile( - IObjectRepository& objectRepository, u8string_view path, bool loadImages); - [[nodiscard]] std::unique_ptr CreateObjectFromLegacyFile( - IObjectRepository& objectRepository, const utf8* path, bool loadImages); + [[nodiscard]] std::unique_ptr CreateObjectFromFile(u8string_view path, bool loadImages); + [[nodiscard]] std::unique_ptr CreateObjectFromLegacyFile(const utf8* path, bool loadImages); [[nodiscard]] std::unique_ptr CreateObjectFromLegacyData( - IObjectRepository& objectRepository, const RCTObjectEntry* entry, const void* data, size_t dataSize); - [[nodiscard]] std::unique_ptr CreateObjectFromZipFile( - IObjectRepository& objectRepository, std::string_view path, bool loadImages); + const RCTObjectEntry* entry, const void* data, size_t dataSize); + [[nodiscard]] std::unique_ptr CreateObjectFromZipFile(std::string_view path, bool loadImages); [[nodiscard]] std::unique_ptr CreateObject(ObjectType type); - [[nodiscard]] std::unique_ptr CreateObjectFromJsonFile( - IObjectRepository& objectRepository, const std::string& path, bool loadImages); + [[nodiscard]] std::unique_ptr CreateObjectFromJsonFile(const std::string& path, bool loadImages); } // namespace OpenRCT2::ObjectFactory diff --git a/src/openrct2/object/ObjectRepository.cpp b/src/openrct2/object/ObjectRepository.cpp index fb0d2bb1b1..940a5013d5 100644 --- a/src/openrct2/object/ObjectRepository.cpp +++ b/src/openrct2/object/ObjectRepository.cpp @@ -81,8 +81,6 @@ namespace OpenRCT2 static constexpr uint16_t kVersion = 31; static constexpr auto kPattern = "*.dat;*.pob;*.json;*.parkobj"; - IObjectRepository& _objectRepository; - public: explicit ObjectFileIndex(IObjectRepository& objectRepository, const IPlatformEnvironment& env) : FileIndex( @@ -91,14 +89,13 @@ namespace OpenRCT2 env.GetDirectoryPath(DirBase::openrct2, DirId::objects), env.GetDirectoryPath(DirBase::user, DirId::objects), }) - , _objectRepository(objectRepository) { } public: std::optional Create([[maybe_unused]] int32_t language, const std::string& path) const override { - std::unique_ptr object = ObjectFactory::CreateObjectFromFile(_objectRepository, path, false); + std::unique_ptr object = ObjectFactory::CreateObjectFromFile(path, false); // All official DAT files have a JSON object counterpart. Avoid loading the obsolete .DAT versions, // which can happen if the user copies the official DAT objects to their custom content folder. @@ -258,7 +255,8 @@ namespace OpenRCT2 std::unique_ptr LoadObject(const ObjectRepositoryItem* ori) override { Guard::ArgumentNotNull(ori, GUARD_LINE); - return ObjectFactory::CreateObjectFromFile(*this, ori->Path, !gOpenRCT2NoGraphics); + + return ObjectFactory::CreateObjectFromFile(ori->Path, !gOpenRCT2NoGraphics); } void RegisterLoadedObject(const ObjectRepositoryItem* ori, std::unique_ptr&& object) override @@ -284,7 +282,7 @@ namespace OpenRCT2 ObjectEntryGetNameFixed(objectName, sizeof(objectName), objectEntry); // Check that the object is loadable before writing it - auto object = ObjectFactory::CreateObjectFromLegacyData(*this, objectEntry, data, dataSize); + auto object = ObjectFactory::CreateObjectFromLegacyData(objectEntry, data, dataSize); if (object == nullptr) { Console::Error::WriteLine("[%s] Unable to export object.", objectName); diff --git a/src/openrct2/object/PathAdditionObject.cpp b/src/openrct2/object/PathAdditionObject.cpp index 24f0e6c78d..144d00c1d2 100644 --- a/src/openrct2/object/PathAdditionObject.cpp +++ b/src/openrct2/object/PathAdditionObject.cpp @@ -47,18 +47,11 @@ namespace OpenRCT2 // Add path additions 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. - - auto& objectRepository = context->GetObjectRepository(); - auto item = objectRepository.FindObject(GetDescriptor()); - if (item != nullptr) + auto firstSourceGame = GetFirstSourceGame(); + if (firstSourceGame == ObjectSourceGame::Custom) { - auto sourceGame = item->GetFirstSourceGame(); - if (sourceGame == ObjectSourceGame::WackyWorlds || sourceGame == ObjectSourceGame::TimeTwister - || sourceGame == ObjectSourceGame::Custom) - { - auto scgPathX = Object::GetScgPathXHeader(); - SetPrimarySceneryGroup(scgPathX); - } + auto scgPathX = Object::GetScgPathXHeader(); + SetPrimarySceneryGroup(scgPathX); } }