From 765394837c57afefd276bedcc41c27de3f0d39e5 Mon Sep 17 00:00:00 2001 From: Fabian Greffrath Date: Fri, 10 Jan 2020 09:23:21 +0100 Subject: [PATCH] Doomprefdir (#29) * Define a user-writeable directory with a little help from SDL_GetPrefPath() * Save config files, translation tables and savegames into this directory. * Screenshots are still saved into the "current" directory, i.e. the user's $HOME on Unix. * unconditionally create the user-writable directory * adapt to pending inclusion of m_misc2.c --- Source/d_main.c | 41 +++++++++++++++++++++++++++++++++++++++-- Source/d_main.h | 1 + Source/m_misc.c | 2 +- Source/r_data.c | 4 ++-- 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/Source/d_main.c b/Source/d_main.c index 1a5b2a5c..e654feab 100644 --- a/Source/d_main.c +++ b/Source/d_main.c @@ -30,6 +30,7 @@ //----------------------------------------------------------------------------- #include "d_io.h" // haleyjd +#include "SDL_filesystem.h" // [FG] SDL_GetPrefPath() #include #include @@ -47,6 +48,7 @@ #include "f_wipe.h" #include "m_argv.h" #include "m_misc.h" +#include "m_misc2.h" // [FG] M_StringDuplicate() #include "m_menu.h" #include "i_system.h" #include "i_sound.h" @@ -581,6 +583,41 @@ char *D_DoomExeName(void) return name; } +// [FG] get the path to the default configuration dir to use + +char *D_DoomPrefDir(void) +{ + static char *dir; + + if (dir == NULL) + { + char *result; + +#if !defined(_WIN32) || defined(_WIN32_WCE) + // Configuration settings are stored in an OS-appropriate path + // determined by SDL. On typical Unix systems, this might be + // ~/.local/share/chocolate-doom. On Windows, we behave like + // Vanilla Doom and save in the current directory. + + result = SDL_GetPrefPath("", PACKAGE_TARNAME); + if (result != NULL) + { + dir = M_StringDuplicate(result); + SDL_free(result); + } + else +#endif /* #ifndef _WIN32 */ + { + result = D_DoomExeDir(); + dir = M_StringDuplicate(result); + } + + M_MakeDirectory(dir); + } + + return dir; +} + // // CheckIWAD // @@ -865,11 +902,11 @@ void IdentifyVersion (void) // get config file from same directory as executable // killough 10/98 - sprintf(basedefault,"%s/%s.cfg", D_DoomExeDir(), D_DoomExeName()); + sprintf(basedefault,"%s/%s.cfg", D_DoomPrefDir(), D_DoomExeName()); // set save path to -save parm or current dir - strcpy(basesavegame,"."); //jff 3/27/98 default to current dir + strcpy(basesavegame,D_DoomPrefDir()); //jff 3/27/98 default to current dir if ((i=M_CheckParm("-save")) && i