simplify unlimited bsp entities code

This commit is contained in:
Garux 2021-02-08 04:36:57 +03:00
parent d5f55e7c35
commit b013f9954c
3 changed files with 56 additions and 87 deletions

View File

@ -504,13 +504,12 @@ void PrintBSPFileSizes( void ){
strips low byte chars off the end of a string strips low byte chars off the end of a string
*/ */
void StripTrailing( char *e ){ StringRange StripTrailing( const char *string ){
char *s = e + strlen( e ) - 1; const char *end = string + strlen( string );
while ( s >= e && *s <= 32 ) while ( end != string && end[-1] <= 32 ){
{ --end;
strClear( s );
s--;
} }
return StringRange( string, end );
} }
@ -523,14 +522,12 @@ void StripTrailing( char *e ){
void ParseEPair( std::list<epair_t>& epairs ){ void ParseEPair( std::list<epair_t>& epairs ){
/* handle key */ /* handle key */
/* strip trailing spaces that sometimes get accidentally added in the editor */ /* strip trailing spaces that sometimes get accidentally added in the editor */
StripTrailing( token );
epair_t ep; epair_t ep;
ep.key = token; ep.key = StripTrailing( token );
/* handle value */ /* handle value */
GetToken( false ); GetToken( false );
StripTrailing( token ); ep.value = StripTrailing( token );
ep.value = token;
if( !ep.key.empty() && !ep.value.empty() ) if( !ep.key.empty() && !ep.value.empty() )
epairs.emplace_back( ep ); epairs.emplace_back( ep );
@ -637,33 +634,18 @@ void InjectCommandLine( char **argv, int beginArgs, int endArgs ){
*/ */
void UnparseEntities( void ){ void UnparseEntities( void ){
char *buf, *end; StringOutputStream data( 8192 );
char line[ 2048 ];
char key[ 1024 ], value[ 1024 ];
/* setup */
AUTOEXPAND_BY_REALLOC( bspEntData, 0, allocatedBSPEntData, 1024 );
strClear( bspEntData );
end = buf = bspEntData;
/* run through entity list */ /* run through entity list */
for ( std::size_t i = 0; i < numBSPEntities && i < entities.size(); i++ ) for ( std::size_t i = 0; i < numBSPEntities && i < entities.size(); i++ )
{ {
{ const entity_t& e = entities[ i ];
int sz = end - buf;
AUTOEXPAND_BY_REALLOC( bspEntData, sz + 65536, allocatedBSPEntData, 1024 );
buf = bspEntData;
end = buf + sz;
}
entity_t *e = &entities[ i ];
/* get epair */ /* get epair */
if ( e->epairs.empty() ) { if ( e.epairs.empty() ) {
continue; /* ent got removed */ continue; /* ent got removed */
} }
/* ydnar: certain entities get stripped from bsp file */ /* ydnar: certain entities get stripped from bsp file */
const char *classname = e->classname(); const char *classname = e.classname();
if ( striEqual( classname, "misc_model" ) || if ( striEqual( classname, "misc_model" ) ||
striEqual( classname, "_decal" ) || striEqual( classname, "_decal" ) ||
striEqual( classname, "_skybox" ) ) { striEqual( classname, "_skybox" ) ) {
@ -671,36 +653,23 @@ void UnparseEntities( void ){
} }
/* add beginning brace */ /* add beginning brace */
strcat( end, "{\n" ); data << "{\n";
end += 2;
/* walk epair list */ /* walk epair list */
for ( const auto& ep : e->epairs ) for ( const auto& ep : e.epairs )
{ {
/* copy and clean */ /* copy and clean */
strcpy( key, ep.key.c_str() ); data << '\"' << StripTrailing( ep.key.c_str() ) << "\" \"" << StripTrailing( ep.value.c_str() ) << "\"\n";
StripTrailing( key );
strcpy( value, ep.value.c_str() );
StripTrailing( value );
/* add to buffer */
sprintf( line, "\"%s\" \"%s\"\n", key, value );
strcat( end, line );
end += strlen( line );
} }
/* add trailing brace */ /* add trailing brace */
strcat( end, "}\n" ); data << "}\n";
end += 2;
/* check for overflow */
if ( end > buf + allocatedBSPEntData ) {
Error( "Entity text too long" );
}
} }
/* set size */ /* save out */
bspEntDataSize = end - buf + 1; bspEntDataSize = data.end() - data.begin() + 1;
AUTOEXPAND_BY_REALLOC( bspEntData, bspEntDataSize, allocatedBSPEntData, 1024 );
strcpy( bspEntData, data );
} }

View File

@ -97,7 +97,7 @@ int ExportEntitiesMain( int argc, char **argv ){
Sys_Printf( "Loading %s\n", source ); Sys_Printf( "Loading %s\n", source );
LoadBSPFile( source ); LoadBSPFile( source );
/* export the lightmaps */ /* export entities */
ExportEntities(); ExportEntities();
/* return to sender */ /* return to sender */