From 338195ff250bd7552ef8167348de2eb05e421c29 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sun, 19 Apr 2020 19:07:54 +0200 Subject: Fix alias handling of get_content_id (#9712) fixes #9632 --- src/itemdef.cpp | 34 +++++++++++++--------------------- src/script/lua_api/l_item.cpp | 13 ++++++++++++- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/itemdef.cpp b/src/itemdef.cpp index a13b3f7d4..8e0492827 100644 --- a/src/itemdef.cpp +++ b/src/itemdef.cpp @@ -270,17 +270,16 @@ public: // Convert name according to possible alias std::string name = getAlias(name_); // Get the definition - std::map::const_iterator i; - i = m_item_definitions.find(name); - if(i == m_item_definitions.end()) + auto i = m_item_definitions.find(name); + if (i == m_item_definitions.cend()) i = m_item_definitions.find("unknown"); - assert(i != m_item_definitions.end()); + assert(i != m_item_definitions.cend()); return *(i->second); } virtual const std::string &getAlias(const std::string &name) const { - StringMap::const_iterator it = m_aliases.find(name); - if (it != m_aliases.end()) + auto it = m_aliases.find(name); + if (it != m_aliases.cend()) return it->second; return name; } @@ -300,8 +299,7 @@ public: // Convert name according to possible alias std::string name = getAlias(name_); // Get the definition - std::map::const_iterator i; - return m_item_definitions.find(name) != m_item_definitions.end(); + return m_item_definitions.find(name) != m_item_definitions.cend(); } #ifndef SERVER public: @@ -443,11 +441,9 @@ public: } void clear() { - for(std::map::const_iterator - i = m_item_definitions.begin(); - i != m_item_definitions.end(); ++i) + for (auto &i : m_item_definitions) { - delete i->second; + delete i.second; } m_item_definitions.clear(); m_aliases.clear(); @@ -520,10 +516,8 @@ public: u16 count = m_item_definitions.size(); writeU16(os, count); - for (std::map::const_iterator - it = m_item_definitions.begin(); - it != m_item_definitions.end(); ++it) { - ItemDefinition *def = it->second; + for (const auto &it : m_item_definitions) { + ItemDefinition *def = it.second; // Serialize ItemDefinition and write wrapped in a string std::ostringstream tmp_os(std::ios::binary); def->serialize(tmp_os, protocol_version); @@ -532,11 +526,9 @@ public: writeU16(os, m_aliases.size()); - for (StringMap::const_iterator - it = m_aliases.begin(); - it != m_aliases.end(); ++it) { - os << serializeString(it->first); - os << serializeString(it->second); + for (const auto &it : m_aliases) { + os << serializeString(it.first); + os << serializeString(it.second); } } void deSerialize(std::istream &is) diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp index 9f12d3ac7..0a403acbd 100644 --- a/src/script/lua_api/l_item.cpp +++ b/src/script/lua_api/l_item.cpp @@ -609,10 +609,21 @@ int ModApiItemMod::l_get_content_id(lua_State *L) NO_MAP_LOCK_REQUIRED; std::string name = luaL_checkstring(L, 1); + const IItemDefManager *idef = getGameDef(L)->getItemDefManager(); 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; - 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); + } lua_pushinteger(L, content_id); return 1; /* number of results */ -- cgit v1.2.3