Initialize utility.h return values to 0 to make lazily error-checked deserialization...
[oweals/minetest.git] / src / nodemetadata.cpp
index 3edf6d3c28ac58e6fe0ae7ab8eb6ff7c281c9987..410b4e2ea78bcb12cf89020dba9451aeb2336312 100644 (file)
@@ -24,14 +24,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #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)
 {
 }
 
@@ -39,7 +39,35 @@ 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];
@@ -55,7 +83,7 @@ NodeMetadata* NodeMetadata::deSerialize(std::istream &is)
        if(n == NULL)
        {
                // If factory is not found, just return.
-               dstream<<"WARNING: NodeMetadata: No factory for typeId="
+               infostream<<"WARNING: NodeMetadata: No factory for typeId="
                                <<id<<std::endl;
                return NULL;
        }
@@ -66,12 +94,12 @@ NodeMetadata* NodeMetadata::deSerialize(std::istream &is)
                std::istringstream iss(data, std::ios_base::binary);
                
                Factory f = n->getValue();
-               NodeMetadata *meta = (*f)(iss);
+               NodeMetadata *meta = (*f)(iss, gamedef);
                return meta;
        }
        catch(SerializationError &e)
        {
-               dstream<<"WARNING: NodeMetadata: ignoring SerializationError"<<std::endl;
+               infostream<<"WARNING: NodeMetadata: ignoring SerializationError"<<std::endl;
                return NULL;
        }
 }
@@ -87,13 +115,21 @@ void NodeMetadata::serialize(std::ostream &os)
        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);
+       { // 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);
+       }
 }
 
 /*
@@ -127,7 +163,7 @@ void NodeMetadataList::serialize(std::ostream &os)
        }
        
 }
-void NodeMetadataList::deSerialize(std::istream &is)
+void NodeMetadataList::deSerialize(std::istream &is, IGameDef *gamedef)
 {
        m_data.clear();
 
@@ -138,7 +174,7 @@ void NodeMetadataList::deSerialize(std::istream &is)
 
        if(version > 1)
        {
-               dstream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
+               infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
                                <<std::endl;
                throw SerializationError("NodeMetadataList::deSerialize");
        }
@@ -158,14 +194,14 @@ void NodeMetadataList::deSerialize(std::istream &is)
                p16 -= p.Y * MAP_BLOCKSIZE;
                p.X += p16;
                
-               NodeMetadata *data = NodeMetadata::deSerialize(is);
+               NodeMetadata *data = NodeMetadata::deSerialize(is, gamedef);
 
                if(data == NULL)
                        continue;
                
                if(m_data.find(p))
                {
-                       dstream<<"WARNING: NodeMetadataList::deSerialize(): "
+                       infostream<<"WARNING: NodeMetadataList::deSerialize(): "
                                        <<"already set data at position"
                                        <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
                                        <<std::endl;