mapblock.cpp 24 KB

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