Fix mod channels crash (#7481)
[oweals/minetest.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 "util/basic_macros.h"
27 #include "constants.h" // MAP_BLOCKSIZE
28 #include <sstream>
29
30 /*
31         NodeMetadata
32 */
33
34 NodeMetadata::NodeMetadata(IItemDefManager *item_def_mgr):
35         m_inventory(new Inventory(item_def_mgr))
36 {}
37
38 NodeMetadata::~NodeMetadata()
39 {
40         delete m_inventory;
41 }
42
43 void NodeMetadata::serialize(std::ostream &os, u8 version, bool disk) const
44 {
45         int num_vars = disk ? m_stringvars.size() : countNonPrivate();
46         writeU32(os, num_vars);
47         for (const auto &sv : m_stringvars) {
48                 bool priv = isPrivate(sv.first);
49                 if (!disk && priv)
50                         continue;
51
52                 os << serializeString(sv.first);
53                 os << serializeLongString(sv.second);
54                 if (version >= 2)
55                         writeU8(os, (priv) ? 1 : 0);
56         }
57
58         m_inventory->serialize(os);
59 }
60
61 void NodeMetadata::deSerialize(std::istream &is, u8 version)
62 {
63         clear();
64         int num_vars = readU32(is);
65         for(int i=0; i<num_vars; i++){
66                 std::string name = deSerializeString(is);
67                 std::string var = deSerializeLongString(is);
68                 m_stringvars[name] = var;
69                 if (version >= 2) {
70                         if (readU8(is) == 1)
71                                 markPrivate(name, true);
72                 }
73         }
74
75         m_inventory->deSerialize(is);
76 }
77
78 void NodeMetadata::clear()
79 {
80         Metadata::clear();
81         m_privatevars.clear();
82         m_inventory->clear();
83 }
84
85 bool NodeMetadata::empty() const
86 {
87         return Metadata::empty() && m_inventory->getLists().empty();
88 }
89
90
91 void NodeMetadata::markPrivate(const std::string &name, bool set)
92 {
93         if (set)
94                 m_privatevars.insert(name);
95         else
96                 m_privatevars.erase(name);
97 }
98
99 int NodeMetadata::countNonPrivate() const
100 {
101         // m_privatevars can contain names not actually present
102         // DON'T: return m_stringvars.size() - m_privatevars.size();
103         int n = 0;
104         for (const auto &sv : m_stringvars) {
105                 if (!isPrivate(sv.first))
106                         n++;
107         }
108         return n;
109 }
110
111 /*
112         NodeMetadataList
113 */
114
115 void NodeMetadataList::serialize(std::ostream &os, u8 blockver, bool disk) const
116 {
117         /*
118                 Version 0 is a placeholder for "nothing to see here; go away."
119         */
120
121         u16 count = countNonEmpty();
122         if (count == 0) {
123                 writeU8(os, 0); // version
124                 return;
125         }
126
127         u8 version = (blockver > 27) ? 2 : 1;
128         writeU8(os, version);
129         writeU16(os, count);
130
131         for (const auto &it : m_data) {
132                 v3s16 p = it.first;
133                 NodeMetadata *data = it.second;
134                 if (data->empty())
135                         continue;
136
137                 u16 p16 = p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE + p.Y * MAP_BLOCKSIZE + p.X;
138                 writeU16(os, p16);
139
140                 data->serialize(os, version, disk);
141         }
142 }
143
144 void NodeMetadataList::deSerialize(std::istream &is, IItemDefManager *item_def_mgr)
145 {
146         clear();
147
148         u8 version = readU8(is);
149
150         if (version == 0) {
151                 // Nothing
152                 return;
153         }
154
155         if (version > 2) {
156                 std::string err_str = std::string(FUNCTION_NAME)
157                         + ": version " + itos(version) + " not supported";
158                 infostream << err_str << std::endl;
159                 throw SerializationError(err_str);
160         }
161
162         u16 count = readU16(is);
163
164         for (u16 i = 0; i < count; i++) {
165                 u16 p16 = readU16(is);
166
167                 v3s16 p;
168                 p.Z = p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
169                 p16 &= MAP_BLOCKSIZE * MAP_BLOCKSIZE - 1;
170                 p.Y = p16 / MAP_BLOCKSIZE;
171                 p16 &= MAP_BLOCKSIZE - 1;
172                 p.X = p16;
173
174                 if (m_data.find(p) != m_data.end()) {
175                         warningstream << "NodeMetadataList::deSerialize(): "
176                                         << "already set data at position " << PP(p)
177                                         << ": Ignoring." << std::endl;
178                         continue;
179                 }
180
181                 NodeMetadata *data = new NodeMetadata(item_def_mgr);
182                 data->deSerialize(is, version);
183                 m_data[p] = data;
184         }
185 }
186
187 NodeMetadataList::~NodeMetadataList()
188 {
189         clear();
190 }
191
192 std::vector<v3s16> NodeMetadataList::getAllKeys()
193 {
194         std::vector<v3s16> keys;
195
196         std::map<v3s16, NodeMetadata *>::const_iterator it;
197         for (it = m_data.begin(); it != m_data.end(); ++it)
198                 keys.push_back(it->first);
199
200         return keys;
201 }
202
203 NodeMetadata *NodeMetadataList::get(v3s16 p)
204 {
205         std::map<v3s16, NodeMetadata *>::const_iterator n = m_data.find(p);
206         if (n == m_data.end())
207                 return NULL;
208         return n->second;
209 }
210
211 void NodeMetadataList::remove(v3s16 p)
212 {
213         NodeMetadata *olddata = get(p);
214         if (olddata) {
215                 delete olddata;
216                 m_data.erase(p);
217         }
218 }
219
220 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
221 {
222         remove(p);
223         m_data.insert(std::make_pair(p, d));
224 }
225
226 void NodeMetadataList::clear()
227 {
228         std::map<v3s16, NodeMetadata*>::iterator it;
229         for (it = m_data.begin(); it != m_data.end(); ++it) {
230                 delete it->second;
231         }
232         m_data.clear();
233 }
234
235 int NodeMetadataList::countNonEmpty() const
236 {
237         int n = 0;
238         std::map<v3s16, NodeMetadata*>::const_iterator it;
239         for (it = m_data.begin(); it != m_data.end(); ++it) {
240                 if (!it->second->empty())
241                         n++;
242         }
243         return n;
244 }