]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/nodemetadata.cpp
easier notice on old version
[dragonfireclient.git] / src / nodemetadata.cpp
index 775b59b241bfc6f85e5d008d94670c2a5c1d446c..3edf6d3c28ac58e6fe0ae7ab8eb6ff7c281c9987 100644 (file)
@@ -21,6 +21,9 @@ 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"
 
 /*
        NodeMetadata
@@ -38,21 +41,39 @@ NodeMetadata::~NodeMetadata()
 
 NodeMetadata* NodeMetadata::deSerialize(std::istream &is)
 {
+       // 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.
+               dstream<<"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);
+               return meta;
+       }
+       catch(SerializationError &e)
+       {
+               dstream<<"WARNING: NodeMetadata: ignoring SerializationError"<<std::endl;
+               return NULL;
+       }
 }
 
 void NodeMetadata::serialize(std::ostream &os)
@@ -60,8 +81,10 @@ 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)
@@ -74,44 +97,17 @@ void NodeMetadata::registerType(u16 id, Factory 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+"\"";
-}
-
-/*
-       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);
@@ -137,6 +133,16 @@ void NodeMetadataList::deSerialize(std::istream &is)
 
        u8 buf[6];
        
+       is.read((char*)buf, 2);
+       u16 version = readU16(buf);
+
+       if(version > 1)
+       {
+               dstream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
+                               <<std::endl;
+               throw SerializationError("NodeMetadataList::deSerialize");
+       }
+       
        is.read((char*)buf, 2);
        u16 count = readU16(buf);
        
@@ -152,17 +158,21 @@ void NodeMetadataList::deSerialize(std::istream &is)
                p16 -= p.Y * MAP_BLOCKSIZE;
                p.X += p16;
                
+               NodeMetadata *data = NodeMetadata::deSerialize(is);
+
+               if(data == NULL)
+                       continue;
+               
                if(m_data.find(p))
                {
-                       dstream<<"ERROR: NodeMetadataList::deSerialize(): "
+                       dstream<<"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 +212,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;
+}
+