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

Allow macOS and Linux to find files in the executable directory #493

Merged
merged 3 commits into from
Jun 14, 2024
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
51 changes: 44 additions & 7 deletions prboom2/src/SDL/i_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,15 @@ void I_SwitchToWindow(HWND hwnd)
}
}

const char *I_DoomExeDir(void)
const char *I_ConfigDir(void)
{
return I_ExeDir();
}

const char *I_ExeDir(void)
{
extern char **dsda_argv;

static const char current_dir_dummy[] = {"."}; // proff - rem extra slash 8/21/03
static char *base;
if (!base) // cache multiple requests
{
Expand All @@ -256,7 +260,7 @@ const char *I_DoomExeDir(void)
Z_Free(base);
base = (char*)Z_Malloc(1024);
if (!M_getcwd(base, 1024) || !M_WriteAccess(base))
strcpy(base, current_dir_dummy);
strcpy(base, ".");
}
}
return base;
Expand Down Expand Up @@ -284,7 +288,12 @@ const char* I_GetTempDir(void)

#elif defined(AMIGA)

const char *I_DoomExeDir(void)
const char *I_ConfigDir(void)
{
return "PROGDIR:";
}

const char *I_ExeDir(void)
{
return "PROGDIR:";
}
Expand All @@ -300,7 +309,7 @@ const char* I_GetTempDir(void)
// cph 2006/07/23 - give prboom+ its own dir
static const char prboom_dir[] = {"/.dsda-doom"}; // Mead rem extra slash 8/21/03

const char *I_DoomExeDir(void)
const char *I_ConfigDir(void)
{
static char *base;
if (!base) // cache multiple requests
Expand All @@ -318,6 +327,31 @@ const char *I_DoomExeDir(void)
return base;
}

const char *I_ExeDir(void)
{
extern char **dsda_argv;

static char *base;
if (!base) // cache multiple requests
{
size_t len = strlen(*dsda_argv);
char *p = (base = (char*)Z_Malloc(len+1)) + len - 1;
strcpy(base,*dsda_argv);
while (p > base && *p!='/' && *p!='\\')
*p--=0;
if (*p=='/' || *p=='\\')
*p--=0;
if (strlen(base) < 2 || !M_WriteAccess(base))
{
Z_Free(base);
base = (char*)Z_Malloc(1024);
if (!M_getcwd(base, 1024) || !M_WriteAccess(base))
strcpy(base, ".");
}
}
return base;
}

const char *I_GetTempDir(void)
{
return "/tmp";
Expand Down Expand Up @@ -376,9 +410,12 @@ char* I_FindFileInternal(const char* wfname, const char* ext, dboolean isStatic)
const char *dir; // directory
const char *sub; // subdirectory
const char *env; // environment variable
const char *(*func)(void); // for I_DoomExeDir
const char *(*func)(void); // for functions that return the directory
} search0[] = {
{NULL, NULL, NULL, I_DoomExeDir}, // config directory
{NULL, NULL, NULL, I_ExeDir}, // executable directory
#ifndef _WIN32
{NULL, NULL, NULL, I_ConfigDir}, // config and autoload directory. on windows, this is the same as I_ExeDir
#endif
{NULL}, // current working directory
{NULL, NULL, "DOOMWADDIR"}, // run-time $DOOMWADDIR
{DOOMWADDIR}, // build-time configured DOOMWADDIR
Expand Down
6 changes: 3 additions & 3 deletions prboom2/src/d_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1218,10 +1218,10 @@ static char *GetAutoloadDir(const char *iwadname, dboolean createdir)

if (autoload_path == NULL)
{
const char* exedir = I_DoomExeDir();
len = snprintf(NULL, 0, "%s/autoload", exedir);
const char* configdir = I_ConfigDir();
len = snprintf(NULL, 0, "%s/autoload", configdir);
autoload_path = Z_Malloc(len+1);
snprintf(autoload_path, len+1, "%s/autoload", exedir);
snprintf(autoload_path, len+1, "%s/autoload", configdir);
}

M_MakeDir(autoload_path, false);
Expand Down
2 changes: 1 addition & 1 deletion prboom2/src/dsda/data_organizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ char* dsda_DetectDirectory(const char* env_key, int arg_id) {
default_directory = M_getenv(env_key);

if (!default_directory)
default_directory = I_DoomExeDir();
default_directory = I_ConfigDir();

arg = dsda_Arg(arg_id);
if (arg->found) {
Expand Down
2 changes: 1 addition & 1 deletion prboom2/src/e6y.c
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ int GetFullPath(const char* FileName, const char* ext, char *Buffer, size_t Buff
strcpy(dir, M_getenv("DOOMWADDIR"));
break;
case 2:
strcpy(dir, I_DoomExeDir());
strcpy(dir, I_ConfigDir());
break;
}

Expand Down
3 changes: 2 additions & 1 deletion prboom2/src/i_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ void I_SwitchToWindow(HWND hwnd);
// e6y
const char* I_GetTempDir(void);

const char *I_DoomExeDir(void); // killough 2/16/98: path to executable's dir
const char *I_ExeDir(void); // killough 2/16/98: path to executable's dir
const char *I_ConfigDir(void); // path to config and autoload dir

dboolean HasTrailingSlash(const char* dn);
char* I_RequireFile(const char* wfname, const char* ext);
Expand Down
8 changes: 4 additions & 4 deletions prboom2/src/m_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -691,10 +691,10 @@ void M_LoadDefaults (void)
}
else
{
const char* exedir = I_DoomExeDir();
int len = snprintf(NULL, 0, "%s/dsda-doom.cfg", exedir);
const char* configdir = I_ConfigDir();
int len = snprintf(NULL, 0, "%s/dsda-doom.cfg", configdir);
defaultfile = Z_Malloc(len + 1);
snprintf(defaultfile, len + 1, "%s/dsda-doom.cfg", exedir);
snprintf(defaultfile, len + 1, "%s/dsda-doom.cfg", configdir);
}

lprintf(LO_DEBUG, " default file: %s\n", defaultfile);
Expand Down Expand Up @@ -889,7 +889,7 @@ void M_ScreenShot(void)
shot_dir = M_CheckWritableDir(dsda_StringConfig(dsda_config_screenshot_dir));
if (!shot_dir)
#ifdef _WIN32
shot_dir = M_CheckWritableDir(I_DoomExeDir());
shot_dir = M_CheckWritableDir(I_ConfigDir());
#else
shot_dir = (M_WriteAccess(SCREENSHOT_DIR) ? SCREENSHOT_DIR : NULL);
#endif
Expand Down
Loading