]> git.lizzy.rs Git - dragonfireclient.git/blob - src/nodemetadata.cpp
serialize.h: use machine native byte swapping if available, fall-back to previous...
[dragonfireclient.git] / src / nodemetadata.cpp
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 NodeMetadata::NodeMetadata(IGameDef *gamedef):
34         m_stringvars(),
35         m_inventory(new Inventory(gamedef->idef()))
36 {
37 }
38
39 NodeMetadata::~NodeMetadata()
40 {
41         delete m_inventory;
42 }
43
44 void NodeMetadata::serialize(std::ostream &os) const
45 {
46         int num_vars = m_stringvars.size();
47         writeU32(os, num_vars);
48         for(std::map<std::string, std::string>::const_iterator
49                         i = m_stringvars.begin(); i != m_stringvars.end(); i++){
50                 os<<serializeString(i->first);
51                 os<<serializeLongString(i->second);
52         }
53
54         m_inventory->serialize(os);
55 }
56
57 void NodeMetadata::deSerialize(std::istream &is)
58 {
59         m_stringvars.clear();
60         int num_vars = readU32(is);
61         for(int i=0; i<num_vars; i++){
62                 std::string name = deSerializeString(is);
63                 std::string var = deSerializeLongString(is);
64                 m_stringvars[name] = var;
65         }
66
67         m_inventory->deSerialize(is);
68 }
69
70 void NodeMetadata::clear()
71 {
72         m_stringvars.clear();
73         m_inventory->clear();
74 }
75
76 /*
77         NodeMetadataList
78 */
79
80 void NodeMetadataList::serialize(std::ostream &os) const
81 {
82         /*
83                 Version 0 is a placeholder for "nothing to see here; go away."
84         */
85
86         if(m_data.size() == 0){
87                 writeU8(os, 0); // version
88                 return;
89         }
90
91         writeU8(os, 1); // version
92
93         u16 count = m_data.size();
94         writeU16(os, count);
95
96         for(std::map<v3s16, NodeMetadata*>::const_iterator
97                         i = m_data.begin();
98                         i != m_data.end(); i++)
99         {
100                 v3s16 p = i->first;
101                 NodeMetadata *data = i->second;
102
103                 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
104                 writeU16(os, p16);
105
106                 data->serialize(os);
107         }
108 }
109
110 void NodeMetadataList::deSerialize(std::istream &is, IGameDef *gamedef)
111 {
112         m_data.clear();
113
114         u8 version = readU8(is);
115         
116         if(version == 0){
117                 // Nothing
118                 return;
119         }
120
121         if(version != 1){
122                 infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
123                                 <<std::endl;
124                 throw SerializationError("NodeMetadataList::deSerialize");
125         }
126
127         u16 count = readU16(is);
128
129         for(u16 i=0; i<count; i++)
130         {
131                 u16 p16 = readU16(is);
132
133                 v3s16 p;
134                 p.Z = p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
135                 p16 &= MAP_BLOCKSIZE * MAP_BLOCKSIZE - 1;
136                 p.Y = p16 / MAP_BLOCKSIZE;
137                 p16 &= MAP_BLOCKSIZE - 1;
138                 p.X = p16;
139
140                 if(m_data.find(p) != m_data.end())
141                 {
142                         infostream<<"WARNING: NodeMetadataList::deSerialize(): "
143                                         <<"already set data at position"
144                                         <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
145                                         <<std::endl;
146                         continue;
147                 }
148
149                 NodeMetadata *data = new NodeMetadata(gamedef);
150                 data->deSerialize(is);
151                 m_data[p] = data;
152         }
153 }
154
155 NodeMetadataList::~NodeMetadataList()
156 {
157         clear();
158 }
159
160 NodeMetadata* NodeMetadataList::get(v3s16 p)
161 {
162         std::map<v3s16, NodeMetadata*>::const_iterator n = m_data.find(p);
163         if(n == m_data.end())
164                 return NULL;
165         return n->second;
166 }
167
168 void NodeMetadataList::remove(v3s16 p)
169 {
170         NodeMetadata *olddata = get(p);
171         if(olddata)
172         {
173                 delete olddata;
174                 m_data.erase(p);
175         }
176 }
177
178 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
179 {
180         remove(p);
181         m_data.insert(std::make_pair(p, d));
182 }
183
184 void NodeMetadataList::clear()
185 {
186         for(std::map<v3s16, NodeMetadata*>::iterator
187                         i = m_data.begin();
188                         i != m_data.end(); i++)
189         {
190                 delete i->second;
191         }
192         m_data.clear();
193 }
194
195 std::string NodeMetadata::getString(const std::string &name, unsigned short recursion) const
196 {
197         std::map<std::string, std::string>::const_iterator it;
198         it = m_stringvars.find(name);
199         if (it == m_stringvars.end()) {
200                 return "";
201         }
202         return resolveString(it->second, recursion);
203 }
204
205 void NodeMetadata::setString(const std::string &name, const std::string &var)
206 {
207         if (var.empty()) {
208                 m_stringvars.erase(name);
209         } else {
210                 m_stringvars[name] = var;
211         }
212 }
213
214 std::string NodeMetadata::resolveString(const std::string &str, unsigned short recursion) const
215 {
216         if (recursion > 1) {
217                 return str;
218         }
219         if (str.substr(0, 2) == "${" && str[str.length() - 1] == '}') {
220                 return getString(str.substr(2, str.length() - 3), recursion + 1);
221         }
222         return str;
223 }
224