Initialize utility.h return values to 0 to make lazily error-checked deserialization...
[oweals/minetest.git] / src / nodemetadata.cpp
index 775b59b241bfc6f85e5d008d94670c2a5c1d446c..410b4e2ea78bcb12cf89020dba9451aeb2336312 100644 (file)
@@ -21,14 +21,17 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "utility.h"
 #include "mapnode.h"
 #include "exceptions.h"
+#include "inventory.h"
+#include <sstream>
+#include "content_mapnode.h"
+#include "log.h"
 
 /*
        NodeMetadata
 */
 
-core::map<u16, NodeMetadata::Factory> NodeMetadata::m_types;
-
-NodeMetadata::NodeMetadata()
+NodeMetadata::NodeMetadata(IGameDef *gamedef):
+       m_gamedef(gamedef)
 {
 }
 
@@ -36,23 +39,69 @@ NodeMetadata::~NodeMetadata()
 {
 }
 
-NodeMetadata* NodeMetadata::deSerialize(std::istream &is)
+NodeMetadata* NodeMetadata::create(const std::string &name, IGameDef *gamedef)
 {
+       // Find factory function
+       core::map<std::string, Factory2>::Node *n;
+       n = m_names.find(name);
+       if(n == NULL)
+       {
+               // If factory is not found, just return.
+               errorstream<<"WARNING: NodeMetadata: No factory for name=\""
+                               <<name<<"\""<<std::endl;
+               return NULL;
+       }
+       
+       // Try to load the metadata. If it fails, just return.
+       try
+       {
+               Factory2 f2 = n->getValue();
+               NodeMetadata *meta = (*f2)(gamedef);
+               return meta;
+       }
+       catch(SerializationError &e)
+       {
+               errorstream<<"NodeMetadata: SerializationError "
+                               <<"while creating name=\""<<name<<"\""<<std::endl;
+               return NULL;
+       }
+}
+
+NodeMetadata* NodeMetadata::deSerialize(std::istream &is, IGameDef *gamedef)
+{
+       // Read id
        u8 buf[2];
        is.read((char*)buf, 2);
        s16 id = readS16(buf);
-
+       
+       // Read data
+       std::string data = deSerializeString(is);
+       
+       // Find factory function
        core::map<u16, Factory>::Node *n;
        n = m_types.find(id);
        if(n == NULL)
        {
-               dstream<<"NodeMetadata(): No factory for typeId="<<id<<std::endl;
-               throw SerializationError("Unknown metadata id");
+               // If factory is not found, just return.
+               infostream<<"WARNING: NodeMetadata: No factory for typeId="
+                               <<id<<std::endl;
+               return NULL;
        }
        
-       Factory f = n->getValue();
-       NodeMetadata *meta = (*f)(is);
-       return meta;
+       // Try to load the metadata. If it fails, just return.
+       try
+       {
+               std::istringstream iss(data, std::ios_base::binary);
+               
+               Factory f = n->getValue();
+               NodeMetadata *meta = (*f)(iss, gamedef);
+               return meta;
+       }
+       catch(SerializationError &e)
+       {
+               infostream<<"WARNING: NodeMetadata: ignoring SerializationError"<<std::endl;
+               return NULL;
+       }
 }
 
 void NodeMetadata::serialize(std::ostream &os)
@@ -60,58 +109,41 @@ void NodeMetadata::serialize(std::ostream &os)
        u8 buf[2];
        writeU16(buf, typeId());
        os.write((char*)buf, 2);
-
-       serializeBody(os);
+       
+       std::ostringstream oss(std::ios_base::binary);
+       serializeBody(oss);
+       os<<serializeString(oss.str());
 }
 
-void NodeMetadata::registerType(u16 id, Factory f)
+void NodeMetadata::registerType(u16 id, const std::string &name, Factory f,
+               Factory2 f2)
 {
-       core::map<u16, Factory>::Node *n;
-       n = m_types.find(id);
-       if(n)
-               return;
-       m_types.insert(id, f);
-}
-
-/*
-       SignNodeMetadata
-*/
-
-SignNodeMetadata::SignNodeMetadata(std::string text):
-       m_text(text)
-{
-       NodeMetadata::registerType(typeId(), create);
-}
-u16 SignNodeMetadata::typeId() const
-{
-       return CONTENT_SIGN_WALL;
-}
-NodeMetadata* SignNodeMetadata::create(std::istream &is)
-{
-       std::string text = deSerializeString(is);
-       return new SignNodeMetadata(text);
-}
-NodeMetadata* SignNodeMetadata::clone()
-{
-       return new SignNodeMetadata(m_text);
-}
-void SignNodeMetadata::serializeBody(std::ostream &os)
-{
-       os<<serializeString(m_text);
-}
-std::string SignNodeMetadata::infoText()
-{
-       return std::string("\"")+m_text+"\"";
+       { // typeId
+               core::map<u16, Factory>::Node *n;
+               n = m_types.find(id);
+               if(!n)
+                       m_types.insert(id, f);
+       }
+       { // typeName
+               core::map<std::string, Factory2>::Node *n;
+               n = m_names.find(name);
+               if(!n)
+                       m_names.insert(name, f2);
+       }
 }
 
 /*
-       NodeMetadatalist
+       NodeMetadataList
 */
 
 void NodeMetadataList::serialize(std::ostream &os)
 {
        u8 buf[6];
        
+       u16 version = 1;
+       writeU16(buf, version);
+       os.write((char*)buf, 2);
+
        u16 count = m_data.size();
        writeU16(buf, count);
        os.write((char*)buf, 2);
@@ -131,12 +163,22 @@ void NodeMetadataList::serialize(std::ostream &os)
        }
        
 }
-void NodeMetadataList::deSerialize(std::istream &is)
+void NodeMetadataList::deSerialize(std::istream &is, IGameDef *gamedef)
 {
        m_data.clear();
 
        u8 buf[6];
        
+       is.read((char*)buf, 2);
+       u16 version = readU16(buf);
+
+       if(version > 1)
+       {
+               infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
+                               <<std::endl;
+               throw SerializationError("NodeMetadataList::deSerialize");
+       }
+       
        is.read((char*)buf, 2);
        u16 count = readU16(buf);
        
@@ -152,17 +194,21 @@ void NodeMetadataList::deSerialize(std::istream &is)
                p16 -= p.Y * MAP_BLOCKSIZE;
                p.X += p16;
                
+               NodeMetadata *data = NodeMetadata::deSerialize(is, gamedef);
+
+               if(data == NULL)
+                       continue;
+               
                if(m_data.find(p))
                {
-                       dstream<<"ERROR: NodeMetadataList::deSerialize(): "
+                       infostream<<"WARNING: NodeMetadataList::deSerialize(): "
                                        <<"already set data at position"
-                                       <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
+                                       <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
                                        <<std::endl;
-                       throw SerializationError("NodeMetadataList::deSerialize()");
+                       delete data;
+                       continue;
                }
 
-               NodeMetadata *data = NodeMetadata::deSerialize(is);
-               
                m_data.insert(p, data);
        }
 }
@@ -202,3 +248,19 @@ void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
        m_data.insert(p, d);
 }
 
+bool NodeMetadataList::step(float dtime)
+{
+       bool something_changed = false;
+       for(core::map<v3s16, NodeMetadata*>::Iterator
+                       i = m_data.getIterator();
+                       i.atEnd()==false; i++)
+       {
+               v3s16 p = i.getNode()->getKey();
+               NodeMetadata *meta = i.getNode()->getValue();
+               bool changed = meta->step(dtime);
+               if(changed)
+                       something_changed = true;
+       }
+       return something_changed;
+}
+