📄 files.c
字号:
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena source code 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.
Quake III Arena source code 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 Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
/*****************************************************************************
* name: files.c
*
* desc: handle based filesystem for Quake III Arena
*
* $Archive: /MissionPack/code/qcommon/files.c $
*
*****************************************************************************/
#include "../game/q_shared.h"
#include "qcommon.h"
#include "unzip.h"
/*
=============================================================================
QUAKE3 FILESYSTEM
All of Quake's data access is through a hierarchical file system, but the contents of
the file system can be transparently merged from several sources.
A "qpath" is a reference to game file data. MAX_ZPATH is 256 characters, which must include
a terminating zero. "..", "\\", and ":" are explicitly illegal in qpaths to prevent any
references outside the quake directory system.
The "base path" is the path to the directory holding all the game directories and usually
the executable. It defaults to ".", but can be overridden with a "+set fs_basepath c:\quake3"
command line to allow code debugging in a different directory. Basepath cannot
be modified at all after startup. Any files that are created (demos, screenshots,
etc) will be created reletive to the base path, so base path should usually be writable.
The "cd path" is the path to an alternate hierarchy that will be searched if a file
is not located in the base path. A user can do a partial install that copies some
data to a base path created on their hard drive and leave the rest on the cd. Files
are never writen to the cd path. It defaults to a value set by the installer, like
"e:\quake3", but it can be overridden with "+set ds_cdpath g:\quake3".
If a user runs the game directly from a CD, the base path would be on the CD. This
should still function correctly, but all file writes will fail (harmlessly).
The "home path" is the path used for all write access. On win32 systems we have "base path"
== "home path", but on *nix systems the base installation is usually readonly, and
"home path" points to ~/.q3a or similar
The user can also install custom mods and content in "home path", so it should be searched
along with "home path" and "cd path" for game content.
The "base game" is the directory under the paths where data comes from by default, and
can be either "baseq3" or "demoq3".
The "current game" may be the same as the base game, or it may be the name of another
directory under the paths that should be searched for files before looking in the base game.
This is the basis for addons.
Clients automatically set the game directory after receiving a gamestate from a server,
so only servers need to worry about +set fs_game.
No other directories outside of the base game and current game will ever be referenced by
filesystem functions.
To save disk space and speed loading, directory trees can be collapsed into zip files.
The files use a ".pk3" extension to prevent users from unzipping them accidentally, but
otherwise the are simply normal uncompressed zip files. A game directory can have multiple
zip files of the form "pak0.pk3", "pak1.pk3", etc. Zip files are searched in decending order
from the highest number to the lowest, and will always take precedence over the filesystem.
This allows a pk3 distributed as a patch to override all existing data.
Because we will have updated executables freely available online, there is no point to
trying to restrict demo / oem versions of the game with code changes. Demo / oem versions
should be exactly the same executables as release versions, but with different data that
automatically restricts where game media can come from to prevent add-ons from working.
After the paths are initialized, quake will look for the product.txt file. If not
found and verified, the game will run in restricted mode. In restricted mode, only
files contained in demoq3/pak0.pk3 will be available for loading, and only if the zip header is
verified to not have been modified. A single exception is made for q3config.cfg. Files
can still be written out in restricted mode, so screenshots and demos are allowed.
Restricted mode can be tested by setting "+set fs_restrict 1" on the command line, even
if there is a valid product.txt under the basepath or cdpath.
If not running in restricted mode, and a file is not found in any local filesystem,
an attempt will be made to download it and save it under the base path.
If the "fs_copyfiles" cvar is set to 1, then every time a file is sourced from the cd
path, it will be copied over to the base path. This is a development aid to help build
test releases and to copy working sets over slow network links.
File search order: when FS_FOpenFileRead gets called it will go through the fs_searchpaths
structure and stop on the first successful hit. fs_searchpaths is built with successive
calls to FS_AddGameDirectory
Additionaly, we search in several subdirectories:
current game is the current mode
base game is a variable to allow mods based on other mods
(such as baseq3 + missionpack content combination in a mod for instance)
BASEGAME is the hardcoded base game ("baseq3")
e.g. the qpath "sound/newstuff/test.wav" would be searched for in the following places:
home path + current game's zip files
home path + current game's directory
base path + current game's zip files
base path + current game's directory
cd path + current game's zip files
cd path + current game's directory
home path + base game's zip file
home path + base game's directory
base path + base game's zip file
base path + base game's directory
cd path + base game's zip file
cd path + base game's directory
home path + BASEGAME's zip file
home path + BASEGAME's directory
base path + BASEGAME's zip file
base path + BASEGAME's directory
cd path + BASEGAME's zip file
cd path + BASEGAME's directory
server download, to be written to home path + current game's directory
The filesystem can be safely shutdown and reinitialized with different
basedir / cddir / game combinations, but all other subsystems that rely on it
(sound, video) must also be forced to restart.
Because the same files are loaded by both the clip model (CM_) and renderer (TR_)
subsystems, a simple single-file caching scheme is used. The CM_ subsystems will
load the file with a request to cache. Only one file will be kept cached at a time,
so any models that are going to be referenced by both subsystems should alternate
between the CM_ load function and the ref load function.
TODO: A qpath that starts with a leading slash will always refer to the base game, even if another
game is currently active. This allows character models, skins, and sounds to be downloaded
to a common directory no matter which game is active.
How to prevent downloading zip files?
Pass pk3 file names in systeminfo, and download before FS_Restart()?
Aborting a download disconnects the client from the server.
How to mark files as downloadable? Commercial add-ons won't be downloadable.
Non-commercial downloads will want to download the entire zip file.
the game would have to be reset to actually read the zip in
Auto-update information
Path separators
Casing
separate server gamedir and client gamedir, so if the user starts
a local game after having connected to a network game, it won't stick
with the network game.
allow menu options for game selection?
Read / write config to floppy option.
Different version coexistance?
When building a pak file, make sure a q3config.cfg isn't present in it,
or configs will never get loaded from disk!
todo:
downloading (outside fs?)
game directory passing and restarting
=============================================================================
*/
#define DEMOGAME "demota"
// every time a new demo pk3 file is built, this checksum must be updated.
// the easiest way to get it is to just run the game and see what it spits out
#define DEMO_PAK_CHECKSUM 437558517u
// if this is defined, the executable positively won't work with any paks other
// than the demo pak, even if productid is present. This is only used for our
// last demo release to prevent the mac and linux users from using the demo
// executable with the production windows pak before the mac/linux products
// hit the shelves a little later
// NOW defined in build files
//#define PRE_RELEASE_TADEMO
#define MAX_ZPATH 256
#define MAX_SEARCH_PATHS 4096
#define MAX_FILEHASH_SIZE 1024
typedef struct fileInPack_s {
char *name; // name of the file
unsigned long pos; // file info position in zip
struct fileInPack_s* next; // next file in the hash
} fileInPack_t;
typedef struct {
char pakFilename[MAX_OSPATH]; // c:\quake3\baseq3\pak0.pk3
char pakBasename[MAX_OSPATH]; // pak0
char pakGamename[MAX_OSPATH]; // baseq3
unzFile handle; // handle to zip file
int checksum; // regular checksum
int pure_checksum; // checksum for pure
int numfiles; // number of files in pk3
int referenced; // referenced file flags
int hashSize; // hash table size (power of 2)
fileInPack_t* *hashTable; // hash table
fileInPack_t* buildBuffer; // buffer with the filenames etc.
} pack_t;
typedef struct {
char path[MAX_OSPATH]; // c:\quake3
char gamedir[MAX_OSPATH]; // baseq3
} directory_t;
typedef struct searchpath_s {
struct searchpath_s *next;
pack_t *pack; // only one of pack / dir will be non NULL
directory_t *dir;
} searchpath_t;
static char fs_gamedir[MAX_OSPATH]; // this will be a single file name with no separators
static cvar_t *fs_debug;
static cvar_t *fs_homepath;
static cvar_t *fs_basepath;
static cvar_t *fs_basegame;
static cvar_t *fs_cdpath;
static cvar_t *fs_copyfiles;
static cvar_t *fs_gamedirvar;
static cvar_t *fs_restrict;
static searchpath_t *fs_searchpaths;
static int fs_readCount; // total bytes read
static int fs_loadCount; // total files read
static int fs_loadStack; // total files in memory
static int fs_packFiles; // total number of files in packs
static int fs_fakeChkSum;
static int fs_checksumFeed;
typedef union qfile_gus {
FILE* o;
unzFile z;
} qfile_gut;
typedef struct qfile_us {
qfile_gut file;
qboolean unique;
} qfile_ut;
typedef struct {
qfile_ut handleFiles;
qboolean handleSync;
int baseOffset;
int fileSize;
int zipFilePos;
qboolean zipFile;
qboolean streamed;
char name[MAX_ZPATH];
} fileHandleData_t;
static fileHandleData_t fsh[MAX_FILE_HANDLES];
// TTimo - https://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=540
// wether we did a reorder on the current search path when joining the server
static qboolean fs_reordered;
// never load anything from pk3 files that are not present at the server when pure
static int fs_numServerPaks;
static int fs_serverPaks[MAX_SEARCH_PATHS]; // checksums
static char *fs_serverPakNames[MAX_SEARCH_PATHS]; // pk3 names
// only used for autodownload, to make sure the client has at least
// all the pk3 files that are referenced at the server side
static int fs_numServerReferencedPaks;
static int fs_serverReferencedPaks[MAX_SEARCH_PATHS]; // checksums
static char *fs_serverReferencedPakNames[MAX_SEARCH_PATHS]; // pk3 names
// last valid game folder used
char lastValidBase[MAX_OSPATH];
char lastValidGame[MAX_OSPATH];
// productId: This file is copyright 1999 Id Software, and may not be duplicated except during a licensed installation of the full commercial version of Quake 3:Arena
static byte fs_scrambledProductId[152] = {
220, 129, 255, 108, 244, 163, 171, 55, 133, 65, 199, 36, 140, 222, 53, 99, 65, 171, 175, 232, 236, 193, 210, 250, 169, 104, 231, 231, 21, 201, 170, 208, 135, 175, 130, 136, 85, 215, 71, 23, 96, 32, 96, 83, 44, 240, 219, 138, 184, 215, 73, 27, 196, 247, 55, 139, 148, 68, 78, 203, 213, 238, 139, 23, 45, 205, 118, 186, 236, 230, 231, 107, 212, 1, 10, 98, 30, 20, 116, 180, 216, 248, 166, 35, 45, 22, 215, 229, 35, 116, 250, 167, 117, 3, 57, 55, 201, 229, 218, 222, 128, 12, 141, 149, 32, 110, 168, 215, 184, 53, 31, 147, 62, 12, 138, 67, 132, 54, 125, 6, 221, 148, 140, 4, 21, 44, 198, 3, 126, 12, 100, 236, 61, 42, 44, 251, 15, 135, 14, 134, 89, 92, 177, 246, 152, 106, 124, 78, 118, 80, 28, 42
};
#ifdef FS_MISSING
FILE* missingFiles = NULL;
#endif
/*
==============
FS_Initialized
==============
*/
qboolean FS_Initialized() {
return (fs_searchpaths != NULL);
}
/*
=================
FS_PakIsPure
=================
*/
qboolean FS_PakIsPure( pack_t *pack ) {
int i;
if ( fs_numServerPaks ) {
for ( i = 0 ; i < fs_numServerPaks ; i++ ) {
// FIXME: also use hashed file names
// NOTE TTimo: a pk3 with same checksum but different name would be validated too
// I don't see this as allowing for any exploit, it would only happen if the client does manips of it's file names 'not a bug'
if ( pack->checksum == fs_serverPaks[i] ) {
return qtrue; // on the aproved list
}
}
return qfalse; // not on the pure server pak list
}
return qtrue;
}
/*
=================
FS_LoadStack
return load stack
=================
*/
int FS_LoadStack()
{
return fs_loadStack;
}
/*
================
return a hash value for the filename
================
*/
static long FS_HashFileName( const char *fname, int hashSize ) {
int i;
long hash;
char letter;
hash = 0;
i = 0;
while (fname[i] != '\0') {
letter = tolower(fname[i]);
if (letter =='.') break; // don't include extension
if (letter =='\\') letter = '/'; // damn path names
if (letter == PATH_SEP) letter = '/'; // damn path names
hash+=(long)(letter)*(i+119);
i++;
}
hash = (hash ^ (hash >> 10) ^ (hash >> 20));
hash &= (hashSize-1);
return hash;
}
static fileHandle_t FS_HandleForFile(void) {
int i;
for ( i = 1 ; i < MAX_FILE_HANDLES ; i++ ) {
if ( fsh[i].handleFiles.file.o == NULL ) {
return i;
}
}
Com_Error( ERR_DROP, "FS_HandleForFile: none free" );
return 0;
}
static FILE *FS_FileForHandle( fileHandle_t f ) {
if ( f < 0 || f > MAX_FILE_HANDLES ) {
Com_Error( ERR_DROP, "FS_FileForHandle: out of reange" );
}
if (fsh[f].zipFile == qtrue) {
Com_Error( ERR_DROP, "FS_FileForHandle: can't get FILE on zip file" );
}
if ( ! fsh[f].handleFiles.file.o ) {
Com_Error( ERR_DROP, "FS_FileForHandle: NULL" );
}
return fsh[f].handleFiles.file.o;
}
void FS_ForceFlush( fileHandle_t f ) {
FILE *file;
file = FS_FileForHandle(f);
setvbuf( file, NULL, _IONBF, 0 );
}
/*
================
FS_filelength
If this is called on a non-unique FILE (from a pak file),
it will return the size of the pak file, not the expected
size of the file.
================
*/
int FS_filelength( fileHandle_t f ) {
int pos;
int end;
FILE* h;
h = FS_FileForHandle(f);
pos = ftell (h);
fseek (h, 0, SEEK_END);
end = ftell (h);
fseek (h, pos, SEEK_SET);
return end;
}
/*
====================
FS_ReplaceSeparators
Fix things up differently for win/unix/mac
====================
*/
static void FS_ReplaceSeparators( char *path ) {
char *s;
for ( s = path ; *s ; s++ ) {
if ( *s == '/' || *s == '\\' ) {
*s = PATH_SEP;
}
}
}
/*
===================
FS_BuildOSPath
Qpath may have either forward or backwards slashes
===================
*/
char *FS_BuildOSPath( const char *base, const char *game, const char *qpath ) {
char temp[MAX_OSPATH];
static char ospath[2][MAX_OSPATH];
static int toggle;
toggle ^= 1; // flip-flop to allow two returns without clash
if( !game || !game[0] ) {
game = fs_gamedir;
}
Com_sprintf( temp, sizeof(temp), "/%s/%s", game, qpath );
FS_ReplaceSeparators( temp );
Com_sprintf( ospath[toggle], sizeof( ospath[0] ), "%s%s", base, temp );
return ospath[toggle];
}
/*
============
FS_CreatePath
Creates any directories needed to store the given filename
============
*/
static qboolean FS_CreatePath (char *OSPath) {
char *ofs;
// make absolutely sure that it can't back up the path
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -