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.
This commit is contained in:
Tim Angus 2025-08-23 13:32:17 +01:00
parent 9293a45368
commit d133be28eb
4 changed files with 44 additions and 1 deletions

View File

@ -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

View File

@ -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()

View File

@ -29,6 +29,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/mman.h>
@ -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;
}
/*
==============================================================

View File

@ -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;
}
/*
==============================================================