diff --git a/src/common/common.c b/src/common/common.c index 1c480dea..770f21a5 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -8,7 +8,7 @@ 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. +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. @@ -27,8 +27,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. int com_argc; char *com_argv[MAX_NUM_ARGVS+1]; int realtime; -jmp_buf abortframe; /* an ERR_DROP occured, exit the entire frame */ - +jmp_buf abortframe; /* an ERR_DROP occured, exit the entire frame */ FILE *log_stats_file; @@ -71,6 +70,7 @@ void Com_BeginRedirect (int target, char *buffer, int buffersize, void (*flush)) { if (!target || !buffer || !buffersize || !flush) return; + rd_target = target; rd_buffer = buffer; rd_buffersize = buffersize; @@ -109,11 +109,12 @@ void Com_Printf (char *fmt, ...) rd_flush(rd_target, rd_buffer); *rd_buffer = 0; } + strcat (rd_buffer, msg); return; } -#ifndef DEDICATED_ONLY +#ifndef DEDICATED_ONLY Con_Print (msg); #endif @@ -124,17 +125,21 @@ void Com_Printf (char *fmt, ...) if (logfile_active && logfile_active->value) { char name[MAX_QPATH]; - + if (!logfile) { Com_sprintf (name, sizeof(name), "%s/qconsole.log", FS_Gamedir ()); + if (logfile_active->value > 2) logfile = fopen (name, "a"); + else logfile = fopen (name, "w"); } + if (logfile) fprintf (logfile, "%s", msg); + if (logfile_active->value > 1) fflush (logfile); /* force it to save every time */ } @@ -147,14 +152,14 @@ void Com_DPrintf (char *fmt, ...) { va_list argptr; char msg[MAXPRINTMSG]; - + if (!developer || !developer->value) return; /* don't confuse non-developers with techie stuff... */ va_start (argptr,fmt); vsnprintf (msg,MAXPRINTMSG,fmt,argptr); va_end (argptr); - + Com_Printf ("%s", msg); } @@ -166,14 +171,14 @@ void Com_MDPrintf (char *fmt, ...) { va_list argptr; char msg[MAXPRINTMSG]; - + if((!modder || !modder->value) && (!developer || !developer->value)) return; - + va_start (argptr,fmt); vsnprintf (msg,MAXPRINTMSG,fmt,argptr); va_end (argptr); - + Com_Printf("%s", msg); } @@ -189,12 +194,13 @@ void Com_Error (int code, char *fmt, ...) if (recursive) Sys_Error ("recursive error after: %s", msg); + recursive = true; va_start (argptr,fmt); vsnprintf (msg,MAXPRINTMSG,fmt,argptr); va_end (argptr); - + if (code == ERR_DISCONNECT) { #ifndef DEDICATED_ONLY @@ -203,22 +209,24 @@ void Com_Error (int code, char *fmt, ...) recursive = false; longjmp (abortframe, -1); } + else if (code == ERR_DROP) { Com_Printf ("********************\nERROR: %s\n********************\n", msg); SV_Shutdown (va("Server crashed: %s\n", msg), false); -#ifndef DEDICATED_ONLY +#ifndef DEDICATED_ONLY CL_Drop (); #endif recursive = false; longjmp (abortframe, -1); } + else { SV_Shutdown (va("Server fatal crashed: %s\n", msg), false); -#ifndef DEDICATED_ONLY +#ifndef DEDICATED_ONLY CL_Shutdown (); -#endif +#endif } if (logfile) @@ -238,9 +246,10 @@ void Com_Error (int code, char *fmt, ...) void Com_Quit (void) { SV_Shutdown ("Server quit\n", false); -#ifndef DEDICATED_ONLY +#ifndef DEDICATED_ONLY CL_Shutdown (); #endif + if (logfile) { fclose (logfile); @@ -272,174 +281,174 @@ Handles byte ordering and avoids alignment errors vec3_t bytedirs[NUMVERTEXNORMALS] = { - {-0.525731, 0.000000, 0.850651}, - {-0.442863, 0.238856, 0.864188}, - {-0.295242, 0.000000, 0.955423}, - {-0.309017, 0.500000, 0.809017}, - {-0.162460, 0.262866, 0.951056}, - {0.000000, 0.000000, 1.000000}, - {0.000000, 0.850651, 0.525731}, - {-0.147621, 0.716567, 0.681718}, - {0.147621, 0.716567, 0.681718}, - {0.000000, 0.525731, 0.850651}, - {0.309017, 0.500000, 0.809017}, - {0.525731, 0.000000, 0.850651}, - {0.295242, 0.000000, 0.955423}, - {0.442863, 0.238856, 0.864188}, - {0.162460, 0.262866, 0.951056}, - {-0.681718, 0.147621, 0.716567}, - {-0.809017, 0.309017, 0.500000}, - {-0.587785, 0.425325, 0.688191}, - {-0.850651, 0.525731, 0.000000}, - {-0.864188, 0.442863, 0.238856}, - {-0.716567, 0.681718, 0.147621}, - {-0.688191, 0.587785, 0.425325}, - {-0.500000, 0.809017, 0.309017}, - {-0.238856, 0.864188, 0.442863}, - {-0.425325, 0.688191, 0.587785}, - {-0.716567, 0.681718, -0.147621}, - {-0.500000, 0.809017, -0.309017}, - {-0.525731, 0.850651, 0.000000}, - {0.000000, 0.850651, -0.525731}, - {-0.238856, 0.864188, -0.442863}, - {0.000000, 0.955423, -0.295242}, - {-0.262866, 0.951056, -0.162460}, - {0.000000, 1.000000, 0.000000}, - {0.000000, 0.955423, 0.295242}, - {-0.262866, 0.951056, 0.162460}, - {0.238856, 0.864188, 0.442863}, - {0.262866, 0.951056, 0.162460}, - {0.500000, 0.809017, 0.309017}, - {0.238856, 0.864188, -0.442863}, - {0.262866, 0.951056, -0.162460}, - {0.500000, 0.809017, -0.309017}, - {0.850651, 0.525731, 0.000000}, - {0.716567, 0.681718, 0.147621}, - {0.716567, 0.681718, -0.147621}, - {0.525731, 0.850651, 0.000000}, - {0.425325, 0.688191, 0.587785}, - {0.864188, 0.442863, 0.238856}, - {0.688191, 0.587785, 0.425325}, - {0.809017, 0.309017, 0.500000}, - {0.681718, 0.147621, 0.716567}, - {0.587785, 0.425325, 0.688191}, - {0.955423, 0.295242, 0.000000}, - {1.000000, 0.000000, 0.000000}, - {0.951056, 0.162460, 0.262866}, - {0.850651, -0.525731, 0.000000}, - {0.955423, -0.295242, 0.000000}, - {0.864188, -0.442863, 0.238856}, - {0.951056, -0.162460, 0.262866}, - {0.809017, -0.309017, 0.500000}, - {0.681718, -0.147621, 0.716567}, - {0.850651, 0.000000, 0.525731}, - {0.864188, 0.442863, -0.238856}, - {0.809017, 0.309017, -0.500000}, - {0.951056, 0.162460, -0.262866}, - {0.525731, 0.000000, -0.850651}, - {0.681718, 0.147621, -0.716567}, - {0.681718, -0.147621, -0.716567}, - {0.850651, 0.000000, -0.525731}, - {0.809017, -0.309017, -0.500000}, - {0.864188, -0.442863, -0.238856}, - {0.951056, -0.162460, -0.262866}, - {0.147621, 0.716567, -0.681718}, - {0.309017, 0.500000, -0.809017}, - {0.425325, 0.688191, -0.587785}, - {0.442863, 0.238856, -0.864188}, - {0.587785, 0.425325, -0.688191}, - {0.688191, 0.587785, -0.425325}, - {-0.147621, 0.716567, -0.681718}, - {-0.309017, 0.500000, -0.809017}, - {0.000000, 0.525731, -0.850651}, - {-0.525731, 0.000000, -0.850651}, - {-0.442863, 0.238856, -0.864188}, - {-0.295242, 0.000000, -0.955423}, - {-0.162460, 0.262866, -0.951056}, - {0.000000, 0.000000, -1.000000}, - {0.295242, 0.000000, -0.955423}, - {0.162460, 0.262866, -0.951056}, - {-0.442863, -0.238856, -0.864188}, - {-0.309017, -0.500000, -0.809017}, - {-0.162460, -0.262866, -0.951056}, - {0.000000, -0.850651, -0.525731}, - {-0.147621, -0.716567, -0.681718}, - {0.147621, -0.716567, -0.681718}, - {0.000000, -0.525731, -0.850651}, - {0.309017, -0.500000, -0.809017}, - {0.442863, -0.238856, -0.864188}, - {0.162460, -0.262866, -0.951056}, - {0.238856, -0.864188, -0.442863}, - {0.500000, -0.809017, -0.309017}, - {0.425325, -0.688191, -0.587785}, - {0.716567, -0.681718, -0.147621}, - {0.688191, -0.587785, -0.425325}, - {0.587785, -0.425325, -0.688191}, - {0.000000, -0.955423, -0.295242}, - {0.000000, -1.000000, 0.000000}, - {0.262866, -0.951056, -0.162460}, - {0.000000, -0.850651, 0.525731}, - {0.000000, -0.955423, 0.295242}, - {0.238856, -0.864188, 0.442863}, - {0.262866, -0.951056, 0.162460}, - {0.500000, -0.809017, 0.309017}, - {0.716567, -0.681718, 0.147621}, - {0.525731, -0.850651, 0.000000}, - {-0.238856, -0.864188, -0.442863}, - {-0.500000, -0.809017, -0.309017}, - {-0.262866, -0.951056, -0.162460}, - {-0.850651, -0.525731, 0.000000}, - {-0.716567, -0.681718, -0.147621}, - {-0.716567, -0.681718, 0.147621}, - {-0.525731, -0.850651, 0.000000}, - {-0.500000, -0.809017, 0.309017}, - {-0.238856, -0.864188, 0.442863}, - {-0.262866, -0.951056, 0.162460}, - {-0.864188, -0.442863, 0.238856}, - {-0.809017, -0.309017, 0.500000}, - {-0.688191, -0.587785, 0.425325}, - {-0.681718, -0.147621, 0.716567}, - {-0.442863, -0.238856, 0.864188}, - {-0.587785, -0.425325, 0.688191}, - {-0.309017, -0.500000, 0.809017}, - {-0.147621, -0.716567, 0.681718}, - {-0.425325, -0.688191, 0.587785}, - {-0.162460, -0.262866, 0.951056}, - {0.442863, -0.238856, 0.864188}, - {0.162460, -0.262866, 0.951056}, - {0.309017, -0.500000, 0.809017}, - {0.147621, -0.716567, 0.681718}, - {0.000000, -0.525731, 0.850651}, - {0.425325, -0.688191, 0.587785}, - {0.587785, -0.425325, 0.688191}, - {0.688191, -0.587785, 0.425325}, - {-0.955423, 0.295242, 0.000000}, - {-0.951056, 0.162460, 0.262866}, - {-1.000000, 0.000000, 0.000000}, - {-0.850651, 0.000000, 0.525731}, - {-0.955423, -0.295242, 0.000000}, - {-0.951056, -0.162460, 0.262866}, - {-0.864188, 0.442863, -0.238856}, - {-0.951056, 0.162460, -0.262866}, - {-0.809017, 0.309017, -0.500000}, - {-0.864188, -0.442863, -0.238856}, - {-0.951056, -0.162460, -0.262866}, - {-0.809017, -0.309017, -0.500000}, - {-0.681718, 0.147621, -0.716567}, - {-0.681718, -0.147621, -0.716567}, - {-0.850651, 0.000000, -0.525731}, - {-0.688191, 0.587785, -0.425325}, - {-0.587785, 0.425325, -0.688191}, - {-0.425325, 0.688191, -0.587785}, - {-0.425325, -0.688191, -0.587785}, - {-0.587785, -0.425325, -0.688191}, + {-0.525731, 0.000000, 0.850651}, + {-0.442863, 0.238856, 0.864188}, + {-0.295242, 0.000000, 0.955423}, + {-0.309017, 0.500000, 0.809017}, + {-0.162460, 0.262866, 0.951056}, + {0.000000, 0.000000, 1.000000}, + {0.000000, 0.850651, 0.525731}, + {-0.147621, 0.716567, 0.681718}, + {0.147621, 0.716567, 0.681718}, + {0.000000, 0.525731, 0.850651}, + {0.309017, 0.500000, 0.809017}, + {0.525731, 0.000000, 0.850651}, + {0.295242, 0.000000, 0.955423}, + {0.442863, 0.238856, 0.864188}, + {0.162460, 0.262866, 0.951056}, + {-0.681718, 0.147621, 0.716567}, + {-0.809017, 0.309017, 0.500000}, + {-0.587785, 0.425325, 0.688191}, + {-0.850651, 0.525731, 0.000000}, + {-0.864188, 0.442863, 0.238856}, + {-0.716567, 0.681718, 0.147621}, + {-0.688191, 0.587785, 0.425325}, + {-0.500000, 0.809017, 0.309017}, + {-0.238856, 0.864188, 0.442863}, + {-0.425325, 0.688191, 0.587785}, + {-0.716567, 0.681718, -0.147621}, + {-0.500000, 0.809017, -0.309017}, + {-0.525731, 0.850651, 0.000000}, + {0.000000, 0.850651, -0.525731}, + {-0.238856, 0.864188, -0.442863}, + {0.000000, 0.955423, -0.295242}, + {-0.262866, 0.951056, -0.162460}, + {0.000000, 1.000000, 0.000000}, + {0.000000, 0.955423, 0.295242}, + {-0.262866, 0.951056, 0.162460}, + {0.238856, 0.864188, 0.442863}, + {0.262866, 0.951056, 0.162460}, + {0.500000, 0.809017, 0.309017}, + {0.238856, 0.864188, -0.442863}, + {0.262866, 0.951056, -0.162460}, + {0.500000, 0.809017, -0.309017}, + {0.850651, 0.525731, 0.000000}, + {0.716567, 0.681718, 0.147621}, + {0.716567, 0.681718, -0.147621}, + {0.525731, 0.850651, 0.000000}, + {0.425325, 0.688191, 0.587785}, + {0.864188, 0.442863, 0.238856}, + {0.688191, 0.587785, 0.425325}, + {0.809017, 0.309017, 0.500000}, + {0.681718, 0.147621, 0.716567}, + {0.587785, 0.425325, 0.688191}, + {0.955423, 0.295242, 0.000000}, + {1.000000, 0.000000, 0.000000}, + {0.951056, 0.162460, 0.262866}, + {0.850651, -0.525731, 0.000000}, + {0.955423, -0.295242, 0.000000}, + {0.864188, -0.442863, 0.238856}, + {0.951056, -0.162460, 0.262866}, + {0.809017, -0.309017, 0.500000}, + {0.681718, -0.147621, 0.716567}, + {0.850651, 0.000000, 0.525731}, + {0.864188, 0.442863, -0.238856}, + {0.809017, 0.309017, -0.500000}, + {0.951056, 0.162460, -0.262866}, + {0.525731, 0.000000, -0.850651}, + {0.681718, 0.147621, -0.716567}, + {0.681718, -0.147621, -0.716567}, + {0.850651, 0.000000, -0.525731}, + {0.809017, -0.309017, -0.500000}, + {0.864188, -0.442863, -0.238856}, + {0.951056, -0.162460, -0.262866}, + {0.147621, 0.716567, -0.681718}, + {0.309017, 0.500000, -0.809017}, + {0.425325, 0.688191, -0.587785}, + {0.442863, 0.238856, -0.864188}, + {0.587785, 0.425325, -0.688191}, + {0.688191, 0.587785, -0.425325}, + {-0.147621, 0.716567, -0.681718}, + {-0.309017, 0.500000, -0.809017}, + {0.000000, 0.525731, -0.850651}, + {-0.525731, 0.000000, -0.850651}, + {-0.442863, 0.238856, -0.864188}, + {-0.295242, 0.000000, -0.955423}, + {-0.162460, 0.262866, -0.951056}, + {0.000000, 0.000000, -1.000000}, + {0.295242, 0.000000, -0.955423}, + {0.162460, 0.262866, -0.951056}, + {-0.442863, -0.238856, -0.864188}, + {-0.309017, -0.500000, -0.809017}, + {-0.162460, -0.262866, -0.951056}, + {0.000000, -0.850651, -0.525731}, + {-0.147621, -0.716567, -0.681718}, + {0.147621, -0.716567, -0.681718}, + {0.000000, -0.525731, -0.850651}, + {0.309017, -0.500000, -0.809017}, + {0.442863, -0.238856, -0.864188}, + {0.162460, -0.262866, -0.951056}, + {0.238856, -0.864188, -0.442863}, + {0.500000, -0.809017, -0.309017}, + {0.425325, -0.688191, -0.587785}, + {0.716567, -0.681718, -0.147621}, + {0.688191, -0.587785, -0.425325}, + {0.587785, -0.425325, -0.688191}, + {0.000000, -0.955423, -0.295242}, + {0.000000, -1.000000, 0.000000}, + {0.262866, -0.951056, -0.162460}, + {0.000000, -0.850651, 0.525731}, + {0.000000, -0.955423, 0.295242}, + {0.238856, -0.864188, 0.442863}, + {0.262866, -0.951056, 0.162460}, + {0.500000, -0.809017, 0.309017}, + {0.716567, -0.681718, 0.147621}, + {0.525731, -0.850651, 0.000000}, + {-0.238856, -0.864188, -0.442863}, + {-0.500000, -0.809017, -0.309017}, + {-0.262866, -0.951056, -0.162460}, + {-0.850651, -0.525731, 0.000000}, + {-0.716567, -0.681718, -0.147621}, + {-0.716567, -0.681718, 0.147621}, + {-0.525731, -0.850651, 0.000000}, + {-0.500000, -0.809017, 0.309017}, + {-0.238856, -0.864188, 0.442863}, + {-0.262866, -0.951056, 0.162460}, + {-0.864188, -0.442863, 0.238856}, + {-0.809017, -0.309017, 0.500000}, + {-0.688191, -0.587785, 0.425325}, + {-0.681718, -0.147621, 0.716567}, + {-0.442863, -0.238856, 0.864188}, + {-0.587785, -0.425325, 0.688191}, + {-0.309017, -0.500000, 0.809017}, + {-0.147621, -0.716567, 0.681718}, + {-0.425325, -0.688191, 0.587785}, + {-0.162460, -0.262866, 0.951056}, + {0.442863, -0.238856, 0.864188}, + {0.162460, -0.262866, 0.951056}, + {0.309017, -0.500000, 0.809017}, + {0.147621, -0.716567, 0.681718}, + {0.000000, -0.525731, 0.850651}, + {0.425325, -0.688191, 0.587785}, + {0.587785, -0.425325, 0.688191}, + {0.688191, -0.587785, 0.425325}, + {-0.955423, 0.295242, 0.000000}, + {-0.951056, 0.162460, 0.262866}, + {-1.000000, 0.000000, 0.000000}, + {-0.850651, 0.000000, 0.525731}, + {-0.955423, -0.295242, 0.000000}, + {-0.951056, -0.162460, 0.262866}, + {-0.864188, 0.442863, -0.238856}, + {-0.951056, 0.162460, -0.262866}, + {-0.809017, 0.309017, -0.500000}, + {-0.864188, -0.442863, -0.238856}, + {-0.951056, -0.162460, -0.262866}, + {-0.809017, -0.309017, -0.500000}, + {-0.681718, 0.147621, -0.716567}, + {-0.681718, -0.147621, -0.716567}, + {-0.850651, 0.000000, -0.525731}, + {-0.688191, 0.587785, -0.425325}, + {-0.587785, 0.425325, -0.688191}, + {-0.425325, 0.688191, -0.587785}, + {-0.425325, -0.688191, -0.587785}, + {-0.587785, -0.425325, -0.688191}, {-0.688191, -0.587785, -0.425325} }; void MSG_WriteChar (sizebuf_t *sb, int c) { byte *buf; - + buf = SZ_GetSpace (sb, 1); buf[0] = c; } @@ -447,7 +456,7 @@ void MSG_WriteChar (sizebuf_t *sb, int c) void MSG_WriteByte (sizebuf_t *sb, int c) { byte *buf; - + buf = SZ_GetSpace (sb, 1); buf[0] = c; } @@ -464,7 +473,7 @@ void MSG_WriteShort (sizebuf_t *sb, int c) void MSG_WriteLong (sizebuf_t *sb, int c) { byte *buf; - + buf = SZ_GetSpace (sb, 4); buf[0] = c&0xff; buf[1] = (c>>8)&0xff; @@ -479,11 +488,11 @@ void MSG_WriteFloat (sizebuf_t *sb, float f) float f; int l; } dat; - - + + dat.f = f; dat.l = LittleLong (dat.l); - + SZ_Write (sb, &dat.l, 4); } @@ -491,6 +500,7 @@ void MSG_WriteString (sizebuf_t *sb, char *s) { if (!s) SZ_Write (sb, "", 1); + else SZ_Write (sb, s, (int)strlen(s)+1); } @@ -517,52 +527,64 @@ void MSG_WriteAngle16 (sizebuf_t *sb, float f) MSG_WriteShort (sb, ANGLE2SHORT(f)); } - void MSG_WriteDeltaUsercmd (sizebuf_t *buf, usercmd_t *from, usercmd_t *cmd) { int bits; /* Movement messages */ bits = 0; + if (cmd->angles[0] != from->angles[0]) bits |= CM_ANGLE1; + if (cmd->angles[1] != from->angles[1]) bits |= CM_ANGLE2; + if (cmd->angles[2] != from->angles[2]) bits |= CM_ANGLE3; + if (cmd->forwardmove != from->forwardmove) bits |= CM_FORWARD; + if (cmd->sidemove != from->sidemove) bits |= CM_SIDE; + if (cmd->upmove != from->upmove) bits |= CM_UP; + if (cmd->buttons != from->buttons) bits |= CM_BUTTONS; + if (cmd->impulse != from->impulse) bits |= CM_IMPULSE; - MSG_WriteByte (buf, bits); + MSG_WriteByte (buf, bits); if (bits & CM_ANGLE1) MSG_WriteShort (buf, cmd->angles[0]); + if (bits & CM_ANGLE2) MSG_WriteShort (buf, cmd->angles[1]); + if (bits & CM_ANGLE3) MSG_WriteShort (buf, cmd->angles[2]); - + if (bits & CM_FORWARD) MSG_WriteShort (buf, cmd->forwardmove); + if (bits & CM_SIDE) - MSG_WriteShort (buf, cmd->sidemove); + MSG_WriteShort (buf, cmd->sidemove); + if (bits & CM_UP) MSG_WriteShort (buf, cmd->upmove); - if (bits & CM_BUTTONS) - MSG_WriteByte (buf, cmd->buttons); - if (bits & CM_IMPULSE) - MSG_WriteByte (buf, cmd->impulse); + if (bits & CM_BUTTONS) + MSG_WriteByte (buf, cmd->buttons); - MSG_WriteByte (buf, cmd->msec); + if (bits & CM_IMPULSE) + MSG_WriteByte (buf, cmd->impulse); + + MSG_WriteByte (buf, cmd->msec); MSG_WriteByte (buf, cmd->lightlevel); } @@ -570,7 +592,7 @@ void MSG_WriteDir (sizebuf_t *sb, vec3_t dir) { int i, best; float d, bestd; - + if (!dir) { MSG_WriteByte (sb, 0); @@ -579,15 +601,18 @@ void MSG_WriteDir (sizebuf_t *sb, vec3_t dir) bestd = 0; best = 0; + for (i=0 ; i bestd) { bestd = d; best = i; } } + MSG_WriteByte (sb, best); } @@ -596,22 +621,24 @@ void MSG_ReadDir (sizebuf_t *sb, vec3_t dir) int b; b = MSG_ReadByte (sb); + if (b >= NUMVERTEXNORMALS) Com_Error (ERR_DROP, "MSF_ReadDir: out of range"); + VectorCopy (bytedirs[b], dir); } - /* * Writes part of a packetentities message. * Can delta from either a baseline or a previous packet_entity -*/ + */ void MSG_WriteDeltaEntity (entity_state_t *from, entity_state_t *to, sizebuf_t *msg, qboolean force, qboolean newentity) { int bits; if (!to->number) Com_Error (ERR_FATAL, "Unset entity number"); + if (to->number >= MAX_EDICTS) Com_Error (ERR_FATAL, "Entity number >= MAX_EDICTS"); @@ -623,32 +650,39 @@ void MSG_WriteDeltaEntity (entity_state_t *from, entity_state_t *to, sizebuf_t * if (to->origin[0] != from->origin[0]) bits |= U_ORIGIN1; + if (to->origin[1] != from->origin[1]) bits |= U_ORIGIN2; + if (to->origin[2] != from->origin[2]) bits |= U_ORIGIN3; if ( to->angles[0] != from->angles[0] ) - bits |= U_ANGLE1; + bits |= U_ANGLE1; + if ( to->angles[1] != from->angles[1] ) bits |= U_ANGLE2; + if ( to->angles[2] != from->angles[2] ) bits |= U_ANGLE3; - + if ( to->skinnum != from->skinnum ) { if ((unsigned)to->skinnum < 256) bits |= U_SKIN8; + else if ((unsigned)to->skinnum < 0x10000) bits |= U_SKIN16; + else bits |= (U_SKIN8|U_SKIN16); } - + if ( to->frame != from->frame ) { if (to->frame < 256) bits |= U_FRAME8; + else bits |= U_FRAME16; } @@ -657,35 +691,42 @@ void MSG_WriteDeltaEntity (entity_state_t *from, entity_state_t *to, sizebuf_t * { if (to->effects < 256) bits |= U_EFFECTS8; + else if (to->effects < 0x8000) bits |= U_EFFECTS16; + else bits |= U_EFFECTS8|U_EFFECTS16; } - + if ( to->renderfx != from->renderfx ) { if (to->renderfx < 256) bits |= U_RENDERFX8; + else if (to->renderfx < 0x8000) bits |= U_RENDERFX16; + else bits |= U_RENDERFX8|U_RENDERFX16; } - + if ( to->solid != from->solid ) bits |= U_SOLID; /* event is not delta compressed, just 0 compressed */ if ( to->event ) bits |= U_EVENT; - + if ( to->modelindex != from->modelindex ) bits |= U_MODEL; + if ( to->modelindex2 != from->modelindex2 ) bits |= U_MODEL2; + if ( to->modelindex3 != from->modelindex3 ) bits |= U_MODEL3; + if ( to->modelindex4 != from->modelindex4 ) bits |= U_MODEL4; @@ -701,8 +742,10 @@ void MSG_WriteDeltaEntity (entity_state_t *from, entity_state_t *to, sizebuf_t * if (bits & 0xff000000) bits |= U_MOREBITS3 | U_MOREBITS2 | U_MOREBITS1; + else if (bits & 0x00ff0000) bits |= U_MOREBITS2 | U_MOREBITS1; + else if (bits & 0x0000ff00) bits |= U_MOREBITS1; @@ -714,11 +757,13 @@ void MSG_WriteDeltaEntity (entity_state_t *from, entity_state_t *to, sizebuf_t * MSG_WriteByte (msg, (bits>>16)&255 ); MSG_WriteByte (msg, (bits>>24)&255 ); } + else if (bits & 0x00ff0000) { MSG_WriteByte (msg, (bits>>8)&255 ); MSG_WriteByte (msg, (bits>>16)&255 ); } + else if (bits & 0x0000ff00) { MSG_WriteByte (msg, (bits>>8)&255 ); @@ -726,55 +771,70 @@ void MSG_WriteDeltaEntity (entity_state_t *from, entity_state_t *to, sizebuf_t * if (bits & U_NUMBER16) MSG_WriteShort (msg, to->number); + else MSG_WriteByte (msg, to->number); if (bits & U_MODEL) MSG_WriteByte (msg, to->modelindex); + if (bits & U_MODEL2) MSG_WriteByte (msg, to->modelindex2); + if (bits & U_MODEL3) MSG_WriteByte (msg, to->modelindex3); + if (bits & U_MODEL4) MSG_WriteByte (msg, to->modelindex4); if (bits & U_FRAME8) MSG_WriteByte (msg, to->frame); + if (bits & U_FRAME16) MSG_WriteShort (msg, to->frame); if ((bits & U_SKIN8) && (bits & U_SKIN16)) /*used for laser colors */ MSG_WriteLong (msg, to->skinnum); + else if (bits & U_SKIN8) MSG_WriteByte (msg, to->skinnum); + else if (bits & U_SKIN16) MSG_WriteShort (msg, to->skinnum); if ( (bits & (U_EFFECTS8|U_EFFECTS16)) == (U_EFFECTS8|U_EFFECTS16) ) MSG_WriteLong (msg, to->effects); + else if (bits & U_EFFECTS8) MSG_WriteByte (msg, to->effects); + else if (bits & U_EFFECTS16) MSG_WriteShort (msg, to->effects); if ( (bits & (U_RENDERFX8|U_RENDERFX16)) == (U_RENDERFX8|U_RENDERFX16) ) MSG_WriteLong (msg, to->renderfx); + else if (bits & U_RENDERFX8) MSG_WriteByte (msg, to->renderfx); + else if (bits & U_RENDERFX16) MSG_WriteShort (msg, to->renderfx); if (bits & U_ORIGIN1) - MSG_WriteCoord (msg, to->origin[0]); + MSG_WriteCoord (msg, to->origin[0]); + if (bits & U_ORIGIN2) MSG_WriteCoord (msg, to->origin[1]); + if (bits & U_ORIGIN3) MSG_WriteCoord (msg, to->origin[2]); if (bits & U_ANGLE1) MSG_WriteAngle(msg, to->angles[0]); + if (bits & U_ANGLE2) MSG_WriteAngle(msg, to->angles[1]); + if (bits & U_ANGLE3) MSG_WriteAngle(msg, to->angles[2]); @@ -787,8 +847,10 @@ void MSG_WriteDeltaEntity (entity_state_t *from, entity_state_t *to, sizebuf_t * if (bits & U_SOUND) MSG_WriteByte (msg, to->sound); + if (bits & U_EVENT) MSG_WriteByte (msg, to->event); + if (bits & U_SOLID) MSG_WriteShort (msg, to->solid); } @@ -807,58 +869,64 @@ void MSG_BeginReading (sizebuf_t *msg) int MSG_ReadChar (sizebuf_t *msg_read) { int c; - + if (msg_read->readcount+1 > msg_read->cursize) c = -1; + else c = (signed char)msg_read->data[msg_read->readcount]; + msg_read->readcount++; - + return c; } int MSG_ReadByte (sizebuf_t *msg_read) { int c; - + if (msg_read->readcount+1 > msg_read->cursize) c = -1; + else c = (unsigned char)msg_read->data[msg_read->readcount]; + msg_read->readcount++; - + return c; } int MSG_ReadShort (sizebuf_t *msg_read) { int c; - + if (msg_read->readcount+2 > msg_read->cursize) c = -1; - else + + else c = (short)(msg_read->data[msg_read->readcount] - + (msg_read->data[msg_read->readcount+1]<<8)); - + + (msg_read->data[msg_read->readcount+1]<<8)); + msg_read->readcount += 2; - + return c; } int MSG_ReadLong (sizebuf_t *msg_read) { int c; - + if (msg_read->readcount+4 > msg_read->cursize) c = -1; + else c = msg_read->data[msg_read->readcount] - + (msg_read->data[msg_read->readcount+1]<<8) - + (msg_read->data[msg_read->readcount+2]<<16) - + (msg_read->data[msg_read->readcount+3]<<24); - + + (msg_read->data[msg_read->readcount+1]<<8) + + (msg_read->data[msg_read->readcount+2]<<16) + + (msg_read->data[msg_read->readcount+3]<<24); + msg_read->readcount += 4; - + return c; } @@ -870,9 +938,10 @@ float MSG_ReadFloat (sizebuf_t *msg_read) float f; int l; } dat; - + if (msg_read->readcount+4 > msg_read->cursize) dat.f = -1; + else { dat.b[0] = msg_read->data[msg_read->readcount]; @@ -880,30 +949,35 @@ float MSG_ReadFloat (sizebuf_t *msg_read) dat.b[2] = msg_read->data[msg_read->readcount+2]; dat.b[3] = msg_read->data[msg_read->readcount+3]; } + msg_read->readcount += 4; - + dat.l = LittleLong (dat.l); - return dat.f; + return dat.f; } char *MSG_ReadString (sizebuf_t *msg_read) { static char string[2048]; int l,c; - + l = 0; + do { c = MSG_ReadChar (msg_read); + if (c == -1 || c == 0) break; + string[l] = c; l++; - } while (l < sizeof(string)-1); - + } + while (l < sizeof(string)-1); + string[l] = 0; - + return string; } @@ -911,19 +985,23 @@ char *MSG_ReadStringLine (sizebuf_t *msg_read) { static char string[2048]; int l,c; - + l = 0; + do { c = MSG_ReadChar (msg_read); + if (c == -1 || c == 0 || c == '\n') break; + string[l] = c; l++; - } while (l < sizeof(string)-1); - + } + while (l < sizeof(string)-1); + string[l] = 0; - + return string; } @@ -956,23 +1034,27 @@ void MSG_ReadDeltaUsercmd (sizebuf_t *msg_read, usercmd_t *from, usercmd_t *move memcpy (move, from, sizeof(*move)); bits = MSG_ReadByte (msg_read); - + /* read current angles */ if (bits & CM_ANGLE1) move->angles[0] = MSG_ReadShort (msg_read); + if (bits & CM_ANGLE2) move->angles[1] = MSG_ReadShort (msg_read); + if (bits & CM_ANGLE3) move->angles[2] = MSG_ReadShort (msg_read); - + /* read movement */ if (bits & CM_FORWARD) move->forwardmove = MSG_ReadShort (msg_read); + if (bits & CM_SIDE) move->sidemove = MSG_ReadShort (msg_read); + if (bits & CM_UP) move->upmove = MSG_ReadShort (msg_read); - + /* read buttons */ if (bits & CM_BUTTONS) move->buttons = MSG_ReadByte (msg_read); @@ -1014,44 +1096,46 @@ void SZ_Clear (sizebuf_t *buf) void *SZ_GetSpace (sizebuf_t *buf, int length) { void *data; - + if (buf->cursize + length > buf->maxsize) { if (!buf->allowoverflow) Com_Error (ERR_FATAL, "SZ_GetSpace: overflow without allowoverflow set"); - + if (length > buf->maxsize) Com_Error (ERR_FATAL, "SZ_GetSpace: %i is > full buffer size", length); - - SZ_Clear (buf); + + SZ_Clear (buf); buf->overflowed = true; Com_Printf ("SZ_GetSpace: overflow\n"); } data = buf->data + buf->cursize; buf->cursize += length; - + return data; } void SZ_Write (sizebuf_t *buf, void *data, int length) { - memcpy (SZ_GetSpace(buf,length),data,length); + memcpy (SZ_GetSpace(buf,length),data,length); } void SZ_Print (sizebuf_t *buf, char *data) { int len; - + len = (int)strlen(data)+1; if (buf->cursize) { if (buf->data[buf->cursize-1]) memcpy ((byte *)SZ_GetSpace(buf, len),data,len); /* no trailing 0 */ + else memcpy ((byte *)SZ_GetSpace(buf, len-1)-1,data,len); /* write over trailing 0 */ } + else memcpy ((byte *)SZ_GetSpace(buf, len),data,len); } @@ -1067,13 +1151,13 @@ void SZ_Print (sizebuf_t *buf, char *data) int COM_CheckParm (char *parm) { int i; - + for (i=1 ; i= com_argc || !com_argv[arg]) return ""; + return com_argv[arg]; } @@ -1093,6 +1178,7 @@ void COM_ClearArgv (int arg) { if (arg < 0 || arg >= com_argc || !com_argv[arg]) return; + com_argv[arg] = ""; } @@ -1102,11 +1188,14 @@ void COM_InitArgv (int argc, char **argv) if (argc > MAX_NUM_ARGVS) Com_Error (ERR_FATAL, "argc > MAX_NUM_ARGVS"); + com_argc = argc; + for (i=0 ; i= MAX_TOKEN_CHARS ) com_argv[i] = ""; + else com_argv[i] = argv[i]; } @@ -1119,24 +1208,25 @@ void COM_AddParm (char *parm) { if (com_argc == MAX_NUM_ARGVS) Com_Error (ERR_FATAL, "COM_AddParm: MAX_NUM)ARGS"); + com_argv[com_argc++] = parm; } int memsearch (byte *start, int count, int search) { int i; - + for (i=0 ; imagic != Z_MAGIC) { - printf( "free: %p failed\n", ptr ); - abort(); + if (z->magic != Z_MAGIC) + { + printf( "free: %p failed\n", ptr ); + abort(); Com_Error (ERR_FATAL, "Z_Free: bad magic"); } @@ -1242,6 +1340,7 @@ void Z_FreeTags (int tag) for (z=z_chain.next ; z != &z_chain ; z=next) { next = z->next; + if (z->tag == tag) Z_Free ((void *)(z+1)); } @@ -1250,11 +1349,13 @@ void Z_FreeTags (int tag) void *Z_TagMalloc (int size, int tag) { zhead_t *z; - + size = size + sizeof(zhead_t); z = malloc(size); + if (!z) Com_Error (ERR_FATAL, "Z_Malloc: failed on allocation of %i bytes",size); + memset (z, 0, size); z_count++; z_bytes += size; @@ -1275,71 +1376,72 @@ void *Z_Malloc (int size) return Z_TagMalloc (size, 0); } -static byte chktbl[1024] = { -0x84, 0x47, 0x51, 0xc1, 0x93, 0x22, 0x21, 0x24, 0x2f, 0x66, 0x60, 0x4d, 0xb0, 0x7c, 0xda, -0x88, 0x54, 0x15, 0x2b, 0xc6, 0x6c, 0x89, 0xc5, 0x9d, 0x48, 0xee, 0xe6, 0x8a, 0xb5, 0xf4, -0xcb, 0xfb, 0xf1, 0x0c, 0x2e, 0xa0, 0xd7, 0xc9, 0x1f, 0xd6, 0x06, 0x9a, 0x09, 0x41, 0x54, -0x67, 0x46, 0xc7, 0x74, 0xe3, 0xc8, 0xb6, 0x5d, 0xa6, 0x36, 0xc4, 0xab, 0x2c, 0x7e, 0x85, -0xa8, 0xa4, 0xa6, 0x4d, 0x96, 0x19, 0x19, 0x9a, 0xcc, 0xd8, 0xac, 0x39, 0x5e, 0x3c, 0xf2, -0xf5, 0x5a, 0x72, 0xe5, 0xa9, 0xd1, 0xb3, 0x23, 0x82, 0x6f, 0x29, 0xcb, 0xd1, 0xcc, 0x71, -0xfb, 0xea, 0x92, 0xeb, 0x1c, 0xca, 0x4c, 0x70, 0xfe, 0x4d, 0xc9, 0x67, 0x43, 0x47, 0x94, -0xb9, 0x47, 0xbc, 0x3f, 0x01, 0xab, 0x7b, 0xa6, 0xe2, 0x76, 0xef, 0x5a, 0x7a, 0x29, 0x0b, -0x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x39, 0x3c, 0x20, 0xf0, 0xd3, 0x6e, 0xb1, 0x72, 0xf8, 0xe6, 0x0f, + 0xef, 0x37, 0xe5, 0x09, 0x33, 0x5a, 0x83, 0x43, 0x80, 0x4f, 0x65, 0x2f, 0x7c, 0x8c, 0x6a, + 0xa0, 0x82, 0x0c, 0xd4, 0xd4, 0xfa, 0x81, 0x60, 0x3d, 0xdf, 0x06, 0xf1, 0x5f, 0x08, 0x0d, + 0x6d, 0x43, 0xf2, 0xe3, 0x11, 0x7d, 0x80, 0x32, 0xc5, 0xfb, 0xc5, 0xd9, 0x27, 0xec, 0xc6, + 0x4e, 0x65, 0x27, 0x76, 0x87, 0xa6, 0xee, 0xee, 0xd7, 0x8b, 0xd1, 0xa0, 0x5c, 0xb0, 0x42, + 0x13, 0x0e, 0x95, 0x4a, 0xf2, 0x06, 0xc6, 0x43, 0x33, 0xf4, 0xc7, 0xf8, 0xe7, 0x1f, 0xdd, + 0xe4, 0x46, 0x4a, 0x70, 0x39, 0x6c, 0xd0, 0xed, 0xca, 0xbe, 0x60, 0x3b, 0xd1, 0x7b, 0x57, + 0x48, 0xe5, 0x3a, 0x79, 0xc1, 0x69, 0x33, 0x53, 0x1b, 0x80, 0xb8, 0x91, 0x7d, 0xb4, 0xf6, + 0x17, 0x1a, 0x1d, 0x5a, 0x32, 0xd6, 0xcc, 0x71, 0x29, 0x3f, 0x28, 0xbb, 0xf3, 0x5e, 0x71, + 0xb8, 0x43, 0xaf, 0xf8, 0xb9, 0x64, 0xef, 0xc4, 0xa5, 0x6c, 0x08, 0x53, 0xc7, 0x00, 0x10, + 0x39, 0x4f, 0xdd, 0xe4, 0xb6, 0x19, 0x27, 0xfb, 0xb8, 0xf5, 0x32, 0x73, 0xe5, 0xcb, 0x32 }; /* @@ -1352,7 +1454,7 @@ byte COM_BlockSequenceCRCByte (byte *base, int length, int sequence) byte *p; byte chkb[60 + 4]; unsigned short crc; - byte r; + byte r; if (sequence < 0) Sys_Error("sequence < 0, this shouldn't happen\n"); @@ -1361,6 +1463,7 @@ byte COM_BlockSequenceCRCByte (byte *base, int length, int sequence) if (length > 60) length = 60; + memcpy (chkb, base, length); chkb[length] = p[0]; @@ -1380,12 +1483,12 @@ byte COM_BlockSequenceCRCByte (byte *base, int length, int sequence) return r; } -float frand(void) +float frand(void) { return (rand()&32767)* (1.0/32767); } -float crand(void) +float crand(void) { return (rand()&32767)* (2.0/32767) - 1; } @@ -1443,8 +1546,8 @@ void Qcommon_Init (int argc, char **argv) Cbuf_Execute (); /* init commands and vars */ - Cmd_AddCommand ("z_stats", Z_Stats_f); - Cmd_AddCommand ("error", Com_Error_f); + Cmd_AddCommand ("z_stats", Z_Stats_f); + Cmd_AddCommand ("error", Com_Error_f); host_speeds = Cvar_Get ("host_speeds", "0", 0); log_stats = Cvar_Get ("log_stats", "0", 0); @@ -1453,7 +1556,7 @@ void Qcommon_Init (int argc, char **argv) timescale = Cvar_Get ("timescale", "1", 0); fixedtime = Cvar_Get ("fixedtime", "0", 0); logfile_active = Cvar_Get ("logfile", "0", 0); -#ifndef DEDICATED_ONLY +#ifndef DEDICATED_ONLY showtrace = Cvar_Get ("showtrace", "0", 0); #endif #ifdef DEDICATED_ONLY @@ -1465,7 +1568,6 @@ void Qcommon_Init (int argc, char **argv) s = va("%4.2f %s %s %s", VERSION, CPUSTRING, __DATE__, BUILDSTRING); Cvar_Get ("version", s, CVAR_SERVERINFO|CVAR_NOSET); - if (dedicated->value) Cmd_AddCommand ("quit", Com_Quit); @@ -1475,35 +1577,42 @@ void Qcommon_Init (int argc, char **argv) Netchan_Init (); SV_Init (); -#ifndef DEDICATED_ONLY +#ifndef DEDICATED_ONLY CL_Init (); #endif /* add + commands from command line */ if (!Cbuf_AddLateCommands ()) - { /* if the user didn't give any commands, run default action */ + { + /* if the user didn't give any commands, run default action */ if (!dedicated->value) Cbuf_AddText ("d1\n"); + else Cbuf_AddText ("dedicated_start\n"); + Cbuf_Execute (); } -#ifndef DEDICATED_ONLY + +#ifndef DEDICATED_ONLY + else - { /* the user asked for something explicit + { + /* the user asked for something explicit so drop the loading plaque */ SCR_EndLoadingPlaque (); } + #endif - Com_Printf ("====== Quake2 Initialized ======\n\n"); + Com_Printf ("====== Quake2 Initialized ======\n\n"); } void Qcommon_Frame (int msec) { char *s; -#ifndef DEDICATED_ONLY +#ifndef DEDICATED_ONLY int time_before = 0; int time_between = 0; int time_after; @@ -1515,6 +1624,7 @@ void Qcommon_Frame (int msec) if ( log_stats->modified ) { log_stats->modified = false; + if ( log_stats->value ) { if ( log_stats_file ) @@ -1522,10 +1632,13 @@ void Qcommon_Frame (int msec) fclose( log_stats_file ); log_stats_file = 0; } + log_stats_file = fopen( "stats.log", "w" ); + if ( log_stats_file ) fprintf( log_stats_file, "entities,dlights,parts,frame time\n" ); } + else { if ( log_stats_file ) @@ -1538,14 +1651,17 @@ void Qcommon_Frame (int msec) if (fixedtime->value) msec = fixedtime->value; + else if (timescale->value) { msec *= timescale->value; + if (msec < 1) msec = 1; } #ifndef DEDICATED_ONLY + if (showtrace->value) { extern int c_traces, c_brush_traces; @@ -1556,31 +1672,38 @@ void Qcommon_Frame (int msec) c_brush_traces = 0; c_pointcontents = 0; } + #endif do { s = Sys_ConsoleInput (); + if (s) Cbuf_AddText (va("%s\n",s)); - } while (s); + } + while (s); + Cbuf_Execute (); -#ifndef DEDICATED_ONLY +#ifndef DEDICATED_ONLY + if (host_speeds->value) time_before = Sys_Milliseconds (); + #endif SV_Frame (msec); -#ifndef DEDICATED_ONLY +#ifndef DEDICATED_ONLY + if (host_speeds->value) - time_between = Sys_Milliseconds (); + time_between = Sys_Milliseconds (); CL_Frame (msec); if (host_speeds->value) - time_after = Sys_Milliseconds (); + time_after = Sys_Milliseconds (); if (host_speeds->value) @@ -1595,8 +1718,9 @@ void Qcommon_Frame (int msec) sv -= gm; cl -= rf; Com_Printf ("all:%3i sv:%3i gm:%3i cl:%3i rf:%3i\n", - all, sv, gm, cl, rf); + all, sv, gm, cl, rf); } + #endif } diff --git a/src/common/crc.c b/src/common/crc.c index 7fe58910..7cda6ed7 100644 --- a/src/common/crc.c +++ b/src/common/crc.c @@ -1,30 +1,33 @@ /* -Copyright (C) 1997-2001 Id Software, Inc. - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - -See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - -*/ -/* crc.c */ + * Copyright (C) 1997-2001 Id Software, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + * ======================================================================= + * + * This is a 16 bit, non-reflected CRC using the polynomial 0x1021 + * and the initial and final xor values shown below... In other words, + * the CCITT standard CRC used by XMODEM. + * + * ======================================================================= + */ #include "qcommon.h" -/* this is a 16 bit, non-reflected CRC using the polynomial 0x1021 - and the initial and final xor values shown below... in other words, the - CCITT standard CRC used by XMODEM */ - #define CRC_INIT_VALUE 0xffff #define CRC_XOR_VALUE 0x0000 @@ -84,9 +87,9 @@ unsigned short CRC_Block (byte *start, int count) unsigned short crc; CRC_Init (&crc); + while (count--) crc = (crc << 8) ^ crctable[(crc >> 8) ^ *start++]; return crc; } - diff --git a/src/common/crc.h b/src/common/crc.h index 5b4977e3..fbdf5f1a 100644 --- a/src/common/crc.h +++ b/src/common/crc.h @@ -1,23 +1,28 @@ /* -Copyright (C) 1997-2001 Id Software, Inc. - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - -See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - -*/ -/* crc.h */ + * Copyright (C) 1997-2001 Id Software, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + * ======================================================================= + * + * Corresponding header for crc.c + * + * ======================================================================= + */ void CRC_Init(unsigned short *crcvalue); void CRC_ProcessByte(unsigned short *crcvalue, byte data); diff --git a/src/common/cvar.c b/src/common/cvar.c index 889eb7f0..c9e0b746 100644 --- a/src/common/cvar.c +++ b/src/common/cvar.c @@ -1,23 +1,28 @@ /* -Copyright (C) 1997-2001 Id Software, Inc. - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - -See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - -*/ -// cvar.c -- dynamic variable tracking + * Copyright (C) 1997-2001 Id Software, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + * ======================================================================= + * + * The Quake II CVAR subsystem. Implements dynamic variable tracking. + * + * ======================================================================= + */ #include "qcommon.h" @@ -27,17 +32,20 @@ static qboolean Cvar_InfoValidate (char *s) { if (strstr (s, "\\")) return false; + if (strstr (s, "\"")) return false; + if (strstr (s, ";")) return false; + return true; } static cvar_t *Cvar_FindVar (const char *var_name) { cvar_t *var; - + for (var=cvar_vars ; var ; var=var->next) if (!strcmp (var_name, var->name)) return var; @@ -48,20 +56,24 @@ static cvar_t *Cvar_FindVar (const char *var_name) float Cvar_VariableValue (char *var_name) { cvar_t *var; - + var = Cvar_FindVar (var_name); + if (!var) return 0; + return atof (var->string); } const char *Cvar_VariableString (const char *var_name) { cvar_t *var; - + var = Cvar_FindVar (var_name); + if (!var) return ""; + return var->string; } @@ -69,12 +81,12 @@ char *Cvar_CompleteVariable (char *partial) { cvar_t *cvar; int len; - + len = (int)strlen(partial); - + if (!len) return NULL; - + /* check exact match */ for (cvar=cvar_vars ; cvar ; cvar=cvar->next) if (!strcmp (partial,cvar->name)) @@ -95,7 +107,7 @@ char *Cvar_CompleteVariable (char *partial) cvar_t *Cvar_Get (char *var_name, char *var_value, int flags) { cvar_t *var; - + if (flags & (CVAR_USERINFO | CVAR_SERVERINFO)) { if (!Cvar_InfoValidate (var_name)) @@ -106,6 +118,7 @@ cvar_t *Cvar_Get (char *var_name, char *var_value, int flags) } var = Cvar_FindVar (var_name); + if (var) { var->flags |= flags; @@ -144,6 +157,7 @@ cvar_t *Cvar_Set2 (char *var_name, char *value, qboolean force) cvar_t *var; var = Cvar_FindVar (var_name); + if (!var) { return Cvar_Get (var_name, value, 0); @@ -172,8 +186,10 @@ cvar_t *Cvar_Set2 (char *var_name, char *value, qboolean force) { if (strcmp(value, var->latched_string) == 0) return var; + Z_Free (var->latched_string); } + else { if (strcmp(value, var->string) == 0) @@ -185,6 +201,7 @@ cvar_t *Cvar_Set2 (char *var_name, char *value, qboolean force) Com_Printf ("%s will be changed for next game.\n", var_name); var->latched_string = CopyString(value); } + else { var->string = CopyString(value); @@ -196,9 +213,11 @@ cvar_t *Cvar_Set2 (char *var_name, char *value, qboolean force) FS_ExecAutoexec (); } } + return var; } } + else { if (var->latched_string) @@ -214,10 +233,10 @@ cvar_t *Cvar_Set2 (char *var_name, char *value, qboolean force) var->modified = true; if (var->flags & CVAR_USERINFO) - userinfo_modified = true; - + userinfo_modified = true; + Z_Free (var->string); - + var->string = CopyString(value); var->value = atof (var->string); @@ -237,10 +256,11 @@ cvar_t *Cvar_Set (char *var_name, char *value) cvar_t *Cvar_FullSet (char *var_name, char *value, int flags) { cvar_t *var; - + var = Cvar_FindVar (var_name); + if (!var) - { + { return Cvar_Get (var_name, value, flags); } @@ -248,9 +268,9 @@ cvar_t *Cvar_FullSet (char *var_name, char *value, int flags) if (var->flags & CVAR_USERINFO) userinfo_modified = true; - + Z_Free (var->string); - + var->string = CopyString(value); var->value = (float)atof (var->string); @@ -265,12 +285,13 @@ void Cvar_SetValue (char *var_name, float value) if (value == (int)value) Com_sprintf (val, sizeof(val), "%i",(int)value); + else Com_sprintf (val, sizeof(val), "%f",value); + Cvar_Set (var_name, val); } - /* * Any variables with latched values will now be updated */ @@ -282,11 +303,12 @@ void Cvar_GetLatchedVars (void) { if (!var->latched_string) continue; + Z_Free (var->string); var->string = var->latched_string; var->latched_string = NULL; var->value = atof(var->string); - + if (!strcmp(var->name, "game")) { FS_SetGamedir (var->string); @@ -304,9 +326,10 @@ qboolean Cvar_Command (void) /* check variables */ v = Cvar_FindVar (Cmd_Argv(0)); + if (!v) return false; - + /* perform a variable print or set */ if (Cmd_Argc() == 1) { @@ -327,6 +350,7 @@ void Cvar_Set_f (void) int flags; c = Cmd_Argc(); + if (c != 3 && c != 4) { Com_Printf ("usage: set [u / s]\n"); @@ -337,20 +361,23 @@ void Cvar_Set_f (void) { if (!strcmp(Cmd_Argv(3), "u")) flags = CVAR_USERINFO; + else if (!strcmp(Cmd_Argv(3), "s")) flags = CVAR_SERVERINFO; + else { Com_Printf ("flags can only be 'u' or 's'\n"); return; } + Cvar_FullSet (Cmd_Argv(1), Cmd_Argv(2), flags); } + else Cvar_Set (Cmd_Argv(1), Cmd_Argv(2)); } - /* * Appends lines containing "set variable value" for all variables * with the archive flag set to true. @@ -362,6 +389,7 @@ void Cvar_WriteVariables (char *path) FILE *f; f = fopen (path, "a"); + for (var = cvar_vars ; var ; var = var->next) { if (var->flags & CVAR_ARCHIVE) @@ -370,6 +398,7 @@ void Cvar_WriteVariables (char *path) fprintf (f, "%s", buffer); } } + fclose (f); } @@ -379,34 +408,45 @@ void Cvar_List_f (void) int i; i = 0; + for (var = cvar_vars ; var ; var = var->next, i++) { if (var->flags & CVAR_ARCHIVE) Com_Printf ("*"); + else Com_Printf (" "); + if (var->flags & CVAR_USERINFO) Com_Printf ("U"); + else Com_Printf (" "); + if (var->flags & CVAR_SERVERINFO) Com_Printf ("S"); + else Com_Printf (" "); + if (var->flags & CVAR_NOSET) Com_Printf ("-"); + else if (var->flags & CVAR_LATCH) Com_Printf ("L"); + else Com_Printf (" "); + Com_Printf (" %s \"%s\"\n", var->name, var->string); } + Com_Printf ("%i cvars\n", i); } qboolean userinfo_modified; -char *Cvar_BitInfo (int bit) +char *Cvar_BitInfo (int bit) { static char info[MAX_INFO_STRING]; cvar_t *var; @@ -418,20 +458,21 @@ char *Cvar_BitInfo (int bit) if (var->flags & bit) Info_SetValueForKey (info, var->name, var->string); } + return info; } -/* - * returns an info string containing +/* + * returns an info string containing * all the CVAR_USERINFO cvars */ -char *Cvar_Userinfo (void) +char *Cvar_Userinfo (void) { return Cvar_BitInfo (CVAR_USERINFO); } -/* - * returns an info string containing +/* + * returns an info string containing * all the CVAR_SERVERINFO cvars */ char *Cvar_Serverinfo (void) diff --git a/src/common/files.c b/src/common/files.c index 48ce9f9b..fa7fd99f 100644 --- a/src/common/files.c +++ b/src/common/files.c @@ -1,54 +1,65 @@ /* * Copyright (C) 1997-2001 Id Software, Inc. * - * This program is free software; you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free - * Software Foundation; either version 2 of the License, or (at your option) - * any later version. + * This program is 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. + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * See the GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this program; if not, write to the Free Software Foundation, Inc., 59 - * Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. * + * ======================================================================= + * + * The Quake II file system, implements generic file system operations + * as well as the .pak file format and support for .pk3 files. + * + * ======================================================================= */ #include "qcommon.h" #include "../posix/glob/glob.h" #include "../unzip/unzip.h" -#define MAX_HANDLES 512 -#define MAX_READ 0x10000 -#define MAX_WRITE 0x10000 +#define MAX_HANDLES 512 +#define MAX_READ 0x10000 +#define MAX_WRITE 0x10000 #define MAX_FIND_FILES 0x04000 -#define MAX_PAKS 100 +#define MAX_PAKS 100 -typedef struct { +typedef struct +{ char name[MAX_QPATH]; fsMode_t mode; FILE *file; /* Only one will be used. */ unzFile *zip; /* (file or zip) */ } fsHandle_t; -typedef struct fsLink_s { +typedef struct fsLink_s +{ char *from; char *to; int length; struct fsLink_s *next; } fsLink_t; -typedef struct { +typedef struct +{ char name[MAX_QPATH]; int size; - int offset; /* Ignored in PK3 files. */ + int offset; /* Ignored in PK3 files. */ } fsPackFile_t; -typedef struct { +typedef struct +{ char name[MAX_OSPATH]; int numFiles; FILE *pak; @@ -56,18 +67,21 @@ typedef struct { fsPackFile_t *files; } fsPack_t; -typedef struct fsSearchPath_s { - char path[MAX_OSPATH]; /* Only one used. */ - fsPack_t *pack; /* (path or pack) */ +typedef struct fsSearchPath_s +{ + char path[MAX_OSPATH]; /* Only one used. */ + fsPack_t *pack; /* (path or pack) */ struct fsSearchPath_s *next; } fsSearchPath_t; -typedef enum { +typedef enum +{ PAK, PK3 } fsPackFormat_t; -typedef struct { +typedef struct +{ char *suffix; fsPackFormat_t format; } fsPackTypes_t; @@ -77,12 +91,13 @@ fsLink_t *fs_links; fsSearchPath_t *fs_searchPaths; fsSearchPath_t *fs_baseSearchPaths; -/* Pack formats / suffixes. */ -fsPackTypes_t fs_packtypes[] = { - {"pak", PAK}, - {"pk2", PK3}, - {"pk3", PK3}, - {"zip", PK3} +/* Pack formats / suffixes. */ +fsPackTypes_t fs_packtypes[] = +{ + {"pak", PAK}, + {"pk2", PK3}, + {"pk3", PK3}, + {"zip", PK3} }; char fs_gamedir[MAX_OSPATH]; @@ -133,16 +148,24 @@ Com_FilePath(const char *path, char *dst, int dstSize) { char *pos; /* Position of the last '/'. */ - if ((pos = strrchr(path, '/')) != NULL) { + if ((pos = strrchr(path, '/')) != NULL) + { pos--; - if ((pos - path) < dstSize) { + + if ((pos - path) < dstSize) + { memcpy(dst, path, pos - path); dst[pos - path] = '\0'; - } else { + } + + else + { Com_Printf("Com_FilePath: not enough space.\n"); return; } - } else + } + + else strncpy(dst, path, dstSize); } @@ -171,17 +194,23 @@ FS_CreatePath(char *path) FS_DPrintf("FS_CreatePath(%s)\n", path); - if (strstr(path, "..") != NULL) { + if (strstr(path, "..") != NULL) + { Com_Printf("WARNING: refusing to create relative path '%s'.\n", path); return; } + cur = old = path; - while (cur != NULL) { - if ((cur - old) > 1) { + + while (cur != NULL) + { + if ((cur - old) > 1) + { *cur = '\0'; Sys_Mkdir(path); *cur = '/'; } + old = cur; cur = strchr(old + 1, '/'); } @@ -218,7 +247,7 @@ FS_DeletePath(char *path) } /* - * Returns a FILE * for a fileHandle_t. + * Returns a FILE * for a fileHandle_t. */ FILE * FS_FileForHandle(fileHandle_t f) @@ -237,17 +266,20 @@ FS_FileForHandle(fileHandle_t f) } /* - * Finds a free fileHandle_t. + * Finds a free fileHandle_t. */ -fsHandle_t * +fsHandle_t * FS_HandleForFile(const char *path, fileHandle_t * f) { int i; fsHandle_t *handle; handle = fs_handles; - for (i = 0; i < MAX_HANDLES; i++, handle++) { - if (handle->file == NULL && handle->zip == NULL) { + + for (i = 0; i < MAX_HANDLES; i++, handle++) + { + if (handle->file == NULL && handle->zip == NULL) + { strncpy(handle->name, path, sizeof(handle->name)); *f = i + 1; return (handle); @@ -261,9 +293,9 @@ FS_HandleForFile(const char *path, fileHandle_t * f) } /* - * Returns a fsHandle_t * for the given fileHandle_t. + * Returns a fsHandle_t * for the given fileHandle_t. */ -fsHandle_t * +fsHandle_t * FS_GetFileByHandle(fileHandle_t f) { if (f < 0 || f > MAX_HANDLES) @@ -278,7 +310,7 @@ FS_GetFileByHandle(fileHandle_t f) } /* - * Returns file size or -1 on error. + * Returns file size or -1 on error. */ int FS_FOpenFileAppend(fsHandle_t * handle) @@ -290,11 +322,15 @@ FS_FOpenFileAppend(fsHandle_t * handle) Com_sprintf(path, sizeof(path), "%s/%s", fs_gamedir, handle->name); handle->file = fopen(path, "a"); - if (handle->file) { + + if (handle->file) + { if (fs_debug->value) Com_Printf("FS_FOpenFileAppend: '%s'.\n", path); + return (FS_FileLength(handle->file)); } + if (fs_debug->value) Com_Printf("FS_FOpenFileAppend: couldn't open '%s'.\n", path); @@ -302,7 +338,7 @@ FS_FOpenFileAppend(fsHandle_t * handle) } /* - * Always returns 0 or -1 on error. + * Always returns 0 or -1 on error. */ int FS_FOpenFileWrite(fsHandle_t * handle) @@ -313,11 +349,14 @@ FS_FOpenFileWrite(fsHandle_t * handle) Com_sprintf(path, sizeof(path), "%s/%s", fs_gamedir, handle->name); - if ((handle->file = fopen(path, "w")) == NULL) { + if ((handle->file = fopen(path, "w")) == NULL) + { if (fs_debug->value) Com_Printf("FS_FOpenFileWrite: '%s'.\n", path); + return (0); } + if (fs_debug->value) Com_Printf("FS_FOpenFileWrite: couldn't open '%s'.\n", path); @@ -326,7 +365,7 @@ FS_FOpenFileWrite(fsHandle_t * handle) /* * Returns file size or -1 if not found. Can open separate files as well as - * files inside pack files (both PAK and PK3). + * files inside pack files (both PAK and PK3). */ int FS_FOpenFileRead(fsHandle_t * handle) @@ -340,68 +379,87 @@ FS_FOpenFileRead(fsHandle_t * handle) file_from_pk3 = 0; /* Search through the path, one element at a time. */ - for (search = fs_searchPaths; search; search = search->next) { + for (search = fs_searchPaths; search; search = search->next) + { /* Search inside a pack file. */ - if (search->pack) { + if (search->pack) + { pack = search->pack; - for (i = 0; i < pack->numFiles; i++) { - if (Q_stricmp(pack->files[i].name, handle->name) == 0) { + + for (i = 0; i < pack->numFiles; i++) + { + if (Q_stricmp(pack->files[i].name, handle->name) == 0) + { /* Found it! */ Com_FilePath(pack->name, fs_fileInPath, sizeof(fs_fileInPath)); fs_fileInPack = true; if (fs_debug->value) Com_Printf("FS_FOpenFileRead: '%s' (found in '%s').\n", - handle->name, pack->name); + handle->name, pack->name); - if (pack->pak) { + if (pack->pak) + { /* PAK */ file_from_pak = 1; handle->file = fopen(pack->name, "r"); - if (handle->file) { + if (handle->file) + { fseek(handle->file, pack->files[i].offset, SEEK_SET); return (pack->files[i].size); } - } else if (pack->pk3) { + } + + else if (pack->pk3) + { /* PK3 */ file_from_pk3 = 1; strncpy(file_from_pk3_name, strrchr(pack->name, '/') + 1, sizeof(file_from_pk3_name)); handle->zip = unzOpen(pack->name); - if (handle->zip) { - if (unzLocateFile(handle->zip, handle->name, 2) == UNZ_OK) { + + if (handle->zip) + { + if (unzLocateFile(handle->zip, handle->name, 2) == UNZ_OK) + { if (unzOpenCurrentFile(handle->zip) == UNZ_OK) return (pack->files[i].size); } + unzClose(handle->zip); } } + Com_Error(ERR_FATAL, "Couldn't reopen '%s'", pack->name); } } - } else { + } + + else + { /* Search in a directory tree. */ Com_sprintf(path, sizeof(path), "%s/%s", search->path, handle->name); - + handle->file = fopen(path, "r"); - + if (!handle->file) { strlwr(path); handle->file = fopen(path, "r"); } - + if (!handle->file) continue; - - if (handle->file) { + + if (handle->file) + { /* Found it! */ strncpy(fs_fileInPath, search->path, sizeof(fs_fileInPath)); fs_fileInPack = false; if (fs_debug->value) Com_Printf("FS_FOpenFileRead: '%s' (found in '%s').\n", - handle->name, search->path); + handle->name, search->path); return FS_FileLength(handle->file); } @@ -430,10 +488,13 @@ FS_FCloseFile(fileHandle_t f) if (handle->file) fclose(handle->file); - else if (handle->zip) { + + else if (handle->zip) + { unzCloseCurrentFile(handle->zip); unzClose(handle->zip); } + memset(handle, 0, sizeof(*handle)); } @@ -446,12 +507,15 @@ Developer_searchpath(int who) if (who == 1) /* xatrix */ ch = 'x'; + else if (who == 2) /* rogue */ ch = 'r'; - for (search = fs_searchPaths; search; search = search->next) { + for (search = fs_searchPaths; search; search = search->next) + { if (strstr(search->path, "xatrix")) return (1); + if (strstr(search->path, "rogue")) return (2); } @@ -464,7 +528,8 @@ modType(char *name) { fsSearchPath_t *search; - for (search = fs_searchPaths; search; search = search->next) { + for (search = fs_searchPaths; search; search = search->next) + { if (strstr(search->path, name)) return true; } @@ -487,19 +552,20 @@ FS_FOpenFile(const char *name, fileHandle_t * f, fsMode_t mode) strncpy(handle->name, name, sizeof(handle->name)); handle->mode = mode; - switch (mode) { - case FS_READ: - size = FS_FOpenFileRead(handle); - break; - case FS_WRITE: - size = FS_FOpenFileWrite(handle); - break; - case FS_APPEND: - size = FS_FOpenFileAppend(handle); - break; - default: - Com_Error(ERR_FATAL, "FS_FOpenFile: bad mode (%i)", mode); - break; + switch (mode) + { + case FS_READ: + size = FS_FOpenFileRead(handle); + break; + case FS_WRITE: + size = FS_FOpenFileWrite(handle); + break; + case FS_APPEND: + size = FS_FOpenFileAppend(handle); + break; + default: + Com_Error(ERR_FATAL, "FS_FOpenFile: bad mode (%i)", mode); + break; } if (size != -1) @@ -513,16 +579,16 @@ FS_FOpenFile(const char *name, fileHandle_t * f, fsMode_t mode) } /* - * Properly handles partial reads. + * Properly handles partial reads. */ int FS_Read(void *buffer, int size, fileHandle_t f) { qboolean tried = false; /* Tried to read from a CD. */ - byte *buf; /* Buffer. */ - int r; /* Number of bytes read. */ - int remaining;/* Remaining bytes. */ - fsHandle_t *handle; /* File handle. */ + byte *buf; /* Buffer. */ + int r; /* Number of bytes read. */ + int remaining;/* Remaining bytes. */ + fsHandle_t *handle; /* File handle. */ handle = FS_GetFileByHandle(f); @@ -530,24 +596,34 @@ FS_Read(void *buffer, int size, fileHandle_t f) remaining = size; buf = (byte *) buffer; - while (remaining) { + while (remaining) + { if (handle->file) r = fread(buf, 1, remaining, handle->file); + else if (handle->zip) r = unzReadCurrentFile(handle->zip, buf, remaining); + else return (0); - if (r == 0) { - if (!tried) { + if (r == 0) + { + if (!tried) + { /* Might tried to read from a CD. */ tried = true; - } else { + } + + else + { /* Already tried once. */ Com_Error(ERR_FATAL, va("FS_Read: 0 bytes read from '%s'", handle->name)); return (size - remaining); } - } else if (r == -1) + } + + else if (r == -1) Com_Error(ERR_FATAL, "FS_Read: -1 bytes read from '%s'", handle->name); remaining -= r; @@ -559,17 +635,17 @@ FS_Read(void *buffer, int size, fileHandle_t f) /* * Properly handles partial reads of size up to count times. No error if it - * can't read. + * can't read. */ int FS_FRead(void *buffer, int size, int count, fileHandle_t f) { - qboolean tried = false; /* Tried to read from a CD. */ - byte *buf; /* Buffer. */ - int loops; /* Loop indicator. */ - int r; /* Number of bytes read. */ - int remaining; /* Remaining bytes. */ - fsHandle_t *handle; /* File handle. */ + qboolean tried = false; /* Tried to read from a CD. */ + byte *buf; /* Buffer. */ + int loops; /* Loop indicator. */ + int r; /* Number of bytes read. */ + int remaining; /* Remaining bytes. */ + fsHandle_t *handle; /* File handle. */ handle = FS_GetFileByHandle(f); @@ -577,31 +653,44 @@ FS_FRead(void *buffer, int size, int count, fileHandle_t f) loops = count; buf = (byte *) buffer; - while (loops) { + while (loops) + { /* Read in chunks. */ remaining = size; - while (remaining) { + + while (remaining) + { if (handle->file) r = fread(buf, 1, remaining, handle->file); + else if (handle->zip) r = unzReadCurrentFile(handle->zip, buf, remaining); + else return (0); - if (r == 0) { - if (!tried) { + if (r == 0) + { + if (!tried) + { /* Might tried to read from a CD. */ tried = true; - } else { + } + + else + { /* Already tried once. */ return (size - remaining); } - } else if (r == -1) + } + + else if (r == -1) Com_Error(ERR_FATAL, "FS_FRead: -1 bytes read from '%s'", handle->name); remaining -= r; buf += r; } + loops--; } @@ -609,16 +698,16 @@ FS_FRead(void *buffer, int size, int count, fileHandle_t f) } /* - * Properly handles partial writes. + * Properly handles partial writes. */ int FS_Write(const void *buffer, int size, fileHandle_t f) { - byte *buf; /* Buffer. */ - int remaining; /* Remaining bytes. */ - int w = 0; /* Numbre of bytes written. */ - fsHandle_t *handle; /* File handle. */ + byte *buf; /* Buffer. */ + int remaining; /* Remaining bytes. */ + int w = 0; /* Numbre of bytes written. */ + fsHandle_t *handle; /* File handle. */ handle = FS_GetFileByHandle(f); @@ -626,18 +715,24 @@ FS_Write(const void *buffer, int size, fileHandle_t f) remaining = size; buf = (byte *) buffer; - while (remaining) { + while (remaining) + { if (handle->file) w = fwrite(buf, 1, remaining, handle->file); + else if (handle->zip) Com_Error(ERR_FATAL, "FS_Write: can't write to zip file '%s'", handle->name); + else return (0); - if (w == 0) { + if (w == 0) + { Com_Printf("FS_Write: 0 bytes written to '%s'.\n", handle->name); return size - remaining; - } else if (w == -1) + } + + else if (w == -1) Com_Error(ERR_FATAL, "FS_Write: -1 bytes written to '%s'", handle->name); remaining -= w; @@ -656,6 +751,7 @@ FS_FTell(fileHandle_t f) if (handle->file) return ftell(handle->file); + else if (handle->zip) return unztell(handle->zip); @@ -668,16 +764,16 @@ FS_FTell(fileHandle_t f) char ** FS_ListPak(char *find, int *num) { - char **list = 0; /* File list. */ - int nfiles = 0; /* Number of files. */ - int nfound = 0; /* Number of files found. */ - int i; /* Loop counter. */ - fsPack_t *pak; /* Pak file. */ - fsSearchPath_t *search; /* Search path. */ - + char **list = 0; /* File list. */ + int nfiles = 0; /* Number of files. */ + int nfound = 0; /* Number of files found. */ + int i; /* Loop counter. */ + fsPack_t *pak; /* Pak file. */ + fsSearchPath_t *search; /* Search path. */ /* Check pak files. */ - for (search = fs_searchPaths; search != NULL; search = search->next) { + for (search = fs_searchPaths; search != NULL; search = search->next) + { if (search->pack == NULL) continue; @@ -690,15 +786,18 @@ FS_ListPak(char *find, int *num) list = calloc(nfiles, sizeof(char *)); - for (search = fs_searchPaths; search != NULL; search = search->next) { + for (search = fs_searchPaths; search != NULL; search = search->next) + { if (search->pack == NULL) continue; pak = search->pack; /* Find and build list. */ - for (i = 0; i < pak->numFiles; i++) { - if (strstr(pak->files[i].name, find)) { + for (i = 0; i < pak->numFiles; i++) + { + if (strstr(pak->files[i].name, find)) + { list[nfound] = strdup(pak->files[i].name); nfound++; } @@ -714,45 +813,51 @@ FS_ListPak(char *find, int *num) void FS_Seek(fileHandle_t f, int offset, fsOrigin_t origin) { - byte dummy[0x8000]; /* Dummy buffer to skip bytes. */ - int len; /* Length of byte chunk to skip. */ - int r; /* Number of bytes read. */ - int remaining = 0; /* Remaining bytes to skip. */ - fsHandle_t *handle; /* File handle. */ - unz_file_info info; /* ZIP information. */ + byte dummy[0x8000]; /* Dummy buffer to skip bytes. */ + int len; /* Length of byte chunk to skip. */ + int r; /* Number of bytes read. */ + int remaining = 0; /* Remaining bytes to skip. */ + fsHandle_t *handle; /* File handle. */ + unz_file_info info; /* ZIP information. */ handle = FS_GetFileByHandle(f); - if (handle->file) { - switch (origin) { - case FS_SEEK_SET: - fseek(handle->file, offset, SEEK_SET); - break; - case FS_SEEK_CUR: - fseek(handle->file, offset, SEEK_CUR); - break; - case FS_SEEK_END: - fseek(handle->file, offset, SEEK_END); - break; - default: - Com_Error(ERR_FATAL, "FS_Seek: bad origin (%i)", origin); - break; + if (handle->file) + { + switch (origin) + { + case FS_SEEK_SET: + fseek(handle->file, offset, SEEK_SET); + break; + case FS_SEEK_CUR: + fseek(handle->file, offset, SEEK_CUR); + break; + case FS_SEEK_END: + fseek(handle->file, offset, SEEK_END); + break; + default: + Com_Error(ERR_FATAL, "FS_Seek: bad origin (%i)", origin); + break; } - } else if (handle->zip) { - switch (origin) { - case FS_SEEK_SET: - remaining = offset; - break; - case FS_SEEK_CUR: - remaining = offset + unztell(handle->zip); - break; - case FS_SEEK_END: - unzGetCurrentFileInfo(handle->zip, &info, NULL, 0, NULL, 0, NULL, 0); - remaining = offset + info.uncompressed_size; - break; - default: - Com_Error(ERR_FATAL, "FS_Seek: bad origin (%i)", origin); - break; + } + + else if (handle->zip) + { + switch (origin) + { + case FS_SEEK_SET: + remaining = offset; + break; + case FS_SEEK_CUR: + remaining = offset + unztell(handle->zip); + break; + case FS_SEEK_END: + unzGetCurrentFileInfo(handle->zip, &info, NULL, 0, NULL, 0, NULL, 0); + remaining = offset + info.uncompressed_size; + break; + default: + Com_Error(ERR_FATAL, "FS_Seek: bad origin (%i)", origin); + break; } /* Reopen the file. */ @@ -760,13 +865,18 @@ FS_Seek(fileHandle_t f, int offset, fsOrigin_t origin) unzOpenCurrentFile(handle->zip); /* Skip until the desired offset is reached. */ - while (remaining) { + while (remaining) + { len = remaining; + if (len > sizeof(dummy)) len = sizeof(dummy); + r = unzReadCurrentFile(handle->zip, dummy, len); + if (r <= 0) break; + remaining -= r; } } @@ -784,6 +894,7 @@ FS_Tell(fileHandle_t f) if (handle->file) return (ftell(handle->file)); + else if (handle->zip) return (unztell(handle->zip)); @@ -797,10 +908,12 @@ FS_FileExists(char *path) FS_FOpenFile(path, &f, FS_READ); - if (f != 0) { + if (f != 0) + { FS_FCloseFile(f); return (true); } + return (false); } @@ -829,22 +942,27 @@ FS_DeleteFile(const char *path) int FS_LoadFile(char *path, void **buffer) { - byte *buf; /* Buffer. */ - int size; /* File size. */ - fileHandle_t f; /* File handle. */ + byte *buf; /* Buffer. */ + int size; /* File size. */ + fileHandle_t f; /* File handle. */ buf = NULL; size = FS_FOpenFile(path, &f, FS_READ); - if (size <= 0) { + if (size <= 0) + { if (buffer) *buffer = NULL; + return (size); } - if (buffer == NULL) { + + if (buffer == NULL) + { FS_FCloseFile(f); return (size); } + buf = Z_Malloc(size); *buffer = buf; @@ -857,10 +975,12 @@ FS_LoadFile(char *path, void **buffer) void FS_FreeFile(void *buffer) { - if (buffer == NULL) { + if (buffer == NULL) + { FS_DPrintf("FS_FreeFile: NULL buffer.\n"); return; } + Z_Free(buffer); } @@ -873,39 +993,46 @@ FS_FreeFile(void *buffer) fsPack_t * FS_LoadPAK(const char *packPath) { - int i; /* Loop counter. */ - int numFiles; /* Number of files in PAK. */ - FILE *handle; /* File handle. */ - fsPackFile_t *files; /* List of files in PAK. */ - fsPack_t *pack; /* PAK file. */ - dpackheader_t header; /* PAK file header. */ - dpackfile_t info[MAX_FILES_IN_PACK]; /* PAK info. */ + int i; /* Loop counter. */ + int numFiles; /* Number of files in PAK. */ + FILE *handle; /* File handle. */ + fsPackFile_t *files; /* List of files in PAK. */ + fsPack_t *pack; /* PAK file. */ + dpackheader_t header; /* PAK file header. */ + dpackfile_t info[MAX_FILES_IN_PACK]; /* PAK info. */ handle = fopen(packPath, "r"); + if (handle == NULL) return (NULL); fread(&header, 1, sizeof(dpackheader_t), handle); - if (LittleLong(header.ident) != IDPAKHEADER) { + if (LittleLong(header.ident) != IDPAKHEADER) + { fclose(handle); Com_Error(ERR_FATAL, "FS_LoadPAK: '%s' is not a pack file", packPath); } + header.dirofs = LittleLong(header.dirofs); header.dirlen = LittleLong(header.dirlen); numFiles = header.dirlen / sizeof(dpackfile_t); - if (numFiles > MAX_FILES_IN_PACK || numFiles == 0) { + + if (numFiles > MAX_FILES_IN_PACK || numFiles == 0) + { fclose(handle); Com_Error(ERR_FATAL, "FS_LoadPAK: '%s' has %i files", packPath, numFiles); } + files = Z_Malloc(numFiles * sizeof(fsPackFile_t)); fseek(handle, header.dirofs, SEEK_SET); fread(info, 1, header.dirlen, handle); /* Parse the directory. */ - for (i = 0; i < numFiles; i++) { + for (i = 0; i < numFiles; i++) + { strncpy(files[i].name, info[i].name, sizeof(files[i].name)); files[i].offset = LittleLong(info[i].filepos); files[i].size = LittleLong(info[i].filelen); @@ -929,42 +1056,49 @@ FS_LoadPAK(const char *packPath) * Loads the header and directory, adding the files at the beginning of the list * so they override previous pack files. */ -fsPack_t * +fsPack_t * FS_LoadPK3(const char *packPath) { - char fileName[MAX_QPATH]; /* File name. */ - int i = 0; /* Loop counter. */ - int numFiles; /* Number of files in PK3. */ - int status; /* Error indicator. */ - fsPackFile_t *files; /* List of files in PK3. */ - fsPack_t *pack; /* PK3 file. */ - unzFile *handle; /* Zip file handle. */ - unz_file_info info; /* Zip file info. */ - unz_global_info global; /* Zip file global info. */ + char fileName[MAX_QPATH]; /* File name. */ + int i = 0; /* Loop counter. */ + int numFiles; /* Number of files in PK3. */ + int status; /* Error indicator. */ + fsPackFile_t *files; /* List of files in PK3. */ + fsPack_t *pack; /* PK3 file. */ + unzFile *handle; /* Zip file handle. */ + unz_file_info info; /* Zip file info. */ + unz_global_info global; /* Zip file global info. */ handle = unzOpen(packPath); + if (handle == NULL) return NULL; - if (unzGetGlobalInfo(handle, &global)!=UNZ_OK) { + if (unzGetGlobalInfo(handle, &global)!=UNZ_OK) + { unzClose(handle); Com_Error(ERR_FATAL, "FS_LoadPK3: '%s' is not a pack file", packPath); } + numFiles = global.number_entry; - if (numFiles > MAX_FILES_IN_PACK || numFiles == 0) { + if (numFiles > MAX_FILES_IN_PACK || numFiles == 0) + { unzClose(handle); Com_Error(ERR_FATAL, "FS_LoadPK3: '%s' has %i files", packPath, numFiles); } + files = Z_Malloc(numFiles * sizeof(fsPackFile_t)); /* Parse the directory. */ status = unzGoToFirstFile(handle); - while (status == UNZ_OK) { + + while (status == UNZ_OK) + { fileName[0] = '\0'; unzGetCurrentFileInfo(handle, &info, fileName, MAX_QPATH, NULL, 0, NULL, 0); strncpy(files[i].name, fileName, sizeof(files[i].name)); - files[i].offset = -1; /* Not used in ZIP files */ + files[i].offset = -1; /* Not used in ZIP files */ files[i].size = info.uncompressed_size; i++; status = unzGoToNextFile(handle); @@ -991,21 +1125,21 @@ FS_LoadPK3(const char *packPath) void FS_AddGameDirectory(const char *dir) { - char **list; /* File list. */ - char path[MAX_OSPATH]; /* Path to PAK / PK3. */ - int i, j; /* Loop counters. */ - int nfiles; /* Number of files in list. */ - fsSearchPath_t *search; /* Search path. */ - fsPack_t *pack; /* PAK / PK3 file. */ + char **list; /* File list. */ + char path[MAX_OSPATH]; /* Path to PAK / PK3. */ + int i, j; /* Loop counters. */ + int nfiles; /* Number of files in list. */ + fsSearchPath_t *search; /* Search path. */ + fsPack_t *pack; /* PAK / PK3 file. */ pack = NULL; /* Set game directory. */ strncpy(fs_gamedir, dir, sizeof(fs_gamedir)); - + /* Create directory if it does not exist. */ Sys_Mkdir(fs_gamedir); - + /* Add the directory to the search path. */ search = Z_Malloc(sizeof(fsSearchPath_t)); strncpy(search->path, dir, sizeof(search->path)); @@ -1014,20 +1148,23 @@ FS_AddGameDirectory(const char *dir) /* Add numbered pack files in sequence. */ for (i = 0; i < sizeof(fs_packtypes) / sizeof(fs_packtypes[0]); i++) - for (j = 0; j < MAX_PAKS; j++) { + for (j = 0; j < MAX_PAKS; j++) + { Com_sprintf(path, sizeof(path), "%s/pak%d.%s", dir, j, fs_packtypes[i].suffix); - switch (fs_packtypes[i].format) { - case PAK: - pack = FS_LoadPAK(path); - break; - case PK3: - pack = FS_LoadPK3(path); - break; + switch (fs_packtypes[i].format) + { + case PAK: + pack = FS_LoadPAK(path); + break; + case PK3: + pack = FS_LoadPK3(path); + break; } if (pack == NULL) continue; + search = Z_Malloc(sizeof(fsSearchPath_t)); search->pack = pack; search->next = fs_searchPaths; @@ -1035,7 +1172,8 @@ FS_AddGameDirectory(const char *dir) } /* Add not numbered pack files. */ - for (i = 0; i < sizeof(fs_packtypes) / sizeof(fs_packtypes[0]); i++) { + for (i = 0; i < sizeof(fs_packtypes) / sizeof(fs_packtypes[0]); i++) + { Com_sprintf(path, sizeof(path), "%s/*.%s", dir, fs_packtypes[i].suffix); if ((list = FS_ListFiles(path, &nfiles, 0, SFF_SUBDIR)) == NULL) @@ -1043,22 +1181,25 @@ FS_AddGameDirectory(const char *dir) Com_sprintf(path, sizeof(path), "%s/pak*.%s", dir, fs_packtypes[i].suffix); - for (j = 0; j < nfiles - 1; j++) { + for (j = 0; j < nfiles - 1; j++) + { /* Skip numbered packs. */ if (glob_match(path, list[j])) continue; - switch (fs_packtypes[i].format) { - case PAK: - pack = FS_LoadPAK(list[j]); - break; - case PK3: - pack = FS_LoadPK3(list[j]); - break; + switch (fs_packtypes[i].format) + { + case PAK: + pack = FS_LoadPAK(list[j]); + break; + case PK3: + pack = FS_LoadPK3(list[j]); + break; } if (pack == NULL) continue; + search = Z_Malloc(sizeof(fsSearchPath_t)); search->pack = pack; search->next = fs_searchPaths; @@ -1077,6 +1218,7 @@ FS_AddHomeAsGameDirectory(char *dir) { char gdir[MAX_OSPATH]; char *homedir=getenv("HOME"); + if(homedir) { int len = snprintf(gdir,sizeof(gdir),"%s/.quake2/%s/", homedir, dir); @@ -1088,7 +1230,7 @@ FS_AddHomeAsGameDirectory(char *dir) strncpy(fs_gamedir,gdir,sizeof(fs_gamedir)-1); fs_gamedir[sizeof(fs_gamedir)-1] = 0; - + FS_AddGameDirectory (gdir); } } @@ -1099,14 +1241,16 @@ FS_AddHomeAsGameDirectory(char *dir) char * FS_NextPath(char *prevPath) { - char *prev; - fsSearchPath_t *search; + char *prev; + fsSearchPath_t *search; if (prevPath == NULL) return (fs_gamedir); prev = fs_gamedir; - for (search = fs_searchPaths; search; search = search->next) { + + for (search = fs_searchPaths; search; search = search->next) + { if (search->pack != NULL) continue; @@ -1122,19 +1266,23 @@ FS_NextPath(char *prevPath) void FS_Path_f(void) { - int i; - int totalFiles = 0; - fsSearchPath_t *search; - fsHandle_t *handle; - fsLink_t *link; + int i; + int totalFiles = 0; + fsSearchPath_t *search; + fsHandle_t *handle; + fsLink_t *link; Com_Printf("Current search path:\n"); - for (search = fs_searchPaths; search; search = search->next) { - if (search->pack != NULL) { + for (search = fs_searchPaths; search; search = search->next) + { + if (search->pack != NULL) + { Com_Printf("%s (%i files)\n", search->pack->name, search->pack->numFiles); totalFiles += search->pack->numFiles; - } else + } + + else Com_Printf("%s\n", search->path); } @@ -1159,25 +1307,34 @@ FS_Startup(void) fsPack_t *pack; if (strstr(fs_gamedirvar->string, "..") || - strstr(fs_gamedirvar->string, ".") || - strstr(fs_gamedirvar->string, "/") || - fs_gamedirvar->string[0] == '\0') { + strstr(fs_gamedirvar->string, ".") || + strstr(fs_gamedirvar->string, "/") || + fs_gamedirvar->string[0] == '\0') + { Com_Printf("Invalid game directory.\n"); Cvar_ForceSet("fs_game", BASEDIRNAME); } + /* Check for game override. */ - if (strcasecmp(fs_gamedirvar->string, fs_currentGame) != 0) { + if (strcasecmp(fs_gamedirvar->string, fs_currentGame) != 0) + { /* Free up any current game dir info. */ - while (fs_searchPaths != fs_baseSearchPaths) { - if (fs_searchPaths->pack != NULL) { + while (fs_searchPaths != fs_baseSearchPaths) + { + if (fs_searchPaths->pack != NULL) + { pack = fs_searchPaths->pack; + if (pack->pak != NULL) fclose(pack->pak); + if (pack->pk3 != NULL) unzClose(pack->pk3); + Z_Free(pack->files); Z_Free(pack); } + next = fs_searchPaths->next; Z_Free(fs_searchPaths); fs_searchPaths = next; @@ -1186,16 +1343,18 @@ FS_Startup(void) /* Close open files for game dir. */ for (i = 0; i < MAX_HANDLES; i++) if (strstr(fs_handles[i].name, fs_currentGame) && - (fs_handles[i].file != NULL || fs_handles[i].zip != NULL)) + (fs_handles[i].file != NULL || fs_handles[i].zip != NULL)) FS_FCloseFile(i); /* Don't add baseq2 again. */ if (Q_stricmp(fs_gamedirvar->string, BASEDIRNAME) == 0) strncpy(fs_gamedir, fs_basedir->string, sizeof(fs_gamedir)); + else /* Add the directories. */ FS_AddGameDirectory(va("%s/%s", fs_homepath->string, fs_gamedirvar->string)); } + strncpy(fs_currentGame, fs_gamedirvar->string, sizeof(fs_currentGame)); FS_Path_f(); @@ -1204,13 +1363,14 @@ FS_Startup(void) void FS_ExecAutoexec(void) { - char *dir; - char name[MAX_QPATH]; + char *dir; + char name[MAX_QPATH]; dir = (char *)Cvar_VariableString("gamedir"); if (dir[0] != '\0') Com_sprintf(name, sizeof(name), "%s/%s/autoexec.cfg", fs_basedir->string, dir); + else Com_sprintf(name, sizeof(name), "%s/%s/autoexec.cfg", fs_basedir->string, BASEDIRNAME); @@ -1229,20 +1389,27 @@ FS_SetGamedir(char *dir) int i; fsSearchPath_t *next; - if (strstr(dir, "..") || strstr(dir, "/")) { + if (strstr(dir, "..") || strstr(dir, "/")) + { Com_Printf("Gamedir should be a single filename, not a path.\n"); return; } + /* Free up any current game dir info. */ - while (fs_searchPaths != fs_baseSearchPaths) { - if (fs_searchPaths->pack) { + while (fs_searchPaths != fs_baseSearchPaths) + { + if (fs_searchPaths->pack) + { if (fs_searchPaths->pack->pak) fclose(fs_searchPaths->pack->pak); + if (fs_searchPaths->pack->pk3) unzClose(fs_searchPaths->pack->pk3); + Z_Free(fs_searchPaths->pack->files); Z_Free(fs_searchPaths->pack); } + next = fs_searchPaths->next; Z_Free(fs_searchPaths); fs_searchPaths = next; @@ -1251,7 +1418,7 @@ FS_SetGamedir(char *dir) /* Close open files for game dir. */ for (i = 0; i < MAX_HANDLES; i++) if (strstr(fs_handles[i].name, dir) && - (fs_handles[i].file != NULL || fs_handles[i].zip != NULL)) + (fs_handles[i].file != NULL || fs_handles[i].zip != NULL)) FS_FCloseFile(i); /* Flush all data, so it will be forced to reload. */ @@ -1260,13 +1427,19 @@ FS_SetGamedir(char *dir) Com_sprintf(fs_gamedir, sizeof(fs_gamedir), "%s/%s", fs_basedir->string, dir); - if (strcmp(dir, BASEDIRNAME) == 0 || (*dir == 0)) { + if (strcmp(dir, BASEDIRNAME) == 0 || (*dir == 0)) + { Cvar_FullSet("gamedir", "", CVAR_SERVERINFO | CVAR_NOSET); Cvar_FullSet("game", "", CVAR_LATCH | CVAR_SERVERINFO); - } else { + } + + else + { Cvar_FullSet("gamedir", dir, CVAR_SERVERINFO | CVAR_NOSET); + if (fs_cddir->string[0] == '\0') FS_AddGameDirectory(va("%s/%s", fs_cddir->string, dir)); + FS_AddGameDirectory(va("%s/%s", fs_basedir->string, dir)); FS_AddHomeAsGameDirectory(dir); } @@ -1278,27 +1451,36 @@ FS_SetGamedir(char *dir) void FS_Link_f(void) { - fsLink_t *l, **prev; + fsLink_t *l, **prev; - if (Cmd_Argc() != 3) { + if (Cmd_Argc() != 3) + { Com_Printf("USAGE: link \n"); return; } + /* See if the link already exists. */ prev = &fs_links; - for (l = fs_links; l != NULL; l = l->next) { - if (strcmp(l->from, Cmd_Argv(1)) == 0) { + + for (l = fs_links; l != NULL; l = l->next) + { + if (strcmp(l->from, Cmd_Argv(1)) == 0) + { Z_Free(l->to); - if (strlen(Cmd_Argv(2)) == 0) { + + if (strlen(Cmd_Argv(2)) == 0) + { /* Delete it. */ *prev = l->next; Z_Free(l->from); Z_Free(l); return; } + l->to = CopyString(Cmd_Argv(2)); return; } + prev = &l->next; } @@ -1317,9 +1499,9 @@ FS_Link_f(void) char ** FS_ListFiles(char *findname, int *numfiles, unsigned musthave, unsigned canthave) { - char **list; /* List of files. */ - char *s; /* Next file in list. */ - int nfiles; /* Number of files in list. */ + char **list; /* List of files. */ + char *s; /* Next file in list. */ + int nfiles; /* Number of files in list. */ /* Initialize variables. */ list = NULL; @@ -1327,18 +1509,22 @@ FS_ListFiles(char *findname, int *numfiles, unsigned musthave, unsigned canthave /* Count the number of matches. */ s = Sys_FindFirst(findname, musthave, canthave); - while (s != NULL) { + + while (s != NULL) + { if (s[strlen(s) - 1] != '.') nfiles++; + s = Sys_FindNext(musthave, canthave); } + Sys_FindClose(); /* Check if there are matches. */ if (nfiles == 0) return (NULL); - nfiles++; /* Add space for a guard. */ + nfiles++; /* Add space for a guard. */ *numfiles = nfiles; /* Allocate the list. */ @@ -1347,13 +1533,18 @@ FS_ListFiles(char *findname, int *numfiles, unsigned musthave, unsigned canthave /* Fill the list. */ s = Sys_FindFirst(findname, musthave, canthave); nfiles = 0; - while (s) { - if (s[strlen(s) - 1] != '.') { + + while (s) + { + if (s[strlen(s) - 1] != '.') + { list[nfiles] = strdup(s); nfiles++; } + s = Sys_FindNext(musthave, canthave); } + Sys_FindClose(); return (list); @@ -1367,7 +1558,7 @@ FS_ListFiles(char *findname, int *numfiles, unsigned musthave, unsigned canthave */ qboolean ComparePackFiles(const char *findname, const char *name, - unsigned musthave, unsigned canthave, char *output, int size) + unsigned musthave, unsigned canthave, char *output, int size) { qboolean retval; char *ptr; @@ -1378,18 +1569,22 @@ ComparePackFiles(const char *findname, const char *name, if ((canthave & SFF_SUBDIR) && name[strlen(name)-1] == '/') return (false); - if (musthave & SFF_SUBDIR) { + if (musthave & SFF_SUBDIR) + { if ((ptr = strrchr(buffer, '/')) != NULL) *ptr = '\0'; + else return (false); } - if ((musthave & SFF_HIDDEN) || (canthave & SFF_HIDDEN)) { + if ((musthave & SFF_HIDDEN) || (canthave & SFF_HIDDEN)) + { if ((ptr = strrchr(buffer, '/')) == NULL) ptr = buffer; + if (((musthave & SFF_HIDDEN) && ptr[1] != '.') || - ((canthave & SFF_HIDDEN) && ptr[1] == '.')) + ((canthave & SFF_HIDDEN) && ptr[1] == '.')) return (false); } @@ -1412,55 +1607,67 @@ ComparePackFiles(const char *findname, const char *name, char ** FS_ListFiles2(char *findname, int *numfiles, unsigned musthave, unsigned canthave) { - fsSearchPath_t *search; /* Search path. */ - int i, j; /* Loop counters. */ - int nfiles; /* Number of files found. */ - int tmpnfiles; /* Temp number of files. */ - char **tmplist; /* Temporary list of files. */ - char **list; /* List of files found. */ - char path[MAX_OSPATH]; /* Temporary path. */ + fsSearchPath_t *search; /* Search path. */ + int i, j; /* Loop counters. */ + int nfiles; /* Number of files found. */ + int tmpnfiles; /* Temp number of files. */ + char **tmplist; /* Temporary list of files. */ + char **list; /* List of files found. */ + char path[MAX_OSPATH]; /* Temporary path. */ nfiles = 0; list = malloc(sizeof(char *)); - for (search = fs_searchPaths; search != NULL; search = search->next) { - if (search->pack != NULL) { + for (search = fs_searchPaths; search != NULL; search = search->next) + { + if (search->pack != NULL) + { if (canthave & SFF_INPACK) continue; for (i = 0, j = 0; i < search->pack->numFiles; i++) if (ComparePackFiles(findname, - search->pack->files[i].name, - musthave, canthave, NULL, 0)) + search->pack->files[i].name, + musthave, canthave, NULL, 0)) j++; + if (j == 0) continue; + nfiles += j; list = realloc(list, nfiles * sizeof(char *)); + for (i = 0, j = nfiles - j; - i < search->pack->numFiles; - i++) + i < search->pack->numFiles; + i++) if (ComparePackFiles(findname, - search->pack->files[i].name, - musthave, canthave, path, sizeof(path))) + search->pack->files[i].name, + musthave, canthave, path, sizeof(path))) list[j++] = strdup(path); - } else if (search->path != NULL) { + } + + else if (search->path != NULL) + { if (musthave & SFF_INPACK) continue; Com_sprintf(path, sizeof(path), "%s/%s", - search->path, findname); + search->path, findname); tmplist = FS_ListFiles(path, &tmpnfiles, musthave, - canthave); - if (tmplist != NULL) { + canthave); + + if (tmplist != NULL) + { tmpnfiles--; nfiles += tmpnfiles; list = realloc(list, nfiles * sizeof(char *)); + for (i = 0, j = nfiles - tmpnfiles; - i < tmpnfiles; - i++, j++) + i < tmpnfiles; + i++, j++) list[j] = strdup(tmplist[i] + - strlen(search->path) + 1); + strlen(search->path) + 1); + FS_FreeList(tmplist, tmpnfiles + 1); } } @@ -1468,34 +1675,45 @@ FS_ListFiles2(char *findname, int *numfiles, unsigned musthave, unsigned canthav /* Delete duplicates. */ tmpnfiles = 0; - for (i = 0; i < nfiles; i++) { + + for (i = 0; i < nfiles; i++) + { if (list[i] == NULL) continue; + for (j = i + 1; j < nfiles; j++) if (list[j] != NULL && - strcmp(list[i], list[j]) == 0) { + strcmp(list[i], list[j]) == 0) + { free(list[j]); list[j] = NULL; tmpnfiles++; } } - if (tmpnfiles > 0) { + if (tmpnfiles > 0) + { nfiles -= tmpnfiles; tmplist = malloc(nfiles * sizeof(char *)); + for (i = 0, j = 0; i < nfiles + tmpnfiles; i++) if (list[i] != NULL) tmplist[j++] = list[i]; + free(list); list = tmplist; } /* Add a guard. */ - if (nfiles > 0) { + if (nfiles > 0) + { nfiles++; list = realloc(list, nfiles * sizeof(char *)); list[nfiles - 1] = NULL; - } else { + } + + else + { free(list); list = NULL; } @@ -1525,32 +1743,38 @@ FS_FreeList(char **list, int nfiles) void FS_Dir_f(void) { - char **dirnames; /* File list. */ - char findname[1024]; /* File search path and pattern. */ - char *path = NULL; /* Search path. */ - char wildcard[1024] = "*.*"; /* File pattern. */ - int i; /* Loop counter. */ - int ndirs; /* Number of files in list. */ + char **dirnames; /* File list. */ + char findname[1024]; /* File search path and pattern. */ + char *path = NULL; /* Search path. */ + char wildcard[1024] = "*.*"; /* File pattern. */ + int i; /* Loop counter. */ + int ndirs; /* Number of files in list. */ /* Check for pattern in arguments. */ if (Cmd_Argc() != 1) strncpy(wildcard, Cmd_Argv(1), sizeof(wildcard)); /* Scan search paths and list files. */ - while ((path = FS_NextPath(path)) != NULL) { + while ((path = FS_NextPath(path)) != NULL) + { Com_sprintf(findname, sizeof(findname), "%s/%s", path, wildcard); Com_Printf("Directory of '%s'.\n", findname); Com_Printf("----\n"); - if ((dirnames = FS_ListFiles(findname, &ndirs, 0, 0)) != 0) { - for (i = 0; i < ndirs - 1; i++) { + if ((dirnames = FS_ListFiles(findname, &ndirs, 0, 0)) != 0) + { + for (i = 0; i < ndirs - 1; i++) + { if (strrchr(dirnames[i], '/')) Com_Printf("%s\n", strrchr(dirnames[i], '/') + 1); + else Com_Printf("%s\n", dirnames[i]); } + FS_FreeList(dirnames, ndirs); } + Com_Printf("\n"); } } @@ -1573,6 +1797,7 @@ FS_InitFilesystem(void) * allow the game to run from outside the data tree. */ fs_cddir = Cvar_Get("cddir", "", CVAR_NOSET); + if (fs_cddir->string[0] != '\0') FS_AddGameDirectory(va("%s/" BASEDIRNAME, fs_cddir->string)); @@ -1606,10 +1831,10 @@ FS_InitFilesystem(void) void FS_Shutdown(void) { - int i; - fsHandle_t *handle; - fsSearchPath_t *next; - fsPack_t *pack; + int i; + fsHandle_t *handle; + fsSearchPath_t *next; + fsPack_t *pack; /* Unregister commands. */ Cmd_RemoveCommand("dir"); @@ -1617,29 +1842,37 @@ FS_Shutdown(void) Cmd_RemoveCommand("path"); /* Close all files. */ - for (i = 0, handle = fs_handles; i < MAX_HANDLES; i++, handle++) { + for (i = 0, handle = fs_handles; i < MAX_HANDLES; i++, handle++) + { if (handle->file != NULL) fclose(handle->file); - if (handle->zip != NULL) { + + if (handle->zip != NULL) + { unzCloseCurrentFile(handle->zip); unzClose(handle->zip); } } /* Free the search paths. */ - while (fs_searchPaths != NULL) { - if (fs_searchPaths->pack != NULL) { + while (fs_searchPaths != NULL) + { + if (fs_searchPaths->pack != NULL) + { pack = fs_searchPaths->pack; + if (pack->pak != NULL) fclose(pack->pak); + if (pack->pk3 != NULL) unzClose(pack->pk3); + Z_Free(pack->files); Z_Free(pack); } + next = fs_searchPaths->next; Z_Free(fs_searchPaths); fs_searchPaths = next; } } -