]> git.lizzy.rs Git - minetest.git/blobdiff - src/nodemetadata.cpp
Dungeongen: Fix out-of-voxelmanip access segfault
[minetest.git] / src / nodemetadata.cpp
index 9c74dcd28cdb4616a28c6475ccd352f124e7d7e4..0801a028bfc76d4d05bca965bd1c5f95dbd1a08d 100644 (file)
@@ -74,6 +74,11 @@ void NodeMetadata::clear()
        m_inventory->clear();
 }
 
+bool NodeMetadata::empty() const
+{
+       return m_stringvars.size() == 0 && m_inventory->getLists().size() == 0;
+}
+
 /*
        NodeMetadataList
 */
@@ -84,14 +89,13 @@ void NodeMetadataList::serialize(std::ostream &os) const
                Version 0 is a placeholder for "nothing to see here; go away."
        */
 
-       if(m_data.empty()){
+       u16 count = countNonEmpty();
+       if (count == 0) {
                writeU8(os, 0); // version
                return;
        }
 
        writeU8(os, 1); // version
-
-       u16 count = m_data.size();
        writeU16(os, count);
 
        for(std::map<v3s16, NodeMetadata*>::const_iterator
@@ -100,6 +104,8 @@ void NodeMetadataList::serialize(std::ostream &os) const
        {
                v3s16 p = i->first;
                NodeMetadata *data = i->second;
+               if (data->empty())
+                       continue;
 
                u16 p16 = p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE + p.Y * MAP_BLOCKSIZE + p.X;
                writeU16(os, p16);
@@ -120,7 +126,7 @@ void NodeMetadataList::deSerialize(std::istream &is, IItemDefManager *item_def_m
        }
 
        if (version != 1) {
-               std::string err_str = std::string(__FUNCTION_NAME)
+               std::string err_str = std::string(FUNCTION_NAME)
                        + ": version " + itos(version) + " not supported";
                infostream << err_str << std::endl;
                throw SerializationError(err_str);
@@ -200,6 +206,17 @@ void NodeMetadataList::clear()
        m_data.clear();
 }
 
+int NodeMetadataList::countNonEmpty() const
+{
+       int n = 0;
+       std::map<v3s16, NodeMetadata*>::const_iterator it;
+       for (it = m_data.begin(); it != m_data.end(); ++it) {
+               if (!it->second->empty())
+                       n++;
+       }
+       return n;
+}
+
 std::string NodeMetadata::getString(const std::string &name,
        unsigned short recursion) const
 {