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 "constants.h" // MAP_BLOCKSIZE
33 NodeMetadata::NodeMetadata(IItemDefManager *item_def_mgr):
35 m_inventory(new Inventory(item_def_mgr))
39 NodeMetadata::~NodeMetadata()
44 void NodeMetadata::serialize(std::ostream &os) const
46 int num_vars = m_stringvars.size();
47 writeU32(os, num_vars);
48 for (StringMap::const_iterator
49 it = m_stringvars.begin();
50 it != m_stringvars.end(); ++it) {
51 os << serializeString(it->first);
52 os << serializeLongString(it->second);
55 m_inventory->serialize(os);
58 void NodeMetadata::deSerialize(std::istream &is)
61 int num_vars = readU32(is);
62 for(int i=0; i<num_vars; i++){
63 std::string name = deSerializeString(is);
64 std::string var = deSerializeLongString(is);
65 m_stringvars[name] = var;
68 m_inventory->deSerialize(is);
71 void NodeMetadata::clear()
81 void NodeMetadataList::serialize(std::ostream &os) const
84 Version 0 is a placeholder for "nothing to see here; go away."
88 writeU8(os, 0); // version
92 writeU8(os, 1); // version
94 u16 count = m_data.size();
97 for(std::map<v3s16, NodeMetadata*>::const_iterator
99 i != m_data.end(); ++i)
102 NodeMetadata *data = i->second;
104 u16 p16 = p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE + p.Y * MAP_BLOCKSIZE + p.X;
111 void NodeMetadataList::deSerialize(std::istream &is, IItemDefManager *item_def_mgr)
115 u8 version = readU8(is);
123 std::string err_str = std::string(__FUNCTION_NAME)
124 + ": version " + itos(version) + " not supported";
125 infostream << err_str << std::endl;
126 throw SerializationError(err_str);
129 u16 count = readU16(is);
131 for (u16 i=0; i < count; i++) {
132 u16 p16 = readU16(is);
135 p.Z = p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
136 p16 &= MAP_BLOCKSIZE * MAP_BLOCKSIZE - 1;
137 p.Y = p16 / MAP_BLOCKSIZE;
138 p16 &= MAP_BLOCKSIZE - 1;
141 if (m_data.find(p) != m_data.end()) {
142 warningstream<<"NodeMetadataList::deSerialize(): "
143 <<"already set data at position"
144 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
149 NodeMetadata *data = new NodeMetadata(item_def_mgr);
150 data->deSerialize(is);
155 NodeMetadataList::~NodeMetadataList()
160 std::vector<v3s16> NodeMetadataList::getAllKeys()
162 std::vector<v3s16> keys;
164 std::map<v3s16, NodeMetadata *>::const_iterator it;
165 for (it = m_data.begin(); it != m_data.end(); ++it)
166 keys.push_back(it->first);
171 NodeMetadata *NodeMetadataList::get(v3s16 p)
173 std::map<v3s16, NodeMetadata *>::const_iterator n = m_data.find(p);
174 if (n == m_data.end())
179 void NodeMetadataList::remove(v3s16 p)
181 NodeMetadata *olddata = get(p);
188 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
191 m_data.insert(std::make_pair(p, d));
194 void NodeMetadataList::clear()
196 std::map<v3s16, NodeMetadata*>::iterator it;
197 for (it = m_data.begin(); it != m_data.end(); ++it) {
203 std::string NodeMetadata::getString(const std::string &name,
204 unsigned short recursion) const
206 StringMap::const_iterator it = m_stringvars.find(name);
207 if (it == m_stringvars.end())
210 return resolveString(it->second, recursion);
213 void NodeMetadata::setString(const std::string &name, const std::string &var)
216 m_stringvars.erase(name);
218 m_stringvars[name] = var;
222 std::string NodeMetadata::resolveString(const std::string &str,
223 unsigned short recursion) const
228 if (str.substr(0, 2) == "${" && str[str.length() - 1] == '}') {
229 return getString(str.substr(2, str.length() - 3), recursion + 1);