]> git.xonotic.org Git - xonotic/darkplaces.git/blob - common.h
Fix OpenSolaris build. Add platform ID for a few other platforms
[xonotic/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 #else
30 #include "strings.h"
31 #endif
32
33 // Create our own define for Mac OS X
34 #if defined(__APPLE__) && defined(__MACH__)
35 # define MACOSX
36 #endif
37
38
39 //============================================================================
40
41 typedef struct sizebuf_s
42 {
43         qboolean        allowoverflow;  ///< if false, do a Sys_Error
44         qboolean        overflowed;             ///< set to true if the buffer size failed
45         unsigned char           *data;
46         int                     maxsize;
47         int                     cursize;
48         int                     readcount;
49         qboolean        badread;                // set if a read goes beyond end of message
50 } sizebuf_t;
51
52 void SZ_Clear (sizebuf_t *buf);
53 unsigned char *SZ_GetSpace (sizebuf_t *buf, int length);
54 void SZ_Write (sizebuf_t *buf, const unsigned char *data, int length);
55 void SZ_HexDumpToConsole(const sizebuf_t *buf);
56
57 void Com_HexDumpToConsole(const unsigned char *data, int size);
58
59 unsigned short CRC_Block(const unsigned char *data, size_t size);
60 unsigned short CRC_Block_CaseInsensitive(const unsigned char *data, size_t size); // for hash lookup functions that use strcasecmp for comparison
61
62 unsigned char COM_BlockSequenceCRCByteQW(unsigned char *base, int length, int sequence);
63
64 // these are actually md4sum (mdfour.c)
65 unsigned Com_BlockChecksum (void *buffer, int length);
66 void Com_BlockFullChecksum (void *buffer, int len, unsigned char *outbuf);
67
68 void COM_Init_Commands(void);
69
70
71 //============================================================================
72 //                                                      Endianess handling
73 //============================================================================
74
75 // check mem_bigendian if you need to know the system byte order
76
77 /*! \name Byte order functions.
78  * @{
79  */
80
81 // unaligned memory access crashes on some platform, so always read bytes...
82 #define BigShort(l) BuffBigShort((unsigned char *)&(l))
83 #define LittleShort(l) BuffLittleShort((unsigned char *)&(l))
84 #define BigLong(l) BuffBigLong((unsigned char *)&(l))
85 #define LittleLong(l) BuffLittleLong((unsigned char *)&(l))
86 #define BigFloat(l) BuffBigFloat((unsigned char *)&(l))
87 #define LittleFloat(l) BuffLittleFloat((unsigned char *)&(l))
88
89 /// Extract a big endian 32bit float from the given \p buffer.
90 float BuffBigFloat (const unsigned char *buffer);
91
92 /// Extract a big endian 32bit int from the given \p buffer.
93 int BuffBigLong (const unsigned char *buffer);
94
95 /// Extract a big endian 16bit short from the given \p buffer.
96 short BuffBigShort (const unsigned char *buffer);
97
98 /// Extract a little endian 32bit float from the given \p buffer.
99 float BuffLittleFloat (const unsigned char *buffer);
100
101 /// Extract a little endian 32bit int from the given \p buffer.
102 int BuffLittleLong (const unsigned char *buffer);
103
104 /// Extract a little endian 16bit short from the given \p buffer.
105 short BuffLittleShort (const unsigned char *buffer);
106
107 /// Encode a big endian 32bit int to the given \p buffer
108 void StoreBigLong (unsigned char *buffer, unsigned int i);
109
110 /// Encode a big endian 16bit int to the given \p buffer
111 void StoreBigShort (unsigned char *buffer, unsigned short i);
112
113 /// Encode a little endian 32bit int to the given \p buffer
114 void StoreLittleLong (unsigned char *buffer, unsigned int i);
115
116 /// Encode a little endian 16bit int to the given \p buffer
117 void StoreLittleShort (unsigned char *buffer, unsigned short i);
118 //@}
119
120 //============================================================================
121
122 // these versions are purely for internal use, never sent in network protocol
123 // (use Protocol_EnumForNumber and Protocol_NumberToEnum to convert)
124 typedef enum protocolversion_e
125 {
126         PROTOCOL_UNKNOWN,
127         PROTOCOL_DARKPLACES7, ///< added QuakeWorld-style movement protocol to allow more consistent prediction
128         PROTOCOL_DARKPLACES6, ///< various changes
129         PROTOCOL_DARKPLACES5, ///< uses EntityFrame5 entity snapshot encoder/decoder which is based on a Tribes networking article at http://www.garagegames.com/articles/networking1/
130         PROTOCOL_DARKPLACES4, ///< various changes
131         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
132         PROTOCOL_DARKPLACES2, ///< various changes
133         PROTOCOL_DARKPLACES1, ///< uses EntityFrame entity snapshot encoder/decoder which is a QuakeWorld-like entity snapshot delta compression method
134         PROTOCOL_QUAKEDP, ///< darkplaces extended quake protocol (used by TomazQuake and others), backwards compatible as long as no extended features are used
135         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)
136         PROTOCOL_QUAKE, ///< quake (aka netquake/normalquake/nq) protocol
137         PROTOCOL_QUAKEWORLD, ///< quakeworld protocol
138         PROTOCOL_NEHAHRABJP, ///< same as QUAKEDP but with 16bit modelindex
139         PROTOCOL_NEHAHRABJP2, ///< same as NEHAHRABJP but with 16bit soundindex
140         PROTOCOL_NEHAHRABJP3 ///< same as NEHAHRABJP2 but with some changes
141 }
142 protocolversion_t;
143
144 /*! \name Message IO functions.
145  * Handles byte ordering and avoids alignment errors
146  * @{
147  */
148
149 void MSG_InitReadBuffer (sizebuf_t *buf, unsigned char *data, int size);
150 void MSG_WriteChar (sizebuf_t *sb, int c);
151 void MSG_WriteByte (sizebuf_t *sb, int c);
152 void MSG_WriteShort (sizebuf_t *sb, int c);
153 void MSG_WriteLong (sizebuf_t *sb, int c);
154 void MSG_WriteFloat (sizebuf_t *sb, vec_t f);
155 void MSG_WriteString (sizebuf_t *sb, const char *s);
156 void MSG_WriteUnterminatedString (sizebuf_t *sb, const char *s);
157 void MSG_WriteAngle8i (sizebuf_t *sb, vec_t f);
158 void MSG_WriteAngle16i (sizebuf_t *sb, vec_t f);
159 void MSG_WriteAngle32f (sizebuf_t *sb, vec_t f);
160 void MSG_WriteCoord13i (sizebuf_t *sb, vec_t f);
161 void MSG_WriteCoord16i (sizebuf_t *sb, vec_t f);
162 void MSG_WriteCoord32f (sizebuf_t *sb, vec_t f);
163 void MSG_WriteCoord (sizebuf_t *sb, vec_t f, protocolversion_t protocol);
164 void MSG_WriteVector (sizebuf_t *sb, const vec3_t v, protocolversion_t protocol);
165 void MSG_WriteAngle (sizebuf_t *sb, vec_t f, protocolversion_t protocol);
166
167 void MSG_BeginReading (sizebuf_t *sb);
168 int MSG_ReadLittleShort (sizebuf_t *sb);
169 int MSG_ReadBigShort (sizebuf_t *sb);
170 int MSG_ReadLittleLong (sizebuf_t *sb);
171 int MSG_ReadBigLong (sizebuf_t *sb);
172 float MSG_ReadLittleFloat (sizebuf_t *sb);
173 float MSG_ReadBigFloat (sizebuf_t *sb);
174 char *MSG_ReadString (sizebuf_t *sb, char *string, size_t maxstring);
175 int MSG_ReadBytes (sizebuf_t *sb, int numbytes, unsigned char *out);
176
177 #define MSG_ReadChar(sb) ((sb)->readcount >= (sb)->cursize ? ((sb)->badread = true, -1) : (signed char)(sb)->data[(sb)->readcount++])
178 #define MSG_ReadByte(sb) ((sb)->readcount >= (sb)->cursize ? ((sb)->badread = true, -1) : (unsigned char)(sb)->data[(sb)->readcount++])
179 #define MSG_ReadShort MSG_ReadLittleShort
180 #define MSG_ReadLong MSG_ReadLittleLong
181 #define MSG_ReadFloat MSG_ReadLittleFloat
182
183 float MSG_ReadAngle8i (sizebuf_t *sb);
184 float MSG_ReadAngle16i (sizebuf_t *sb);
185 float MSG_ReadAngle32f (sizebuf_t *sb);
186 float MSG_ReadCoord13i (sizebuf_t *sb);
187 float MSG_ReadCoord16i (sizebuf_t *sb);
188 float MSG_ReadCoord32f (sizebuf_t *sb);
189 float MSG_ReadCoord (sizebuf_t *sb, protocolversion_t protocol);
190 void MSG_ReadVector (sizebuf_t *sb, vec3_t v, protocolversion_t protocol);
191 float MSG_ReadAngle (sizebuf_t *sb, protocolversion_t protocol);
192 //@}
193 //============================================================================
194
195 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
196 typedef int (*COM_LineProcessorFunc) (void *passthrough, const char *line, size_t length, float width, qboolean isContination);
197 int COM_Wordwrap(const char *string, size_t length, float continuationSize, float maxWidth, COM_WordWidthFunc_t wordWidth, void *passthroughCW, COM_LineProcessorFunc processLine, void *passthroughPL);
198
199 extern char com_token[MAX_INPUTLINE];
200
201 int COM_ParseToken_Simple(const char **datapointer, qboolean returnnewline, qboolean parsebackslash, qboolean parsecomments);
202 int COM_ParseToken_QuakeC(const char **datapointer, qboolean returnnewline);
203 int COM_ParseToken_VM_Tokenize(const char **datapointer, qboolean returnnewline);
204 int COM_ParseToken_Console(const char **datapointer);
205
206 int COM_CheckParm (const char *parm);
207 void COM_Init (void);
208 void COM_Shutdown (void);
209 void COM_InitGameType (void);
210
211 char *va(char *buf, size_t buflen, const char *format, ...) DP_FUNC_PRINTF(3);
212 // does a varargs printf into provided buffer, returns buffer (so it can be called in-line unlike dpsnprintf)
213
214
215 // snprintf and vsnprintf are NOT portable. Use their DP counterparts instead
216 #ifdef snprintf
217 # undef snprintf
218 #endif
219 #define snprintf DO_NOT_USE_SNPRINTF__USE_DPSNPRINTF
220 #ifdef vsnprintf
221 # undef vsnprintf
222 #endif
223 #define vsnprintf DO_NOT_USE_VSNPRINTF__USE_DPVSNPRINTF
224
225 // dpsnprintf and dpvsnprintf
226 // return the number of printed characters, excluding the final '\0'
227 // or return -1 if the buffer isn't big enough to contain the entire string.
228 // buffer is ALWAYS null-terminated
229 extern int dpsnprintf (char *buffer, size_t buffersize, const char *format, ...) DP_FUNC_PRINTF(3);
230 extern int dpvsnprintf (char *buffer, size_t buffersize, const char *format, va_list args);
231
232 // A bunch of functions are forbidden for security reasons (and also to please MSVS 2005, for some of them)
233 // LadyHavoc: added #undef lines here to avoid warnings in Linux
234 #undef strcat
235 #define strcat DO_NOT_USE_STRCAT__USE_STRLCAT_OR_MEMCPY
236 #undef strncat
237 #define strncat DO_NOT_USE_STRNCAT__USE_STRLCAT_OR_MEMCPY
238 #undef strcpy
239 #define strcpy DO_NOT_USE_STRCPY__USE_STRLCPY_OR_MEMCPY
240 #undef strncpy
241 #define strncpy DO_NOT_USE_STRNCPY__USE_STRLCPY_OR_MEMCPY
242 //#undef sprintf
243 //#define sprintf DO_NOT_USE_SPRINTF__USE_DPSNPRINTF
244
245
246 //============================================================================
247
248 extern  struct cvar_s   registered;
249 extern  struct cvar_s   cmdline;
250
251 typedef enum userdirmode_e
252 {
253         USERDIRMODE_NOHOME, // basedir only
254         USERDIRMODE_HOME, // Windows basedir, general POSIX (~/.)
255         USERDIRMODE_MYGAMES, // pre-Vista (My Documents/My Games/), general POSIX (~/.)
256         USERDIRMODE_SAVEDGAMES, // Vista (%USERPROFILE%/Saved Games/), OSX (~/Library/Application Support/), Linux (~/.config)
257         USERDIRMODE_COUNT
258 }
259 userdirmode_t;
260
261 typedef enum gamemode_e
262 {
263         GAME_NORMAL,
264         GAME_HIPNOTIC,
265         GAME_ROGUE,
266         GAME_QUOTH,
267         GAME_NEHAHRA,
268         GAME_NEXUIZ,
269         GAME_XONOTIC,
270         GAME_TRANSFUSION,
271         GAME_GOODVSBAD2,
272         GAME_TEU,
273         GAME_BATTLEMECH,
274         GAME_ZYMOTIC,
275         GAME_SETHERAL,
276         GAME_TENEBRAE, // full of evil hackery
277         GAME_NEOTERIC,
278         GAME_OPENQUARTZ, //this game sucks
279         GAME_PRYDON,
280         GAME_DELUXEQUAKE,
281         GAME_THEHUNTED,
282         GAME_DEFEATINDETAIL2,
283         GAME_DARSANA,
284         GAME_CONTAGIONTHEORY,
285         GAME_EDU2P,
286         GAME_PROPHECY,
287         GAME_BLOODOMNICIDE,
288         GAME_STEELSTORM, // added by motorsep
289         GAME_STEELSTORM2, // added by motorsep
290         GAME_SSAMMO, // added by motorsep
291         GAME_STEELSTORMREVENANTS, // added by motorsep 07/19/2015
292         GAME_TOMESOFMEPHISTOPHELES, // added by motorsep
293         GAME_STRAPBOMB, // added by motorsep for Urre
294         GAME_MOONHELM,
295         GAME_VORETOURNAMENT,
296         GAME_DOOMBRINGER, // added by Cloudwalk for kristus
297         GAME_COUNT
298 }
299 gamemode_t;
300
301 // Master switch for some hacks/changes that eventually should become cvars.
302 #define IS_NEXUIZ_DERIVED(g) ((g) == GAME_NEXUIZ || (g) == GAME_XONOTIC || (g) == GAME_VORETOURNAMENT)
303 // Pre-csqcmodels era.
304 #define IS_OLDNEXUIZ_DERIVED(g) ((g) == GAME_NEXUIZ || (g) == GAME_VORETOURNAMENT)
305
306 extern gamemode_t gamemode;
307 extern const char *gamename;
308 extern const char *gamenetworkfiltername;
309 extern const char *gamedirname1;
310 extern const char *gamedirname2;
311 extern const char *gamescreenshotname;
312 extern const char *gameuserdirname;
313 extern char com_modname[MAX_OSPATH];
314
315 void COM_ChangeGameTypeForGameDirs(void);
316
317 void COM_ToLowerString (const char *in, char *out, size_t size_out);
318 void COM_ToUpperString (const char *in, char *out, size_t size_out);
319 int COM_StringBeginsWith(const char *s, const char *match);
320
321 int COM_ReadAndTokenizeLine(const char **text, char **argv, int maxargc, char *tokenbuf, int tokenbufsize, const char *commentprefix);
322
323 size_t COM_StringLengthNoColors(const char *s, size_t size_s, qboolean *valid);
324 qboolean COM_StringDecolorize(const char *in, size_t size_in, char *out, size_t size_out, qboolean escape_carets);
325 void COM_ToLowerString (const char *in, char *out, size_t size_out);
326 void COM_ToUpperString (const char *in, char *out, size_t size_out);
327
328 typedef struct stringlist_s
329 {
330         /// maxstrings changes as needed, causing reallocation of strings[] array
331         int maxstrings;
332         int numstrings;
333         char **strings;
334 } stringlist_t;
335
336 int matchpattern(const char *in, const char *pattern, int caseinsensitive);
337 int matchpattern_with_separator(const char *in, const char *pattern, int caseinsensitive, const char *separators, qboolean wildcard_least_one);
338 void stringlistinit(stringlist_t *list);
339 void stringlistfreecontents(stringlist_t *list);
340 void stringlistappend(stringlist_t *list, const char *text);
341 void stringlistsort(stringlist_t *list, qboolean uniq);
342 void listdirectory(stringlist_t *list, const char *basepath, const char *path);
343
344 char *InfoString_GetValue(const char *buffer, const char *key, char *value, size_t valuelength);
345 void InfoString_SetValue(char *buffer, size_t bufferlength, const char *key, const char *value);
346 void InfoString_Print(char *buffer);
347
348 // strlcat and strlcpy, from OpenBSD
349 // Most (all?) BSDs already have them
350 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(MACOSX)
351 # define HAVE_STRLCAT 1
352 # define HAVE_STRLCPY 1
353 #endif
354
355 #ifndef HAVE_STRLCAT
356 /*!
357  * Appends src to string dst of size siz (unlike strncat, siz is the
358  * full size of dst, not space left).  At most siz-1 characters
359  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
360  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
361  * If retval >= siz, truncation occurred.
362  */
363 size_t strlcat(char *dst, const char *src, size_t siz);
364 #endif  // #ifndef HAVE_STRLCAT
365
366 #ifndef HAVE_STRLCPY
367 /*!
368  * Copy src to string dst of size siz.  At most siz-1 characters
369  * will be copied.  Always NUL terminates (unless siz == 0).
370  * Returns strlen(src); if retval >= siz, truncation occurred.
371  */
372 size_t strlcpy(char *dst, const char *src, size_t siz);
373
374 #endif  // #ifndef HAVE_STRLCPY
375
376 void FindFraction(double val, int *num, int *denom, int denomMax);
377
378 // decodes XPM file to XPM array (as if #include'd)
379 char **XPM_DecodeString(const char *in);
380
381 size_t base64_encode(unsigned char *buf, size_t buflen, size_t outbuflen);
382
383 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
384
385 #endif
386