also fix dummy targetnames in -mergebsp -fixnames, may legitimately exist as outcome of misc_model->group_entity connection

This commit is contained in:
Garux 2021-11-04 19:45:57 +03:00
parent d1610b49ff
commit 0aa61b7dfa

View File

@ -782,31 +782,34 @@ int MergeBSPMain( Args& args ){
} }
} }
/* make target/targetname names unique */ /* make target/targetname names unique */
if( fixnames ){ ///! assumes there are no dummy targetnames to fix in incoming bsp if( fixnames ){
const auto has_name = []( const std::vector<entity_t>& entities, const char *name ){ const auto is_name = []( const epair_t& ep ){ return striEqual( ep.key.c_str(), "target" ) || striEqual( ep.key.c_str(), "targetname" ); };
const auto has_name = [is_name]( const std::vector<entity_t>& entities, const char *name ){
for( auto&& e : entities ) for( auto&& e : entities )
if( striEqual( name, e.valueForKey( "target" ) ) for( auto&& ep : e.epairs )
|| striEqual( name, e.valueForKey( "targetname" ) ) ) if( is_name( ep ) && striEqual( name, ep.value.c_str() ) )
return true; return true;
return false; return false;
}; };
for( auto&& e : bsp.entities ) for( auto&& e : bsp.entities )
{ {
if( const char *name; e.read_keyvalue( name, "target" ) ){ for( const char *key : { "target", "targetname" } )
if( has_name( entities, name ) ){ {
StringOutputStream newName; if( const char *name; e.read_keyvalue( name, key ) ){
int id = 0; if( has_name( entities, name ) ){
do{ StringOutputStream newName;
newName( name, '_', id++ ); int id = 0;
} while( has_name( entities, newName ) do{
|| has_name( bsp.entities, newName ) ); newName( name, '_', id++ );
} while( has_name( entities, newName )
|| has_name( bsp.entities, newName ) );
const CopiedString oldName = name; // backup it, original will change const CopiedString oldName = name; // backup it, original will change
for( auto&& e : bsp.entities ) for( auto&& e : bsp.entities )
for( auto&& ep : e.epairs ) for( auto&& ep : e.epairs )
if( ( striEqual( ep.key.c_str(), "target" ) || striEqual( ep.key.c_str(), "targetname" ) ) if( is_name( ep ) && striEqual( ep.value.c_str(), oldName.c_str() ) )
&& striEqual( ep.value.c_str(), oldName.c_str() ) ) ep.value = newName;
ep.value = newName; }
} }
} }
} }