3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "nodemetadata.h"
21 #include "exceptions.h"
23 #include "inventory.h"
25 #include "util/serialize.h"
26 #include "util/basic_macros.h"
27 #include "constants.h" // MAP_BLOCKSIZE
34 NodeMetadata::NodeMetadata(IItemDefManager *item_def_mgr):
35 m_inventory(new Inventory(item_def_mgr))
38 NodeMetadata::~NodeMetadata()
43 void NodeMetadata::serialize(std::ostream &os, u8 version, bool disk) const
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);
52 os << serializeString(sv.first);
53 os << serializeLongString(sv.second);
55 writeU8(os, (priv) ? 1 : 0);
58 m_inventory->serialize(os);
61 void NodeMetadata::deSerialize(std::istream &is, u8 version)
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;
71 markPrivate(name, true);
75 m_inventory->deSerialize(is);
78 void NodeMetadata::clear()
81 m_privatevars.clear();
85 bool NodeMetadata::empty() const
87 return Metadata::empty() && m_inventory->getLists().empty();
91 void NodeMetadata::markPrivate(const std::string &name, bool set)
94 m_privatevars.insert(name);
96 m_privatevars.erase(name);
99 int NodeMetadata::countNonPrivate() const
101 // m_privatevars can contain names not actually present
102 // DON'T: return m_stringvars.size() - m_privatevars.size();
104 for (const auto &sv : m_stringvars) {
105 if (!isPrivate(sv.first))
115 void NodeMetadataList::serialize(std::ostream &os, u8 blockver, bool disk) const
118 Version 0 is a placeholder for "nothing to see here; go away."
121 u16 count = countNonEmpty();
123 writeU8(os, 0); // version
127 u8 version = (blockver > 27) ? 2 : 1;
128 writeU8(os, version);
131 for (const auto &it : m_data) {
133 NodeMetadata *data = it.second;
137 u16 p16 = p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE + p.Y * MAP_BLOCKSIZE + p.X;
140 data->serialize(os, version, disk);
144 void NodeMetadataList::deSerialize(std::istream &is, IItemDefManager *item_def_mgr)
148 u8 version = readU8(is);
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);
162 u16 count = readU16(is);
164 for (u16 i = 0; i < count; i++) {
165 u16 p16 = readU16(is);
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;
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;
181 NodeMetadata *data = new NodeMetadata(item_def_mgr);
182 data->deSerialize(is, version);
187 NodeMetadataList::~NodeMetadataList()
192 std::vector<v3s16> NodeMetadataList::getAllKeys()
194 std::vector<v3s16> keys;
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);
203 NodeMetadata *NodeMetadataList::get(v3s16 p)
205 std::map<v3s16, NodeMetadata *>::const_iterator n = m_data.find(p);
206 if (n == m_data.end())
211 void NodeMetadataList::remove(v3s16 p)
213 NodeMetadata *olddata = get(p);
220 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
223 m_data.insert(std::make_pair(p, d));
226 void NodeMetadataList::clear()
228 std::map<v3s16, NodeMetadata*>::iterator it;
229 for (it = m_data.begin(); it != m_data.end(); ++it) {
235 int NodeMetadataList::countNonEmpty() const
238 std::map<v3s16, NodeMetadata*>::const_iterator it;
239 for (it = m_data.begin(); it != m_data.end(); ++it) {
240 if (!it->second->empty())