mirror of
https://github.com/Jacobwasbeast/LegacyWeaveLoader.git
synced 2026-08-02 23:51:15 +00:00
101 lines
2.9 KiB
C++
101 lines
2.9 KiB
C++
#include "IdRegistry.h"
|
|
#include "LogUtil.h"
|
|
|
|
IdRegistry& IdRegistry::Instance()
|
|
{
|
|
static IdRegistry instance;
|
|
return instance;
|
|
}
|
|
|
|
IdRegistry::IdRegistry()
|
|
{
|
|
m_registries[static_cast<int>(Type::Block)].nextFreeId = BLOCK_MOD_START;
|
|
m_registries[static_cast<int>(Type::Item)].nextFreeId = ITEM_MOD_START;
|
|
m_registries[static_cast<int>(Type::Entity)].nextFreeId = ENTITY_MOD_START;
|
|
}
|
|
|
|
int IdRegistry::Register(Type type, const std::string& namespacedId)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
auto& reg = m_registries[static_cast<int>(type)];
|
|
|
|
auto it = reg.stringToNum.find(namespacedId);
|
|
if (it != reg.stringToNum.end())
|
|
return it->second;
|
|
|
|
int maxId;
|
|
switch (type)
|
|
{
|
|
case Type::Block: maxId = BLOCK_MAX; break;
|
|
case Type::Item: maxId = ITEM_MAX; break;
|
|
case Type::Entity: maxId = ENTITY_MAX; break;
|
|
default: return -1;
|
|
}
|
|
|
|
if (reg.nextFreeId > maxId)
|
|
{
|
|
LogUtil::Log("[WeaveLoader] IdRegistry: No free IDs for type %d (max %d)",
|
|
static_cast<int>(type), maxId);
|
|
return -1;
|
|
}
|
|
|
|
int id = reg.nextFreeId++;
|
|
|
|
while (reg.numToString.count(id) && id <= maxId)
|
|
id = reg.nextFreeId++;
|
|
|
|
if (id > maxId) return -1;
|
|
|
|
reg.stringToNum[namespacedId] = id;
|
|
reg.numToString[id] = namespacedId;
|
|
|
|
return id;
|
|
}
|
|
|
|
int IdRegistry::GetNumericId(Type type, const std::string& namespacedId) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
const auto& reg = m_registries[static_cast<int>(type)];
|
|
auto it = reg.stringToNum.find(namespacedId);
|
|
return (it != reg.stringToNum.end()) ? it->second : -1;
|
|
}
|
|
|
|
std::string IdRegistry::GetStringId(Type type, int numericId) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
const auto& reg = m_registries[static_cast<int>(type)];
|
|
auto it = reg.numToString.find(numericId);
|
|
return (it != reg.numToString.end()) ? it->second : "";
|
|
}
|
|
|
|
void IdRegistry::RegisterVanilla(Type type, int numericId, const std::string& namespacedId)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
auto& reg = m_registries[static_cast<int>(type)];
|
|
reg.stringToNum[namespacedId] = numericId;
|
|
reg.numToString[numericId] = namespacedId;
|
|
}
|
|
|
|
std::vector<std::pair<int, std::string>> IdRegistry::GetEntries(Type type) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
std::vector<std::pair<int, std::string>> entries;
|
|
const auto& reg = m_registries[static_cast<int>(type)];
|
|
entries.reserve(reg.numToString.size());
|
|
for (const auto& it : reg.numToString)
|
|
entries.push_back(it);
|
|
return entries;
|
|
}
|
|
|
|
void IdRegistry::SetMissingFallback(Type type, int numericId)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_registries[static_cast<int>(type)].missingFallbackId = numericId;
|
|
}
|
|
|
|
int IdRegistry::GetMissingFallback(Type type) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
return m_registries[static_cast<int>(type)].missingFallbackId;
|
|
}
|