mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-16 11:33:03 +01:00
Create functions to load objects by JSON id
This commit is contained in:
@@ -47,6 +47,28 @@
|
||||
|
||||
using namespace OpenRCT2;
|
||||
|
||||
struct ObjectIdentifierHash
|
||||
{
|
||||
size_t operator()(const std::string_view& identifier) const
|
||||
{
|
||||
std::string copy = identifier.data();
|
||||
uint32_t hash = 5381;
|
||||
for (auto i : copy)
|
||||
{
|
||||
hash = ((hash << 5) + hash) + i;
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
};
|
||||
|
||||
struct ObjectIdentifierEqual
|
||||
{
|
||||
bool operator()(const std::string_view& lhs, const std::string_view& rhs) const
|
||||
{
|
||||
return lhs == rhs;
|
||||
}
|
||||
};
|
||||
|
||||
struct ObjectEntryHash
|
||||
{
|
||||
size_t operator()(const rct_object_entry& entry) const
|
||||
@@ -68,6 +90,7 @@ struct ObjectEntryEqual
|
||||
}
|
||||
};
|
||||
|
||||
using ObjectIdentifierMap = std::unordered_map<std::string, size_t, ObjectIdentifierHash, ObjectIdentifierEqual>;
|
||||
using ObjectEntryMap = std::unordered_map<rct_object_entry, size_t, ObjectEntryHash, ObjectEntryEqual>;
|
||||
|
||||
class ObjectFileIndex final : public FileIndex<ObjectRepositoryItem>
|
||||
@@ -230,6 +253,7 @@ class ObjectRepository final : public IObjectRepository
|
||||
std::shared_ptr<IPlatformEnvironment> const _env;
|
||||
ObjectFileIndex const _fileIndex;
|
||||
std::vector<ObjectRepositoryItem> _items;
|
||||
ObjectIdentifierMap _newItemMap;
|
||||
ObjectEntryMap _itemMap;
|
||||
|
||||
public:
|
||||
@@ -269,7 +293,7 @@ public:
|
||||
return _items.data();
|
||||
}
|
||||
|
||||
const ObjectRepositoryItem* FindObject(const std::string_view& legacyIdentifier) const override
|
||||
const ObjectRepositoryItem* FindObjectLegacy(const std::string_view& legacyIdentifier) const override
|
||||
{
|
||||
rct_object_entry entry = {};
|
||||
entry.SetName(legacyIdentifier);
|
||||
@@ -282,6 +306,16 @@ public:
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const ObjectRepositoryItem* FindObject(const std::string_view& identifier) const override final
|
||||
{
|
||||
auto kvp = _newItemMap.find(identifier.data());
|
||||
if (kvp != _newItemMap.end())
|
||||
{
|
||||
return &_items[kvp->second];
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const ObjectRepositoryItem* FindObject(const rct_object_entry* objectEntry) const override final
|
||||
{
|
||||
auto kvp = _itemMap.find(*objectEntry);
|
||||
@@ -411,6 +445,7 @@ private:
|
||||
void ClearItems()
|
||||
{
|
||||
_items.clear();
|
||||
_newItemMap.clear();
|
||||
_itemMap.clear();
|
||||
}
|
||||
|
||||
@@ -428,10 +463,15 @@ private:
|
||||
|
||||
// Rebuild item map
|
||||
_itemMap.clear();
|
||||
_newItemMap.clear();
|
||||
for (size_t i = 0; i < _items.size(); i++)
|
||||
{
|
||||
rct_object_entry entry = _items[i].ObjectEntry;
|
||||
_itemMap[entry] = i;
|
||||
if (_items[i].Identifier != "")
|
||||
{
|
||||
_newItemMap[_items[i].Identifier] = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -460,6 +500,10 @@ private:
|
||||
auto copy = item;
|
||||
copy.Id = index;
|
||||
_items.push_back(copy);
|
||||
if (item.Identifier != "")
|
||||
{
|
||||
_newItemMap[item.Identifier] = index;
|
||||
}
|
||||
_itemMap[item.ObjectEntry] = index;
|
||||
return true;
|
||||
}
|
||||
@@ -749,7 +793,7 @@ const ObjectRepositoryItem* object_repository_find_object_by_entry(const rct_obj
|
||||
const ObjectRepositoryItem* object_repository_find_object_by_name(const char* name)
|
||||
{
|
||||
auto& objectRepository = GetContext()->GetObjectRepository();
|
||||
return objectRepository.FindObject(name);
|
||||
return objectRepository.FindObjectLegacy(name);
|
||||
}
|
||||
|
||||
bool object_entry_compare(const rct_object_entry* a, const rct_object_entry* b)
|
||||
|
||||
Reference in New Issue
Block a user