mapblock.cpp 23 KB

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