Fix WorldStorage accessing null entityMap if a broken world throws an exception on load

happened to me when trying to repair a broken world file
This commit is contained in:
Kae 2024-08-08 11:59:38 +10:00
parent dc37c9bdb8
commit b2afd65144

View File

@ -371,15 +371,17 @@ void WorldStorage::unloadAll(bool force) {
// or being entirely outside of the world geometry. This limits the number // or being entirely outside of the world geometry. This limits the number
// of tries to completely uninit and store all entities before giving up // of tries to completely uninit and store all entities before giving up
// and just letting some entities not be stored. // and just letting some entities not be stored.
if (m_entityMap) {
unsigned forceUnloadTries = storageConfig.getUInt("forceUnloadTries"); unsigned forceUnloadTries = storageConfig.getUInt("forceUnloadTries");
for (unsigned i = 0; i < forceUnloadTries; ++i) { for (unsigned i = 0; i < forceUnloadTries; ++i) {
for (auto sector : sectors) for (auto& sector : sectors)
unloadSectorToLevel(sector, SectorLoadLevel::Tiles, force); unloadSectorToLevel(sector, SectorLoadLevel::Tiles, force);
if (!force || m_entityMap->size() == 0) if (!force || m_entityMap->size() == 0)
break; break;
} }
for (auto sector : sectors) }
for (auto& sector : sectors)
unloadSectorToLevel(sector, SectorLoadLevel::None, force); unloadSectorToLevel(sector, SectorLoadLevel::None, force);
} catch (std::exception const& e) { } catch (std::exception const& e) {
@ -721,13 +723,12 @@ bool WorldStorage::unloadSectorToLevel(Sector const& sector, SectorLoadLevel tar
if (!m_tileArray->sectorValid(sector) || targetLoadLevel == SectorLoadLevel::Loaded) if (!m_tileArray->sectorValid(sector) || targetLoadLevel == SectorLoadLevel::Loaded)
return true; return true;
auto entityFactory = Root::singleton().entityFactory();
auto& metadata = m_sectorMetadata[sector]; auto& metadata = m_sectorMetadata[sector];
bool entitiesOverlap = false;
if (m_entityMap) {
auto entityFactory = Root::singleton().entityFactory();
List<EntityPtr> entitiesToStore; List<EntityPtr> entitiesToStore;
List<EntityPtr> entitiesToRemove; List<EntityPtr> entitiesToRemove;
bool entitiesOverlap = false;
for (auto& entity : m_entityMap->entityQuery(RectF(m_tileArray->sectorRegion(sector)))) { for (auto& entity : m_entityMap->entityQuery(RectF(m_tileArray->sectorRegion(sector)))) {
// Only store / remove entities who belong to this sector. If an entity // Only store / remove entities who belong to this sector. If an entity
@ -788,6 +789,7 @@ bool WorldStorage::unloadSectorToLevel(Sector const& sector, SectorLoadLevel tar
m_generatorFacade->sectorLoadLevelChanged(this, sector, SectorLoadLevel::Tiles); m_generatorFacade->sectorLoadLevelChanged(this, sector, SectorLoadLevel::Tiles);
} }
} }
}
if (targetLoadLevel == SectorLoadLevel::None) { if (targetLoadLevel == SectorLoadLevel::None) {
if (metadata.loadLevel > SectorLoadLevel::None && !entitiesOverlap) { if (metadata.loadLevel > SectorLoadLevel::None && !entitiesOverlap) {