quake2forge/qcommon/files.c

933 lines
18 KiB
C
Raw Normal View History

2001-12-22 04:27:19 +00:00
/*
Copyright (C) 1997-2001 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 the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
2001-12-22 04:27:19 +00:00
#include "qcommon.h"
// define this to dissalow any data but the demo pak file
//#define NO_ADDONS
// if a packfile directory differs from this, it is assumed to be hacked
// Full version
#define PAK0_CHECKSUM 0x40e614e0
// Demo
//#define PAK0_CHECKSUM 0xb2c6d7ea
// OEM
//#define PAK0_CHECKSUM 0x78e135c
/*
=============================================================================
QUAKE FILESYSTEM
=============================================================================
*/
//
// in memory
//
typedef struct
{
char name[MAX_QPATH];
int filepos, filelen;
} packfile_t;
typedef struct pack_s
{
char filename[MAX_OSPATH];
2002-01-03 20:09:12 +00:00
QFile *handle;
2001-12-22 04:27:19 +00:00
int numfiles;
packfile_t *files;
} pack_t;
char fs_gamedir[MAX_OSPATH];
cvar_t *fs_basedir;
cvar_t *fs_cddir;
cvar_t *fs_gamedirvar;
typedef struct filelink_s
{
struct filelink_s *next;
char *from;
int fromlength;
char *to;
} filelink_t;
filelink_t *fs_links;
typedef struct searchpath_s
{
char filename[MAX_OSPATH];
pack_t *pack; // only one of filename / pack will be used
struct searchpath_s *next;
} searchpath_t;
searchpath_t *fs_searchpaths;
searchpath_t *fs_base_searchpaths; // without gamedirs
/*
All of Quake's data access is through a hierchal file system, but the contents of the file system can be transparently merged from several sources.
The "base directory" is the path to the directory holding the quake.exe and all game directories. The sys_* files pass this to host_init in quakeparms_t->basedir. This can be overridden with the "-basedir" command line parm to allow code debugging in a different directory. 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.
*/
/*
================
FS_filelength
================
*/
2002-01-03 20:09:12 +00:00
int FS_filelength (QFile *f)
2001-12-22 04:27:19 +00:00
{
int pos;
int end;
2002-01-03 20:09:12 +00:00
pos = Qtell (f);
Qseek (f, 0, SEEK_END);
end = Qtell (f);
Qseek (f, pos, SEEK_SET);
2001-12-22 04:27:19 +00:00
return end;
}
/*
============
FS_CreatePath
Creates any directories needed to store the given filename
============
*/
void FS_CreatePath (char *path)
{
char *ofs;
for (ofs = path+1 ; *ofs ; ofs++)
{
if (*ofs == '/')
{ // create the directory
*ofs = 0;
Sys_Mkdir (path);
*ofs = '/';
}
}
}
/*
==============
FS_FCloseFile
2002-01-03 20:09:12 +00:00
For some reason, other dll's can't just cal Qclose()
2001-12-22 04:27:19 +00:00
on files returned by FS_FOpenFile...
==============
*/
2002-01-03 20:09:12 +00:00
void FS_FCloseFile (QFile *f)
2001-12-22 04:27:19 +00:00
{
2002-01-03 20:09:12 +00:00
Qclose (f);
2001-12-22 04:27:19 +00:00
}
// RAFAEL
/*
Developer_searchpath
*/
int Developer_searchpath (int who)
{
int ch;
// PMM - warning removal
// char *start;
searchpath_t *search;
if (who == 1) // xatrix
ch = 'x';
else if (who == 2)
ch = 'r';
for (search = fs_searchpaths ; search ; search = search->next)
{
if (strstr (search->filename, "xatrix"))
return 1;
if (strstr (search->filename, "rogue"))
return 2;
/*
start = strchr (search->filename, ch);
if (start == NULL)
continue;
if (strcmp (start ,"xatrix") == 0)
return (1);
*/
}
return (0);
}
QFile *
FS_OpenRead (const char *path, int offs, int len, int zip, int *size)
{
int fd = open (path, O_RDONLY);
unsigned char id[2];
unsigned char len_bytes[4];
if (fd == -1)
return 0;
if (offs < 0 || len < 0) {
// normal file
offs = 0;
len = lseek (fd, 0, SEEK_END);
lseek (fd, 0, SEEK_SET);
}
lseek (fd, offs, SEEK_SET);
if (zip) {
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]));
}
}
lseek (fd, offs, SEEK_SET);
*size = len;
#ifdef WIN32
setmode (fd, O_BINARY);
#endif
if (zip)
return Qdopen (fd, "rbz");
else
return Qdopen (fd, "rb");
}
2001-12-22 04:27:19 +00:00
/*
===========
FS_FOpenFile
Finds the file in the search path.
2002-01-03 20:09:12 +00:00
returns filesize and an open QFile *
2001-12-22 04:27:19 +00:00
Used for streaming data out of either a pak file or
a seperate file.
===========
*/
int file_from_pak = 0;
int _FS_FOpenFile (char *filename, QFile **file, char *foundname, int zip)
2001-12-22 04:27:19 +00:00
{
searchpath_t *search;
char netpath[MAX_OSPATH];
pack_t *pak;
int i;
filelink_t *link;
int size;
#ifdef HAVE_ZLIB
char gzfilename[MAX_OSPATH];
int filenamelen;
filenamelen = strlen (filename);
strncpy (gzfilename, filename, sizeof (gzfilename));
strncat (gzfilename, ".gz", sizeof (gzfilename) - strlen (gzfilename));
#endif
2001-12-22 04:27:19 +00:00
file_from_pak = 0;
// check for links first
for (link = fs_links ; link ; link=link->next)
{
if (!strncmp (filename, link->from, link->fromlength))
{
Com_sprintf (netpath, sizeof(netpath), "%s%s",link->to, filename+link->fromlength);
*file = FS_OpenRead (netpath, -1, -1, zip, &size);
#ifdef HAVE_ZLIB
if (!*file) {
Com_sprintf (netpath, sizeof(netpath), "%s%s.gz",link->to, filename+link->fromlength);
*file = FS_OpenRead (netpath, -1, -1, zip, &size);
}
#endif
2001-12-22 04:27:19 +00:00
if (*file)
{
Com_DPrintf ("link file: %s\n",netpath);
return size;
2001-12-22 04:27:19 +00:00
}
return -1;
}
}
//
// search through the path, one element at a time
//
for (search = fs_searchpaths ; search ; search = search->next)
{
// is the element a pak file?
if (search->pack)
{
// look through all the pak file elements
pak = search->pack;
for (i=0 ; i<pak->numfiles ; i++)
if (!Q_strcasecmp (pak->files[i].name, filename)
#ifdef HAVE_ZLIB
|| !Q_strcasecmp (pak->files[i].name, gzfilename)
#endif
)
2001-12-22 04:27:19 +00:00
{ // found it!
file_from_pak = 1;
Com_DPrintf ("PackFile: %s : %s\n",pak->filename, filename);
// open a new file on the pakfile
strncpy (foundname, pak->files[i].name, MAX_OSPATH);
*file = FS_OpenRead (pak->files[i].name,
pak->files[i].filepos,
pak->files[i].filelen, zip, &size);
2001-12-22 04:27:19 +00:00
if (!*file)
Com_Error (ERR_FATAL, "Couldn't reopen %s", pak->filename);
return size;
2001-12-22 04:27:19 +00:00
}
}
else
{
// check a file in the directory tree
Com_sprintf (netpath, sizeof(netpath), "%s/%s",search->filename, filename);
*file = FS_OpenRead (netpath, -1, -1, zip, &size);
#ifdef HAVE_ZLIB
if (!*file) {
Com_sprintf (netpath, sizeof(netpath), "%s/%s.gz",search->filename, filename);
*file = FS_OpenRead (netpath, -1, -1, zip, &size);
}
#endif
2001-12-22 04:27:19 +00:00
if (!*file)
continue;
Com_DPrintf ("FindFile: %s\n",netpath);
return size;
2001-12-22 04:27:19 +00:00
}
}
Com_DPrintf ("FindFile: can't find %s\n", filename);
*file = NULL;
return -1;
}
int
FS_FOpenFile (char *filename, QFile **gzfile)
2001-12-22 04:27:19 +00:00
{
char foundname[MAX_OSPATH];
2001-12-22 04:27:19 +00:00
return _FS_FOpenFile (filename, gzfile, foundname, 1);
2001-12-22 04:27:19 +00:00
}
/*
=================
FS_ReadFile
Properly handles partial reads
=================
*/
void CDAudio_Stop(void);
#define MAX_READ 0x10000 // read in blocks of 64k
2002-01-03 20:09:12 +00:00
void FS_Read (void *buffer, int len, QFile *f)
2001-12-22 04:27:19 +00:00
{
int block, remaining;
int read;
byte *buf;
int tries;
buf = (byte *)buffer;
// read in chunks for progress bar
remaining = len;
tries = 0;
while (remaining)
{
block = remaining;
if (block > MAX_READ)
block = MAX_READ;
2002-01-03 20:09:12 +00:00
read = Qread (f, buf, block);
2001-12-22 04:27:19 +00:00
if (read == 0)
{
// we might have been trying to read from a CD
if (!tries)
{
tries = 1;
CDAudio_Stop();
}
else
Com_Error (ERR_FATAL, "FS_Read: 0 bytes read");
}
if (read == -1)
Com_Error (ERR_FATAL, "FS_Read: -1 bytes read");
// do some progress bar thing here...
remaining -= read;
buf += read;
}
}
/*
============
FS_LoadFile
Filename are reletive to the quake search path
a null buffer will just return the file length without loading
============
*/
int FS_LoadFile (char *path, void **buffer)
{
2002-01-03 20:09:12 +00:00
QFile *h;
2001-12-22 04:27:19 +00:00
byte *buf;
int len;
buf = NULL; // quiet compiler warning
// look for it in the filesystem or pack files
len = FS_FOpenFile (path, &h);
if (!h)
{
if (buffer)
*buffer = NULL;
return -1;
}
if (!buffer)
{
2002-01-03 20:09:12 +00:00
Qclose (h);
2001-12-22 04:27:19 +00:00
return len;
}
buf = Z_Malloc(len);
*buffer = buf;
FS_Read (buf, len, h);
2002-01-03 20:09:12 +00:00
Qclose (h);
2001-12-22 04:27:19 +00:00
return len;
}
/*
=============
FS_FreeFile
=============
*/
void FS_FreeFile (void *buffer)
{
Z_Free (buffer);
}
/*
=================
FS_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.
=================
*/
pack_t *FS_LoadPackFile (char *packfile)
{
dpackheader_t header;
int i;
packfile_t *newfiles;
int numpackfiles;
pack_t *pack;
2002-01-03 20:09:12 +00:00
QFile *packhandle;
2001-12-22 04:27:19 +00:00
dpackfile_t info[MAX_FILES_IN_PACK];
unsigned checksum;
2002-01-03 20:09:12 +00:00
packhandle = Qopen(packfile, "rb");
2001-12-22 04:27:19 +00:00
if (!packhandle)
return NULL;
2002-01-03 20:09:12 +00:00
Qread (packhandle, &header, sizeof(header));
2001-12-22 04:27:19 +00:00
if (LittleLong(header.ident) != IDPAKHEADER)
Com_Error (ERR_FATAL, "%s is not a packfile", packfile);
header.dirofs = LittleLong (header.dirofs);
header.dirlen = LittleLong (header.dirlen);
numpackfiles = header.dirlen / sizeof(dpackfile_t);
if (numpackfiles > MAX_FILES_IN_PACK)
Com_Error (ERR_FATAL, "%s has %i files", packfile, numpackfiles);
newfiles = Z_Malloc (numpackfiles * sizeof(packfile_t));
2002-01-03 20:09:12 +00:00
Qseek (packhandle, header.dirofs, SEEK_SET);
Qread (packhandle, info, header.dirlen);
2001-12-22 04:27:19 +00:00
// crc the directory to check for modifications
checksum = Com_BlockChecksum ((void *)info, header.dirlen);
#ifdef NO_ADDONS
if (checksum != PAK0_CHECKSUM)
return NULL;
#endif
// parse the directory
for (i=0 ; i<numpackfiles ; i++)
{
strcpy (newfiles[i].name, info[i].name);
newfiles[i].filepos = LittleLong(info[i].filepos);
newfiles[i].filelen = LittleLong(info[i].filelen);
}
pack = Z_Malloc (sizeof (pack_t));
strcpy (pack->filename, packfile);
pack->handle = packhandle;
pack->numfiles = numpackfiles;
pack->files = newfiles;
Com_Printf ("Added packfile %s (%i files)\n", packfile, numpackfiles);
return pack;
}
/*
================
FS_AddGameDirectory
Adds the directory to the head of the path,
2001-12-22 04:27:19 +00:00
then loads and adds pak1.pak pak2.pak ...
================
*/
void FS_AddGameDirectory (char *dir)
{
int i;
searchpath_t *search;
pack_t *pak;
char pakfile[MAX_OSPATH];
//
// add the base directory to the search path
2001-12-22 04:27:19 +00:00
//
search = Z_Malloc (sizeof(searchpath_t));
strcpy (search->filename, dir);
search->next = fs_searchpaths;
fs_searchpaths = search;
//
// add any pak files in the format pak0.pak pak1.pak, ...
//
for (i=0; i<10; i++)
{
Com_sprintf (pakfile, sizeof(pakfile), "%s/pak%i.pak", dir, i);
pak = FS_LoadPackFile (pakfile);
if (!pak)
continue;
search = Z_Malloc (sizeof(searchpath_t));
search->pack = pak;
search->next = fs_searchpaths;
fs_searchpaths = search;
}
}
/*
============
FS_Gamedir
Called to find where to write a file (demos, savegames, etc)
============
*/
char *FS_Gamedir (void)
{
return fs_gamedir;
2001-12-22 04:27:19 +00:00
}
/*
=============
FS_ExecAutoexec
=============
*/
void FS_ExecAutoexec (void)
{
char *dir;
char name [MAX_QPATH];
char command [MAX_QPATH];
char newpath [MAX_QPATH];
searchpath_t *s;
2001-12-22 04:27:19 +00:00
dir = Cvar_VariableString("gamedir");
// search through all the paths for an autoexec.cfg file
for (s = fs_searchpaths ; s ; s = s->next)
{
if (s->pack)
continue;
strcpy (newpath, s->filename);
// eat the last '/'
if (newpath[strlen(newpath) - 1] == '/')
newpath[strlen(newpath) - 1] = '\0';
if (*dir)
{
// skip when current search path and gamedir are equal
if(!strncmp(dir, newpath + 2, strlen(dir)))
Com_sprintf(name, sizeof(name), "%s/autoexec.cfg", newpath);
else
Com_sprintf(name, sizeof(name), "%s/%s/autoexec.cfg", newpath, dir);
}
else
Com_sprintf(name, sizeof(name), "%s/autoexec.cfg", newpath);
if (Sys_FindFirst(name, 0, SFF_SUBDIR | SFF_HIDDEN | SFF_SYSTEM))
{
strcpy (command, "exec ");
strcat (command, name);
Cbuf_AddText (command);
Sys_FindClose();
break;
}
Sys_FindClose();
}
2001-12-22 04:27:19 +00:00
}
/*
================
FS_SetGamedir
Sets the gamedir and path to a different directory.
2001-12-22 04:27:19 +00:00
================
*/
void FS_SetGamedir (char *dir)
{
searchpath_t *next;
if (strstr(dir, "..") || strstr(dir, "/")
|| strstr(dir, "\\") || strstr(dir, ":") )
{
Com_Printf ("Gamedir should be a single filename, not a path\n");
return;
}
//
// free up any current game dir info
//
while (fs_searchpaths != fs_base_searchpaths)
{
if (fs_searchpaths->pack)
{
2002-01-03 20:09:12 +00:00
Qclose (fs_searchpaths->pack->handle);
2001-12-22 04:27:19 +00:00
Z_Free (fs_searchpaths->pack->files);
Z_Free (fs_searchpaths->pack);
}
next = fs_searchpaths->next;
Z_Free (fs_searchpaths);
fs_searchpaths = next;
}
//
// flush all data, so it will be forced to reload
//
if (dedicated && !dedicated->value)
Cbuf_AddText ("vid_restart\nsnd_restart\n");
Com_sprintf (fs_gamedir, sizeof(fs_gamedir), "%s/%s", fs_basedir->string, dir);
// now add new entries for
2001-12-22 04:27:19 +00:00
if (!strcmp(dir,BASEDIRNAME) || (*dir == 0))
{
Cvar_FullSet ("gamedir", "", CVAR_SERVERINFO|CVAR_NOSET);
Cvar_FullSet ("game", "", CVAR_LATCH|CVAR_SERVERINFO);
}
else
{
Cvar_FullSet ("gamedir", dir, CVAR_SERVERINFO|CVAR_NOSET);
if (fs_cddir->string[0])
FS_AddGameDirectory (va("%s/%s", fs_cddir->string, dir) );
FS_AddGameDirectory (va("%s/%s", fs_basedir->string, dir) );
}
}
/*
================
FS_Link_f
Creates a filelink_t
================
*/
void FS_Link_f (void)
{
filelink_t *l, **prev;
if (Cmd_Argc() != 3)
{
Com_Printf ("USAGE: link <from> <to>\n");
return;
}
// see if the link already exists
prev = &fs_links;
for (l=fs_links ; l ; l=l->next)
{
if (!strcmp (l->from, Cmd_Argv(1)))
{
Z_Free (l->to);
if (!strlen(Cmd_Argv(2)))
{ // delete it
*prev = l->next;
Z_Free (l->from);
Z_Free (l);
return;
}
l->to = CopyString (Cmd_Argv(2));
return;
}
prev = &l->next;
}
// create a new link
l = Z_Malloc(sizeof(*l));
l->next = fs_links;
fs_links = l;
l->from = CopyString(Cmd_Argv(1));
l->fromlength = strlen(l->from);
l->to = CopyString(Cmd_Argv(2));
}
/*
** FS_ListFiles
*/
char **FS_ListFiles( char *findname, int *numfiles, unsigned musthave, unsigned canthave )
{
char *s;
int nfiles = 0;
char **list = 0;
s = Sys_FindFirst( findname, musthave, canthave );
while ( s )
{
if ( s[strlen(s)-1] != '.' )
nfiles++;
s = Sys_FindNext( musthave, canthave );
}
Sys_FindClose ();
if ( !nfiles )
return NULL;
nfiles++; // add space for a guard
*numfiles = nfiles;
list = malloc( sizeof( char * ) * nfiles );
memset( list, 0, sizeof( char * ) * nfiles );
s = Sys_FindFirst( findname, musthave, canthave );
nfiles = 0;
while ( s )
{
if ( s[strlen(s)-1] != '.' )
{
list[nfiles] = strdup( s );
#ifdef _WIN32
strlwr( list[nfiles] );
#endif
nfiles++;
}
s = Sys_FindNext( musthave, canthave );
}
Sys_FindClose ();
return list;
}
/*
** FS_Dir_f
*/
void FS_Dir_f( void )
{
char *path = NULL;
char findname[1024];
char wildcard[1024] = "*.*";
char **dirnames;
int ndirs;
if ( Cmd_Argc() != 1 )
{
strcpy( wildcard, Cmd_Argv( 1 ) );
}
while ( ( path = FS_NextPath( path ) ) != NULL )
{
char *tmp = findname;
Com_sprintf( findname, sizeof(findname), "%s/%s", path, wildcard );
while ( *tmp != 0 )
{
if ( *tmp == '\\' )
*tmp = '/';
tmp++;
}
Com_Printf( "Directory of %s\n", findname );
Com_Printf( "----\n" );
if ( ( dirnames = FS_ListFiles( findname, &ndirs, 0, 0 ) ) != 0 )
{
int i;
for ( i = 0; i < ndirs-1; i++ )
{
if ( strrchr( dirnames[i], '/' ) )
Com_Printf( "%s\n", strrchr( dirnames[i], '/' ) + 1 );
else
Com_Printf( "%s\n", dirnames[i] );
free( dirnames[i] );
}
free( dirnames );
}
Com_Printf( "\n" );
};
}
/*
============
FS_Path_f
============
*/
void FS_Path_f (void)
{
searchpath_t *s;
filelink_t *l;
Com_Printf ("Current search path:\n");
for (s=fs_searchpaths ; s ; s=s->next)
{
if (s == fs_base_searchpaths)
Com_Printf ("----------\n");
if (s->pack)
Com_Printf ("%s (%i files)\n", s->pack->filename, s->pack->numfiles);
else
Com_Printf ("%s\n", s->filename);
}
Com_Printf ("\nLinks:\n");
for (l=fs_links ; l ; l=l->next)
Com_Printf ("%s : %s\n", l->from, l->to);
}
/*
================
FS_NextPath
Allows enumerating all of the directories in the search path
================
*/
char *FS_NextPath (char *prevpath)
{
searchpath_t *s;
char *prev;
if (!prevpath)
return fs_gamedir;
prev = fs_gamedir;
for (s=fs_searchpaths ; s ; s=s->next)
{
if (s->pack)
continue;
if (prevpath == prev)
return s->filename;
prev = s->filename;
}
return NULL;
}
/*
================
FS_InitFilesystem
================
*/
void FS_InitFilesystem (void)
{
Cmd_AddCommand ("path", FS_Path_f);
Cmd_AddCommand ("link", FS_Link_f);
Cmd_AddCommand ("dir", FS_Dir_f );
//
// basedir <path>
// allows the game to run from outside the data tree
//
fs_basedir = Cvar_Get ("basedir", ".", CVAR_NOSET);
//
// cddir <path>
// Logically concatenates the cddir after the basedir for
// allows the game to run from outside the data tree
//
fs_cddir = Cvar_Get ("cddir", "", CVAR_NOSET);
if (fs_cddir->string[0])
FS_AddGameDirectory (va("%s/"BASEDIRNAME, fs_cddir->string) );
//
// add baseq2 to search path
2001-12-22 04:27:19 +00:00
//
FS_AddGameDirectory (va("%s/"BASEDIRNAME, fs_basedir->string) );
//
// then add a '.quake2/baseq2' directory in home directory by default
//
strcpy (fs_gamedir, getenv("HOME"));
strcat (fs_gamedir, "/.quake2/baseq2/");
FS_CreatePath (fs_gamedir);
FS_AddGameDirectory (fs_gamedir);
2001-12-22 04:27:19 +00:00
// any set gamedirs will be freed up to here
fs_base_searchpaths = fs_searchpaths;
// check for game override
fs_gamedirvar = Cvar_Get ("game", "", CVAR_LATCH|CVAR_SERVERINFO);
if (fs_gamedirvar->string[0])
FS_SetGamedir (fs_gamedirvar->string);
}