Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move core-generated files (config + saves) to frontend-defined save directory - 2nd attempt #60

Merged
merged 1 commit into from
Apr 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,7 @@ typedef struct

char com_gamedir[MAX_OSPATH];
char com_basedir[MAX_OSPATH];
char com_savedir[MAX_OSPATH];

typedef struct searchpath_s {
char filename[MAX_OSPATH];
Expand Down Expand Up @@ -1974,6 +1975,9 @@ static void COM_InitFilesystem(void)
searchpath_t *search;
#endif

// Set save directory
strcpy(com_savedir, host_parms.savedir);

// -basedir <path>
// Overrides the system supplied base directory (under id1)
i = COM_CheckParm("-basedir");
Expand Down Expand Up @@ -2002,6 +2006,17 @@ static void COM_InitFilesystem(void)
#ifdef QW_HACK
COM_AddGameDirectory(com_basedir, "qw");
#endif

// Hack: Add save directory to search path, otherwise 'exec config.cfg' will fail
// (NB: 'host_parms.use_exernal_savedir' is a bit of a kludge, but since basedir
// changes depending upon the game being loaded and various flags modify the
// final 'rom' directory, it's the cleanest way to prevent the same directory
// being added to the search list twice...)
if (host_parms.use_exernal_savedir != 0)
{
COM_AddGameDirectory(com_savedir, "");
}

//
// -path <dir or packfile> [<dir or packfile>] ...
// Fully specifies the exact search path, overriding the generated one
Expand Down
1 change: 1 addition & 0 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ struct cache_user_s;

extern char com_basedir[MAX_OSPATH];
extern char com_gamedir[MAX_OSPATH];
extern char com_savedir[MAX_OSPATH];
extern int file_from_pak; // global indicating that file came from a pak

void COM_WriteFile(const char *filename, const void *data, int len);
Expand Down
4 changes: 2 additions & 2 deletions common/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ void Con_Printf(const char *fmt, ...)

/* log all messages to file */
if (debuglog)
Sys_DebugLog(va("%s/qconsole.log", com_gamedir), "%s", msg);
Sys_DebugLog(va("%s/qconsole.log", com_savedir), "%s", msg);

if (!con_initialized)
return;
Expand Down Expand Up @@ -381,7 +381,7 @@ Con_DPrintf(const char *fmt, ...)
va_start(argptr, fmt);
vsnprintf(msg + 7, sizeof(msg) - 7, fmt, argptr);
va_end(argptr);
Sys_DebugLog(va("%s/qconsole.log", com_gamedir), "%s", msg);
Sys_DebugLog(va("%s/qconsole.log", com_savedir), "%s", msg);
}
return;
}
Expand Down
2 changes: 1 addition & 1 deletion common/host.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ Host_WriteConfiguration(void)
// dedicated servers initialize the host but don't parse and set the
// config.cfg cvars
if (host_initialized & !isDedicated) {
f = fopen(va("%s/config.cfg", com_gamedir), "w");
f = fopen(va("%s/config.cfg", com_savedir), "w");
if (!f) {
Con_Printf("Couldn't write config.cfg.\n");
return;
Expand Down
4 changes: 2 additions & 2 deletions common/host_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ void Host_Savegame_f(void)
}
}

sprintf(name, "%s%c%s", com_gamedir, slash, Cmd_Argv(1));
sprintf(name, "%s%c%s", com_savedir, slash, Cmd_Argv(1));
COM_DefaultExtension(name, ".sav");

Con_Printf("Saving game to %s...\n", name);
Expand Down Expand Up @@ -572,7 +572,7 @@ void Host_Loadgame_f(void)

cls.demonum = -1; // stop demo loop in case this fails

sprintf(name, "%s%c%s", com_gamedir, slash, Cmd_Argv(1));
sprintf(name, "%s%c%s", com_savedir, slash, Cmd_Argv(1));
COM_DefaultExtension(name, ".sav");

// we can't call SCR_BeginLoadingPlaque, because too much stack space has
Expand Down
54 changes: 51 additions & 3 deletions common/libretro.c
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,18 @@ static void extract_directory(char *buf, const char *path, size_t size)

bool retro_load_game(const struct retro_game_info *info)
{

// Define slash character
// (Handle Windows nonsense...)
char slash;
#if defined(_WIN32)
slash = '\\';
#else
slash = '/';
#endif

unsigned i;
char g_rom_dir[1024], g_pak_path[1024];
char g_rom_dir[1024], g_pak_path[1024], g_save_dir[1024];
char cfg_file[1024];
char *path_lower;
quakeparms_t parms;
Expand All @@ -824,6 +834,42 @@ bool retro_load_game(const struct retro_game_info *info)
extract_directory(g_rom_dir, info->path, sizeof(g_rom_dir));

snprintf(g_pak_path, sizeof(g_pak_path), "%s", info->path);

// Get save directory...
bool use_external_savedir = false;
// > Get base path
const char *base_save_dir = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &base_save_dir) && base_save_dir)
{
if (strlen(base_save_dir) > 0)
{
// Get game 'name' (i.e. subdirectory)
char game_name[1024];
extract_basename(game_name, g_rom_dir, sizeof(game_name));

// > Build final save path
snprintf(g_save_dir, sizeof(g_save_dir), "%s%c%s", base_save_dir, slash, game_name);
use_external_savedir = true;

// > Create save directory, if required
if (!path_is_directory(g_save_dir))
{
use_external_savedir = path_mkdir(g_save_dir);
}
}
}
// > Error check
if (!use_external_savedir)
{
// > Use ROM directory fallback...
snprintf(g_save_dir, sizeof(g_save_dir), "%s", g_rom_dir);
}
else
{
// > Final check: is the save directory the same as the 'rom' directory?
// (i.e. ensure logical behaviour if user has set a bizarre save path...)
use_external_savedir = (strcmp(g_save_dir, g_rom_dir) != 0);
}

if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble))
log_cb(RETRO_LOG_INFO, "Rumble environment supported.\n");
Expand All @@ -847,6 +893,8 @@ bool retro_load_game(const struct retro_game_info *info)

parms.argc = 1;
parms.basedir = g_rom_dir;
parms.savedir = g_save_dir;
parms.use_exernal_savedir = use_external_savedir ? 1 : 0;
parms.memsize = MEMSIZE_MB * 1024 * 1024;
argv[0] = empty_string;

Expand Down Expand Up @@ -921,9 +969,9 @@ bool retro_load_game(const struct retro_game_info *info)

/* Override some default binds with more modern ones if we are booting the
* game for the first time. */
snprintf(cfg_file, sizeof(cfg_file), "%s/config.cfg", g_rom_dir);
snprintf(cfg_file, sizeof(cfg_file), "%s%cconfig.cfg", g_save_dir, slash);

if (path_is_valid(cfg_file))
if (!path_is_valid(cfg_file))
{
Cvar_Set("gamma", "0.95");
Cmd_ExecuteString("bind ' \"toggleconsole\"", src_command);
Expand Down
2 changes: 1 addition & 1 deletion common/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ static void M_ScanSaves(void)

strcpy(m_filenames[i], "--- UNUSED SLOT ---");
loadable[i] = false;
sprintf(name, "%s%cs%i.sav", com_gamedir, slash, i);
sprintf(name, "%s%cs%i.sav", com_savedir, slash, i);
f = fopen(name, "r");
if (!f)
continue;
Expand Down
2 changes: 2 additions & 0 deletions common/quakedef.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ typedef struct {

typedef struct {
const char *basedir;
const char *savedir;
unsigned short use_exernal_savedir; // should be a bool, but don't want to mess with the headers...
int argc;
const char **argv;
void *membase;
Expand Down