mapblock.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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. s16 MapBlock::getGroundLevel(v2s16 p2d)
  183. {
  184. if(isDummy())
  185. return -3;
  186. try
  187. {
  188. s16 y = MAP_BLOCKSIZE-1;
  189. for(; y>=0; y--)
  190. {
  191. MapNode n = getNodeRef(p2d.X, y, p2d.Y);
  192. if (m_gamedef->ndef()->get(n).walkable) {
  193. if(y == MAP_BLOCKSIZE-1)
  194. return -2;
  195. return y;
  196. }
  197. }
  198. return -1;
  199. }
  200. catch(InvalidPositionException &e)
  201. {
  202. return -3;
  203. }
  204. }
  205. /*
  206. Serialization
  207. */
  208. // List relevant id-name pairs for ids in the block using nodedef
  209. // Renumbers the content IDs (starting at 0 and incrementing
  210. // use static memory requires about 65535 * sizeof(int) ram in order to be
  211. // sure we can handle all content ids. But it's absolutely worth it as it's
  212. // a speedup of 4 for one of the major time consuming functions on storing
  213. // mapblocks.
  214. static content_t getBlockNodeIdMapping_mapping[USHRT_MAX + 1];
  215. static void getBlockNodeIdMapping(NameIdMapping *nimap, MapNode *nodes,
  216. const NodeDefManager *nodedef)
  217. {
  218. memset(getBlockNodeIdMapping_mapping, 0xFF, (USHRT_MAX + 1) * sizeof(content_t));
  219. std::set<content_t> unknown_contents;
  220. content_t id_counter = 0;
  221. for (u32 i = 0; i < MapBlock::nodecount; i++) {
  222. content_t global_id = nodes[i].getContent();
  223. content_t id = CONTENT_IGNORE;
  224. // Try to find an existing mapping
  225. if (getBlockNodeIdMapping_mapping[global_id] != 0xFFFF) {
  226. id = getBlockNodeIdMapping_mapping[global_id];
  227. }
  228. else
  229. {
  230. // We have to assign a new mapping
  231. id = id_counter++;
  232. getBlockNodeIdMapping_mapping[global_id] = id;
  233. const ContentFeatures &f = nodedef->get(global_id);
  234. const std::string &name = f.name;
  235. if (name.empty())
  236. unknown_contents.insert(global_id);
  237. else
  238. nimap->set(id, name);
  239. }
  240. // Update the MapNode
  241. nodes[i].setContent(id);
  242. }
  243. for (u16 unknown_content : unknown_contents) {
  244. errorstream << "getBlockNodeIdMapping(): IGNORING ERROR: "
  245. << "Name for node id " << unknown_content << " not known" << std::endl;
  246. }
  247. }
  248. // Correct ids in the block to match nodedef based on names.
  249. // Unknown ones are added to nodedef.
  250. // Will not update itself to match id-name pairs in nodedef.
  251. static void correctBlockNodeIds(const NameIdMapping *nimap, MapNode *nodes,
  252. IGameDef *gamedef)
  253. {
  254. const NodeDefManager *nodedef = gamedef->ndef();
  255. // This means the block contains incorrect ids, and we contain
  256. // the information to convert those to names.
  257. // nodedef contains information to convert our names to globally
  258. // correct ids.
  259. std::unordered_set<content_t> unnamed_contents;
  260. std::unordered_set<std::string> unallocatable_contents;
  261. bool previous_exists = false;
  262. content_t previous_local_id = CONTENT_IGNORE;
  263. content_t previous_global_id = CONTENT_IGNORE;
  264. for (u32 i = 0; i < MapBlock::nodecount; i++) {
  265. content_t local_id = nodes[i].getContent();
  266. // If previous node local_id was found and same than before, don't lookup maps
  267. // apply directly previous resolved id
  268. // This permits to massively improve loading performance when nodes are similar
  269. // example: default:air, default:stone are massively present
  270. if (previous_exists && local_id == previous_local_id) {
  271. nodes[i].setContent(previous_global_id);
  272. continue;
  273. }
  274. std::string name;
  275. if (!nimap->getName(local_id, name)) {
  276. unnamed_contents.insert(local_id);
  277. previous_exists = false;
  278. continue;
  279. }
  280. content_t global_id;
  281. if (!nodedef->getId(name, global_id)) {
  282. global_id = gamedef->allocateUnknownNodeId(name);
  283. if (global_id == CONTENT_IGNORE) {
  284. unallocatable_contents.insert(name);
  285. previous_exists = false;
  286. continue;
  287. }
  288. }
  289. nodes[i].setContent(global_id);
  290. // Save previous node local_id & global_id result
  291. previous_local_id = local_id;
  292. previous_global_id = global_id;
  293. previous_exists = true;
  294. }
  295. for (const content_t c: unnamed_contents) {
  296. errorstream << "correctBlockNodeIds(): IGNORING ERROR: "
  297. << "Block contains id " << c
  298. << " with no name mapping" << std::endl;
  299. }
  300. for (const std::string &node_name: unallocatable_contents) {
  301. errorstream << "correctBlockNodeIds(): IGNORING ERROR: "
  302. << "Could not allocate global id for node name \""
  303. << node_name << "\"" << std::endl;
  304. }
  305. }
  306. void MapBlock::serialize(std::ostream &os_compressed, u8 version, bool disk, int compression_level)
  307. {
  308. if(!ser_ver_supported(version))
  309. throw VersionMismatchException("ERROR: MapBlock format not supported");
  310. if (!data)
  311. throw SerializationError("ERROR: Not writing dummy block.");
  312. FATAL_ERROR_IF(version < SER_FMT_VER_LOWEST_WRITE, "Serialisation version error");
  313. std::ostringstream os_raw(std::ios_base::binary);
  314. std::ostream &os = version >= 29 ? os_raw : os_compressed;
  315. // First byte
  316. u8 flags = 0;
  317. if(is_underground)
  318. flags |= 0x01;
  319. if(getDayNightDiff())
  320. flags |= 0x02;
  321. if (!m_generated)
  322. flags |= 0x08;
  323. writeU8(os, flags);
  324. if (version >= 27) {
  325. writeU16(os, m_lighting_complete);
  326. }
  327. /*
  328. Bulk node data
  329. */
  330. NameIdMapping nimap;
  331. SharedBuffer<u8> buf;
  332. const u8 content_width = 2;
  333. const u8 params_width = 2;
  334. if(disk)
  335. {
  336. MapNode *tmp_nodes = new MapNode[nodecount];
  337. memcpy(tmp_nodes, data, nodecount * sizeof(MapNode));
  338. getBlockNodeIdMapping(&nimap, tmp_nodes, m_gamedef->ndef());
  339. buf = MapNode::serializeBulk(version, tmp_nodes, nodecount,
  340. content_width, params_width);
  341. delete[] tmp_nodes;
  342. // write timestamp and node/id mapping first
  343. if (version >= 29) {
  344. writeU32(os, getTimestamp());
  345. nimap.serialize(os);
  346. }
  347. }
  348. else
  349. {
  350. buf = MapNode::serializeBulk(version, data, nodecount,
  351. content_width, params_width);
  352. }
  353. writeU8(os, content_width);
  354. writeU8(os, params_width);
  355. if (version >= 29) {
  356. os.write(reinterpret_cast<char*>(*buf), buf.getSize());
  357. } else {
  358. // prior to 29 node data was compressed individually
  359. compress(buf, os, version, compression_level);
  360. }
  361. /*
  362. Node metadata
  363. */
  364. if (version >= 29) {
  365. m_node_metadata.serialize(os, version, disk);
  366. } else {
  367. // use os_raw from above to avoid allocating another stream object
  368. m_node_metadata.serialize(os_raw, version, disk);
  369. // prior to 29 node data was compressed individually
  370. compress(os_raw.str(), os, version, compression_level);
  371. }
  372. /*
  373. Data that goes to disk, but not the network
  374. */
  375. if(disk)
  376. {
  377. if(version <= 24){
  378. // Node timers
  379. m_node_timers.serialize(os, version);
  380. }
  381. // Static objects
  382. m_static_objects.serialize(os);
  383. if(version < 29){
  384. // Timestamp
  385. writeU32(os, getTimestamp());
  386. // Write block-specific node definition id mapping
  387. nimap.serialize(os);
  388. }
  389. if(version >= 25){
  390. // Node timers
  391. m_node_timers.serialize(os, version);
  392. }
  393. }
  394. if (version >= 29) {
  395. // now compress the whole thing
  396. compress(os_raw.str(), os_compressed, version, compression_level);
  397. }
  398. }
  399. void MapBlock::serializeNetworkSpecific(std::ostream &os)
  400. {
  401. if (!data) {
  402. throw SerializationError("ERROR: Not writing dummy block.");
  403. }
  404. writeU8(os, 2); // version
  405. }
  406. void MapBlock::deSerialize(std::istream &in_compressed, u8 version, bool disk)
  407. {
  408. if(!ser_ver_supported(version))
  409. throw VersionMismatchException("ERROR: MapBlock format not supported");
  410. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())<<std::endl);
  411. m_day_night_differs_expired = false;
  412. if(version <= 21)
  413. {
  414. deSerialize_pre22(in_compressed, version, disk);
  415. return;
  416. }
  417. // Decompress the whole block (version >= 29)
  418. std::stringstream in_raw(std::ios_base::binary | std::ios_base::in | std::ios_base::out);
  419. if (version >= 29)
  420. decompress(in_compressed, in_raw, version);
  421. std::istream &is = version >= 29 ? in_raw : in_compressed;
  422. u8 flags = readU8(is);
  423. is_underground = (flags & 0x01) != 0;
  424. m_day_night_differs = (flags & 0x02) != 0;
  425. if (version < 27)
  426. m_lighting_complete = 0xFFFF;
  427. else
  428. m_lighting_complete = readU16(is);
  429. m_generated = (flags & 0x08) == 0;
  430. NameIdMapping nimap;
  431. if (disk && version >= 29) {
  432. // Timestamp
  433. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  434. <<": Timestamp"<<std::endl);
  435. setTimestampNoChangedFlag(readU32(is));
  436. m_disk_timestamp = m_timestamp;
  437. // Node/id mapping
  438. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  439. <<": NameIdMapping"<<std::endl);
  440. nimap.deSerialize(is);
  441. }
  442. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  443. <<": Bulk node data"<<std::endl);
  444. u8 content_width = readU8(is);
  445. u8 params_width = readU8(is);
  446. if(content_width != 1 && content_width != 2)
  447. throw SerializationError("MapBlock::deSerialize(): invalid content_width");
  448. if(params_width != 2)
  449. throw SerializationError("MapBlock::deSerialize(): invalid params_width");
  450. /*
  451. Bulk node data
  452. */
  453. if (version >= 29) {
  454. MapNode::deSerializeBulk(is, version, data, nodecount,
  455. content_width, params_width);
  456. } else {
  457. // use in_raw from above to avoid allocating another stream object
  458. decompress(is, in_raw, version);
  459. MapNode::deSerializeBulk(in_raw, version, data, nodecount,
  460. content_width, params_width);
  461. }
  462. /*
  463. NodeMetadata
  464. */
  465. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  466. <<": Node metadata"<<std::endl);
  467. if (version >= 29) {
  468. m_node_metadata.deSerialize(is, m_gamedef->idef());
  469. } else {
  470. try {
  471. // reuse in_raw
  472. in_raw.str("");
  473. in_raw.clear();
  474. decompress(is, in_raw, version);
  475. if (version >= 23)
  476. m_node_metadata.deSerialize(in_raw, m_gamedef->idef());
  477. else
  478. content_nodemeta_deserialize_legacy(in_raw,
  479. &m_node_metadata, &m_node_timers,
  480. m_gamedef->idef());
  481. } catch(SerializationError &e) {
  482. warningstream<<"MapBlock::deSerialize(): Ignoring an error"
  483. <<" while deserializing node metadata at ("
  484. <<PP(getPos())<<": "<<e.what()<<std::endl;
  485. }
  486. }
  487. /*
  488. Data that is only on disk
  489. */
  490. if(disk)
  491. {
  492. // Node timers
  493. if(version == 23){
  494. // Read unused zero
  495. readU8(is);
  496. }
  497. if(version == 24){
  498. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  499. <<": Node timers (ver==24)"<<std::endl);
  500. m_node_timers.deSerialize(is, version);
  501. }
  502. // Static objects
  503. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  504. <<": Static objects"<<std::endl);
  505. m_static_objects.deSerialize(is);
  506. if(version < 29) {
  507. // Timestamp
  508. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  509. <<": Timestamp"<<std::endl);
  510. setTimestampNoChangedFlag(readU32(is));
  511. m_disk_timestamp = m_timestamp;
  512. // Node/id mapping
  513. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  514. <<": NameIdMapping"<<std::endl);
  515. nimap.deSerialize(is);
  516. }
  517. // Dynamically re-set ids based on node names
  518. correctBlockNodeIds(&nimap, data, m_gamedef);
  519. if(version >= 25){
  520. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  521. <<": Node timers (ver>=25)"<<std::endl);
  522. m_node_timers.deSerialize(is, version);
  523. }
  524. }
  525. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  526. <<": Done."<<std::endl);
  527. }
  528. void MapBlock::deSerializeNetworkSpecific(std::istream &is)
  529. {
  530. try {
  531. readU8(is);
  532. //const u8 version = readU8(is);
  533. //if (version != 1)
  534. //throw SerializationError("unsupported MapBlock version");
  535. } catch(SerializationError &e) {
  536. warningstream<<"MapBlock::deSerializeNetworkSpecific(): Ignoring an error"
  537. <<": "<<e.what()<<std::endl;
  538. }
  539. }
  540. /*
  541. Legacy serialization
  542. */
  543. void MapBlock::deSerialize_pre22(std::istream &is, u8 version, bool disk)
  544. {
  545. // Initialize default flags
  546. is_underground = false;
  547. m_day_night_differs = false;
  548. m_lighting_complete = 0xFFFF;
  549. m_generated = true;
  550. // Make a temporary buffer
  551. u32 ser_length = MapNode::serializedLength(version);
  552. SharedBuffer<u8> databuf_nodelist(nodecount * ser_length);
  553. // These have no compression
  554. if (version <= 3 || version == 5 || version == 6) {
  555. char tmp;
  556. is.read(&tmp, 1);
  557. if (is.gcount() != 1)
  558. throw SerializationError(std::string(FUNCTION_NAME)
  559. + ": not enough input data");
  560. is_underground = tmp;
  561. is.read((char *)*databuf_nodelist, nodecount * ser_length);
  562. if ((u32)is.gcount() != nodecount * ser_length)
  563. throw SerializationError(std::string(FUNCTION_NAME)
  564. + ": not enough input data");
  565. } else if (version <= 10) {
  566. u8 t8;
  567. is.read((char *)&t8, 1);
  568. is_underground = t8;
  569. {
  570. // Uncompress and set material data
  571. std::ostringstream os(std::ios_base::binary);
  572. decompress(is, os, version);
  573. std::string s = os.str();
  574. if (s.size() != nodecount)
  575. throw SerializationError(std::string(FUNCTION_NAME)
  576. + ": not enough input data");
  577. for (u32 i = 0; i < s.size(); i++) {
  578. databuf_nodelist[i*ser_length] = s[i];
  579. }
  580. }
  581. {
  582. // Uncompress and set param data
  583. std::ostringstream os(std::ios_base::binary);
  584. decompress(is, os, version);
  585. std::string s = os.str();
  586. if (s.size() != nodecount)
  587. throw SerializationError(std::string(FUNCTION_NAME)
  588. + ": not enough input data");
  589. for (u32 i = 0; i < s.size(); i++) {
  590. databuf_nodelist[i*ser_length + 1] = s[i];
  591. }
  592. }
  593. if (version >= 10) {
  594. // Uncompress and set param2 data
  595. std::ostringstream os(std::ios_base::binary);
  596. decompress(is, os, version);
  597. std::string s = os.str();
  598. if (s.size() != nodecount)
  599. throw SerializationError(std::string(FUNCTION_NAME)
  600. + ": not enough input data");
  601. for (u32 i = 0; i < s.size(); i++) {
  602. databuf_nodelist[i*ser_length + 2] = s[i];
  603. }
  604. }
  605. } else { // All other versions (10 to 21)
  606. u8 flags;
  607. is.read((char*)&flags, 1);
  608. is_underground = (flags & 0x01) != 0;
  609. m_day_night_differs = (flags & 0x02) != 0;
  610. if(version >= 18)
  611. m_generated = (flags & 0x08) == 0;
  612. // Uncompress data
  613. std::ostringstream os(std::ios_base::binary);
  614. decompress(is, os, version);
  615. std::string s = os.str();
  616. if (s.size() != nodecount * 3)
  617. throw SerializationError(std::string(FUNCTION_NAME)
  618. + ": decompress resulted in size other than nodecount*3");
  619. // deserialize nodes from buffer
  620. for (u32 i = 0; i < nodecount; i++) {
  621. databuf_nodelist[i*ser_length] = s[i];
  622. databuf_nodelist[i*ser_length + 1] = s[i+nodecount];
  623. databuf_nodelist[i*ser_length + 2] = s[i+nodecount*2];
  624. }
  625. /*
  626. NodeMetadata
  627. */
  628. if (version >= 14) {
  629. // Ignore errors
  630. try {
  631. if (version <= 15) {
  632. std::string data = deSerializeString16(is);
  633. std::istringstream iss(data, std::ios_base::binary);
  634. content_nodemeta_deserialize_legacy(iss,
  635. &m_node_metadata, &m_node_timers,
  636. m_gamedef->idef());
  637. } else {
  638. //std::string data = deSerializeString32(is);
  639. std::ostringstream oss(std::ios_base::binary);
  640. decompressZlib(is, oss);
  641. std::istringstream iss(oss.str(), std::ios_base::binary);
  642. content_nodemeta_deserialize_legacy(iss,
  643. &m_node_metadata, &m_node_timers,
  644. m_gamedef->idef());
  645. }
  646. } catch(SerializationError &e) {
  647. warningstream<<"MapBlock::deSerialize(): Ignoring an error"
  648. <<" while deserializing node metadata"<<std::endl;
  649. }
  650. }
  651. }
  652. // Deserialize node data
  653. for (u32 i = 0; i < nodecount; i++) {
  654. data[i].deSerialize(&databuf_nodelist[i * ser_length], version);
  655. }
  656. if (disk) {
  657. /*
  658. Versions up from 9 have block objects. (DEPRECATED)
  659. */
  660. if (version >= 9) {
  661. u16 count = readU16(is);
  662. // Not supported and length not known if count is not 0
  663. if(count != 0){
  664. warningstream<<"MapBlock::deSerialize_pre22(): "
  665. <<"Ignoring stuff coming at and after MBOs"<<std::endl;
  666. return;
  667. }
  668. }
  669. /*
  670. Versions up from 15 have static objects.
  671. */
  672. if (version >= 15)
  673. m_static_objects.deSerialize(is);
  674. // Timestamp
  675. if (version >= 17) {
  676. setTimestampNoChangedFlag(readU32(is));
  677. m_disk_timestamp = m_timestamp;
  678. } else {
  679. setTimestampNoChangedFlag(BLOCK_TIMESTAMP_UNDEFINED);
  680. }
  681. // Dynamically re-set ids based on node names
  682. NameIdMapping nimap;
  683. // If supported, read node definition id mapping
  684. if (version >= 21) {
  685. nimap.deSerialize(is);
  686. // Else set the legacy mapping
  687. } else {
  688. content_mapnode_get_name_id_mapping(&nimap);
  689. }
  690. correctBlockNodeIds(&nimap, data, m_gamedef);
  691. }
  692. // Legacy data changes
  693. // This code has to convert from pre-22 to post-22 format.
  694. const NodeDefManager *nodedef = m_gamedef->ndef();
  695. for(u32 i=0; i<nodecount; i++)
  696. {
  697. const ContentFeatures &f = nodedef->get(data[i].getContent());
  698. // Mineral
  699. if(nodedef->getId("default:stone") == data[i].getContent()
  700. && data[i].getParam1() == 1)
  701. {
  702. data[i].setContent(nodedef->getId("default:stone_with_coal"));
  703. data[i].setParam1(0);
  704. }
  705. else if(nodedef->getId("default:stone") == data[i].getContent()
  706. && data[i].getParam1() == 2)
  707. {
  708. data[i].setContent(nodedef->getId("default:stone_with_iron"));
  709. data[i].setParam1(0);
  710. }
  711. // facedir_simple
  712. if(f.legacy_facedir_simple)
  713. {
  714. data[i].setParam2(data[i].getParam1());
  715. data[i].setParam1(0);
  716. }
  717. // wall_mounted
  718. if(f.legacy_wallmounted)
  719. {
  720. u8 wallmounted_new_to_old[8] = {0x04, 0x08, 0x01, 0x02, 0x10, 0x20, 0, 0};
  721. u8 dir_old_format = data[i].getParam2();
  722. u8 dir_new_format = 0;
  723. for(u8 j=0; j<8; j++)
  724. {
  725. if((dir_old_format & wallmounted_new_to_old[j]) != 0)
  726. {
  727. dir_new_format = j;
  728. break;
  729. }
  730. }
  731. data[i].setParam2(dir_new_format);
  732. }
  733. }
  734. }
  735. /*
  736. Get a quick string to describe what a block actually contains
  737. */
  738. std::string analyze_block(MapBlock *block)
  739. {
  740. if(block == NULL)
  741. return "NULL";
  742. std::ostringstream desc;
  743. v3s16 p = block->getPos();
  744. char spos[25];
  745. porting::mt_snprintf(spos, sizeof(spos), "(%2d,%2d,%2d), ", p.X, p.Y, p.Z);
  746. desc<<spos;
  747. switch(block->getModified())
  748. {
  749. case MOD_STATE_CLEAN:
  750. desc<<"CLEAN, ";
  751. break;
  752. case MOD_STATE_WRITE_AT_UNLOAD:
  753. desc<<"WRITE_AT_UNLOAD, ";
  754. break;
  755. case MOD_STATE_WRITE_NEEDED:
  756. desc<<"WRITE_NEEDED, ";
  757. break;
  758. default:
  759. desc<<"unknown getModified()="+itos(block->getModified())+", ";
  760. }
  761. if(block->isGenerated())
  762. desc<<"is_gen [X], ";
  763. else
  764. desc<<"is_gen [ ], ";
  765. if(block->getIsUnderground())
  766. desc<<"is_ug [X], ";
  767. else
  768. desc<<"is_ug [ ], ";
  769. desc<<"lighting_complete: "<<block->getLightingComplete()<<", ";
  770. if(block->isDummy())
  771. {
  772. desc<<"Dummy, ";
  773. }
  774. else
  775. {
  776. bool full_ignore = true;
  777. bool some_ignore = false;
  778. bool full_air = true;
  779. bool some_air = false;
  780. for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
  781. for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
  782. for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
  783. {
  784. v3s16 p(x0,y0,z0);
  785. MapNode n = block->getNodeNoEx(p);
  786. content_t c = n.getContent();
  787. if(c == CONTENT_IGNORE)
  788. some_ignore = true;
  789. else
  790. full_ignore = false;
  791. if(c == CONTENT_AIR)
  792. some_air = true;
  793. else
  794. full_air = false;
  795. }
  796. desc<<"content {";
  797. std::ostringstream ss;
  798. if(full_ignore)
  799. ss<<"IGNORE (full), ";
  800. else if(some_ignore)
  801. ss<<"IGNORE, ";
  802. if(full_air)
  803. ss<<"AIR (full), ";
  804. else if(some_air)
  805. ss<<"AIR, ";
  806. if(ss.str().size()>=2)
  807. desc<<ss.str().substr(0, ss.str().size()-2);
  808. desc<<"}, ";
  809. }
  810. return desc.str().substr(0, desc.str().size()-2);
  811. }
  812. //END