]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - common.h
updated Dev-C++ project file from Spirit
[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 // many buffers use this size
25 #define MAX_INPUTLINE 16384
26
27
28 // MSVC has a different name for several standard functions
29 #ifdef WIN32
30 # define strcasecmp stricmp
31 # define strncasecmp strnicmp
32 #endif
33
34 // Create our own define for Mac OS X
35 #if defined(__APPLE__) && defined(__MACH__)
36 # define MACOSX
37 #endif
38
39 #ifdef SUNOS
40 #include <sys/file.h>           // Needed for FNDELAY
41 # define model_t dp_model_t // Workaround conflict with /usr/include/sys/model.h
42 #endif
43
44 //============================================================================
45
46 typedef struct sizebuf_s
47 {
48         qboolean        allowoverflow;  // if false, do a Sys_Error
49         qboolean        overflowed;             // set to true if the buffer size failed
50         unsigned char           *data;
51         int                     maxsize;
52         int                     cursize;
53 } sizebuf_t;
54
55 void SZ_Clear (sizebuf_t *buf);
56 unsigned char *SZ_GetSpace (sizebuf_t *buf, int length);
57 void SZ_Write (sizebuf_t *buf, const unsigned char *data, int length);
58 void SZ_HexDumpToConsole(const sizebuf_t *buf);
59
60 void Com_HexDumpToConsole(const unsigned char *data, int size);
61
62 unsigned short CRC_Block(const unsigned char *data, size_t size);
63
64 unsigned char COM_BlockSequenceCRCByteQW(unsigned char *base, int length, int sequence);
65
66 // these are actually md4sum (mdfour.c)
67 unsigned Com_BlockChecksum (void *buffer, int length);
68 void Com_BlockFullChecksum (void *buffer, int len, unsigned char *outbuf);
69
70
71 //============================================================================
72 //                                                      Endianess handling
73 //============================================================================
74
75 // We use BSD-style defines: BYTE_ORDER is defined to either BIG_ENDIAN or LITTLE_ENDIAN
76
77 // Initializations
78 #if !defined(BYTE_ORDER) || !defined(LITTLE_ENDIAN) || !defined(BIG_ENDIAN) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
79 # undef BYTE_ORDER
80 # undef LITTLE_ENDIAN
81 # undef BIG_ENDIAN
82 # define LITTLE_ENDIAN 1234
83 # define BIG_ENDIAN 4321
84 #endif
85
86 // If we still don't know the CPU endianess at this point, we try to guess
87 #ifndef BYTE_ORDER
88 # if defined(WIN32)
89 #  define BYTE_ORDER LITTLE_ENDIAN
90 # else
91 #  if defined(SUNOS)
92 #   if defined(__i386) || defined(__amd64)
93 #    define BYTE_ORDER LITTLE_ENDIAN
94 #   else
95 #    define BYTE_ORDER BIG_ENDIAN
96 #   endif
97 #  else
98 #   warning "Unable to determine the CPU endianess. Defaulting to little endian"
99 #   define BYTE_ORDER LITTLE_ENDIAN
100 #  endif
101 # endif
102 #endif
103
104
105 short ShortSwap (short l);
106 int LongSwap (int l);
107 float FloatSwap (float f);
108
109 #if BYTE_ORDER == LITTLE_ENDIAN
110 // little endian
111 #define BigShort(l) ShortSwap(l)
112 #define LittleShort(l) (l)
113 #define BigLong(l) LongSwap(l)
114 #define LittleLong(l) (l)
115 #define BigFloat(l) FloatSwap(l)
116 #define LittleFloat(l) (l)
117 #else
118 // big endian
119 #define BigShort(l) (l)
120 #define LittleShort(l) ShortSwap(l)
121 #define BigLong(l) (l)
122 #define LittleLong(l) LongSwap(l)
123 #define BigFloat(l) (l)
124 #define LittleFloat(l) FloatSwap(l)
125 #endif
126
127 unsigned int BuffBigLong (const unsigned char *buffer);
128 unsigned short BuffBigShort (const unsigned char *buffer);
129 unsigned int BuffLittleLong (const unsigned char *buffer);
130 unsigned short BuffLittleShort (const unsigned char *buffer);
131
132
133 //============================================================================
134
135 // these versions are purely for internal use, never sent in network protocol
136 // (use Protocol_EnumForNumber and Protocol_NumberToEnum to convert)
137 typedef enum protocolversion_e
138 {
139         PROTOCOL_UNKNOWN,
140         PROTOCOL_DARKPLACES7, // added QuakeWorld-style movement protocol to allow more consistent prediction
141         PROTOCOL_DARKPLACES6, // various changes
142         PROTOCOL_DARKPLACES5, // uses EntityFrame5 entity snapshot encoder/decoder which is based on a Tribes networking article at http://www.garagegames.com/articles/networking1/
143         PROTOCOL_DARKPLACES4, // various changes
144         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
145         PROTOCOL_DARKPLACES2, // various changes
146         PROTOCOL_DARKPLACES1, // uses EntityFrame entity snapshot encoder/decoder which is a QuakeWorld-like entity snapshot delta compression method
147         PROTOCOL_QUAKEDP, // darkplaces extended quake protocol (used by TomazQuake and others), backwards compatible as long as no extended features are used
148         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)
149         PROTOCOL_QUAKE, // quake (aka netquake/normalquake/nq) protocol
150         PROTOCOL_QUAKEWORLD, // quakeworld protocol
151 }
152 protocolversion_t;
153
154 void MSG_WriteChar (sizebuf_t *sb, int c);
155 void MSG_WriteByte (sizebuf_t *sb, int c);
156 void MSG_WriteShort (sizebuf_t *sb, int c);
157 void MSG_WriteLong (sizebuf_t *sb, int c);
158 void MSG_WriteFloat (sizebuf_t *sb, float f);
159 void MSG_WriteString (sizebuf_t *sb, const char *s);
160 void MSG_WriteUnterminatedString (sizebuf_t *sb, const char *s);
161 void MSG_WriteAngle8i (sizebuf_t *sb, float f);
162 void MSG_WriteAngle16i (sizebuf_t *sb, float f);
163 void MSG_WriteAngle32f (sizebuf_t *sb, float f);
164 void MSG_WriteCoord13i (sizebuf_t *sb, float f);
165 void MSG_WriteCoord16i (sizebuf_t *sb, float f);
166 void MSG_WriteCoord32f (sizebuf_t *sb, float f);
167 void MSG_WriteCoord (sizebuf_t *sb, float f, protocolversion_t protocol);
168 void MSG_WriteVector (sizebuf_t *sb, float *v, protocolversion_t protocol);
169 void MSG_WriteAngle (sizebuf_t *sb, float f, protocolversion_t protocol);
170
171 extern  int                     msg_readcount;
172 extern  qboolean        msg_badread;            // set if a read goes beyond end of message
173
174 void MSG_BeginReading (void);
175 int MSG_ReadLittleShort (void);
176 int MSG_ReadBigShort (void);
177 int MSG_ReadLittleLong (void);
178 int MSG_ReadBigLong (void);
179 float MSG_ReadLittleFloat (void);
180 float MSG_ReadBigFloat (void);
181 char *MSG_ReadString (void);
182 int MSG_ReadBytes (int numbytes, unsigned char *out);
183
184 #define MSG_ReadChar() (msg_readcount >= net_message.cursize ? (msg_badread = true, -1) : (signed char)net_message.data[msg_readcount++])
185 #define MSG_ReadByte() (msg_readcount >= net_message.cursize ? (msg_badread = true, -1) : (unsigned char)net_message.data[msg_readcount++])
186 #define MSG_ReadShort MSG_ReadLittleShort
187 #define MSG_ReadLong MSG_ReadLittleLong
188 #define MSG_ReadFloat MSG_ReadLittleFloat
189
190 float MSG_ReadAngle8i (void);
191 float MSG_ReadAngle16i (void);
192 float MSG_ReadAngle32f (void);
193 float MSG_ReadCoord13i (void);
194 float MSG_ReadCoord16i (void);
195 float MSG_ReadCoord32f (void);
196 float MSG_ReadCoord (protocolversion_t protocol);
197 void MSG_ReadVector (float *v, protocolversion_t protocol);
198 float MSG_ReadAngle (protocolversion_t protocol);
199
200 //============================================================================
201
202 extern char com_token[MAX_INPUTLINE];
203
204 int COM_ParseToken(const char **datapointer, int returnnewline);
205 int COM_ParseTokenConsole(const char **datapointer);
206
207 extern int com_argc;
208 extern const char **com_argv;
209
210 int COM_CheckParm (const char *parm);
211 void COM_Init (void);
212 void COM_Shutdown (void);
213 void COM_InitGameType (void);
214
215 char    *va(const char *format, ...);
216 // does a varargs printf into a temp buffer
217
218
219 // snprintf and vsnprintf are NOT portable. Use their DP counterparts instead
220 #define snprintf DO_NOT_USE_SNPRINTF__USE_DPSNPRINTF
221 #define vsnprintf DO_NOT_USE_VSNPRINTF__USE_DPVSNPRINTF
222
223 // dpsnprintf and dpvsnprintf
224 // return the number of printed characters, excluding the final '\0'
225 // or return -1 if the buffer isn't big enough to contain the entire string.
226 // buffer is ALWAYS null-terminated
227 extern int dpsnprintf (char *buffer, size_t buffersize, const char *format, ...);
228 extern int dpvsnprintf (char *buffer, size_t buffersize, const char *format, va_list args);
229
230 // A bunch of functions are forbidden for security reasons (and also to please MSVS 2005, for some of them)
231 // LordHavoc: added #undef lines here to avoid warnings in Linux
232 #undef strcat
233 #define strcat DO_NOT_USE_STRCAT__USE_STRLCAT_OR_MEMCPY
234 #undef strncat
235 #define strncat DO_NOT_USE_STRNCAT__USE_STRLCAT_OR_MEMCPY
236 #undef strcpy
237 #define strcpy DO_NOT_USE_STRCPY__USE_STRLCPY_OR_MEMCPY
238 #undef strncpy
239 #define strncpy DO_NOT_USE_STRNCPY__USE_STRLCPY_OR_MEMCPY
240 //#undef sprintf
241 //#define sprintf DO_NOT_USE_SPRINTF__USE_DPSNPRINTF
242
243
244 //============================================================================
245
246 extern  struct cvar_s   registered;
247 extern  struct cvar_s   cmdline;
248
249 typedef enum gamemode_e
250 {
251         GAME_NORMAL,
252         GAME_HIPNOTIC,
253         GAME_ROGUE,
254         GAME_NEHAHRA,
255         GAME_NEXUIZ,
256         GAME_TRANSFUSION,
257         GAME_GOODVSBAD2,
258         GAME_TEU,
259         GAME_BATTLEMECH,
260         GAME_ZYMOTIC,
261         GAME_SETHERAL,
262         GAME_SOM,
263         GAME_TENEBRAE, // full of evil hackery
264         GAME_NEOTERIC,
265         GAME_OPENQUARTZ, //this game sucks
266         GAME_PRYDON,
267         GAME_NETHERWORLD,
268         GAME_THEHUNTED,
269         GAME_DEFEATINDETAIL2,
270         GAME_DARSANA,
271         GAME_CONTAGIONTHEORY,
272 }
273 gamemode_t;
274
275 extern gamemode_t gamemode;
276 extern const char *gamename;
277 extern const char *gamedirname1;
278 extern const char *gamedirname2;
279 extern const char *gamescreenshotname;
280 extern const char *gameuserdirname;
281 extern char com_modname[MAX_OSPATH];
282
283 void COM_ToLowerString (const char *in, char *out, size_t size_out);
284 void COM_ToUpperString (const char *in, char *out, size_t size_out);
285 int COM_StringBeginsWith(const char *s, const char *match);
286
287 int COM_ReadAndTokenizeLine(const char **text, char **argv, int maxargc, char *tokenbuf, int tokenbufsize, const char *commentprefix);
288
289 typedef struct stringlist_s
290 {
291         struct stringlist_s *next;
292         char *text;
293 } stringlist_t;
294
295 int matchpattern(const char *in, const char *pattern, int caseinsensitive);
296 stringlist_t *stringlistappend(stringlist_t *current, char *text);
297 void stringlistfree(stringlist_t *current);
298 stringlist_t *stringlistsort(stringlist_t *start);
299 stringlist_t *listdirectory(const char *path);
300 void freedirectory(stringlist_t *list);
301
302 char *SearchInfostring(const char *infostring, const char *key);
303
304 void InfoString_GetValue(const char *buffer, const char *key, char *value, size_t valuelength);
305 void InfoString_SetValue(char *buffer, size_t bufferlength, const char *key, const char *value);
306 void InfoString_Print(char *buffer);
307
308 // strlcat and strlcpy, from OpenBSD
309 // Most (all?) BSDs already have them
310 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(MACOSX)
311 # define HAVE_STRLCAT 1
312 # define HAVE_STRLCPY 1
313 #endif
314
315 #ifndef HAVE_STRLCAT
316 /*
317  * Appends src to string dst of size siz (unlike strncat, siz is the
318  * full size of dst, not space left).  At most siz-1 characters
319  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
320  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
321  * If retval >= siz, truncation occurred.
322  */
323 size_t strlcat(char *dst, const char *src, size_t siz);
324 #endif  // #ifndef HAVE_STRLCAT
325
326 #ifndef HAVE_STRLCPY
327 /*
328  * Copy src to string dst of size siz.  At most siz-1 characters
329  * will be copied.  Always NUL terminates (unless siz == 0).
330  * Returns strlen(src); if retval >= siz, truncation occurred.
331  */
332 size_t strlcpy(char *dst, const char *src, size_t siz);
333
334 #endif  // #ifndef HAVE_STRLCPY
335
336 #endif
337