3 Copyright (C) 2010-2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU 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"
23 #include "exceptions.h"
29 core::map<u16, NodeMetadata::Factory> NodeMetadata::m_types;
31 NodeMetadata::NodeMetadata()
35 NodeMetadata::~NodeMetadata()
39 NodeMetadata* NodeMetadata::deSerialize(std::istream &is)
42 is.read((char*)buf, 2);
43 s16 id = readS16(buf);
45 core::map<u16, Factory>::Node *n;
49 dstream<<"NodeMetadata(): No factory for typeId="<<id<<std::endl;
50 throw SerializationError("Unknown metadata id");
53 Factory f = n->getValue();
54 NodeMetadata *meta = (*f)(is);
58 void NodeMetadata::serialize(std::ostream &os)
61 writeU16(buf, typeId());
62 os.write((char*)buf, 2);
67 void NodeMetadata::registerType(u16 id, Factory f)
69 core::map<u16, Factory>::Node *n;
73 m_types.insert(id, f);
80 SignNodeMetadata::SignNodeMetadata(std::string text):
83 NodeMetadata::registerType(typeId(), create);
85 u16 SignNodeMetadata::typeId() const
87 return CONTENT_SIGN_WALL;
89 NodeMetadata* SignNodeMetadata::create(std::istream &is)
91 std::string text = deSerializeString(is);
92 return new SignNodeMetadata(text);
94 NodeMetadata* SignNodeMetadata::clone()
96 return new SignNodeMetadata(m_text);
98 void SignNodeMetadata::serializeBody(std::ostream &os)
100 os<<serializeString(m_text);
102 std::string SignNodeMetadata::infoText()
104 return std::string("\"")+m_text+"\"";
111 ChestNodeMetadata::ChestNodeMetadata()
113 NodeMetadata::registerType(typeId(), create);
115 u16 ChestNodeMetadata::typeId() const
117 return CONTENT_CHEST;
119 NodeMetadata* ChestNodeMetadata::create(std::istream &is)
121 return new ChestNodeMetadata();
123 NodeMetadata* ChestNodeMetadata::clone()
125 return new ChestNodeMetadata();
127 void ChestNodeMetadata::serializeBody(std::ostream &os)
130 std::string ChestNodeMetadata::infoText()
139 void NodeMetadataList::serialize(std::ostream &os)
143 u16 count = m_data.size();
144 writeU16(buf, count);
145 os.write((char*)buf, 2);
147 for(core::map<v3s16, NodeMetadata*>::Iterator
148 i = m_data.getIterator();
149 i.atEnd()==false; i++)
151 v3s16 p = i.getNode()->getKey();
152 NodeMetadata *data = i.getNode()->getValue();
154 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
156 os.write((char*)buf, 2);
162 void NodeMetadataList::deSerialize(std::istream &is)
168 is.read((char*)buf, 2);
169 u16 count = readU16(buf);
171 for(u16 i=0; i<count; i++)
173 is.read((char*)buf, 2);
174 u16 p16 = readU16(buf);
177 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
178 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
179 p.Y += p16 / MAP_BLOCKSIZE;
180 p16 -= p.Y * MAP_BLOCKSIZE;
185 dstream<<"ERROR: NodeMetadataList::deSerialize(): "
186 <<"already set data at position"
187 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
189 throw SerializationError("NodeMetadataList::deSerialize()");
192 NodeMetadata *data = NodeMetadata::deSerialize(is);
194 m_data.insert(p, data);
198 NodeMetadataList::~NodeMetadataList()
200 for(core::map<v3s16, NodeMetadata*>::Iterator
201 i = m_data.getIterator();
202 i.atEnd()==false; i++)
204 delete i.getNode()->getValue();
208 NodeMetadata* NodeMetadataList::get(v3s16 p)
210 core::map<v3s16, NodeMetadata*>::Node *n;
214 return n->getValue();
217 void NodeMetadataList::remove(v3s16 p)
219 NodeMetadata *olddata = get(p);
227 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)