Improve item serialization
authorPerttu Ahola <celeron55@gmail.com>
Wed, 16 Nov 2011 20:47:37 +0000 (22:47 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Tue, 29 Nov 2011 17:13:48 +0000 (19:13 +0200)
src/inventory.cpp
src/inventory.h
src/strfnd.h
src/utility.h

index 5b29818dc5db89275126a2c21e7f3b4aaa648243..3868686637b8f91588c6e3af78f2ecf64fe898d9 100644 (file)
@@ -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
index 64fab921bde1f38c5d1634ca7cf277ea30c6a586..9c6a967a2140476f82e2edddaa2249532172e338 100644 (file)
@@ -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<<nodename;
+               os<<m_nodename;
                os<<"\" ";
                os<<m_count;
-
-               // Old
-               /*os<<"MaterialItem2";
-               os<<" ";
-               os<<(unsigned int)m_content;
-               os<<" ";
-               os<<m_count;*/
        }
        virtual InventoryItem* clone()
        {
@@ -225,9 +214,9 @@ public:
        virtual void serialize(std::ostream &os) const
        {
                os<<getName();
-               os<<" ";
+               os<<" \"";
                os<<m_subname;
-               os<<" ";
+               os<<"\" ";
                os<<m_count;
        }
        virtual InventoryItem* clone()
@@ -304,9 +293,9 @@ public:
        virtual void serialize(std::ostream &os) const
        {
                os<<getName();
-               os<<" ";
+               os<<" \"";
                os<<m_toolname;
-               os<<" ";
+               os<<"\" ";
                os<<m_wear;
        }
        virtual InventoryItem* clone()
index e3d380e377a5233e3f7f33a099a03f661b827bbe..049ae0bae99375554e596418849fb0e1bbd1469d 100644 (file)
@@ -65,6 +65,19 @@ public:
                //std::cout<<"palautus=\""<<palautus<<"\""<<std::endl;
         return palautus;
     }
+       void skip_over(std::string chars){
+               while(p < tek.size()){
+                       bool is = false;
+                       for(unsigned int i=0; i<chars.size(); i++){
+                               if(chars[i] == tek[p]){
+                                       is = true;
+                                       break;
+                               }
+                       }
+                       if(!is) break;
+                       p++;
+               }
+       }
     bool atend(){
         if(p>=tek.size()) return true;
         return false;
index 7c1fde6f0798200d0149a80e0ea56a6dda7076ad..935df4b2e29d8af9737b99a9fa68ed076486ae2c 100644 (file)
@@ -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];