From 0ba1a8e7bc96fce7e9569c72b341e9260f9d179d Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Mon, 15 Mar 2021 03:36:50 +0300 Subject: [PATCH] engine: cfgscript: reduce copypasted code --- engine/common/cfgscript.c | 107 +++++++++++++++----------------------- 1 file changed, 41 insertions(+), 66 deletions(-) diff --git a/engine/common/cfgscript.c b/engine/common/cfgscript.c index da8c12db..b4efc1fc 100644 --- a/engine/common/cfgscript.c +++ b/engine/common/cfgscript.c @@ -203,13 +203,15 @@ qboolean CSCR_ParseHeader( parserstate_t *ps ) } /* -====================== -CSCR_WriteGameCVars +============== +CSCR_ParseFile -Print all cvars declared in script to game.cfg file -====================== +generic scr parser +will callback on each scrvardef_t +============== */ -int CSCR_WriteGameCVars( file_t *cfg, const char *scriptfilename ) +static int CSCR_ParseFile( const char *scriptfilename, + void (*callback)( scrvardef_t *var, void * ), void *userdata ) { parserstate_t state = { 0 }; qboolean success = false; @@ -232,23 +234,14 @@ int CSCR_WriteGameCVars( file_t *cfg, const char *scriptfilename ) { scrvardef_t var = { 0 }; + // Create a new object if( CSCR_ParseSingleCvar( &state, &var ) ) { - convar_t *cvar = Cvar_FindVar( var.name ); - - if( cvar && !FBitSet( cvar->flags, FCVAR_SERVER|FCVAR_ARCHIVE )) - { - // cvars will be placed in game.cfg and restored on map start - if( var.flags & FCVAR_USERINFO ) - FS_Printf( cfg, "setinfo %s \"%s\"\n", var.name, cvar->string ); - else FS_Printf( cfg, "%s \"%s\"\n", var.name, cvar->string ); - } + callback( &var, userdata ); count++; } else - { break; - } if( count > 1024 ) break; @@ -261,7 +254,6 @@ finish: if( !success ) { state.token[sizeof( state.token ) - 1] = 0; - if( start && state.buf ) Con_DPrintf( S_ERROR "Parse error in %s, byte %d, token %s\n", scriptfilename, (int)( state.buf - start ), state.token ); else Con_DPrintf( S_ERROR "Parse error in %s, token %s\n", scriptfilename, state.token ); @@ -272,62 +264,45 @@ finish: return count; } +static void CSCR_WriteVariableToFile( scrvardef_t *var, void *file ) +{ + file_t *cfg = (file_t*)file; + convar_t *cvar = Cvar_FindVar( var->name ); + + if( cvar && !FBitSet( cvar->flags, FCVAR_SERVER|FCVAR_ARCHIVE )) + { + // cvars will be placed in game.cfg and restored on map start + if( var->flags & FCVAR_USERINFO ) + FS_Printf( cfg, "setinfo %s \"%s\"\n", var->name, cvar->string ); + else FS_Printf( cfg, "%s \"%s\"\n", var->name, cvar->string ); + } +} + /* ====================== -CSCR_LoadDefaultCVars +CSCR_WriteGameCVars -Register all cvars declared in config file and set default values +Print all cvars declared in script to game.cfg file ====================== */ -int CSCR_LoadDefaultCVars( const char *scriptfilename ) +int CSCR_WriteGameCVars( file_t *cfg, const char *scriptfilename ) { - parserstate_t state = { 0 }; - qboolean success = false; - int count = 0; - fs_offset_t length = 0; - char *start; - - state.filename = scriptfilename; - state.buf = start = (char *)FS_LoadFile( scriptfilename, &length, true ); - - if( !state.buf || !length ) - return 0; - - Con_DPrintf( "Reading config script file %s\n", scriptfilename ); - - if( !CSCR_ParseHeader( &state )) - goto finish; - - while( !CSCR_ExpectString( &state, "}", false, false )) - { - scrvardef_t var = { 0 }; - - // Create a new object - if( CSCR_ParseSingleCvar( &state, &var ) ) - { - Cvar_Get( var.name, var.value, var.flags|FCVAR_TEMPORARY, var.desc ); - count++; - } - else - break; - - if( count > 1024 ) - break; - } + return CSCR_ParseFile( scriptfilename, CSCR_WriteVariableToFile, cfg ); +} - if( COM_ParseFile( state.buf, state.token )) - Con_DPrintf( S_ERROR "Got extra tokens!\n" ); - else success = true; -finish: - if( !success ) - { - state.token[sizeof( state.token ) - 1] = 0; - if( start && state.buf ) - Con_DPrintf( S_ERROR "Parse error in %s, byte %d, token %s\n", scriptfilename, (int)( state.buf - start ), state.token ); - else Con_DPrintf( S_ERROR "Parse error in %s, token %s\n", scriptfilename, state.token ); - } +static void CSCR_RegisterVariable( scrvardef_t *var, void *unused ) +{ + Cvar_Get( var->name, var->value, var->flags|FCVAR_TEMPORARY, var->desc ); +} - if( start ) Mem_Free( start ); +/* +====================== +CSCR_LoadDefaultCVars - return count; +Register all cvars declared in config file and set default values +====================== +*/ +int CSCR_LoadDefaultCVars( const char *scriptfilename ) +{ + return CSCR_ParseFile( scriptfilename, CSCR_RegisterVariable, NULL ); }