From d133be28ebce5e93f56e2f38e26d620c4779ce32 Mon Sep 17 00:00:00 2001 From: Tim Angus Date: Sat, 23 Aug 2025 13:32:17 +0100 Subject: [PATCH] Add DEFAULT_RELATIVE_BASEDIR Sys_BinaryPathRelative takes a parameter which is path concatenated with Sys_BinaryPath, and resolved to a canonical path. The intended use case is to facilitate the situation where you want the game data directory to exist outside the same directory in which the binary lives, but relative to it. More specifically, if you want to distriute multiple binaries for different architectures, in the same tree, this allows for a means of having said binaries in architecture subdirectories, with a shared data directory, e.g.: ioq3/x86/ioquake3.exe etc. ioq3/x86_64/ioquake3.exe etc. ioq3/baseq3/pak0.pk3 etc. Here, when building you would define DEFAULT_RELATIVE_BASEDIR=".." by appending it to the build system CFLAGS, and then the executables will by default look in their parent directory for the data. --- code/sys/sys_local.h | 3 +++ code/sys/sys_main.c | 4 +++- code/sys/sys_unix.c | 19 +++++++++++++++++++ code/sys/sys_win32.c | 19 +++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/code/sys/sys_local.h b/code/sys/sys_local.h index a0d6dc24..ce9f99b3 100644 --- a/code/sys/sys_local.h +++ b/code/sys/sys_local.h @@ -50,6 +50,9 @@ unsigned int CON_LogSize( void ); unsigned int CON_LogWrite( const char *in ); unsigned int CON_LogRead( char *out, unsigned int outSize ); +char *Sys_BinaryPath( void ); +char *Sys_BinaryPathRelative( const char *relative ); + #ifdef __APPLE__ char *Sys_StripAppBundle( char *pwd ); #endif diff --git a/code/sys/sys_main.c b/code/sys/sys_main.c index 44fb0981..6101fded 100644 --- a/code/sys/sys_main.c +++ b/code/sys/sys_main.c @@ -721,7 +721,9 @@ char *Sys_ParseProtocolUri( const char *uri ) #endif #ifndef DEFAULT_BASEDIR -# ifdef __APPLE__ +# if defined(DEFAULT_RELATIVE_BASEDIR) +# define DEFAULT_BASEDIR Sys_BinaryPathRelative(DEFAULT_RELATIVE_BASEDIR) +# elif defined(__APPLE__) # define DEFAULT_BASEDIR Sys_StripAppBundle(Sys_BinaryPath()) # else # define DEFAULT_BASEDIR Sys_BinaryPath() diff --git a/code/sys/sys_unix.c b/code/sys/sys_unix.c index 07de6500..53593600 100644 --- a/code/sys/sys_unix.c +++ b/code/sys/sys_unix.c @@ -29,6 +29,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include #include +#include #include #include #include @@ -352,6 +353,24 @@ char *Sys_Cwd( void ) return cwd; } +/* +================== +Sys_BinaryPathRelative +================== +*/ +char *Sys_BinaryPathRelative(const char *relative) +{ + static char resolved[MAX_OSPATH]; + char combined[MAX_OSPATH]; + + snprintf(combined, sizeof(combined), "%s/%s", Sys_BinaryPath(), relative); + + if (!realpath(combined, resolved)) + return NULL; + + return resolved; +} + /* ============================================================== diff --git a/code/sys/sys_win32.c b/code/sys/sys_win32.c index e740bcd6..d67c0716 100644 --- a/code/sys/sys_win32.c +++ b/code/sys/sys_win32.c @@ -424,6 +424,25 @@ char *Sys_Cwd( void ) { return cwd; } +/* +============== +Sys_BinaryPathRelative +============== +*/ +char *Sys_BinaryPathRelative(const char *relative) +{ + static char resolved[MAX_OSPATH]; + char combined[MAX_OSPATH]; + + snprintf(combined, sizeof(combined), "%s\\%s", Sys_BinaryPath(), relative); + + DWORD len = GetFullPathNameA(combined, MAX_OSPATH, resolved, NULL); + if (len == 0 || len >= MAX_OSPATH) + return NULL; + + return resolved; +} + /* ==============================================================