#include "utility.h"
#include "mapnode.h"
#include "exceptions.h"
+#include "inventory.h"
+#include <sstream>
+#include "content_mapnode.h"
/*
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)
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)
}
/*
- 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+"\"";
-}
-
-/*
- ChestNodeMetadata
-*/
-
-ChestNodeMetadata::ChestNodeMetadata()
-{
- NodeMetadata::registerType(typeId(), create);
-}
-u16 ChestNodeMetadata::typeId() const
-{
- return CONTENT_CHEST;
-}
-NodeMetadata* ChestNodeMetadata::create(std::istream &is)
-{
- return new ChestNodeMetadata();
-}
-NodeMetadata* ChestNodeMetadata::clone()
-{
- return new ChestNodeMetadata();
-}
-void ChestNodeMetadata::serializeBody(std::ostream &os)
-{
-}
-std::string ChestNodeMetadata::infoText()
-{
- return "Chest";
-}
-
-/*
- 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);
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);
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);
}
}
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;
+}
+