mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-27 00:34:46 +01:00
Implement cache of loaded ObjData files
This commit is contained in:
committed by
GitHub
parent
34c7bed26b
commit
342e3d7e09
@@ -30,6 +30,8 @@
|
||||
using namespace OpenRCT2;
|
||||
using namespace OpenRCT2::Drawing;
|
||||
|
||||
static thread_local std::map<u8string, std::unique_ptr<Object>> _objDataCache = {};
|
||||
|
||||
struct ImageTable::RequiredImage
|
||||
{
|
||||
rct_g1_element g1{};
|
||||
@@ -294,12 +296,25 @@ std::vector<std::unique_ptr<ImageTable::RequiredImage>> ImageTable::LoadObjectIm
|
||||
IReadObjectContext* context, const std::string& name, const std::vector<int32_t>& range)
|
||||
{
|
||||
std::vector<std::unique_ptr<RequiredImage>> result;
|
||||
auto objectPath = FindLegacyObject(name);
|
||||
auto obj = ObjectFactory::CreateObjectFromLegacyFile(
|
||||
context->GetObjectRepository(), objectPath.c_str(), !gOpenRCT2NoGraphics);
|
||||
Object* obj;
|
||||
|
||||
auto cached = _objDataCache.find(name);
|
||||
if (cached != _objDataCache.end())
|
||||
{
|
||||
obj = cached->second.get();
|
||||
}
|
||||
else
|
||||
{
|
||||
auto objectPath = FindLegacyObject(name);
|
||||
auto tmp = ObjectFactory::CreateObjectFromLegacyFile(
|
||||
context->GetObjectRepository(), objectPath.c_str(), !gOpenRCT2NoGraphics);
|
||||
auto inserted = _objDataCache.insert({ name, std::move(tmp) });
|
||||
obj = inserted.first->second.get();
|
||||
}
|
||||
|
||||
if (obj != nullptr)
|
||||
{
|
||||
auto& imgTable = static_cast<const Object*>(obj.get())->GetImageTable();
|
||||
auto& imgTable = static_cast<const Object*>(obj)->GetImageTable();
|
||||
auto numImages = static_cast<int32_t>(imgTable.GetCount());
|
||||
auto images = imgTable.GetImages();
|
||||
size_t placeHoldersAdded = 0;
|
||||
@@ -326,7 +341,7 @@ std::vector<std::unique_ptr<ImageTable::RequiredImage>> ImageTable::LoadObjectIm
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string msg = "Unable to open '" + objectPath + "'";
|
||||
std::string msg = "Unable to open '" + name + "'";
|
||||
context->LogWarning(ObjectError::InvalidProperty, msg.c_str());
|
||||
for (size_t i = 0; i < range.size(); i++)
|
||||
{
|
||||
@@ -588,6 +603,8 @@ bool ImageTable::ReadJson(IReadObjectContext* context, json_t& root)
|
||||
}
|
||||
}
|
||||
|
||||
_objDataCache.clear();
|
||||
|
||||
return usesFallbackSprites;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user