Files
LegacyWeaveLoader/LegacyForgeRuntime/src/IdRegistry.cpp
Jacobwasbeast de22a24100 Initial commit: LegacyForge mod loader for Minecraft Legacy Edition
SKSE-style external mod loader with zero game source modifications.
- LegacyForge.Launcher: C# console app that injects runtime DLL into game process
- LegacyForgeRuntime: C++ DLL with PDB symbol resolution, MinHook function hooking, and .NET CoreCLR hosting
- LegacyForge.Core: C# mod discovery and lifecycle management
- LegacyForge.API: Fabric-style mod API with namespaced string IDs, fluent property builders, and event system
- ExampleMod: Sample mod demonstrating block/item registration
2026-03-06 15:11:53 -06:00

79 lines
2.2 KiB
C++

#include "IdRegistry.h"
#include <cstdio>
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)
{
printf("[LegacyForge] IdRegistry: No free IDs for type %d (max %d)\n",
static_cast<int>(type), maxId);
return -1;
}
int id = reg.nextFreeId++;
// Skip IDs that are already taken by vanilla entries
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;
}