mapblock.cpp 24 KB

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