Fix alias handling of get_content_id (#9712)

fixes #9632
This commit is contained in:
sfan5 2020-04-19 19:07:54 +02:00 committed by GitHub
parent cdbe3c5e57
commit 338195ff25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 22 deletions

@ -270,17 +270,16 @@ public:
// Convert name according to possible alias // Convert name according to possible alias
std::string name = getAlias(name_); std::string name = getAlias(name_);
// Get the definition // Get the definition
std::map<std::string, ItemDefinition*>::const_iterator i; auto i = m_item_definitions.find(name);
i = m_item_definitions.find(name); if (i == m_item_definitions.cend())
if(i == m_item_definitions.end())
i = m_item_definitions.find("unknown"); i = m_item_definitions.find("unknown");
assert(i != m_item_definitions.end()); assert(i != m_item_definitions.cend());
return *(i->second); return *(i->second);
} }
virtual const std::string &getAlias(const std::string &name) const virtual const std::string &getAlias(const std::string &name) const
{ {
StringMap::const_iterator it = m_aliases.find(name); auto it = m_aliases.find(name);
if (it != m_aliases.end()) if (it != m_aliases.cend())
return it->second; return it->second;
return name; return name;
} }
@ -300,8 +299,7 @@ public:
// Convert name according to possible alias // Convert name according to possible alias
std::string name = getAlias(name_); std::string name = getAlias(name_);
// Get the definition // Get the definition
std::map<std::string, ItemDefinition*>::const_iterator i; return m_item_definitions.find(name) != m_item_definitions.cend();
return m_item_definitions.find(name) != m_item_definitions.end();
} }
#ifndef SERVER #ifndef SERVER
public: public:
@ -443,11 +441,9 @@ public:
} }
void clear() void clear()
{ {
for(std::map<std::string, ItemDefinition*>::const_iterator for (auto &i : m_item_definitions)
i = m_item_definitions.begin();
i != m_item_definitions.end(); ++i)
{ {
delete i->second; delete i.second;
} }
m_item_definitions.clear(); m_item_definitions.clear();
m_aliases.clear(); m_aliases.clear();
@ -520,10 +516,8 @@ public:
u16 count = m_item_definitions.size(); u16 count = m_item_definitions.size();
writeU16(os, count); writeU16(os, count);
for (std::map<std::string, ItemDefinition *>::const_iterator for (const auto &it : m_item_definitions) {
it = m_item_definitions.begin(); ItemDefinition *def = it.second;
it != m_item_definitions.end(); ++it) {
ItemDefinition *def = it->second;
// Serialize ItemDefinition and write wrapped in a string // Serialize ItemDefinition and write wrapped in a string
std::ostringstream tmp_os(std::ios::binary); std::ostringstream tmp_os(std::ios::binary);
def->serialize(tmp_os, protocol_version); def->serialize(tmp_os, protocol_version);
@ -532,11 +526,9 @@ public:
writeU16(os, m_aliases.size()); writeU16(os, m_aliases.size());
for (StringMap::const_iterator for (const auto &it : m_aliases) {
it = m_aliases.begin(); os << serializeString(it.first);
it != m_aliases.end(); ++it) { os << serializeString(it.second);
os << serializeString(it->first);
os << serializeString(it->second);
} }
} }
void deSerialize(std::istream &is) void deSerialize(std::istream &is)

@ -609,10 +609,21 @@ int ModApiItemMod::l_get_content_id(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
std::string name = luaL_checkstring(L, 1); std::string name = luaL_checkstring(L, 1);
const IItemDefManager *idef = getGameDef(L)->getItemDefManager();
const NodeDefManager *ndef = getGameDef(L)->getNodeDefManager(); const NodeDefManager *ndef = getGameDef(L)->getNodeDefManager();
// If this is called at mod load time, NodeDefManager isn't aware of
// aliases yet, so we need to handle them manually
std::string alias_name = idef->getAlias(name);
content_t content_id; content_t content_id;
if (!ndef->getId(name, content_id)) if (alias_name != name) {
if (!ndef->getId(alias_name, content_id))
throw LuaError("Unknown node: " + alias_name +
" (from alias " + name + ")");
} else if (!ndef->getId(name, content_id)) {
throw LuaError("Unknown node: " + name); throw LuaError("Unknown node: " + name);
}
lua_pushinteger(L, content_id); lua_pushinteger(L, content_id);
return 1; /* number of results */ return 1; /* number of results */