Skip to content

Commit

Permalink
Added writeconfig nodefault and force options
Browse files Browse the repository at this point in the history
  • Loading branch information
ensiform committed Nov 15, 2023
1 parent f55da33 commit 6c065dd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
40 changes: 32 additions & 8 deletions src/qcommon/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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 );
}

Expand Down Expand Up @@ -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 );
}
}

Expand All @@ -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 <filename>\n" );
if ( Cmd_Argc() < 2 ) {
Com_Printf( "Usage: writeconfig [options] <filename>\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] <filename>\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 ) ) {
Expand All @@ -4353,7 +4377,7 @@ static void Com_WriteConfig_f( void ) {
}

Com_Printf( "Writing %s.\n", filename );
Com_WriteConfigToFile( filename );
Com_WriteConfigToFile( filename, force, nodefault );
}


Expand Down
6 changes: 3 additions & 3 deletions src/qcommon/cvar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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;
Expand All @@ -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 )
Expand Down
2 changes: 1 addition & 1 deletion src/qcommon/qcommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit 6c065dd

Please sign in to comment.