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 1 commit
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
49 changes: 44 additions & 5 deletions prboom2/src/SDL/i_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,12 @@ void I_SwitchToWindow(HWND hwnd)
}
}

const char *I_DoomExeDir(void)
const char *I_DoomDir(void)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we rather call this I_ConfigDir?

Copy link
Collaborator Author

@Pedro-Beirao Pedro-Beirao May 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was originally going to name it that, but opted with DoomDir since that folder hosts more than just the config file.
Made a new commit that changes the name

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, maybe I_DataDir? It's maybe too generic, plus confusing since dsda-doom has a "data dir" as well that's one level below that. Any other ideas?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like I_ConfigDir.
Its pretty clear what it represents, so no one will be confused.

{
return I_ExeDir();
}

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

Expand Down Expand Up @@ -284,7 +289,12 @@ const char* I_GetTempDir(void)

#elif defined(AMIGA)

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

const char *I_ExeDir(void)
{
return "PROGDIR:";
}
Expand All @@ -300,7 +310,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_DoomDir(void)
{
static char *base;
if (!base) // cache multiple requests
Expand All @@ -318,6 +328,32 @@ const char *I_DoomExeDir(void)
return base;
}

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
{
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, current_dir_dummy);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just do strcpy(base, ".");? Does this dummy variable need to persist for some reason?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No there is no reason it needs to persist. I simply copied over the code from the Windows function, didnt even try to understand it.
Made a new commit.

}
}
return base;
}

const char *I_GetTempDir(void)
{
return "/tmp";
Expand Down Expand Up @@ -376,9 +412,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_DoomDir}, // 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* doomdir = I_DoomDir();
len = snprintf(NULL, 0, "%s/autoload", doomdir);
autoload_path = Z_Malloc(len+1);
snprintf(autoload_path, len+1, "%s/autoload", exedir);
snprintf(autoload_path, len+1, "%s/autoload", doomdir);
}

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_DoomDir();

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_DoomDir());
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_DoomDir(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* doomdir = I_DoomDir();
int len = snprintf(NULL, 0, "%s/dsda-doom.cfg", doomdir);
defaultfile = Z_Malloc(len + 1);
snprintf(defaultfile, len + 1, "%s/dsda-doom.cfg", exedir);
snprintf(defaultfile, len + 1, "%s/dsda-doom.cfg", doomdir);
}

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_DoomDir());
#else
shot_dir = (M_WriteAccess(SCREENSHOT_DIR) ? SCREENSHOT_DIR : NULL);
#endif
Expand Down
Loading