From 57252d1b300d96b2353bf9d564b0de281552d2c5 Mon Sep 17 00:00:00 2001 From: havoc Date: Fri, 21 Mar 2003 21:47:57 +0000 Subject: [PATCH] This is a patch from Elric greatly cleaning up the filesystem portions of the engine, and partially prepares the engine for .pk3 support (yay), this also removes support for gzipped files (but that had been disabled for months anyway) git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@2844 d7cf8633-e32d-0410-b094-e92efae38249 --- cd_linux.c | 24 +- cd_win.c | 24 +- cl_demo.c | 52 +-- cl_particles.c | 2 +- cl_screen.c | 8 +- cl_video.c | 2 +- client.h | 2 +- cmd.c | 10 +- common.c | 775 +------------------------------- common.h | 33 +- console.c | 35 +- cvar.c | 4 +- cvar.h | 2 +- darkplaces.dsp | 4 +- dpvsimpledecode.c | 14 +- fs.c | 1025 +++++++++++++++++++++++++++++++++++++++++++ fs.h | 75 ++++ gl_textures.c | 2 +- glquake.h | 7 - host.c | 8 +- host_cmd.c | 63 +-- image.c | 40 +- jpeg.c | 2 +- keys.c | 6 +- keys.h | 2 +- makefile | 2 +- makefile.mingw | 2 +- makefile.mingwcross | 2 +- menu.c | 20 +- model_brush.c | 16 +- model_shared.c | 8 +- net_dgrm.c | 10 +- net_main.c | 6 +- palette.c | 6 +- pr_edict.c | 32 +- progs.h | 4 +- quakedef.h | 2 +- quakeio.c | 401 ----------------- quakeio.h | 75 ---- r_shadow.c | 12 +- snd_mem.c | 4 +- snd_mix.c | 2 +- sv_main.c | 2 +- sv_user.c | 34 +- sys.h | 17 - sys_shared.c | 145 +----- wad.c | 24 +- wavefile.c | 25 +- wavefile.h | 4 +- 49 files changed, 1377 insertions(+), 1699 deletions(-) create mode 100644 fs.c create mode 100644 fs.h delete mode 100644 quakeio.c delete mode 100644 quakeio.h diff --git a/cd_linux.c b/cd_linux.c index eb8ef45f..7623b0a3 100644 --- a/cd_linux.c +++ b/cd_linux.c @@ -220,13 +220,13 @@ static void CD_f (void) command = Cmd_Argv (1); - if (Q_strcasecmp(command, "on") == 0) + if (strcasecmp(command, "on") == 0) { enabled = true; return; } - if (Q_strcasecmp(command, "off") == 0) + if (strcasecmp(command, "off") == 0) { if (playing) CDAudio_Stop(); @@ -234,7 +234,7 @@ static void CD_f (void) return; } - if (Q_strcasecmp(command, "reset") == 0) + if (strcasecmp(command, "reset") == 0) { enabled = true; if (playing) @@ -245,7 +245,7 @@ static void CD_f (void) return; } - if (Q_strcasecmp(command, "remap") == 0) + if (strcasecmp(command, "remap") == 0) { ret = Cmd_Argc() - 2; if (ret <= 0) @@ -260,7 +260,7 @@ static void CD_f (void) return; } - if (Q_strcasecmp(command, "close") == 0) + if (strcasecmp(command, "close") == 0) { CDAudio_CloseDoor(); return; @@ -276,37 +276,37 @@ static void CD_f (void) } } - if (Q_strcasecmp(command, "play") == 0) + if (strcasecmp(command, "play") == 0) { CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), false); return; } - if (Q_strcasecmp(command, "loop") == 0) + if (strcasecmp(command, "loop") == 0) { CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), true); return; } - if (Q_strcasecmp(command, "stop") == 0) + if (strcasecmp(command, "stop") == 0) { CDAudio_Stop(); return; } - if (Q_strcasecmp(command, "pause") == 0) + if (strcasecmp(command, "pause") == 0) { CDAudio_Pause(); return; } - if (Q_strcasecmp(command, "resume") == 0) + if (strcasecmp(command, "resume") == 0) { CDAudio_Resume(); return; } - if (Q_strcasecmp(command, "eject") == 0) + if (strcasecmp(command, "eject") == 0) { if (playing) CDAudio_Stop(); @@ -315,7 +315,7 @@ static void CD_f (void) return; } - if (Q_strcasecmp(command, "info") == 0) + if (strcasecmp(command, "info") == 0) { Con_Printf("%u tracks\n", maxTrack); if (playing) diff --git a/cd_win.c b/cd_win.c index 8d9f5684..8fba829a 100644 --- a/cd_win.c +++ b/cd_win.c @@ -250,13 +250,13 @@ static void CD_f (void) command = Cmd_Argv (1); - if (Q_strcasecmp(command, "on") == 0) + if (strcasecmp(command, "on") == 0) { enabled = true; return; } - if (Q_strcasecmp(command, "off") == 0) + if (strcasecmp(command, "off") == 0) { if (playing) CDAudio_Stop(); @@ -264,7 +264,7 @@ static void CD_f (void) return; } - if (Q_strcasecmp(command, "reset") == 0) + if (strcasecmp(command, "reset") == 0) { enabled = true; if (playing) @@ -275,7 +275,7 @@ static void CD_f (void) return; } - if (Q_strcasecmp(command, "remap") == 0) + if (strcasecmp(command, "remap") == 0) { ret = Cmd_Argc() - 2; if (ret <= 0) @@ -290,7 +290,7 @@ static void CD_f (void) return; } - if (Q_strcasecmp(command, "close") == 0) + if (strcasecmp(command, "close") == 0) { CDAudio_CloseDoor(); return; @@ -306,37 +306,37 @@ static void CD_f (void) } } - if (Q_strcasecmp(command, "play") == 0) + if (strcasecmp(command, "play") == 0) { CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), false); return; } - if (Q_strcasecmp(command, "loop") == 0) + if (strcasecmp(command, "loop") == 0) { CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), true); return; } - if (Q_strcasecmp(command, "stop") == 0) + if (strcasecmp(command, "stop") == 0) { CDAudio_Stop(); return; } - if (Q_strcasecmp(command, "pause") == 0) + if (strcasecmp(command, "pause") == 0) { CDAudio_Pause(); return; } - if (Q_strcasecmp(command, "resume") == 0) + if (strcasecmp(command, "resume") == 0) { CDAudio_Resume(); return; } - if (Q_strcasecmp(command, "eject") == 0) + if (strcasecmp(command, "eject") == 0) { if (playing) CDAudio_Stop(); @@ -345,7 +345,7 @@ static void CD_f (void) return; } - if (Q_strcasecmp(command, "info") == 0) + if (strcasecmp(command, "info") == 0) { Con_Printf("%u tracks\n", maxTrack); if (playing) diff --git a/cl_demo.c b/cl_demo.c index 831973dc..2df5b97f 100644 --- a/cl_demo.c +++ b/cl_demo.c @@ -78,7 +78,7 @@ void CL_StopPlayback (void) if (!cls.demoplayback) return; - Qclose (cls.demofile); + FS_Close (cls.demofile); cls.demoplayback = false; cls.demofile = NULL; @@ -103,14 +103,14 @@ void CL_WriteDemoMessage (void) return; len = LittleLong (net_message.cursize); - Qwrite (cls.demofile, &len, 4); + FS_Write (cls.demofile, &len, 4); for (i=0 ; i<3 ; i++) { f = LittleFloat (cl.viewangles[i]); - Qwrite (cls.demofile, &f, 4); + FS_Write (cls.demofile, &f, 4); } - Qwrite (cls.demofile, net_message.data, net_message.cursize); - Qflush (cls.demofile); + FS_Write (cls.demofile, net_message.data, net_message.cursize); + FS_Flush (cls.demofile); } /* @@ -150,18 +150,18 @@ int CL_GetMessage (void) } // get the next message - Qread (cls.demofile, &net_message.cursize, 4); + FS_Read (cls.demofile, &net_message.cursize, 4); VectorCopy (cl.mviewangles[0], cl.mviewangles[1]); for (i=0 ; i<3 ; i++) { - r = Qread (cls.demofile, &f, 4); + r = FS_Read (cls.demofile, &f, 4); cl.mviewangles[0][i] = LittleFloat (f); } net_message.cursize = LittleLong (net_message.cursize); if (net_message.cursize > MAX_DATAGRAM) Host_Error ("Demo message > MAX_DATAGRAM"); - r = Qread (cls.demofile, net_message.data, net_message.cursize); + r = FS_Read (cls.demofile, net_message.data, net_message.cursize); if (r != net_message.cursize) { CL_Disconnect (); @@ -216,7 +216,7 @@ void CL_Stop_f (void) CL_WriteDemoMessage (); // finish up - Qclose (cls.demofile); + FS_Close (cls.demofile); cls.demofile = NULL; cls.demorecording = false; Con_Printf ("Completed demo\n"); @@ -257,7 +257,7 @@ void CL_Record_f (void) return; } -// write the forced cd track number, or -1 + // write the forced cd track number, or -1 if (c == 4) { track = atoi(Cmd_Argv(3)); @@ -265,22 +265,18 @@ void CL_Record_f (void) } else track = -1; - - sprintf (name, "%s/%s", com_gamedir, Cmd_Argv(1)); -// -// start the map up -// + // start the map up if (c > 2) Cmd_ExecuteString ( va("map %s", Cmd_Argv(2)), src_command); -// -// open the demo file -// - COM_DefaultExtension (name, ".dem"); + // open the demo file + strncpy (name, Cmd_Argv(1), sizeof (name) - 1); + name[sizeof (name) - 1] = '\0'; + FS_DefaultExtension (name, ".dem"); Con_Printf ("recording to %s.\n", name); - cls.demofile = Qopen (name, "wb"); + cls.demofile = FS_Open (name, "wb", false); if (!cls.demofile) { Con_Printf ("ERROR: couldn't open.\n"); @@ -288,7 +284,7 @@ void CL_Record_f (void) } cls.forcetrack = track; - Qprintf (cls.demofile, "%i\n", cls.forcetrack); + FS_Printf (cls.demofile, "%i\n", cls.forcetrack); cls.demorecording = true; } @@ -316,19 +312,15 @@ void CL_PlayDemo_f (void) return; } -// -// disconnect from server -// + // disconnect from server CL_Disconnect (); -// -// open the demo file -// + // open the demo file strcpy (name, Cmd_Argv(1)); - COM_DefaultExtension (name, ".dem"); + FS_DefaultExtension (name, ".dem"); Con_Printf ("Playing demo from %s.\n", name); - COM_FOpenFile (name, &cls.demofile, false, true); + cls.demofile = FS_Open (name, "rb", false); if (!cls.demofile) { Con_Printf ("ERROR: couldn't open.\n"); @@ -342,7 +334,7 @@ void CL_PlayDemo_f (void) cls.state = ca_connected; cls.forcetrack = 0; - while ((c = Qgetc(cls.demofile)) != '\n') + while ((c = FS_Getc (cls.demofile)) != '\n') if (c == '-') neg = true; else diff --git a/cl_particles.c b/cl_particles.c index d0b5fd57..2dbe39dc 100644 --- a/cl_particles.c +++ b/cl_particles.c @@ -473,7 +473,7 @@ void CL_ReadPointFile_f (void) fclose(f); } #else - pointfile = COM_LoadFile(va("maps/%s.pts", cl.worldmodel->name), true); + pointfile = FS_LoadFile(va("maps/%s.pts", cl.worldmodel->name), true); #endif if (!pointfile) { diff --git a/cl_screen.c b/cl_screen.c index 8e23cc53..5e7ce092 100644 --- a/cl_screen.c +++ b/cl_screen.c @@ -476,7 +476,7 @@ void CL_Screen_Init(void) // HACK HACK HACK // load the image data for the player image in the config menu - dat = (qpic_t *)COM_LoadFile ("gfx/menuplyr.lmp", false); + dat = (qpic_t *)FS_LoadFile ("gfx/menuplyr.lmp", false); if (!dat) Sys_Error("unable to load gfx/menuplyr.lmp"); SwapPic (dat); @@ -821,9 +821,9 @@ void SCR_ScreenShot_f (void) for (; i<=9999 ; i++) { sprintf (filename, "dp%04i.tga", i); - sprintf (checkname, "%s/%s", com_gamedir, filename); - if (Sys_FileTime(checkname) == -1) - break; // file doesn't exist + sprintf (checkname, "%s/%s", fs_gamedir, filename); + if (!FS_SysFileExists(checkname)) + break; } if (i==10000) { diff --git a/cl_video.c b/cl_video.c index fb42b6f0..a309b2e4 100644 --- a/cl_video.c +++ b/cl_video.c @@ -193,7 +193,7 @@ static void CL_PlayVideo_f(void) return; } - sprintf(name, "%s/video/%s.dpv", com_gamedir, Cmd_Argv(1)); + sprintf(name, "%s/video/%s.dpv", fs_gamedir, Cmd_Argv(1)); CL_VideoStart(name); } diff --git a/client.h b/client.h index 08c98223..ff6f7b77 100644 --- a/client.h +++ b/client.h @@ -291,7 +291,7 @@ typedef struct qboolean timedemo; // -1 = use normal cd track int forcetrack; - QFile *demofile; + qfile_t *demofile; // to meter out one message a frame int td_lastframe; // host_framecount at start diff --git a/cmd.c b/cmd.c index c8aa3803..fdd51bba 100644 --- a/cmd.c +++ b/cmd.c @@ -287,7 +287,7 @@ static void Cmd_Exec_f (void) return; } - f = (char *)COM_LoadFile (Cmd_Argv(1), false); + f = (char *)FS_LoadFile (Cmd_Argv(1), false); if (!f) { Con_Printf ("couldn't exec %s\n",Cmd_Argv(1)); @@ -806,7 +806,7 @@ void Cmd_ExecuteString (const char *text, cmd_source_t src) // check functions for (cmd=cmd_functions ; cmd ; cmd=cmd->next) { - if (!Q_strcasecmp (cmd_argv[0],cmd->name)) + if (!strcasecmp (cmd_argv[0],cmd->name)) { cmd->function (); return; @@ -816,7 +816,7 @@ void Cmd_ExecuteString (const char *text, cmd_source_t src) // check alias for (a=cmd_alias ; a ; a=a->next) { - if (!Q_strcasecmp (cmd_argv[0], a->name)) + if (!strcasecmp (cmd_argv[0], a->name)) { Cbuf_InsertText (a->value); return; @@ -848,7 +848,7 @@ void Cmd_ForwardToServer (void) return; // not really connected MSG_WriteByte (&cls.message, clc_stringcmd); - if (Q_strcasecmp(Cmd_Argv(0), "cmd") != 0) + if (strcasecmp(Cmd_Argv(0), "cmd") != 0) { SZ_Print (&cls.message, Cmd_Argv(0)); SZ_Print (&cls.message, " "); @@ -877,7 +877,7 @@ int Cmd_CheckParm (const char *parm) Sys_Error ("Cmd_CheckParm: NULL"); for (i = 1; i < Cmd_Argc (); i++) - if (!Q_strcasecmp (parm, Cmd_Argv (i))) + if (!strcasecmp (parm, Cmd_Argv (i))) return i; return 0; diff --git a/common.c b/common.c index 9e549fe7..c3ae64b0 100644 --- a/common.c +++ b/common.c @@ -21,9 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include #include -#ifdef WIN32 -#include -#else +#ifndef WIN32 #include #endif @@ -32,14 +30,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. cvar_t registered = {0, "registered","0"}; cvar_t cmdline = {0, "cmdline","0"}; -mempool_t *pak_mempool; - -qboolean com_modified; // set true if using non-id files - -void COM_InitFilesystem (void); +extern qboolean fs_modified; // set true if using non-id files char com_token[1024]; -char com_basedir[MAX_OSPATH]; int com_argc; const char **com_argv; @@ -52,70 +45,6 @@ char *gamename; char *gamedirname; char com_modname[MAX_OSPATH]; -/* - - -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. - -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. - - - -FIXME: -The file "parms.txt" will be read out of the game directory and appended to the current command line arguments to allow different games to initialize startup parms differently. This could be used to add a "-sspeed 22050" for the high quality sound edition. Because they are added at the end, they will not override an explicit setting on the original command line. - -*/ - -//============================================================================ - - -/* -============================================================================ - - LIBRARY REPLACEMENT FUNCTIONS - -============================================================================ -*/ - -int Q_strncasecmp (const char *s1, const char *s2, int n) -{ - int c1, c2; - - while (1) - { - c1 = *s1++; - c2 = *s2++; - - if (!n--) - return 0; // strings are equal until end point - - if (c1 != c2) - { - if (c1 >= 'a' && c1 <= 'z') - c1 -= ('a' - 'A'); - if (c2 >= 'a' && c2 <= 'z') - c2 -= ('a' - 'A'); - if (c1 != c2) - return -1; // strings not equal - } - if (!c1) - return 0; // strings are equal - } - - return -1; -} - -int Q_strcasecmp (const char *s1, const char *s2) -{ - return Q_strncasecmp (s1, s2, 99999); -} /* ============================================================================ @@ -528,108 +457,6 @@ void SZ_HexDumpToConsole(const sizebuf_t *buf) //============================================================================ -/* -============ -COM_StripExtension -============ -*/ -// LordHavoc: replacement for severely broken COM_StripExtension that was used in original quake. -void COM_StripExtension (const char *in, char *out) -{ - char *last = NULL; - while (*in) - { - if (*in == '.') - last = out; - else if (*in == '/' || *in == '\\' || *in == ':') - last = NULL; - *out++ = *in++; - } - if (last) - *last = 0; - else - *out = 0; -} - -/* -============ -COM_FileExtension -============ -*/ -char *COM_FileExtension (const char *in) -{ - static char exten[8]; - int i; - - while (*in && *in != '.') - in++; - if (!*in) - return ""; - in++; - for (i=0 ; i<7 && *in ; i++,in++) - exten[i] = *in; - exten[i] = 0; - return exten; -} - -/* -============ -COM_FileBase -============ -*/ -void COM_FileBase (const char *in, char *out) -{ - const char *slash, *dot, *s; - - 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) - strcpy (out,"?model?"); - else - { - while (slash < dot) - *out++ = *slash++; - *out++ = 0; - } -} - - -/* -================== -COM_DefaultExtension -================== -*/ -void COM_DefaultExtension (char *path, const char *extension) -{ - const char *src; -// -// if path doesn't have a .EXT, append extension -// (extension should include the .) -// - src = path + strlen(path) - 1; - - while (*src != '/' && src != path) - { - if (*src == '.') - return; // it has an extension - src--; - } - - strcat (path, extension); -} - - /* ============== COM_ParseToken @@ -756,9 +583,9 @@ void COM_CheckRegistered (void) { Cvar_Set ("cmdline", com_cmdline); - if (!Sys_FileTime("gfx/pop.lmp")) + if (!FS_FileExists("gfx/pop.lmp")) { - if (com_modified) + if (fs_modified) Con_Printf ("Playing shareware version, with modification.\nwarning: most mods require full quake data.\n"); else Con_Printf ("Playing shareware version.\n"); @@ -770,9 +597,6 @@ void COM_CheckRegistered (void) } -void COM_Path_f (void); - - /* ================ COM_InitArgv @@ -799,7 +623,7 @@ void COM_InitArgv (void) void COM_InitGameType (void) { char name[MAX_OSPATH]; - COM_StripExtension(com_argv[0], name); + FS_StripExtension(com_argv[0], name); COM_ToLowerString(name, name); if (strstr(name, "transfusion")) @@ -862,6 +686,7 @@ void COM_InitGameType (void) extern void Mathlib_Init(void); +extern void FS_Init (void); /* ================ @@ -894,15 +719,12 @@ void COM_Init (void) } #endif - pak_mempool = Mem_AllocPool("paks"); - Cvar_RegisterVariable (®istered); Cvar_RegisterVariable (&cmdline); - Cmd_AddCommand ("path", COM_Path_f); Mathlib_Init(); - COM_InitFilesystem (); + FS_Init (); COM_CheckRegistered (); COM_InitGameType(); @@ -935,589 +757,6 @@ char *va(const char *format, ...) } -/* -============================================================================= - -QUAKE FILESYSTEM - -============================================================================= -*/ - -int com_filesize; - - -// -// in memory -// - -typedef struct -{ - char name[MAX_QPATH]; - int filepos, filelen; -} packfile_t; - -typedef struct pack_s -{ - char filename[MAX_OSPATH]; - int handle; - int numfiles; - packfile_t *files; - mempool_t *mempool; - struct pack_s *next; -} pack_t; - -// -// on disk -// -typedef struct -{ - char name[56]; - int filepos, filelen; -} dpackfile_t; - -typedef struct -{ - char id[4]; - int dirofs; - int dirlen; -} dpackheader_t; - -// LordHavoc: was 2048, increased to 65536 and changed info[MAX_PACK_FILES] to a temporary alloc -#define MAX_FILES_IN_PACK 65536 - -pack_t *packlist = NULL; - -#if CACHEENABLE -char com_cachedir[MAX_OSPATH]; -#endif -char com_gamedir[MAX_OSPATH]; - -typedef struct searchpath_s -{ - // only one of filename / pack will be used - char filename[MAX_OSPATH]; - pack_t *pack; - struct searchpath_s *next; -} searchpath_t; - -searchpath_t *com_searchpaths; - -/* -============ -COM_Path_f - -============ -*/ -void COM_Path_f (void) -{ - searchpath_t *s; - - Con_Printf ("Current search path:\n"); - for (s=com_searchpaths ; s ; s=s->next) - { - if (s->pack) - { - Con_Printf ("%s (%i files)\n", s->pack->filename, s->pack->numfiles); - } - else - Con_Printf ("%s\n", s->filename); - } -} - -/* -============ -COM_CreatePath - -LordHavoc: Previously only used for CopyFile, now also used for COM_WriteFile. -============ -*/ -void COM_CreatePath (char *path) -{ - char *ofs, save; - - for (ofs = path+1 ; *ofs ; ofs++) - { - if (*ofs == '/' || *ofs == '\\') - { - // create the directory - save = *ofs; - *ofs = 0; - Sys_mkdir (path); - *ofs = save; - } - } -} - - -/* -============ -COM_WriteFile - -The filename will be prefixed by the current game directory -============ -*/ -qboolean COM_WriteFile (const char *filename, void *data, int len) -{ - int handle; - char name[MAX_OSPATH]; - - sprintf (name, "%s/%s", com_gamedir, filename); - - // LordHavoc: added this - // create directories up to the file - COM_CreatePath (name); - - handle = Sys_FileOpenWrite (name); - if (handle == -1) - { - Con_Printf ("COM_WriteFile: failed on %s\n", name); - return false; - } - - Con_DPrintf ("COM_WriteFile: %s\n", name); - Sys_FileWrite (handle, data, len); - Sys_FileClose (handle); - return true; -} - - -/* -=========== -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) -{ - int in, out, remaining, count; - char buf[4096]; - - remaining = Sys_FileOpenRead (netpath, &in); - COM_CreatePath (cachepath); // create directories up to the cache file - out = Sys_FileOpenWrite (cachepath); - - while (remaining) - { - if (remaining < (int)sizeof(buf)) - count = remaining; - else - count = sizeof(buf); - Sys_FileRead (in, buf, count); - Sys_FileWrite (out, buf, count); - remaining -= count; - } - - Sys_FileClose (in); - Sys_FileClose (out); -} - -/* -=========== -COM_OpenRead -=========== -*/ -QFile * COM_OpenRead (const char *path, int offs, int len, qboolean zip) -{ - int fd = open (path, O_RDONLY); - unsigned char id[2], len_bytes[4]; - - if (fd == -1) - { - Sys_Error ("Couldn't open %s", path); - 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); - com_filesize = len; - -#ifdef WIN32 - setmode (fd, O_BINARY); -#endif - if (zip) - return Qdopen (fd, "rbz"); - else - return Qdopen (fd, "rb"); -} - -/* -=========== -COM_FindFile - -Finds the file in the search path. -Sets com_filesize and one of handle or file -=========== -*/ -int COM_FindFile (const char *filename, QFile **file, qboolean quiet, qboolean zip) -{ - searchpath_t *search; - char netpath[MAX_OSPATH]; -#if CACHEENABLE - char cachepath[MAX_OSPATH]; - int cachetime; -#endif - pack_t *pak; - int i, findtime, filenamelen; - char gzfilename[MAX_OSPATH]; - - filenamelen = strlen (filename); - sprintf (gzfilename, "%s.gz", filename); - - if (!file) - Sys_Error ("COM_FindFile: file not set"); - -// -// search through the path, one element at a time -// - search = com_searchpaths; - - for ( ; 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 ; inumfiles ; i++) - if (!strcmp (pak->files[i].name, filename) - || !strcmp (pak->files[i].name, gzfilename)) - { // found it! - if (!quiet) - Sys_Printf ("PackFile: %s : %s\n",pak->filename, pak->files[i].name); - // open a new file on the pakfile - *file = COM_OpenRead (pak->filename, pak->files[i].filepos, pak->files[i].filelen, zip); - return com_filesize; - } - } - else - { - sprintf (netpath, "%s/%s",search->filename, filename); - - findtime = Sys_FileTime (netpath); - if (findtime == -1) - continue; - -#if CACHEENABLE - // see if the file needs to be updated in the cache - if (com_cachedir[0]) - { -#if defined(_WIN32) - if ((strlen(netpath) < 2) || (netpath[1] != ':')) - sprintf (cachepath,"%s%s", com_cachedir, netpath); - else - sprintf (cachepath,"%s%s", com_cachedir, netpath+2); -#else - sprintf (cachepath,"%s%s", com_cachedir, netpath); -#endif - - cachetime = Sys_FileTime (cachepath); - - if (cachetime < findtime) - COM_CopyFile (netpath, cachepath); - strcpy (netpath, cachepath); - } -#endif - if (!quiet) - Sys_Printf ("FindFile: %s\n",netpath); - *file = COM_OpenRead (netpath, -1, -1, zip); - return com_filesize; - } - } - - if (!quiet) - Sys_Printf ("FindFile: can't find %s\n", filename); - - *file = NULL; - com_filesize = -1; - return -1; -} - - -/* -=========== -COM_FOpenFile - -If the requested file is inside a packfile, a new QFile * will be opened -into the file. -=========== -*/ -int COM_FOpenFile (const char *filename, QFile **file, qboolean quiet, qboolean zip) -{ - return COM_FindFile (filename, file, quiet, zip); -} - - -/* -============ -COM_LoadFile - -Filename are reletive to the quake directory. -Always appends a 0 byte. -============ -*/ -qbyte *loadbuf; -int loadsize; -qbyte *COM_LoadFile (const char *path, qboolean quiet) -{ - QFile *h; - qbyte *buf; - char base[1024]; - int len; - - buf = NULL; // quiet compiler warning - loadsize = 0; - -// look for it in the filesystem or pack files - len = COM_FOpenFile (path, &h, quiet, true); - if (!h) - return NULL; - - loadsize = len; - -// extract the filename base name for hunk tag - COM_FileBase (path, base); - - buf = Mem_Alloc(tempmempool, len+1); - if (!buf) - Sys_Error ("COM_LoadFile: not enough available memory for %s (size %i)", path, len); - - ((qbyte *)buf)[len] = 0; - - Qread (h, buf, len); - Qclose (h); - - 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. -================= -*/ -pack_t *COM_LoadPackFile (const char *packfile) -{ - dpackheader_t header; - int i, numpackfiles, packhandle; - pack_t *pack; - // LordHavoc: changed from stack array to temporary alloc, allowing huge pack directories - dpackfile_t *info; - - if (Sys_FileOpenRead (packfile, &packhandle) == -1) - return NULL; - - Sys_FileRead (packhandle, (void *)&header, sizeof(header)); - if (memcmp(header.id, "PACK", 4)) - Sys_Error ("%s is not a packfile", packfile); - header.dirofs = LittleLong (header.dirofs); - header.dirlen = LittleLong (header.dirlen); - - if (header.dirlen % sizeof(dpackfile_t)) - Sys_Error ("%s has an invalid directory size", packfile); - - numpackfiles = header.dirlen / sizeof(dpackfile_t); - - if (numpackfiles > MAX_FILES_IN_PACK) - Sys_Error ("%s has %i files", packfile, numpackfiles); - - pack = Mem_Alloc(pak_mempool, sizeof (pack_t)); - strcpy (pack->filename, packfile); - pack->handle = packhandle; - pack->numfiles = numpackfiles; - pack->mempool = Mem_AllocPool(packfile); - pack->files = Mem_Alloc(pack->mempool, numpackfiles * sizeof(packfile_t)); - pack->next = packlist; - packlist = pack; - - info = Mem_Alloc(tempmempool, sizeof(*info) * numpackfiles); - Sys_FileSeek (packhandle, header.dirofs); - Sys_FileRead (packhandle, (void *)info, header.dirlen); - -// parse the directory - for (i = 0;i < numpackfiles;i++) - { - strcpy (pack->files[i].name, info[i].name); - pack->files[i].filepos = LittleLong(info[i].filepos); - pack->files[i].filelen = LittleLong(info[i].filelen); - } - - Mem_Free(info); - - Con_Printf ("Added packfile %s (%i files)\n", packfile, numpackfiles); - return pack; -} - - -/* -================ -COM_AddGameDirectory - -Sets com_gamedir, adds the directory to the head of the path, -then loads and adds pak1.pak pak2.pak ... -================ -*/ -void COM_AddGameDirectory (char *dir) -{ - stringlist_t *list, *current; - searchpath_t *search; - pack_t *pak; - char pakfile[MAX_OSPATH]; - - strcpy (com_gamedir, dir); - -// -// add the directory to the search path -// - search = Mem_Alloc(pak_mempool, sizeof(searchpath_t)); - strcpy (search->filename, dir); - search->next = com_searchpaths; - com_searchpaths = search; - - // add any paks in the directory - list = listdirectory(dir); - for (current = list;current;current = current->next) - { - if (matchpattern(current->text, "*.pak", true)) - { - sprintf (pakfile, "%s/%s", dir, current->text); - pak = COM_LoadPackFile (pakfile); - if (pak) - { - search = Mem_Alloc(pak_mempool, sizeof(searchpath_t)); - search->pack = pak; - search->next = com_searchpaths; - com_searchpaths = search; - } - else - Con_Printf("unable to load pak \"%s\"\n", pakfile); - } - } - freedirectory(list); -} - -/* -================ -COM_InitFilesystem -================ -*/ -void COM_InitFilesystem (void) -{ - int i; - searchpath_t *search; - - strcpy(com_basedir, "."); - - // -basedir - // Overrides the system supplied base directory (under GAMENAME) - i = COM_CheckParm ("-basedir"); - if (i && i < com_argc-1) - strcpy (com_basedir, com_argv[i+1]); - - i = strlen (com_basedir); - if (i > 0 && (com_basedir[i-1] == '\\' || com_basedir[i-1] == '/')) - com_basedir[i-1] = 0; - -// start up with GAMENAME by default (id1) - strcpy(com_modname, GAMENAME); - COM_AddGameDirectory (va("%s/"GAMENAME, com_basedir)); - if (gamedirname[0]) - { - com_modified = true; - strcpy(com_modname, gamedirname); - COM_AddGameDirectory (va("%s/%s", com_basedir, gamedirname)); - } - - // -game - // Adds basedir/gamedir as an override game - i = COM_CheckParm ("-game"); - if (i && i < com_argc-1) - { - com_modified = true; - strcpy(com_modname, com_argv[i+1]); - COM_AddGameDirectory (va("%s/%s", com_basedir, com_argv[i+1])); - } - - // -path [] ... - // Fully specifies the exact search path, overriding the generated one - i = COM_CheckParm ("-path"); - if (i) - { - com_modified = true; - com_searchpaths = NULL; - while (++i < com_argc) - { - if (!com_argv[i] || com_argv[i][0] == '+' || com_argv[i][0] == '-') - break; - - search = Mem_Alloc(pak_mempool, sizeof(searchpath_t)); - if ( !strcmp(COM_FileExtension(com_argv[i]), "pak") ) - { - search->pack = COM_LoadPackFile (com_argv[i]); - if (!search->pack) - Sys_Error ("Couldn't load packfile: %s", com_argv[i]); - } - else - strcpy (search->filename, com_argv[i]); - search->next = com_searchpaths; - com_searchpaths = search; - } - } -} - -int COM_FileExists(const char *filename) -{ - searchpath_t *search; - char netpath[MAX_OSPATH]; - pack_t *pak; - int i, findtime; - - for (search = com_searchpaths;search;search = search->next) - { - if (search->pack) - { - pak = search->pack; - for (i = 0;i < pak->numfiles;i++) - if (!strcmp (pak->files[i].name, filename)) - return true; - } - else - { - sprintf (netpath, "%s/%s",search->filename, filename); - findtime = Sys_FileTime (netpath); - if (findtime != -1) - return true; - } - } - - return false; -} - - //====================================== // LordHavoc: added these because they are useful diff --git a/common.h b/common.h index 1a917ca5..80d80e48 100644 --- a/common.h +++ b/common.h @@ -21,11 +21,15 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef COMMON_H #define COMMON_H -// LordHavoc: MSVC has a different name for snprintf +// MSVC has a different name for several standard functions #ifdef WIN32 -#define snprintf _snprintf +# define snprintf _snprintf +# define vsnprintf _vsnprintf +# define strcasecmp stricmp +# define strncasecmp strnicmp #endif + //============================================================================ typedef struct sizebuf_s @@ -125,17 +129,10 @@ extern int dpprotocol; //============================================================================ -int Q_strcasecmp (const char *s1, const char *s2); -int Q_strncasecmp (const char *s1, const char *s2, int n); - -//============================================================================ - extern char com_token[1024]; -extern qboolean com_eof; int COM_ParseToken (const char **data); -extern char com_basedir[MAX_OSPATH]; extern int com_argc; extern const char **com_argv; @@ -144,29 +141,12 @@ void COM_Init (void); void COM_InitArgv (void); void COM_InitGameType (void); -void COM_StripExtension (const char *in, char *out); -void COM_FileBase (const char *in, char *out); -void COM_DefaultExtension (char *path, const char *extension); - char *va(const char *format, ...); // does a varargs printf into a temp buffer //============================================================================ -extern int com_filesize; - -extern char com_gamedir[MAX_OSPATH]; - -qboolean COM_WriteFile (const char *filename, void *data, int len); -int COM_FOpenFile (const char *filename, QFile **file, qboolean quiet, qboolean zip); - -// set by COM_LoadFile functions -extern int loadsize; -qbyte *COM_LoadFile (const char *path, qboolean quiet); - -int COM_FileExists(const char *filename); - extern struct cvar_s registered; #define GAME_NORMAL 0 @@ -178,6 +158,7 @@ extern struct cvar_s registered; extern int gamemode; extern char *gamename; +extern char *gamedirname; extern char com_modname[MAX_OSPATH]; // LordHavoc: useful... diff --git a/console.c b/console.c index bb971ad4..21148248 100644 --- a/console.c +++ b/console.c @@ -217,9 +217,9 @@ void Con_Init (void) if (con_debuglog) { - if (strlen (com_gamedir) < (MAXGAMEDIRLEN - strlen (t2))) + if (strlen (fs_gamedir) < (MAXGAMEDIRLEN - strlen (t2))) { - sprintf (temp, "%s%s", com_gamedir, t2); + sprintf (temp, "%s%s", fs_gamedir, t2); unlink (temp); } logfile.integer = 1; @@ -355,16 +355,16 @@ void Con_Print (const char *txt) Con_DebugLog ================ */ -void Con_DebugLog(const char *file, const char *fmt, ...) +void Con_DebugLog (const char *msg) { - va_list argptr; - FILE* fd; - - fd = fopen(file, "at"); - va_start(argptr, fmt); - vfprintf (fd, fmt, argptr); - va_end(argptr); - fclose(fd); + qfile_t* file; + + file = FS_Open ("qconsole.log", "at", true); + if (file) + { + FS_Printf (file, "%s", msg); + FS_Close (file); + } } @@ -387,17 +387,12 @@ void Con_Printf (const char *fmt, ...) vsprintf (msg,fmt,argptr); va_end (argptr); -// also echo to debugging console + // also echo to debugging console Sys_Printf ("%s", msg); -// log all messages to file + // log all messages to file if (con_debuglog) - { - // can't use va() here because it might overwrite other important things - char logname[MAX_OSPATH]; - sprintf(logname, "%s/qconsole.log", com_gamedir); - Con_DebugLog(logname, "%s", msg); - } + Con_DebugLog (msg); if (!con_initialized) return; @@ -405,7 +400,7 @@ void Con_Printf (const char *fmt, ...) if (cls.state == ca_dedicated) return; // no graphics mode -// write it to the scrollable buffer + // write it to the scrollable buffer Con_Print (msg); } diff --git a/cvar.c b/cvar.c index f0923b02..790bdd05 100644 --- a/cvar.c +++ b/cvar.c @@ -323,13 +323,13 @@ Writes lines containing "set variable value" for all variables with the archive flag set to true. ============ */ -void Cvar_WriteVariables (QFile *f) +void Cvar_WriteVariables (qfile_t *f) { cvar_t *var; for (var = cvar_vars ; var ; var = var->next) if (var->flags & CVAR_SAVE) - Qprintf (f, "%s \"%s\"\n", var->name, var->string); + FS_Printf (f, "%s \"%s\"\n", var->name, var->string); } diff --git a/cvar.h b/cvar.h index 8a7dbc8e..b623566b 100644 --- a/cvar.h +++ b/cvar.h @@ -144,7 +144,7 @@ qboolean Cvar_Command (void); // command. Returns true if the command was a variable reference that // was handled. (print or change) -void Cvar_WriteVariables (QFile *f); +void Cvar_WriteVariables (qfile_t *f); // Writes lines containing "set variable value" for all variables // with the archive flag set to true. diff --git a/darkplaces.dsp b/darkplaces.dsp index 8c871dd2..27e0f1d6 100644 --- a/darkplaces.dsp +++ b/darkplaces.dsp @@ -312,7 +312,7 @@ SOURCE=.\protocol.c # End Source File # Begin Source File -SOURCE=.\quakeio.c +SOURCE=.\fs.c # End Source File # Begin Source File @@ -628,7 +628,7 @@ SOURCE=.\quakedef.h # End Source File # Begin Source File -SOURCE=.\quakeio.h +SOURCE=.\fs.h # End Source File # Begin Source File diff --git a/dpvsimpledecode.c b/dpvsimpledecode.c index 0d30b4f7..b248ea0f 100644 --- a/dpvsimpledecode.c +++ b/dpvsimpledecode.c @@ -20,7 +20,7 @@ typedef struct { - FILE *file; + qfile_t *file; int endoffile; } hz_bitstream_read_t; @@ -45,9 +45,9 @@ hz_bitstream_readblocks_t; hz_bitstream_read_t *hz_bitstream_read_open(char *filename) { - FILE *file; + qfile_t *file; hz_bitstream_read_t *stream; - if ((file = fopen(filename, "rb"))) + if ((file = FS_Open (filename, "rb", false))) { stream = malloc(sizeof(hz_bitstream_read_t)); memset(stream, 0, sizeof(*stream)); @@ -62,20 +62,20 @@ void hz_bitstream_read_close(hz_bitstream_read_t *stream) { if (stream) { - fclose(stream->file); + FS_Close(stream->file); free(stream); } } unsigned int hz_bitstream_read_currentbyte(hz_bitstream_read_t *stream) { - return ftell(stream->file); + return FS_Tell(stream->file); } int hz_bitstream_read_seek(hz_bitstream_read_t *stream, unsigned int position) { stream->endoffile = 0; - return fseek(stream->file, position, SEEK_SET) != 0; + return FS_Seek(stream->file, position, SEEK_SET) != 0; } hz_bitstream_readblocks_t *hz_bitstream_read_blocks_new(void) @@ -133,7 +133,7 @@ int hz_bitstream_read_blocks_read(hz_bitstream_readblocks_t *blocks, hz_bitstrea else b->size = s; s -= b->size; - if (fread(b->data, 1, b->size, stream->file) != b->size) + if (FS_Read(stream->file, b->data, b->size) != b->size) { stream->endoffile = 1; break; diff --git a/fs.c b/fs.c new file mode 100644 index 00000000..eeb41d95 --- /dev/null +++ b/fs.c @@ -0,0 +1,1025 @@ +/* + Quake file system + + Copyright (C) 2003 Mathieu Olivier + Copyright (C) 1999,2000 contributors of the QuakeForge project + + 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 +*/ + +#include "quakedef.h" + +#include +#include +#include +#include + +#ifdef WIN32 +# include +# include +#else +# include +# include +# include +#endif + +#ifndef PATH_MAX +# define PATH_MAX 512 +#endif + +#include "fs.h" + +/* + +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. + +*/ + + +/* +============================================================================= + +TYPES + +============================================================================= +*/ + +// Our own file structure on top of FILE +typedef enum +{ + FS_FLAG_NONE = 0, + FS_FLAG_PACKED = (1 << 0) // inside a package (PAK or PK3) +// FS_FLAG_COMPRESSED = (1 << 1) // compressed (inside a PK3 file) +} fs_flags_t; + +struct qfile_s +{ + fs_flags_t flags; + FILE* stream; + size_t length; // file size (PACKED only) + size_t offset; // offset into a package (PACKED only) + size_t position; // current position in the file (PACKED only) +}; + + +// PAK files on disk +typedef struct +{ + char name[56]; + int filepos, filelen; +} dpackfile_t; + +typedef struct +{ + char id[4]; + int dirofs; + int dirlen; +} dpackheader_t; + + +// Packages in memory +typedef struct +{ + char name[MAX_QPATH]; + int filepos, filelen; +} packfile_t; + +typedef struct pack_s +{ + char filename[MAX_OSPATH]; + FILE *handle; + int numfiles; + packfile_t *files; + mempool_t *mempool; + struct pack_s *next; +} pack_t; + + +// Search paths for files (including packages) +typedef struct searchpath_s +{ + // only one of filename / pack will be used + char filename[MAX_OSPATH]; + pack_t *pack; + struct searchpath_s *next; +} searchpath_t; + + +/* +============================================================================= + +VARIABLES + +============================================================================= +*/ + +mempool_t *fs_mempool; +mempool_t *pak_mempool; + +int fs_filesize; + +pack_t *packlist = NULL; + +searchpath_t *fs_searchpaths; + +// LordHavoc: was 2048, increased to 65536 and changed info[MAX_PACK_FILES] to a temporary alloc +#define MAX_FILES_IN_PACK 65536 + +char fs_gamedir[MAX_OSPATH]; +char fs_basedir[MAX_OSPATH]; + +qboolean fs_modified; // set true if using non-id files + + +/* +============================================================================= + +PRIVATE FUNCTIONS + +============================================================================= +*/ + + +/* +============ +FS_CreatePath + +LordHavoc: Previously only used for CopyFile, now also used for FS_WriteFile. +============ +*/ +void FS_CreatePath (char *path) +{ + char *ofs, save; + + for (ofs = path+1 ; *ofs ; ofs++) + { + if (*ofs == '/' || *ofs == '\\') + { + // create the directory + save = *ofs; + *ofs = 0; + FS_mkdir (path); + *ofs = save; + } + } +} + + +/* +============ +FS_Path_f + +============ +*/ +void FS_Path_f (void) +{ + searchpath_t *s; + + Con_Printf ("Current search path:\n"); + for (s=fs_searchpaths ; s ; s=s->next) + { + if (s->pack) + { + Con_Printf ("%s (%i files)\n", s->pack->filename, s->pack->numfiles); + } + else + Con_Printf ("%s\n", s->filename); + } +} + + +/* +================= +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 (const char *packfile) +{ + dpackheader_t header; + int i, numpackfiles; + FILE *packhandle; + pack_t *pack; + // LordHavoc: changed from stack array to temporary alloc, allowing huge pack directories + dpackfile_t *info; + + packhandle = fopen (packfile, "rb"); + if (!packhandle) + return NULL; + + fread ((void *)&header, 1, sizeof(header), packhandle); + if (memcmp(header.id, "PACK", 4)) + Sys_Error ("%s is not a packfile", packfile); + header.dirofs = LittleLong (header.dirofs); + header.dirlen = LittleLong (header.dirlen); + + if (header.dirlen % sizeof(dpackfile_t)) + Sys_Error ("%s has an invalid directory size", packfile); + + numpackfiles = header.dirlen / sizeof(dpackfile_t); + + if (numpackfiles > MAX_FILES_IN_PACK) + Sys_Error ("%s has %i files", packfile, numpackfiles); + + pack = Mem_Alloc(pak_mempool, sizeof (pack_t)); + strcpy (pack->filename, packfile); + pack->handle = packhandle; + pack->numfiles = numpackfiles; + pack->mempool = Mem_AllocPool(packfile); + pack->files = Mem_Alloc(pack->mempool, numpackfiles * sizeof(packfile_t)); + pack->next = packlist; + packlist = pack; + + info = Mem_Alloc(tempmempool, sizeof(*info) * numpackfiles); + fseek (packhandle, header.dirofs, SEEK_SET); + fread ((void *)info, 1, header.dirlen, packhandle); + +// parse the directory + for (i = 0;i < numpackfiles;i++) + { + strcpy (pack->files[i].name, info[i].name); + pack->files[i].filepos = LittleLong(info[i].filepos); + pack->files[i].filelen = LittleLong(info[i].filelen); + } + + Mem_Free(info); + + Con_Printf ("Added packfile %s (%i files)\n", packfile, numpackfiles); + return pack; +} + + +/* +================ +FS_AddGameDirectory + +Sets fs_gamedir, adds the directory to the head of the path, +then loads and adds pak1.pak pak2.pak ... +================ +*/ +void FS_AddGameDirectory (char *dir) +{ + stringlist_t *list, *current; + searchpath_t *search; + pack_t *pak; + char pakfile[MAX_OSPATH]; + + strcpy (fs_gamedir, dir); + + // add the directory to the search path + search = Mem_Alloc(pak_mempool, sizeof(searchpath_t)); + strcpy (search->filename, dir); + search->next = fs_searchpaths; + fs_searchpaths = search; + + // add any paks in the directory + list = listdirectory(dir); + for (current = list;current;current = current->next) + { + if (matchpattern(current->text, "*.pak", true)) + { + sprintf (pakfile, "%s/%s", dir, current->text); + pak = FS_LoadPackFile (pakfile); + if (pak) + { + search = Mem_Alloc(pak_mempool, sizeof(searchpath_t)); + search->pack = pak; + search->next = fs_searchpaths; + fs_searchpaths = search; + } + else + Con_Printf("unable to load pak \"%s\"\n", pakfile); + } + } + freedirectory(list); +} + + +/* +============ +FS_FileExtension +============ +*/ +char *FS_FileExtension (const char *in) +{ + static char exten[8]; + int i; + + while (*in && *in != '.') + in++; + if (!*in) + return ""; + in++; + for (i=0 ; i<7 && *in ; i++,in++) + exten[i] = *in; + exten[i] = 0; + return exten; +} + + +/* +================ +FS_Init +================ +*/ +void FS_Init (void) +{ + int i; + searchpath_t *search; + + fs_mempool = Mem_AllocPool("file management"); + pak_mempool = Mem_AllocPool("paks"); + + Cmd_AddCommand ("path", FS_Path_f); + + strcpy(fs_basedir, "."); + + // -basedir + // Overrides the system supplied base directory (under GAMENAME) + i = COM_CheckParm ("-basedir"); + if (i && i < com_argc-1) + strcpy (fs_basedir, com_argv[i+1]); + + i = strlen (fs_basedir); + if (i > 0 && (fs_basedir[i-1] == '\\' || fs_basedir[i-1] == '/')) + fs_basedir[i-1] = 0; + + // start up with GAMENAME by default (id1) + strcpy(com_modname, GAMENAME); + FS_AddGameDirectory (va("%s/"GAMENAME, fs_basedir)); + if (gamedirname[0]) + { + fs_modified = true; + strcpy(com_modname, gamedirname); + FS_AddGameDirectory (va("%s/%s", fs_basedir, gamedirname)); + } + + // -game + // Adds basedir/gamedir as an override game + i = COM_CheckParm ("-game"); + if (i && i < com_argc-1) + { + fs_modified = true; + strcpy(com_modname, com_argv[i+1]); + FS_AddGameDirectory (va("%s/%s", fs_basedir, com_argv[i+1])); + } + + // -path [] ... + // Fully specifies the exact search path, overriding the generated one + i = COM_CheckParm ("-path"); + if (i) + { + fs_modified = true; + fs_searchpaths = NULL; + while (++i < com_argc) + { + if (!com_argv[i] || com_argv[i][0] == '+' || com_argv[i][0] == '-') + break; + + search = Mem_Alloc(pak_mempool, sizeof(searchpath_t)); + if ( !strcmp(FS_FileExtension(com_argv[i]), "pak") ) + { + search->pack = FS_LoadPackFile (com_argv[i]); + if (!search->pack) + Sys_Error ("Couldn't load packfile: %s", com_argv[i]); + } + else + strcpy (search->filename, com_argv[i]); + search->next = fs_searchpaths; + fs_searchpaths = search; + } + } +} + + +/* +============================================================================= + +MAIN FUNCTIONS + +============================================================================= +*/ + +/* +==================== +FS_Open + +Internal function used to create a qfile_t and open the relevant file on disk +==================== +*/ +static qfile_t* FS_SysOpen (const char* filepath, const char* mode) +{ + qfile_t* file; + + file = Mem_Alloc (fs_mempool, sizeof (*file)); + memset (file, 0, sizeof (*file)); + + file->stream = fopen (filepath, mode); + if (!file->stream) + { + Mem_Free (file); + return NULL; + } + + return file; +} + + +/* +=========== +FS_OpenRead +=========== +*/ +qfile_t *FS_OpenRead (const char *path, int offs, int len) +{ + qfile_t* file; + + file = FS_SysOpen (path, "rb"); + if (!file) + { + Sys_Error ("Couldn't open %s", path); + return NULL; + } + + // Normal file + if (offs < 0 || len < 0) + { + FS_Seek (file, 0, SEEK_END); + len = FS_Tell (file); + FS_Seek (file, 0, SEEK_SET); + } + // Packed file + else + { + FS_Seek (file, offs, SEEK_SET); + + file->flags |= FS_FLAG_PACKED; + file->length = len; + file->offset = offs; + file->position = 0; + } + + fs_filesize = len; + + return file; +} + +/* +=========== +FS_FOpenFile + +If the requested file is inside a packfile, a new qfile_t* will be opened +into the file. + +Sets fs_filesize +=========== +*/ +qfile_t *FS_FOpenFile (const char *filename, qboolean quiet) +{ + searchpath_t *search; + char netpath[MAX_OSPATH]; + pack_t *pak; + int i, filenamelen; + + filenamelen = strlen (filename); + + // search through the path, one element at a time + search = fs_searchpaths; + + for ( ; 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 ; inumfiles ; i++) + if (!strcmp (pak->files[i].name, filename)) + { // found it! + if (!quiet) + Sys_Printf ("PackFile: %s : %s\n",pak->filename, pak->files[i].name); + // open a new file in the pakfile + return FS_OpenRead (pak->filename, pak->files[i].filepos, pak->files[i].filelen); + } + } + else + { + sprintf (netpath, "%s/%s",search->filename, filename); + + if (!FS_SysFileExists (netpath)) + continue; + + if (!quiet) + Sys_Printf ("FindFile: %s\n",netpath); + return FS_OpenRead (netpath, -1, -1); + } + } + + if (!quiet) + Sys_Printf ("FindFile: can't find %s\n", filename); + + fs_filesize = -1; + return NULL; +} + + +/* +==================== +FS_Open + +Open a file. The syntax is the same as fopen +==================== +*/ +qfile_t* FS_Open (const char* filepath, const char* mode, qboolean quiet) +{ + // If the file is opened in "write" or "append" mode + if (strchr (mode, 'w') || strchr (mode, 'a')) + { + char real_path [MAX_OSPATH]; + + // Open the file on disk directly + snprintf (real_path, sizeof (real_path), "%s/%s", fs_gamedir, filepath); + return FS_SysOpen (real_path, mode); + } + + // Else, we look at the various search paths + return FS_FOpenFile (filepath, quiet); +} + + +/* +==================== +FS_Close + +Close a file +==================== +*/ +int FS_Close (qfile_t* file) +{ + if (fclose (file->stream)) + return EOF; + + Mem_Free (file); + return 0; +} + + +/* +==================== +FS_Write + +Write "datasize" bytes into a file +==================== +*/ +size_t FS_Write (qfile_t* file, const void* data, size_t datasize) +{ + return fwrite (data, 1, datasize, file->stream); +} + + +/* +==================== +FS_Read + +Read up to "buffersize" bytes from a file +==================== +*/ +size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize) +{ + size_t nb; + + // If the file belongs to a package, we must take care + // to not read after the end of the file + if (file->flags & FS_FLAG_PACKED) + { + size_t remain = file->length - file->position; + if (buffersize > remain) + buffersize = remain; + } + + nb = fread (buffer, 1, buffersize, file->stream); + + // Update the position index if the file is packed + if ((file->flags & FS_FLAG_PACKED) && nb > 0) + file->position += nb; + + return nb; +} + + +/* +==================== +FS_Flush + +Flush the file output stream +==================== +*/ +int FS_Flush (qfile_t* file) +{ + return fflush (file->stream); +} + + +/* +==================== +FS_Printf + +Print a string into a file +==================== +*/ +int FS_Printf (qfile_t* file, const char* format, ...) +{ + int result; + va_list args; + + va_start (args, format); + result = vfprintf (file->stream, format, args); + va_end (args); + + return result; +} + + +/* +==================== +FS_Getc + +Get the next character of a file +==================== +*/ +int FS_Getc (qfile_t* file) +{ + int c; + + // If the file belongs to a package, we must take care + // to not read after the end of the file + if (file->flags & FS_FLAG_PACKED) + { + if (file->position >= file->length) + return EOF; + } + + c = fgetc (file->stream); + + // Update the position index if the file is packed + if ((file->flags & FS_FLAG_PACKED) && c != EOF) + file->position++; + + return c; +} + + +/* +==================== +FS_Seek + +Move the position index in a file +==================== +*/ +int FS_Seek (qfile_t* file, long offset, int whence) +{ + // Packed files receive a special treatment + if (file->flags & FS_FLAG_PACKED) + { + switch (whence) + { + case SEEK_CUR: + offset += file->position; + // It continues on the next case (no break) + + case SEEK_SET: + if (offset < 0 || offset > file->length) + return -1; + if (fseek (file->stream, file->offset + offset, SEEK_SET) == -1) + return -1; + file->position = offset; + return 0; + + case SEEK_END: + if (offset > 0 || -offset > file->length) + return -1; + if (fseek (file->stream, file->offset + file->length + offset, SEEK_SET) == -1) + return -1; + file->position = file->length + offset; + return 0; + + default: + return -1; + } + } + + return fseek (file->stream, offset, whence); +} + + +/* +==================== +FS_Tell + +Give the current position in a file +==================== +*/ +long FS_Tell (qfile_t* file) +{ + if (file->flags & FS_FLAG_PACKED) + return file->position; + + return ftell (file->stream); +} + + +/* +==================== +FS_Gets + +Extract a line from a file +==================== +*/ +char* FS_Gets (qfile_t* file, char* buffer, int buffersize) +{ + if (!fgets (buffer, buffersize, file->stream)) + return NULL; + + // Check that we didn't read after the end of a packed file, and update the position + if (file->flags & FS_FLAG_PACKED) + { + size_t len = strlen (buffer); + size_t max = file->length - file->position; + + if (len > max) + { + buffer[max] = '\0'; + file->position = file->length; + } + else + file->position += len; + } + + return buffer; +} + + +/* +========== +FS_Getline + +Dynamic length version of fgets. DO NOT free the buffer. +========== +*/ +char *FS_Getline (qfile_t *file) +{ + static int size = 256; + static char *buf = 0; + char *t; + int len; + + if (!buf) + buf = Mem_Alloc (fs_mempool, size); + + if (!FS_Gets (file, buf, size)) + return 0; + + len = strlen (buf); + while (buf[len - 1] != '\n' && buf[len - 1] != '\r') + { + t = Mem_Alloc (fs_mempool, size + 256); + memcpy(t, buf, size); + Mem_Free(buf); + size += 256; + buf = t; + if (!FS_Gets (file, buf + len, size - len)) + break; + len = strlen (buf); + } + while ((len = strlen(buf)) && (buf[len - 1] == '\n' || buf[len - 1] == '\r')) + buf[len - 1] = 0; + return buf; +} + + +/* +==================== +FS_Eof + +Extract a line from a file +==================== +*/ +int FS_Eof (qfile_t* file) +{ + if (file->flags & FS_FLAG_PACKED) + return (file->position == file->length); + + return feof (file->stream); +} + + +/* +============ +FS_LoadFile + +Filename are relative to the quake directory. +Always appends a 0 byte. +============ +*/ +qbyte *FS_LoadFile (const char *path, qboolean quiet) +{ + qfile_t *h; + qbyte *buf; + + // look for it in the filesystem or pack files + h = FS_Open (path, "rb", quiet); + if (!h) + return NULL; + + buf = Mem_Alloc(tempmempool, fs_filesize+1); + if (!buf) + Sys_Error ("FS_LoadFile: not enough available memory for %s (size %i)", path, fs_filesize); + + ((qbyte *)buf)[fs_filesize] = 0; + + FS_Read (h, buf, fs_filesize); + FS_Close (h); + + return buf; +} + + +/* +============ +FS_WriteFile + +The filename will be prefixed by the current game directory +============ +*/ +qboolean FS_WriteFile (const char *filename, void *data, int len) +{ + FILE *handle; + char name[MAX_OSPATH]; + + sprintf (name, "%s/%s", fs_gamedir, filename); + + // Create directories up to the file + FS_CreatePath (name); + + handle = fopen (name, "wb"); + if (!handle) + { + Con_Printf ("FS_WriteFile: failed on %s\n", name); + return false; + } + + Con_DPrintf ("FS_WriteFile: %s\n", name); + fwrite (data, 1, len, handle); + fclose (handle); + return true; +} + + +/* +============================================================================= + +OTHERS FUNCTIONS + +============================================================================= +*/ + +/* +============ +FS_StripExtension +============ +*/ +void FS_StripExtension (const char *in, char *out) +{ + char *last = NULL; + while (*in) + { + if (*in == '.') + last = out; + else if (*in == '/' || *in == '\\' || *in == ':') + last = NULL; + *out++ = *in++; + } + if (last) + *last = 0; + else + *out = 0; +} + + +/* +================== +FS_DefaultExtension +================== +*/ +void FS_DefaultExtension (char *path, const char *extension) +{ + const char *src; + + // if path doesn't have a .EXT, append extension + // (extension should include the .) + src = path + strlen(path) - 1; + + while (*src != '/' && src != path) + { + if (*src == '.') + return; // it has an extension + src--; + } + + strcat (path, extension); +} + + +qboolean FS_FileExists (const char *filename) +{ + searchpath_t *search; + char netpath[MAX_OSPATH]; + pack_t *pak; + int i; + + for (search = fs_searchpaths;search;search = search->next) + { + if (search->pack) + { + pak = search->pack; + for (i = 0;i < pak->numfiles;i++) + if (!strcmp (pak->files[i].name, filename)) + return true; + } + else + { + sprintf (netpath, "%s/%s",search->filename, filename); + if (FS_SysFileExists (netpath)) + return true; + } + } + + return false; +} + + +qboolean FS_SysFileExists (const char *path) +{ +#if WIN32 + FILE *f; + + f = fopen (path, "rb"); + if (f) + { + fclose (f); + return true; + } + + return false; +#else + struct stat buf; + + if (stat (path,&buf) == -1) + return false; + + return true; +#endif +} + +void FS_mkdir (const char *path) +{ +#if WIN32 + _mkdir (path); +#else + mkdir (path, 0777); +#endif +} diff --git a/fs.h b/fs.h new file mode 100644 index 00000000..7a409e54 --- /dev/null +++ b/fs.h @@ -0,0 +1,75 @@ +/* + Quake file system + + Copyright (C) 2003 Mathieu Olivier + + 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 +*/ + +#ifndef QUAKEIO_H +#define QUAKEIO_H + + +// ------ Types ------ // + +typedef struct qfile_s qfile_t; + + +// ------ Variables ------ // + +extern char fs_gamedir [MAX_OSPATH]; +extern char fs_basedir [MAX_OSPATH]; + +extern int fs_filesize; // set by FS_Open and FS_LoadFile + + +// ------ Main functions ------ // + +// NOTE: the file path is automatically prefixed by the current game directory for each +// file created by FS_WriteFile, or opened in "write" or "append" mode by FS_Open + +qfile_t *FS_Open (const char* filepath, const char* mode, qboolean quiet); +int FS_Close (qfile_t* file); +size_t FS_Write (qfile_t* file, const void* data, size_t datasize); +size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize); +int FS_Flush (qfile_t* file); +int FS_Printf (qfile_t* file, const char* format, ...); +int FS_Getc (qfile_t* file); +int FS_Seek (qfile_t* file, long offset, int whence); +long FS_Tell (qfile_t* file); +char *FS_Gets (qfile_t* file, char* buffer, int buffersize); +char *FS_Getline (qfile_t *file); // DO NOT FREE the returned buffer +int FS_Eof (qfile_t* file); + +qbyte *FS_LoadFile (const char *path, qboolean quiet); +qboolean FS_WriteFile (const char *filename, void *data, int len); + + +// ------ Other functions ------ // + +void FS_StripExtension (const char *in, char *out); +void FS_DefaultExtension (char *path, const char *extension); + +qboolean FS_FileExists (const char *filename); // the file can be into a package +qboolean FS_SysFileExists (const char *filename); // only look for files outside of packages + +void FS_mkdir (const char *path); + + +#endif diff --git a/gl_textures.c b/gl_textures.c index 65794a0f..cab9f636 100644 --- a/gl_textures.c +++ b/gl_textures.c @@ -343,7 +343,7 @@ static void GL_TextureMode_f (void) } for (i = 0;i < 6;i++) - if (!Q_strcasecmp (modes[i].name, Cmd_Argv(1) ) ) + if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) ) break; if (i == 6) { diff --git a/glquake.h b/glquake.h index 58068e6b..4e9e3a24 100644 --- a/glquake.h +++ b/glquake.h @@ -29,13 +29,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #pragma warning(disable : 4018) // LordHavoc: MSVC++ 4 x86, signed/unsigned mismatch #endif -#ifdef _WIN32 -#include -#define strcasecmp stricmp -#define strncasecmp strnicmp -#endif - -//#include //==================================================== diff --git a/host.c b/host.c index 3ac0f7e0..465545ad 100644 --- a/host.c +++ b/host.c @@ -321,13 +321,13 @@ Writes key bindings and archived cvars to config.cfg */ void Host_WriteConfiguration (void) { - QFile *f; + qfile_t *f; // dedicated servers initialize the host but don't parse and set the // config.cfg cvars if (host_initialized && cls.state != ca_dedicated) { - f = Qopen (va("%s/config.cfg",com_gamedir), "w"); + f = FS_Open ("config.cfg", "w", false); if (!f) { Con_Printf ("Couldn't write config.cfg.\n"); @@ -337,7 +337,7 @@ void Host_WriteConfiguration (void) Key_WriteBindings (f); Cvar_WriteVariables (f); - Qclose (f); + FS_Close (f); } } @@ -856,7 +856,6 @@ void Host_Frame (float time) //============================================================================ void Render_Init(void); -void QuakeIO_Init(void); /* ==================== @@ -880,7 +879,6 @@ void Host_Init (void) Memory_Init_Commands(); R_Modules_Init(); Cbuf_Init (); - QuakeIO_Init (); V_Init (); COM_Init (); Host_InitLocal (); diff --git a/host_cmd.c b/host_cmd.c index 3179cee2..fc78cee4 100644 --- a/host_cmd.c +++ b/host_cmd.c @@ -379,7 +379,7 @@ Host_Savegame_f void Host_Savegame_f (void) { char name[256]; - QFile *f; + qfile_t *f; int i; char comment[SAVEGAME_COMMENT_LENGTH+1]; @@ -425,34 +425,35 @@ void Host_Savegame_f (void) } } - sprintf (name, "%s/%s", com_gamedir, Cmd_Argv(1)); - COM_DefaultExtension (name, ".sav"); + strncpy (name, Cmd_Argv(1), sizeof (name) - 1); + name[sizeof (name) - 1] = '\0'; + FS_DefaultExtension (name, ".sav"); Con_Printf ("Saving game to %s...\n", name); - f = Qopen (name, "w"); + f = FS_Open (name, "w", false); if (!f) { Con_Printf ("ERROR: couldn't open.\n"); return; } - Qprintf (f, "%i\n", SAVEGAME_VERSION); + FS_Printf (f, "%i\n", SAVEGAME_VERSION); Host_SavegameComment (comment); - Qprintf (f, "%s\n", comment); + FS_Printf (f, "%s\n", comment); for (i=0 ; ispawn_parms[i]); - Qprintf (f, "%d\n", current_skill); - Qprintf (f, "%s\n", sv.name); - Qprintf (f, "%f\n",sv.time); + FS_Printf (f, "%f\n", svs.clients->spawn_parms[i]); + FS_Printf (f, "%d\n", current_skill); + FS_Printf (f, "%s\n", sv.name); + FS_Printf (f, "%f\n",sv.time); // write the light styles for (i=0 ; ispawn_parms[i] = spawn_parms[i]; @@ -821,7 +822,7 @@ void Host_Tell_f(void) { if (!client->active || !client->spawned) continue; - if (Q_strcasecmp(client->name, Cmd_Argv(1))) + if (strcasecmp(client->name, Cmd_Argv(1))) continue; host_client = client; SV_ClientPrintf("%s", text); @@ -1185,7 +1186,7 @@ void Host_Kick_f (void) { if (!host_client->active) continue; - if (Q_strcasecmp(host_client->name, Cmd_Argv(1)) == 0) + if (strcasecmp(host_client->name, Cmd_Argv(1)) == 0) break; } } diff --git a/image.c b/image.c index 53ddafb1..51cde771 100644 --- a/image.c +++ b/image.c @@ -93,7 +93,7 @@ qbyte* LoadPCX (const qbyte *f, int matchwidth, int matchheight) const qbyte *palette, *fin, *enddata; int x, y, x2, dataByte; - if (loadsize < (int)sizeof(pcx) + 768) + if (fs_filesize < (int)sizeof(pcx) + 768) { Con_Printf ("Bad pcx file\n"); return NULL; @@ -132,7 +132,7 @@ qbyte* LoadPCX (const qbyte *f, int matchwidth, int matchheight) image_width = pcx.xmax+1; image_height = pcx.ymax+1; - palette = f + loadsize - 768; + palette = f + fs_filesize - 768; image_rgba = Mem_Alloc(tempmempool, image_width*image_height*4); if (!image_rgba) @@ -231,10 +231,10 @@ qbyte *LoadTGA (const qbyte *f, int matchwidth, int matchheight) TargaHeader targa_header; unsigned char palette[256*4], *p; - if (loadsize < 19) + if (fs_filesize < 19) return NULL; - enddata = f + loadsize; + enddata = f + fs_filesize; targa_header.id_length = f[0]; targa_header.colormap_type = f[1]; @@ -441,7 +441,7 @@ qbyte *LoadLMP (const qbyte *f, int matchwidth, int matchheight) qbyte *image_rgba; int width, height; - if (loadsize < 9) + if (fs_filesize < 9) { Con_Printf("LoadLMP: invalid LMP file\n"); return NULL; @@ -458,7 +458,7 @@ qbyte *LoadLMP (const qbyte *f, int matchwidth, int matchheight) if ((matchwidth && width != matchwidth) || (matchheight && height != matchheight)) return NULL; - if (loadsize < 8 + width * height) + if (fs_filesize < 8 + width * height) { Con_Printf("LoadLMP: invalid LMP file\n"); return NULL; @@ -487,7 +487,7 @@ qbyte *LoadLMPAs8Bit (const qbyte *f, int matchwidth, int matchheight) qbyte *image_8bit; int width, height; - if (loadsize < 9) + if (fs_filesize < 9) { Con_Printf("LoadLMPAs8Bit: invalid LMP file\n"); return NULL; @@ -504,7 +504,7 @@ qbyte *LoadLMPAs8Bit (const qbyte *f, int matchwidth, int matchheight) if ((matchwidth && width != matchwidth) || (matchheight && height != matchheight)) return NULL; - if (loadsize < 8 + width * height) + if (fs_filesize < 8 + width * height) { Con_Printf("LoadLMPAs8Bit: invalid LMP file\n"); return NULL; @@ -550,63 +550,63 @@ qbyte *loadimagepixels (const char *filename, qboolean complain, int matchwidth, if (*c == '*') *c = '#'; sprintf (name, "override/%s.tga", basename); - f = COM_LoadFile(name, true); + f = FS_LoadFile(name, true); if (f) { data = LoadTGA (f, matchwidth, matchheight); goto loaded; } sprintf (name, "override/%s.jpg", basename); - f = COM_LoadFile(name, true); + f = FS_LoadFile(name, true); if (f) { data = JPEG_LoadImage (f, matchwidth, matchheight); goto loaded; } sprintf (name, "textures/%s.tga", basename); - f = COM_LoadFile(name, true); + f = FS_LoadFile(name, true); if (f) { data = LoadTGA (f, matchwidth, matchheight); goto loaded; } sprintf (name, "textures/%s.jpg", basename); - f = COM_LoadFile(name, true); + f = FS_LoadFile(name, true); if (f) { data = JPEG_LoadImage (f, matchwidth, matchheight); goto loaded; } sprintf (name, "textures/%s.pcx", basename); - f = COM_LoadFile(name, true); + f = FS_LoadFile(name, true); if (f) { data = LoadPCX (f, matchwidth, matchheight); goto loaded; } sprintf (name, "%s.tga", basename); - f = COM_LoadFile(name, true); + f = FS_LoadFile(name, true); if (f) { data = LoadTGA (f, matchwidth, matchheight); goto loaded; } sprintf (name, "%s.jpg", basename); - f = COM_LoadFile(name, true); + f = FS_LoadFile(name, true); if (f) { data = JPEG_LoadImage (f, matchwidth, matchheight); goto loaded; } sprintf (name, "%s.pcx", basename); - f = COM_LoadFile(name, true); + f = FS_LoadFile(name, true); if (f) { data = LoadPCX (f, matchwidth, matchheight); goto loaded; } sprintf (name, "%s.lmp", basename); - f = COM_LoadFile(name, true); + f = FS_LoadFile(name, true); if (f) { data = LoadLMP (f, matchwidth, matchheight); @@ -768,7 +768,7 @@ qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int heig *out++ = in[1]; *out++ = in[0]; } - ret = COM_WriteFile (filename, buffer, width*height*3 + 18 ); + ret = FS_WriteFile (filename, buffer, width*height*3 + 18 ); Mem_Free(buffer); return ret; @@ -803,7 +803,7 @@ void Image_WriteTGARGB (const char *filename, int width, int height, const qbyte *out++ = in[0]; } } - COM_WriteFile (filename, buffer, width*height*3 + 18 ); + FS_WriteFile (filename, buffer, width*height*3 + 18 ); Mem_Free(buffer); } @@ -838,7 +838,7 @@ void Image_WriteTGARGBA (const char *filename, int width, int height, const qbyt *out++ = in[3]; } } - COM_WriteFile (filename, buffer, width*height*4 + 18 ); + FS_WriteFile (filename, buffer, width*height*4 + 18 ); Mem_Free(buffer); } diff --git a/jpeg.c b/jpeg.c index 85e0a719..3519ab19 100644 --- a/jpeg.c +++ b/jpeg.c @@ -359,7 +359,7 @@ static void JPEG_MemSrc (j_decompress_ptr cinfo, qbyte *buffer) cinfo->src = (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof (struct jpeg_source_mgr)); cinfo->src->next_input_byte = buffer; - cinfo->src->bytes_in_buffer = com_filesize; + cinfo->src->bytes_in_buffer = fs_filesize; cinfo->src->init_source = JPEG_Noop; cinfo->src->fill_input_buffer = JPEG_FillInputBuffer; diff --git a/keys.c b/keys.c index 9b018bde..b49e2ac1 100644 --- a/keys.c +++ b/keys.c @@ -509,7 +509,7 @@ int Key_StringToKeynum (const char *str) return str[0]; for (kn=keynames ; kn->name ; kn++) - if (!Q_strcasecmp(str,kn->name)) + if (!strcasecmp(str,kn->name)) return kn->keynum; return -1; } @@ -661,13 +661,13 @@ Key_WriteBindings Writes lines containing "bind key value" ============ */ -void Key_WriteBindings (QFile *f) +void Key_WriteBindings (qfile_t *f) { int i; for (i = 0;i < 256;i++) if (keybindings[i] && *keybindings[i]) - Qprintf (f, "bind \"%s\" \"%s\"\n", Key_KeynumToString(i), keybindings[i]); + FS_Printf (f, "bind \"%s\" \"%s\"\n", Key_KeynumToString(i), keybindings[i]); } diff --git a/keys.h b/keys.h index fcd39ce4..37141c0b 100644 --- a/keys.h +++ b/keys.h @@ -151,7 +151,7 @@ extern keydest_t key_dest; void Key_Event (int key, qboolean down); void Key_Init (void); -void Key_WriteBindings (QFile *f); +void Key_WriteBindings (qfile_t *f); void Key_SetBinding (int keynum, char *binding); void Key_ClearStates (void); diff --git a/makefile b/makefile index 507f6233..9da3b6b6 100644 --- a/makefile +++ b/makefile @@ -50,7 +50,7 @@ SHAREDOBJECTS= builddate.o cmd.o collision.o common.o crc.o cvar.o \ filematch.o host.o host_cmd.o image.o mathlib.o matrixlib.o \ model_alias.o model_brush.o model_shared.o model_sprite.o \ net_bsd.o net_dgrm.o net_loop.o net_main.o net_master.o \ - net_udp.o palette.o portals.o protocol.o quakeio.o sys_linux.o \ + net_udp.o palette.o portals.o protocol.o fs.o sys_linux.o \ sys_shared.o world.o wad.o zone.o OBJ_COMMON= $(CLIENTOBJECTS) $(SERVEROBJECTS) $(SHAREDOBJECTS) diff --git a/makefile.mingw b/makefile.mingw index 0c08811f..c8957585 100644 --- a/makefile.mingw +++ b/makefile.mingw @@ -7,7 +7,7 @@ OBJECTS= builddate.o chase.o cl_demo.o cl_input.o cl_main.o cl_parse.o cmd.o \ sbar.o snd_dma.o snd_mem.o snd_mix.o sv_main.o sv_move.o \ sv_phys.o sv_user.o sv_light.o view.o wad.o world.o zone.o vid_shared.o \ palette.o r_crosshairs.o gl_textures.o gl_models.o r_sprites.o \ - r_modules.o r_explosion.o r_lerpanim.o protocol.o quakeio.o ui.o \ + r_modules.o r_explosion.o r_lerpanim.o protocol.o fs.o ui.o \ portals.o sys_shared.o gl_backend.o cl_particles.o cl_screen.o cgamevm.o \ cgame.o filematch.o collision.o cl_collision.o matrixlib.o cl_video.o \ dpvsimpledecode.o wavefile.o meshqueue.o net_master.o r_shadow.o jpeg.o diff --git a/makefile.mingwcross b/makefile.mingwcross index 16642b4a..ce0d6105 100644 --- a/makefile.mingwcross +++ b/makefile.mingwcross @@ -37,7 +37,7 @@ SHAREDOBJECTS= builddate.o cmd.o collision.o common.o crc.o cvar.o \ filematch.o host.o host_cmd.o image.o mathlib.o matrixlib.o \ model_alias.o model_brush.o model_shared.o model_sprite.o \ net_dgrm.o net_loop.o net_main.o net_master.o \ - palette.o portals.o protocol.o quakeio.o \ + palette.o portals.o protocol.o fs.o \ sys_shared.o world.o wad.o zone.o diff --git a/menu.c b/menu.c index f9407669..f4bda352 100644 --- a/menu.c +++ b/menu.c @@ -676,20 +676,20 @@ void M_ScanSaves (void) int i, j; char name[MAX_OSPATH]; char *str; - QFile *f; + qfile_t *f; int version; for (i=0 ; iname); - COM_StripExtension(litfilename, litfilename); + FS_StripExtension(litfilename, litfilename); strcat(litfilename, ".lit"); - data = (qbyte*) COM_LoadFile (litfilename, false); + data = (qbyte*) FS_LoadFile (litfilename, false); if (data) { - if (loadsize > 8 && data[0] == 'Q' && data[1] == 'L' && data[2] == 'I' && data[3] == 'T') + if (fs_filesize > 8 && data[0] == 'Q' && data[1] == 'L' && data[2] == 'I' && data[3] == 'T') { i = LittleLong(((int *)data)[1]); if (i == 1) { Con_DPrintf("loaded %s\n", litfilename); - loadmodel->lightdata = Mem_Alloc(loadmodel->mempool, loadsize - 8); - memcpy(loadmodel->lightdata, data + 8, loadsize - 8); + loadmodel->lightdata = Mem_Alloc(loadmodel->mempool, fs_filesize - 8); + memcpy(loadmodel->lightdata, data + 8, fs_filesize - 8); Mem_Free(data); return; } @@ -641,7 +641,7 @@ static void Mod_LoadLighting (lump_t *l) } else { - if (loadsize == 8) + if (fs_filesize == 8) Con_Printf("Empty .lit file, ignoring\n"); else Con_Printf("Corrupt .lit file (old version?), ignoring\n"); @@ -672,9 +672,9 @@ void Mod_LoadLightList(void) mlight_t *e; strcpy(lightsfilename, loadmodel->name); - COM_StripExtension(lightsfilename, lightsfilename); + FS_StripExtension(lightsfilename, lightsfilename); strcat(lightsfilename, ".lights"); - s = lightsstring = (char *) COM_LoadFile (lightsfilename, false); + s = lightsstring = (char *) FS_LoadFile (lightsfilename, false); if (s) { numlights = 0; diff --git a/model_shared.c b/model_shared.c index 97efa1f5..05e7edd3 100644 --- a/model_shared.c +++ b/model_shared.c @@ -199,7 +199,7 @@ static model_t *Mod_LoadModel (model_t *mod, qboolean crash, qboolean checkdisk, { if (checkdisk) { - buf = COM_LoadFile (mod->name, false); + buf = FS_LoadFile (mod->name, false); if (!buf) { if (crash) @@ -207,7 +207,7 @@ static model_t *Mod_LoadModel (model_t *mod, qboolean crash, qboolean checkdisk, return NULL; } - crc = CRC_Block(buf, com_filesize); + crc = CRC_Block(buf, fs_filesize); } else crc = mod->crc; @@ -224,14 +224,14 @@ static model_t *Mod_LoadModel (model_t *mod, qboolean crash, qboolean checkdisk, if (!buf) { - buf = COM_LoadFile (mod->name, false); + buf = FS_LoadFile (mod->name, false); if (!buf) { if (crash) Host_Error ("Mod_LoadModel: %s not found", mod->name); return NULL; } - crc = CRC_Block(buf, com_filesize); + crc = CRC_Block(buf, fs_filesize); } // allocate a new model diff --git a/net_dgrm.c b/net_dgrm.c index cf72d84d..31b654a8 100644 --- a/net_dgrm.c +++ b/net_dgrm.c @@ -136,7 +136,7 @@ void NET_Ban_f (void) break; case 2: - if (Q_strcasecmp(Cmd_Argv(1), "off") == 0) + if (strcasecmp(Cmd_Argv(1), "off") == 0) banAddr = 0x00000000; else banAddr = inet_addr(Cmd_Argv(1)); @@ -491,7 +491,7 @@ void NET_Stats_f (void) else { for (s = net_activeSockets; s; s = s->next) - if (Q_strcasecmp(Cmd_Argv(1), s->address) == 0) + if (strcasecmp(Cmd_Argv(1), s->address) == 0) break; if (s == NULL) return; @@ -581,7 +581,7 @@ static void Test_f (void) if (host && hostCacheCount) { for (n = 0; n < hostCacheCount; n++) - if (Q_strcasecmp (host, hostcache[n].name) == 0) + if (strcasecmp (host, hostcache[n].name) == 0) { if (hostcache[n].driver != myDriverLevel) continue; @@ -711,7 +711,7 @@ static void Test2_f (void) if (host && hostCacheCount) { for (n = 0; n < hostCacheCount; n++) - if (Q_strcasecmp (host, hostcache[n].name) == 0) + if (strcasecmp (host, hostcache[n].name) == 0) { if (hostcache[n].driver != myDriverLevel) continue; @@ -1156,7 +1156,7 @@ static qboolean Datagram_HandleServerInfo (struct qsockaddr *readaddr) { if (i == n) continue; - if (Q_strcasecmp (hostcache[n].name, hostcache[i].name) == 0) + if (strcasecmp (hostcache[n].name, hostcache[i].name) == 0) { i = strlen(hostcache[n].name); if (i < 15 && hostcache[n].name[i-1] > '8') diff --git a/net_main.c b/net_main.c index 836d2d04..e29480a3 100644 --- a/net_main.c +++ b/net_main.c @@ -525,7 +525,7 @@ qsocket_t *NET_Connect (char *host) if (host) { - if (Q_strcasecmp (host, "local") == 0) + if (strcasecmp (host, "local") == 0) { net_driverlevel = 0; return dfunc.Connect (host); @@ -534,7 +534,7 @@ qsocket_t *NET_Connect (char *host) if (hostCacheCount) { for (n = 0; n < hostCacheCount; n++) - if (Q_strcasecmp (host, hostcache[n].name) == 0) + if (strcasecmp (host, hostcache[n].name) == 0) { host = hostcache[n].cname; break; @@ -560,7 +560,7 @@ qsocket_t *NET_Connect (char *host) if (hostCacheCount) for (n = 0; n < hostCacheCount; n++) - if (Q_strcasecmp (host, hostcache[n].name) == 0) + if (strcasecmp (host, hostcache[n].name) == 0) { host = hostcache[n].cname; break; diff --git a/palette.c b/palette.c index b08118b8..1a481e3f 100644 --- a/palette.c +++ b/palette.c @@ -35,8 +35,8 @@ void Palette_Setup8to24(void) palette_complete[255] = 0; // completely transparent black // FIXME: fullbright_start should be read from colormap.lmp - colormap = COM_LoadFile("gfx/colormap.lmp", true); - if (colormap && com_filesize >= 16385) + colormap = FS_LoadFile("gfx/colormap.lmp", true); + if (colormap && fs_filesize >= 16385) fullbright_start = 256 - colormap[16384]; else fullbright_start = 256; @@ -171,7 +171,7 @@ void Palette_Init(void) float gamma, scale, base; qbyte *pal; qbyte temp[256]; - pal = (qbyte *)COM_LoadFile ("gfx/palette.lmp", false); + pal = (qbyte *)FS_LoadFile ("gfx/palette.lmp", false); if (!pal) Sys_Error ("Couldn't load gfx/palette.lmp"); memcpy(host_basepal, pal, 765); diff --git a/pr_edict.c b/pr_edict.c index 374a9648..db12799b 100644 --- a/pr_edict.c +++ b/pr_edict.c @@ -622,7 +622,7 @@ ED_Write For savegames ============= */ -void ED_Write (QFile *f, edict_t *ed) +void ED_Write (qfile_t *f, edict_t *ed) { ddef_t *d; int *v; @@ -630,11 +630,11 @@ void ED_Write (QFile *f, edict_t *ed) char *name; int type; - Qprintf (f, "{\n"); + FS_Printf (f, "{\n"); if (ed->free) { - Qprintf (f, "}\n"); + FS_Printf (f, "}\n"); return; } @@ -655,11 +655,11 @@ void ED_Write (QFile *f, edict_t *ed) if (j == type_size[type]) continue; - Qprintf (f,"\"%s\" ",name); - Qprintf (f,"\"%s\"\n", PR_UglyValueString(d->type, (eval_t *)v)); + FS_Printf (f,"\"%s\" ",name); + FS_Printf (f,"\"%s\"\n", PR_UglyValueString(d->type, (eval_t *)v)); } - Qprintf (f, "}\n"); + FS_Printf (f, "}\n"); } void ED_PrintNum (int ent) @@ -753,14 +753,14 @@ FIXME: need to tag constants, doesn't really work ED_WriteGlobals ============= */ -void ED_WriteGlobals (QFile *f) +void ED_WriteGlobals (qfile_t *f) { ddef_t *def; int i; char *name; int type; - Qprintf (f,"{\n"); + FS_Printf (f,"{\n"); for (i=0 ; inumglobaldefs ; i++) { def = &pr_globaldefs[i]; @@ -773,10 +773,10 @@ void ED_WriteGlobals (QFile *f) continue; name = PR_GetString(def->s_name); - Qprintf (f,"\"%s\" ", name); - Qprintf (f,"\"%s\"\n", PR_UglyValueString(type, (eval_t *)&pr_globals[def->ofs])); + FS_Printf (f,"\"%s\" ", name); + FS_Printf (f,"\"%s\"\n", PR_UglyValueString(type, (eval_t *)&pr_globals[def->ofs])); } - Qprintf (f,"}\n"); + FS_Printf (f,"}\n"); } /* @@ -1181,18 +1181,18 @@ void PR_LoadProgs (void) Mem_EmptyPool(progs_mempool); Mem_EmptyPool(edictstring_mempool); - temp = COM_LoadFile ("progs.dat", false); + temp = FS_LoadFile ("progs.dat", false); if (!temp) Host_Error ("PR_LoadProgs: couldn't load progs.dat"); - progs = (dprograms_t *)Mem_Alloc(progs_mempool, com_filesize); + progs = (dprograms_t *)Mem_Alloc(progs_mempool, fs_filesize); - memcpy(progs, temp, com_filesize); + memcpy(progs, temp, fs_filesize); Mem_Free(temp); - Con_DPrintf ("Programs occupy %iK.\n", com_filesize/1024); + Con_DPrintf ("Programs occupy %iK.\n", fs_filesize/1024); - pr_crc = CRC_Block((qbyte *)progs, com_filesize); + pr_crc = CRC_Block((qbyte *)progs, fs_filesize); // byte swap the header for (i = 0;i < (int) sizeof(*progs) / 4;i++) diff --git a/progs.h b/progs.h index f9d1bde4..702c4459 100644 --- a/progs.h +++ b/progs.h @@ -149,10 +149,10 @@ char *ED_NewString (const char *string); // returns a copy of the string allocated from the server's string heap void ED_Print (edict_t *ed); -void ED_Write (QFile *f, edict_t *ed); +void ED_Write (qfile_t *f, edict_t *ed); const char *ED_ParseEdict (const char *data, edict_t *ent); -void ED_WriteGlobals (QFile *f); +void ED_WriteGlobals (qfile_t *f); void ED_ParseGlobals (const char *data); void ED_LoadFromFile (const char *data); diff --git a/quakedef.h b/quakedef.h index f1d6d5ed..4ebc22f2 100644 --- a/quakedef.h +++ b/quakedef.h @@ -156,7 +156,7 @@ extern char *buildstring; #define SOUND_CHANNELS 8 #include "zone.h" -#include "quakeio.h" +#include "fs.h" #include "common.h" #include "cvar.h" #include "bspfile.h" diff --git a/quakeio.c b/quakeio.c deleted file mode 100644 index d3dd6cfd..00000000 --- a/quakeio.c +++ /dev/null @@ -1,401 +0,0 @@ -/* - quakeio.c - - (description) - - Copyright (C) 1996-1997 Id Software, Inc. - Copyright (C) 1999,2000 contributors of the QuakeForge project - Please see the file "AUTHORS" for a list of contributors - - 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 - - $Id$ -*/ - -#include "quakedef.h" -#include -#include -#ifdef WIN32 -# include -# include -#else -# include -# include -#endif - -#include -#include -#include - -#ifndef PATH_MAX -# define PATH_MAX 512 -#endif - -#include "quakeio.h" - -#ifdef WIN32 -# ifndef __BORLANDC__ -# define setmode _setmode -# define O_BINARY _O_BINARY -# endif -#endif - -mempool_t *quakeio_mempool; - -void -Qexpand_squiggle (const char *path, char *dest) -{ - char *home; - -#ifndef _WIN32 - struct passwd *pwd_ent; -#endif - - if (strncmp (path, "~/", 2) != 0) { - strcpy (dest, path); - return; - } - -#ifdef _WIN32 - // LordHavoc: first check HOME to duplicate previous version behavior - // (also handy if someone wants it somewhere other than their - // windows directory) - home = getenv ("HOME"); - if (!home || !home[0]) - home = getenv ("WINDIR"); -#else - if ((pwd_ent = getpwuid (getuid ()))) { - home = pwd_ent->pw_dir; - } else - home = getenv ("HOME"); -#endif - - if (home) { - strcpy (dest, home); - strncat (dest, path + 1, MAX_OSPATH - strlen (dest)); // skip - // leading ~ - } else - strcpy (dest, path); -} - -int -Qrename (const char *old, const char *new) -{ - char e_old[PATH_MAX]; - char e_new[PATH_MAX]; - - Qexpand_squiggle (old, e_old); - Qexpand_squiggle (new, e_new); - return rename (e_old, e_new); -} - -QFile * -Qopen (const char *path, const char *mode) -{ - QFile *file; - char m[80], *p; - int zip = 0; - char e_path[PATH_MAX]; - - Qexpand_squiggle (path, e_path); - path = e_path; - - for (p = m; *mode && p - m < (int)(sizeof (m) - 1); mode++) { - if (*mode == 'z') { - zip = 1; - continue; - } -#ifndef HAVE_ZLIB - if (strchr ("0123456789fh", *mode)) { - continue; - } -#endif - *p++ = *mode; - } - *p = 0; - - file = Mem_Alloc(quakeio_mempool, sizeof (*file)); - memset(file, 0, sizeof(*file)); - if (!file) - return 0; -#ifdef HAVE_ZLIB - if (zip) { - file->gzfile = gzopen (path, m); - if (!file->gzfile) { - Mem_Free(file); - return 0; - } - } else -#endif - { - file->file = fopen (path, m); - if (!file->file) { - Mem_Free(file); - return 0; - } - } - return file; -} - -QFile * -Qdopen (int fd, const char *mode) -{ - QFile *file; - char m[80], *p; - int zip = 0; - - for (p = m; *mode && p - m < (int)(sizeof (m) - 1); mode++) { - if (*mode == 'z') { - zip = 1; - continue; - } - *p++ = *mode; - } - - *p = 0; - - file = Mem_Alloc(quakeio_mempool, sizeof (*file)); - memset(file, 0, sizeof(*file)); - if (!file) - return 0; -#ifdef HAVE_ZLIB - if (zip) { - file->gzfile = gzdopen (fd, m); - if (!file->gzfile) { - Mem_Free(file); - return 0; - } - } else -#endif - { - file->file = fdopen (fd, m); - if (!file->file) { - Mem_Free(file); - return 0; - } - } -#ifdef WIN32 - if (file->file) - setmode (_fileno (file->file), O_BINARY); -#endif - return file; -} - -void -Qclose (QFile *file) -{ - if (file->file) - fclose (file->file); -#ifdef HAVE_ZLIB - else - gzclose (file->gzfile); -#endif - Mem_Free(file); -} - -int -Qread (QFile *file, void *buf, int count) -{ - if (file->file) - return fread (buf, 1, count, file->file); -#ifdef HAVE_ZLIB - else - return gzread (file->gzfile, buf, count); -#else - return -1; -#endif -} - -int -Qwrite (QFile *file, void *buf, int count) -{ - if (file->file) - return fwrite (buf, 1, count, file->file); -#ifdef HAVE_ZLIB - else - return gzwrite (file->gzfile, buf, count); -#else - return -1; -#endif -} - -int -Qprintf (QFile *file, const char *fmt, ...) -{ - va_list args; - int ret = -1; - - va_start (args, fmt); - if (file->file) - ret = vfprintf (file->file, fmt, args); -#ifdef HAVE_ZLIB - else { - char buf[4096]; - - va_start (args, fmt); -#ifdef HAVE_VSNPRINTF - (void) vsnprintf (buf, sizeof (buf), fmt, args); -#else - (void) vsprintf (buf, fmt, args); -#endif - va_end (args); - ret = strlen (buf); /* some *snprintf don't return the nb - of bytes written */ - if (ret > 0) - ret = gzwrite (file->gzfile, buf, (unsigned) ret); - } -#endif - va_end (args); - return ret; -} - -char * -Qgets (QFile *file, char *buf, int count) -{ - if (file->file) - return fgets (buf, count, file->file); -#ifdef HAVE_ZLIB - else - return gzgets (file->gzfile, buf, count); -#else - return 0; -#endif -} - -int -Qgetc (QFile *file) -{ - if (file->file) - return fgetc (file->file); -#ifdef HAVE_ZLIB - else - return gzgetc (file->gzfile); -#else - return -1; -#endif -} - -int -Qputc (QFile *file, int c) -{ - if (file->file) - return fputc (c, file->file); -#ifdef HAVE_ZLIB - else - return gzputc (file->gzfile, c); -#else - return -1; -#endif -} - -int -Qseek (QFile *file, long offset, int whence) -{ - if (file->file) - return fseek (file->file, offset, whence); -#ifdef HAVE_ZLIB - else - return gzseek (file->gzfile, offset, whence); -#else - return -1; -#endif -} - -long -Qtell (QFile *file) -{ - if (file->file) - return ftell (file->file); -#ifdef HAVE_ZLIB - else - return gztell (file->gzfile); -#else - return -1; -#endif -} - -int -Qflush (QFile *file) -{ - if (file->file) - return fflush (file->file); -#ifdef HAVE_ZLIB - else - return gzflush (file->gzfile, Z_SYNC_FLUSH); -#else - return -1; -#endif -} - -int -Qeof (QFile *file) -{ - if (file->file) - return feof (file->file); -#ifdef HAVE_ZLIB - else - return gzeof (file->gzfile); -#else - return -1; -#endif -} - -/* - - Qgetline - - Dynamic length version of Qgets. DO NOT free the buffer. - -*/ -char * -Qgetline (QFile *file) -{ - static int size = 256; - static char *buf = 0; - char *t; - int len; - - if (!buf) - buf = Mem_Alloc(quakeio_mempool, size); - - if (!Qgets (file, buf, size)) - return 0; - - len = strlen (buf); - while (buf[len - 1] != '\n' && buf[len - 1] != '\r') - { - t = Mem_Alloc(quakeio_mempool, size + 256); - memcpy(t, buf, size); - Mem_Free(buf); - size += 256; - buf = t; - if (!Qgets (file, buf + len, size - len)) - break; - len = strlen (buf); - } - while ((len = strlen(buf)) && (buf[len - 1] == '\n' || buf[len - 1] == '\r')) - buf[len - 1] = 0; - return buf; -} - -void QuakeIO_Init(void) -{ - quakeio_mempool = Mem_AllocPool("file management"); -} - diff --git a/quakeio.h b/quakeio.h deleted file mode 100644 index cf8a9b50..00000000 --- a/quakeio.h +++ /dev/null @@ -1,75 +0,0 @@ -/* - quakeio.h - - (description) - - Copyright (C) 1996-1997 Id Software, Inc. - Copyright (C) 1999,2000 contributors of the QuakeForge project - Please see the file "AUTHORS" for a list of contributors - - 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 - - $Id$ -*/ -#ifndef __quakeio_h -#define __quakeio_h - -//#define HAVE_ZLIB - -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - -#include - -#ifdef HAVE_ZLIB -# include -# ifdef WIN32 -# undef FAR -# endif -#endif - -//#include "QF/gcc_attr.h" - -typedef struct { - FILE *file; -#ifdef HAVE_ZLIB - gzFile *gzfile; -#endif -} QFile; - -void Qexpand_squiggle(const char *path, char *dest); -int Qrename(const char *old, const char *new); -QFile *Qopen(const char *path, const char *mode); -QFile *Qdopen(int fd, const char *mode); -void Qclose(QFile *file); -int Qread(QFile *file, void *buf, int count); -int Qwrite(QFile *file, void *buf, int count); -//int Qprintf(QFile *file, const char *fmt, ...) __attribute__((format(printf,2,3))); -int Qprintf(QFile *file, const char *fmt, ...); -char *Qgets(QFile *file, char *buf, int count); -int Qgetc(QFile *file); -int Qputc(QFile *file, int c); -int Qseek(QFile *file, long offset, int whence); -long Qtell(QFile *file); -int Qflush(QFile *file); -int Qeof(QFile *file); -char *Qgetline(QFile *file); - -#endif /*__quakeio_h*/ diff --git a/r_shadow.c b/r_shadow.c index 2036a8b5..de429d0b 100644 --- a/r_shadow.c +++ b/r_shadow.c @@ -1963,9 +1963,9 @@ void R_Shadow_LoadWorldLights(void) Con_Printf("No map loaded.\n"); return; } - COM_StripExtension(cl.worldmodel->name, name); + FS_StripExtension(cl.worldmodel->name, name); strcat(name, ".rtlights"); - lightsstring = COM_LoadFile(name, false); + lightsstring = FS_LoadFile(name, false); if (lightsstring) { s = lightsstring; @@ -2020,7 +2020,7 @@ void R_Shadow_SaveWorldLights(void) Con_Printf("No map loaded.\n"); return; } - COM_StripExtension(cl.worldmodel->name, name); + FS_StripExtension(cl.worldmodel->name, name); strcat(name, ".rtlights"); bufchars = bufmaxchars = 0; buf = NULL; @@ -2046,7 +2046,7 @@ void R_Shadow_SaveWorldLights(void) } } if (bufchars) - COM_WriteFile(name, buf, bufchars); + FS_WriteFile(name, buf, bufchars); if (buf) Mem_Free(buf); } @@ -2061,9 +2061,9 @@ void R_Shadow_LoadLightsFile(void) Con_Printf("No map loaded.\n"); return; } - COM_StripExtension(cl.worldmodel->name, name); + FS_StripExtension(cl.worldmodel->name, name); strcat(name, ".lights"); - lightsstring = COM_LoadFile(name, false); + lightsstring = FS_LoadFile(name, false); if (lightsstring) { s = lightsstring; diff --git a/snd_mem.c b/snd_mem.c index c8c0bdaa..47398c6e 100644 --- a/snd_mem.c +++ b/snd_mem.c @@ -228,7 +228,7 @@ sfxcache_t *S_LoadSound (sfx_t *s, int complain) strcpy(namebuffer, "sound/"); strcat(namebuffer, s->name); - data = COM_LoadFile(namebuffer, false); + data = FS_LoadFile(namebuffer, false); if (!data) { @@ -237,7 +237,7 @@ sfxcache_t *S_LoadSound (sfx_t *s, int complain) return NULL; } - info = GetWavinfo (s->name, data, com_filesize); + info = GetWavinfo (s->name, data, fs_filesize); // LordHavoc: stereo sounds are now allowed (intended for music) if (info.channels < 1 || info.channels > 2) { diff --git a/snd_mix.c b/snd_mix.c index a006f68f..790642cc 100644 --- a/snd_mix.c +++ b/snd_mix.c @@ -41,7 +41,7 @@ void S_CaptureAVISound(portable_samplepair_t *buf, int length) if (cl_avidemo_soundfile == NULL) { sprintf (filename, "%s/dpavi.wav", com_gamedir); - cl_avidemo_soundfile = fopen(filename, "wb"); + cl_avidemo_soundfile = FS_Open (filename, "wb", false); memset(out, 0, 44); fwrite(out, 1, 44, cl_avidemo_soundfile); // header will be filled out when file is closed diff --git a/sv_main.c b/sv_main.c index 502a630e..5941b55e 100644 --- a/sv_main.c +++ b/sv_main.c @@ -1868,7 +1868,7 @@ void SV_SpawnServer (const char *server) // load replacement entity file if found entities = NULL; if (sv_entpatch.integer) - entities = COM_LoadFile(va("maps/%s.ent", sv.name), true); + entities = FS_LoadFile(va("maps/%s.ent", sv.name), true); if (entities) { Con_Printf("Loaded maps/%s.ent\n", sv.name); diff --git a/sv_user.c b/sv_user.c index 135a6d50..06ef02ea 100644 --- a/sv_user.c +++ b/sv_user.c @@ -580,23 +580,23 @@ nextmsg: case clc_stringcmd: s = MSG_ReadString (); ret = 0; - if (Q_strncasecmp(s, "status", 6) == 0 - || Q_strncasecmp(s, "name", 4) == 0 - || Q_strncasecmp(s, "say", 3) == 0 - || Q_strncasecmp(s, "say_team", 8) == 0 - || Q_strncasecmp(s, "tell", 4) == 0 - || Q_strncasecmp(s, "color", 5) == 0 - || Q_strncasecmp(s, "kill", 4) == 0 - || Q_strncasecmp(s, "pause", 5) == 0 - || Q_strncasecmp(s, "spawn", 5) == 0 - || Q_strncasecmp(s, "begin", 5) == 0 - || Q_strncasecmp(s, "prespawn", 8) == 0 - || Q_strncasecmp(s, "kick", 4) == 0 - || Q_strncasecmp(s, "ping", 4) == 0 - || Q_strncasecmp(s, "ban", 3) == 0 - || Q_strncasecmp(s, "pmodel", 6) == 0 - || (gamemode == GAME_NEHAHRA && (Q_strncasecmp(s, "max", 3) == 0 || Q_strncasecmp(s, "monster", 7) == 0 || Q_strncasecmp(s, "scrag", 5) == 0 || Q_strncasecmp(s, "gimme", 5) == 0 || Q_strncasecmp(s, "wraith", 6) == 0)) - || (gamemode != GAME_NEHAHRA && (Q_strncasecmp(s, "god", 3) == 0 || Q_strncasecmp(s, "notarget", 8) == 0 || Q_strncasecmp(s, "fly", 3) == 0 || Q_strncasecmp(s, "give", 4) == 0 || Q_strncasecmp(s, "noclip", 6) == 0))) + if (strncasecmp(s, "status", 6) == 0 + || strncasecmp(s, "name", 4) == 0 + || strncasecmp(s, "say", 3) == 0 + || strncasecmp(s, "say_team", 8) == 0 + || strncasecmp(s, "tell", 4) == 0 + || strncasecmp(s, "color", 5) == 0 + || strncasecmp(s, "kill", 4) == 0 + || strncasecmp(s, "pause", 5) == 0 + || strncasecmp(s, "spawn", 5) == 0 + || strncasecmp(s, "begin", 5) == 0 + || strncasecmp(s, "prespawn", 8) == 0 + || strncasecmp(s, "kick", 4) == 0 + || strncasecmp(s, "ping", 4) == 0 + || strncasecmp(s, "ban", 3) == 0 + || strncasecmp(s, "pmodel", 6) == 0 + || (gamemode == GAME_NEHAHRA && (strncasecmp(s, "max", 3) == 0 || strncasecmp(s, "monster", 7) == 0 || strncasecmp(s, "scrag", 5) == 0 || strncasecmp(s, "gimme", 5) == 0 || strncasecmp(s, "wraith", 6) == 0)) + || (gamemode != GAME_NEHAHRA && (strncasecmp(s, "god", 3) == 0 || strncasecmp(s, "notarget", 8) == 0 || strncasecmp(s, "fly", 3) == 0 || strncasecmp(s, "give", 4) == 0 || strncasecmp(s, "noclip", 6) == 0))) { ret = 1; Cmd_ExecuteString (s, src_client); diff --git a/sys.h b/sys.h index cc0ced45..91eeb2c0 100644 --- a/sys.h +++ b/sys.h @@ -49,23 +49,6 @@ void Sys_UnloadLibrary (dllhandle_t handle); void* Sys_GetProcAddress (dllhandle_t handle, const char* name); -// -// file IO -// - -// returns the file size -// return -1 if file is not present -// the file should be in BINARY mode for stupid OSs that care -int Sys_FileOpenRead (const char *path, int *hndl); - -int Sys_FileOpenWrite (const char *path); -void Sys_FileClose (int handle); -void Sys_FileSeek (int handle, int position); -int Sys_FileRead (int handle, void *dest, int count); -int Sys_FileWrite (int handle, void *data, int count); -int Sys_FileTime (const char *path); -void Sys_mkdir (const char *path); - // // system IO // diff --git a/sys_shared.c b/sys_shared.c index 76c32fa7..f6f6804f 100644 --- a/sys_shared.c +++ b/sys_shared.c @@ -1,14 +1,10 @@ #include "quakedef.h" #include -#ifdef WIN32 -#include -#else -#include +#ifndef WIN32 #include #include #endif -#include extern cvar_t timestamps; extern cvar_t timeformat; @@ -73,11 +69,7 @@ void Sys_Printf (const char *fmt, ...) #endif va_start (argptr, fmt); -#ifdef HAVE_VSNPRINTF vsnprintf (start, sizeof(start), fmt, argptr); -#else - vsprintf (start, fmt, argptr); -#endif va_end (argptr); if (sys_nostdout) @@ -106,149 +98,26 @@ void Sys_Printf (const char *fmt, ...) #endif } -// LordHavoc: 256 pak files (was 10) -#define MAX_HANDLES 256 -QFile *sys_handles[MAX_HANDLES]; - -int findhandle (void) -{ - int i; - - for (i = 1;i < MAX_HANDLES;i++) - if (!sys_handles[i]) - return i; - Sys_Error ("out of handles"); - return -1; -} - -/* -================ -Sys_FileLength -================ -*/ -int Sys_FileLength (QFile *f) -{ - int pos, end; - - pos = Qtell (f); - Qseek (f, 0, SEEK_END); - end = Qtell (f); - Qseek (f, pos, SEEK_SET); - - return end; -} - -int Sys_FileOpenRead (const char *path, int *handle) -{ - QFile *f; - int i, retval; - - i = findhandle (); - - f = Qopen(path, "rbz"); - - if (!f) - { - *handle = -1; - retval = -1; - } - else - { - sys_handles[i] = f; - *handle = i; - retval = Sys_FileLength(f); - } - - return retval; -} - -int Sys_FileOpenWrite (const char *path) -{ - QFile *f; - int i; - - i = findhandle (); - - f = Qopen(path, "wb"); - if (!f) - { - Con_Printf("Sys_FileOpenWrite: Error opening %s: %s", path, strerror(errno)); - return 0; - } - sys_handles[i] = f; - - return i; -} - -void Sys_FileClose (int handle) -{ - Qclose (sys_handles[handle]); - sys_handles[handle] = NULL; -} - -void Sys_FileSeek (int handle, int position) -{ - Qseek (sys_handles[handle], position, SEEK_SET); -} - -int Sys_FileRead (int handle, void *dest, int count) -{ - return Qread (sys_handles[handle], dest, count); -} - -int Sys_FileWrite (int handle, void *data, int count) -{ - return Qwrite (sys_handles[handle], data, count); -} - -int Sys_FileTime (const char *path) -{ -#if WIN32 - QFile *f; - - f = Qopen(path, "rb"); - if (f) - { - Qclose(f); - return 1; - } - - return -1; -#else - struct stat buf; - - if (stat (path,&buf) == -1) - return -1; - - return buf.st_mtime; -#endif -} - -void Sys_mkdir (const char *path) -{ -#if WIN32 - _mkdir (path); -#else - mkdir (path, 0777); -#endif -} char engineversion[128]; void Sys_Shared_EarlyInit(void) { + const char* os; + Memory_Init (); COM_InitArgv(); COM_InitGameType(); #if defined(__linux__) - sprintf (engineversion, "%s Linux %s", gamename, buildstring); + os = "Linux"; #elif defined(WIN32) - sprintf (engineversion, "%s Windows %s", gamename, buildstring); + os = "Windows"; #else - sprintf (engineversion, "%s Unknown %s", gamename, buildstring); + os = "Unknown"; #endif + snprintf (engineversion, sizeof (engineversion), "%s %s %s", gamename, os, buildstring); if (COM_CheckParm("-nostdout")) sys_nostdout = 1; diff --git a/wad.c b/wad.c index 755ae5e6..e5b53c35 100644 --- a/wad.c +++ b/wad.c @@ -74,16 +74,16 @@ void W_LoadWadFile (char *filename) int infotableofs; void *temp; - temp = COM_LoadFile (filename, false); + temp = FS_LoadFile (filename, false); if (!temp) Sys_Error ("W_LoadWadFile: couldn't load %s", filename); if (wad_mempool) Mem_FreePool(&wad_mempool); wad_mempool = Mem_AllocPool(filename); - wad_base = Mem_Alloc(wad_mempool, loadsize); + wad_base = Mem_Alloc(wad_mempool, fs_filesize); - memcpy(wad_base, temp, loadsize); + memcpy(wad_base, temp, fs_filesize); Mem_Free(temp); header = (wadinfo_t *)wad_base; @@ -139,7 +139,7 @@ void SwapPic (qpic_t *pic) typedef struct { char name[16]; - QFile *file; + qfile_t *file; int position; int size; } texwadlump_t; @@ -157,10 +157,10 @@ void W_LoadTextureWadFile (char *filename, int complain) wadinfo_t header; int i, j; int infotableofs; - QFile *file; + qfile_t *file; int numlumps; - COM_FOpenFile (filename, &file, false, false); + file = FS_Open (filename, "rb", false); if (!file) { if (complain) @@ -168,7 +168,7 @@ void W_LoadTextureWadFile (char *filename, int complain) return; } - if (Qread(file, &header, sizeof(wadinfo_t)) != sizeof(wadinfo_t)) + if (FS_Read(file, &header, sizeof(wadinfo_t)) != sizeof(wadinfo_t)) {Con_Printf ("W_LoadTextureWadFile: unable to read wad header");return;} if(memcmp(header.identification, "WAD3", 4)) @@ -178,12 +178,12 @@ void W_LoadTextureWadFile (char *filename, int complain) if (numlumps < 1 || numlumps > TEXWAD_MAXIMAGES) {Con_Printf ("W_LoadTextureWadFile: invalid number of lumps (%i)\n", numlumps);return;} infotableofs = LittleLong(header.infotableofs); - if (Qseek(file, infotableofs, SEEK_SET)) + if (FS_Seek (file, infotableofs, SEEK_SET)) {Con_Printf ("W_LoadTextureWadFile: unable to seek to lump table");return;} if (!(lumps = Mem_Alloc(tempmempool, sizeof(lumpinfo_t)*numlumps))) {Con_Printf ("W_LoadTextureWadFile: unable to allocate temporary memory for lump table");return;} - if (Qread(file, lumps, sizeof(lumpinfo_t) * numlumps) != (int)sizeof(lumpinfo_t) * numlumps) + if (FS_Read(file, lumps, sizeof(lumpinfo_t) * numlumps) != (int)sizeof(lumpinfo_t) * numlumps) {Con_Printf ("W_LoadTextureWadFile: unable to read lump table");return;} for (i=0, lump_p = lumps ; iwidth = LittleLong(tex->width); diff --git a/wavefile.c b/wavefile.c index 67a4575c..37db56f2 100644 --- a/wavefile.c +++ b/wavefile.c @@ -1,4 +1,5 @@ +#include "quakedef.h" #include #include #include @@ -9,10 +10,10 @@ wavefile_t *waveopen(char *filename, char **errorstring) int validfmt, position, length, l; char *error; wavefile_t *w; - FILE *file; + qfile_t *file; unsigned char buffer[1024]; error = NULL; - file = fopen(filename, "rb"); + file = FS_Open (filename, "rb", true); if (file) { w = malloc(sizeof(*w)); @@ -20,7 +21,7 @@ wavefile_t *waveopen(char *filename, char **errorstring) if (w) { w->file = file; - if (fread(buffer, 12, 1, w->file)) + if (FS_Read (w->file, buffer, 12)) { if (!memcmp(buffer, "RIFF", 4)) { @@ -29,12 +30,12 @@ wavefile_t *waveopen(char *filename, char **errorstring) validfmt = 0; for(;;) { - if (!fread(buffer, 8, 1, w->file)) + if (!FS_Read(w->file, buffer, 8)) { //error = "error reading chunk\n"); break; } - position = ftell(w->file); + position = FS_Tell(w->file); length = buffer[4] | (buffer[5] << 8) | (buffer[6] << 16) | (buffer[7] << 24); if (!memcmp(buffer, "fmt ", 4)) { @@ -42,7 +43,7 @@ wavefile_t *waveopen(char *filename, char **errorstring) l = length; if (l > 16) l = 16; - if (!fread(buffer, l, 1, w->file)) + if (!FS_Read(w->file, buffer, l)) { error = "error reading \"fmt \" chunk\n"; break; @@ -83,7 +84,7 @@ wavefile_t *waveopen(char *filename, char **errorstring) } // other chunks that might be of interest: // "cue " (for looping) - if (fseek(w->file, position + length, SEEK_SET)) + if (FS_Seek(w->file, position + length, SEEK_SET)) { error = "error seeking to next chunk\n"; break; @@ -95,7 +96,7 @@ wavefile_t *waveopen(char *filename, char **errorstring) w->info_bytespersample = w->info_channels * w->info_bytesperchannel; w->length = w->datalength / w->info_bytespersample; w->position = 0; - fseek(w->file, w->dataposition, SEEK_SET); + FS_Seek(w->file, w->dataposition, SEEK_SET); return w; } } @@ -111,7 +112,7 @@ wavefile_t *waveopen(char *filename, char **errorstring) } else error = "unable to allocate memory\n"; - fclose(file); + FS_Close(file); } else error = "unable to open file\n"; @@ -124,7 +125,7 @@ void waveclose(wavefile_t *f) { if (f) { - fclose(f->file); + FS_Close(f->file); free(f); } } @@ -145,7 +146,7 @@ unsigned int waveread16stereo(wavefile_t *w, short *soundbuffer, unsigned int sa w->bufferlength = length + 100; w->buffer = malloc(w->bufferlength * w->info_bytespersample); } - length = fread(w->buffer, w->info_bytespersample, length, w->file); + length = FS_Read(w->file, w->buffer, w->info_bytespersample * length); w->position += length; if (length > 0) { @@ -188,7 +189,7 @@ unsigned int waveseek(wavefile_t *w, unsigned int samples) else { w->position = samples; - fseek(w->file, w->dataposition + w->position * w->info_bytespersample, SEEK_SET); + FS_Seek(w->file, w->dataposition + w->position * w->info_bytespersample, SEEK_SET); return 0; } } diff --git a/wavefile.h b/wavefile.h index de8b33aa..f0037ca6 100644 --- a/wavefile.h +++ b/wavefile.h @@ -2,10 +2,12 @@ #ifndef WAVEFILE_H #define WAVEFILE_H +#include "quakedef.h" + typedef struct wavefile_s { // file this is reading from - FILE *file; + qfile_t *file; // these settings are read directly from the wave format // 1 is uncompressed PCM -- 2.39.2