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

@ -46,33 +46,33 @@
*/ */
void ExportEntities( void ){ void ExportEntities( void ){
char filename[ 1024 ]; char filename[ 1024 ];
FILE *file; FILE *file;
/* note it */ /* note it */
Sys_FPrintf( SYS_VRB, "--- ExportEntities ---\n" ); Sys_FPrintf( SYS_VRB, "--- ExportEntities ---\n" );
/* do some path mangling */ /* do some path mangling */
strcpy( filename, source ); strcpy( filename, source );
path_set_extension( filename, ".ent" ); path_set_extension( filename, ".ent" );
/* sanity check */ /* sanity check */
if ( bspEntData == NULL || bspEntDataSize == 0 ) { if ( bspEntData == NULL || bspEntDataSize == 0 ) {
Sys_Warning( "No BSP entity data. aborting...\n" ); Sys_Warning( "No BSP entity data. aborting...\n" );
return; return;
} }
/* write it */ /* write it */
Sys_Printf( "Writing %s\n", filename ); Sys_Printf( "Writing %s\n", filename );
Sys_FPrintf( SYS_VRB, "(%d bytes)\n", bspEntDataSize ); Sys_FPrintf( SYS_VRB, "(%d bytes)\n", bspEntDataSize );
file = fopen( filename, "w" ); file = fopen( filename, "w" );
if ( file == NULL ) { if ( file == NULL ) {
Error( "Unable to open %s for writing", filename ); Error( "Unable to open %s for writing", filename );
} }
fprintf( file, "%s\n", bspEntData ); fprintf( file, "%s\n", bspEntData );
fclose( file ); fclose( file );
} }
@ -83,23 +83,23 @@ void ExportEntities( void ){
*/ */
int ExportEntitiesMain( int argc, char **argv ){ int ExportEntitiesMain( int argc, char **argv ){
/* arg checking */ /* arg checking */
if ( argc < 2 ) { if ( argc < 2 ) {
Sys_Printf( "Usage: q3map2 -exportents [-v] <mapname>\n" ); Sys_Printf( "Usage: q3map2 -exportents [-v] <mapname>\n" );
return 0; return 0;
} }
/* do some path mangling */ /* do some path mangling */
strcpy( source, ExpandArg( argv[ argc - 1 ] ) ); strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
path_set_extension( source, ".bsp" ); path_set_extension( source, ".bsp" );
/* load the bsp */ /* load the bsp */
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 */
return 0; return 0;
} }

View File

@ -918,7 +918,7 @@ enum class ESurfaceType
constexpr const char *surfaceTypeName( ESurfaceType type ){ constexpr const char *surfaceTypeName( ESurfaceType type ){
switch ( type ) switch ( type )
{ {
case ESurfaceType::Bad: return "ESurfaceType::Bad"; case ESurfaceType::Bad: return "ESurfaceType::Bad";
case ESurfaceType::Face: return "ESurfaceType::Face"; case ESurfaceType::Face: return "ESurfaceType::Face";
case ESurfaceType::Patch: return "ESurfaceType::Patch"; case ESurfaceType::Patch: return "ESurfaceType::Patch";
case ESurfaceType::Triangles: return "ESurfaceType::Triangles"; case ESurfaceType::Triangles: return "ESurfaceType::Triangles";