Skip to content

Commit

Permalink
allow to toggle "Organize save files" at runtime (#2082)
Browse files Browse the repository at this point in the history
* allow to toggle "Organize save files" at runtime

* create savegame directory only when saving the game

* put free/reassign into a macro
  • Loading branch information
fabiangreffrath authored Dec 8, 2024
1 parent 84a56a2 commit 913c068
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 97 deletions.
191 changes: 95 additions & 96 deletions src/d_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -889,57 +889,6 @@ void IdentifyVersion(void)

basedefault = M_StringJoin(D_DoomPrefDir(), DIR_SEPARATOR_S,
D_DoomExeName(), ".cfg");
// set save path to -save parm or current dir

screenshotdir = M_StringDuplicate("."); // [FG] default to current dir

basesavegame = M_StringDuplicate(
D_DoomPrefDir()); // jff 3/27/98 default to current dir

//!
// @arg <directory>
//
// Specify a path from which to load and save games. If the directory
// does not exist then it will automatically be created.
//

int p = M_CheckParmWithArgs("-save", 1);
if (p > 0)
{
if (basesavegame)
{
free(basesavegame);
}
basesavegame = M_StringDuplicate(myargv[p + 1]);

M_MakeDirectory(basesavegame);

// [FG] fall back to -save parm
if (screenshotdir)
{
free(screenshotdir);
}
screenshotdir = M_StringDuplicate(basesavegame);
}

//!
// @arg <directory>
//
// Specify a path to save screenshots. If the directory does not
// exist then it will automatically be created.
//

p = M_CheckParmWithArgs("-shotdir", 1);
if (p > 0)
{
if (screenshotdir)
{
free(screenshotdir);
}
screenshotdir = M_StringDuplicate(myargv[p + 1]);

M_MakeDirectory(screenshotdir);
}

// locate the IWAD and determine game mode from it

Expand Down Expand Up @@ -1759,14 +1708,107 @@ static boolean CheckHaveSSG (void)
return true;
}

static int mainwadfile;

#define SET_DIR(a, b) \
if ((a)) \
{ \
free((a)); \
} \
(a) = (b);

void D_SetSavegameDirectory(void)
{
// set save path to -save parm or current dir

SET_DIR(screenshotdir, M_StringDuplicate("."));

SET_DIR(basesavegame, M_StringDuplicate(D_DoomPrefDir()));

//!
// @arg <directory>
//
// Specify a path from which to load and save games. If the directory
// does not exist then it will automatically be created.
//

int p = M_CheckParmWithArgs("-save", 1);
if (p > 0)
{
SET_DIR(basesavegame, M_StringDuplicate(myargv[p + 1]));

M_MakeDirectory(basesavegame);

// [FG] fall back to -save parm
SET_DIR(screenshotdir, M_StringDuplicate(basesavegame));
}
else
{
if (organize_savefiles == -1)
{
// [FG] check for at least one savegame in the old location
glob_t *glob = I_StartMultiGlob(
basesavegame, GLOB_FLAG_NOCASE | GLOB_FLAG_SORTED, "*.dsg");

organize_savefiles = (I_NextGlob(glob) == NULL);

I_EndGlob(glob);
}

if (organize_savefiles)
{
const char *wadname = wadfiles[0];

for (int i = mainwadfile; i < array_size(wadfiles); i++)
{
if (FileContainsMaps(wadfiles[i]))
{
wadname = wadfiles[i];
break;
}
}

char *oldsavegame = basesavegame;
basesavegame =
M_StringJoin(oldsavegame, DIR_SEPARATOR_S, "savegames");
free(oldsavegame);

M_MakeDirectory(basesavegame);

oldsavegame = basesavegame;
basesavegame = M_StringJoin(oldsavegame, DIR_SEPARATOR_S,
M_BaseName(wadname));
free(oldsavegame);
}
}

//!
// @arg <directory>
//
// Specify a path to save screenshots. If the directory does not
// exist then it will automatically be created.
//

p = M_CheckParmWithArgs("-shotdir", 1);
if (p > 0)
{
SET_DIR(screenshotdir, M_StringDuplicate(myargv[p + 1]));

M_MakeDirectory(screenshotdir);
}

I_Printf(VB_INFO, "Savegame directory: %s", basesavegame);
}

#undef SET_DIR

//
// D_DoomMain
//

void D_DoomMain(void)
{
int p;
int mainwadfile = 0;

setbuf(stdout,NULL);

Expand Down Expand Up @@ -2323,50 +2365,7 @@ void D_DoomMain(void)

G_ParseCompDatabase();

if (!M_CheckParm("-save"))
{
if (organize_savefiles == -1)
{
// [FG] check for at least one savegame in the old location
glob_t *glob = I_StartMultiGlob(
basesavegame, GLOB_FLAG_NOCASE | GLOB_FLAG_SORTED, "*.dsg");

organize_savefiles = (I_NextGlob(glob) == NULL);

I_EndGlob(glob);
}

if (organize_savefiles)
{
int i;
const char *wadname = wadfiles[0];
char *oldsavegame = basesavegame;

for (i = mainwadfile; i < array_size(wadfiles); i++)
{
if (FileContainsMaps(wadfiles[i]))
{
wadname = wadfiles[i];
break;
}
}

basesavegame =
M_StringJoin(oldsavegame, DIR_SEPARATOR_S, "savegames");
free(oldsavegame);

M_MakeDirectory(basesavegame);

oldsavegame = basesavegame;
basesavegame = M_StringJoin(oldsavegame, DIR_SEPARATOR_S,
M_BaseName(wadname));
free(oldsavegame);

M_MakeDirectory(basesavegame);
}
}

I_Printf(VB_INFO, "Savegame directory: %s", basesavegame);
D_SetSavegameDirectory();

V_InitColorTranslation(); //jff 4/24/98 load color translation lumps

Expand Down
1 change: 1 addition & 0 deletions src/d_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ char *D_DoomExeName(void); // killough 10/98: executable's name
extern char *basesavegame; // killough 2/16/98: savegame path
extern char *screenshotdir; // [FG] screenshot path
char *D_DoomPrefDir(void); // [FG] default configuration dir
void D_SetSavegameDirectory(void);

extern const char *gamedescription;

Expand Down
2 changes: 2 additions & 0 deletions src/g_game.c
Original file line number Diff line number Diff line change
Expand Up @@ -2465,6 +2465,8 @@ static void DoSaveGame(char *name)

length = save_p - savebuffer;

M_MakeDirectory(basesavegame);

if (!M_WriteFile(name, savebuffer, length))
displaymsg("%s", errno ? strerror(errno) : "Could not save game: Error unknown");
else
Expand Down
3 changes: 2 additions & 1 deletion src/mn_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -3313,7 +3313,7 @@ static setup_menu_t gen_settings6[] = {
.action = M_ResetAutoSave},

{"Organize save files", S_ONOFF | S_PRGWARN, OFF_CNTR_X, M_SPC,
{"organize_savefiles"}},
{"organize_savefiles"}, .action = D_SetSavegameDirectory},

MI_GAP,

Expand Down Expand Up @@ -4867,6 +4867,7 @@ void MN_SetupResetMenu(void)
DisableItem(!brightmaps_found || force_brightmaps, gen_settings5,
"brightmaps");
DisableItem(!trakinfo_found, gen_settings2, "extra_music");
DisableItem(M_ParmExists("-save"), gen_settings6, "organize_savefiles");
UpdateInterceptsEmuItem();
UpdateStatsFormatItem();
UpdateCrosshairItems();
Expand Down

0 comments on commit 913c068

Please sign in to comment.