From 8dd3622c6d3c85adbb293eacf61f76c1118c9255 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Wed, 16 Nov 2011 22:47:37 +0200 Subject: [PATCH] Improve item serialization --- src/inventory.cpp | 51 +++++++++++++++++++++++++++++++++++++---------- src/inventory.h | 23 ++++++--------------- src/strfnd.h | 13 ++++++++++++ src/utility.h | 2 +- 4 files changed, 61 insertions(+), 28 deletions(-) diff --git a/src/inventory.cpp b/src/inventory.cpp index 5b29818d..38686866 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -79,9 +79,7 @@ InventoryItem* InventoryItem::deSerialize(std::istream &is, IGameDef *gamedef) is>>count; // Convert old materials if(material <= 0xff) - { material = content_translate_from_19_to_internal(material); - } if(material > MAX_CONTENT) throw SerializationError("Too large material number"); return new MaterialItem(gamedef, material, count); @@ -96,13 +94,22 @@ InventoryItem* InventoryItem::deSerialize(std::istream &is, IGameDef *gamedef) throw SerializationError("Too large material number"); return new MaterialItem(gamedef, material, count); } - else if(name == "MaterialItem3") + else if(name == "NodeItem" || name == "MaterialItem3") { std::string all; std::getline(is, all, '\n'); + std::string nodename; + // First attempt to read inside "" Strfnd fnd(all); fnd.next("\""); - std::string nodename = fnd.next("\""); + // If didn't skip to end, we have ""s + if(!fnd.atend()){ + nodename = fnd.next("\""); + } else { // No luck, just read a word then + fnd.start(all); + nodename = fnd.next(" "); + } + fnd.skip_over(" "); u16 count = stoi(trim(fnd.next(""))); return new MaterialItem(gamedef, nodename, count); } @@ -114,18 +121,42 @@ InventoryItem* InventoryItem::deSerialize(std::istream &is, IGameDef *gamedef) } else if(name == "CraftItem") { + std::string all; + std::getline(is, all, '\n'); std::string subname; - std::getline(is, subname, ' '); - u16 count; - is>>count; + // First attempt to read inside "" + Strfnd fnd(all); + fnd.next("\""); + // If didn't skip to end, we have ""s + if(!fnd.atend()){ + subname = fnd.next("\""); + } else { // No luck, just read a word then + fnd.start(all); + subname = fnd.next(" "); + } + // Then read count + fnd.skip_over(" "); + u16 count = stoi(trim(fnd.next(""))); return new CraftItem(gamedef, subname, count); } else if(name == "ToolItem") { + std::string all; + std::getline(is, all, '\n'); std::string toolname; - std::getline(is, toolname, ' '); - u16 wear; - is>>wear; + // First attempt to read inside "" + Strfnd fnd(all); + fnd.next("\""); + // If didn't skip to end, we have ""s + if(!fnd.atend()){ + toolname = fnd.next("\""); + } else { // No luck, just read a word then + fnd.start(all); + toolname = fnd.next(" "); + } + // Then read wear + fnd.skip_over(" "); + u16 wear = stoi(trim(fnd.next(""))); return new ToolItem(gamedef, toolname, wear); } else diff --git a/src/inventory.h b/src/inventory.h index 64fab921..9c6a967a 100644 --- a/src/inventory.h +++ b/src/inventory.h @@ -139,22 +139,11 @@ public: } virtual void serialize(std::ostream &os) const { - std::string nodename = m_nodename; - if(nodename == "") - nodename = "unknown_block"; - - os<<"MaterialItem3"; + os<<"NodeItem"; os<<" \""; - os<=tek.size()) return true; return false; diff --git a/src/utility.h b/src/utility.h index 7c1fde6f..935df4b2 100644 --- a/src/utility.h +++ b/src/utility.h @@ -32,9 +32,9 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "common_irrlicht.h" #include "debug.h" -#include "strfnd.h" #include "exceptions.h" #include "porting.h" +#include "strfnd.h" // For trim() extern const v3s16 g_6dirs[6];