aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPerttu Ahola <celeron55@gmail.com>2011-11-15 00:05:26 +0200
committerPerttu Ahola <celeron55@gmail.com>2011-11-29 19:13:42 +0200
commit0754f2a7af831922e26c12e707dfb6724897322f (patch)
tree3a7d3cfbfec021307cf60aa5fd6028f5e56db0e4
parent8d2d28f1dc575a6ed5e3aeb63a6c1b917813aeb4 (diff)
downloadminetest-0754f2a7af831922e26c12e707dfb6724897322f.tar.gz
minetest-0754f2a7af831922e26c12e707dfb6724897322f.tar.bz2
minetest-0754f2a7af831922e26c12e707dfb6724897322f.zip
ToolDefManager serialization
-rw-r--r--src/tooldef.cpp37
-rw-r--r--src/tooldef.h3
2 files changed, 39 insertions, 1 deletions
diff --git a/src/tooldef.cpp b/src/tooldef.cpp
index 93bedf92a..1c85dbd8e 100644
--- a/src/tooldef.cpp
+++ b/src/tooldef.cpp
@@ -66,7 +66,7 @@ void ToolDefinition::deSerialize(std::istream &is)
{
int version = readU8(is);
if(version != 0) throw SerializationError(
- "unsupported ToolDiggingProperties version");
+ "unsupported ToolDefinition version");
imagename = deSerializeString(is);
properties.basetime = readF1000(is);
properties.dt_weight = readF1000(is);
@@ -133,6 +133,41 @@ public:
m_tool_definitions[toolname] = new ToolDefinition(def);
return true;
}
+ virtual void serialize(std::ostream &os)
+ {
+ writeU8(os, 0); // version
+ u16 count = m_tool_definitions.size();
+ writeU16(os, count);
+ for(core::map<std::string, ToolDefinition*>::Iterator
+ i = m_tool_definitions.getIterator();
+ i.atEnd() == false; i++){
+ std::string name = i.getNode()->getKey();
+ ToolDefinition *def = i.getNode()->getValue();
+ // Serialize name
+ os<<serializeString(name);
+ // Serialize ToolDefinition and write wrapped in a string
+ std::ostringstream tmp_os(std::ios::binary);
+ def->serialize(tmp_os);
+ os<<serializeString(tmp_os.str());
+ }
+ }
+ virtual void deSerialize(std::istream &is)
+ {
+ int version = readU8(is);
+ if(version != 0) throw SerializationError(
+ "unsupported ToolDefManager version");
+ u16 count = readU16(is);
+ for(u16 i=0; i<count; i++){
+ // Deserialize name
+ std::string name = deSerializeString(is);
+ // Deserialize a string and grab a ToolDefinition from it
+ std::istringstream tmp_is(deSerializeString(is), std::ios::binary);
+ ToolDefinition def;
+ def.deSerialize(tmp_is);
+ // Register
+ registerTool(name, def);
+ }
+ }
private:
// Key is name
core::map<std::string, ToolDefinition*> m_tool_definitions;
diff --git a/src/tooldef.h b/src/tooldef.h
index fdb02b012..2c758d902 100644
--- a/src/tooldef.h
+++ b/src/tooldef.h
@@ -85,6 +85,9 @@ public:
const std::string &toolname) const =0;
virtual bool registerTool(std::string toolname, const ToolDefinition &def)=0;
+
+ virtual void serialize(std::ostream &os)=0;
+ virtual void deSerialize(std::istream &is)=0;
};
IWritableToolDefManager* createToolDefManager();