]> icculus.org git repositories - divverent/darkplaces.git/blob - common.h
make some cvars static, to help the compiler a bit
[divverent/darkplaces.git] / common.h
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #ifndef COMMON_H
22 #define COMMON_H
23
24
25 /// MSVC has a different name for several standard functions
26 #ifdef WIN32
27 # define strcasecmp _stricmp
28 # define strncasecmp _strnicmp
29 #endif
30
31 // Create our own define for Mac OS X
32 #if defined(__APPLE__) && defined(__MACH__)
33 # define MACOSX
34 #endif
35
36 #ifdef SUNOS
37 #include <sys/file.h>           ///< Needed for FNDELAY
38 #endif
39
40 //============================================================================
41
42 typedef struct sizebuf_s
43 {
44         qboolean        allowoverflow;  ///< if false, do a Sys_Error
45         qboolean        overflowed;             ///< set to true if the buffer size failed
46         unsigned char           *data;
47         int                     maxsize;
48         int                     cursize;
49 } sizebuf_t;
50
51 void SZ_Clear (sizebuf_t *buf);
52 unsigned char *SZ_GetSpace (sizebuf_t *buf, int length);
53 void SZ_Write (sizebuf_t *buf, const unsigned char *data, int length);
54 void SZ_HexDumpToConsole(const sizebuf_t *buf);
55
56 void Com_HexDumpToConsole(const unsigned char *data, int size);
57
58 unsigned short CRC_Block(const unsigned char *data, size_t size);
59 unsigned short CRC_Block_CaseInsensitive(const unsigned char *data, size_t size); // for hash lookup functions that use strcasecmp for comparison
60
61 unsigned char COM_BlockSequenceCRCByteQW(unsigned char *base, int length, int sequence);
62
63 // these are actually md4sum (mdfour.c)
64 unsigned Com_BlockChecksum (void *buffer, int length);
65 void Com_BlockFullChecksum (void *buffer, int len, unsigned char *outbuf);
66
67
68 //============================================================================
69 //                                                      Endianess handling
70 //============================================================================
71
72 // check mem_bigendian if you need to know the system byte order
73
74 /*! \name Byte order functions.
75  * @{
76  */
77
78 // unaligned memory access crashes on some platform, so always read bytes...
79 #define BigShort(l) BuffBigShort((unsigned char *)&(l))
80 #define LittleShort(l) BuffLittleShort((unsigned char *)&(l))
81 #define BigLong(l) BuffBigLong((unsigned char *)&(l))
82 #define LittleLong(l) BuffLittleLong((unsigned char *)&(l))
83 #define BigFloat(l) BuffBigFloat((unsigned char *)&(l))
84 #define LittleFloat(l) BuffLittleFloat((unsigned char *)&(l))
85
86 /// Extract a big endian 32bit float from the given \p buffer.
87 float BuffBigFloat (const unsigned char *buffer);
88
89 /// Extract a big endian 32bit int from the given \p buffer.
90 int BuffBigLong (const unsigned char *buffer);
91
92 /// Extract a big endian 16bit short from the given \p buffer.
93 short BuffBigShort (const unsigned char *buffer);
94
95 /// Extract a little endian 32bit float from the given \p buffer.
96 float BuffLittleFloat (const unsigned char *buffer);
97
98 /// Extract a little endian 32bit int from the given \p buffer.
99 int BuffLittleLong (const unsigned char *buffer);
100
101 /// Extract a little endian 16bit short from the given \p buffer.
102 short BuffLittleShort (const unsigned char *buffer);
103
104 /// Encode a big endian 32bit int to the given \p buffer
105 void StoreBigLong (unsigned char *buffer, unsigned int i);
106
107 /// Encode a big endian 16bit int to the given \p buffer
108 void StoreBigShort (unsigned char *buffer, unsigned short i);
109
110 /// Encode a little endian 32bit int to the given \p buffer
111 void StoreLittleLong (unsigned char *buffer, unsigned int i);
112
113 /// Encode a little endian 16bit int to the given \p buffer
114 void StoreLittleShort (unsigned char *buffer, unsigned short i);
115 //@}
116
117 //============================================================================
118
119 // these versions are purely for internal use, never sent in network protocol
120 // (use Protocol_EnumForNumber and Protocol_NumberToEnum to convert)
121 typedef enum protocolversion_e
122 {
123         PROTOCOL_UNKNOWN,
124         PROTOCOL_DARKPLACES7, ///< added QuakeWorld-style movement protocol to allow more consistent prediction
125         PROTOCOL_DARKPLACES6, ///< various changes
126         PROTOCOL_DARKPLACES5, ///< uses EntityFrame5 entity snapshot encoder/decoder which is based on a Tribes networking article at http://www.garagegames.com/articles/networking1/
127         PROTOCOL_DARKPLACES4, ///< various changes
128         PROTOCOL_DARKPLACES3, ///< uses EntityFrame4 entity snapshot encoder/decoder which is broken, this attempted to do partial snapshot updates on a QuakeWorld-like protocol, but it is broken and impossible to fix
129         PROTOCOL_DARKPLACES2, ///< various changes
130         PROTOCOL_DARKPLACES1, ///< uses EntityFrame entity snapshot encoder/decoder which is a QuakeWorld-like entity snapshot delta compression method
131         PROTOCOL_QUAKEDP, ///< darkplaces extended quake protocol (used by TomazQuake and others), backwards compatible as long as no extended features are used
132         PROTOCOL_NEHAHRAMOVIE, ///< Nehahra movie protocol, a big nasty hack dating back to early days of the Quake Standards Group (but only ever used by neh_gl.exe), this is potentially backwards compatible with quake protocol as long as no extended features are used (but in actuality the neh_gl.exe which wrote this protocol ALWAYS wrote the extended information)
133         PROTOCOL_QUAKE, ///< quake (aka netquake/normalquake/nq) protocol
134         PROTOCOL_QUAKEWORLD, ///< quakeworld protocol
135         PROTOCOL_NEHAHRABJP, ///< same as QUAKEDP but with 16bit modelindex
136         PROTOCOL_NEHAHRABJP2, ///< same as NEHAHRABJP but with 16bit soundindex
137         PROTOCOL_NEHAHRABJP3, ///< same as NEHAHRABJP2 but with some changes
138 }
139 protocolversion_t;
140
141 /*! \name Message IO functions.
142  * Handles byte ordering and avoids alignment errors
143  * @{
144  */
145
146 void MSG_WriteChar (sizebuf_t *sb, int c);
147 void MSG_WriteByte (sizebuf_t *sb, int c);
148 void MSG_WriteShort (sizebuf_t *sb, int c);
149 void MSG_WriteLong (sizebuf_t *sb, int c);
150 void MSG_WriteFloat (sizebuf_t *sb, float f);
151 void MSG_WriteString (sizebuf_t *sb, const char *s);
152 void MSG_WriteUnterminatedString (sizebuf_t *sb, const char *s);
153 void MSG_WriteAngle8i (sizebuf_t *sb, float f);
154 void MSG_WriteAngle16i (sizebuf_t *sb, float f);
155 void MSG_WriteAngle32f (sizebuf_t *sb, float f);
156 void MSG_WriteCoord13i (sizebuf_t *sb, float f);
157 void MSG_WriteCoord16i (sizebuf_t *sb, float f);
158 void MSG_WriteCoord32f (sizebuf_t *sb, float f);
159 void MSG_WriteCoord (sizebuf_t *sb, float f, protocolversion_t protocol);
160 void MSG_WriteVector (sizebuf_t *sb, float *v, protocolversion_t protocol);
161 void MSG_WriteAngle (sizebuf_t *sb, float f, protocolversion_t protocol);
162
163 extern  int                     msg_readcount;
164 extern  qboolean        msg_badread;            // set if a read goes beyond end of message
165
166 void MSG_BeginReading (void);
167 int MSG_ReadLittleShort (void);
168 int MSG_ReadBigShort (void);
169 int MSG_ReadLittleLong (void);
170 int MSG_ReadBigLong (void);
171 float MSG_ReadLittleFloat (void);
172 float MSG_ReadBigFloat (void);
173 char *MSG_ReadString (void);
174 int MSG_ReadBytes (int numbytes, unsigned char *out);
175
176 #define MSG_ReadChar() (msg_readcount >= net_message.cursize ? (msg_badread = true, -1) : (signed char)net_message.data[msg_readcount++])
177 #define MSG_ReadByte() (msg_readcount >= net_message.cursize ? (msg_badread = true, -1) : (unsigned char)net_message.data[msg_readcount++])
178 #define MSG_ReadShort MSG_ReadLittleShort
179 #define MSG_ReadLong MSG_ReadLittleLong
180 #define MSG_ReadFloat MSG_ReadLittleFloat
181
182 float MSG_ReadAngle8i (void);
183 float MSG_ReadAngle16i (void);
184 float MSG_ReadAngle32f (void);
185 float MSG_ReadCoord13i (void);
186 float MSG_ReadCoord16i (void);
187 float MSG_ReadCoord32f (void);
188 float MSG_ReadCoord (protocolversion_t protocol);
189 void MSG_ReadVector (float *v, protocolversion_t protocol);
190 float MSG_ReadAngle (protocolversion_t protocol);
191 //@}
192 //============================================================================
193
194 typedef float (*COM_WordWidthFunc_t) (void *passthrough, const char *w, size_t *length, float maxWidth); // length is updated to the longest fitting string into maxWidth; if maxWidth < 0, all characters are used and length is used as is
195 typedef int (*COM_LineProcessorFunc) (void *passthrough, const char *line, size_t length, float width, qboolean isContination);
196 int COM_Wordwrap(const char *string, size_t length, float continuationSize, float maxWidth, COM_WordWidthFunc_t wordWidth, void *passthroughCW, COM_LineProcessorFunc processLine, void *passthroughPL);
197
198 extern char com_token[MAX_INPUTLINE];
199
200 int COM_ParseToken_Simple(const char **datapointer, qboolean returnnewline, qboolean parsebackslash);
201 int COM_ParseToken_QuakeC(const char **datapointer, qboolean returnnewline);
202 int COM_ParseToken_VM_Tokenize(const char **datapointer, qboolean returnnewline);
203 int COM_ParseToken_Console(const char **datapointer);
204
205 extern int com_argc;
206 extern const char **com_argv;
207
208 int COM_CheckParm (const char *parm);
209 void COM_Init (void);
210 void COM_Shutdown (void);
211 void COM_InitGameType (void);
212
213 char    *va(const char *format, ...) DP_FUNC_PRINTF(1);
214 // does a varargs printf into a temp buffer
215
216
217 // snprintf and vsnprintf are NOT portable. Use their DP counterparts instead
218 #define snprintf DO_NOT_USE_SNPRINTF__USE_DPSNPRINTF
219 #define vsnprintf DO_NOT_USE_VSNPRINTF__USE_DPVSNPRINTF
220
221 // dpsnprintf and dpvsnprintf
222 // return the number of printed characters, excluding the final '\0'
223 // or return -1 if the buffer isn't big enough to contain the entire string.
224 // buffer is ALWAYS null-terminated
225 extern int dpsnprintf (char *buffer, size_t buffersize, const char *format, ...) DP_FUNC_PRINTF(3);
226 extern int dpvsnprintf (char *buffer, size_t buffersize, const char *format, va_list args);
227
228 // A bunch of functions are forbidden for security reasons (and also to please MSVS 2005, for some of them)
229 // LordHavoc: added #undef lines here to avoid warnings in Linux
230 #undef strcat
231 #define strcat DO_NOT_USE_STRCAT__USE_STRLCAT_OR_MEMCPY
232 #undef strncat
233 #define strncat DO_NOT_USE_STRNCAT__USE_STRLCAT_OR_MEMCPY
234 #undef strcpy
235 #define strcpy DO_NOT_USE_STRCPY__USE_STRLCPY_OR_MEMCPY
236 #undef strncpy
237 #define strncpy DO_NOT_USE_STRNCPY__USE_STRLCPY_OR_MEMCPY
238 //#undef sprintf
239 //#define sprintf DO_NOT_USE_SPRINTF__USE_DPSNPRINTF
240
241
242 //============================================================================
243
244 extern  struct cvar_s   registered;
245 extern  struct cvar_s   cmdline;
246
247 typedef enum gamemode_e
248 {
249         GAME_NORMAL,
250         GAME_HIPNOTIC,
251         GAME_ROGUE,
252         GAME_NEHAHRA,
253         GAME_NEXUIZ,
254         GAME_TRANSFUSION,
255         GAME_GOODVSBAD2,
256         GAME_TEU,
257         GAME_BATTLEMECH,
258         GAME_ZYMOTIC,
259         GAME_SETHERAL,
260         GAME_SOM,
261         GAME_TENEBRAE, // full of evil hackery
262         GAME_NEOTERIC,
263         GAME_OPENQUARTZ, //this game sucks
264         GAME_PRYDON,
265         GAME_DELUXEQUAKE,
266         GAME_THEHUNTED,
267         GAME_DEFEATINDETAIL2,
268         GAME_DARSANA,
269         GAME_CONTAGIONTHEORY,
270         GAME_EDU2P,
271         GAME_PROPHECY,
272         GAME_BLOODOMNICIDE,
273         GAME_STEELSTORM, // added by motorsep
274         GAME_COUNT
275 }
276 gamemode_t;
277
278 extern gamemode_t gamemode;
279 extern const char *gamename;
280 extern const char *gamedirname1;
281 extern const char *gamedirname2;
282 extern const char *gamescreenshotname;
283 extern const char *gameuserdirname;
284 extern char com_modname[MAX_OSPATH];
285
286 void COM_ToLowerString (const char *in, char *out, size_t size_out);
287 void COM_ToUpperString (const char *in, char *out, size_t size_out);
288 int COM_StringBeginsWith(const char *s, const char *match);
289
290 int COM_ReadAndTokenizeLine(const char **text, char **argv, int maxargc, char *tokenbuf, int tokenbufsize, const char *commentprefix);
291
292 size_t COM_StringLengthNoColors(const char *s, size_t size_s, qboolean *valid);
293 qboolean COM_StringDecolorize(const char *in, size_t size_in, char *out, size_t size_out, qboolean escape_carets);
294 void COM_ToLowerString (const char *in, char *out, size_t size_out);
295 void COM_ToUpperString (const char *in, char *out, size_t size_out);
296
297 typedef struct stringlist_s
298 {
299         /// maxstrings changes as needed, causing reallocation of strings[] array
300         int maxstrings;
301         int numstrings;
302         char **strings;
303 } stringlist_t;
304
305 int matchpattern(const char *in, const char *pattern, int caseinsensitive);
306 int matchpattern_with_separator(const char *in, const char *pattern, int caseinsensitive, const char *separators, qboolean wildcard_least_one);
307 void stringlistinit(stringlist_t *list);
308 void stringlistfreecontents(stringlist_t *list);
309 void stringlistappend(stringlist_t *list, const char *text);
310 void stringlistsort(stringlist_t *list);
311 void listdirectory(stringlist_t *list, const char *basepath, const char *path);
312
313 char *SearchInfostring(const char *infostring, const char *key);
314
315 void InfoString_GetValue(const char *buffer, const char *key, char *value, size_t valuelength);
316 void InfoString_SetValue(char *buffer, size_t bufferlength, const char *key, const char *value);
317 void InfoString_Print(char *buffer);
318
319 // strlcat and strlcpy, from OpenBSD
320 // Most (all?) BSDs already have them
321 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(MACOSX)
322 # define HAVE_STRLCAT 1
323 # define HAVE_STRLCPY 1
324 #endif
325
326 #ifndef HAVE_STRLCAT
327 /*!
328  * Appends src to string dst of size siz (unlike strncat, siz is the
329  * full size of dst, not space left).  At most siz-1 characters
330  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
331  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
332  * If retval >= siz, truncation occurred.
333  */
334 size_t strlcat(char *dst, const char *src, size_t siz);
335 #endif  // #ifndef HAVE_STRLCAT
336
337 #ifndef HAVE_STRLCPY
338 /*!
339  * Copy src to string dst of size siz.  At most siz-1 characters
340  * will be copied.  Always NUL terminates (unless siz == 0).
341  * Returns strlen(src); if retval >= siz, truncation occurred.
342  */
343 size_t strlcpy(char *dst, const char *src, size_t siz);
344
345 #endif  // #ifndef HAVE_STRLCPY
346
347 void FindFraction(double val, int *num, int *denom, int denomMax);
348
349 #endif
350