From 9f7501d20a0eda96bbf348a5e4470bc464baccba Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sun, 25 May 2025 19:16:58 +0200 Subject: [PATCH] Handle writing to unloaded blocks in MMVManip::blitBackAll() This could happen before and would just silently discard the data, but now that Lua can create VManips without loading from map beforehand we definitely need to handle this case. --- src/map.cpp | 37 ++++++++++++++++++++++--------------- src/voxel.cpp | 1 - src/voxel.h | 1 - 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/map.cpp b/src/map.cpp index 1c1dfd51c..964d8b5af 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -789,20 +789,10 @@ void MMVManip::initialEmerge(v3s16 p_min, v3s16 p_max, bool load_if_inexistent) if (auto it = had_blocks.find(p); it != had_blocks.end() && it->second) continue; - MapBlock *block; - bool block_data_inexistent = false; - { - TimeTaker timer2("emerge load", &emerge_load_time); - - block = m_map->getBlockNoCreateNoEx(p); - if (!block) - block_data_inexistent = true; - else - block->copyTo(*this); - } - - if(block_data_inexistent) - { + MapBlock *block = m_map->getBlockNoCreateNoEx(p); + if (block) { + block->copyTo(*this); + } else { if (load_if_inexistent && !blockpos_over_max_limit(p)) { block = m_map->emergeBlock(p, true); assert(block); @@ -862,6 +852,8 @@ void MMVManip::blitBackAll(std::map *modified_blocks, return; assert(m_map); + size_t nload = 0; + // Copy all the blocks with data back to the map const auto loaded_blocks = getCoveredBlocks(); for (auto &it : loaded_blocks) { @@ -869,7 +861,18 @@ void MMVManip::blitBackAll(std::map *modified_blocks, continue; v3s16 p = it.first; MapBlock *block = m_map->getBlockNoCreateNoEx(p); - if (!block || (!overwrite_generated && block->isGenerated())) + if (!block) { + if (!blockpos_over_max_limit(p)) { + block = m_map->emergeBlock(p, true); + nload++; + } + } + if (!block) { + warningstream << "blitBackAll: Couldn't load block " << p + << " to write data to map" << std::endl; + continue; + } + if (!overwrite_generated && block->isGenerated()) continue; block->copyFrom(*this); @@ -879,6 +882,10 @@ void MMVManip::blitBackAll(std::map *modified_blocks, if(modified_blocks) (*modified_blocks)[p] = block; } + + if (nload > 0) { + verbosestream << "blitBackAll: " << nload << " blocks had to be loaded for writing" << std::endl; + } } MMVManip *MMVManip::clone() const diff --git a/src/voxel.cpp b/src/voxel.cpp index 4d0ce84b7..5a69b0725 100644 --- a/src/voxel.cpp +++ b/src/voxel.cpp @@ -15,7 +15,6 @@ Debug stuff */ u64 emerge_time = 0; -u64 emerge_load_time = 0; VoxelManipulator::~VoxelManipulator() { diff --git a/src/voxel.h b/src/voxel.h index a1a3784e5..7fbaadc28 100644 --- a/src/voxel.h +++ b/src/voxel.h @@ -34,7 +34,6 @@ class NodeDefManager; Debug stuff */ extern u64 emerge_time; -extern u64 emerge_load_time; /* This class resembles aabbox3d a lot, but has inclusive