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
|
|
|
|
|
|
|
|
*/
|
2001-09-28 06:26:31 +00:00
|
|
|
static const char rcsid[] =
|
|
|
|
"$Id$";
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2001-03-09 07:58:10 +00:00
|
|
|
# include "config.h"
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
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
|
|
|
|
|
2001-03-09 07:58:10 +00:00
|
|
|
#include <ctype.h>
|
2001-02-19 21:15:25 +00:00
|
|
|
#include <dirent.h>
|
2001-08-22 22:03:16 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.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
|
|
|
|
# ifdef WIN32
|
|
|
|
# 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>
|
|
|
|
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/cmd.h"
|
|
|
|
#include "QF/cvar.h"
|
|
|
|
#include "QF/hash.h"
|
|
|
|
#include "QF/qargs.h"
|
|
|
|
#include "QF/qendian.h"
|
|
|
|
#include "QF/qtypes.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/va.h"
|
2001-08-22 22:03:16 +00:00
|
|
|
#include "QF/vfs.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
|
|
|
|
int fnmatch (const char *__pattern, const char *__string, int __flags);
|
|
|
|
#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.
|
2001-02-19 21:15:25 +00:00
|
|
|
The base directory is only used during filesystem initialization.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
The "cache directory" is only used during development to save network
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2001-03-09 07:58:10 +00:00
|
|
|
QUAKE FILESYSTEM
|
2001-02-19 21:15:25 +00:00
|
|
|
*/
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
char gamedirfile[MAX_OSPATH];
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
cvar_t *fs_userpath;
|
|
|
|
cvar_t *fs_sharepath;
|
|
|
|
cvar_t *fs_basegame;
|
2001-03-30 00:44:35 +00:00
|
|
|
cvar_t *fs_skinbase;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int com_filesize;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Structs for pack files on disk
|
|
|
|
*/
|
2001-02-26 06:48:02 +00:00
|
|
|
typedef struct {
|
|
|
|
char name[56];
|
|
|
|
int filepos, filelen;
|
2001-02-19 21:15:25 +00:00
|
|
|
} dpackfile_t;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
typedef struct {
|
|
|
|
char id[4];
|
|
|
|
int dirofs;
|
|
|
|
int dirlen;
|
2001-02-19 21:15:25 +00:00
|
|
|
} dpackheader_t;
|
|
|
|
|
|
|
|
#define MAX_FILES_IN_PACK 2048
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
char com_gamedir[MAX_OSPATH];
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
searchpath_t *com_searchpaths;
|
|
|
|
searchpath_t *com_base_searchpaths; // without gamedirs
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
void
|
2001-07-14 23:50:18 +00:00
|
|
|
COM_FileBase (const char *in, char *out)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-07-14 23:50:18 +00:00
|
|
|
const char *slash, *dot;
|
|
|
|
const char *s;
|
2001-03-09 07:58:10 +00:00
|
|
|
|
|
|
|
slash = in;
|
|
|
|
dot = NULL;
|
|
|
|
s = in;
|
|
|
|
while (*s) {
|
|
|
|
if (*s == '/')
|
|
|
|
slash = s + 1;
|
|
|
|
if (*s == '.')
|
|
|
|
dot = s;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
if (dot == NULL)
|
|
|
|
dot = s;
|
|
|
|
if (dot - slash < 2)
|
2001-02-26 06:48:02 +00:00
|
|
|
strcpy (out, "?model?");
|
|
|
|
else {
|
2001-03-09 07:58:10 +00:00
|
|
|
while (slash < dot)
|
|
|
|
*out++ = *slash++;
|
|
|
|
*out++ = 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
int
|
2001-05-30 03:21:19 +00:00
|
|
|
COM_filelength (VFile *f)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int pos;
|
|
|
|
int end;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
pos = Qtell (f);
|
|
|
|
Qseek (f, 0, SEEK_END);
|
|
|
|
end = Qtell (f);
|
|
|
|
Qseek (f, pos, SEEK_SET);
|
|
|
|
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
int
|
2001-05-30 03:21:19 +00:00
|
|
|
COM_FileOpenRead (char *path, VFile **hndl)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-05-30 03:21:19 +00:00
|
|
|
VFile *f;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
f = Qopen (path, "rbz");
|
|
|
|
if (!f) {
|
2001-02-19 21:15:25 +00:00
|
|
|
*hndl = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*hndl = f;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
return COM_filelength (f);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
void
|
|
|
|
COM_Path_f (void)
|
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
searchpath_t *s;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("Current search path:\n");
|
2001-02-26 06:48:02 +00:00
|
|
|
for (s = com_searchpaths; s; s = s->next) {
|
2001-02-19 21:15:25 +00:00
|
|
|
if (s == com_base_searchpaths)
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("----------\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
if (s->pack)
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("%s (%i files)\n", s->pack->filename,
|
2001-02-26 06:48:02 +00:00
|
|
|
s->pack->numfiles);
|
2001-02-19 21:15:25 +00:00
|
|
|
else
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("%s\n", s->filename);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
COM_WriteFile
|
|
|
|
|
|
|
|
The filename will be prefixed by the current game directory
|
|
|
|
*/
|
|
|
|
void
|
2001-07-15 07:04:17 +00:00
|
|
|
COM_WriteFile (const char *filename, void *data, int len)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-05-30 03:21:19 +00:00
|
|
|
VFile *f;
|
2001-02-26 06:48:02 +00:00
|
|
|
char name[MAX_OSPATH];
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
snprintf (name, sizeof (name), "%s/%s", com_gamedir, filename);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
f = Qopen (name, "wb");
|
|
|
|
if (!f) {
|
2001-02-26 06:48:02 +00:00
|
|
|
Sys_mkdir (com_gamedir);
|
2001-02-19 21:15:25 +00:00
|
|
|
f = Qopen (name, "wb");
|
|
|
|
if (!f)
|
|
|
|
Sys_Error ("Error opening %s", filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sys_Printf ("COM_WriteFile: %s\n", name);
|
|
|
|
Qwrite (f, data, len);
|
|
|
|
Qclose (f);
|
|
|
|
}
|
|
|
|
|
2001-03-09 07:58:10 +00:00
|
|
|
/*
|
|
|
|
COM_WriteBuffers
|
|
|
|
|
|
|
|
The filename will be prefixed by the current game directory
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
COM_WriteBuffers (const char *filename, int count, ...)
|
|
|
|
{
|
2001-05-30 03:21:19 +00:00
|
|
|
VFile *f;
|
2001-03-09 07:58:10 +00:00
|
|
|
char name[MAX_OSPATH];
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start (args, count);
|
|
|
|
|
|
|
|
snprintf (name, sizeof (name), "%s/%s", com_gamedir, filename);
|
|
|
|
|
|
|
|
f = Qopen (name, "wb");
|
|
|
|
if (!f) {
|
|
|
|
Sys_mkdir (com_gamedir);
|
|
|
|
f = Qopen (name, "wb");
|
|
|
|
if (!f)
|
|
|
|
Sys_Error ("Error opening %s", filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sys_Printf ("COM_WriteBuffers: %s\n", name);
|
|
|
|
while (count--) {
|
|
|
|
void *data = va_arg (args, void *);
|
|
|
|
int len = va_arg (args, int);
|
|
|
|
|
|
|
|
Qwrite (f, data, len);
|
|
|
|
}
|
|
|
|
Qclose (f);
|
|
|
|
va_end (args);
|
|
|
|
}
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
/*
|
|
|
|
COM_CreatePath
|
|
|
|
|
|
|
|
Only used for CopyFile and download
|
|
|
|
*/
|
|
|
|
void
|
2001-07-15 07:04:17 +00:00
|
|
|
COM_CreatePath (const char *path)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-07-24 03:22:54 +00:00
|
|
|
char *ofs;
|
2001-03-09 07:58:10 +00:00
|
|
|
char e_path[MAX_OSPATH];
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
Qexpand_squiggle (path, e_path);
|
|
|
|
|
2001-07-24 03:22:54 +00:00
|
|
|
for (ofs = e_path + 1; *ofs; ofs++) {
|
2001-02-26 06:48:02 +00:00
|
|
|
if (*ofs == '/') { // create the directory
|
2001-07-24 03:22:54 +00:00
|
|
|
*ofs = 0;
|
|
|
|
Sys_mkdir (e_path);
|
|
|
|
*ofs = '/';
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
COM_CopyFile
|
|
|
|
|
|
|
|
Copies a file over from the net to the local cache, creating any
|
|
|
|
directories needed. This is for the convenience of developers using
|
|
|
|
ISDN from home.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
COM_CopyFile (char *netpath, char *cachepath)
|
|
|
|
{
|
2001-05-30 03:21:19 +00:00
|
|
|
VFile *in, *out;
|
2001-02-26 06:48:02 +00:00
|
|
|
int remaining, count;
|
|
|
|
char buf[4096];
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
remaining = COM_FileOpenRead (netpath, &in);
|
2001-02-26 06:48:02 +00:00
|
|
|
COM_CreatePath (cachepath); // create directories up to the cache
|
|
|
|
// file
|
|
|
|
out = Qopen (cachepath, "wb");
|
2001-02-19 21:15:25 +00:00
|
|
|
if (!out)
|
|
|
|
Sys_Error ("Error opening %s", cachepath);
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
while (remaining) {
|
|
|
|
if (remaining < sizeof (buf))
|
2001-02-19 21:15:25 +00:00
|
|
|
count = remaining;
|
|
|
|
else
|
2001-02-26 06:48:02 +00:00
|
|
|
count = sizeof (buf);
|
|
|
|
Qread (in, buf, count);
|
2001-02-19 21:15:25 +00:00
|
|
|
Qwrite (out, buf, count);
|
|
|
|
remaining -= count;
|
|
|
|
}
|
|
|
|
|
|
|
|
Qclose (in);
|
|
|
|
Qclose (out);
|
|
|
|
}
|
|
|
|
|
2001-05-30 03:21:19 +00:00
|
|
|
VFile *
|
2001-02-19 21:15:25 +00:00
|
|
|
COM_OpenRead (const char *path, int offs, int len, int zip)
|
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int fd = open (path, O_RDONLY);
|
2001-02-19 21:15:25 +00:00
|
|
|
unsigned char id[2];
|
|
|
|
unsigned char len_bytes[4];
|
2001-02-26 06:48:02 +00:00
|
|
|
|
|
|
|
if (fd == -1) {
|
2001-02-19 21:15:25 +00:00
|
|
|
Sys_Error ("Couldn't open %s", path);
|
|
|
|
return 0;
|
|
|
|
}
|
2001-02-26 06:48:02 +00:00
|
|
|
if (offs < 0 || len < 0) {
|
2001-02-19 21:15:25 +00:00
|
|
|
// normal file
|
2001-02-26 06:48:02 +00:00
|
|
|
offs = 0;
|
|
|
|
len = lseek (fd, 0, SEEK_END);
|
|
|
|
lseek (fd, 0, SEEK_SET);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2001-02-26 06:48:02 +00:00
|
|
|
lseek (fd, offs, SEEK_SET);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (zip) {
|
2001-02-26 06:48:02 +00:00
|
|
|
read (fd, id, 2);
|
|
|
|
if (id[0] == 0x1f && id[1] == 0x8b) {
|
|
|
|
lseek (fd, offs + len - 4, SEEK_SET);
|
|
|
|
read (fd, len_bytes, 4);
|
|
|
|
len = ((len_bytes[3] << 24)
|
|
|
|
| (len_bytes[2] << 16)
|
|
|
|
| (len_bytes[1] << 8)
|
|
|
|
| (len_bytes[0]));
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
2001-02-26 06:48:02 +00:00
|
|
|
lseek (fd, offs, SEEK_SET);
|
|
|
|
com_filesize = len;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
2001-02-26 06:48:02 +00:00
|
|
|
setmode (fd, O_BINARY);
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
|
|
|
if (zip)
|
2001-02-26 06:48:02 +00:00
|
|
|
return Qdopen (fd, "rbz");
|
2001-02-19 21:15:25 +00:00
|
|
|
else
|
2001-02-26 06:48:02 +00:00
|
|
|
return Qdopen (fd, "rb");
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2001-08-21 00:06:22 +00:00
|
|
|
/*
|
|
|
|
contains_updir
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2001-08-21 00:06:22 +00:00
|
|
|
Checks if a string contains an updir ('..'), either at the ends or
|
|
|
|
surrounded by slashes ('/'). Doesn't check for leading slash.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
contains_updir (const char *filename)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (filename[0] == 0 || filename [1] == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// FIXME: maybe I should handle alternate seperators?
|
|
|
|
for (i = 0; filename[i+1]; i++) {
|
2001-08-22 22:03:16 +00:00
|
|
|
if (!(i == 0 || filename[i-1] == '/') // beginning of string
|
|
|
|
// or first slash
|
2001-08-21 00:06:22 +00:00
|
|
|
|| filename[i] != '.' // first dot
|
|
|
|
|| filename[i+1] != '.') // second dot
|
|
|
|
continue;
|
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
if (filename[i+2] == 0 || filename[i+2] == '/')
|
|
|
|
// end of string or second slash
|
2001-08-21 00:06:22 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int file_from_pak; // global indicating file came from pack file ZOID
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
COM_FOpenFile
|
|
|
|
|
|
|
|
Finds the file in the search path.
|
|
|
|
Sets com_filesize and one of handle or file
|
|
|
|
*/
|
|
|
|
int
|
2001-07-14 23:50:18 +00:00
|
|
|
_COM_FOpenFile (const char *filename, VFile **gzfile, char *foundname, int zip)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
searchpath_t *search;
|
|
|
|
char netpath[MAX_OSPATH];
|
|
|
|
int findtime;
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2001-02-26 06:48:02 +00:00
|
|
|
char gzfilename[MAX_OSPATH];
|
2002-01-03 23:24:20 +00:00
|
|
|
int filenamelen;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
filenamelen = strlen (filename);
|
|
|
|
strncpy (gzfilename, filename, sizeof (gzfilename));
|
2001-03-09 07:58:10 +00:00
|
|
|
strncat (gzfilename, ".gz", sizeof (gzfilename) - strlen (gzfilename));
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
file_from_pak = 0;
|
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
// make sure they're not trying to do wierd stuff with our private files
|
2001-08-21 00:06:22 +00:00
|
|
|
if (contains_updir(filename)) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("FindFile: %s: attempt to escape directory tree!\n", filename);
|
2001-08-21 02:44:45 +00:00
|
|
|
goto error;
|
2001-08-21 00:06:22 +00:00
|
|
|
}
|
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
// search through the path, one element at a time
|
2001-02-26 06:48:02 +00:00
|
|
|
for (search = com_searchpaths; search; search = search->next) {
|
|
|
|
// is the element a pak file?
|
|
|
|
if (search->pack) {
|
2001-03-09 07:58:10 +00:00
|
|
|
packfile_t *packfile;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2001-03-09 07:58:10 +00:00
|
|
|
packfile = (packfile_t *) Hash_Find (search->pack->file_hash,
|
|
|
|
filename);
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2001-03-09 07:58:10 +00:00
|
|
|
if (!packfile)
|
|
|
|
packfile = (packfile_t *) Hash_Find (search->pack->file_hash,
|
|
|
|
gzfilename);
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
2001-03-09 07:58:10 +00:00
|
|
|
if (packfile) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_DPrintf ("PackFile: %s : %s\n", search->pack->filename,
|
2001-03-09 07:58:10 +00:00
|
|
|
packfile->name);
|
|
|
|
// open a new file on the pakfile
|
|
|
|
strncpy (foundname, packfile->name, MAX_OSPATH);
|
|
|
|
*gzfile =
|
|
|
|
COM_OpenRead (search->pack->filename, packfile->filepos,
|
|
|
|
packfile->filelen, zip);
|
|
|
|
file_from_pak = 1;
|
|
|
|
return com_filesize;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2001-02-26 06:48:02 +00:00
|
|
|
} else {
|
|
|
|
// check a file in the directory tree
|
2001-07-17 22:10:20 +00:00
|
|
|
snprintf (netpath, sizeof (netpath), "%s%s%s", search->filename,
|
|
|
|
search->filename[0] ? "/" : "", filename);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
strncpy (foundname, filename, MAX_OSPATH);
|
2001-02-19 21:15:25 +00:00
|
|
|
findtime = Sys_FileTime (netpath);
|
|
|
|
if (findtime == -1) {
|
|
|
|
#ifdef HAVE_ZLIB
|
2001-02-26 06:48:02 +00:00
|
|
|
strncpy (foundname, gzfilename, MAX_OSPATH);
|
2001-08-22 22:03:16 +00:00
|
|
|
snprintf (netpath, sizeof (netpath), "%s%s%s",
|
|
|
|
search->filename,
|
2001-07-17 22:10:20 +00:00
|
|
|
search->filename[0] ? "/" : "", gzfilename);
|
2001-02-19 21:15:25 +00:00
|
|
|
findtime = Sys_FileTime (netpath);
|
|
|
|
if (findtime == -1)
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_DPrintf ("FindFile: %s\n", netpath);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
*gzfile = COM_OpenRead (netpath, -1, -1, zip);
|
2001-02-19 21:15:25 +00:00
|
|
|
return com_filesize;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_DPrintf ("FindFile: can't find %s\n", filename);
|
2001-08-21 02:44:45 +00:00
|
|
|
error:
|
2001-02-19 21:15:25 +00:00
|
|
|
*gzfile = NULL;
|
|
|
|
com_filesize = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-07-14 23:50:18 +00:00
|
|
|
COM_FOpenFile (const char *filename, VFile **gzfile)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
char foundname[MAX_OSPATH];
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
return _COM_FOpenFile (filename, gzfile, foundname, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
cache_user_t *loadcache;
|
2001-02-26 06:48:02 +00:00
|
|
|
byte *loadbuf;
|
|
|
|
int loadsize;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
COM_LoadFile
|
|
|
|
|
|
|
|
Filename are relative to the quake directory.
|
|
|
|
Allways appends a 0 byte to the loaded data.
|
|
|
|
*/
|
2001-02-26 06:48:02 +00:00
|
|
|
byte *
|
2001-07-14 23:50:18 +00:00
|
|
|
COM_LoadFile (const char *path, int usehunk)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-05-30 03:21:19 +00:00
|
|
|
VFile *h;
|
2001-03-09 07:58:10 +00:00
|
|
|
byte *buf = NULL;
|
2001-02-26 06:48:02 +00:00
|
|
|
char base[32];
|
|
|
|
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
|
2001-02-19 21:15:25 +00:00
|
|
|
len = com_filesize = COM_FOpenFile (path, &h);
|
|
|
|
if (!h)
|
|
|
|
return NULL;
|
|
|
|
|
2001-03-09 07:58:10 +00:00
|
|
|
// extract the filename base name for hunk tag
|
2001-02-19 21:15:25 +00:00
|
|
|
COM_FileBase (path, base);
|
|
|
|
|
|
|
|
if (usehunk == 1)
|
2001-02-26 06:48:02 +00:00
|
|
|
buf = Hunk_AllocName (len + 1, base);
|
2001-02-19 21:15:25 +00:00
|
|
|
else if (usehunk == 2)
|
2001-02-26 06:48:02 +00:00
|
|
|
buf = Hunk_TempAlloc (len + 1);
|
2001-02-19 21:15:25 +00:00
|
|
|
else if (usehunk == 0)
|
2001-02-26 06:48:02 +00:00
|
|
|
buf = calloc (1, 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);
|
|
|
|
else if (usehunk == 4) {
|
|
|
|
if (len + 1 > loadsize)
|
|
|
|
buf = Hunk_TempAlloc (len + 1);
|
2001-02-19 21:15:25 +00:00
|
|
|
else
|
|
|
|
buf = loadbuf;
|
2001-02-26 06:48:02 +00:00
|
|
|
} else
|
2001-02-19 21:15:25 +00:00
|
|
|
Sys_Error ("COM_LoadFile: bad usehunk");
|
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
Sys_Error ("COM_LoadFile: not enough space for %s", path);
|
|
|
|
|
2001-11-18 23:40:01 +00:00
|
|
|
buf[len] = 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
Qread (h, buf, len);
|
|
|
|
Qclose (h);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
byte *
|
2001-07-14 23:50:18 +00:00
|
|
|
COM_LoadHunkFile (const char *path)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
return COM_LoadFile (path, 1);
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
byte *
|
2001-07-14 23:50:18 +00:00
|
|
|
COM_LoadTempFile (const char *path)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
return COM_LoadFile (path, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-07-14 23:50:18 +00:00
|
|
|
COM_LoadCacheFile (const char *path, struct cache_user_s *cu)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
loadcache = cu;
|
|
|
|
COM_LoadFile (path, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// uses temp hunk if larger than bufsize
|
2001-02-26 06:48:02 +00:00
|
|
|
byte *
|
2001-07-14 23:50:18 +00:00
|
|
|
COM_LoadStackFile (const char *path, void *buffer, int bufsize)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
byte *buf;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
loadbuf = (byte *) buffer;
|
2001-02-19 21:15:25 +00:00
|
|
|
loadsize = bufsize;
|
|
|
|
buf = COM_LoadFile (path, 4);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2001-06-04 04:52:14 +00:00
|
|
|
static const char *
|
2001-03-09 07:58:10 +00:00
|
|
|
pack_get_key (void *_p, void *unused)
|
|
|
|
{
|
|
|
|
packfile_t *p = (packfile_t *) _p;
|
|
|
|
|
|
|
|
return p->name;
|
|
|
|
}
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
/*
|
|
|
|
COM_LoadPackFile
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2001-02-26 06:48:02 +00:00
|
|
|
pack_t *
|
2001-02-19 21:15:25 +00:00
|
|
|
COM_LoadPackFile (char *packfile)
|
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
dpackheader_t header;
|
|
|
|
int i;
|
|
|
|
packfile_t *newfiles;
|
|
|
|
int numpackfiles;
|
|
|
|
pack_t *pack;
|
2001-05-30 03:21:19 +00:00
|
|
|
VFile *packhandle;
|
2001-02-26 06:48:02 +00:00
|
|
|
dpackfile_t info[MAX_FILES_IN_PACK];
|
2001-03-09 07:58:10 +00:00
|
|
|
hashtab_t *hash;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (COM_FileOpenRead (packfile, &packhandle) == -1)
|
|
|
|
return NULL;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
Qread (packhandle, &header, sizeof (header));
|
2001-02-19 21:15:25 +00:00
|
|
|
if (header.id[0] != 'P' || header.id[1] != 'A'
|
2001-02-26 06:48:02 +00:00
|
|
|
|| header.id[2] != 'C' || header.id[3] != 'K')
|
2001-02-19 21:15:25 +00:00
|
|
|
Sys_Error ("%s is not a packfile", packfile);
|
|
|
|
header.dirofs = LittleLong (header.dirofs);
|
|
|
|
header.dirlen = LittleLong (header.dirlen);
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
numpackfiles = header.dirlen / sizeof (dpackfile_t);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (numpackfiles > MAX_FILES_IN_PACK)
|
|
|
|
Sys_Error ("%s has %i files", packfile, numpackfiles);
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
newfiles = calloc (1, numpackfiles * sizeof (packfile_t));
|
2001-03-09 07:58:10 +00:00
|
|
|
hash = Hash_NewTable (1021, pack_get_key, 0, 0);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
Qseek (packhandle, header.dirofs, SEEK_SET);
|
|
|
|
Qread (packhandle, info, header.dirlen);
|
|
|
|
|
2001-03-09 07:58:10 +00:00
|
|
|
// parse the directory
|
2001-02-26 06:48:02 +00:00
|
|
|
for (i = 0; i < numpackfiles; i++) {
|
2001-02-19 21:15:25 +00:00
|
|
|
strcpy (newfiles[i].name, info[i].name);
|
2001-02-26 06:48:02 +00:00
|
|
|
newfiles[i].filepos = LittleLong (info[i].filepos);
|
|
|
|
newfiles[i].filelen = LittleLong (info[i].filelen);
|
2001-03-09 07:58:10 +00:00
|
|
|
Hash_Add (hash, &newfiles[i]);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pack = calloc (1, sizeof (pack_t));
|
|
|
|
strcpy (pack->filename, packfile);
|
|
|
|
pack->handle = packhandle;
|
|
|
|
pack->numfiles = numpackfiles;
|
|
|
|
pack->files = newfiles;
|
2001-03-09 07:58:10 +00:00
|
|
|
pack->file_hash = hash;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("Added packfile %s (%i files)\n", packfile, numpackfiles);
|
2001-02-19 21:15:25 +00:00
|
|
|
return pack;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define FBLOCK_SIZE 32
|
|
|
|
#define FNAME_SIZE MAX_OSPATH
|
|
|
|
|
|
|
|
// Note, this is /NOT/ a work-alike strcmp, this groups numbers sanely.
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
qstrcmp (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) {
|
|
|
|
in1 = in2 = n1 = n2 = 0;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if ((in1 = isdigit ((int) *s1)))
|
|
|
|
n1 = strtol (s1, &s1, 10);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if ((in2 = isdigit ((int) *s2)))
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-07-15 07:04:17 +00:00
|
|
|
COM_LoadGameDirectory (const char *dir)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
searchpath_t *search;
|
|
|
|
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
|
|
|
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_DPrintf ("COM_LoadGameDirectory (\"%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)
|
|
|
|
goto COM_LoadGameDirectory_free;
|
|
|
|
|
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)
|
|
|
|
return;
|
|
|
|
|
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)
|
|
|
|
goto COM_LoadGameDirectory_free;
|
|
|
|
for (i = count; i < bufsize; i++)
|
|
|
|
pakfiles[i] = NULL;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
pakfiles[count] = malloc (FNAME_SIZE);
|
2001-10-24 22:50:06 +00:00
|
|
|
if (!pakfiles[count])
|
|
|
|
Sys_Error ("COM_LoadGameDirectory: MemoryAllocationFailure\n");
|
2001-02-26 06:48:02 +00:00
|
|
|
snprintf (pakfiles[count], FNAME_SIZE - 1, "%s/%s", dir,
|
|
|
|
dirent->d_name);
|
2001-02-19 21:15:25 +00:00
|
|
|
pakfiles[count][FNAME_SIZE - 1] = '\0';
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
2001-02-26 06:48:02 +00:00
|
|
|
closedir (dir_ptr);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
// XXX WARNING!!! This is /NOT/ subtable for strcmp!!!!!
|
2001-06-07 17:11:34 +00:00
|
|
|
// This passes 'void **' instead of 'char *' to the cmp function!
|
2001-02-26 06:48:02 +00:00
|
|
|
qsort (pakfiles, count, sizeof (char *),
|
|
|
|
(int (*)(const void *, const void *)) qstrcmp);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
2001-02-26 06:48:02 +00:00
|
|
|
pak = COM_LoadPackFile (pakfiles[i]);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (!pak) {
|
2001-02-26 06:48:02 +00:00
|
|
|
Sys_Error (va ("Bad pakfile %s!!", pakfiles[i]));
|
2001-02-19 21:15:25 +00:00
|
|
|
} else {
|
2001-02-26 06:48:02 +00:00
|
|
|
search = calloc (1, sizeof (searchpath_t));
|
2001-02-19 21:15:25 +00:00
|
|
|
search->pack = pak;
|
|
|
|
search->next = com_searchpaths;
|
|
|
|
com_searchpaths = search;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
COM_LoadGameDirectory_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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
COM_AddDirectory
|
|
|
|
|
|
|
|
Sets com_gamedir, adds the directory to the head of the path,
|
|
|
|
then loads and adds pak1.pak pak2.pak ...
|
|
|
|
*/
|
|
|
|
void
|
2001-07-15 07:04:17 +00:00
|
|
|
COM_AddDirectory (const char *dir)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
searchpath_t *search;
|
|
|
|
char *p;
|
2001-03-09 07:58:10 +00:00
|
|
|
char e_dir[MAX_OSPATH];
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
Qexpand_squiggle (dir, e_dir);
|
|
|
|
dir = e_dir;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if ((p = strrchr (dir, '/')) != NULL) {
|
2001-02-19 21:15:25 +00:00
|
|
|
strcpy (gamedirfile, ++p);
|
|
|
|
strcpy (com_gamedir, dir);
|
|
|
|
} else {
|
|
|
|
strcpy (gamedirfile, dir);
|
2001-02-26 06:48:02 +00:00
|
|
|
strcpy (com_gamedir, va ("%s/%s", fs_userpath->string, dir));
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2001-03-09 07:58:10 +00:00
|
|
|
// add the directory to the search path
|
2001-02-26 06:48:02 +00:00
|
|
|
search = calloc (1, sizeof (searchpath_t));
|
2001-02-19 21:15:25 +00:00
|
|
|
strcpy (search->filename, dir);
|
|
|
|
search->next = com_searchpaths;
|
|
|
|
com_searchpaths = search;
|
|
|
|
|
2001-03-09 07:58:10 +00:00
|
|
|
// add any pak files in the format pak0.pak pak1.pak, ...
|
2001-02-19 21:15:25 +00:00
|
|
|
COM_LoadGameDirectory (dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-07-15 07:04:17 +00:00
|
|
|
COM_AddGameDirectory (const char *dir)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_DPrintf ("COM_AddGameDirectory (\"%s/%s\")\n",
|
2001-02-26 06:48:02 +00:00
|
|
|
fs_sharepath->string, dir);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (strcmp (fs_sharepath->string, fs_userpath->string) != 0)
|
2001-02-26 06:48:02 +00:00
|
|
|
COM_AddDirectory (va ("%s/%s", fs_sharepath->string, dir));
|
|
|
|
COM_AddDirectory (va ("%s/%s", fs_userpath->string, dir));
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-03-09 07:58:10 +00:00
|
|
|
COM_Gamedir
|
|
|
|
|
|
|
|
Sets the gamedir and path to a different directory.
|
2001-02-19 21:15:25 +00:00
|
|
|
*/
|
|
|
|
void
|
2001-07-15 07:04:17 +00:00
|
|
|
COM_Gamedir (const char *dir)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-03-09 07:58:10 +00:00
|
|
|
searchpath_t *next;
|
|
|
|
|
|
|
|
if (strstr (dir, "..") || strstr (dir, "/")
|
|
|
|
|| strstr (dir, "\\") || strstr (dir, ":")) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("Gamedir should be a single filename, not a path\n");
|
2001-03-09 07:58:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp (gamedirfile, dir) == 0)
|
|
|
|
return; // still the same
|
|
|
|
strcpy (gamedirfile, dir);
|
|
|
|
|
|
|
|
// free up any current game dir info
|
|
|
|
while (com_searchpaths != com_base_searchpaths) {
|
|
|
|
if (com_searchpaths->pack) {
|
|
|
|
Qclose (com_searchpaths->pack->handle);
|
|
|
|
free (com_searchpaths->pack->files);
|
|
|
|
free (com_searchpaths->pack);
|
|
|
|
}
|
|
|
|
next = com_searchpaths->next;
|
|
|
|
free (com_searchpaths);
|
|
|
|
com_searchpaths = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// flush all data, so it will be forced to reload
|
|
|
|
Cache_Flush ();
|
|
|
|
|
2001-03-30 00:44:35 +00:00
|
|
|
if (fs_skinbase && strcmp (dir, fs_skinbase->string) == 0)
|
|
|
|
return;
|
|
|
|
|
2001-03-09 07:58:10 +00:00
|
|
|
COM_AddGameDirectory (dir);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-07-15 07:04:17 +00:00
|
|
|
COM_CreateGameDirectory (const char *gamename)
|
2001-02-21 21:44:57 +00:00
|
|
|
{
|
2001-07-31 21:52:52 +00:00
|
|
|
if (strcmp (fs_userpath->string, "."))
|
2001-03-09 07:58:10 +00:00
|
|
|
COM_CreatePath (va ("%s/%s/dummy", fs_userpath->string, gamename));
|
|
|
|
COM_AddGameDirectory (gamename);
|
2001-02-21 21:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-02-26 06:48:02 +00:00
|
|
|
COM_Filesystem_Init (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int i;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-07-17 22:10:20 +00:00
|
|
|
if (!fs_sharepath->string[0]
|
|
|
|
&& !fs_userpath->string[0]
|
|
|
|
&& !fs_basegame->string[0]) {
|
|
|
|
// a util (or a silly user:) are subverting the fs code
|
|
|
|
// add the directory to the search path
|
|
|
|
searchpath_t *search = calloc (1, sizeof (searchpath_t));
|
|
|
|
strcpy (search->filename, "");
|
|
|
|
search->next = com_searchpaths;
|
|
|
|
com_searchpaths = search;
|
|
|
|
com_base_searchpaths = com_searchpaths;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-03-09 07:58:10 +00:00
|
|
|
// start up with basegame->string by default
|
2001-02-19 21:15:25 +00:00
|
|
|
COM_CreateGameDirectory (fs_basegame->string);
|
|
|
|
|
2001-03-30 00:44:35 +00:00
|
|
|
// If we're dealing with id1, use qw too
|
|
|
|
if (fs_skinbase && !strequal (fs_basegame->string, fs_skinbase->string)) {
|
|
|
|
COM_CreateGameDirectory (fs_skinbase->string);
|
|
|
|
}
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
if ((i = COM_CheckParm ("-game")) && i < com_argc - 1) {
|
2001-02-26 06:48:02 +00:00
|
|
|
char *gamedirs = NULL;
|
|
|
|
char *where;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
gamedirs = strdup (com_argv[i + 1]);
|
2001-02-19 21:15:25 +00:00
|
|
|
where = strtok (gamedirs, ",");
|
|
|
|
while (where) {
|
|
|
|
COM_CreateGameDirectory (where);
|
|
|
|
where = strtok (NULL, ",");
|
|
|
|
}
|
|
|
|
free (gamedirs);
|
|
|
|
}
|
|
|
|
// any set gamedirs will be freed up to here
|
|
|
|
com_base_searchpaths = com_searchpaths;
|
|
|
|
}
|
|
|
|
|
2001-03-09 07:58:10 +00:00
|
|
|
void
|
|
|
|
COM_Filesystem_Init_Cvars (void)
|
|
|
|
{
|
2001-04-10 23:39:30 +00:00
|
|
|
fs_sharepath = Cvar_Get ("fs_sharepath", FS_SHAREPATH, CVAR_ROM, NULL,
|
2001-08-22 22:03:16 +00:00
|
|
|
"location of shared (read only) game "
|
|
|
|
"directories");
|
2001-04-10 23:39:30 +00:00
|
|
|
fs_userpath = Cvar_Get ("fs_userpath", FS_USERPATH, CVAR_ROM, NULL,
|
2001-03-09 07:58:10 +00:00
|
|
|
"location of your game directories");
|
2001-04-10 23:39:30 +00:00
|
|
|
fs_basegame = Cvar_Get ("fs_basegame", "id1", CVAR_ROM, NULL,
|
2001-03-09 07:58:10 +00:00
|
|
|
"game to use by default");
|
|
|
|
}
|
|
|
|
|
2001-07-15 07:04:17 +00:00
|
|
|
const char *
|
|
|
|
COM_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;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
2001-07-15 07:04:17 +00:00
|
|
|
COM_StripExtension (const char *in, char *out)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
while (*in && *in != '.')
|
|
|
|
*out++ = *in++;
|
|
|
|
*out = 0;
|
|
|
|
}
|
|
|
|
|
2001-05-19 23:24:57 +00:00
|
|
|
char *
|
2001-02-26 06:48:02 +00:00
|
|
|
COM_FileExtension (char *in)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
static char exten[8];
|
2001-02-26 06:48:02 +00:00
|
|
|
int i;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
while (*in && *in != '.')
|
|
|
|
in++;
|
|
|
|
if (!*in)
|
|
|
|
return "";
|
|
|
|
in++;
|
2001-02-26 06:48:02 +00:00
|
|
|
for (i = 0; i < 7 && *in; i++, in++)
|
2001-02-19 21:15:25 +00:00
|
|
|
exten[i] = *in;
|
|
|
|
exten[i] = 0;
|
|
|
|
return exten;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
COM_DefaultExtension (char *path, char *extension)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
char *src;
|
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
// if path doesn't have a .EXT, append extension
|
|
|
|
// (extension should include the .)
|
2001-02-26 06:48:02 +00:00
|
|
|
src = path + strlen (path) - 1;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
while (*src != '/' && src != path) {
|
2001-02-19 21:15:25 +00:00
|
|
|
if (*src == '.')
|
2001-02-26 06:48:02 +00:00
|
|
|
return; // it has an extension
|
2001-02-19 21:15:25 +00:00
|
|
|
src--;
|
|
|
|
}
|
|
|
|
|
2001-03-09 07:58:10 +00:00
|
|
|
strncat (path, extension, MAX_OSPATH - strlen (path));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
COM_NextFilename (char *filename, const char *prefix, const char *ext)
|
|
|
|
{
|
|
|
|
char *digits;
|
|
|
|
char checkname[MAX_OSPATH];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
strncpy (filename, prefix, MAX_OSPATH - 4);
|
|
|
|
filename[MAX_OSPATH - 4] = 0;
|
|
|
|
digits = filename + strlen (filename);
|
|
|
|
strcat (filename, "000");
|
|
|
|
strncat (filename, ext, MAX_OSPATH - strlen (filename));
|
|
|
|
|
|
|
|
for (i = 0; i <= 999; i++) {
|
|
|
|
digits[0] = i / 100 + '0';
|
|
|
|
digits[1] = i / 10 % 10 + '0';
|
|
|
|
digits[2] = i % 10 + '0';
|
|
|
|
snprintf (checkname, sizeof (checkname), "%s/%s", com_gamedir,
|
|
|
|
filename);
|
|
|
|
if (Sys_FileTime (checkname) == -1)
|
|
|
|
return 1; // file doesn't exist
|
|
|
|
}
|
|
|
|
return 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|