LCOV - code coverage report
Current view: top level - src - nodemetadata.cpp (source / functions) Hit Total Coverage
Test: report Lines: 74 118 62.7 %
Date: 2015-07-11 18:23:49 Functions: 13 18 72.2 %

          Line data    Source code
       1             : /*
       2             : Minetest
       3             : Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
       4             : 
       5             : This program is free software; you can redistribute it and/or modify
       6             : it under the terms of the GNU Lesser General Public License as published by
       7             : the Free Software Foundation; either version 2.1 of the License, or
       8             : (at your option) any later version.
       9             : 
      10             : This program is distributed in the hope that it will be useful,
      11             : but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             : GNU Lesser General Public License for more details.
      14             : 
      15             : You should have received a copy of the GNU Lesser General Public License along
      16             : with this program; if not, write to the Free Software Foundation, Inc.,
      17             : 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      18             : */
      19             : 
      20             : #include "nodemetadata.h"
      21             : #include "exceptions.h"
      22             : #include "gamedef.h"
      23             : #include "inventory.h"
      24             : #include "log.h"
      25             : #include "util/serialize.h"
      26             : #include "constants.h" // MAP_BLOCKSIZE
      27             : #include <sstream>
      28             : 
      29             : /*
      30             :         NodeMetadata
      31             : */
      32             : 
      33      186806 : NodeMetadata::NodeMetadata(IGameDef *gamedef):
      34             :         m_stringvars(),
      35      186806 :         m_inventory(new Inventory(gamedef->idef()))
      36             : {
      37      186806 : }
      38             : 
      39      373612 : NodeMetadata::~NodeMetadata()
      40             : {
      41      186806 :         delete m_inventory;
      42      186806 : }
      43             : 
      44          23 : void NodeMetadata::serialize(std::ostream &os) const
      45             : {
      46          23 :         int num_vars = m_stringvars.size();
      47          23 :         writeU32(os, num_vars);
      48         247 :         for (StringMap::const_iterator
      49          23 :                         it = m_stringvars.begin();
      50         180 :                         it != m_stringvars.end(); ++it) {
      51          67 :                 os << serializeString(it->first);
      52          67 :                 os << serializeLongString(it->second);
      53             :         }
      54             : 
      55          23 :         m_inventory->serialize(os);
      56          23 : }
      57             : 
      58      186806 : void NodeMetadata::deSerialize(std::istream &is)
      59             : {
      60      186806 :         m_stringvars.clear();
      61      186806 :         int num_vars = readU32(is);
      62      191477 :         for(int i=0; i<num_vars; i++){
      63        9342 :                 std::string name = deSerializeString(is);
      64        9342 :                 std::string var = deSerializeLongString(is);
      65        4671 :                 m_stringvars[name] = var;
      66             :         }
      67             : 
      68      186806 :         m_inventory->deSerialize(is);
      69      186806 : }
      70             : 
      71           0 : void NodeMetadata::clear()
      72             : {
      73           0 :         m_stringvars.clear();
      74           0 :         m_inventory->clear();
      75           0 : }
      76             : 
      77             : /*
      78             :         NodeMetadataList
      79             : */
      80             : 
      81           0 : void NodeMetadataList::serialize(std::ostream &os) const
      82             : {
      83             :         /*
      84             :                 Version 0 is a placeholder for "nothing to see here; go away."
      85             :         */
      86             : 
      87           0 :         if(m_data.empty()){
      88           0 :                 writeU8(os, 0); // version
      89           0 :                 return;
      90             :         }
      91             : 
      92           0 :         writeU8(os, 1); // version
      93             : 
      94           0 :         u16 count = m_data.size();
      95           0 :         writeU16(os, count);
      96             : 
      97           0 :         for(std::map<v3s16, NodeMetadata*>::const_iterator
      98           0 :                         i = m_data.begin();
      99           0 :                         i != m_data.end(); i++)
     100             :         {
     101           0 :                 v3s16 p = i->first;
     102           0 :                 NodeMetadata *data = i->second;
     103             : 
     104           0 :                 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
     105           0 :                 writeU16(os, p16);
     106             : 
     107           0 :                 data->serialize(os);
     108             :         }
     109             : }
     110             : 
     111         786 : void NodeMetadataList::deSerialize(std::istream &is, IGameDef *gamedef)
     112             : {
     113         786 :         clear();
     114             : 
     115         786 :         u8 version = readU8(is);
     116             : 
     117         786 :         if(version == 0){
     118             :                 // Nothing
     119         568 :                 return;
     120             :         }
     121             : 
     122         218 :         if(version != 1){
     123           0 :                 infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
     124           0 :                                 <<std::endl;
     125           0 :                 throw SerializationError("NodeMetadataList::deSerialize");
     126             :         }
     127             : 
     128         218 :         u16 count = readU16(is);
     129             : 
     130      187024 :         for(u16 i=0; i<count; i++)
     131             :         {
     132      186806 :                 u16 p16 = readU16(is);
     133             : 
     134      186806 :                 v3s16 p;
     135      186806 :                 p.Z = p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
     136      186806 :                 p16 &= MAP_BLOCKSIZE * MAP_BLOCKSIZE - 1;
     137      186806 :                 p.Y = p16 / MAP_BLOCKSIZE;
     138      186806 :                 p16 &= MAP_BLOCKSIZE - 1;
     139      186806 :                 p.X = p16;
     140             : 
     141      186806 :                 if(m_data.find(p) != m_data.end())
     142             :                 {
     143           0 :                         infostream<<"WARNING: NodeMetadataList::deSerialize(): "
     144           0 :                                         <<"already set data at position"
     145           0 :                                         <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
     146           0 :                                         <<std::endl;
     147           0 :                         continue;
     148             :                 }
     149             : 
     150      186806 :                 NodeMetadata *data = new NodeMetadata(gamedef);
     151      186806 :                 data->deSerialize(is);
     152      186806 :                 m_data[p] = data;
     153             :         }
     154             : }
     155             : 
     156        1502 : NodeMetadataList::~NodeMetadataList()
     157             : {
     158         751 :         clear();
     159         751 : }
     160             : 
     161           0 : std::vector<v3s16> NodeMetadataList::getAllKeys()
     162             : {
     163           0 :         std::vector<v3s16> keys;
     164             : 
     165           0 :         std::map<v3s16, NodeMetadata *>::const_iterator it;
     166           0 :         for (it = m_data.begin(); it != m_data.end(); ++it)
     167           0 :                 keys.push_back(it->first);
     168             : 
     169           0 :         return keys;
     170             : }
     171             : 
     172         353 : NodeMetadata *NodeMetadataList::get(v3s16 p)
     173             : {
     174         353 :         std::map<v3s16, NodeMetadata *>::const_iterator n = m_data.find(p);
     175         353 :         if (n == m_data.end())
     176         253 :                 return NULL;
     177         100 :         return n->second;
     178             : }
     179             : 
     180          13 : void NodeMetadataList::remove(v3s16 p)
     181             : {
     182          13 :         NodeMetadata *olddata = get(p);
     183          13 :         if (olddata) {
     184           1 :                 delete olddata;
     185           1 :                 m_data.erase(p);
     186             :         }
     187          13 : }
     188             : 
     189           0 : void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
     190             : {
     191           0 :         remove(p);
     192           0 :         m_data.insert(std::make_pair(p, d));
     193           0 : }
     194             : 
     195        1537 : void NodeMetadataList::clear()
     196             : {
     197        1537 :         std::map<v3s16, NodeMetadata*>::iterator it;
     198      188342 :         for (it = m_data.begin(); it != m_data.end(); ++it) {
     199      186805 :                 delete it->second;
     200             :         }
     201        1537 :         m_data.clear();
     202        1537 : }
     203             : 
     204          76 : std::string NodeMetadata::getString(const std::string &name,
     205             :         unsigned short recursion) const
     206             : {
     207          76 :         StringMap::const_iterator it = m_stringvars.find(name);
     208          76 :         if (it == m_stringvars.end())
     209          19 :                 return "";
     210             : 
     211          57 :         return resolveString(it->second, recursion);
     212             : }
     213             : 
     214           0 : void NodeMetadata::setString(const std::string &name, const std::string &var)
     215             : {
     216           0 :         if (var.empty()) {
     217           0 :                 m_stringvars.erase(name);
     218             :         } else {
     219           0 :                 m_stringvars[name] = var;
     220             :         }
     221           0 : }
     222             : 
     223          57 : std::string NodeMetadata::resolveString(const std::string &str,
     224             :         unsigned short recursion) const
     225             : {
     226          57 :         if (recursion > 1) {
     227           0 :                 return str;
     228             :         }
     229          57 :         if (str.substr(0, 2) == "${" && str[str.length() - 1] == '}') {
     230           0 :                 return getString(str.substr(2, str.length() - 3), recursion + 1);
     231             :         }
     232          57 :         return str;
     233           3 : }
     234             : 

Generated by: LCOV version 1.11