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"
24 #include "inventory.h"
31 core::map<u16, NodeMetadata::Factory> NodeMetadata::m_types;
33 NodeMetadata::NodeMetadata()
37 NodeMetadata::~NodeMetadata()
41 NodeMetadata* NodeMetadata::deSerialize(std::istream &is)
45 is.read((char*)buf, 2);
46 s16 id = readS16(buf);
49 std::string data = deSerializeString(is);
51 // Find factory function
52 core::map<u16, Factory>::Node *n;
56 // If factory is not found, just return.
57 dstream<<"WARNING: NodeMetadata: No factory for typeId="
62 // Try to load the metadata. If it fails, just return.
65 std::istringstream iss(data, std::ios_base::binary);
67 Factory f = n->getValue();
68 NodeMetadata *meta = (*f)(iss);
71 catch(SerializationError &e)
73 dstream<<"WARNING: NodeMetadata: ignoring SerializationError"<<std::endl;
78 void NodeMetadata::serialize(std::ostream &os)
81 writeU16(buf, typeId());
82 os.write((char*)buf, 2);
84 std::ostringstream oss(std::ios_base::binary);
86 os<<serializeString(oss.str());
89 void NodeMetadata::registerType(u16 id, Factory f)
91 core::map<u16, Factory>::Node *n;
95 m_types.insert(id, f);
102 SignNodeMetadata::SignNodeMetadata(std::string text):
105 NodeMetadata::registerType(typeId(), create);
107 u16 SignNodeMetadata::typeId() const
109 return CONTENT_SIGN_WALL;
111 NodeMetadata* SignNodeMetadata::create(std::istream &is)
113 std::string text = deSerializeString(is);
114 return new SignNodeMetadata(text);
116 NodeMetadata* SignNodeMetadata::clone()
118 return new SignNodeMetadata(m_text);
120 void SignNodeMetadata::serializeBody(std::ostream &os)
122 os<<serializeString(m_text);
124 std::string SignNodeMetadata::infoText()
126 return std::string("\"")+m_text+"\"";
133 ChestNodeMetadata::ChestNodeMetadata()
135 NodeMetadata::registerType(typeId(), create);
137 m_inventory = new Inventory();
138 m_inventory->addList("0", 8*4);
140 ChestNodeMetadata::~ChestNodeMetadata()
144 u16 ChestNodeMetadata::typeId() const
146 return CONTENT_CHEST;
148 NodeMetadata* ChestNodeMetadata::create(std::istream &is)
150 ChestNodeMetadata *d = new ChestNodeMetadata();
151 d->m_inventory->deSerialize(is);
154 NodeMetadata* ChestNodeMetadata::clone()
156 ChestNodeMetadata *d = new ChestNodeMetadata();
157 *d->m_inventory = *m_inventory;
160 void ChestNodeMetadata::serializeBody(std::ostream &os)
162 m_inventory->serialize(os);
164 std::string ChestNodeMetadata::infoText()
173 FurnaceNodeMetadata::FurnaceNodeMetadata()
175 NodeMetadata::registerType(typeId(), create);
177 m_inventory = new Inventory();
178 m_inventory->addList("fuel", 1);
179 m_inventory->addList("src", 1);
180 m_inventory->addList("dst", 1);
182 FurnaceNodeMetadata::~FurnaceNodeMetadata()
186 u16 FurnaceNodeMetadata::typeId() const
188 return CONTENT_FURNACE;
190 NodeMetadata* FurnaceNodeMetadata::clone()
192 FurnaceNodeMetadata *d = new FurnaceNodeMetadata();
193 *d->m_inventory = *m_inventory;
196 NodeMetadata* FurnaceNodeMetadata::create(std::istream &is)
198 FurnaceNodeMetadata *d = new FurnaceNodeMetadata();
199 d->m_inventory->deSerialize(is);
200 /*std::string params;
201 std::getline(is, params, '\n');*/
204 void FurnaceNodeMetadata::serializeBody(std::ostream &os)
206 m_inventory->serialize(os);
207 // This line will contain the other parameters
210 std::string FurnaceNodeMetadata::infoText()
214 void FurnaceNodeMetadata::inventoryModified()
216 dstream<<"Furnace inventory modification callback"<<std::endl;
223 void NodeMetadataList::serialize(std::ostream &os)
227 u16 count = m_data.size();
228 writeU16(buf, count);
229 os.write((char*)buf, 2);
231 for(core::map<v3s16, NodeMetadata*>::Iterator
232 i = m_data.getIterator();
233 i.atEnd()==false; i++)
235 v3s16 p = i.getNode()->getKey();
236 NodeMetadata *data = i.getNode()->getValue();
238 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
240 os.write((char*)buf, 2);
246 void NodeMetadataList::deSerialize(std::istream &is)
252 is.read((char*)buf, 2);
253 u16 count = readU16(buf);
255 for(u16 i=0; i<count; i++)
257 is.read((char*)buf, 2);
258 u16 p16 = readU16(buf);
261 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
262 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
263 p.Y += p16 / MAP_BLOCKSIZE;
264 p16 -= p.Y * MAP_BLOCKSIZE;
267 NodeMetadata *data = NodeMetadata::deSerialize(is);
274 dstream<<"WARNING: NodeMetadataList::deSerialize(): "
275 <<"already set data at position"
276 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
282 m_data.insert(p, data);
286 NodeMetadataList::~NodeMetadataList()
288 for(core::map<v3s16, NodeMetadata*>::Iterator
289 i = m_data.getIterator();
290 i.atEnd()==false; i++)
292 delete i.getNode()->getValue();
296 NodeMetadata* NodeMetadataList::get(v3s16 p)
298 core::map<v3s16, NodeMetadata*>::Node *n;
302 return n->getValue();
305 void NodeMetadataList::remove(v3s16 p)
307 NodeMetadata *olddata = get(p);
315 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)