From b013f9954c1f5b4d291b106b9ec5df67ac3746b9 Mon Sep 17 00:00:00 2001 From: Garux Date: Mon, 8 Feb 2021 04:36:57 +0300 Subject: [PATCH] simplify unlimited bsp entities code --- tools/quake3/q3map2/bspfile_abstract.cpp | 69 +++++++---------------- tools/quake3/q3map2/exportents.cpp | 72 ++++++++++++------------ tools/quake3/q3map2/q3map2.h | 2 +- 3 files changed, 56 insertions(+), 87 deletions(-) diff --git a/tools/quake3/q3map2/bspfile_abstract.cpp b/tools/quake3/q3map2/bspfile_abstract.cpp index 5086a6f2..94b6bbe5 100644 --- a/tools/quake3/q3map2/bspfile_abstract.cpp +++ b/tools/quake3/q3map2/bspfile_abstract.cpp @@ -504,13 +504,12 @@ void PrintBSPFileSizes( void ){ strips low byte chars off the end of a string */ -void StripTrailing( char *e ){ - char *s = e + strlen( e ) - 1; - while ( s >= e && *s <= 32 ) - { - strClear( s ); - s--; +StringRange StripTrailing( const char *string ){ + const char *end = string + strlen( string ); + while ( end != string && end[-1] <= 32 ){ + --end; } + return StringRange( string, end ); } @@ -523,14 +522,12 @@ void StripTrailing( char *e ){ void ParseEPair( std::list& epairs ){ /* handle key */ /* strip trailing spaces that sometimes get accidentally added in the editor */ - StripTrailing( token ); epair_t ep; - ep.key = token; + ep.key = StripTrailing( token ); /* handle value */ GetToken( false ); - StripTrailing( token ); - ep.value = token; + ep.value = StripTrailing( token ); if( !ep.key.empty() && !ep.value.empty() ) epairs.emplace_back( ep ); @@ -637,33 +634,18 @@ void InjectCommandLine( char **argv, int beginArgs, int endArgs ){ */ void UnparseEntities( void ){ - char *buf, *end; - char line[ 2048 ]; - char key[ 1024 ], value[ 1024 ]; - - - /* setup */ - AUTOEXPAND_BY_REALLOC( bspEntData, 0, allocatedBSPEntData, 1024 ); - strClear( bspEntData ); - end = buf = bspEntData; + StringOutputStream data( 8192 ); /* run through entity list */ for ( std::size_t i = 0; i < numBSPEntities && i < entities.size(); i++ ) { - { - int sz = end - buf; - AUTOEXPAND_BY_REALLOC( bspEntData, sz + 65536, allocatedBSPEntData, 1024 ); - buf = bspEntData; - end = buf + sz; - } - - entity_t *e = &entities[ i ]; + const entity_t& e = entities[ i ]; /* get epair */ - if ( e->epairs.empty() ) { + if ( e.epairs.empty() ) { continue; /* ent got removed */ } /* ydnar: certain entities get stripped from bsp file */ - const char *classname = e->classname(); + const char *classname = e.classname(); if ( striEqual( classname, "misc_model" ) || striEqual( classname, "_decal" ) || striEqual( classname, "_skybox" ) ) { @@ -671,36 +653,23 @@ void UnparseEntities( void ){ } /* add beginning brace */ - strcat( end, "{\n" ); - end += 2; + data << "{\n"; /* walk epair list */ - for ( const auto& ep : e->epairs ) + for ( const auto& ep : e.epairs ) { /* copy and clean */ - strcpy( key, ep.key.c_str() ); - 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 ); + data << '\"' << StripTrailing( ep.key.c_str() ) << "\" \"" << StripTrailing( ep.value.c_str() ) << "\"\n"; } /* add trailing brace */ - strcat( end, "}\n" ); - end += 2; - - /* check for overflow */ - if ( end > buf + allocatedBSPEntData ) { - Error( "Entity text too long" ); - } + data << "}\n"; } - /* set size */ - bspEntDataSize = end - buf + 1; + /* save out */ + bspEntDataSize = data.end() - data.begin() + 1; + AUTOEXPAND_BY_REALLOC( bspEntData, bspEntDataSize, allocatedBSPEntData, 1024 ); + strcpy( bspEntData, data ); } diff --git a/tools/quake3/q3map2/exportents.cpp b/tools/quake3/q3map2/exportents.cpp index e6521672..f7296f27 100644 --- a/tools/quake3/q3map2/exportents.cpp +++ b/tools/quake3/q3map2/exportents.cpp @@ -46,33 +46,33 @@ */ void ExportEntities( void ){ - char filename[ 1024 ]; - FILE *file; + char filename[ 1024 ]; + FILE *file; - /* note it */ - Sys_FPrintf( SYS_VRB, "--- ExportEntities ---\n" ); + /* note it */ + Sys_FPrintf( SYS_VRB, "--- ExportEntities ---\n" ); - /* do some path mangling */ - strcpy( filename, source ); - path_set_extension( filename, ".ent" ); + /* do some path mangling */ + strcpy( filename, source ); + path_set_extension( filename, ".ent" ); - /* sanity check */ - if ( bspEntData == NULL || bspEntDataSize == 0 ) { - Sys_Warning( "No BSP entity data. aborting...\n" ); - return; - } + /* sanity check */ + if ( bspEntData == NULL || bspEntDataSize == 0 ) { + Sys_Warning( "No BSP entity data. aborting...\n" ); + return; + } - /* write it */ - Sys_Printf( "Writing %s\n", filename ); - Sys_FPrintf( SYS_VRB, "(%d bytes)\n", bspEntDataSize ); - file = fopen( filename, "w" ); + /* write it */ + Sys_Printf( "Writing %s\n", filename ); + Sys_FPrintf( SYS_VRB, "(%d bytes)\n", bspEntDataSize ); + file = fopen( filename, "w" ); - if ( file == NULL ) { - Error( "Unable to open %s for writing", filename ); - } + if ( file == NULL ) { + Error( "Unable to open %s for writing", filename ); + } - fprintf( file, "%s\n", bspEntData ); - fclose( file ); + fprintf( file, "%s\n", bspEntData ); + fclose( file ); } @@ -83,23 +83,23 @@ void ExportEntities( void ){ */ int ExportEntitiesMain( int argc, char **argv ){ - /* arg checking */ - if ( argc < 2 ) { - Sys_Printf( "Usage: q3map2 -exportents [-v] \n" ); - return 0; - } + /* arg checking */ + if ( argc < 2 ) { + Sys_Printf( "Usage: q3map2 -exportents [-v] \n" ); + return 0; + } - /* do some path mangling */ - strcpy( source, ExpandArg( argv[ argc - 1 ] ) ); - path_set_extension( source, ".bsp" ); + /* do some path mangling */ + strcpy( source, ExpandArg( argv[ argc - 1 ] ) ); + path_set_extension( source, ".bsp" ); - /* load the bsp */ - Sys_Printf( "Loading %s\n", source ); - LoadBSPFile( source ); + /* load the bsp */ + Sys_Printf( "Loading %s\n", source ); + LoadBSPFile( source ); - /* export the lightmaps */ - ExportEntities(); + /* export entities */ + ExportEntities(); - /* return to sender */ - return 0; + /* return to sender */ + return 0; } diff --git a/tools/quake3/q3map2/q3map2.h b/tools/quake3/q3map2/q3map2.h index 241e0682..ca5fe652 100644 --- a/tools/quake3/q3map2/q3map2.h +++ b/tools/quake3/q3map2/q3map2.h @@ -918,7 +918,7 @@ enum class ESurfaceType constexpr const char *surfaceTypeName( ESurfaceType type ){ switch ( type ) { - case ESurfaceType::Bad: return "ESurfaceType::Bad"; + case ESurfaceType::Bad: return "ESurfaceType::Bad"; case ESurfaceType::Face: return "ESurfaceType::Face"; case ESurfaceType::Patch: return "ESurfaceType::Patch"; case ESurfaceType::Triangles: return "ESurfaceType::Triangles";