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