1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-12 02:22:26 +01:00

Remove repository from IReadObjectContext, ObjectFileIndex and helper functions

This commit is contained in:
Gymnasiast
2025-09-21 01:33:15 +02:00
parent f768d2873e
commit 04af97ec74
7 changed files with 28 additions and 55 deletions

View File

@@ -122,11 +122,6 @@ namespace OpenRCT2
throw std::runtime_error("Not implemented"); throw std::runtime_error("Not implemented");
} }
IObjectRepository& GetObjectRepository() override
{
throw std::runtime_error("Not implemented");
}
bool ShouldLoadImages() override bool ShouldLoadImages() override
{ {
return true; return true;

View File

@@ -584,8 +584,7 @@ namespace OpenRCT2
auto context = CreateContext(); auto context = CreateContext();
context->Initialise(); context->Initialise();
auto& objectRepository = GetContext()->GetObjectRepository(); std::unique_ptr<Object> metaObject = OpenRCT2::ObjectFactory::CreateObjectFromFile(objectPath, true);
std::unique_ptr<Object> metaObject = OpenRCT2::ObjectFactory::CreateObjectFromFile(objectRepository, objectPath, true);
if (metaObject == nullptr) if (metaObject == nullptr)
{ {
fprintf(stderr, "Could not load the object.\n"); fprintf(stderr, "Could not load the object.\n");

View File

@@ -298,8 +298,7 @@ namespace OpenRCT2
else else
{ {
auto objectPath = FindLegacyObject(name); auto objectPath = FindLegacyObject(name);
auto tmp = ObjectFactory::CreateObjectFromLegacyFile( auto tmp = ObjectFactory::CreateObjectFromLegacyFile(objectPath.c_str(), !gOpenRCT2NoGraphics);
context->GetObjectRepository(), objectPath.c_str(), !gOpenRCT2NoGraphics);
auto inserted = _objDataCache.insert({ name, std::move(tmp) }); auto inserted = _objDataCache.insert({ name, std::move(tmp) });
obj = inserted.first->second.get(); obj = inserted.first->second.get();
} }

View File

@@ -168,7 +168,6 @@ namespace OpenRCT2
virtual ~IReadObjectContext() = default; virtual ~IReadObjectContext() = default;
virtual std::string_view GetObjectIdentifier() = 0; virtual std::string_view GetObjectIdentifier() = 0;
virtual IObjectRepository& GetObjectRepository() = 0;
virtual bool ShouldLoadImages() = 0; virtual bool ShouldLoadImages() = 0;
virtual std::vector<uint8_t> GetData(std::string_view path) = 0; virtual std::vector<uint8_t> GetData(std::string_view path) = 0;
virtual ObjectAsset GetAsset(std::string_view path) = 0; virtual ObjectAsset GetAsset(std::string_view path) = 0;

View File

@@ -122,7 +122,6 @@ namespace OpenRCT2
class ReadObjectContext : public IReadObjectContext class ReadObjectContext : public IReadObjectContext
{ {
private: private:
IObjectRepository& _objectRepository;
const IFileDataRetriever* _fileDataRetriever; const IFileDataRetriever* _fileDataRetriever;
std::string _identifier; std::string _identifier;
@@ -146,11 +145,8 @@ namespace OpenRCT2
return _wasError; return _wasError;
} }
ReadObjectContext( ReadObjectContext(const std::string& identifier, bool loadImages, const IFileDataRetriever* fileDataRetriever)
IObjectRepository& objectRepository, const std::string& identifier, bool loadImages, : _fileDataRetriever(fileDataRetriever)
const IFileDataRetriever* fileDataRetriever)
: _objectRepository(objectRepository)
, _fileDataRetriever(fileDataRetriever)
, _identifier(identifier) , _identifier(identifier)
, _loadImages(loadImages) , _loadImages(loadImages)
{ {
@@ -161,11 +157,6 @@ namespace OpenRCT2
return _identifier; return _identifier;
} }
IObjectRepository& GetObjectRepository() override
{
return _objectRepository;
}
bool ShouldLoadImages() override bool ShouldLoadImages() override
{ {
return _loadImages; return _loadImages;
@@ -228,8 +219,7 @@ namespace OpenRCT2::ObjectFactory
* @note jRoot is deliberately left non-const: json_t behaviour changes when const * @note jRoot is deliberately left non-const: json_t behaviour changes when const
*/ */
static std::unique_ptr<Object> CreateObjectFromJson( static std::unique_ptr<Object> CreateObjectFromJson(
IObjectRepository& objectRepository, json_t& jRoot, const IFileDataRetriever* fileRetriever, bool loadImageTable, json_t& jRoot, const IFileDataRetriever* fileRetriever, bool loadImageTable, const std::string_view path);
const std::string_view path);
static ObjectSourceGame ParseSourceGame(const std::string_view s) static ObjectSourceGame ParseSourceGame(const std::string_view s)
{ {
@@ -264,29 +254,29 @@ namespace OpenRCT2::ObjectFactory
} }
} }
std::unique_ptr<Object> CreateObjectFromFile(IObjectRepository& objectRepository, u8string_view path, bool loadImages) std::unique_ptr<Object> CreateObjectFromFile(u8string_view path, bool loadImages)
{ {
std::unique_ptr<Object> object; std::unique_ptr<Object> object;
auto extension = Path::GetExtension(path); auto extension = Path::GetExtension(path);
if (String::iequals(extension, ".json")) if (String::iequals(extension, ".json"))
{ {
auto pathStr = u8string(path); auto pathStr = u8string(path);
object = ObjectFactory::CreateObjectFromJsonFile(objectRepository, pathStr, loadImages); object = ObjectFactory::CreateObjectFromJsonFile(pathStr, loadImages);
} }
else if (String::iequals(extension, ".parkobj")) else if (String::iequals(extension, ".parkobj"))
{ {
object = ObjectFactory::CreateObjectFromZipFile(objectRepository, path, loadImages); object = ObjectFactory::CreateObjectFromZipFile(path, loadImages);
} }
else else
{ {
auto pathStr = u8string(path); auto pathStr = u8string(path);
object = ObjectFactory::CreateObjectFromLegacyFile(objectRepository, pathStr.c_str(), loadImages); object = ObjectFactory::CreateObjectFromLegacyFile(pathStr.c_str(), loadImages);
} }
return object; return object;
} }
std::unique_ptr<Object> CreateObjectFromLegacyFile(IObjectRepository& objectRepository, const utf8* path, bool loadImages) std::unique_ptr<Object> CreateObjectFromLegacyFile(const utf8* path, bool loadImages)
{ {
LOG_VERBOSE("CreateObjectFromLegacyFile(..., \"%s\")", path); LOG_VERBOSE("CreateObjectFromLegacyFile(..., \"%s\")", path);
@@ -312,7 +302,7 @@ namespace OpenRCT2::ObjectFactory
LOG_VERBOSE(" size: %zu", chunk->GetLength()); LOG_VERBOSE(" size: %zu", chunk->GetLength());
auto chunkStream = OpenRCT2::MemoryStream(chunk->GetData(), 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); ReadObjectLegacy(*result, &readContext, &chunkStream);
if (readContext.WasError()) if (readContext.WasError())
{ {
@@ -328,8 +318,7 @@ namespace OpenRCT2::ObjectFactory
return result; return result;
} }
std::unique_ptr<Object> CreateObjectFromLegacyData( std::unique_ptr<Object> CreateObjectFromLegacyData(const RCTObjectEntry* entry, const void* data, size_t dataSize)
IObjectRepository& objectRepository, const RCTObjectEntry* entry, const void* data, size_t dataSize)
{ {
Guard::ArgumentNotNull(entry, GUARD_LINE); Guard::ArgumentNotNull(entry, GUARD_LINE);
Guard::ArgumentNotNull(data, GUARD_LINE); Guard::ArgumentNotNull(data, GUARD_LINE);
@@ -342,7 +331,7 @@ namespace OpenRCT2::ObjectFactory
utf8 objectName[kDatNameLength + 1]; utf8 objectName[kDatNameLength + 1];
ObjectEntryGetNameFixed(objectName, sizeof(objectName), entry); ObjectEntryGetNameFixed(objectName, sizeof(objectName), entry);
auto readContext = ReadObjectContext(objectRepository, objectName, !gOpenRCT2NoGraphics, nullptr); auto readContext = ReadObjectContext(objectName, !gOpenRCT2NoGraphics, nullptr);
auto chunkStream = OpenRCT2::MemoryStream(data, dataSize); auto chunkStream = OpenRCT2::MemoryStream(data, dataSize);
ReadObjectLegacy(*result, &readContext, &chunkStream); ReadObjectLegacy(*result, &readContext, &chunkStream);
@@ -456,7 +445,7 @@ namespace OpenRCT2::ObjectFactory
{ "climate", ObjectType::climate }, { "climate", ObjectType::climate },
}; };
std::unique_ptr<Object> CreateObjectFromZipFile(IObjectRepository& objectRepository, std::string_view path, bool loadImages) std::unique_ptr<Object> CreateObjectFromZipFile(std::string_view path, bool loadImages)
{ {
try try
{ {
@@ -472,7 +461,7 @@ namespace OpenRCT2::ObjectFactory
if (jRoot.is_object()) if (jRoot.is_object())
{ {
auto fileDataRetriever = ZipDataRetriever(path, *archive); auto fileDataRetriever = ZipDataRetriever(path, *archive);
return CreateObjectFromJson(objectRepository, jRoot, &fileDataRetriever, loadImages, path); return CreateObjectFromJson(jRoot, &fileDataRetriever, loadImages, path);
} }
} }
catch (const std::exception& e) catch (const std::exception& e)
@@ -482,8 +471,7 @@ namespace OpenRCT2::ObjectFactory
return nullptr; return nullptr;
} }
std::unique_ptr<Object> CreateObjectFromJsonFile( std::unique_ptr<Object> CreateObjectFromJsonFile(const std::string& path, bool loadImages)
IObjectRepository& objectRepository, const std::string& path, bool loadImages)
{ {
LOG_VERBOSE("CreateObjectFromJsonFile(\"%s\")", path.c_str()); LOG_VERBOSE("CreateObjectFromJsonFile(\"%s\")", path.c_str());
@@ -491,7 +479,7 @@ namespace OpenRCT2::ObjectFactory
{ {
json_t jRoot = Json::ReadFromFile(path.c_str()); json_t jRoot = Json::ReadFromFile(path.c_str());
auto fileDataRetriever = FileSystemDataRetriever(Path::GetDirectory(path)); 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) catch (const std::runtime_error& err)
{ {
@@ -540,8 +528,7 @@ namespace OpenRCT2::ObjectFactory
} }
std::unique_ptr<Object> CreateObjectFromJson( std::unique_ptr<Object> CreateObjectFromJson(
IObjectRepository& objectRepository, json_t& jRoot, const IFileDataRetriever* fileRetriever, bool loadImageTable, json_t& jRoot, const IFileDataRetriever* fileRetriever, bool loadImageTable, const std::string_view path)
const std::string_view path)
{ {
if (!jRoot.is_object()) if (!jRoot.is_object())
{ {
@@ -602,7 +589,7 @@ namespace OpenRCT2::ObjectFactory
result->SetDescriptor(descriptor); result->SetDescriptor(descriptor);
result->SetFileName(OpenRCT2::Path::GetFileNameWithoutExtension(path)); result->SetFileName(OpenRCT2::Path::GetFileNameWithoutExtension(path));
result->MarkAsJsonObject(); result->MarkAsJsonObject();
auto readContext = ReadObjectContext(objectRepository, id, loadImageTable, fileRetriever); auto readContext = ReadObjectContext(id, loadImageTable, fileRetriever);
result->ReadJson(&readContext, jRoot); result->ReadJson(&readContext, jRoot);
if (readContext.WasError()) if (readContext.WasError())
{ {

View File

@@ -24,16 +24,12 @@ namespace OpenRCT2
namespace OpenRCT2::ObjectFactory namespace OpenRCT2::ObjectFactory
{ {
[[nodiscard]] std::unique_ptr<Object> CreateObjectFromFile( [[nodiscard]] std::unique_ptr<Object> CreateObjectFromFile(u8string_view path, bool loadImages);
IObjectRepository& objectRepository, u8string_view path, bool loadImages); [[nodiscard]] std::unique_ptr<Object> CreateObjectFromLegacyFile(const utf8* path, bool loadImages);
[[nodiscard]] std::unique_ptr<Object> CreateObjectFromLegacyFile(
IObjectRepository& objectRepository, const utf8* path, bool loadImages);
[[nodiscard]] std::unique_ptr<Object> CreateObjectFromLegacyData( [[nodiscard]] std::unique_ptr<Object> CreateObjectFromLegacyData(
IObjectRepository& objectRepository, const RCTObjectEntry* entry, const void* data, size_t dataSize); const RCTObjectEntry* entry, const void* data, size_t dataSize);
[[nodiscard]] std::unique_ptr<Object> CreateObjectFromZipFile( [[nodiscard]] std::unique_ptr<Object> CreateObjectFromZipFile(std::string_view path, bool loadImages);
IObjectRepository& objectRepository, std::string_view path, bool loadImages);
[[nodiscard]] std::unique_ptr<Object> CreateObject(ObjectType type); [[nodiscard]] std::unique_ptr<Object> CreateObject(ObjectType type);
[[nodiscard]] std::unique_ptr<Object> CreateObjectFromJsonFile( [[nodiscard]] std::unique_ptr<Object> CreateObjectFromJsonFile(const std::string& path, bool loadImages);
IObjectRepository& objectRepository, const std::string& path, bool loadImages);
} // namespace OpenRCT2::ObjectFactory } // namespace OpenRCT2::ObjectFactory

View File

@@ -81,8 +81,6 @@ namespace OpenRCT2
static constexpr uint16_t kVersion = 31; static constexpr uint16_t kVersion = 31;
static constexpr auto kPattern = "*.dat;*.pob;*.json;*.parkobj"; static constexpr auto kPattern = "*.dat;*.pob;*.json;*.parkobj";
IObjectRepository& _objectRepository;
public: public:
explicit ObjectFileIndex(IObjectRepository& objectRepository, const IPlatformEnvironment& env) explicit ObjectFileIndex(IObjectRepository& objectRepository, const IPlatformEnvironment& env)
: FileIndex( : FileIndex(
@@ -91,14 +89,13 @@ namespace OpenRCT2
env.GetDirectoryPath(DirBase::openrct2, DirId::objects), env.GetDirectoryPath(DirBase::openrct2, DirId::objects),
env.GetDirectoryPath(DirBase::user, DirId::objects), env.GetDirectoryPath(DirBase::user, DirId::objects),
}) })
, _objectRepository(objectRepository)
{ {
} }
public: public:
std::optional<ObjectRepositoryItem> Create([[maybe_unused]] int32_t language, const std::string& path) const override std::optional<ObjectRepositoryItem> Create([[maybe_unused]] int32_t language, const std::string& path) const override
{ {
std::unique_ptr<Object> object = ObjectFactory::CreateObjectFromFile(_objectRepository, path, false); std::unique_ptr<Object> object = ObjectFactory::CreateObjectFromFile(path, false);
// All official DAT files have a JSON object counterpart. Avoid loading the obsolete .DAT versions, // 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. // 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<Object> LoadObject(const ObjectRepositoryItem* ori) override std::unique_ptr<Object> LoadObject(const ObjectRepositoryItem* ori) override
{ {
Guard::ArgumentNotNull(ori, GUARD_LINE); 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>&& object) override void RegisterLoadedObject(const ObjectRepositoryItem* ori, std::unique_ptr<Object>&& object) override
@@ -284,7 +282,7 @@ namespace OpenRCT2
ObjectEntryGetNameFixed(objectName, sizeof(objectName), objectEntry); ObjectEntryGetNameFixed(objectName, sizeof(objectName), objectEntry);
// Check that the object is loadable before writing it // 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) if (object == nullptr)
{ {
Console::Error::WriteLine("[%s] Unable to export object.", objectName); Console::Error::WriteLine("[%s] Unable to export object.", objectName);