]> git.lizzy.rs Git - dragonfireclient.git/blob - src/nodemetadata.cpp
forgot some files
[dragonfireclient.git] / src / nodemetadata.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "nodemetadata.h"
21 #include "utility.h"
22 #include "mapnode.h"
23 #include "exceptions.h"
24
25 /*
26         NodeMetadata
27 */
28
29 core::map<u16, NodeMetadata::Factory> NodeMetadata::m_types;
30
31 NodeMetadata::NodeMetadata()
32 {
33 }
34
35 NodeMetadata::~NodeMetadata()
36 {
37 }
38
39 NodeMetadata* NodeMetadata::deSerialize(std::istream &is)
40 {
41         u8 buf[2];
42         is.read((char*)buf, 2);
43         s16 id = readS16(buf);
44
45         core::map<u16, Factory>::Node *n;
46         n = m_types.find(id);
47         if(n == NULL)
48         {
49                 dstream<<"NodeMetadata(): No factory for typeId="<<id<<std::endl;
50                 throw SerializationError("Unknown metadata id");
51         }
52         
53         Factory f = n->getValue();
54         NodeMetadata *meta = (*f)(is);
55         return meta;
56 }
57
58 void NodeMetadata::serialize(std::ostream &os)
59 {
60         u8 buf[2];
61         writeU16(buf, typeId());
62         os.write((char*)buf, 2);
63
64         serializeBody(os);
65 }
66
67 void NodeMetadata::registerType(u16 id, Factory f)
68 {
69         core::map<u16, Factory>::Node *n;
70         n = m_types.find(id);
71         if(n)
72                 return;
73         m_types.insert(id, f);
74 }
75
76 /*
77         SignNodeMetadata
78 */
79
80 SignNodeMetadata::SignNodeMetadata(std::string text):
81         m_text(text)
82 {
83         NodeMetadata::registerType(typeId(), create);
84 }
85 u16 SignNodeMetadata::typeId() const
86 {
87         return CONTENT_SIGN_WALL;
88 }
89 NodeMetadata* SignNodeMetadata::create(std::istream &is)
90 {
91         std::string text = deSerializeString(is);
92         return new SignNodeMetadata(text);
93 }
94 NodeMetadata* SignNodeMetadata::clone()
95 {
96         return new SignNodeMetadata(m_text);
97 }
98 void SignNodeMetadata::serializeBody(std::ostream &os)
99 {
100         os<<serializeString(m_text);
101 }
102 std::string SignNodeMetadata::infoText()
103 {
104         return std::string("\"")+m_text+"\"";
105 }
106
107 /*
108         NodeMetadatalist
109 */
110
111 void NodeMetadataList::serialize(std::ostream &os)
112 {
113         u8 buf[6];
114         
115         u16 count = m_data.size();
116         writeU16(buf, count);
117         os.write((char*)buf, 2);
118
119         for(core::map<v3s16, NodeMetadata*>::Iterator
120                         i = m_data.getIterator();
121                         i.atEnd()==false; i++)
122         {
123                 v3s16 p = i.getNode()->getKey();
124                 NodeMetadata *data = i.getNode()->getValue();
125                 
126                 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
127                 writeU16(buf, p16);
128                 os.write((char*)buf, 2);
129
130                 data->serialize(os);
131         }
132         
133 }
134 void NodeMetadataList::deSerialize(std::istream &is)
135 {
136         m_data.clear();
137
138         u8 buf[6];
139         
140         is.read((char*)buf, 2);
141         u16 count = readU16(buf);
142         
143         for(u16 i=0; i<count; i++)
144         {
145                 is.read((char*)buf, 2);
146                 u16 p16 = readU16(buf);
147
148                 v3s16 p(0,0,0);
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;
153                 p.X += p16;
154                 
155                 if(m_data.find(p))
156                 {
157                         dstream<<"ERROR: NodeMetadataList::deSerialize(): "
158                                         <<"already set data at position"
159                                         <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
160                                         <<std::endl;
161                         throw SerializationError("NodeMetadataList::deSerialize()");
162                 }
163
164                 NodeMetadata *data = NodeMetadata::deSerialize(is);
165                 
166                 m_data.insert(p, data);
167         }
168 }
169         
170 NodeMetadataList::~NodeMetadataList()
171 {
172         for(core::map<v3s16, NodeMetadata*>::Iterator
173                         i = m_data.getIterator();
174                         i.atEnd()==false; i++)
175         {
176                 delete i.getNode()->getValue();
177         }
178 }
179
180 NodeMetadata* NodeMetadataList::get(v3s16 p)
181 {
182         core::map<v3s16, NodeMetadata*>::Node *n;
183         n = m_data.find(p);
184         if(n == NULL)
185                 return NULL;
186         return n->getValue();
187 }
188
189 void NodeMetadataList::remove(v3s16 p)
190 {
191         NodeMetadata *olddata = get(p);
192         if(olddata)
193         {
194                 delete olddata;
195                 m_data.remove(p);
196         }
197 }
198
199 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
200 {
201         remove(p);
202         m_data.insert(p, d);
203 }
204