X-Git-Url: http://de.git.xonotic.org/?a=blobdiff_plain;f=common.c;h=c6a16ee05e8df11eb81ca0a5741917b6c2481153;hb=eef4de5c2e8ee6dbc76c96e7f42ff8ac0c506889;hp=3859d20bde55cc0205b71f02438add599dd4df22;hpb=ea7c24e1fb41f3b1df984ac0eed6881c9fde16f5;p=xonotic%2Fdarkplaces.git diff --git a/common.c b/common.c index 3859d20b..c6a16ee0 100644 --- a/common.c +++ b/common.c @@ -29,14 +29,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "quakedef.h" -#define NUM_SAFE_ARGVS 7 - -static char *largv[MAX_NUM_ARGVS + NUM_SAFE_ARGVS + 1]; -static char *argvdummy = " "; - -static char *safeargvs[NUM_SAFE_ARGVS] = - {"-stdvid", "-nolan", "-nosound", "-nocdaudio", "-nojoy", "-nomouse", "-window"}; - cvar_t registered = {0, "registered","0"}; cvar_t cmdline = {0, "cmdline","0"}; @@ -44,13 +36,12 @@ mempool_t *pak_mempool; qboolean com_modified; // set true if using non-id files -qboolean msg_suppress_1 = 0; - void COM_InitFilesystem (void); char com_token[1024]; +char com_basedir[MAX_OSPATH]; int com_argc; -char **com_argv; +const char **com_argv; // LordHavoc: made commandline 1024 characters instead of 256 #define CMDLINE_LENGTH 1024 @@ -58,6 +49,8 @@ char com_cmdline[CMDLINE_LENGTH]; int gamemode; char *gamename; +char *gamedirname; +char com_modname[MAX_OSPATH]; /* @@ -91,7 +84,7 @@ The file "parms.txt" will be read out of the game directory and appended to the ============================================================================ */ -int Q_strncasecmp (char *s1, char *s2, int n) +int Q_strncasecmp (const char *s1, const char *s2, int n) { int c1, c2; @@ -119,7 +112,7 @@ int Q_strncasecmp (char *s1, char *s2, int n) return -1; } -int Q_strcasecmp (char *s1, char *s2) +int Q_strcasecmp (const char *s1, const char *s2) { return Q_strncasecmp (s1, s2, 99999); } @@ -265,7 +258,7 @@ void MSG_WriteFloat (sizebuf_t *sb, float f) SZ_Write (sb, &dat.l, 4); } -void MSG_WriteString (sizebuf_t *sb, char *s) +void MSG_WriteString (sizebuf_t *sb, const char *s) { if (!s) SZ_Write (sb, "", 1); @@ -302,8 +295,8 @@ void MSG_WriteAngle (sizebuf_t *sb, float f) // // reading functions // -int msg_readcount; -qboolean msg_badread; +int msg_readcount; +qboolean msg_badread; void MSG_BeginReading (void) { @@ -313,7 +306,7 @@ void MSG_BeginReading (void) int MSG_ReadShort (void) { - int c; + int c; if (msg_readcount+2 > net_message.cursize) { @@ -331,7 +324,7 @@ int MSG_ReadShort (void) int MSG_ReadLong (void) { - int c; + int c; if (msg_readcount+4 > net_message.cursize) { @@ -353,9 +346,9 @@ float MSG_ReadFloat (void) { union { - qbyte b[4]; - float f; - int l; + qbyte b[4]; + float f; + int l; } dat; dat.b[0] = net_message.data[msg_readcount]; @@ -371,8 +364,8 @@ float MSG_ReadFloat (void) char *MSG_ReadString (void) { - static char string[2048]; - int l,c; + static char string[2048]; + int l,c; l = 0; do @@ -382,7 +375,7 @@ char *MSG_ReadString (void) break; string[l] = c; l++; - } while (l < sizeof(string)-1); + } while (l < (int)sizeof(string)-1); string[l] = 0; @@ -409,7 +402,7 @@ float MSG_ReadCoord (void) //=========================================================================== -void SZ_Alloc (sizebuf_t *buf, int startsize, char *name) +void SZ_Alloc (sizebuf_t *buf, int startsize, const char *name) { if (startsize < 256) startsize = 256; @@ -435,36 +428,35 @@ void SZ_Clear (sizebuf_t *buf) void *SZ_GetSpace (sizebuf_t *buf, int length) { - void *data; + void *data; if (buf->cursize + length > buf->maxsize) { if (!buf->allowoverflow) - Host_Error ("SZ_GetSpace: overflow without allowoverflow set"); + Host_Error ("SZ_GetSpace: overflow without allowoverflow set\n"); if (length > buf->maxsize) - Host_Error ("SZ_GetSpace: %i is > full buffer size", length); + Host_Error ("SZ_GetSpace: %i is > full buffer size\n", length); buf->overflowed = true; - Con_Printf ("SZ_GetSpace: overflow"); + Con_Printf ("SZ_GetSpace: overflow\n"); SZ_Clear (buf); } data = buf->data + buf->cursize; buf->cursize += length; - + return data; } -void SZ_Write (sizebuf_t *buf, void *data, int length) +void SZ_Write (sizebuf_t *buf, const 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) +void SZ_Print (sizebuf_t *buf, const char *data) { - int len; - + int len; len = strlen(data)+1; // byte * cast to keep VC++ happy @@ -474,36 +466,71 @@ void SZ_Print (sizebuf_t *buf, char *data) memcpy ((qbyte *)SZ_GetSpace(buf, len-1)-1,data,len); // write over trailing 0 } - -//============================================================================ - - -/* -============ -COM_SkipPath -============ -*/ -char *COM_SkipPath (char *pathname) +static char *hexchar = "0123456789ABCDEF"; +void Com_HexDumpToConsole(const qbyte *data, int size) { - char *last; - - last = pathname; - while (*pathname) + int i; + char text[1024]; + char *cur, *flushpointer; + cur = text; + flushpointer = text + 512; + for (i = 0;i < size;i++) { - if (*pathname=='/') - last = pathname+1; - pathname++; + if ((i & 15) == 0) + { + *cur++ = hexchar[(i >> 12) & 15]; + *cur++ = hexchar[(i >> 8) & 15]; + *cur++ = hexchar[(i >> 4) & 15]; + *cur++ = hexchar[(i >> 0) & 15]; + *cur++ = ':'; + *cur++ = ' '; + } + else if ((i & 15) == 15) + *cur++ = '\n'; + else + *cur++ = ' '; + if (i & 1) + { + *cur++ = hexchar[(data[i] >> 4) & 15] | 0x80; + *cur++ = hexchar[(data[i] >> 0) & 15] | 0x80; + } + else + { + *cur++ = hexchar[(data[i] >> 4) & 15]; + *cur++ = hexchar[(data[i] >> 0) & 15]; + } + if (cur >= flushpointer) + { + *cur++ = 0; + Con_Printf("%s", text); + cur = text; + } + } + if ((i & 15) != 0) + *cur++ = '\n'; + if (cur > text) + { + *cur++ = 0; + Con_Printf("%s", text); } - return last; } +void SZ_HexDumpToConsole(const sizebuf_t *buf) +{ + Com_HexDumpToConsole(buf->data, buf->cursize); +} + + +//============================================================================ + + /* ============ COM_StripExtension ============ */ // LordHavoc: replacement for severely broken COM_StripExtension that was used in original quake. -void COM_StripExtension (char *in, char *out) +void COM_StripExtension (const char *in, char *out) { char *last = NULL; while (*in) @@ -516,6 +543,8 @@ void COM_StripExtension (char *in, char *out) } if (last) *last = 0; + else + *out = 0; } /* @@ -523,10 +552,10 @@ void COM_StripExtension (char *in, char *out) COM_FileExtension ============ */ -char *COM_FileExtension (char *in) +char *COM_FileExtension (const char *in) { static char exten[8]; - int i; + int i; while (*in && *in != '.') in++; @@ -544,10 +573,9 @@ char *COM_FileExtension (char *in) COM_FileBase ============ */ -void COM_FileBase (char *in, char *out) +void COM_FileBase (const char *in, char *out) { - char *slash, *dot; - char *s; + const char *slash, *dot, *s; slash = in; dot = NULL; @@ -578,9 +606,9 @@ void COM_FileBase (char *in, char *out) COM_DefaultExtension ================== */ -void COM_DefaultExtension (char *path, char *extension) +void COM_DefaultExtension (char *path, const char *extension) { - char *src; + const char *src; // // if path doesn't have a .EXT, append extension // (extension should include the .) @@ -600,31 +628,39 @@ void COM_DefaultExtension (char *path, char *extension) /* ============== -COM_Parse +COM_ParseToken Parse a token out of a string ============== */ -char *COM_Parse (char *data) +int COM_ParseToken (const char **datapointer) { - int c; - int len; - + int c; + int len; + const char *data = *datapointer; + len = 0; com_token[0] = 0; - + if (!data) - return NULL; - + { + *datapointer = NULL; + return false; + } + // skip whitespace skipwhite: - while ( (c = *data) <= ' ') + while ((c = *data) <= ' ') { if (c == 0) - return NULL; // end of file; + { + // end of file + *datapointer = NULL; + return false; + } data++; } - + // skip // comments if (c=='/' && data[1] == '/') { @@ -632,7 +668,7 @@ skipwhite: data++; goto skipwhite; } - + // handle quoted strings specially if (c == '\"') @@ -644,7 +680,8 @@ skipwhite: if (c=='\"' || !c) { com_token[len] = 0; - return data; + *datapointer = data; + return true; } com_token[len] = c; len++; @@ -657,7 +694,8 @@ skipwhite: com_token[len] = c; len++; com_token[len] = 0; - return data+1; + *datapointer = data+1; + return true; } // parse a regular word @@ -667,12 +705,13 @@ skipwhite: data++; len++; c = *data; - if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':') + if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':') break; } while (c>32); - + com_token[len] = 0; - return data; + *datapointer = data; + return true; } @@ -684,10 +723,10 @@ 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 COM_CheckParm (const char *parm) { - int i; - + int i; + for (i=1 ; inext) @@ -1005,7 +1029,7 @@ LordHavoc: Previously only used for CopyFile, now also used for COM_WriteFile. */ void COM_CreatePath (char *path) { - char *ofs, save; + char *ofs, save; for (ofs = path+1 ; *ofs ; ofs++) { @@ -1028,15 +1052,16 @@ COM_WriteFile The filename will be prefixed by the current game directory ============ */ -qboolean COM_WriteFile (char *filename, void *data, int len) +qboolean COM_WriteFile (const char *filename, void *data, int len) { - int handle; - char name[MAX_OSPATH]; + int handle; + char name[MAX_OSPATH]; sprintf (name, "%s/%s", com_gamedir, filename); // LordHavoc: added this - COM_CreatePath (name); // create directories up to the file + // create directories up to the file + COM_CreatePath (name); handle = Sys_FileOpenWrite (name); if (handle == -1) @@ -1062,17 +1087,16 @@ needed. This is for the convenience of developers using ISDN from home. */ void COM_CopyFile (char *netpath, char *cachepath) { - int in, out; - int remaining, count; - char buf[4096]; + int in, out, remaining, count; + char buf[4096]; - remaining = Sys_FileOpenRead (netpath, &in); + remaining = Sys_FileOpenRead (netpath, &in); COM_CreatePath (cachepath); // create directories up to the cache file out = Sys_FileOpenWrite (cachepath); - + while (remaining) { - if (remaining < sizeof(buf)) + if (remaining < (int)sizeof(buf)) count = remaining; else count = sizeof(buf); @@ -1082,7 +1106,7 @@ void COM_CopyFile (char *netpath, char *cachepath) } Sys_FileClose (in); - Sys_FileClose (out); + Sys_FileClose (out); } /* @@ -1092,9 +1116,8 @@ COM_OpenRead */ QFile * COM_OpenRead (const char *path, int offs, int len, qboolean zip) { - int fd = open (path, O_RDONLY); - unsigned char id[2]; - unsigned char len_bytes[4]; + int fd = open (path, O_RDONLY); + unsigned char id[2], len_bytes[4]; if (fd == -1) { @@ -1142,26 +1165,24 @@ Finds the file in the search path. Sets com_filesize and one of handle or file =========== */ -int COM_FindFile (char *filename, QFile **file, qboolean quiet, qboolean zip) +int COM_FindFile (const char *filename, QFile **file, qboolean quiet, qboolean zip) { - searchpath_t *search; - char netpath[MAX_OSPATH]; + searchpath_t *search; + char netpath[MAX_OSPATH]; #if CACHEENABLE - char cachepath[MAX_OSPATH]; - int cachetime; + char cachepath[MAX_OSPATH]; + int cachetime; #endif - pack_t *pak; - int i; - int findtime; - char gzfilename[MAX_OSPATH]; - int filenamelen; + pack_t *pak; + int i, findtime, filenamelen; + char gzfilename[MAX_OSPATH]; filenamelen = strlen (filename); sprintf (gzfilename, "%s.gz", filename); if (!file) Sys_Error ("COM_FindFile: file not set"); - + // // search through the path, one element at a time // @@ -1186,17 +1207,17 @@ int COM_FindFile (char *filename, QFile **file, qboolean quiet, qboolean zip) } } else - { + { sprintf (netpath, "%s/%s",search->filename, filename); - + findtime = Sys_FileTime (netpath); if (findtime == -1) continue; - + #if CACHEENABLE // see if the file needs to be updated in the cache if (com_cachedir[0]) - { + { #if defined(_WIN32) if ((strlen(netpath) < 2) || (netpath[1] != ':')) sprintf (cachepath,"%s%s", com_cachedir, netpath); @@ -1207,24 +1228,22 @@ int COM_FindFile (char *filename, QFile **file, qboolean quiet, qboolean zip) #endif cachetime = Sys_FileTime (cachepath); - + if (cachetime < findtime) COM_CopyFile (netpath, cachepath); strcpy (netpath, cachepath); - } + } #endif - if (!quiet) Sys_Printf ("FindFile: %s\n",netpath); *file = COM_OpenRead (netpath, -1, -1, zip); return com_filesize; } - } - + if (!quiet) Sys_Printf ("FindFile: can't find %s\n", filename); - + *file = NULL; com_filesize = -1; return -1; @@ -1239,7 +1258,7 @@ If the requested file is inside a packfile, a new QFile * will be opened into the file. =========== */ -int COM_FOpenFile (char *filename, QFile **file, qboolean quiet, qboolean zip) +int COM_FOpenFile (const char *filename, QFile **file, qboolean quiet, qboolean zip) { return COM_FindFile (filename, file, quiet, zip); } @@ -1255,7 +1274,7 @@ Always appends a 0 byte. */ qbyte *loadbuf; int loadsize; -qbyte *COM_LoadFile (char *path, qboolean quiet) +qbyte *COM_LoadFile (const char *path, qboolean quiet) { QFile *h; qbyte *buf; @@ -1297,15 +1316,13 @@ Loads the header and directory, adding the files at the beginning of the list so they override previous pack files. ================= */ -pack_t *COM_LoadPackFile (char *packfile) +pack_t *COM_LoadPackFile (const char *packfile) { - dpackheader_t header; - int i; - int numpackfiles; - pack_t *pack; - int packhandle; + dpackheader_t header; + int i, numpackfiles, packhandle; + pack_t *pack; // LordHavoc: changed from stack array to temporary alloc, allowing huge pack directories - dpackfile_t *info; + dpackfile_t *info; if (Sys_FileOpenRead (packfile, &packhandle) == -1) return NULL; @@ -1381,7 +1398,7 @@ void COM_AddGameDirectory (char *dir) list = listdirectory(dir); for (current = list;current;current = current->next) { - if (matchpattern(current->text, "*.pak")) + if (matchpattern(current->text, "*.pak", true)) { sprintf (pakfile, "%s/%s", dir, current->text); pak = COM_LoadPackFile (pakfile); @@ -1406,93 +1423,43 @@ COM_InitFilesystem */ void COM_InitFilesystem (void) { - int i, j; - char basedir[MAX_OSPATH]; - searchpath_t *search; + int i; + searchpath_t *search; -// -// -basedir -// Overrides the system supplied base directory (under GAMENAME) -// + strcpy(com_basedir, "."); + + // -basedir + // Overrides the system supplied base directory (under GAMENAME) i = COM_CheckParm ("-basedir"); if (i && i < com_argc-1) - strcpy (basedir, com_argv[i+1]); - else - strcpy (basedir, host_parms.basedir); - - j = strlen (basedir); + strcpy (com_basedir, com_argv[i+1]); - if (j > 0) - { - if ((basedir[j-1] == '\\') || (basedir[j-1] == '/')) - basedir[j-1] = 0; - } - -#if CACHEENABLE -// -// -cachedir -// Overrides the system supplied cache directory (NULL or /qcache) -// -cachedir - will disable caching. -// - i = COM_CheckParm ("-cachedir"); - if (i && i < com_argc-1) - { - if (com_argv[i+1][0] == '-') - com_cachedir[0] = 0; - else - strcpy (com_cachedir, com_argv[i+1]); - } - else if (host_parms.cachedir) - strcpy (com_cachedir, host_parms.cachedir); - else - com_cachedir[0] = 0; -#endif + i = strlen (com_basedir); + if (i > 0 && (com_basedir[i-1] == '\\' || com_basedir[i-1] == '/')) + com_basedir[i-1] = 0; // start up with GAMENAME by default (id1) - COM_AddGameDirectory (va("%s/"GAMENAME, basedir) ); - - switch(gamemode) + strcpy(com_modname, GAMENAME); + COM_AddGameDirectory (va("%s/"GAMENAME, com_basedir)); + if (gamedirname[0]) { - case GAME_NORMAL: - break; - case GAME_HIPNOTIC: - COM_AddGameDirectory (va("%s/hipnotic", basedir) ); - break; - case GAME_ROGUE: - COM_AddGameDirectory (va("%s/rogue", basedir) ); - break; - case GAME_NEHAHRA: - COM_AddGameDirectory (va("%s/nehahra", basedir) ); - break; - case GAME_FIENDARENA: - COM_AddGameDirectory (va("%s/fiendarena", basedir) ); - break; - case GAME_ZYMOTIC: - COM_AddGameDirectory (va("%s/zymotic", basedir) ); - break; - case GAME_BLOODBATH: - COM_AddGameDirectory (va("%s/bb", basedir) ); - break; - default: - Sys_Error("COM_InitFilesystem: unknown gamemode %i\n", gamemode); - break; + com_modified = true; + strcpy(com_modname, gamedirname); + COM_AddGameDirectory (va("%s/%s", com_basedir, gamedirname)); } -// -// -game -// Adds basedir/gamedir as an override game -// + // -game + // Adds basedir/gamedir as an override game i = COM_CheckParm ("-game"); if (i && i < com_argc-1) { com_modified = true; - COM_AddGameDirectory (va("%s/%s", basedir, com_argv[i+1])); + strcpy(com_modname, com_argv[i+1]); + COM_AddGameDirectory (va("%s/%s", com_basedir, com_argv[i+1])); } -// -// -path [] ... -// Fully specifies the exact search path, overriding the generated one -// + // -path [] ... + // Fully specifies the exact search path, overriding the generated one i = COM_CheckParm ("-path"); if (i) { @@ -1518,13 +1485,12 @@ void COM_InitFilesystem (void) } } -int COM_FileExists(char *filename) +int COM_FileExists(const char *filename) { - searchpath_t *search; - char netpath[MAX_OSPATH]; - pack_t *pak; - int i; - int findtime; + searchpath_t *search; + char netpath[MAX_OSPATH]; + pack_t *pak; + int i, findtime; for (search = com_searchpaths;search;search = search->next) { @@ -1541,7 +1507,7 @@ int COM_FileExists(char *filename) findtime = Sys_FileTime (netpath); if (findtime != -1) return true; - } + } } return false; @@ -1551,7 +1517,7 @@ int COM_FileExists(char *filename) //====================================== // LordHavoc: added these because they are useful -void COM_ToLowerString(char *in, char *out) +void COM_ToLowerString(const char *in, char *out) { while (*in) { @@ -1562,7 +1528,7 @@ void COM_ToLowerString(char *in, char *out) } } -void COM_ToUpperString(char *in, char *out) +void COM_ToUpperString(const char *in, char *out) { while (*in) { @@ -1573,3 +1539,10 @@ void COM_ToUpperString(char *in, char *out) } } +int COM_StringBeginsWith(const char *s, const char *match) +{ + for (;*s && *match;s++, match++) + if (*s != *match) + return false; + return true; +}