📄 common.c
字号:
return c;
}
int MSG_ReadLong (void)
{
int c;
if (msg_readcount+4 > net_message.cursize)
{
msg_badread = true;
return -1;
}
c = net_message.data[msg_readcount]
+ (net_message.data[msg_readcount+1]<<8)
+ (net_message.data[msg_readcount+2]<<16)
+ (net_message.data[msg_readcount+3]<<24);
msg_readcount += 4;
return c;
}
float MSG_ReadFloat (void)
{
union
{
byte b[4];
float f;
int l;
} dat;
dat.b[0] = net_message.data[msg_readcount];
dat.b[1] = net_message.data[msg_readcount+1];
dat.b[2] = net_message.data[msg_readcount+2];
dat.b[3] = net_message.data[msg_readcount+3];
msg_readcount += 4;
dat.l = LittleLong (dat.l);
return dat.f;
}
char *MSG_ReadString (void)
{
static char string[2048];
int l,c;
l = 0;
do
{
c = MSG_ReadChar ();
if (c == -1 || c == 0)
break;
string[l] = c;
l++;
} while (l < sizeof(string)-1);
string[l] = 0;
return string;
}
float MSG_ReadCoord (void)
{
return MSG_ReadShort() * (1.0/8);
}
float MSG_ReadAngle (void)
{
return MSG_ReadChar() * (360.0/256);
}
//===========================================================================
void SZ_Alloc (sizebuf_t *buf, int startsize)
{
if (startsize < 256)
startsize = 256;
buf->data = Hunk_AllocName (startsize, "sizebuf");
buf->maxsize = startsize;
buf->cursize = 0;
}
void SZ_Free (sizebuf_t *buf)
{
// Z_Free (mainzone, buf->data); // 2001-09-20 Enhanced zone handling by Maddes
// buf->data = NULL;
// buf->maxsize = 0;
buf->cursize = 0;
}
void SZ_Clear (sizebuf_t *buf)
{
buf->cursize = 0;
}
void *SZ_GetSpace (sizebuf_t *buf, int length)
{
void *data;
if (buf->cursize + length > buf->maxsize)
{
if (!buf->allowoverflow)
Sys_Error ("SZ_GetSpace: overflow without allowoverflow set");
if (length > buf->maxsize)
Sys_Error ("SZ_GetSpace: %i is > full buffer size", length);
buf->overflowed = true;
Con_Printf ("SZ_GetSpace: overflow");
SZ_Clear (buf);
}
data = buf->data + buf->cursize;
buf->cursize += length;
return data;
}
void SZ_Write (sizebuf_t *buf, void *data, int length)
{
Q_memcpy (SZ_GetSpace(buf,length),data,length);
}
void SZ_Print (sizebuf_t *buf, char *data)
{
int len;
len = strlen(data)+1;
// byte * cast to keep VC++ happy
if (buf->data[buf->cursize-1])
Q_memcpy ((byte *)SZ_GetSpace(buf, len),data,len); // no trailing 0
else
Q_memcpy ((byte *)SZ_GetSpace(buf, len-1)-1,data,len); // write over trailing 0
}
//============================================================================
/*
============
COM_SkipPath
============
*/
char *COM_SkipPath (char *pathname)
{
char *last;
last = pathname;
while (*pathname)
{
if (*pathname=='/')
last = pathname+1;
pathname++;
}
return last;
}
/*
============
COM_StripExtension
============
*/
void COM_StripExtension (char *in, char *out)
{
while (*in && *in != '.')
*out++ = *in++;
*out = 0;
}
/*
============
COM_FileExtension
============
*/
char *COM_FileExtension (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 (char *in, char *out)
{
char *s, *s2;
s = in + strlen(in) - 1;
while (s != in && *s != '.')
s--;
for (s2 = s ; *s2 && *s2 != '/' ; s2--)
;
if (s-s2 < 2)
strcpy (out,"?model?");
else
{
s--;
strncpy (out,s2+1, s-s2);
out[s-s2] = 0;
}
}
/*
==================
COM_DefaultExtension
==================
*/
void COM_DefaultExtension (char *path, char *extension)
{
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_Parse
Parse a token out of a string
==============
*/
char *COM_Parse (char *data)
{
int c;
int len;
len = 0;
com_token[0] = 0;
if (!data)
return NULL;
// skip whitespace
skipwhite:
while ( (c = *data) <= ' ')
{
if (c == 0)
return NULL; // end of file;
data++;
}
// skip // comments
if (c=='/' && data[1] == '/')
{
while (*data && *data != '\n')
data++;
goto skipwhite;
}
// handle quoted strings specially
if (c == '\"')
{
data++;
while (1)
{
c = *data++;
if (c=='\"' || !c)
{
com_token[len] = 0;
return data;
}
com_token[len] = c;
len++;
}
}
// parse single characters
if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
{
com_token[len] = c;
len++;
com_token[len] = 0;
return data+1;
}
// parse a regular word
do
{
com_token[len] = c;
data++;
len++;
c = *data;
if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
break;
} while (c>32);
com_token[len] = 0;
return data;
}
/*
================
COM_CheckParm
Returns the position (1 to argc-1) in the program's argument list
where the given parameter apears, or 0 if not present
================
*/
int COM_CheckParm (char *parm)
{
int i;
for (i=1 ; i<com_argc ; i++)
{
if (!com_argv[i])
continue; // NEXTSTEP sometimes clears appkit vars.
if (!strcmp (parm,com_argv[i]))
return i;
}
return 0;
}
// 1999-12-23 Multiple "-data" parameters by Maddes start
/*
================
COM_CheckParmOffset
Returns the position (1 to argc-1) in the program's argument list
where the given parameter apears, or 0 if not present
================
*/
int COM_CheckParmOffset (char *parm, int offset)
{
int i;
for (i=offset ; i<com_argc ; i++)
{
if (!com_argv[i])
continue; // NEXTSTEP sometimes clears appkit vars.
if (!strcmp (parm,com_argv[i]))
return i;
}
return 0;
}
// 1999-12-23 Multiple "-data" parameters by Maddes end
/*
================
COM_CheckRegistered
Looks for the pop.txt file and verifies it.
Sets the "registered" cvar.
Immediately exits out if an alternate game was attempted to be started without
being registered.
================
*/
void COM_CheckRegistered (void)
{
int h;
unsigned short check[128];
int i;
COM_OpenFile("gfx/pop.lmp", &h, NULL); // 2001-09-12 Returning from which searchpath a file was loaded by Maddes
static_registered = 0;
if (h == -1)
{
#if WINDED
Sys_Error ("This dedicated server requires a full registered copy of Quake");
#endif
Con_Printf ("Playing shareware version.\n");
if (com_modified)
Sys_Error ("You must have the registered version to use modified games");
return;
}
Sys_FileRead (h, check, sizeof(check));
COM_CloseFile (h);
for (i=0 ; i<128 ; i++)
if (pop[i] != (unsigned short)BigShort (check[i]))
Sys_Error ("Corrupted data file.");
Cvar_Set (cmdline, com_cmdline);
Cvar_Set (registered, "1");
static_registered = 1;
Con_Printf ("Playing registered version.\n");
}
void COM_Path_f (void);
/*
================
COM_InitArgv
================
*/
void COM_InitArgv (int argc, char **argv)
{
qboolean safe;
int i, j, n;
// reconstitute the command line for the cmdline externally visible cvar
n = 0;
for (j=0 ; (j<MAX_NUM_ARGVS) && (j< argc) ; j++)
{
i = 0;
while ((n < (CMDLINE_LENGTH - 1)) && argv[j][i])
{
com_cmdline[n++] = argv[j][i++];
}
if (n < (CMDLINE_LENGTH - 1))
com_cmdline[n++] = ' ';
else
break;
}
com_cmdline[n] = 0;
safe = false;
for (com_argc=0 ; (com_argc<MAX_NUM_ARGVS) && (com_argc < argc) ;
com_argc++)
{
largv[com_argc] = argv[com_argc];
if (!strcmp ("-safe", argv[com_argc]))
safe = true;
}
if (safe)
{
// force all the safe-mode switches. Note that we reserved extra space in
// case we need to add these, so we don't need an overflow check
for (i=0 ; i<NUM_SAFE_ARGVS ; i++)
{
largv[com_argc] = safeargvs[i];
com_argc++;
}
}
largv[com_argc] = argvdummy;
com_argv = largv;
if (COM_CheckParm ("-rogue"))
{
rogue = true;
standard_quake = false;
}
if (COM_CheckParm ("-hipnotic"))
{
hipnotic = true;
standard_quake = false;
}
// 1999-10-29 +USE fix by Maddes start
if (COM_CheckParm ("-nouse"))
{
nouse = true;
}
// 1999-10-29 +USE fix by Maddes end
}
// 2001-09-18 New cvar system by Maddes (Init) start
/*
================
COM_Init_Cvars
================
*/
void COM_Init_Cvars (void)
{
registered = Cvar_Get ("registered", "0", CVAR_ORIGINAL);
cmdline = Cvar_Get ("cmdline", "0", CVAR_NOTIFY|CVAR_SERVERINFO|CVAR_ORIGINAL);
}
// 2001-09-18 New cvar system by Maddes (Init) end
/*
================
COM_Init
================
*/
void COM_Init (char *basedir)
{
byte swaptest[2] = {1,0};
// set the byte swapping variables in a portable manner
if ( *(short *)swaptest == 1)
{
bigendien = false;
BigShort = ShortSwap;
LittleShort = ShortNoSwap;
BigLong = LongSwap;
LittleLong = LongNoSwap;
BigFloat = FloatSwap;
LittleFloat = FloatNoSwap;
}
else
{
bigendien = true;
BigShort = ShortNoSwap;
LittleShort = ShortSwap;
BigLong = LongNoSwap;
LittleLong = LongSwap;
BigFloat = FloatNoSwap;
LittleFloat = FloatSwap;
}
// 2001-09-18 New cvar system by Maddes (Init) start
/*
registered = Cvar_Get ("registered", "0", CVAR_ORIGINAL);
cmdline = Cvar_Get ("cmdline", "0", CVAR_NOTIFY|CVAR_SERVERINFO|CVAR_ORIGINAL);
*/
// 2001-09-18 New cvar system by Maddes (Init) end
Cmd_AddCommand ("path", COM_Path_f);
COM_InitFilesystem ();
COM_CheckRegistered ();
}
/*
============
va
does a varargs printf into a temp buffer, so I don't need to have
varargs versions of all text functions.
FIXME: make this buffer size safe someday
============
*/
char *va(char *format, ...)
{
va_list argptr;
static char string[1024];
va_start (argptr, format);
vsprintf (string, format,argptr);
va_end (argptr);
return string;
}
/// just for debugging
int memsearch (byte *start, int count, int search)
{
int i;
for (i=0 ; i<count ; i++)
if (start[i] == search)
return i;
return -1;
}
/*
=============================================================================
QUAKE FILESYSTEM
=============================================================================
*/
int com_filesize;
// 2001-09-12 Returning from which searchpath a file was loaded by Maddes start
/*
//
// 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;
} 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;
#define MAX_FILES_IN_PACK 2048
*/
// 2001-09-12 Returning from which searchpath a file was loaded by Maddes end
char com_cachedir[MAX_OSPATH];
char com_gamedir[MAX_OSPATH];
// 2001-09-12 Returning from which searchpath a file was loaded by Maddes start
/*
typedef struct searchpath_s
{
char filename[MAX_OSPATH];
pack_t *pack; // only one of filename / pack will be used
struct searchpath_s *next;
} searchpath_t;
*/
// 2001-09-12 Returning from which searchpath a file was loaded by Maddes end
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);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -