From 6c065dd30ecaa1fa68c20e894a07993a3fdff1f2 Mon Sep 17 00:00:00 2001 From: Ensiform Date: Wed, 15 Nov 2023 08:23:55 -0600 Subject: [PATCH] Added writeconfig nodefault and force options --- src/qcommon/common.c | 40 ++++++++++++++++++++++++++++++++-------- src/qcommon/cvar.c | 6 +++--- src/qcommon/qcommon.h | 2 +- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/qcommon/common.c b/src/qcommon/common.c index dd37b3d0..3a14d270 100644 --- a/src/qcommon/common.c +++ b/src/qcommon/common.c @@ -4269,7 +4269,7 @@ void Com_Init( char *commandLine ) { //================================================================== -static void Com_WriteConfigToFile( const char *filename ) { +static void Com_WriteConfigToFile( const char *filename, qboolean forcewrite, qboolean nodefault ) { fileHandle_t f; f = FS_FOpenFileWrite( filename ); @@ -4284,7 +4284,7 @@ static void Com_WriteConfigToFile( const char *filename ) { #ifndef DEDICATED Key_WriteBindings( f ); #endif - Cvar_WriteVariables( f ); + Cvar_WriteVariables( f, forcewrite, nodefault ); FS_FCloseFile( f ); } @@ -4313,12 +4313,12 @@ void Com_WriteConfiguration( void ) { #ifndef DEDICATED if ( com_gameInfo.usesProfiles && cl_profileStr[0] ) { - Com_WriteConfigToFile( va( "profiles/%s/%s", cl_profileStr, CONFIG_NAME ) ); + Com_WriteConfigToFile( va( "profiles/%s/%s", cl_profileStr, CONFIG_NAME ), qfalse, qfalse ); } else #endif { - Com_WriteConfigToFile( Q3CONFIG_CFG ); + Com_WriteConfigToFile( Q3CONFIG_CFG, qfalse, qfalse ); } } @@ -4333,13 +4333,37 @@ Write the config file to a specific name static void Com_WriteConfig_f( void ) { char filename[MAX_QPATH]; const char *ext; + int i, skip = 1; + qboolean force = qfalse, nodefault = qfalse; - if ( Cmd_Argc() != 2 ) { - Com_Printf( "Usage: writeconfig \n" ); + if ( Cmd_Argc() < 2 ) { + Com_Printf( "Usage: writeconfig [options] \n" + "-nd, --no-defaults : omit writing all cvars that are not default value\n" + "-f, --force : write all cvars regardless of archive and default value state\n" ); + return; + } + + if ( Cmd_Argc() >= 3 ) { + for ( i = 1; i < Cmd_Argc(); i++ ) { + const char *opt = Cmd_Argv(i); + if ( !Q_stricmp(opt, "-nd") || !Q_stricmp(opt, "--no-defaults") ) { + nodefault = qtrue; + skip++; + } + else if ( !Q_stricmp(opt, "-f") || !Q_stricmp(opt, "--force") ) { + force = qtrue; + skip++; + } + } + } + else { + Com_Printf( "Usage: writeconfig [options] \n" + "-nd, --no-defaults : omit writing all cvars that are not default value\n" + "-f, --force : write all cvars regardless of archive and default value state\n" ); return; } - Q_strncpyz( filename, Cmd_Argv(1), sizeof( filename ) ); + Q_strncpyz( filename, Cmd_Argv(skip), sizeof( filename ) ); COM_DefaultExtension( filename, sizeof( filename ), ".cfg" ); if ( !FS_AllowedExtension( filename, qfalse, &ext ) ) { @@ -4353,7 +4377,7 @@ static void Com_WriteConfig_f( void ) { } Com_Printf( "Writing %s.\n", filename ); - Com_WriteConfigToFile( filename ); + Com_WriteConfigToFile( filename, force, nodefault ); } diff --git a/src/qcommon/cvar.c b/src/qcommon/cvar.c index 5a7900ab..8c8a95bd 100644 --- a/src/qcommon/cvar.c +++ b/src/qcommon/cvar.c @@ -1560,7 +1560,7 @@ Appends lines containing "set variable value" for all variables with the archive flag set to qtrue. ============ */ -void Cvar_WriteVariables( fileHandle_t f ) +void Cvar_WriteVariables( fileHandle_t f, qboolean forcewrite, qboolean nodefault ) { cvar_t *var; char buffer[MAX_CMD_LINE]; @@ -1577,7 +1577,7 @@ void Cvar_WriteVariables( fileHandle_t f ) if ( !var->name || Q_stricmp( var->name, "cl_cdkey" ) == 0 ) continue; - if ( var->flags & CVAR_ARCHIVE ) { + if ( forcewrite || (var->flags & CVAR_ARCHIVE) ) { int len; // write the latched value, even if it hasn't taken effect yet value = var->latchedString ? var->latchedString : var->string; @@ -1586,7 +1586,7 @@ void Cvar_WriteVariables( fileHandle_t f ) value == var->latchedString ? "latched " : "", var->name ); continue; } - if ( (var->flags & CVAR_NODEFAULT) && !strcmp( value, var->resetString ) ) { + if ( !forcewrite && (nodefault || (var->flags & CVAR_NODEFAULT)) && !strcmp( value, var->resetString ) ) { continue; } if ( var->flags & CVAR_UNSAFE ) diff --git a/src/qcommon/qcommon.h b/src/qcommon/qcommon.h index 3a822621..00358480 100644 --- a/src/qcommon/qcommon.h +++ b/src/qcommon/qcommon.h @@ -649,7 +649,7 @@ qboolean Cvar_Command( void ); // command. Returns true if the command was a variable reference that // was handled. (print or change) -void Cvar_WriteVariables( fileHandle_t f ); +void Cvar_WriteVariables( fileHandle_t f, qboolean forcewrite, qboolean nodefault ); // writes lines containing "set variable value" for all variables // with the archive flag set to true.