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