mirror of
https://github.com/minetest/minetest.git
synced 2024-11-27 18:13:46 +01:00
ToolDefManager serialization
This commit is contained in:
parent
8d2d28f1dc
commit
0754f2a7af
@ -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;
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user