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>
|
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>
|
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"
|
2002-03-12 23:45:36 +00:00
|
|
|
#include "QF/pak.h"
|
2002-07-21 06:11:28 +00:00
|
|
|
#include "QF/pakfile.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.
|
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.
|
|
|
|
*/
|
|
|
|
|
2002-08-25 23:10:57 +00:00
|
|
|
// QUAKE FILESYSTEM
|
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
|
|
|
|
2002-08-25 23:10:57 +00:00
|
|
|
char gamedirfile[MAX_OSPATH];
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
char com_gamedir[MAX_OSPATH];
|
2002-08-25 23:10:57 +00:00
|
|
|
int com_filesize;
|
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
|
|
|
{
|
2002-08-25 23:10:57 +00:00
|
|
|
const char *slash, *dot, *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
|
2002-08-27 07:16:28 +00:00
|
|
|
COM_filelength (QFile *f)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2002-08-25 23:10:57 +00:00
|
|
|
int end, pos;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-08-27 07:16:28 +00:00
|
|
|
COM_FileOpenRead (char *path, QFile **hndl)
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *
|
2002-08-25 23:10:57 +00:00
|
|
|
COM_SafeOpenRead (const char *filename)
|
|
|
|
{
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *f;
|
2002-08-25 23:10:57 +00:00
|
|
|
|
|
|
|
f = Qopen (filename, "rb");
|
|
|
|
|
|
|
|
if (!f)
|
|
|
|
Sys_Error ("Error opening %s: %s", filename, strerror (errno));
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *
|
2002-08-25 23:10:57 +00:00
|
|
|
COM_SafeOpenWrite (const char *filename)
|
|
|
|
{
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *f;
|
2002-08-25 23:10:57 +00:00
|
|
|
|
|
|
|
f = Qopen (filename, "wb");
|
|
|
|
|
|
|
|
if (!f)
|
|
|
|
Sys_Error ("Error opening %s: %s", filename, strerror (errno));
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-08-27 07:16:28 +00:00
|
|
|
COM_SafeRead (QFile *f, void *buffer, int count)
|
2002-08-25 23:10:57 +00:00
|
|
|
{
|
|
|
|
if (Qread (f, buffer, count) != (size_t) count)
|
|
|
|
Sys_Error ("File read failure");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-08-27 07:16:28 +00:00
|
|
|
COM_SafeWrite (QFile *f, const void *buffer, int count)
|
2002-08-25 23:10:57 +00:00
|
|
|
{
|
|
|
|
if (Qwrite (f, buffer, count) != (size_t) count)
|
|
|
|
Sys_Error ("File read failure");
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
LoadFile (const char *filename, void **bufferptr)
|
|
|
|
{
|
|
|
|
int length;
|
|
|
|
void *buffer;
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *f;
|
2002-08-25 23:10:57 +00:00
|
|
|
|
|
|
|
f = COM_SafeOpenRead (filename);
|
|
|
|
length = COM_filelength (f);
|
|
|
|
buffer = malloc (length + 1);
|
|
|
|
SYS_CHECKMEM (buffer);
|
|
|
|
((char *) buffer)[length] = 0;
|
|
|
|
COM_SafeRead (f, buffer, length);
|
|
|
|
Qclose (f);
|
|
|
|
|
|
|
|
*bufferptr = buffer;
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
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-02-26 06:48:02 +00:00
|
|
|
char name[MAX_OSPATH];
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *f;
|
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, ...)
|
|
|
|
{
|
|
|
|
char name[MAX_OSPATH];
|
|
|
|
va_list args;
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *f;
|
2001-03-09 07:58:10 +00:00
|
|
|
|
|
|
|
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-02-26 06:48:02 +00:00
|
|
|
int remaining, count;
|
|
|
|
char buf[4096];
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *in, *out;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
remaining = COM_FileOpenRead (netpath, &in);
|
2002-08-25 23:10:57 +00:00
|
|
|
COM_CreatePath (cachepath); // create directories up to the cache file
|
2001-02-26 06:48:02 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *
|
2001-02-19 21:15:25 +00:00
|
|
|
COM_OpenRead (const char *path, int offs, int len, int zip)
|
|
|
|
{
|
2002-08-25 23:10:57 +00:00
|
|
|
unsigned char id[2], len_bytes[4];
|
2001-02-26 06:48:02 +00:00
|
|
|
int fd = open (path, O_RDONLY);
|
|
|
|
|
|
|
|
if (fd == -1) {
|
2001-02-19 21:15:25 +00:00
|
|
|
Sys_Error ("Couldn't open %s", path);
|
|
|
|
return 0;
|
|
|
|
}
|
2002-07-27 06:42:52 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
setmode (fd, O_BINARY);
|
|
|
|
#endif
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2002-08-20 06:11:10 +00:00
|
|
|
char *
|
|
|
|
COM_CompressPath (const char *pth)
|
|
|
|
{
|
|
|
|
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] == '.'
|
|
|
|
&& (d[2] == '/' || d[2] == '0')) {
|
|
|
|
p += 2 + (p[2] == '/');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
strcpy (d, p + 2 + (p[2] == '/'));
|
|
|
|
p = d;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (p[1] == '/') {
|
|
|
|
strcpy (p, p + 2);
|
|
|
|
continue;
|
|
|
|
} else if (p[1] == 0) {
|
|
|
|
p[0] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (*p && *p != '/')
|
|
|
|
p++;
|
|
|
|
if (*p == '/')
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2002-06-14 20:36:28 +00:00
|
|
|
|
|
|
|
static int
|
2002-08-27 07:16:28 +00:00
|
|
|
open_file (searchpath_t *search, const char *filename, QFile **gzfile,
|
2002-06-14 20:36:28 +00:00
|
|
|
char *foundname, int zip)
|
|
|
|
{
|
|
|
|
char netpath[MAX_OSPATH];
|
|
|
|
|
|
|
|
file_from_pak = 0;
|
|
|
|
|
|
|
|
// is the element a pak file?
|
|
|
|
if (search->pack) {
|
2002-07-21 06:11:28 +00:00
|
|
|
dpackfile_t *packfile;
|
2002-06-14 20:36:28 +00:00
|
|
|
|
2002-07-21 06:11:28 +00:00
|
|
|
packfile = pack_find_file (search->pack, filename);
|
2002-06-14 20:36:28 +00:00
|
|
|
if (packfile) {
|
|
|
|
Sys_DPrintf ("PackFile: %s : %s\n", search->pack->filename,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
} else {
|
2002-08-27 17:53:24 +00:00
|
|
|
// sanity check the strings
|
|
|
|
if (strnlen (search->filename, sizeof (netpath))
|
|
|
|
+ strnlen (filename, sizeof (netpath)) + 2 > sizeof (netpath))
|
|
|
|
Sys_Error ("open_file: search->filename and/or filename "
|
|
|
|
"bogus: `%.*s' `%.*s'",
|
|
|
|
(int) sizeof (netpath), search->filename,
|
|
|
|
(int) sizeof (netpath), filename);
|
2002-06-14 20:36:28 +00:00
|
|
|
// check a file in the directory tree
|
|
|
|
snprintf (netpath, sizeof (netpath), "%s%s%s", search->filename,
|
|
|
|
search->filename[0] ? "/" : "", filename);
|
|
|
|
|
|
|
|
strncpy (foundname, filename, MAX_OSPATH);
|
|
|
|
if (Sys_FileTime (netpath) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
Sys_DPrintf ("FindFile: %s\n", netpath);
|
|
|
|
|
|
|
|
*gzfile = COM_OpenRead (netpath, -1, -1, zip);
|
|
|
|
return com_filesize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
int
|
2002-08-27 07:16:28 +00:00
|
|
|
_COM_FOpenFile (const char *filename, QFile **gzfile, char *foundname, int zip)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
searchpath_t *search;
|
2002-08-20 06:11:10 +00:00
|
|
|
char *path;
|
2002-06-14 20:36:28 +00:00
|
|
|
#ifdef HAVE_VORBIS
|
|
|
|
char oggfilename[MAX_OSPATH];
|
|
|
|
#endif
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2001-02-26 06:48:02 +00:00
|
|
|
char gzfilename[MAX_OSPATH];
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
|
|
|
|
2002-08-20 06:11:10 +00:00
|
|
|
// make sure they're not trying to do weird stuff with our private files
|
|
|
|
path = COM_CompressPath (filename);
|
|
|
|
if (contains_updir(path)) {
|
|
|
|
Sys_Printf ("FindFile: %s: attempt to escape directory tree!\n", path);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2002-06-14 20:36:28 +00:00
|
|
|
#ifdef HAVE_VORBIS
|
2002-08-20 06:11:10 +00:00
|
|
|
if (strequal (".wav", COM_FileExtension (path))) {
|
|
|
|
COM_StripExtension (path, oggfilename);
|
2002-06-14 20:36:28 +00:00
|
|
|
strncat (oggfilename, ".ogg",
|
|
|
|
sizeof (oggfilename) - strlen (oggfilename) - 1);
|
|
|
|
} else {
|
|
|
|
oggfilename[0] = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ZLIB
|
2002-08-20 06:11:10 +00:00
|
|
|
snprintf (gzfilename, sizeof (gzfilename), "%s.gz", path);
|
2002-06-14 20:36:28 +00:00
|
|
|
#endif
|
2001-02-19 21:15:25 +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) {
|
2002-06-14 20:36:28 +00:00
|
|
|
#ifdef HAVE_VORBIS
|
|
|
|
if (oggfilename[0]
|
|
|
|
&& open_file (search, oggfilename, gzfile, foundname, zip) != -1)
|
2002-08-20 06:11:10 +00:00
|
|
|
goto ok;
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
2002-04-30 04:39:03 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2002-06-14 20:36:28 +00:00
|
|
|
if (open_file (search, gzfilename, gzfile, foundname, zip) != -1)
|
2002-08-20 06:11:10 +00:00
|
|
|
goto ok;
|
2002-04-30 04:39:03 +00:00
|
|
|
#endif
|
2002-08-20 06:11:10 +00:00
|
|
|
if (open_file (search, path, gzfile, foundname, zip) != -1)
|
|
|
|
goto ok;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2002-08-20 06:11:10 +00:00
|
|
|
free (path);
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
2002-08-20 06:11:10 +00:00
|
|
|
ok:
|
|
|
|
free(path);
|
|
|
|
return com_filesize;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-08-27 07:16:28 +00:00
|
|
|
COM_FOpenFile (const char *filename, QFile **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.
|
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
|
|
|
*/
|
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
|
|
|
{
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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)
|
|
|
|
{
|
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)
|
|
|
|
Sys_Printf ("Added packfile %s (%i files)\n",
|
|
|
|
packfile, pack->numfiles);
|
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;
|
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2002-03-20 22:57:26 +00:00
|
|
|
goto COM_LoadGameDirectory_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)
|
|
|
|
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])
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("COM_LoadGameDirectory: MemoryAllocationFailure");
|
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;
|
|
|
|
}
|
|
|
|
|
2002-06-14 20:36:28 +00:00
|
|
|
const char *
|
|
|
|
COM_FileExtension (const char *in)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
while (*in && *in != '.')
|
|
|
|
in++;
|
2002-06-14 20:36:28 +00:00
|
|
|
return in;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|