From 7edb8f205339005d84932d4ec4b97ff8893940a2 Mon Sep 17 00:00:00 2001 From: jdgleaver Date: Sat, 21 Apr 2018 17:00:20 +0100 Subject: [PATCH] Fix mistaken function definition deletion in save-relocation branch... --- common/common.c | 15 +++++++++++++ common/common.h | 1 + common/console.c | 4 ++-- common/host.c | 2 +- common/host_cmd.c | 4 ++-- common/libretro.c | 54 ++++++++++++++++++++++++++++++++++++++++++++--- common/menu.c | 2 +- common/quakedef.h | 2 ++ 8 files changed, 75 insertions(+), 9 deletions(-) diff --git a/common/common.c b/common/common.c index 3e927fae..e68234f7 100644 --- a/common/common.c +++ b/common/common.c @@ -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]; @@ -1974,6 +1975,9 @@ static void COM_InitFilesystem(void) searchpath_t *search; #endif + // Set save directory + strcpy(com_savedir, host_parms.savedir); + // -basedir // Overrides the system supplied base directory (under id1) i = COM_CheckParm("-basedir"); @@ -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 [] ... // Fully specifies the exact search path, overriding the generated one diff --git a/common/common.h b/common/common.h index 30f99cd1..f210b207 100644 --- a/common/common.h +++ b/common/common.h @@ -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); diff --git a/common/console.c b/common/console.c index 59984585..f98706d1 100644 --- a/common/console.c +++ b/common/console.c @@ -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; @@ -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; } diff --git a/common/host.c b/common/host.c index c9387122..39a3ced3 100644 --- a/common/host.c +++ b/common/host.c @@ -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; diff --git a/common/host_cmd.c b/common/host_cmd.c index 45aaae10..1e6e0697 100644 --- a/common/host_cmd.c +++ b/common/host_cmd.c @@ -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); @@ -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 diff --git a/common/libretro.c b/common/libretro.c index b46ee9eb..ca6c2923 100644 --- a/common/libretro.c +++ b/common/libretro.c @@ -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; @@ -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"); @@ -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; @@ -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); diff --git a/common/menu.c b/common/menu.c index a608ca71..a7412efa 100644 --- a/common/menu.c +++ b/common/menu.c @@ -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; diff --git a/common/quakedef.h b/common/quakedef.h index 42dca4f3..ebb6ccf6 100644 --- a/common/quakedef.h +++ b/common/quakedef.h @@ -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;