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 void NodeMetadataList::serialize(std::ostream &os)
115 u16 count = m_data.size();
116 writeU16(buf, count);
117 os.write((char*)buf, 2);
119 for(core::map<v3s16, NodeMetadata*>::Iterator
120 i = m_data.getIterator();
121 i.atEnd()==false; i++)
123 v3s16 p = i.getNode()->getKey();
124 NodeMetadata *data = i.getNode()->getValue();
126 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
128 os.write((char*)buf, 2);
134 void NodeMetadataList::deSerialize(std::istream &is)
140 is.read((char*)buf, 2);
141 u16 count = readU16(buf);
143 for(u16 i=0; i<count; i++)
145 is.read((char*)buf, 2);
146 u16 p16 = readU16(buf);
149 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
150 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
151 p.Y += p16 / MAP_BLOCKSIZE;
152 p16 -= p.Y * MAP_BLOCKSIZE;
157 dstream<<"ERROR: NodeMetadataList::deSerialize(): "
158 <<"already set data at position"
159 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
161 throw SerializationError("NodeMetadataList::deSerialize()");
164 NodeMetadata *data = NodeMetadata::deSerialize(is);
166 m_data.insert(p, data);
170 NodeMetadataList::~NodeMetadataList()
172 for(core::map<v3s16, NodeMetadata*>::Iterator
173 i = m_data.getIterator();
174 i.atEnd()==false; i++)
176 delete i.getNode()->getValue();
180 NodeMetadata* NodeMetadataList::get(v3s16 p)
182 core::map<v3s16, NodeMetadata*>::Node *n;
186 return n->getValue();
189 void NodeMetadataList::remove(v3s16 p)
191 NodeMetadata *olddata = get(p);
199 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)