2001-02-19 21:15:25 +00:00
|
|
|
/*
|
|
|
|
quakefs.c
|
|
|
|
|
|
|
|
virtual filesystem functions
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2001-03-09 07:58:10 +00:00
|
|
|
# include "config.h"
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2001-03-09 07:58:10 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
2001-03-09 07:58:10 +00:00
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
2003-05-23 17:17:01 +00:00
|
|
|
#ifdef HAVE_IO_H
|
|
|
|
# include <io.h>
|
2003-07-09 04:21:45 +00:00
|
|
|
#endif
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2011-08-25 13:35:20 +00:00
|
|
|
#if defined(_WIN32) && defined(HAVE_MALLOC_H)
|
2007-03-22 23:20:57 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
|
|
|
|
2001-03-09 07:58:10 +00:00
|
|
|
#include <ctype.h>
|
2001-02-19 21:15:25 +00:00
|
|
|
#include <dirent.h>
|
2002-08-25 23:10:57 +00:00
|
|
|
#include <errno.h>
|
2001-08-22 22:03:16 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdarg.h>
|
2002-08-25 23:10:57 +00:00
|
|
|
#include <stdlib.h>
|
2007-03-19 10:43:29 +00:00
|
|
|
#include <sys/stat.h>
|
2001-03-09 07:58:10 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_FNMATCH_H
|
2001-03-09 07:58:10 +00:00
|
|
|
# define model_t sunmodel_t
|
|
|
|
# include <fnmatch.h>
|
|
|
|
# undef model_t
|
2001-09-29 09:39:27 +00:00
|
|
|
#else
|
2004-01-20 03:47:27 +00:00
|
|
|
# ifdef _WIN32
|
2001-09-29 09:39:27 +00:00
|
|
|
# include "fnmatch.h"
|
|
|
|
# endif
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
|
|
|
|
2001-10-16 21:40:45 +00:00
|
|
|
#ifdef HAVE_IO_H
|
2001-03-09 07:58:10 +00:00
|
|
|
# include <io.h>
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2001-03-09 07:58:10 +00:00
|
|
|
# define _POSIX_
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
|
|
|
|
2001-03-09 07:58:10 +00:00
|
|
|
#include <limits.h>
|
|
|
|
|
2013-01-22 05:09:41 +00:00
|
|
|
#include "qfalloca.h"
|
2013-01-20 04:41:30 +00:00
|
|
|
#include "QF/alloc.h"
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/cmd.h"
|
|
|
|
#include "QF/cvar.h"
|
2003-01-10 22:47:18 +00:00
|
|
|
#include "QF/dstring.h"
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/hash.h"
|
2011-12-24 01:04:33 +00:00
|
|
|
#include "QF/mathlib.h"
|
2002-03-12 23:45:36 +00:00
|
|
|
#include "QF/pak.h"
|
2002-07-21 06:11:28 +00:00
|
|
|
#include "QF/pakfile.h"
|
2021-03-21 07:13:03 +00:00
|
|
|
#include "QF/plist.h"
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/qargs.h"
|
|
|
|
#include "QF/qendian.h"
|
|
|
|
#include "QF/qtypes.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
#include "QF/quakefs.h"
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/va.h"
|
2001-03-30 00:30:38 +00:00
|
|
|
#include "QF/zone.h"
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
#include "compat.h"
|
|
|
|
|
2001-03-09 07:58:10 +00:00
|
|
|
#ifndef HAVE_FNMATCH_PROTO
|
2002-08-25 23:10:57 +00:00
|
|
|
int fnmatch (const char *__pattern, const char *__string, int __flags);
|
2001-03-09 07:58:10 +00:00
|
|
|
#endif
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
/*
|
|
|
|
All of Quake's data access is through a hierchical file system, but the
|
|
|
|
contents of the file system can be transparently merged from several
|
|
|
|
sources.
|
|
|
|
|
|
|
|
The "user directory" is the path to the directory holding the quake.exe
|
2001-03-09 07:58:10 +00:00
|
|
|
and all game directories. This can be overridden with the "fs_sharepath"
|
|
|
|
and "fs_userpath" cvars to allow code debugging in a different directory.
|
2010-01-13 06:42:26 +00:00
|
|
|
The base directory is used only during filesystem initialization.
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
The "game directory" is the first tree on the search path and directory
|
|
|
|
that all generated files (savegames, screenshots, demos, config files)
|
|
|
|
will be saved to. This can be overridden with the "-game" command line
|
|
|
|
parameter. The game directory can never be changed while quake is
|
|
|
|
executing. This is a precacution against having a malicious server
|
|
|
|
instruct clients to write files over areas they shouldn't.
|
|
|
|
|
2010-01-13 06:42:26 +00:00
|
|
|
The "cache directory" is used only during development to save network
|
2001-02-19 21:15:25 +00:00
|
|
|
bandwidth, especially over ISDN / T1 lines. If there is a cache directory
|
|
|
|
specified, when a file is found by the normal search path, it will be
|
|
|
|
mirrored into the cache directory, then opened there.
|
|
|
|
*/
|
|
|
|
|
2002-08-25 23:10:57 +00:00
|
|
|
// QUAKE FILESYSTEM
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2021-12-13 00:13:39 +00:00
|
|
|
static memhunk_t *qfs_hunk;
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
static char *fs_userpath;
|
|
|
|
static cvar_t fs_userpath_cvar = {
|
|
|
|
.name = "fs_userpath",
|
|
|
|
.description =
|
|
|
|
"location of your game directories",
|
|
|
|
.default_value = FS_USERPATH,
|
|
|
|
.flags = CVAR_ROM,
|
|
|
|
.value = { .type = 0, .value = &fs_userpath },
|
|
|
|
};
|
|
|
|
static char *fs_sharepath;
|
|
|
|
static cvar_t fs_sharepath_cvar = {
|
|
|
|
.name = "fs_sharepath",
|
|
|
|
.description =
|
|
|
|
"location of shared (read-only) game directories",
|
|
|
|
.default_value = FS_SHAREPATH,
|
|
|
|
.flags = CVAR_ROM,
|
|
|
|
.value = { .type = 0, .value = &fs_sharepath },
|
|
|
|
};
|
|
|
|
static char *fs_dirconf;
|
|
|
|
static cvar_t fs_dirconf_cvar = {
|
|
|
|
.name = "fs_dirconf",
|
|
|
|
.description =
|
|
|
|
"full path to gamedir.conf FIXME",
|
|
|
|
.default_value = "",
|
|
|
|
.flags = CVAR_ROM,
|
|
|
|
.value = { .type = 0, .value = &fs_dirconf },
|
|
|
|
};
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE const char *qfs_userpath;
|
2003-05-23 17:17:01 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE int qfs_filesize;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2010-08-24 07:47:39 +00:00
|
|
|
typedef struct searchpath_s {
|
|
|
|
char *filename;
|
|
|
|
struct pack_s *pack; // only one of filename / pack will be used
|
|
|
|
struct searchpath_s *next;
|
|
|
|
} searchpath_t;
|
|
|
|
|
2013-01-20 04:41:30 +00:00
|
|
|
/** Represent a single game directory.
|
|
|
|
|
|
|
|
A vpath is the union of all locations searched for a single gamedir. This
|
|
|
|
includes all pak files in the gamedir in the user's directory, the
|
|
|
|
filesystem in the user's gamedir, then all pak files in the share
|
|
|
|
game directory, and finally share's gamedir filesystem.
|
|
|
|
|
|
|
|
The purpose is to enable searches to be limited to a single gamedir or to
|
|
|
|
not search past a certain gamedir.
|
|
|
|
*/
|
2013-01-20 09:09:02 +00:00
|
|
|
struct vpath_s { // typedef to vpath_t is in quakefs.h
|
2013-01-20 04:41:30 +00:00
|
|
|
char *name; // usually the gamedir name
|
|
|
|
searchpath_t *user;
|
|
|
|
searchpath_t *share;
|
|
|
|
struct vpath_s *next;
|
2013-01-20 09:09:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct int_findfile_s {
|
|
|
|
findfile_t ff;
|
|
|
|
struct pack_s *pack;
|
|
|
|
dpackfile_t *packfile;
|
|
|
|
const char *path;
|
|
|
|
int fname_index;
|
|
|
|
} int_findfile_t;
|
2013-01-20 04:41:30 +00:00
|
|
|
|
2013-11-24 03:28:13 +00:00
|
|
|
static searchpath_t *searchpaths_freelist;
|
|
|
|
static vpath_t *vpaths_freelist;
|
2013-01-20 04:41:30 +00:00
|
|
|
static vpath_t *qfs_vpaths;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2003-01-10 22:47:18 +00:00
|
|
|
//QFS
|
|
|
|
|
2003-01-27 18:16:15 +00:00
|
|
|
typedef struct qfs_var_s {
|
|
|
|
char *var;
|
|
|
|
char *val;
|
|
|
|
} qfs_var_t;
|
|
|
|
|
2013-01-20 04:41:30 +00:00
|
|
|
static void qfs_add_gamedir (vpath_t *vpath, const char *dir);
|
2003-01-10 22:47:18 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE gamedir_t *qfs_gamedir;
|
2003-01-10 22:47:18 +00:00
|
|
|
static plitem_t *qfs_gd_plist;
|
|
|
|
static const char *qfs_game = "";
|
|
|
|
static const char *qfs_default_dirconf =
|
|
|
|
"{"
|
2003-02-07 20:18:59 +00:00
|
|
|
" QF = {"
|
|
|
|
" Path = \"QF\";"
|
|
|
|
" };"
|
2003-01-10 22:47:18 +00:00
|
|
|
" Quake = {"
|
2003-02-07 20:18:59 +00:00
|
|
|
" Inherit = QF;"
|
2003-01-10 22:47:18 +00:00
|
|
|
" Path = \"id1\";"
|
2003-01-13 17:32:00 +00:00
|
|
|
" GameCode = \"progs.dat\";"
|
2011-09-10 23:04:47 +00:00
|
|
|
" HudType = \"id\";"
|
2003-01-10 22:47:18 +00:00
|
|
|
" };"
|
|
|
|
" QuakeWorld = {"
|
|
|
|
" Inherit = (Quake);"
|
|
|
|
" Path = \"qw\";"
|
2003-01-27 21:56:30 +00:00
|
|
|
" SkinPath = \"${path}/skins\";"
|
2003-01-13 17:32:00 +00:00
|
|
|
" GameCode = \"qwprogs.dat\";"
|
2011-09-10 23:04:47 +00:00
|
|
|
" HudType = \"id\";"
|
|
|
|
" };"
|
|
|
|
" \"Hipnotic\" = {"
|
|
|
|
" Inherit = (Quake);"
|
|
|
|
" Path = \"hipnotic\";"
|
|
|
|
" HudType = \"hipnotic\";"
|
|
|
|
" };"
|
|
|
|
" \"Rogue\" = {"
|
|
|
|
" Inherit = (Quake);"
|
|
|
|
" Path = \"rogue\";"
|
|
|
|
" HudType = \"rogue\";"
|
2003-01-10 22:47:18 +00:00
|
|
|
" };"
|
2003-03-13 05:43:45 +00:00
|
|
|
" \"qw:qw\" = {"
|
|
|
|
" Inherit = (QuakeWorld);"
|
|
|
|
" };"
|
2003-01-10 22:47:18 +00:00
|
|
|
" \"qw:*\" = {"
|
|
|
|
" Inherit = (QuakeWorld);"
|
|
|
|
" Path = \"$gamedir\";"
|
|
|
|
" };"
|
|
|
|
" \"nq:*\" = {"
|
|
|
|
" Inherit = (Quake);"
|
2003-01-13 16:46:13 +00:00
|
|
|
" Path = \"$gamedir\";"
|
2003-01-10 22:47:18 +00:00
|
|
|
" };"
|
2005-02-14 09:08:32 +00:00
|
|
|
" \"hipnotic:*\" = {"
|
2011-09-10 23:04:47 +00:00
|
|
|
" Inherit = (Hipnotic);"
|
2005-02-14 09:08:32 +00:00
|
|
|
" Path = \"$gamedir\";"
|
|
|
|
" };"
|
|
|
|
" \"rogue:*\" = {"
|
2011-09-10 23:04:47 +00:00
|
|
|
" Inherit = (Rogue);"
|
2005-02-14 09:08:32 +00:00
|
|
|
" Path = \"$gamedir\";"
|
|
|
|
" };"
|
|
|
|
" \"abyss\" = {"
|
|
|
|
" Inherit = (Quake);"
|
|
|
|
" Path = \"abyss\";"
|
|
|
|
" };"
|
|
|
|
" \"abyss:*\" = {"
|
|
|
|
" Inherit = (abyss);"
|
|
|
|
" Path = \"$gamedir\";"
|
|
|
|
" };"
|
2003-01-10 22:47:18 +00:00
|
|
|
"}";
|
|
|
|
|
2004-02-03 03:01:06 +00:00
|
|
|
|
2010-08-24 07:33:22 +00:00
|
|
|
#define GAMEDIR_CALLBACK_CHUNK 16
|
|
|
|
static gamedir_callback_t **gamedir_callbacks;
|
2004-02-03 03:01:06 +00:00
|
|
|
static int num_gamedir_callbacks;
|
2010-08-24 07:33:22 +00:00
|
|
|
static int max_gamedir_callbacks;
|
2004-02-03 03:01:06 +00:00
|
|
|
|
2013-01-20 04:41:30 +00:00
|
|
|
static searchpath_t *
|
|
|
|
new_searchpath (void)
|
|
|
|
{
|
|
|
|
searchpath_t *searchpath;
|
|
|
|
ALLOC (16, searchpath_t, searchpaths, searchpath);
|
|
|
|
return searchpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
delete_searchpath (searchpath_t *searchpath)
|
|
|
|
{
|
|
|
|
if (searchpath->pack) {
|
2021-03-21 10:56:17 +00:00
|
|
|
pack_del (searchpath->pack);
|
2013-01-20 04:41:30 +00:00
|
|
|
}
|
2021-03-21 10:56:17 +00:00
|
|
|
if (searchpath->filename) {
|
2013-01-20 04:41:30 +00:00
|
|
|
free (searchpath->filename);
|
2021-03-21 10:56:17 +00:00
|
|
|
}
|
2013-01-20 04:41:30 +00:00
|
|
|
FREE (searchpaths, searchpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
static vpath_t *
|
|
|
|
new_vpath (void)
|
|
|
|
{
|
|
|
|
vpath_t *vpath;
|
|
|
|
ALLOC (16, vpath_t, vpaths, vpath);
|
|
|
|
return vpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
delete_vpath (vpath_t *vpath)
|
|
|
|
{
|
|
|
|
searchpath_t *next;
|
|
|
|
|
2021-03-21 10:56:17 +00:00
|
|
|
if (vpath->name) {
|
2013-01-20 04:41:30 +00:00
|
|
|
free (vpath->name);
|
2021-03-21 10:56:17 +00:00
|
|
|
}
|
2013-01-20 04:41:30 +00:00
|
|
|
while (vpath->user) {
|
|
|
|
next = vpath->user->next;
|
|
|
|
delete_searchpath (vpath->user);
|
|
|
|
vpath->user = next;
|
|
|
|
}
|
|
|
|
while (vpath->share) {
|
|
|
|
next = vpath->share->next;
|
|
|
|
delete_searchpath (vpath->share);
|
|
|
|
vpath->share = next;
|
|
|
|
}
|
|
|
|
FREE (vpaths, vpath);
|
|
|
|
}
|
|
|
|
|
2003-01-27 18:16:15 +00:00
|
|
|
static const char *
|
2012-07-18 13:34:37 +00:00
|
|
|
qfs_var_get_key (const void *_v, void *unused)
|
2003-01-27 18:16:15 +00:00
|
|
|
{
|
|
|
|
return ((qfs_var_t *)_v)->var;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
qfs_var_free (void *_v, void *unused)
|
|
|
|
{
|
|
|
|
qfs_var_t *v = (qfs_var_t *) _v;
|
|
|
|
free (v->var);
|
|
|
|
free (v->val);
|
|
|
|
free (v);
|
|
|
|
}
|
|
|
|
|
2003-01-27 21:56:30 +00:00
|
|
|
static hashtab_t *
|
2003-01-27 18:16:15 +00:00
|
|
|
qfs_new_vars (void)
|
|
|
|
{
|
2020-03-25 06:43:16 +00:00
|
|
|
return Hash_NewTable (61, qfs_var_get_key, qfs_var_free, 0, 0);
|
2003-01-27 18:16:15 +00:00
|
|
|
}
|
|
|
|
|
2003-01-27 21:56:30 +00:00
|
|
|
static void
|
2003-01-27 18:16:15 +00:00
|
|
|
qfs_set_var (hashtab_t *vars, const char *var, const char *val)
|
|
|
|
{
|
|
|
|
qfs_var_t *v = Hash_Find (vars, var);
|
|
|
|
|
|
|
|
if (!v) {
|
|
|
|
v = malloc (sizeof (qfs_var_t));
|
|
|
|
v->var = strdup (var);
|
|
|
|
v->val = 0;
|
|
|
|
Hash_Add (vars, v);
|
|
|
|
}
|
|
|
|
if (v->val)
|
|
|
|
free (v->val);
|
|
|
|
v->val = strdup (val);
|
|
|
|
}
|
|
|
|
|
2003-01-27 21:56:30 +00:00
|
|
|
static inline int
|
|
|
|
qfs_isident (byte c)
|
|
|
|
{
|
|
|
|
return ((c >= 'a' && c <='z') || (c >= 'A' && c <='Z')
|
|
|
|
|| (c >= '0' && c <= '9') || c == '_');
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2003-01-27 18:16:15 +00:00
|
|
|
qfs_var_subst (const char *string, hashtab_t *vars)
|
|
|
|
{
|
|
|
|
dstring_t *new = dstring_newstr ();
|
|
|
|
const char *s = string;
|
|
|
|
const char *e = s;
|
|
|
|
const char *var;
|
|
|
|
qfs_var_t *sub;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
while (*e && *e != '$')
|
|
|
|
e++;
|
|
|
|
dstring_appendsubstr (new, s, (e - s));
|
|
|
|
if (!*e++)
|
|
|
|
break;
|
|
|
|
if (*e == '$') {
|
|
|
|
dstring_appendstr (new, "$");
|
|
|
|
s = ++e;
|
|
|
|
} else if (*e == '{') {
|
|
|
|
s = e;
|
|
|
|
while (*e && *e != '}')
|
|
|
|
e++;
|
|
|
|
if (!*e) {
|
|
|
|
dstring_appendsubstr (new, s, (e - s));
|
|
|
|
break;
|
|
|
|
}
|
2021-01-31 07:01:20 +00:00
|
|
|
var = va (0, "%.*s", (int) (e - s) - 1, s + 1);
|
2003-01-27 18:16:15 +00:00
|
|
|
sub = Hash_Find (vars, var);
|
|
|
|
if (sub)
|
|
|
|
dstring_appendstr (new, sub->val);
|
|
|
|
else
|
|
|
|
dstring_appendsubstr (new, s - 1, (e - s) + 2);
|
|
|
|
s = ++e;
|
2003-01-27 21:56:30 +00:00
|
|
|
} else if (qfs_isident (*e)) {
|
2003-01-27 18:16:15 +00:00
|
|
|
s = e;
|
2003-01-27 21:56:30 +00:00
|
|
|
while (qfs_isident (*e))
|
2003-01-27 18:16:15 +00:00
|
|
|
e++;
|
2021-01-31 07:01:20 +00:00
|
|
|
var = va (0, "%.*s", (int) (e - s), s);
|
2003-01-27 18:16:15 +00:00
|
|
|
sub = Hash_Find (vars, var);
|
|
|
|
if (sub)
|
|
|
|
dstring_appendstr (new, sub->val);
|
|
|
|
else
|
|
|
|
dstring_appendsubstr (new, s - 1, (e - s) + 1);
|
|
|
|
s = e;
|
|
|
|
} else {
|
|
|
|
dstring_appendstr (new, "$");
|
|
|
|
s = e;
|
|
|
|
}
|
|
|
|
}
|
2011-09-11 04:09:24 +00:00
|
|
|
return dstring_freeze (new);
|
2003-01-27 18:16:15 +00:00
|
|
|
}
|
|
|
|
|
2003-01-10 22:47:18 +00:00
|
|
|
static void
|
2003-01-27 21:56:30 +00:00
|
|
|
qfs_get_gd_params (plitem_t *gdpl, gamedir_t *gamedir, dstring_t *path,
|
|
|
|
hashtab_t *vars)
|
2003-01-10 22:47:18 +00:00
|
|
|
{
|
|
|
|
plitem_t *p;
|
2010-08-20 03:58:37 +00:00
|
|
|
const char *ps;
|
2003-01-10 22:47:18 +00:00
|
|
|
|
2010-08-20 03:58:37 +00:00
|
|
|
if ((p = PL_ObjectForKey (gdpl, "Path")) && *(ps = PL_String (p))) {
|
|
|
|
char *str = qfs_var_subst (ps, vars);
|
2003-01-27 21:56:30 +00:00
|
|
|
|
2011-09-11 04:10:14 +00:00
|
|
|
qfs_set_var (vars, "path", str);
|
2003-01-10 22:47:18 +00:00
|
|
|
if (path->str[0])
|
|
|
|
dstring_appendstr (path, ":");
|
2003-01-27 21:56:30 +00:00
|
|
|
dstring_appendstr (path, str);
|
|
|
|
free (str);
|
2003-01-10 22:47:18 +00:00
|
|
|
}
|
|
|
|
if (!gamedir->gamecode && (p = PL_ObjectForKey (gdpl, "GameCode")))
|
2010-08-20 03:58:37 +00:00
|
|
|
gamedir->gamecode = qfs_var_subst (PL_String (p), vars);
|
2011-09-10 23:04:47 +00:00
|
|
|
if (!gamedir->hudtype && (p = PL_ObjectForKey (gdpl, "HudType")))
|
|
|
|
gamedir->hudtype = qfs_var_subst (PL_String (p), vars);
|
2003-02-14 22:36:10 +00:00
|
|
|
if (!gamedir->dir.skins && (p = PL_ObjectForKey (gdpl, "SkinPath")))
|
2010-08-20 03:58:37 +00:00
|
|
|
gamedir->dir.skins = qfs_var_subst (PL_String (p), vars);
|
2010-08-24 13:42:00 +00:00
|
|
|
if (!gamedir->dir.models && (p = PL_ObjectForKey (gdpl, "ModelPath")))
|
|
|
|
gamedir->dir.models = qfs_var_subst (PL_String (p), vars);
|
2003-02-14 22:36:10 +00:00
|
|
|
if (!gamedir->dir.sound && (p = PL_ObjectForKey (gdpl, "SoundPath")))
|
2010-08-20 03:58:37 +00:00
|
|
|
gamedir->dir.sound = qfs_var_subst (PL_String (p), vars);
|
2003-02-14 22:36:10 +00:00
|
|
|
if (!gamedir->dir.maps && (p = PL_ObjectForKey (gdpl, "MapPath")))
|
2010-08-20 03:58:37 +00:00
|
|
|
gamedir->dir.maps = qfs_var_subst (PL_String (p), vars);
|
2012-01-05 08:04:23 +00:00
|
|
|
if (!gamedir->dir.shots && (p = PL_ObjectForKey (gdpl, "ShotsPath")))
|
|
|
|
gamedir->dir.shots = qfs_var_subst (PL_String (p), vars);
|
2003-01-10 22:47:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
qfs_inherit (plitem_t *plist, plitem_t *gdpl, gamedir_t *gamedir,
|
2003-01-27 21:56:30 +00:00
|
|
|
dstring_t *path, hashtab_t *dirs, hashtab_t *vars)
|
2003-01-10 22:47:18 +00:00
|
|
|
{
|
2010-08-20 03:58:37 +00:00
|
|
|
plitem_t *base_item;
|
2003-01-10 22:47:18 +00:00
|
|
|
|
2010-08-20 03:58:37 +00:00
|
|
|
if (!(base_item = PL_ObjectForKey (gdpl, "Inherit")))
|
2003-01-10 22:47:18 +00:00
|
|
|
return;
|
2010-08-20 03:58:37 +00:00
|
|
|
switch (PL_Type (base_item)) {
|
|
|
|
case QFString:
|
|
|
|
{
|
|
|
|
const char *base = PL_String (base_item);
|
|
|
|
if (Hash_Find (dirs, base))
|
|
|
|
return;
|
|
|
|
gdpl = PL_ObjectForKey (plist, base);
|
|
|
|
if (!gdpl) {
|
|
|
|
Sys_Printf ("base `%s' not found\n", base);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
qfs_set_var (vars, "gamedir", base);
|
|
|
|
Hash_Add (dirs, strdup (base));
|
|
|
|
qfs_get_gd_params (gdpl, gamedir, path, vars);
|
|
|
|
qfs_inherit (plist, gdpl, gamedir, path, dirs, vars);
|
2003-01-10 22:47:18 +00:00
|
|
|
}
|
2010-08-20 03:58:37 +00:00
|
|
|
break;
|
|
|
|
case QFArray:
|
|
|
|
{
|
|
|
|
int i, num_dirs;
|
|
|
|
plitem_t *basedir_item;
|
|
|
|
const char *basedir;
|
|
|
|
|
|
|
|
num_dirs = PL_A_NumObjects (base_item);
|
|
|
|
for (i = 0; i < num_dirs; i++) {
|
|
|
|
basedir_item = PL_ObjectAtIndex (base_item, i);
|
|
|
|
if (!basedir_item)
|
|
|
|
continue;
|
|
|
|
basedir = PL_String (basedir_item);
|
|
|
|
if (!basedir || Hash_Find (dirs, basedir))
|
|
|
|
continue;
|
|
|
|
gdpl = PL_ObjectForKey (plist, basedir);
|
|
|
|
if (!gdpl) {
|
|
|
|
Sys_Printf ("base `%s' not found\n", basedir);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
qfs_set_var (vars, "gamedir", basedir);
|
|
|
|
Hash_Add (dirs, strdup (basedir));
|
|
|
|
qfs_get_gd_params (gdpl, gamedir, path, vars);
|
|
|
|
qfs_inherit (plist, gdpl, gamedir, path, dirs, vars);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2003-01-10 22:47:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qfs_compare (const void *a, const void *b)
|
|
|
|
{
|
2010-08-20 02:57:47 +00:00
|
|
|
return strcmp (*(const char **) a, *(const char **) b);
|
2003-01-10 22:47:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
2012-07-18 13:34:37 +00:00
|
|
|
qfs_dir_get_key (const void *_k, void *unused)
|
2003-01-10 22:47:18 +00:00
|
|
|
{
|
|
|
|
return _k;
|
|
|
|
}
|
|
|
|
|
2004-01-07 06:40:10 +00:00
|
|
|
static void
|
|
|
|
qfs_dir_free (void *_k, void *unused)
|
|
|
|
{
|
|
|
|
free (_k);
|
|
|
|
}
|
|
|
|
|
2003-01-22 20:00:46 +00:00
|
|
|
static plitem_t *
|
|
|
|
qfs_find_gamedir (const char *name, hashtab_t *dirs)
|
2003-01-10 22:47:18 +00:00
|
|
|
{
|
2003-01-22 20:00:46 +00:00
|
|
|
plitem_t *gdpl = PL_ObjectForKey (qfs_gd_plist, name);
|
2003-01-10 22:47:18 +00:00
|
|
|
|
|
|
|
if (!gdpl) {
|
2010-08-20 02:57:47 +00:00
|
|
|
plitem_t *keys = PL_D_AllKeys (qfs_gd_plist);
|
|
|
|
int num_keys = PL_A_NumObjects (keys);
|
|
|
|
const char **list = malloc (num_keys * sizeof (char *));
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < num_keys; i++)
|
|
|
|
list[i] = PL_String (PL_ObjectAtIndex (keys, i));
|
|
|
|
qsort (list, num_keys, sizeof (const char *), qfs_compare);
|
|
|
|
|
|
|
|
while (i--) {
|
|
|
|
if (!fnmatch (list[i], name, 0)) {
|
|
|
|
gdpl = PL_ObjectForKey (qfs_gd_plist, list[i]);
|
|
|
|
Hash_Add (dirs, strdup (list[i]));
|
2003-01-10 22:47:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free (list);
|
2010-08-20 02:57:47 +00:00
|
|
|
PL_Free (keys);
|
2003-01-10 22:47:18 +00:00
|
|
|
}
|
2003-01-22 20:00:46 +00:00
|
|
|
return gdpl;
|
|
|
|
}
|
|
|
|
|
2003-01-27 21:56:30 +00:00
|
|
|
static void
|
|
|
|
qfs_process_path (const char *path, const char *gamedir)
|
2003-01-22 20:00:46 +00:00
|
|
|
{
|
2003-01-27 21:56:30 +00:00
|
|
|
const char *e = path + strlen (path);
|
|
|
|
const char *s = e;
|
2013-01-20 04:41:30 +00:00
|
|
|
char *dir;
|
|
|
|
vpath_t *vpath;
|
2003-01-27 21:56:30 +00:00
|
|
|
|
|
|
|
while (s >= path) {
|
|
|
|
while (s != path && s[-1] !=':')
|
|
|
|
s--;
|
|
|
|
if (s != e) {
|
2013-01-20 04:41:30 +00:00
|
|
|
dir = nva ("%.*s", (int) (e - s), s);
|
|
|
|
vpath = new_vpath ();
|
|
|
|
vpath->name = dir;
|
|
|
|
qfs_add_gamedir (vpath, dir);
|
|
|
|
vpath->next = qfs_vpaths;
|
|
|
|
qfs_vpaths = vpath;
|
2003-01-27 21:56:30 +00:00
|
|
|
}
|
|
|
|
e = --s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
qfs_build_gamedir (const char **list)
|
|
|
|
{
|
|
|
|
int j;
|
2003-01-22 20:00:46 +00:00
|
|
|
gamedir_t *gamedir;
|
|
|
|
plitem_t *gdpl;
|
|
|
|
dstring_t *path;
|
2020-03-25 06:43:16 +00:00
|
|
|
hashtab_t *dirs = Hash_NewTable (31, qfs_dir_get_key, qfs_dir_free, 0, 0);
|
2003-01-27 21:56:30 +00:00
|
|
|
hashtab_t *vars = qfs_new_vars ();
|
|
|
|
const char *dir = 0;
|
2003-01-22 20:00:46 +00:00
|
|
|
|
2003-01-27 21:56:30 +00:00
|
|
|
qfs_set_var (vars, "game", qfs_game);
|
|
|
|
|
|
|
|
if (qfs_gamedir) {
|
|
|
|
if (qfs_gamedir->name)
|
|
|
|
free ((char *)qfs_gamedir->name);
|
2003-02-14 22:36:10 +00:00
|
|
|
if (qfs_gamedir->gamedir)
|
|
|
|
free ((char *)qfs_gamedir->gamedir);
|
2003-01-27 21:56:30 +00:00
|
|
|
if (qfs_gamedir->path)
|
|
|
|
free ((char *)qfs_gamedir->path);
|
|
|
|
if (qfs_gamedir->gamecode)
|
|
|
|
free ((char *)qfs_gamedir->gamecode);
|
2003-02-14 22:36:10 +00:00
|
|
|
if (qfs_gamedir->dir.def)
|
|
|
|
free ((char *)qfs_gamedir->dir.def);
|
|
|
|
if (qfs_gamedir->dir.skins)
|
|
|
|
free ((char *)qfs_gamedir->dir.skins);
|
2010-08-24 13:42:00 +00:00
|
|
|
if (qfs_gamedir->dir.models)
|
|
|
|
free ((char *)qfs_gamedir->dir.models);
|
2003-02-14 22:36:10 +00:00
|
|
|
if (qfs_gamedir->dir.sound)
|
|
|
|
free ((char *)qfs_gamedir->dir.sound);
|
|
|
|
if (qfs_gamedir->dir.maps)
|
|
|
|
free ((char *)qfs_gamedir->dir.maps);
|
2003-01-27 21:56:30 +00:00
|
|
|
free (qfs_gamedir);
|
2003-01-22 20:00:46 +00:00
|
|
|
}
|
|
|
|
|
2013-01-20 04:41:30 +00:00
|
|
|
while (qfs_vpaths) {
|
|
|
|
vpath_t *next = qfs_vpaths->next;
|
|
|
|
delete_vpath (qfs_vpaths);
|
|
|
|
qfs_vpaths = next;
|
2003-01-27 21:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; list[j]; j++)
|
|
|
|
;
|
2003-01-10 22:47:18 +00:00
|
|
|
gamedir = calloc (1, sizeof (gamedir_t));
|
|
|
|
path = dstring_newstr ();
|
2003-01-27 21:56:30 +00:00
|
|
|
while (j--) {
|
2021-01-31 07:01:20 +00:00
|
|
|
const char *name = va (0, "%s:%s", qfs_game, dir = list[j]);
|
2003-01-27 21:56:30 +00:00
|
|
|
if (Hash_Find (dirs, name))
|
|
|
|
continue;
|
|
|
|
gdpl = qfs_find_gamedir (name, dirs);
|
|
|
|
if (!gdpl) {
|
|
|
|
Sys_Printf ("gamedir `%s' not found\n", name);
|
|
|
|
continue;
|
|
|
|
}
|
2004-01-07 06:40:10 +00:00
|
|
|
Hash_Add (dirs, strdup (name));
|
2003-02-14 22:36:10 +00:00
|
|
|
if (!j) {
|
2003-01-27 21:56:30 +00:00
|
|
|
gamedir->name = strdup (name);
|
2003-02-14 22:36:10 +00:00
|
|
|
gamedir->gamedir = strdup (list[j]);
|
|
|
|
}
|
2003-01-27 21:56:30 +00:00
|
|
|
qfs_set_var (vars, "gamedir", dir);
|
|
|
|
qfs_get_gd_params (gdpl, gamedir, path, vars);
|
|
|
|
qfs_inherit (qfs_gd_plist, gdpl, gamedir, path, dirs, vars);
|
|
|
|
}
|
2003-01-10 22:47:18 +00:00
|
|
|
gamedir->path = path->str;
|
2003-02-14 22:36:10 +00:00
|
|
|
|
|
|
|
for (dir = gamedir->path; *dir && *dir != ':'; dir++)
|
|
|
|
;
|
|
|
|
gamedir->dir.def = nva ("%.*s", (int) (dir - gamedir->path),
|
|
|
|
gamedir->path);
|
2012-01-05 05:00:57 +00:00
|
|
|
if (!gamedir->hudtype)
|
|
|
|
gamedir->hudtype = strdup ("id");
|
2003-02-14 22:36:10 +00:00
|
|
|
if (!gamedir->dir.skins)
|
|
|
|
gamedir->dir.skins = nva ("%s/skins", gamedir->dir.def);
|
2010-08-24 13:42:00 +00:00
|
|
|
if (!gamedir->dir.models)
|
|
|
|
gamedir->dir.models = nva ("%s/progs", gamedir->dir.def);
|
2003-02-14 22:36:10 +00:00
|
|
|
if (!gamedir->dir.sound)
|
|
|
|
gamedir->dir.sound = nva ("%s/sound", gamedir->dir.def);
|
|
|
|
if (!gamedir->dir.maps)
|
|
|
|
gamedir->dir.maps = nva ("%s/maps", gamedir->dir.def);
|
2012-01-05 08:04:23 +00:00
|
|
|
if (!gamedir->dir.shots)
|
|
|
|
gamedir->dir.shots = strdup ("QF");
|
2003-02-14 22:36:10 +00:00
|
|
|
|
2003-01-27 21:56:30 +00:00
|
|
|
qfs_gamedir = gamedir;
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_fs, "%s\n", qfs_gamedir->name);
|
|
|
|
Sys_MaskPrintf (SYS_fs, " gamedir : %s\n", qfs_gamedir->gamedir);
|
|
|
|
Sys_MaskPrintf (SYS_fs, " path : %s\n", qfs_gamedir->path);
|
|
|
|
Sys_MaskPrintf (SYS_fs, " gamecode: %s\n", qfs_gamedir->gamecode);
|
|
|
|
Sys_MaskPrintf (SYS_fs, " hudtype : %s\n", qfs_gamedir->hudtype);
|
|
|
|
Sys_MaskPrintf (SYS_fs, " def : %s\n", qfs_gamedir->dir.def);
|
|
|
|
Sys_MaskPrintf (SYS_fs, " skins : %s\n", qfs_gamedir->dir.skins);
|
|
|
|
Sys_MaskPrintf (SYS_fs, " models : %s\n", qfs_gamedir->dir.models);
|
|
|
|
Sys_MaskPrintf (SYS_fs, " sound : %s\n", qfs_gamedir->dir.sound);
|
|
|
|
Sys_MaskPrintf (SYS_fs, " maps : %s\n", qfs_gamedir->dir.maps);
|
2003-01-27 21:56:30 +00:00
|
|
|
qfs_process_path (qfs_gamedir->path, dir);
|
2003-01-10 22:47:18 +00:00
|
|
|
free (path);
|
|
|
|
Hash_DelTable (dirs);
|
2003-01-27 21:56:30 +00:00
|
|
|
Hash_DelTable (vars);
|
2003-01-10 22:47:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
qfs_load_config (void)
|
|
|
|
{
|
2007-04-07 12:17:01 +00:00
|
|
|
QFile *f = 0;
|
2003-01-10 22:47:18 +00:00
|
|
|
int len;
|
|
|
|
char *buf;
|
2004-01-06 07:33:19 +00:00
|
|
|
char *dirconf;
|
2003-01-10 22:47:18 +00:00
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
if (*fs_dirconf) {
|
|
|
|
dirconf = Sys_ExpandSquiggle (fs_dirconf);
|
2007-04-07 12:17:01 +00:00
|
|
|
if (!(f = Qopen (dirconf, "rt")))
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_fs,
|
2010-11-23 05:09:30 +00:00
|
|
|
"Could not load `%s', using builtin defaults\n",
|
|
|
|
dirconf);
|
2007-04-07 12:17:01 +00:00
|
|
|
free (dirconf);
|
|
|
|
}
|
2004-01-06 07:33:19 +00:00
|
|
|
if (!f)
|
2003-01-10 22:47:18 +00:00
|
|
|
goto no_config;
|
2004-01-06 07:33:19 +00:00
|
|
|
|
2003-01-10 22:47:18 +00:00
|
|
|
len = Qfilesize (f);
|
|
|
|
buf = malloc (len + 3); // +3 for { } and \0
|
|
|
|
|
|
|
|
Qread (f, buf + 1, len);
|
|
|
|
Qclose (f);
|
|
|
|
|
|
|
|
// convert the config file to a plist dictionary
|
|
|
|
buf[0] = '{';
|
|
|
|
buf[len + 1] = '}';
|
|
|
|
buf[len + 2] = 0;
|
2005-05-05 22:50:09 +00:00
|
|
|
if (qfs_gd_plist)
|
|
|
|
PL_Free (qfs_gd_plist);
|
2021-02-09 00:57:07 +00:00
|
|
|
qfs_gd_plist = PL_GetPropertyList (buf, 0);
|
2003-01-10 22:47:18 +00:00
|
|
|
free (buf);
|
2010-08-20 03:58:37 +00:00
|
|
|
if (qfs_gd_plist && PL_Type (qfs_gd_plist) == QFDictionary)
|
2003-01-10 22:47:18 +00:00
|
|
|
return; // done
|
|
|
|
Sys_Printf ("not a dictionary\n");
|
|
|
|
no_config:
|
2005-05-05 22:50:09 +00:00
|
|
|
if (qfs_gd_plist)
|
|
|
|
PL_Free (qfs_gd_plist);
|
2021-02-09 00:57:07 +00:00
|
|
|
qfs_gd_plist = PL_GetPropertyList (qfs_default_dirconf, 0);
|
2003-01-10 22:47:18 +00:00
|
|
|
}
|
|
|
|
|
2010-08-24 23:26:31 +00:00
|
|
|
/*
|
2010-08-25 00:57:44 +00:00
|
|
|
qfs_contains_updir
|
2010-08-24 23:26:31 +00:00
|
|
|
|
|
|
|
Checks if a string contains an updir ('..'), either at the ends or
|
|
|
|
surrounded by slashes ('/'). Doesn't check for leading slash.
|
|
|
|
Assumes canonical (compressed) path.
|
|
|
|
*/
|
|
|
|
static inline int
|
2010-08-25 00:57:44 +00:00
|
|
|
qfs_contains_updir (const char *path, int levels)
|
2010-08-24 23:26:31 +00:00
|
|
|
{
|
|
|
|
do {
|
|
|
|
if (path[0] != '.' || path[1] != '.'
|
|
|
|
|| (path[2] != '/' && path[2] != 0))
|
|
|
|
return 0;
|
|
|
|
if (!path[2])
|
|
|
|
break;
|
|
|
|
// first part of path is ../
|
|
|
|
if (levels <= 0)
|
|
|
|
return 1;
|
|
|
|
path += 3;
|
|
|
|
} while (levels-- > 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-10-16 05:17:09 +00:00
|
|
|
qfs_expand_path (dstring_t *full_path, const char *base, const char *path,
|
|
|
|
int levels)
|
2010-08-24 23:26:31 +00:00
|
|
|
{
|
|
|
|
const char *separator = "/";
|
|
|
|
char *cpath;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (!base || !*base) {
|
|
|
|
errno = EACCES;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cpath = QFS_CompressPath (path);
|
2010-10-16 05:17:09 +00:00
|
|
|
if (qfs_contains_updir (cpath, levels)) {
|
2010-08-24 23:26:31 +00:00
|
|
|
free (cpath);
|
|
|
|
errno = EACCES;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (*cpath == '/')
|
|
|
|
separator = "";
|
|
|
|
len = strlen (base);
|
2011-09-09 08:30:07 +00:00
|
|
|
if (len && base[len - 1] == '/')
|
2010-08-24 23:26:31 +00:00
|
|
|
len--;
|
|
|
|
dsprintf (full_path, "%.*s%s%s", len, base, separator, cpath);
|
|
|
|
free (cpath);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qfs_expand_userpath (dstring_t *full_path, const char *path)
|
|
|
|
{
|
2010-10-16 05:17:09 +00:00
|
|
|
return qfs_expand_path (full_path, qfs_userpath, path, 0);
|
2010-08-24 23:26:31 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE char *
|
2003-07-08 22:39:45 +00:00
|
|
|
QFS_FileBase (const char *in)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2011-09-09 08:39:00 +00:00
|
|
|
const char *base;
|
|
|
|
const char *ext;
|
2011-09-09 10:55:54 +00:00
|
|
|
int len;
|
2003-07-08 22:39:45 +00:00
|
|
|
char *out;
|
2001-03-09 07:58:10 +00:00
|
|
|
|
2011-09-09 08:39:00 +00:00
|
|
|
base = QFS_SkipPath (in);
|
|
|
|
ext = QFS_FileExtension (base);
|
|
|
|
len = ext - base;
|
|
|
|
out = malloc (len + 1);
|
2011-09-09 10:55:54 +00:00
|
|
|
strncpy (out, base, len);
|
2011-09-09 08:39:00 +00:00
|
|
|
out [len] = 0;
|
2003-07-08 22:39:45 +00:00
|
|
|
return out;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2013-01-20 04:41:30 +00:00
|
|
|
static void
|
|
|
|
qfs_path_print (searchpath_t *sp)
|
|
|
|
{
|
|
|
|
if (sp->pack) {
|
|
|
|
Sys_Printf ("%s (%i files)\n", sp->pack->filename, sp->pack->numfiles);
|
|
|
|
} else {
|
|
|
|
Sys_Printf ("%s\n", sp->filename);
|
|
|
|
}
|
|
|
|
}
|
2001-08-22 22:03:16 +00:00
|
|
|
|
2003-02-14 19:46:07 +00:00
|
|
|
static void
|
2010-08-25 00:57:44 +00:00
|
|
|
qfs_path_f (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2013-01-20 04:41:30 +00:00
|
|
|
vpath_t *vp;
|
|
|
|
searchpath_t *sp;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("Current search path:\n");
|
2013-01-20 04:41:30 +00:00
|
|
|
for (vp = qfs_vpaths; vp; vp = vp->next) {
|
2013-01-20 05:35:19 +00:00
|
|
|
Sys_Printf ("%s\n", vp->name);
|
2013-01-20 04:41:30 +00:00
|
|
|
for (sp = vp->user; sp; sp = sp->next) {
|
|
|
|
qfs_path_print (sp);
|
|
|
|
}
|
|
|
|
for (sp = vp->share; sp; sp = sp->next) {
|
|
|
|
qfs_path_print (sp);
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2004-01-19 07:47:41 +00:00
|
|
|
QFS_WriteFile (const char *filename, const void *data, int len)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *f;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2003-05-23 17:17:01 +00:00
|
|
|
f = QFS_WOpen (filename, 0);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (!f) {
|
2003-02-14 22:36:10 +00:00
|
|
|
Sys_Error ("Error opening %s", filename);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Qwrite (f, data, len);
|
|
|
|
Qclose (f);
|
|
|
|
}
|
|
|
|
|
2013-01-20 09:09:02 +00:00
|
|
|
static int_findfile_t *
|
|
|
|
qfs_findfile_search (const vpath_t *vpath, const searchpath_t *sp,
|
|
|
|
const char **fnames)
|
|
|
|
{
|
|
|
|
static int_findfile_t found;
|
|
|
|
const char **fn;
|
|
|
|
|
|
|
|
found.ff.vpath = 0;
|
|
|
|
found.ff.in_pak = false;
|
|
|
|
found.pack = 0;
|
|
|
|
found.packfile = 0;
|
|
|
|
found.fname_index = 0;
|
|
|
|
if (found.ff.realname) {
|
|
|
|
free ((char *) found.ff.realname);
|
|
|
|
found.ff.realname = 0;
|
|
|
|
}
|
|
|
|
if (found.path) {
|
|
|
|
free ((char *) found.path);
|
|
|
|
found.path = 0;
|
|
|
|
}
|
|
|
|
// is the element a pak file?
|
|
|
|
if (sp->pack) {
|
2013-01-20 12:04:16 +00:00
|
|
|
dpackfile_t *packfile = 0;
|
2013-01-20 09:09:02 +00:00
|
|
|
|
|
|
|
for (fn = fnames; *fn; fn++) {
|
|
|
|
packfile = pack_find_file (sp->pack, *fn);
|
|
|
|
if (packfile) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (packfile) {
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_fs_f, "PackFile: %s : %s\n",
|
2013-01-20 09:09:02 +00:00
|
|
|
sp->pack->filename, packfile->name);
|
|
|
|
found.ff.vpath = vpath;
|
|
|
|
found.ff.in_pak = true;
|
|
|
|
found.ff.realname = strdup (*fn);
|
|
|
|
found.pack = sp->pack;
|
|
|
|
found.packfile = packfile;
|
|
|
|
found.fname_index = fn - fnames;
|
|
|
|
found.path = strdup (sp->pack->filename);
|
|
|
|
return &found;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// check a file in the directory tree
|
|
|
|
dstring_t *path = dstring_new ();
|
|
|
|
|
|
|
|
for (fn = fnames; *fn; fn++) {
|
|
|
|
if (qfs_expand_path (path, sp->filename, *fn, 1) == 0) {
|
2013-11-24 03:28:13 +00:00
|
|
|
if (Sys_FileExists (path->str) == -1) {
|
2014-01-29 07:13:24 +00:00
|
|
|
continue;
|
2013-01-20 09:09:02 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_fs_f, "FindFile: %s\n", path->str);
|
2013-01-20 09:09:02 +00:00
|
|
|
|
|
|
|
found.ff.vpath = vpath;
|
|
|
|
found.ff.in_pak = false;
|
|
|
|
found.ff.realname = strdup (*fn);
|
|
|
|
found.path = strdup (path->str);
|
|
|
|
found.fname_index = fn - fnames;
|
2021-03-21 10:56:17 +00:00
|
|
|
dstring_delete (path);
|
2013-01-20 09:09:02 +00:00
|
|
|
return &found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dstring_delete (path);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int_findfile_t *
|
|
|
|
qfs_findfile (const char **fnames, const vpath_t *start, const vpath_t *end)
|
|
|
|
{
|
|
|
|
const vpath_t *vp;
|
|
|
|
searchpath_t *sp;
|
|
|
|
int_findfile_t *found;
|
|
|
|
|
|
|
|
if (!start) {
|
|
|
|
start = qfs_vpaths;
|
|
|
|
}
|
|
|
|
if (end) {
|
|
|
|
for (vp = start; vp; vp = vp->next) {
|
|
|
|
if (vp == end) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!vp) {
|
|
|
|
Sys_Error ("QFS_FindFile: end vpath not in search list");
|
|
|
|
}
|
|
|
|
end = end->next;
|
|
|
|
}
|
|
|
|
for (vp = start; vp && vp != end; vp = vp->next) {
|
|
|
|
for (sp = vp->user; sp; sp = sp->next) {
|
|
|
|
found = qfs_findfile_search (vp, sp, fnames);
|
|
|
|
if (found) {
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (sp = vp->share; sp; sp = sp->next) {
|
|
|
|
found = qfs_findfile_search (vp, sp, fnames);
|
|
|
|
if (found) {
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// file not found
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE findfile_t *
|
|
|
|
QFS_FindFile (const char *fname, const vpath_t *start, const vpath_t *end)
|
|
|
|
{
|
|
|
|
int_findfile_t *found;
|
|
|
|
const char *fname_list[2];
|
|
|
|
|
|
|
|
fname_list[0] = fname;
|
|
|
|
fname_list[1] = 0;
|
|
|
|
|
|
|
|
found = qfs_findfile (fname_list, start, end);
|
|
|
|
if (found) {
|
|
|
|
return &found->ff;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static QFile *
|
2010-08-25 00:57:44 +00:00
|
|
|
qfs_openread (const char *path, int offs, int len, int zip)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2002-09-27 04:27:19 +00:00
|
|
|
QFile *file;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2002-09-27 04:27:19 +00:00
|
|
|
if (offs < 0 || len < 0)
|
|
|
|
file = Qopen (path, zip ? "rbz" : "rb");
|
|
|
|
else
|
|
|
|
file = Qsubopen (path, offs, len, zip);
|
|
|
|
|
|
|
|
if (!file) {
|
2001-02-19 21:15:25 +00:00
|
|
|
Sys_Error ("Couldn't open %s", path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-02-14 19:46:07 +00:00
|
|
|
qfs_filesize = Qfilesize (file);
|
2002-09-27 04:27:19 +00:00
|
|
|
|
|
|
|
return file;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE char *
|
2003-02-14 19:46:07 +00:00
|
|
|
QFS_CompressPath (const char *pth)
|
2002-08-20 06:11:10 +00:00
|
|
|
{
|
|
|
|
char *p, *d;
|
2002-08-25 23:10:57 +00:00
|
|
|
char *path= malloc (strlen (pth) + 1);
|
2002-08-20 06:11:10 +00:00
|
|
|
|
|
|
|
for (d = path; *pth; d++, pth++) {
|
|
|
|
if (*pth == '\\')
|
|
|
|
*d = '/';
|
|
|
|
else
|
|
|
|
*d = *pth;
|
|
|
|
}
|
|
|
|
*d = 0;
|
|
|
|
|
|
|
|
p = path;
|
|
|
|
while (*p) {
|
|
|
|
if (p[0] == '.') {
|
|
|
|
if (p[1] == '.') {
|
|
|
|
if (p[2] == '/' || p[2] == 0) {
|
|
|
|
d = p;
|
|
|
|
if (d > path)
|
|
|
|
d--;
|
|
|
|
while (d > path && d[-1] != '/')
|
|
|
|
d--;
|
|
|
|
if (d == path
|
|
|
|
&& d[0] == '.' && d[1] == '.'
|
2010-08-25 03:04:58 +00:00
|
|
|
&& (d[2] == '/' || d[2] == '\0')) {
|
|
|
|
p += 2 + (p[2] == '/');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (d[0] == '/'
|
|
|
|
&& d[1] == '.' && d[2] == '.'
|
|
|
|
&& (d[3] == '/' || d[3] == '\0')) {
|
|
|
|
*p = 0;
|
2002-08-20 06:11:10 +00:00
|
|
|
p += 2 + (p[2] == '/');
|
|
|
|
continue;
|
|
|
|
}
|
2012-01-24 10:54:21 +00:00
|
|
|
p = p + 2 + (p[2] == '/');
|
|
|
|
memmove (d, p, strlen (p) + 1);
|
2002-08-20 06:11:10 +00:00
|
|
|
p = d;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (p[1] == '/') {
|
2012-01-24 10:54:21 +00:00
|
|
|
memmove (p, p + 2, strlen (p + 2) + 1);
|
2002-08-20 06:11:10 +00:00
|
|
|
continue;
|
|
|
|
} else if (p[1] == 0) {
|
|
|
|
p[0] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (*p && *p != '/')
|
|
|
|
p++;
|
2007-03-21 13:05:23 +00:00
|
|
|
if (*p == '/') {
|
2002-08-20 06:11:10 +00:00
|
|
|
p++;
|
2010-08-25 03:04:58 +00:00
|
|
|
// skip over multiple / (foo//bar -> foo/bar)
|
2007-03-21 13:05:23 +00:00
|
|
|
for (d = p; *d == '/'; d++)
|
|
|
|
;
|
|
|
|
if (d != p)
|
2012-01-24 10:54:21 +00:00
|
|
|
memmove (p, d, strlen (d) + 1);
|
2007-03-21 13:05:23 +00:00
|
|
|
}
|
2002-08-20 06:11:10 +00:00
|
|
|
}
|
2010-08-25 03:04:58 +00:00
|
|
|
// strip any trailing /, but not if it's the root /
|
2012-01-24 10:54:21 +00:00
|
|
|
if (--p > path && *p == '/')
|
2010-08-25 03:04:58 +00:00
|
|
|
*p = 0;
|
2002-08-20 06:11:10 +00:00
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2013-11-24 06:18:42 +00:00
|
|
|
VISIBLE findfile_t qfs_foundfile;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
/*
|
2003-02-14 19:46:07 +00:00
|
|
|
QFS_FOpenFile
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
Finds the file in the search path.
|
2003-02-14 19:46:07 +00:00
|
|
|
Sets qfs_filesize and one of handle or file
|
2001-02-19 21:15:25 +00:00
|
|
|
*/
|
2002-06-14 20:36:28 +00:00
|
|
|
|
2013-01-20 09:12:09 +00:00
|
|
|
static void
|
2013-11-24 13:45:42 +00:00
|
|
|
open_file (int_findfile_t *found, QFile **gzfile, int zip)
|
2002-06-14 20:36:28 +00:00
|
|
|
{
|
2013-11-24 06:18:42 +00:00
|
|
|
qfs_foundfile = found->ff;
|
2013-01-20 09:12:09 +00:00
|
|
|
if (found->ff.in_pak) {
|
|
|
|
*gzfile = qfs_openread (found->pack->filename,
|
|
|
|
found->packfile->filepos,
|
|
|
|
found->packfile->filelen, zip);
|
2002-06-14 20:36:28 +00:00
|
|
|
} else {
|
2013-01-20 09:12:09 +00:00
|
|
|
*gzfile = qfs_openread (found->path, -1, -1, zip);
|
2002-06-14 20:36:28 +00:00
|
|
|
}
|
2013-01-20 04:41:30 +00:00
|
|
|
}
|
|
|
|
|
2014-01-23 02:57:57 +00:00
|
|
|
VISIBLE QFile *
|
2014-01-23 04:08:44 +00:00
|
|
|
_QFS_VOpenFile (const char *filename, int zip,
|
|
|
|
const vpath_t *start, const vpath_t *end)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2014-01-23 02:57:57 +00:00
|
|
|
QFile *gzfile;
|
2013-01-20 09:12:09 +00:00
|
|
|
int_findfile_t *found;
|
2002-08-20 06:11:10 +00:00
|
|
|
char *path;
|
2013-01-20 09:12:09 +00:00
|
|
|
const char *fnames[4];
|
|
|
|
int zip_flags[3];
|
|
|
|
int ind;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2002-08-20 06:11:10 +00:00
|
|
|
// make sure they're not trying to do weird stuff with our private files
|
2003-02-14 19:46:07 +00:00
|
|
|
path = QFS_CompressPath (filename);
|
2010-08-25 00:57:44 +00:00
|
|
|
if (qfs_contains_updir(path, 1)) {
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_fs,
|
2010-11-23 05:09:30 +00:00
|
|
|
"FindFile: %s: attempt to escape directory tree!\n",
|
|
|
|
path);
|
2002-08-20 06:11:10 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2013-01-20 09:12:09 +00:00
|
|
|
ind = 0;
|
2002-06-14 20:36:28 +00:00
|
|
|
#ifdef HAVE_VORBIS
|
2003-02-14 19:46:07 +00:00
|
|
|
if (strequal (".wav", QFS_FileExtension (path))) {
|
2021-03-27 11:06:40 +00:00
|
|
|
size_t len = strlen (path);
|
|
|
|
char *oggfilename = alloca (len + 1);
|
2003-02-14 19:46:07 +00:00
|
|
|
QFS_StripExtension (path, oggfilename);
|
2021-03-27 11:06:40 +00:00
|
|
|
strcat (oggfilename, ".ogg");
|
2013-01-20 09:12:09 +00:00
|
|
|
fnames[ind] = oggfilename;
|
|
|
|
zip_flags[ind] = 0;
|
|
|
|
ind++;
|
2002-06-14 20:36:28 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ZLIB
|
2013-01-20 12:09:16 +00:00
|
|
|
{
|
|
|
|
char *gzfilename;
|
|
|
|
gzfilename = alloca (strlen (path) + 3 + 1);
|
|
|
|
sprintf (gzfilename, "%s.gz", path);
|
|
|
|
fnames[ind] = gzfilename;
|
|
|
|
zip_flags[ind] = zip;
|
|
|
|
ind++;
|
|
|
|
}
|
2002-06-14 20:36:28 +00:00
|
|
|
#endif
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2013-01-20 09:12:09 +00:00
|
|
|
fnames[ind] = path;
|
|
|
|
zip_flags[ind] = zip;
|
|
|
|
ind++;
|
|
|
|
|
|
|
|
fnames[ind] = 0;
|
|
|
|
|
2014-01-23 04:08:44 +00:00
|
|
|
found = qfs_findfile (fnames, start, end);
|
2013-01-20 09:12:09 +00:00
|
|
|
|
|
|
|
if (found) {
|
2014-01-23 02:57:57 +00:00
|
|
|
open_file (found, &gzfile, zip_flags[found->fname_index]);
|
2013-01-20 09:12:09 +00:00
|
|
|
free(path);
|
2014-01-23 02:57:57 +00:00
|
|
|
return gzfile;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_fs_nf, "FindFile: can't find %s\n", filename);
|
2001-08-21 02:44:45 +00:00
|
|
|
error:
|
2003-02-14 19:46:07 +00:00
|
|
|
qfs_filesize = -1;
|
2002-08-20 06:11:10 +00:00
|
|
|
free (path);
|
2014-01-23 02:57:57 +00:00
|
|
|
return 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2014-01-23 04:08:44 +00:00
|
|
|
VISIBLE QFile *
|
|
|
|
QFS_VOpenFile (const char *filename, const vpath_t *start, const vpath_t *end)
|
|
|
|
{
|
|
|
|
return _QFS_VOpenFile (filename, 1, start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE QFile *
|
|
|
|
_QFS_FOpenFile (const char *filename, int zip)
|
|
|
|
{
|
|
|
|
return _QFS_VOpenFile (filename, zip, 0, 0);
|
|
|
|
}
|
|
|
|
|
2014-01-23 02:57:57 +00:00
|
|
|
VISIBLE QFile *
|
|
|
|
QFS_FOpenFile (const char *filename)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2014-01-23 04:08:44 +00:00
|
|
|
return _QFS_VOpenFile (filename, 1, 0, 0);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2014-01-23 02:57:57 +00:00
|
|
|
static cache_user_t *loadcache;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
/*
|
2003-02-14 19:46:07 +00:00
|
|
|
QFS_LoadFile
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
Filename are relative to the quake directory.
|
2002-06-18 21:41:24 +00:00
|
|
|
Always appends a 0 byte to the loaded data.
|
2001-02-19 21:15:25 +00:00
|
|
|
*/
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE byte *
|
2014-01-23 02:57:57 +00:00
|
|
|
QFS_LoadFile (QFile *file, int usehunk)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-03-09 07:58:10 +00:00
|
|
|
byte *buf = NULL;
|
2003-07-08 22:39:45 +00:00
|
|
|
char *base;
|
2001-02-26 06:48:02 +00:00
|
|
|
int len;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-03-09 07:58:10 +00:00
|
|
|
// look for it in the filesystem or pack files
|
2014-01-23 02:57:57 +00:00
|
|
|
if (!file)
|
2001-02-19 21:15:25 +00:00
|
|
|
return NULL;
|
|
|
|
|
2014-01-23 02:57:57 +00:00
|
|
|
base = strdup ("QFS_LoadFile");
|
|
|
|
|
|
|
|
len = qfs_filesize = Qfilesize (file);
|
|
|
|
|
2001-03-09 07:58:10 +00:00
|
|
|
// extract the filename base name for hunk tag
|
2014-01-23 02:57:57 +00:00
|
|
|
//base = QFS_FileBase (path);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (usehunk == 1)
|
2021-12-13 00:15:51 +00:00
|
|
|
buf = Hunk_RawAllocName (qfs_hunk, len + 1, base);
|
2001-02-19 21:15:25 +00:00
|
|
|
else if (usehunk == 2)
|
2021-12-13 00:15:51 +00:00
|
|
|
buf = Hunk_TempAlloc (qfs_hunk, len + 1);
|
2001-02-19 21:15:25 +00:00
|
|
|
else if (usehunk == 0)
|
2021-12-13 00:15:51 +00:00
|
|
|
buf = malloc (len + 1);
|
2001-02-19 21:15:25 +00:00
|
|
|
else if (usehunk == 3)
|
2001-02-26 06:48:02 +00:00
|
|
|
buf = Cache_Alloc (loadcache, len + 1, base);
|
2014-01-23 00:01:55 +00:00
|
|
|
else
|
2003-02-14 19:46:07 +00:00
|
|
|
Sys_Error ("QFS_LoadFile: bad usehunk");
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (!buf)
|
2014-01-23 02:57:57 +00:00
|
|
|
Sys_Error ("QFS_LoadFile: not enough space");
|
|
|
|
//Sys_Error ("QFS_LoadFile: not enough space for %s", path);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2021-12-13 00:15:51 +00:00
|
|
|
len = Qread (file, buf, len);
|
2001-11-18 23:40:01 +00:00
|
|
|
buf[len] = 0;
|
2014-01-23 02:57:57 +00:00
|
|
|
Qclose (file);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2003-07-08 22:39:45 +00:00
|
|
|
free (base);
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE byte *
|
2014-01-23 02:57:57 +00:00
|
|
|
QFS_LoadHunkFile (QFile *file)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2014-01-23 02:57:57 +00:00
|
|
|
return QFS_LoadFile (file, 1);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2014-01-23 02:57:57 +00:00
|
|
|
QFS_LoadCacheFile (QFile *file, struct cache_user_s *cu)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
loadcache = cu;
|
2014-01-23 02:57:57 +00:00
|
|
|
QFS_LoadFile (file, 3);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-08-25 00:57:44 +00:00
|
|
|
qfs_load_pakfile
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
Takes an explicit (not game tree related) path to a pak file.
|
|
|
|
|
|
|
|
Loads the header and directory, adding the files at the beginning
|
|
|
|
of the list so they override previous pack files.
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static pack_t *
|
2010-08-25 00:57:44 +00:00
|
|
|
qfs_load_pakfile (char *packfile)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2002-07-21 06:11:28 +00:00
|
|
|
pack_t *pack = pack_open (packfile);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2002-07-21 06:11:28 +00:00
|
|
|
if (pack)
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_fs, "Added packfile %s (%i files)\n",
|
2002-07-21 06:11:28 +00:00
|
|
|
packfile, pack->numfiles);
|
2001-02-19 21:15:25 +00:00
|
|
|
return pack;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define FBLOCK_SIZE 32
|
|
|
|
|
|
|
|
// Note, this is /NOT/ a work-alike strcmp, this groups numbers sanely.
|
2002-10-30 02:43:23 +00:00
|
|
|
static int
|
2010-08-25 00:57:44 +00:00
|
|
|
qfs_file_sort (char **os1, char **os2)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int in1, in2, n1, n2;
|
|
|
|
char *s1, *s2;
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
s1 = *os1;
|
|
|
|
s2 = *os2;
|
|
|
|
|
|
|
|
while (1) {
|
2018-09-08 13:23:57 +00:00
|
|
|
n1 = n2 = 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2002-08-02 03:22:32 +00:00
|
|
|
if ((in1 = isdigit ((byte) *s1)))
|
2001-02-26 06:48:02 +00:00
|
|
|
n1 = strtol (s1, &s1, 10);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2002-08-02 03:22:32 +00:00
|
|
|
if ((in2 = isdigit ((byte) *s2)))
|
2001-02-26 06:48:02 +00:00
|
|
|
n2 = strtol (s2, &s2, 10);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (in1 && in2) {
|
|
|
|
if (n1 != n2)
|
2001-02-26 06:48:02 +00:00
|
|
|
return n1 - n2;
|
2001-02-19 21:15:25 +00:00
|
|
|
} else {
|
|
|
|
if (*s1 != *s2)
|
|
|
|
return *s1 - *s2;
|
|
|
|
else if (*s1 == '\0')
|
|
|
|
return *s1 - *s2;
|
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2013-01-20 04:41:30 +00:00
|
|
|
qfs_load_gamedir (searchpath_t **searchpath, const char *dir)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2013-01-20 04:41:30 +00:00
|
|
|
searchpath_t *sp;
|
2001-02-26 06:48:02 +00:00
|
|
|
pack_t *pak;
|
|
|
|
DIR *dir_ptr;
|
|
|
|
struct dirent *dirent;
|
|
|
|
char **pakfiles = NULL;
|
|
|
|
int i = 0, bufsize = 0, count = 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_fs, "qfs_load_gamedir (\"%s\")\n", dir);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
pakfiles = calloc (1, FBLOCK_SIZE * sizeof (char *));
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
bufsize += FBLOCK_SIZE;
|
|
|
|
if (!pakfiles)
|
2010-08-25 00:57:44 +00:00
|
|
|
goto qfs_load_gamedir_free;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-12-05 16:29:09 +00:00
|
|
|
for (i = 0; i < bufsize; i++) {
|
2001-02-19 21:15:25 +00:00
|
|
|
pakfiles[i] = NULL;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
dir_ptr = opendir (dir);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (!dir_ptr)
|
2010-08-25 00:57:44 +00:00
|
|
|
goto qfs_load_gamedir_free;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
while ((dirent = readdir (dir_ptr))) {
|
|
|
|
if (!fnmatch ("*.pak", dirent->d_name, 0)) {
|
2001-02-19 21:15:25 +00:00
|
|
|
if (count >= bufsize) {
|
|
|
|
bufsize += FBLOCK_SIZE;
|
2001-02-26 06:48:02 +00:00
|
|
|
pakfiles = realloc (pakfiles, bufsize * sizeof (char *));
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
if (!pakfiles)
|
2010-08-25 00:57:44 +00:00
|
|
|
goto qfs_load_gamedir_free;
|
2001-02-19 21:15:25 +00:00
|
|
|
for (i = count; i < bufsize; i++)
|
|
|
|
pakfiles[i] = NULL;
|
|
|
|
}
|
|
|
|
|
2010-08-24 23:26:31 +00:00
|
|
|
// at this point, dir is known to not have a trailing /, and
|
|
|
|
// dirent->d_name definitely won't start with one.
|
2010-08-24 07:20:07 +00:00
|
|
|
pakfiles[count] = nva ("%s/%s", dir, dirent->d_name);
|
2001-10-24 22:50:06 +00:00
|
|
|
if (!pakfiles[count])
|
2010-08-25 00:57:44 +00:00
|
|
|
Sys_Error ("qfs_load_gamedir: Memory allocation failure");
|
2001-02-19 21:15:25 +00:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
2001-02-26 06:48:02 +00:00
|
|
|
closedir (dir_ptr);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
qsort (pakfiles, count, sizeof (char *),
|
2010-08-25 00:57:44 +00:00
|
|
|
(int (*)(const void *, const void *)) qfs_file_sort);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
2010-08-25 00:57:44 +00:00
|
|
|
pak = qfs_load_pakfile (pakfiles[i]);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (!pak) {
|
2004-11-02 07:39:00 +00:00
|
|
|
Sys_Error ("Bad pakfile %s!!", pakfiles[i]);
|
2001-02-19 21:15:25 +00:00
|
|
|
} else {
|
2013-01-20 04:41:30 +00:00
|
|
|
sp = new_searchpath ();
|
|
|
|
sp->pack = pak;
|
|
|
|
sp->next = *searchpath;
|
|
|
|
*searchpath = sp;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-25 00:57:44 +00:00
|
|
|
qfs_load_gamedir_free:
|
2001-02-19 21:15:25 +00:00
|
|
|
for (i = 0; i < count; i++)
|
2001-02-26 06:48:02 +00:00
|
|
|
free (pakfiles[i]);
|
|
|
|
free (pakfiles);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-08-25 00:57:44 +00:00
|
|
|
qfs_add_dir
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2003-02-14 22:36:10 +00:00
|
|
|
Adds the directory to the head of the path, then loads and adds pak0.pak
|
|
|
|
pak1.pak ...
|
2001-02-19 21:15:25 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2013-01-20 04:41:30 +00:00
|
|
|
qfs_add_dir (searchpath_t **searchpath, const char *dir)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2013-01-20 04:41:30 +00:00
|
|
|
searchpath_t *sp;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-03-09 07:58:10 +00:00
|
|
|
// add the directory to the search path
|
2013-01-20 04:41:30 +00:00
|
|
|
sp = new_searchpath ();
|
|
|
|
sp->filename = strdup (dir);
|
|
|
|
sp->next = *searchpath;
|
|
|
|
*searchpath = sp;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-03-09 07:58:10 +00:00
|
|
|
// add any pak files in the format pak0.pak pak1.pak, ...
|
2013-01-20 04:41:30 +00:00
|
|
|
qfs_load_gamedir (searchpath, dir);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2013-01-20 04:41:30 +00:00
|
|
|
qfs_add_gamedir (vpath_t *vpath, const char *dir)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2005-09-06 11:39:32 +00:00
|
|
|
const char *e;
|
|
|
|
const char *s;
|
|
|
|
dstring_t *s_dir;
|
2010-08-24 23:26:31 +00:00
|
|
|
dstring_t *f_dir;
|
2005-09-06 11:39:32 +00:00
|
|
|
|
2003-01-10 22:47:18 +00:00
|
|
|
if (!*dir)
|
|
|
|
return;
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
e = fs_sharepath + strlen (fs_sharepath);
|
2005-09-06 11:39:32 +00:00
|
|
|
s = e;
|
|
|
|
s_dir = dstring_new ();
|
2010-08-24 23:26:31 +00:00
|
|
|
f_dir = dstring_new ();
|
2005-09-06 11:39:32 +00:00
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
while (s >= fs_sharepath) {
|
|
|
|
while (s != fs_sharepath && s[-1] !=':')
|
2005-09-06 11:39:32 +00:00
|
|
|
s--;
|
|
|
|
if (s != e) {
|
|
|
|
dsprintf (s_dir, "%.*s", (int) (e - s), s);
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
if (strcmp (s_dir->str, fs_userpath) != 0) {
|
2010-10-16 05:17:09 +00:00
|
|
|
if (qfs_expand_path (f_dir, s_dir->str, dir, 0) != 0) {
|
2010-08-24 23:26:31 +00:00
|
|
|
Sys_Printf ("dropping bad directory %s\n", dir);
|
|
|
|
break;
|
|
|
|
}
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_fs, "qfs_add_gamedir (\"%s\")\n",
|
2010-11-23 05:09:30 +00:00
|
|
|
f_dir->str);
|
2005-09-06 11:39:32 +00:00
|
|
|
|
2013-01-20 04:41:30 +00:00
|
|
|
qfs_add_dir (&vpath->share, f_dir->str);
|
2005-09-06 11:39:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
e = --s;
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2010-08-24 23:26:31 +00:00
|
|
|
qfs_expand_userpath (f_dir, dir);
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_fs, "qfs_add_gamedir (\"%s\")\n", f_dir->str);
|
2013-01-20 04:41:30 +00:00
|
|
|
qfs_add_dir (&vpath->user, f_dir->str);
|
2010-08-24 23:26:31 +00:00
|
|
|
|
|
|
|
dstring_delete (f_dir);
|
|
|
|
dstring_delete (s_dir);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-02-14 19:46:07 +00:00
|
|
|
QFS_Gamedir
|
2001-03-09 07:58:10 +00:00
|
|
|
|
|
|
|
Sets the gamedir and path to a different directory.
|
2001-02-19 21:15:25 +00:00
|
|
|
*/
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2010-08-24 23:25:23 +00:00
|
|
|
QFS_Gamedir (const char *gamedir)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2004-02-03 03:01:06 +00:00
|
|
|
int i;
|
2010-08-24 23:25:23 +00:00
|
|
|
const char *list[2] = {gamedir, 0};
|
2003-01-10 22:47:18 +00:00
|
|
|
|
2003-01-27 21:56:30 +00:00
|
|
|
qfs_build_gamedir (list);
|
2004-02-03 03:01:06 +00:00
|
|
|
|
|
|
|
// Make sure everyone else knows we've changed gamedirs
|
|
|
|
for (i = 0; i < num_gamedir_callbacks; i++) {
|
2007-03-24 10:13:10 +00:00
|
|
|
gamedir_callbacks[i] (0);
|
2004-02-03 03:01:06 +00:00
|
|
|
}
|
2001-03-09 07:58:10 +00:00
|
|
|
Cache_Flush ();
|
2007-03-24 10:13:10 +00:00
|
|
|
for (i = 0; i < num_gamedir_callbacks; i++) {
|
|
|
|
gamedir_callbacks[i] (1);
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2004-02-03 03:01:06 +00:00
|
|
|
QFS_GamedirCallback (gamedir_callback_t *func)
|
|
|
|
{
|
2010-08-24 07:33:22 +00:00
|
|
|
if (num_gamedir_callbacks == max_gamedir_callbacks) {
|
2012-05-21 23:23:22 +00:00
|
|
|
size_t size = (max_gamedir_callbacks + GAMEDIR_CALLBACK_CHUNK)
|
2010-08-24 07:33:22 +00:00
|
|
|
* sizeof (gamedir_callback_t *);
|
|
|
|
gamedir_callbacks = realloc (gamedir_callbacks, size);
|
|
|
|
if (!gamedir_callbacks)
|
|
|
|
Sys_Error ("Too many gamedir callbacks!\n");
|
|
|
|
max_gamedir_callbacks += GAMEDIR_CALLBACK_CHUNK;
|
2004-02-03 03:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!func) {
|
|
|
|
Sys_Error ("null gamedir callback\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
gamedir_callbacks[num_gamedir_callbacks] = func;
|
|
|
|
num_gamedir_callbacks++;
|
|
|
|
}
|
|
|
|
|
2010-08-24 23:26:31 +00:00
|
|
|
static void
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
qfs_path_cvar (void *data, const cvar_t *cvar)
|
2010-08-24 23:26:31 +00:00
|
|
|
{
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
char *cpath = QFS_CompressPath (*(char **)data);
|
|
|
|
if (strcmp (cpath, *(char **)data)) {
|
|
|
|
free (*(char **)cvar->value.value);
|
|
|
|
*(char **)cvar->value.value = cpath;
|
|
|
|
} else {
|
|
|
|
free (cpath);
|
|
|
|
}
|
2010-08-24 23:26:31 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 10:56:17 +00:00
|
|
|
static void
|
|
|
|
qfs_shutdown (void *data)
|
|
|
|
{
|
|
|
|
while (qfs_vpaths) {
|
|
|
|
vpath_t *next = qfs_vpaths->next;
|
|
|
|
delete_vpath (qfs_vpaths);
|
|
|
|
qfs_vpaths = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2021-12-13 00:13:39 +00:00
|
|
|
QFS_Init (memhunk_t *hunk, const char *game)
|
2001-03-09 07:58:10 +00:00
|
|
|
{
|
2003-01-22 20:00:46 +00:00
|
|
|
int i;
|
|
|
|
|
2021-12-13 00:13:39 +00:00
|
|
|
qfs_hunk = hunk;
|
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
Cvar_Register (&fs_sharepath_cvar, qfs_path_cvar, &fs_sharepath);
|
|
|
|
Cvar_Register (&fs_userpath_cvar, qfs_path_cvar, &fs_userpath);
|
|
|
|
Cvar_Register (&fs_dirconf_cvar, 0, 0);
|
2003-01-10 22:47:18 +00:00
|
|
|
|
2010-08-25 00:57:44 +00:00
|
|
|
Cmd_AddCommand ("path", qfs_path_f, "Show what paths Quake is using");
|
2003-02-14 19:46:07 +00:00
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
qfs_userpath = Sys_ExpandSquiggle (fs_userpath);
|
2003-05-23 17:17:01 +00:00
|
|
|
|
2003-01-10 22:47:18 +00:00
|
|
|
qfs_load_config ();
|
|
|
|
|
|
|
|
qfs_game = game;
|
|
|
|
|
2003-01-22 20:00:46 +00:00
|
|
|
if ((i = COM_CheckParm ("-game")) && i < com_argc - 1) {
|
|
|
|
char *gamedirs = NULL;
|
2003-01-27 21:56:30 +00:00
|
|
|
const char **list;
|
2003-01-22 20:00:46 +00:00
|
|
|
char *where;
|
|
|
|
int j, count = 1;
|
|
|
|
|
|
|
|
gamedirs = strdup (com_argv[i + 1]);
|
|
|
|
|
|
|
|
for (j = 0; gamedirs[j]; j++)
|
|
|
|
if (gamedirs[j] == ',')
|
|
|
|
count++;
|
|
|
|
|
2003-04-21 04:49:02 +00:00
|
|
|
list = calloc (count + 1, sizeof (char *));
|
2003-01-22 20:00:46 +00:00
|
|
|
|
|
|
|
j = 0;
|
|
|
|
where = strtok (gamedirs, ",");
|
|
|
|
while (where) {
|
|
|
|
list[j++] = where;
|
|
|
|
where = strtok (NULL, ",");
|
|
|
|
}
|
2003-01-27 21:56:30 +00:00
|
|
|
qfs_build_gamedir (list);
|
2003-01-22 20:00:46 +00:00
|
|
|
free (gamedirs);
|
2007-03-22 23:20:57 +00:00
|
|
|
free ((void*)list);
|
2003-01-22 20:00:46 +00:00
|
|
|
} else {
|
2003-02-14 19:46:07 +00:00
|
|
|
QFS_Gamedir ("");
|
2003-01-22 20:00:46 +00:00
|
|
|
}
|
2021-03-21 10:56:17 +00:00
|
|
|
Sys_RegisterShutdown (qfs_shutdown, 0);
|
2001-03-09 07:58:10 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE const char *
|
2003-02-14 19:46:07 +00:00
|
|
|
QFS_SkipPath (const char *pathname)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-07-15 07:04:17 +00:00
|
|
|
const char *last;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2001-03-09 07:58:10 +00:00
|
|
|
// char after last / on the line
|
2001-03-01 07:42:28 +00:00
|
|
|
if ((last = strrchr (pathname, '/')))
|
|
|
|
last++;
|
|
|
|
else
|
|
|
|
last = pathname;
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
return last;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2003-02-14 19:46:07 +00:00
|
|
|
QFS_StripExtension (const char *in, char *out)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2003-12-28 05:25:06 +00:00
|
|
|
char *tmp;
|
|
|
|
|
2007-03-26 09:06:50 +00:00
|
|
|
if (out != in)
|
|
|
|
strcpy (out, in);
|
2003-12-28 05:25:06 +00:00
|
|
|
|
2011-09-09 08:35:12 +00:00
|
|
|
tmp = out + (QFS_FileExtension (out) - out);
|
|
|
|
*tmp = 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE const char *
|
2003-02-14 19:46:07 +00:00
|
|
|
QFS_FileExtension (const char *in)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2011-09-09 08:33:29 +00:00
|
|
|
const char *tmp;
|
|
|
|
const char *end = in + strlen (in);
|
|
|
|
|
|
|
|
for (tmp = end; tmp != in; tmp--) {
|
|
|
|
if (tmp[-1] == '/')
|
|
|
|
return end;
|
|
|
|
if (tmp[-1] == '.') {
|
|
|
|
if (tmp - 1 == in || tmp[-2] == '/')
|
|
|
|
return end;
|
|
|
|
return tmp - 1;
|
|
|
|
}
|
|
|
|
}
|
2003-12-28 05:25:06 +00:00
|
|
|
|
2011-09-09 08:33:29 +00:00
|
|
|
return end;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2010-08-24 07:20:07 +00:00
|
|
|
QFS_DefaultExtension (dstring_t *path, const char *extension)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2011-09-09 08:37:45 +00:00
|
|
|
const char *ext;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
// if path doesn't have a .EXT, append extension
|
|
|
|
// (extension should include the .)
|
2011-09-09 08:37:45 +00:00
|
|
|
ext = QFS_FileExtension (path->str);
|
|
|
|
if (*ext)
|
|
|
|
return; // it has an extension
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2010-08-24 07:20:07 +00:00
|
|
|
dstring_appendstr (path, extension);
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE void
|
|
|
|
QFS_SetExtension (struct dstring_s *path, const char *extension)
|
|
|
|
{
|
|
|
|
const char *ext = QFS_FileExtension (path->str);
|
2011-09-09 10:55:54 +00:00
|
|
|
int offs = ext - path->str;
|
2010-08-24 07:20:07 +00:00
|
|
|
|
2011-09-09 08:37:45 +00:00
|
|
|
if (*ext) {
|
|
|
|
// path has an extension... cut it off
|
|
|
|
path->str[offs] = 0;
|
|
|
|
path->size = offs + 1;
|
2010-08-24 07:20:07 +00:00
|
|
|
}
|
|
|
|
dstring_appendstr (path, extension);
|
2001-03-09 07:58:10 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 08:27:04 +00:00
|
|
|
VISIBLE QFile *
|
|
|
|
QFS_NextFile (dstring_t *filename, const char *prefix, const char *ext)
|
2001-03-09 07:58:10 +00:00
|
|
|
{
|
2022-03-31 08:27:04 +00:00
|
|
|
QFile *file = 0;
|
2010-08-24 23:26:31 +00:00
|
|
|
dstring_t *full_path = dstring_new ();
|
2001-03-09 07:58:10 +00:00
|
|
|
|
2022-03-31 07:40:27 +00:00
|
|
|
if (qfs_expand_userpath (full_path, "") == -1) {
|
|
|
|
dsprintf (filename, "failed to expand userpath");
|
|
|
|
} else {
|
|
|
|
size_t qfs_pos = strlen (full_path->str);
|
|
|
|
dstring_appendstr (full_path, prefix);
|
|
|
|
int fd = Sys_UniqueFile (filename, full_path->str, ext, 4);
|
|
|
|
if (fd >= 0) {
|
|
|
|
dstring_snip (filename, 0, qfs_pos);
|
2022-03-31 08:27:04 +00:00
|
|
|
// Sys_UniqueFile opens with O_RDWR, and ensure binary files work
|
|
|
|
// on Windows. gzip writing is NOT specified
|
|
|
|
file = Qdopen (fd, "w+b");
|
2010-08-24 23:26:31 +00:00
|
|
|
}
|
2001-03-09 07:58:10 +00:00
|
|
|
}
|
2010-08-24 23:26:31 +00:00
|
|
|
dstring_delete (full_path);
|
2022-03-31 08:27:04 +00:00
|
|
|
return file;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2003-02-14 22:36:10 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE QFile *
|
2003-05-23 17:17:01 +00:00
|
|
|
QFS_Open (const char *path, const char *mode)
|
2003-02-14 22:36:10 +00:00
|
|
|
{
|
|
|
|
dstring_t *full_path = dstring_new ();
|
2004-05-09 22:58:37 +00:00
|
|
|
QFile *file = 0;
|
2004-04-13 03:38:16 +00:00
|
|
|
const char *m;
|
|
|
|
int write = 0;
|
2003-02-14 22:36:10 +00:00
|
|
|
|
2010-08-24 23:26:31 +00:00
|
|
|
if (qfs_expand_userpath (full_path, path) == 0) {
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_fs, "QFS_Open: %s %s\n", full_path->str, mode);
|
2004-04-13 04:42:12 +00:00
|
|
|
for (m = mode; *m; m++)
|
|
|
|
if (*m == 'w' || *m == '+' || *m == 'a')
|
|
|
|
write = 1;
|
|
|
|
if (write)
|
2010-08-24 23:21:07 +00:00
|
|
|
if (Sys_CreatePath (full_path->str) == -1)
|
2004-05-09 22:58:37 +00:00
|
|
|
goto done;
|
2004-04-13 04:42:12 +00:00
|
|
|
file = Qopen (full_path->str, mode);
|
|
|
|
}
|
2004-05-09 22:58:37 +00:00
|
|
|
done:
|
2003-02-14 22:36:10 +00:00
|
|
|
dstring_delete (full_path);
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE QFile *
|
2003-05-23 17:17:01 +00:00
|
|
|
QFS_WOpen (const char *path, int zip)
|
|
|
|
{
|
2003-08-05 01:45:52 +00:00
|
|
|
char mode[5] = "wb\000\000\000";
|
2003-05-23 17:17:01 +00:00
|
|
|
|
2003-07-08 20:11:10 +00:00
|
|
|
if (zip) {
|
|
|
|
mode[2] = 'z';
|
|
|
|
mode[3] = bound (1, zip, 9) + '0';
|
|
|
|
}
|
2003-05-23 17:17:01 +00:00
|
|
|
return QFS_Open (path, mode);
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE int
|
2007-03-18 07:12:45 +00:00
|
|
|
QFS_Rename (const char *old_path, const char *new_path)
|
2003-02-14 22:36:10 +00:00
|
|
|
{
|
|
|
|
dstring_t *full_old = dstring_new ();
|
|
|
|
dstring_t *full_new = dstring_new ();
|
|
|
|
int ret;
|
|
|
|
|
2010-08-24 23:26:31 +00:00
|
|
|
if ((ret = qfs_expand_userpath (full_old, old_path)) != -1)
|
2010-12-28 01:28:46 +00:00
|
|
|
if ((ret = qfs_expand_userpath (full_new, new_path)) != -1)
|
|
|
|
if ((ret = Sys_CreatePath (full_new->str)) != -1) {
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_fs, "QFS_Rename %s %s\n", full_old->str,
|
2012-07-21 13:34:10 +00:00
|
|
|
full_new->str);
|
2010-08-24 23:26:31 +00:00
|
|
|
ret = Qrename (full_old->str, full_new->str);
|
2010-12-28 01:28:46 +00:00
|
|
|
}
|
2003-02-14 22:36:10 +00:00
|
|
|
dstring_delete (full_old);
|
|
|
|
dstring_delete (full_new);
|
|
|
|
return ret;
|
|
|
|
}
|
2003-05-23 17:17:01 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE int
|
2003-05-23 17:17:01 +00:00
|
|
|
QFS_Remove (const char *path)
|
|
|
|
{
|
|
|
|
dstring_t *full_path = dstring_new ();
|
|
|
|
int ret;
|
|
|
|
|
2010-08-24 23:26:31 +00:00
|
|
|
if ((ret = qfs_expand_userpath (full_path, path)) != -1)
|
|
|
|
ret = Qremove (full_path->str);
|
2003-05-23 17:17:01 +00:00
|
|
|
dstring_delete (full_path);
|
|
|
|
return ret;
|
|
|
|
}
|
2004-04-20 22:25:52 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE filelist_t *
|
2004-04-20 22:25:52 +00:00
|
|
|
QFS_FilelistNew (void)
|
|
|
|
{
|
|
|
|
return calloc (1, sizeof (filelist_t));
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2004-04-23 22:53:39 +00:00
|
|
|
QFS_FilelistAdd (filelist_t *filelist, const char *fname, const char *ext)
|
2004-04-20 22:25:52 +00:00
|
|
|
{
|
|
|
|
char **new_list;
|
2004-04-23 22:53:39 +00:00
|
|
|
char *s, *str;
|
2004-04-20 22:25:52 +00:00
|
|
|
|
|
|
|
while ((s = strchr(fname, '/')))
|
2004-04-23 22:53:39 +00:00
|
|
|
fname = s + 1;
|
2004-04-20 22:25:52 +00:00
|
|
|
if (filelist->count == filelist->size) {
|
|
|
|
filelist->size += 32;
|
|
|
|
new_list = realloc (filelist->list, filelist->size * sizeof (char *));
|
|
|
|
|
|
|
|
if (!new_list) {
|
|
|
|
filelist->size -= 32;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
filelist->list = new_list;
|
|
|
|
}
|
2004-04-23 22:53:39 +00:00
|
|
|
str = strdup (fname);
|
2004-04-20 22:25:52 +00:00
|
|
|
|
2021-01-31 07:01:20 +00:00
|
|
|
if (ext && (s = strstr(str, va (0, ".%s", ext))))
|
2004-04-20 22:25:52 +00:00
|
|
|
*s = 0;
|
2004-04-23 22:53:39 +00:00
|
|
|
filelist->list[filelist->count++] = str;
|
2004-04-20 22:25:52 +00:00
|
|
|
}
|
|
|
|
|
2013-01-20 05:07:57 +00:00
|
|
|
static void
|
|
|
|
qfs_filelistfill_do (filelist_t *list, const searchpath_t *search, const char *cp, const char *ext, int strip)
|
|
|
|
{
|
|
|
|
const char *separator = "/";
|
|
|
|
|
|
|
|
if (*cp && cp[strlen (cp) - 1] == '/')
|
|
|
|
separator = "";
|
|
|
|
|
|
|
|
if (search->pack) {
|
|
|
|
int i;
|
|
|
|
pack_t *pak = search->pack;
|
|
|
|
|
|
|
|
for (i = 0; i < pak->numfiles; i++) {
|
|
|
|
char *name = pak->files[i].name;
|
2021-01-31 07:01:20 +00:00
|
|
|
if (!fnmatch (va (0, "%s%s*.%s", cp, separator, ext), name,
|
2013-01-20 05:07:57 +00:00
|
|
|
FNM_PATHNAME)
|
2021-01-31 07:01:20 +00:00
|
|
|
|| !fnmatch (va (0, "%s%s*.%s.gz", cp, separator, ext), name,
|
2013-01-20 05:07:57 +00:00
|
|
|
FNM_PATHNAME))
|
|
|
|
QFS_FilelistAdd (list, name, strip ? ext : 0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DIR *dir_ptr;
|
|
|
|
struct dirent *dirent;
|
|
|
|
|
2021-01-31 07:01:20 +00:00
|
|
|
dir_ptr = opendir (va (0, "%s/%s", search->filename, cp));
|
2013-01-20 05:07:57 +00:00
|
|
|
if (!dir_ptr)
|
|
|
|
return;
|
|
|
|
while ((dirent = readdir (dir_ptr)))
|
2021-01-31 07:01:20 +00:00
|
|
|
if (!fnmatch (va (0, "*.%s", ext), dirent->d_name, 0)
|
|
|
|
|| !fnmatch (va (0, "*.%s.gz", ext), dirent->d_name, 0))
|
2013-01-20 05:07:57 +00:00
|
|
|
QFS_FilelistAdd (list, dirent->d_name, strip ? ext : 0);
|
|
|
|
closedir (dir_ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2004-04-20 22:52:09 +00:00
|
|
|
QFS_FilelistFill (filelist_t *list, const char *path, const char *ext,
|
|
|
|
int strip)
|
2004-04-20 22:25:52 +00:00
|
|
|
{
|
2013-01-20 05:07:57 +00:00
|
|
|
vpath_t *vpath;
|
2004-04-20 22:25:52 +00:00
|
|
|
searchpath_t *search;
|
2010-08-24 23:26:31 +00:00
|
|
|
char *cpath, *cp;
|
|
|
|
|
|
|
|
if (strchr (ext, '/') || strchr (ext, '\\'))
|
|
|
|
return;
|
|
|
|
|
|
|
|
cp = cpath = QFS_CompressPath (path);
|
2004-04-20 22:25:52 +00:00
|
|
|
|
2013-01-20 05:07:57 +00:00
|
|
|
for (vpath = qfs_vpaths; vpath; vpath = vpath->next) {
|
|
|
|
for (search = vpath->user; search; search = search->next) {
|
|
|
|
qfs_filelistfill_do (list, search, cp, ext, strip);
|
2004-04-20 22:25:52 +00:00
|
|
|
}
|
|
|
|
}
|
2010-08-24 23:26:31 +00:00
|
|
|
free (cpath);
|
2004-04-20 22:25:52 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2004-04-20 22:25:52 +00:00
|
|
|
QFS_FilelistFree (filelist_t *list)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < list->count; i++)
|
|
|
|
free (list->list[i]);
|
|
|
|
free (list->list);
|
|
|
|
free (list);
|
|
|
|
}
|