mapblock.cpp 24 KB

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