]> git.lizzy.rs Git - minetest.git/blob - src/mapblock.cpp
Switch MapBlock compression to zstd (#10788)
[minetest.git] / src / mapblock.cpp
1 /*
2 Minetest
3 Copyright (C) 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 "mapblock.h"
21
22 #include <sstream>
23 #include "map.h"
24 #include "light.h"
25 #include "nodedef.h"
26 #include "nodemetadata.h"
27 #include "gamedef.h"
28 #include "log.h"
29 #include "nameidmapping.h"
30 #include "content_mapnode.h" // For legacy name-id mapping
31 #include "content_nodemeta.h" // For legacy deserialization
32 #include "serialization.h"
33 #ifndef SERVER
34 #include "client/mapblock_mesh.h"
35 #endif
36 #include "porting.h"
37 #include "util/string.h"
38 #include "util/serialize.h"
39 #include "util/basic_macros.h"
40
41 static const char *modified_reason_strings[] = {
42         "initial",
43         "reallocate",
44         "setIsUnderground",
45         "setLightingExpired",
46         "setGenerated",
47         "setNode",
48         "setNodeNoCheck",
49         "setTimestamp",
50         "NodeMetaRef::reportMetadataChange",
51         "clearAllObjects",
52         "Timestamp expired (step)",
53         "addActiveObjectRaw",
54         "removeRemovedObjects/remove",
55         "removeRemovedObjects/deactivate",
56         "Stored list cleared in activateObjects due to overflow",
57         "deactivateFarObjects: Static data moved in",
58         "deactivateFarObjects: Static data moved out",
59         "deactivateFarObjects: Static data changed considerably",
60         "finishBlockMake: expireDayNightDiff",
61         "unknown",
62 };
63
64
65 /*
66         MapBlock
67 */
68
69 MapBlock::MapBlock(Map *parent, v3s16 pos, IGameDef *gamedef, bool dummy):
70                 m_parent(parent),
71                 m_pos(pos),
72                 m_pos_relative(pos * MAP_BLOCKSIZE),
73                 m_gamedef(gamedef)
74 {
75         if (!dummy)
76                 reallocate();
77 }
78
79 MapBlock::~MapBlock()
80 {
81 #ifndef SERVER
82         {
83                 delete mesh;
84                 mesh = nullptr;
85         }
86 #endif
87
88         delete[] data;
89 }
90
91 bool MapBlock::isValidPositionParent(v3s16 p)
92 {
93         if (isValidPosition(p)) {
94                 return true;
95         }
96
97         return m_parent->isValidPosition(getPosRelative() + p);
98 }
99
100 MapNode MapBlock::getNodeParent(v3s16 p, bool *is_valid_position)
101 {
102         if (!isValidPosition(p))
103                 return m_parent->getNode(getPosRelative() + p, is_valid_position);
104
105         if (!data) {
106                 if (is_valid_position)
107                         *is_valid_position = false;
108                 return {CONTENT_IGNORE};
109         }
110         if (is_valid_position)
111                 *is_valid_position = true;
112         return data[p.Z * zstride + p.Y * ystride + p.X];
113 }
114
115 std::string MapBlock::getModifiedReasonString()
116 {
117         std::string reason;
118
119         const u32 ubound = MYMIN(sizeof(m_modified_reason) * CHAR_BIT,
120                 ARRLEN(modified_reason_strings));
121
122         for (u32 i = 0; i != ubound; i++) {
123                 if ((m_modified_reason & (1 << i)) == 0)
124                         continue;
125
126                 reason += modified_reason_strings[i];
127                 reason += ", ";
128         }
129
130         if (reason.length() > 2)
131                 reason.resize(reason.length() - 2);
132
133         return reason;
134 }
135
136
137 void MapBlock::copyTo(VoxelManipulator &dst)
138 {
139         v3s16 data_size(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
140         VoxelArea data_area(v3s16(0,0,0), data_size - v3s16(1,1,1));
141
142         // Copy from data to VoxelManipulator
143         dst.copyFrom(data, data_area, v3s16(0,0,0),
144                         getPosRelative(), data_size);
145 }
146
147 void MapBlock::copyFrom(VoxelManipulator &dst)
148 {
149         v3s16 data_size(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
150         VoxelArea data_area(v3s16(0,0,0), data_size - v3s16(1,1,1));
151
152         // Copy from VoxelManipulator to data
153         dst.copyTo(data, data_area, v3s16(0,0,0),
154                         getPosRelative(), data_size);
155 }
156
157 void MapBlock::actuallyUpdateDayNightDiff()
158 {
159         const NodeDefManager *nodemgr = m_gamedef->ndef();
160
161         // Running this function un-expires m_day_night_differs
162         m_day_night_differs_expired = false;
163
164         if (!data) {
165                 m_day_night_differs = false;
166                 return;
167         }
168
169         bool differs = false;
170
171         /*
172                 Check if any lighting value differs
173         */
174
175         MapNode previous_n(CONTENT_IGNORE);
176         for (u32 i = 0; i < nodecount; i++) {
177                 MapNode n = data[i];
178
179                 // If node is identical to previous node, don't verify if it differs
180                 if (n == previous_n)
181                         continue;
182
183                 differs = !n.isLightDayNightEq(nodemgr);
184                 if (differs)
185                         break;
186                 previous_n = n;
187         }
188
189         /*
190                 If some lighting values differ, check if the whole thing is
191                 just air. If it is just air, differs = false
192         */
193         if (differs) {
194                 bool only_air = true;
195                 for (u32 i = 0; i < nodecount; i++) {
196                         MapNode &n = data[i];
197                         if (n.getContent() != CONTENT_AIR) {
198                                 only_air = false;
199                                 break;
200                         }
201                 }
202                 if (only_air)
203                         differs = false;
204         }
205
206         // Set member variable
207         m_day_night_differs = differs;
208 }
209
210 void MapBlock::expireDayNightDiff()
211 {
212         if (!data) {
213                 m_day_night_differs = false;
214                 m_day_night_differs_expired = false;
215                 return;
216         }
217
218         m_day_night_differs_expired = true;
219 }
220
221 s16 MapBlock::getGroundLevel(v2s16 p2d)
222 {
223         if(isDummy())
224                 return -3;
225         try
226         {
227                 s16 y = MAP_BLOCKSIZE-1;
228                 for(; y>=0; y--)
229                 {
230                         MapNode n = getNodeRef(p2d.X, y, p2d.Y);
231                         if (m_gamedef->ndef()->get(n).walkable) {
232                                 if(y == MAP_BLOCKSIZE-1)
233                                         return -2;
234
235                                 return y;
236                         }
237                 }
238                 return -1;
239         }
240         catch(InvalidPositionException &e)
241         {
242                 return -3;
243         }
244 }
245
246 /*
247         Serialization
248 */
249 // List relevant id-name pairs for ids in the block using nodedef
250 // Renumbers the content IDs (starting at 0 and incrementing
251 // use static memory requires about 65535 * sizeof(int) ram in order to be
252 // sure we can handle all content ids. But it's absolutely worth it as it's
253 // a speedup of 4 for one of the major time consuming functions on storing
254 // mapblocks.
255 static content_t getBlockNodeIdMapping_mapping[USHRT_MAX + 1];
256 static void getBlockNodeIdMapping(NameIdMapping *nimap, MapNode *nodes,
257         const NodeDefManager *nodedef)
258 {
259         memset(getBlockNodeIdMapping_mapping, 0xFF, (USHRT_MAX + 1) * sizeof(content_t));
260
261         std::set<content_t> unknown_contents;
262         content_t id_counter = 0;
263         for (u32 i = 0; i < MapBlock::nodecount; i++) {
264                 content_t global_id = nodes[i].getContent();
265                 content_t id = CONTENT_IGNORE;
266
267                 // Try to find an existing mapping
268                 if (getBlockNodeIdMapping_mapping[global_id] != 0xFFFF) {
269                         id = getBlockNodeIdMapping_mapping[global_id];
270                 }
271                 else
272                 {
273                         // We have to assign a new mapping
274                         id = id_counter++;
275                         getBlockNodeIdMapping_mapping[global_id] = id;
276
277                         const ContentFeatures &f = nodedef->get(global_id);
278                         const std::string &name = f.name;
279                         if (name.empty())
280                                 unknown_contents.insert(global_id);
281                         else
282                                 nimap->set(id, name);
283                 }
284
285                 // Update the MapNode
286                 nodes[i].setContent(id);
287         }
288         for (u16 unknown_content : unknown_contents) {
289                 errorstream << "getBlockNodeIdMapping(): IGNORING ERROR: "
290                                 << "Name for node id " << unknown_content << " not known" << std::endl;
291         }
292 }
293 // Correct ids in the block to match nodedef based on names.
294 // Unknown ones are added to nodedef.
295 // Will not update itself to match id-name pairs in nodedef.
296 static void correctBlockNodeIds(const NameIdMapping *nimap, MapNode *nodes,
297                 IGameDef *gamedef)
298 {
299         const NodeDefManager *nodedef = gamedef->ndef();
300         // This means the block contains incorrect ids, and we contain
301         // the information to convert those to names.
302         // nodedef contains information to convert our names to globally
303         // correct ids.
304         std::unordered_set<content_t> unnamed_contents;
305         std::unordered_set<std::string> unallocatable_contents;
306
307         bool previous_exists = false;
308         content_t previous_local_id = CONTENT_IGNORE;
309         content_t previous_global_id = CONTENT_IGNORE;
310
311         for (u32 i = 0; i < MapBlock::nodecount; i++) {
312                 content_t local_id = nodes[i].getContent();
313                 // If previous node local_id was found and same than before, don't lookup maps
314                 // apply directly previous resolved id
315                 // This permits to massively improve loading performance when nodes are similar
316                 // example: default:air, default:stone are massively present
317                 if (previous_exists && local_id == previous_local_id) {
318                         nodes[i].setContent(previous_global_id);
319                         continue;
320                 }
321
322                 std::string name;
323                 if (!nimap->getName(local_id, name)) {
324                         unnamed_contents.insert(local_id);
325                         previous_exists = false;
326                         continue;
327                 }
328
329                 content_t global_id;
330                 if (!nodedef->getId(name, global_id)) {
331                         global_id = gamedef->allocateUnknownNodeId(name);
332                         if (global_id == CONTENT_IGNORE) {
333                                 unallocatable_contents.insert(name);
334                                 previous_exists = false;
335                                 continue;
336                         }
337                 }
338                 nodes[i].setContent(global_id);
339
340                 // Save previous node local_id & global_id result
341                 previous_local_id = local_id;
342                 previous_global_id = global_id;
343                 previous_exists = true;
344         }
345
346         for (const content_t c: unnamed_contents) {
347                 errorstream << "correctBlockNodeIds(): IGNORING ERROR: "
348                                 << "Block contains id " << c
349                                 << " with no name mapping" << std::endl;
350         }
351         for (const std::string &node_name: unallocatable_contents) {
352                 errorstream << "correctBlockNodeIds(): IGNORING ERROR: "
353                                 << "Could not allocate global id for node name \""
354                                 << node_name << "\"" << std::endl;
355         }
356 }
357
358 void MapBlock::serialize(std::ostream &os_compressed, u8 version, bool disk, int compression_level)
359 {
360         if(!ser_ver_supported(version))
361                 throw VersionMismatchException("ERROR: MapBlock format not supported");
362
363         if (!data)
364                 throw SerializationError("ERROR: Not writing dummy block.");
365
366         FATAL_ERROR_IF(version < SER_FMT_VER_LOWEST_WRITE, "Serialisation version error");
367
368         std::ostringstream os_raw(std::ios_base::binary);
369         std::ostream &os = version >= 29 ? os_raw : os_compressed;
370
371         // First byte
372         u8 flags = 0;
373         if(is_underground)
374                 flags |= 0x01;
375         if(getDayNightDiff())
376                 flags |= 0x02;
377         if (!m_generated)
378                 flags |= 0x08;
379         writeU8(os, flags);
380         if (version >= 27) {
381                 writeU16(os, m_lighting_complete);
382         }
383
384         /*
385                 Bulk node data
386         */
387         NameIdMapping nimap;
388         SharedBuffer<u8> buf;
389         const u8 content_width = 2;
390         const u8 params_width = 2;
391         if(disk)
392         {
393                 MapNode *tmp_nodes = new MapNode[nodecount];
394                 memcpy(tmp_nodes, data, nodecount * sizeof(MapNode));
395                 getBlockNodeIdMapping(&nimap, tmp_nodes, m_gamedef->ndef());
396
397                 buf = MapNode::serializeBulk(version, tmp_nodes, nodecount,
398                                 content_width, params_width);
399                 delete[] tmp_nodes;
400
401                 // write timestamp and node/id mapping first
402                 if (version >= 29) {
403                         writeU32(os, getTimestamp());
404
405                         nimap.serialize(os);
406                 }
407         }
408         else
409         {
410                 buf = MapNode::serializeBulk(version, data, nodecount,
411                                 content_width, params_width);
412         }
413
414         writeU8(os, content_width);
415         writeU8(os, params_width);
416         if (version >= 29) {
417                 os.write(reinterpret_cast<char*>(*buf), buf.getSize());
418         } else {
419                 // prior to 29 node data was compressed individually
420                 compress(buf, os, version, compression_level);
421         }
422
423         /*
424                 Node metadata
425         */
426         if (version >= 29) {
427                 m_node_metadata.serialize(os, version, disk);
428         } else {
429                 // use os_raw from above to avoid allocating another stream object
430                 m_node_metadata.serialize(os_raw, version, disk);
431                 // prior to 29 node data was compressed individually
432                 compress(os_raw.str(), os, version, compression_level);
433         }
434
435         /*
436                 Data that goes to disk, but not the network
437         */
438         if(disk)
439         {
440                 if(version <= 24){
441                         // Node timers
442                         m_node_timers.serialize(os, version);
443                 }
444
445                 // Static objects
446                 m_static_objects.serialize(os);
447
448                 if(version < 29){
449                         // Timestamp
450                         writeU32(os, getTimestamp());
451
452                         // Write block-specific node definition id mapping
453                         nimap.serialize(os);
454                 }
455
456                 if(version >= 25){
457                         // Node timers
458                         m_node_timers.serialize(os, version);
459                 }
460         }
461
462         if (version >= 29) {
463                 // now compress the whole thing
464                 compress(os_raw.str(), os_compressed, version, compression_level);
465         }
466 }
467
468 void MapBlock::serializeNetworkSpecific(std::ostream &os)
469 {
470         if (!data) {
471                 throw SerializationError("ERROR: Not writing dummy block.");
472         }
473
474         writeU8(os, 2); // version
475 }
476
477 void MapBlock::deSerialize(std::istream &in_compressed, u8 version, bool disk)
478 {
479         if(!ser_ver_supported(version))
480                 throw VersionMismatchException("ERROR: MapBlock format not supported");
481
482         TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())<<std::endl);
483
484         m_day_night_differs_expired = false;
485
486         if(version <= 21)
487         {
488                 deSerialize_pre22(in_compressed, version, disk);
489                 return;
490         }
491
492         // Decompress the whole block (version >= 29)
493         std::stringstream in_raw(std::ios_base::binary | std::ios_base::in | std::ios_base::out);
494         if (version >= 29)
495                 decompress(in_compressed, in_raw, version);
496         std::istream &is = version >= 29 ? in_raw : in_compressed;
497
498         u8 flags = readU8(is);
499         is_underground = (flags & 0x01) != 0;
500         m_day_night_differs = (flags & 0x02) != 0;
501         if (version < 27)
502                 m_lighting_complete = 0xFFFF;
503         else
504                 m_lighting_complete = readU16(is);
505         m_generated = (flags & 0x08) == 0;
506
507         NameIdMapping nimap;
508         if (disk && version >= 29) {
509                 // Timestamp
510                 TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
511                                 <<": Timestamp"<<std::endl);
512                 setTimestampNoChangedFlag(readU32(is));
513                 m_disk_timestamp = m_timestamp;
514
515                 // Node/id mapping
516                 TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
517                                 <<": NameIdMapping"<<std::endl);
518                 nimap.deSerialize(is);
519         }
520
521         TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
522                         <<": Bulk node data"<<std::endl);
523         u8 content_width = readU8(is);
524         u8 params_width = readU8(is);
525         if(content_width != 1 && content_width != 2)
526                 throw SerializationError("MapBlock::deSerialize(): invalid content_width");
527         if(params_width != 2)
528                 throw SerializationError("MapBlock::deSerialize(): invalid params_width");
529
530         /*
531                 Bulk node data
532         */
533         if (version >= 29) {
534                 MapNode::deSerializeBulk(is, version, data, nodecount,
535                         content_width, params_width);
536         } else {
537                 // use in_raw from above to avoid allocating another stream object
538                 decompress(is, in_raw, version);
539                 MapNode::deSerializeBulk(in_raw, version, data, nodecount,
540                         content_width, params_width);
541         }
542
543         /*
544                 NodeMetadata
545         */
546         TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
547                         <<": Node metadata"<<std::endl);
548         if (version >= 29) {
549                 m_node_metadata.deSerialize(is, m_gamedef->idef());
550         } else {
551                 try {
552                         // reuse in_raw
553                         in_raw.str("");
554                         in_raw.clear();
555                         decompress(is, in_raw, version);
556                         if (version >= 23)
557                                 m_node_metadata.deSerialize(in_raw, m_gamedef->idef());
558                         else
559                                 content_nodemeta_deserialize_legacy(in_raw,
560                                         &m_node_metadata, &m_node_timers,
561                                         m_gamedef->idef());
562                 } catch(SerializationError &e) {
563                         warningstream<<"MapBlock::deSerialize(): Ignoring an error"
564                                         <<" while deserializing node metadata at ("
565                                         <<PP(getPos())<<": "<<e.what()<<std::endl;
566                 }
567         }
568
569         /*
570                 Data that is only on disk
571         */
572         if(disk)
573         {
574                 // Node timers
575                 if(version == 23){
576                         // Read unused zero
577                         readU8(is);
578                 }
579                 if(version == 24){
580                         TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
581                                         <<": Node timers (ver==24)"<<std::endl);
582                         m_node_timers.deSerialize(is, version);
583                 }
584
585                 // Static objects
586                 TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
587                                 <<": Static objects"<<std::endl);
588                 m_static_objects.deSerialize(is);
589
590                 if(version < 29) {
591                         // Timestamp
592                         TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
593                                     <<": Timestamp"<<std::endl);
594                         setTimestampNoChangedFlag(readU32(is));
595                         m_disk_timestamp = m_timestamp;
596
597                         // Node/id mapping
598                         TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
599                                     <<": NameIdMapping"<<std::endl);
600                         nimap.deSerialize(is);
601                 }
602
603                 // Dynamically re-set ids based on node names
604                 correctBlockNodeIds(&nimap, data, m_gamedef);
605
606                 if(version >= 25){
607                         TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
608                                         <<": Node timers (ver>=25)"<<std::endl);
609                         m_node_timers.deSerialize(is, version);
610                 }
611         }
612
613         TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
614                         <<": Done."<<std::endl);
615 }
616
617 void MapBlock::deSerializeNetworkSpecific(std::istream &is)
618 {
619         try {
620                 readU8(is);
621                 //const u8 version = readU8(is);
622                 //if (version != 1)
623                         //throw SerializationError("unsupported MapBlock version");
624
625         } catch(SerializationError &e) {
626                 warningstream<<"MapBlock::deSerializeNetworkSpecific(): Ignoring an error"
627                                 <<": "<<e.what()<<std::endl;
628         }
629 }
630
631 /*
632         Legacy serialization
633 */
634
635 void MapBlock::deSerialize_pre22(std::istream &is, u8 version, bool disk)
636 {
637         // Initialize default flags
638         is_underground = false;
639         m_day_night_differs = false;
640         m_lighting_complete = 0xFFFF;
641         m_generated = true;
642
643         // Make a temporary buffer
644         u32 ser_length = MapNode::serializedLength(version);
645         SharedBuffer<u8> databuf_nodelist(nodecount * ser_length);
646
647         // These have no compression
648         if (version <= 3 || version == 5 || version == 6) {
649                 char tmp;
650                 is.read(&tmp, 1);
651                 if (is.gcount() != 1)
652                         throw SerializationError(std::string(FUNCTION_NAME)
653                                 + ": not enough input data");
654                 is_underground = tmp;
655                 is.read((char *)*databuf_nodelist, nodecount * ser_length);
656                 if ((u32)is.gcount() != nodecount * ser_length)
657                         throw SerializationError(std::string(FUNCTION_NAME)
658                                 + ": not enough input data");
659         } else if (version <= 10) {
660                 u8 t8;
661                 is.read((char *)&t8, 1);
662                 is_underground = t8;
663
664                 {
665                         // Uncompress and set material data
666                         std::ostringstream os(std::ios_base::binary);
667                         decompress(is, os, version);
668                         std::string s = os.str();
669                         if (s.size() != nodecount)
670                                 throw SerializationError(std::string(FUNCTION_NAME)
671                                         + ": not enough input data");
672                         for (u32 i = 0; i < s.size(); i++) {
673                                 databuf_nodelist[i*ser_length] = s[i];
674                         }
675                 }
676                 {
677                         // Uncompress and set param data
678                         std::ostringstream os(std::ios_base::binary);
679                         decompress(is, os, version);
680                         std::string s = os.str();
681                         if (s.size() != nodecount)
682                                 throw SerializationError(std::string(FUNCTION_NAME)
683                                         + ": not enough input data");
684                         for (u32 i = 0; i < s.size(); i++) {
685                                 databuf_nodelist[i*ser_length + 1] = s[i];
686                         }
687                 }
688
689                 if (version >= 10) {
690                         // Uncompress and set param2 data
691                         std::ostringstream os(std::ios_base::binary);
692                         decompress(is, os, version);
693                         std::string s = os.str();
694                         if (s.size() != nodecount)
695                                 throw SerializationError(std::string(FUNCTION_NAME)
696                                         + ": not enough input data");
697                         for (u32 i = 0; i < s.size(); i++) {
698                                 databuf_nodelist[i*ser_length + 2] = s[i];
699                         }
700                 }
701         } else { // All other versions (10 to 21)
702                 u8 flags;
703                 is.read((char*)&flags, 1);
704                 is_underground = (flags & 0x01) != 0;
705                 m_day_night_differs = (flags & 0x02) != 0;
706                 if(version >= 18)
707                         m_generated = (flags & 0x08) == 0;
708
709                 // Uncompress data
710                 std::ostringstream os(std::ios_base::binary);
711                 decompress(is, os, version);
712                 std::string s = os.str();
713                 if (s.size() != nodecount * 3)
714                         throw SerializationError(std::string(FUNCTION_NAME)
715                                 + ": decompress resulted in size other than nodecount*3");
716
717                 // deserialize nodes from buffer
718                 for (u32 i = 0; i < nodecount; i++) {
719                         databuf_nodelist[i*ser_length] = s[i];
720                         databuf_nodelist[i*ser_length + 1] = s[i+nodecount];
721                         databuf_nodelist[i*ser_length + 2] = s[i+nodecount*2];
722                 }
723
724                 /*
725                         NodeMetadata
726                 */
727                 if (version >= 14) {
728                         // Ignore errors
729                         try {
730                                 if (version <= 15) {
731                                         std::string data = deSerializeString16(is);
732                                         std::istringstream iss(data, std::ios_base::binary);
733                                         content_nodemeta_deserialize_legacy(iss,
734                                                 &m_node_metadata, &m_node_timers,
735                                                 m_gamedef->idef());
736                                 } else {
737                                         //std::string data = deSerializeString32(is);
738                                         std::ostringstream oss(std::ios_base::binary);
739                                         decompressZlib(is, oss);
740                                         std::istringstream iss(oss.str(), std::ios_base::binary);
741                                         content_nodemeta_deserialize_legacy(iss,
742                                                 &m_node_metadata, &m_node_timers,
743                                                 m_gamedef->idef());
744                                 }
745                         } catch(SerializationError &e) {
746                                 warningstream<<"MapBlock::deSerialize(): Ignoring an error"
747                                                 <<" while deserializing node metadata"<<std::endl;
748                         }
749                 }
750         }
751
752         // Deserialize node data
753         for (u32 i = 0; i < nodecount; i++) {
754                 data[i].deSerialize(&databuf_nodelist[i * ser_length], version);
755         }
756
757         if (disk) {
758                 /*
759                         Versions up from 9 have block objects. (DEPRECATED)
760                 */
761                 if (version >= 9) {
762                         u16 count = readU16(is);
763                         // Not supported and length not known if count is not 0
764                         if(count != 0){
765                                 warningstream<<"MapBlock::deSerialize_pre22(): "
766                                                 <<"Ignoring stuff coming at and after MBOs"<<std::endl;
767                                 return;
768                         }
769                 }
770
771                 /*
772                         Versions up from 15 have static objects.
773                 */
774                 if (version >= 15)
775                         m_static_objects.deSerialize(is);
776
777                 // Timestamp
778                 if (version >= 17) {
779                         setTimestampNoChangedFlag(readU32(is));
780                         m_disk_timestamp = m_timestamp;
781                 } else {
782                         setTimestampNoChangedFlag(BLOCK_TIMESTAMP_UNDEFINED);
783                 }
784
785                 // Dynamically re-set ids based on node names
786                 NameIdMapping nimap;
787                 // If supported, read node definition id mapping
788                 if (version >= 21) {
789                         nimap.deSerialize(is);
790                 // Else set the legacy mapping
791                 } else {
792                         content_mapnode_get_name_id_mapping(&nimap);
793                 }
794                 correctBlockNodeIds(&nimap, data, m_gamedef);
795         }
796
797
798         // Legacy data changes
799         // This code has to convert from pre-22 to post-22 format.
800         const NodeDefManager *nodedef = m_gamedef->ndef();
801         for(u32 i=0; i<nodecount; i++)
802         {
803                 const ContentFeatures &f = nodedef->get(data[i].getContent());
804                 // Mineral
805                 if(nodedef->getId("default:stone") == data[i].getContent()
806                                 && data[i].getParam1() == 1)
807                 {
808                         data[i].setContent(nodedef->getId("default:stone_with_coal"));
809                         data[i].setParam1(0);
810                 }
811                 else if(nodedef->getId("default:stone") == data[i].getContent()
812                                 && data[i].getParam1() == 2)
813                 {
814                         data[i].setContent(nodedef->getId("default:stone_with_iron"));
815                         data[i].setParam1(0);
816                 }
817                 // facedir_simple
818                 if(f.legacy_facedir_simple)
819                 {
820                         data[i].setParam2(data[i].getParam1());
821                         data[i].setParam1(0);
822                 }
823                 // wall_mounted
824                 if(f.legacy_wallmounted)
825                 {
826                         u8 wallmounted_new_to_old[8] = {0x04, 0x08, 0x01, 0x02, 0x10, 0x20, 0, 0};
827                         u8 dir_old_format = data[i].getParam2();
828                         u8 dir_new_format = 0;
829                         for(u8 j=0; j<8; j++)
830                         {
831                                 if((dir_old_format & wallmounted_new_to_old[j]) != 0)
832                                 {
833                                         dir_new_format = j;
834                                         break;
835                                 }
836                         }
837                         data[i].setParam2(dir_new_format);
838                 }
839         }
840
841 }
842
843 /*
844         Get a quick string to describe what a block actually contains
845 */
846 std::string analyze_block(MapBlock *block)
847 {
848         if(block == NULL)
849                 return "NULL";
850
851         std::ostringstream desc;
852
853         v3s16 p = block->getPos();
854         char spos[25];
855         porting::mt_snprintf(spos, sizeof(spos), "(%2d,%2d,%2d), ", p.X, p.Y, p.Z);
856         desc<<spos;
857
858         switch(block->getModified())
859         {
860         case MOD_STATE_CLEAN:
861                 desc<<"CLEAN,           ";
862                 break;
863         case MOD_STATE_WRITE_AT_UNLOAD:
864                 desc<<"WRITE_AT_UNLOAD, ";
865                 break;
866         case MOD_STATE_WRITE_NEEDED:
867                 desc<<"WRITE_NEEDED,    ";
868                 break;
869         default:
870                 desc<<"unknown getModified()="+itos(block->getModified())+", ";
871         }
872
873         if(block->isGenerated())
874                 desc<<"is_gen [X], ";
875         else
876                 desc<<"is_gen [ ], ";
877
878         if(block->getIsUnderground())
879                 desc<<"is_ug [X], ";
880         else
881                 desc<<"is_ug [ ], ";
882
883         desc<<"lighting_complete: "<<block->getLightingComplete()<<", ";
884
885         if(block->isDummy())
886         {
887                 desc<<"Dummy, ";
888         }
889         else
890         {
891                 bool full_ignore = true;
892                 bool some_ignore = false;
893                 bool full_air = true;
894                 bool some_air = false;
895                 for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
896                 for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
897                 for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
898                 {
899                         v3s16 p(x0,y0,z0);
900                         MapNode n = block->getNodeNoEx(p);
901                         content_t c = n.getContent();
902                         if(c == CONTENT_IGNORE)
903                                 some_ignore = true;
904                         else
905                                 full_ignore = false;
906                         if(c == CONTENT_AIR)
907                                 some_air = true;
908                         else
909                                 full_air = false;
910                 }
911
912                 desc<<"content {";
913
914                 std::ostringstream ss;
915
916                 if(full_ignore)
917                         ss<<"IGNORE (full), ";
918                 else if(some_ignore)
919                         ss<<"IGNORE, ";
920
921                 if(full_air)
922                         ss<<"AIR (full), ";
923                 else if(some_air)
924                         ss<<"AIR, ";
925
926                 if(ss.str().size()>=2)
927                         desc<<ss.str().substr(0, ss.str().size()-2);
928
929                 desc<<"}, ";
930         }
931
932         return desc.str().substr(0, desc.str().size()-2);
933 }
934
935
936 //END