1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-10 09:32:29 +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");
}
IObjectRepository& GetObjectRepository() override
{
throw std::runtime_error("Not implemented");
}
bool ShouldLoadImages() override
{
return true;

View File

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

View File

@@ -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();
}

View File

@@ -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<uint8_t> GetData(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
{
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<Object> 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<Object> CreateObjectFromFile(IObjectRepository& objectRepository, u8string_view path, bool loadImages)
std::unique_ptr<Object> CreateObjectFromFile(u8string_view path, bool loadImages)
{
std::unique_ptr<Object> 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<Object> CreateObjectFromLegacyFile(IObjectRepository& objectRepository, const utf8* path, bool loadImages)
std::unique_ptr<Object> 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<Object> CreateObjectFromLegacyData(
IObjectRepository& objectRepository, const RCTObjectEntry* entry, const void* data, size_t dataSize)
std::unique_ptr<Object> 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<Object> CreateObjectFromZipFile(IObjectRepository& objectRepository, std::string_view path, bool loadImages)
std::unique_ptr<Object> 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<Object> CreateObjectFromJsonFile(
IObjectRepository& objectRepository, const std::string& path, bool loadImages)
std::unique_ptr<Object> 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<Object> 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())
{

View File

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

View File

@@ -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<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,
// 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
{
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
@@ -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);