Line data Source code
1 : /*
2 : Minetest
3 : Copyright (C) 2010-2013 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 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.
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 Lesser General Public License for more details.
14 :
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.
18 : */
19 :
20 : #include "content_nodemeta.h"
21 : #include "nodemetadata.h"
22 : #include "nodetimer.h"
23 : #include "inventory.h"
24 : #include "log.h"
25 : #include "serialization.h"
26 : #include "util/serialize.h"
27 : #include "util/string.h"
28 : #include "constants.h" // MAP_BLOCKSIZE
29 : #include <sstream>
30 :
31 : #define NODEMETA_GENERIC 1
32 : #define NODEMETA_SIGN 14
33 : #define NODEMETA_CHEST 15
34 : #define NODEMETA_FURNACE 16
35 : #define NODEMETA_LOCKABLE_CHEST 17
36 :
37 : // Returns true if node timer must be set
38 0 : static bool content_nodemeta_deserialize_legacy_body(
39 : std::istream &is, s16 id, NodeMetadata *meta)
40 : {
41 0 : meta->clear();
42 :
43 0 : if(id == NODEMETA_GENERIC) // GenericNodeMetadata (0.4-dev)
44 : {
45 0 : meta->getInventory()->deSerialize(is);
46 0 : deSerializeLongString(is); // m_text
47 0 : deSerializeString(is); // m_owner
48 :
49 0 : meta->setString("infotext",deSerializeString(is));
50 0 : meta->setString("formspec",deSerializeString(is));
51 0 : readU8(is); // m_allow_text_input
52 0 : readU8(is); // m_allow_removal
53 0 : readU8(is); // m_enforce_owner
54 :
55 0 : int num_vars = readU32(is);
56 0 : for(int i=0; i<num_vars; i++){
57 0 : std::string name = deSerializeString(is);
58 0 : std::string var = deSerializeLongString(is);
59 0 : meta->setString(name, var);
60 : }
61 0 : return false;
62 : }
63 0 : else if(id == NODEMETA_SIGN) // SignNodeMetadata
64 : {
65 0 : meta->setString("text", deSerializeString(is));
66 : //meta->setString("infotext","\"${text}\"");
67 0 : meta->setString("infotext",
68 0 : std::string("\"") + meta->getString("text") + "\"");
69 0 : meta->setString("formspec","field[text;;${text}]");
70 0 : return false;
71 : }
72 0 : else if(id == NODEMETA_CHEST) // ChestNodeMetadata
73 : {
74 0 : meta->getInventory()->deSerialize(is);
75 :
76 : // Rename inventory list "0" to "main"
77 0 : Inventory *inv = meta->getInventory();
78 0 : if(!inv->getList("main") && inv->getList("0")){
79 0 : inv->getList("0")->setName("main");
80 : }
81 : assert(inv->getList("main") && !inv->getList("0"));
82 :
83 0 : meta->setString("formspec","size[8,9]"
84 : "list[current_name;main;0,0;8,4;]"
85 0 : "list[current_player;main;0,5;8,4;]");
86 0 : return false;
87 : }
88 0 : else if(id == NODEMETA_LOCKABLE_CHEST) // LockingChestNodeMetadata
89 : {
90 0 : meta->setString("owner", deSerializeString(is));
91 0 : meta->getInventory()->deSerialize(is);
92 :
93 : // Rename inventory list "0" to "main"
94 0 : Inventory *inv = meta->getInventory();
95 0 : if(!inv->getList("main") && inv->getList("0")){
96 0 : inv->getList("0")->setName("main");
97 : }
98 : assert(inv->getList("main") && !inv->getList("0"));
99 :
100 0 : meta->setString("formspec","size[8,9]"
101 : "list[current_name;main;0,0;8,4;]"
102 0 : "list[current_player;main;0,5;8,4;]");
103 0 : return false;
104 : }
105 0 : else if(id == NODEMETA_FURNACE) // FurnaceNodeMetadata
106 : {
107 0 : meta->getInventory()->deSerialize(is);
108 0 : int temp = 0;
109 0 : is>>temp;
110 0 : meta->setString("fuel_totaltime", ftos((float)temp/10));
111 0 : temp = 0;
112 0 : is>>temp;
113 0 : meta->setString("fuel_time", ftos((float)temp/10));
114 0 : temp = 0;
115 0 : is>>temp;
116 : //meta->setString("src_totaltime", ftos((float)temp/10));
117 0 : temp = 0;
118 0 : is>>temp;
119 0 : meta->setString("src_time", ftos((float)temp/10));
120 :
121 0 : meta->setString("formspec","size[8,9]"
122 : "list[current_name;fuel;2,3;1,1;]"
123 : "list[current_name;src;2,1;1,1;]"
124 : "list[current_name;dst;5,1;2,2;]"
125 0 : "list[current_player;main;0,5;8,4;]");
126 0 : return true;
127 : }
128 : else
129 : {
130 0 : throw SerializationError("Unknown legacy node metadata");
131 : }
132 : }
133 :
134 0 : static bool content_nodemeta_deserialize_legacy_meta(
135 : std::istream &is, NodeMetadata *meta)
136 : {
137 : // Read id
138 0 : s16 id = readS16(is);
139 :
140 : // Read data
141 0 : std::string data = deSerializeString(is);
142 0 : std::istringstream tmp_is(data, std::ios::binary);
143 0 : return content_nodemeta_deserialize_legacy_body(tmp_is, id, meta);
144 : }
145 :
146 0 : void content_nodemeta_deserialize_legacy(std::istream &is,
147 : NodeMetadataList *meta, NodeTimerList *timers,
148 : IGameDef *gamedef)
149 : {
150 0 : meta->clear();
151 0 : timers->clear();
152 :
153 0 : u16 version = readU16(is);
154 :
155 0 : if(version > 1)
156 : {
157 0 : infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
158 0 : <<std::endl;
159 0 : throw SerializationError(__FUNCTION_NAME);
160 : }
161 :
162 0 : u16 count = readU16(is);
163 :
164 0 : for(u16 i=0; i<count; i++)
165 : {
166 0 : u16 p16 = readU16(is);
167 :
168 0 : v3s16 p(0,0,0);
169 0 : p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
170 0 : p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
171 0 : p.Y += p16 / MAP_BLOCKSIZE;
172 0 : p16 -= p.Y * MAP_BLOCKSIZE;
173 0 : p.X += p16;
174 :
175 0 : if(meta->get(p) != NULL)
176 : {
177 0 : infostream<<"WARNING: "<<__FUNCTION_NAME<<": "
178 0 : <<"already set data at position"
179 0 : <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
180 0 : <<std::endl;
181 0 : continue;
182 : }
183 :
184 0 : NodeMetadata *data = new NodeMetadata(gamedef);
185 0 : bool need_timer = content_nodemeta_deserialize_legacy_meta(is, data);
186 0 : meta->set(p, data);
187 :
188 0 : if(need_timer)
189 0 : timers->set(p, NodeTimer(1., 0.));
190 : }
191 0 : }
192 :
193 0 : void content_nodemeta_serialize_legacy(std::ostream &os, NodeMetadataList *meta)
194 : {
195 : // Sorry, I was too lazy to implement this. --kahrl
196 0 : writeU16(os, 1); // version
197 0 : writeU16(os, 0); // count
198 3 : }
199 :
200 : // END
|