Skip to content

Commit dba41eb

Browse files
authored
Detect doubled objects' UIDs in fh2m maps in debug build (#9866)
1 parent 89ed0ce commit dba41eb

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/fheroes2/maps/map_format_helper.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,15 +1213,66 @@ namespace Maps
12131213
auto sortObjects = []( const IndexedObjectInfo & left, const IndexedObjectInfo & right ) { return left.info->id < right.info->id; };
12141214
std::multiset<IndexedObjectInfo, decltype( sortObjects )> sortedObjects( sortObjects );
12151215

1216+
#if defined( WITH_DEBUG )
1217+
std::map<uint32_t, IndexedObjectInfo> objectsUIDs;
1218+
std::multiset<IndexedObjectInfo, decltype( sortObjects )> incorrectObjects( sortObjects );
1219+
#endif
1220+
12161221
for ( size_t i = 0; i < tilesConut; ++i ) {
12171222
for ( const auto & object : map.tiles[i].objects ) {
12181223
IndexedObjectInfo info;
12191224
info.tileIndex = static_cast<int32_t>( i );
12201225
info.info = &object;
12211226
sortedObjects.emplace( info );
1227+
1228+
#if defined( WITH_DEBUG )
1229+
if ( object.group != Maps::ObjectGroup::LANDSCAPE_TOWN_BASEMENTS && object.group != Maps::ObjectGroup::LANDSCAPE_FLAGS ) {
1230+
const auto [iter, inserted] = objectsUIDs.try_emplace( object.id, info );
1231+
if ( !inserted ) {
1232+
incorrectObjects.emplace( iter->second );
1233+
incorrectObjects.emplace( info );
1234+
}
1235+
}
1236+
#endif
12221237
}
12231238
}
12241239

1240+
#if defined( WITH_DEBUG )
1241+
uint32_t uid = 0;
1242+
for ( const IndexedObjectInfo & info : incorrectObjects ) {
1243+
if ( info.info->id != uid ) {
1244+
uid = info.info->id;
1245+
if ( map.standardMetadata.find( uid ) != map.standardMetadata.end() ) {
1246+
VERBOSE_LOG( "`standardMetadata` belongs to many objects with same UID: " << uid )
1247+
}
1248+
if ( map.castleMetadata.find( uid ) != map.castleMetadata.end() ) {
1249+
VERBOSE_LOG( "`castleMetadata` belongs to many objects with same UID: " << uid )
1250+
}
1251+
if ( map.heroMetadata.find( uid ) != map.heroMetadata.end() ) {
1252+
VERBOSE_LOG( "`heroMetadata` belongs to many objects with same UID: " << uid )
1253+
}
1254+
if ( map.sphinxMetadata.find( uid ) != map.sphinxMetadata.end() ) {
1255+
VERBOSE_LOG( "`sphinxMetadata` belongs to many objects with same UID: " << uid )
1256+
}
1257+
if ( map.signMetadata.find( uid ) != map.signMetadata.end() ) {
1258+
VERBOSE_LOG( "`signMetadata` belongs to many objects with same UID: " << uid )
1259+
}
1260+
if ( map.adventureMapEventMetadata.find( uid ) != map.adventureMapEventMetadata.end() ) {
1261+
VERBOSE_LOG( "`adventureMapEventMetadata` belongs to many objects with same UID: " << uid )
1262+
}
1263+
if ( map.selectionObjectMetadata.find( uid ) != map.selectionObjectMetadata.end() ) {
1264+
VERBOSE_LOG( "`selectionObjectMetadata` belongs to many objects with same UID: " << uid )
1265+
}
1266+
if ( map.capturableObjectsMetadata.find( uid ) != map.capturableObjectsMetadata.end() ) {
1267+
VERBOSE_LOG( "`capturableObjectsMetadata` belongs to many objects with same UID: " << uid )
1268+
}
1269+
}
1270+
1271+
VERBOSE_LOG( "Non-unique UID " << info.info->id << " at " << info.tileIndex << " (" << info.tileIndex % map.size << ", " << info.tileIndex / map.size
1272+
<< ") tile for object type: " << MP2::StringObject( getObjectInfo( info.info->group, info.info->index ).objectType ) )
1273+
}
1274+
#endif
1275+
12251276
for ( const auto & info : sortedObjects ) {
12261277
assert( info.info != nullptr );
12271278
if ( !readTileObject( world.getTile( info.tileIndex ), *info.info ) ) {

0 commit comments

Comments
 (0)