Skip to content

Commit

Permalink
Console: Don't save config and bindings before they've been restored
Browse files Browse the repository at this point in the history
An abnormal or early shutdown may cause the state to be saved
prematurely.
  • Loading branch information
skyjake committed Apr 11, 2012
1 parent 36ed1aa commit b99f211
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
11 changes: 10 additions & 1 deletion doomsday/engine/portable/include/con_config.h
Expand Up @@ -29,8 +29,17 @@
#ifndef LIBDENG_CONSOLE_CONFIG_H
#define LIBDENG_CONSOLE_CONFIG_H

boolean Con_ParseCommands(const char* fileName, boolean setdefault);
// Flags for Con_ParseCommands2.
#define CPCF_SET_DEFAULT 0x1
#define CPCF_ALLOW_SAVE_STATE 0x2
#define CPCF_ALLOW_SAVE_BINDINGS 0x4

boolean Con_ParseCommands(const char* fileName);

boolean Con_ParseCommands2(const char* fileName, int flags);

void Con_SaveDefaults(void);

boolean Con_WriteState(const char* fileName, const char* bindingsFileName);

D_CMD(WriteConsole);
Expand Down
31 changes: 20 additions & 11 deletions doomsday/engine/portable/src/con_config.c
Expand Up @@ -51,6 +51,7 @@
// PUBLIC DATA DEFINITIONS -------------------------------------------------

static filename_t cfgFile;
static int flagsAllow = 0;

// PRIVATE DATA DEFINITIONS ------------------------------------------------

Expand Down Expand Up @@ -155,6 +156,8 @@ static boolean writeConsoleState(const char* fileName)
FILE* file;
if(!fileName || !fileName[0]) return false;

VERBOSE(Con_Message("Writing state to \"%s\"...\n", fileName));

Str_Init(&nativePath);
Str_Set(&nativePath, fileName);
F_ToNativeSlashes(&nativePath, &nativePath);
Expand Down Expand Up @@ -191,6 +194,8 @@ static boolean writeBindingsState(const char* fileName)
FILE* file;
if(!fileName || !fileName[0]) return false;

VERBOSE(Con_Message("Writing bindings to \"%s\"...\n", fileName));

Str_Init(&nativePath);
Str_Set(&nativePath, fileName);
F_ToNativeSlashes(&nativePath, &nativePath);
Expand All @@ -217,10 +222,16 @@ static boolean writeBindingsState(const char* fileName)
return false;
}

boolean Con_ParseCommands(const char* fileName, boolean setdefault)
boolean Con_ParseCommands(const char* fileName)
{
return Con_ParseCommands2(fileName, 0);
}

boolean Con_ParseCommands2(const char* fileName, int flags)
{
DFile* file;
char buff[512];
boolean setdefault = (flags & CPCF_SET_DEFAULT) != 0;

// Is this supposed to be the default?
if(setdefault)
Expand Down Expand Up @@ -253,6 +264,10 @@ boolean Con_ParseCommands(const char* fileName, boolean setdefault)
}}

F_Delete(file);

// Update the allowed operations.
flagsAllow |= flags & (CPCF_ALLOW_SAVE_STATE | CPCF_ALLOW_SAVE_BINDINGS);

return true;
}

Expand All @@ -262,18 +277,12 @@ boolean Con_ParseCommands(const char* fileName, boolean setdefault)
*/
boolean Con_WriteState(const char* fileName, const char* bindingsFileName)
{
if(fileName || bindingsFileName)
if(fileName && (flagsAllow & CPCF_ALLOW_SAVE_STATE))
{
VERBOSE(
Con_Printf("Writing");
if(fileName)
Con_Printf(" state:\"%s\"", fileName);
if(bindingsFileName)
Con_Printf(" bindings:\"%s\"", bindingsFileName);
Con_Printf("...\n")
)

writeConsoleState(fileName);
}
if(bindingsFileName && (flagsAllow & CPCF_ALLOW_SAVE_BINDINGS))
{
// Bindings go into a separate file.
writeBindingsState(bindingsFileName);
}
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/con_main.c
Expand Up @@ -2250,7 +2250,7 @@ D_CMD(Parse)
for(i = 1; i < argc; ++i)
{
Con_Printf("Parsing %s.\n", argv[i]);
Con_ParseCommands(argv[i], false);
Con_ParseCommands(argv[i]);
}
return true;
}
Expand Down
12 changes: 6 additions & 6 deletions doomsday/engine/portable/src/dd_main.c
Expand Up @@ -993,7 +993,7 @@ static int DD_ActivateGameWorker(void* paramaters)
}

Con_Message("Parsing primary config \"%s\"...\n", F_PrettyPath(Str_Text(configFileName)));
Con_ParseCommands(Str_Text(configFileName), true);
Con_ParseCommands2(Str_Text(configFileName), CPCF_SET_DEFAULT | CPCF_ALLOW_SAVE_STATE);
if(configFileName == &tmp)
Str_Free(&tmp);
}
Expand All @@ -1004,7 +1004,7 @@ static int DD_ActivateGameWorker(void* paramaters)
B_BindGameDefaults();

// Read bindings for this game and merge with the working set.
Con_ParseCommands(Str_Text(Game_BindingConfig(theGame)), false);
Con_ParseCommands2(Str_Text(Game_BindingConfig(theGame)), CPCF_ALLOW_SAVE_BINDINGS);
}

if(p->initiatedBusyMode)
Expand Down Expand Up @@ -1641,7 +1641,7 @@ boolean DD_Init(void)
// Try to load the autoexec file. This is done here to make sure everything is
// initialized: the user can do here anything that s/he'd be able to do in-game
// provided a game was loaded during startup.
Con_ParseCommands("autoexec.cfg", false);
Con_ParseCommands("autoexec.cfg");

// Read additional config files that should be processed post engine init.
if(ArgCheckWith("-parse", 1))
Expand All @@ -1655,7 +1655,7 @@ boolean DD_Init(void)
if(!arg || arg[0] == '-') break;

Con_Message(" Processing \"%s\"...\n", F_PrettyPath(arg));
Con_ParseCommands(arg, false);
Con_ParseCommands(arg);
}
VERBOSE( Con_Message(" Done in %.2f seconds.\n", (Sys_GetRealTime() - startTime) / 1000.0f) );
}
Expand Down Expand Up @@ -1791,7 +1791,7 @@ static int DD_StartupWorker(void* parm)
if(!arg || arg[0] == '-')
break;
Con_Message(" Processing \"%s\"...\n", F_PrettyPath(arg));
Con_ParseCommands(arg, false);
Con_ParseCommands(arg);
}
VERBOSE( Con_Message(" Done in %.2f seconds.\n", (Sys_GetRealTime() - startTime) / 1000.0f) );
}
Expand All @@ -1815,7 +1815,7 @@ static int DD_StartupWorker(void* parm)
Con_SetProgress(60);

// Execute the startup script (Startup.cfg).
Con_ParseCommands("startup.cfg", false);
Con_ParseCommands("startup.cfg");

// Get the material manager up and running.
Con_SetProgress(90);
Expand Down

0 comments on commit b99f211

Please sign in to comment.