ToolDefinition serialization
authorPerttu Ahola <celeron55@gmail.com>
Mon, 14 Nov 2011 21:39:32 +0000 (23:39 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Tue, 29 Nov 2011 17:13:42 +0000 (19:13 +0200)
src/tooldef.cpp
src/tooldef.h

index f35cf2b82a80b2a9e30216afd59c21f97cd80422..93bedf92ac1b7516c9cabb0341b3a8f01e60b049 100644 (file)
@@ -20,7 +20,65 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "tooldef.h"
 #include "irrlichttypes.h"
 #include "log.h"
-#include <ostream>
+#include <sstream>
+#include "utility.h"
+
+ToolDiggingProperties::ToolDiggingProperties(
+               float a, float b, float c, float d, float e,
+               float f, float g, float h, float i, float j):
+       basetime(a),
+       dt_weight(b),
+       dt_crackiness(c),
+       dt_crumbliness(d),
+       dt_cuttability(e),
+       basedurability(f),
+       dd_weight(g),
+       dd_crackiness(h),
+       dd_crumbliness(i),
+       dd_cuttability(j)
+{}
+
+std::string ToolDefinition::dump()
+{
+       std::ostringstream os(std::ios::binary);
+       os<<"[ToolDefinition::dump() not implemented due to lazyness]"
+                       <<std::endl;
+       return os.str();
+}
+
+void ToolDefinition::serialize(std::ostream &os)
+{
+       writeU8(os, 0); // version
+       os<<serializeString(imagename);
+       writeF1000(os, properties.basetime);
+       writeF1000(os, properties.dt_weight);
+       writeF1000(os, properties.dt_crackiness);
+       writeF1000(os, properties.dt_crumbliness);
+       writeF1000(os, properties.dt_cuttability);
+       writeF1000(os, properties.basedurability);
+       writeF1000(os, properties.dd_weight);
+       writeF1000(os, properties.dd_crackiness);
+       writeF1000(os, properties.dd_crumbliness);
+       writeF1000(os, properties.dd_cuttability);
+}
+
+void ToolDefinition::deSerialize(std::istream &is)
+{
+       int version = readU8(is);
+       if(version != 0) throw SerializationError(
+                       "unsupported ToolDiggingProperties version");
+       imagename = deSerializeString(is);
+       properties.basetime = readF1000(is);
+       properties.dt_weight = readF1000(is);
+       properties.dt_crackiness = readF1000(is);
+       properties.dt_crumbliness = readF1000(is);
+       properties.dt_cuttability = readF1000(is);
+       properties.basedurability = readF1000(is);
+       properties.dd_weight = readF1000(is);
+       properties.dd_crackiness = readF1000(is);
+       properties.dd_crumbliness = readF1000(is);
+       properties.dd_cuttability = readF1000(is);
+}
 
 class CToolDefManager: public IWritableToolDefManager
 {
index 8aa6abea41cde03e87d3cadb55daae681cb7f33a..fdb02b012cdd30c493b54f3b8f0dfdca980ff128 100644 (file)
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #define TOOLDEF_HEADER
 
 #include <string>
+#include <iostream>
 
 /*
        TODO: Rename to tooldef.h
@@ -40,19 +41,9 @@ struct ToolDiggingProperties
        float dd_crumbliness;
        float dd_cuttability;
 
-       ToolDiggingProperties(float a=0.75, float b=0, float c=0, float d=0, float e=0,
-                       float f=50, float g=0, float h=0, float i=0, float j=0):
-               basetime(a),
-               dt_weight(b),
-               dt_crackiness(c),
-               dt_crumbliness(d),
-               dt_cuttability(e),
-               basedurability(f),
-               dd_weight(g),
-               dd_crackiness(h),
-               dd_crumbliness(i),
-               dd_cuttability(j)
-       {}
+       ToolDiggingProperties(
+                       float a=0.75, float b=0, float c=0, float d=0, float e=0,
+                       float f=50, float g=0, float h=0, float i=0, float j=0);
 };
 
 struct ToolDefinition
@@ -66,6 +57,10 @@ struct ToolDefinition
                imagename(imagename_),
                properties(properties_)
        {}
+       
+       std::string dump();
+       void serialize(std::ostream &os);
+       void deSerialize(std::istream &is);
 };
 
 class IToolDefManager