1 /*
2 ===========================================================================
3 Copyright (C) 1999 - 2005, Id Software, Inc.
4 Copyright (C) 2000 - 2013, Raven Software, Inc.
5 Copyright (C) 2001 - 2013, Activision, Inc.
6 Copyright (C) 2005 - 2015, ioquake3 contributors
7 Copyright (C) 2013 - 2015, OpenJK contributors
8 
9 This file is part of the OpenJK source code.
10 
11 OpenJK is free software; you can redistribute it and/or modify it
12 under the terms of the GNU General Public License version 2 as
13 published by the Free Software Foundation.
14 
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, see <http://www.gnu.org/licenses/>.
22 ===========================================================================
23 */
24 
25 // qcommon.h -- definitions common between client and server, but not game.or ref modules
26 #ifndef __QCOMMON_H__
27 #define __QCOMMON_H__
28 
29 #include "q_shared.h"
30 #include "stringed_ingame.h"
31 #include "strippublic.h"
32 #include "cm_public.h"
33 #include "sys/sys_public.h"
34 
35 
36 // some zone mem debugging stuff
37 #ifndef FINAL_BUILD
38 	#ifdef _DEBUG
39 	//
40 	// both of these should be REM'd unless you specifically need them...
41 	//
42 	//#define DEBUG_ZONE_ALLOCS			// adds __FILE__ and __LINE__ info to zone blocks, to see who's leaking
43 	//#define DETAILED_ZONE_DEBUG_CODE	// this slows things down a LOT, and is only for tracking nasty double-freeing Z_Malloc bugs
44 	#endif
45 #endif
46 
47 
48 //============================================================================
49 
50 //
51 // msg.c
52 //
53 typedef struct {
54 	qboolean	allowoverflow;	// if false, do a Com_Error
55 	qboolean	overflowed;		// set to true if the buffer size failed (with allowoverflow set)
56 	byte	*data;
57 	int		maxsize;
58 	int		cursize;
59 	int		readcount;
60 	int		bit;				// for bitwise reads and writes
61 } msg_t;
62 
63 void MSG_Init (msg_t *buf, byte *data, int length);
64 void MSG_Clear (msg_t *buf);
65 void *MSG_GetSpace (msg_t *buf, int length);
66 void MSG_WriteData (msg_t *buf, const void *data, int length);
67 
68 
69 struct usercmd_s;
70 struct entityState_s;
71 
72 template<typename TSaberInfo>
73 class PlayerStateBase;
74 
75 using playerState_t = PlayerStateBase<saberInfo_t>;
76 
77 void MSG_WriteBits( msg_t *msg, int value, int bits );
78 
79 void MSG_WriteByte (msg_t *sb, int c);
80 void MSG_WriteShort (msg_t *sb, int c);
81 void MSG_WriteLong (msg_t *sb, int c);
82 void MSG_WriteString (msg_t *sb, const char *s);
83 
84 void	MSG_BeginReading (msg_t *sb);
85 
86 int		MSG_ReadBits( msg_t *msg, int bits );
87 
88 int		MSG_ReadByte (msg_t *sb);
89 int		MSG_ReadShort (msg_t *sb);
90 int		MSG_ReadLong (msg_t *sb);
91 char	*MSG_ReadString (msg_t *sb);
92 char	*MSG_ReadStringLine (msg_t *sb);
93 void	MSG_ReadData (msg_t *sb, void *buffer, int size);
94 
95 
96 void MSG_WriteDeltaUsercmd( msg_t *msg, struct usercmd_s *from, struct usercmd_s *to );
97 void MSG_ReadDeltaUsercmd( msg_t *msg, struct usercmd_s *from, struct usercmd_s *to );
98 
99 void MSG_WriteDeltaEntity( msg_t *msg, struct entityState_s *from, struct entityState_s *to
100 						   , qboolean force );
101 void MSG_ReadDeltaEntity( msg_t *msg, entityState_t *from, entityState_t *to,
102 						 int number );
103 void MSG_ReadEntity( msg_t *msg, entityState_t *to);
104 void MSG_WriteEntity( msg_t *msg, entityState_t *to, int removeNum);
105 
106 void MSG_WriteDeltaPlayerstate( msg_t *msg, playerState_t *from, playerState_t *to );
107 void MSG_ReadDeltaPlayerstate( msg_t *msg, playerState_t *from, playerState_t *to );
108 
109 /*
110 ==============================================================
111 
112 NET
113 
114 ==============================================================
115 */
116 
117 
118 #define	PACKET_BACKUP	16	// number of old messages that must be kept on client and
119 													// server for delta comrpession and ping estimation
120 #define	PACKET_MASK		(PACKET_BACKUP-1)
121 
122 #define	MAX_PACKET_USERCMDS		32		// max number of usercmd_t in a packet
123 
124 #define	PORT_ANY			-1
125 
126 #define	MAX_RELIABLE_COMMANDS	64			// max string commands buffered for restransmit
127 
128 typedef enum {
129 	NS_CLIENT,
130 	NS_SERVER
131 } netsrc_t;
132 
133 // For compatibility with shared code
NET_Init(void)134 static inline void NET_Init( void ) {}
NET_Shutdown(void)135 static inline void NET_Shutdown( void ) {}
136 
137 void		NET_SendPacket (netsrc_t sock, int length, const void *data, netadr_t to);
138 void		NET_OutOfBandPrint( netsrc_t net_socket, netadr_t adr, const char *format, ...);
139 
140 qboolean	NET_CompareAdr (netadr_t a, netadr_t b);
141 qboolean	NET_CompareBaseAdr (netadr_t a, netadr_t b);
142 qboolean	NET_IsLocalAddress (netadr_t adr);
143 qboolean	NET_IsLANAddress (netadr_t adr);
144 const char	*NET_AdrToString (netadr_t a);
145 qboolean	NET_StringToAdr ( const char *s, netadr_t *a);
146 qboolean	NET_GetLoopPacket (netsrc_t sock, netadr_t *net_from, msg_t *net_message);
147 
148 void		Sys_SendPacket( int length, const void *data, netadr_t to );
149 //Does NOT parse port numbers, only base addresses.
150 qboolean	Sys_StringToAdr( const char *s, netadr_t *a );
151 qboolean	Sys_IsLANAddress (netadr_t adr);
152 void		Sys_ShowIP(void);
153 
154 
155 #define	MAX_MSGLEN				(1*17408)		// max length of a message, which may
156 //#define	MAX_MSGLEN				(3*16384)		// max length of a message, which may
157 											// be fragmented into multiple packets
158 
159 
160 /*
161 Netchan handles packet fragmentation and out of order / duplicate suppression
162 */
163 
164 typedef struct {
165 	netsrc_t	sock;
166 
167 	int			dropped;			// between last packet and previous
168 
169 	netadr_t	remoteAddress;
170 	int			qport;				// qport value to write when transmitting
171 
172 	// sequencing variables
173 	int			incomingSequence;
174 	int			incomingAcknowledged;
175 
176 	int			outgoingSequence;
177 
178 	// incoming fragment assembly buffer
179 	int			fragmentSequence;
180 	int			fragmentLength;
181 	byte		fragmentBuffer[MAX_MSGLEN];
182 } netchan_t;
183 
184 void Netchan_Init( int qport );
185 void Netchan_Setup( netsrc_t sock, netchan_t *chan, netadr_t adr, int qport );
186 
187 void Netchan_Transmit( netchan_t *chan, int length, const byte *data );
188 qboolean Netchan_Process( netchan_t *chan, msg_t *msg );
189 
190 
191 /*
192 ==============================================================
193 
194 PROTOCOL
195 
196 ==============================================================
197 */
198 
199 #define	PROTOCOL_VERSION	40
200 
201 #define	PORT_SERVER			27960
202 
203 // the svc_strings[] array in cl_parse.c should mirror this
204 //
205 // server to client
206 //
207 enum svc_ops_e {
208 	svc_bad,
209 	svc_nop,
210 	svc_gamestate,
211 	svc_configstring,			// [short] [string] only in gamestate messages
212 	svc_baseline,				// only in gamestate messages
213 	svc_serverCommand,			// [string] to be executed by client game module
214 	svc_download,				// [short] size [size bytes]
215 	svc_snapshot
216 };
217 
218 
219 //
220 // client to server
221 //
222 enum clc_ops_e {
223 	clc_bad,
224 	clc_nop,
225 	clc_move,				// [[usercmd_t]
226 	clc_clientCommand		// [string] message
227 };
228 
229 /*
230 ==============================================================
231 
232 VIRTUAL MACHINE
233 
234 ==============================================================
235 */
236 
237 typedef enum vmSlots_e {
238 	VM_GAME=0,
239 	VM_CGAME,
240 	VM_UI,
241 	MAX_VM
242 } vmSlots_t;
243 
244 #define	VMA(x) ((void*)args[x])
_vmf(intptr_t x)245 inline float _vmf(intptr_t x)
246 {
247 	byteAlias_t fi;
248 	fi.i = (int) x;
249 	return fi.f;
250 }
251 #define	VMF(x)	_vmf(args[x])
252 
253 /*
254 ==============================================================
255 
256 CMD
257 
258 Command text buffering and command execution
259 
260 ==============================================================
261 */
262 
263 /*
264 
265 Any number of commands can be added in a frame, from several different sources.
266 Most commands come from either keybindings or console line input, but entire text
267 files can be execed.
268 
269 */
270 
271 void Cbuf_Init (void);
272 // allocates an initial text buffer that will grow as needed
273 
274 void Cbuf_AddText( const char *text );
275 // Adds command text at the end of the buffer, does NOT add a final \n
276 
277 void Cbuf_ExecuteText( int exec_when, const char *text );
278 // this can be used in place of either Cbuf_AddText or Cbuf_InsertText
279 
280 void Cbuf_Execute (void);
281 // Pulls off \n terminated lines of text from the command buffer and sends
282 // them through Cmd_ExecuteString.  Stops when the buffer is empty.
283 // Normally called once per frame, but may be explicitly invoked.
284 // Do not call inside a command function, or current args will be destroyed.
285 
286 //===========================================================================
287 
288 /*
289 
290 Command execution takes a null terminated string, breaks it into tokens,
291 then searches for a command or variable that matches the first token.
292 
293 */
294 
295 typedef void (*xcommand_t) (void);
296 typedef void ( *callbackFunc_t )( const char *s );
297 
298 void	Cmd_Init (void);
299 
300 void	Cmd_AddCommand( const char *cmd_name, xcommand_t function );
301 // called by the init functions of other parts of the program to
302 // register commands and functions to call for them.
303 // The cmd_name is referenced later, so it should not be in temp memory
304 // if function is NULL, the command will be forwarded to the server
305 // as a clc_clientCommand instead of executed locally
306 
307 void	Cmd_RemoveCommand( const char *cmd_name );
308 typedef void (*completionFunc_t)( char *args, int argNum );
309 
310 void	Cmd_CommandCompletion( callbackFunc_t callback );
311 // callback with each valid string
312 void Cmd_SetCommandCompletionFunc( const char *command, completionFunc_t complete );
313 void Cmd_CompleteArgument( const char *command, char *args, int argNum );
314 void Cmd_CompleteCfgName( char *args, int argNum );
315 
316 int		Cmd_Argc (void);
317 char	*Cmd_Argv (int arg);
318 void	Cmd_ArgvBuffer( int arg, char *buffer, int bufferLength );
319 char	*Cmd_Args (void);
320 char	*Cmd_ArgsFrom( int arg );
321 void	Cmd_ArgsBuffer( char *buffer, int bufferLength );
322 // The functions that execute commands get their parameters with these
323 // functions. Cmd_Argv () will return an empty string, not a NULL
324 // if arg > argc, so string operations are allways safe.
325 
326 void	Cmd_TokenizeString( const char *text );
327 void	Cmd_TokenizeStringIgnoreQuotes( const char *text_in );
328 // Takes a null terminated string.  Does not need to be /n terminated.
329 // breaks the string up into arg tokens.
330 
331 void	Cmd_ExecuteString( const char *text );
332 // Parses a single line of text into arguments and tries to execute it
333 // as if it was typed at the console
334 
335 
336 /*
337 ==============================================================
338 
339 CVAR
340 
341 ==============================================================
342 */
343 
344 /*
345 
346 cvar_t variables are used to hold scalar or string variables that can be changed
347 or displayed at the console or prog code as well as accessed directly
348 in C code.
349 
350 The user can access cvars from the console in three ways:
351 r_draworder			prints the current value
352 r_draworder 0		sets the current value to 0
353 set r_draworder 0	as above, but creates the cvar if not present
354 
355 Cvars are restricted from having the same names as commands to keep this
356 interface from being ambiguous.
357 
358 The are also occasionally used to communicated information between different
359 modules of the program.
360 
361 */
362 
363 cvar_t *Cvar_Get( const char *var_name, const char *value, int flags );
364 // creates the variable if it doesn't exist, or returns the existing one
365 // if it exists, the value will not be changed, but flags will be ORed in
366 // that allows variables to be unarchived without needing bitflags
367 // if value is "", the value will not override a previously set value.
368 
369 void	Cvar_Register( vmCvar_t *vmCvar, const char *varName, const char *defaultValue, int flags );
370 // basically a slightly modified Cvar_Get for the interpreted modules
371 
372 void	Cvar_Update( vmCvar_t *vmCvar );
373 // updates an interpreted modules' version of a cvar
374 
375 void 	Cvar_Set( const char *var_name, const char *value );
376 // will create the variable with no flags if it doesn't exist
377 
378 cvar_t	*Cvar_Set2(const char *var_name, const char *value, qboolean force);
379 // same as Cvar_Set, but allows more control over setting of cvar
380 
381 void	Cvar_SetValue( const char *var_name, float value );
382 // expands value to a string and calls Cvar_Set
383 
384 float	Cvar_VariableValue( const char *var_name );
385 int		Cvar_VariableIntegerValue( const char *var_name );
386 // returns 0 if not defined or non numeric
387 
388 char	*Cvar_VariableString( const char *var_name );
389 void	Cvar_VariableStringBuffer( const char *var_name, char *buffer, int bufsize );
390 // returns an empty string if not defined
391 
392 int	Cvar_Flags(const char *var_name);
393 // returns CVAR_NONEXISTENT if cvar doesn't exist or the flags of that particular CVAR.
394 
395 void	Cvar_CommandCompletion( callbackFunc_t callback );
396 // callback with each valid string
397 
398 void 	Cvar_Reset( const char *var_name );
399 void 	Cvar_ForceReset( const char *var_name );
400 
401 void	Cvar_SetCheatState( void );
402 // reset all testing vars to a safe value
403 
404 qboolean Cvar_Command( void );
405 // called by Cmd_ExecuteString when Cmd_Argv(0) doesn't match a known
406 // command.  Returns true if the command was a variable reference that
407 // was handled. (print or change)
408 
409 void 	Cvar_WriteVariables( fileHandle_t f );
410 // writes lines containing "set variable value" for all variables
411 // with the archive flag set to true.
412 
413 void	Cvar_Init( void );
414 
415 char	*Cvar_InfoString( int bit );
416 // returns an info string containing all the cvars that have the given bit set
417 // in their flags ( CVAR_USERINFO, CVAR_SERVERINFO, CVAR_SYSTEMINFO, etc )
418 void	Cvar_InfoStringBuffer( int bit, char *buff, int buffsize );
419 void Cvar_CheckRange( cvar_t *cv, float minVal, float maxVal, qboolean shouldBeIntegral );
420 
421 void	Cvar_Restart(qboolean unsetVM);
422 void	Cvar_Restart_f( void );
423 
424 void Cvar_CompleteCvarName( char *args, int argNum );
425 
426 extern	int			cvar_modifiedFlags;
427 // whenever a cvar is modifed, its flags will be OR'd into this, so
428 // a single check can determine if any CVAR_USERINFO, CVAR_SERVERINFO,
429 // etc, variables have been modified since the last check.  The bit
430 // can then be cleared to allow another change detection.
431 
432 /*
433 ==============================================================
434 
435 FILESYSTEM
436 
437 No stdio calls should be used by any part of the game, because
438 we need to deal with all sorts of directory and seperator char
439 issues.
440 ==============================================================
441 */
442 
443 #define	MAX_FILE_HANDLES	64
444 
445 qboolean FS_Initialized();
446 
447 void	FS_InitFilesystem (void);
448 void	FS_Shutdown( void );
449 
450 qboolean FS_ConditionalRestart( void );
451 
452 char	**FS_ListFiles( const char *directory, const char *extension, int *numfiles );
453 // directory should not have either a leading or trailing /
454 // if extension is "/", only subdirectories will be returned
455 // the returned files will not include any directories or /
456 
457 void	FS_FreeFileList( char **filelist );
458 //rwwRMG - changed to fileList to not conflict with list type
459 
460 void FS_Remove( const char *osPath );
461 void FS_HomeRemove( const char *homePath );
462 
463 void FS_Rmdir( const char *osPath, qboolean recursive );
464 void FS_HomeRmdir( const char *homePath, qboolean recursive );
465 
466 qboolean FS_FileExists( const char *file );
467 
468 char   *FS_BuildOSPath( const char *base, const char *game, const char *qpath );
469 
470 int	FS_GetFileList(  const char *path, const char *extension, char *listbuf, int bufsize );
471 int		FS_GetModList(  char *listbuf, int bufsize );
472 
473 // will properly create any needed paths and deal with seperater character issues
474 fileHandle_t FS_FOpenFileWrite( const char *qpath, qboolean safe = qtrue );
475 
476 fileHandle_t FS_FOpenFileAppend( const char *filename );	// this was present already, but no public proto
477 
478 int		FS_filelength( fileHandle_t f );
479 fileHandle_t FS_SV_FOpenFileWrite( const char *filename );
480 int		FS_SV_FOpenFileRead( const char *filename, fileHandle_t *fp );
481 void	FS_SV_Rename( const char *from, const char *to, qboolean safe );
482 long		FS_FOpenFileRead( const char *qpath, fileHandle_t *file, qboolean uniqueFILE );
483 // if uniqueFILE is true, then a new FILE will be fopened even if the file
484 // is found in an already open pak file.  If uniqueFILE is false, you must call
485 // FS_FCloseFile instead of fclose, otherwise the pak FILE would be improperly closed
486 // It is generally safe to always set uniqueFILE to true, because the majority of
487 // file IO goes through FS_ReadFile, which Does The Right Thing already.
488 
489 // returns 1 if a file is in the PAK file, otherwise -1
490 int	FS_FileIsInPAK(const char *filename );
FS_FileIsInPAK(const char * filename,int * checksum)491 static inline int FS_FileIsInPAK( const char *filename, int *checksum )
492 {
493 	return FS_FileIsInPAK( filename );
494 }
495 
496 int	FS_Write( const void *buffer, int len, fileHandle_t f );
497 
498 int	FS_Read( void *buffer, int len, fileHandle_t f );
499 // properly handles partial reads and reads from other dlls
500 
501 void	FS_FCloseFile( fileHandle_t f );
502 // note: you can't just fclose from another DLL, due to MS libc issues
503 
504 long		FS_ReadFile( const char *qpath, void **buffer );
505 // returns the length of the file
506 // a null buffer will just return the file length without loading
507 // as a quick check for existance. -1 length == not present
508 // A 0 byte will always be appended at the end, so string ops are safe.
509 // the buffer should be considered read-only, because it may be cached
510 // for other uses.
511 
512 void	FS_ForceFlush( fileHandle_t f );
513 // forces flush on files we're writing to.
514 
515 void	FS_FreeFile( void *buffer );
516 // frees the memory returned by FS_ReadFile
517 
518 void	FS_WriteFile( const char *qpath, const void *buffer, int size );
519 // writes a complete file, creating any subdirectories needed
520 
521 int		FS_filelength( fileHandle_t f );
522 // doesn't work for files that are opened from a pack file
523 
524 int		FS_FTell( fileHandle_t f );
525 // where are we?
526 
527 void	FS_Flush( fileHandle_t f );
528 
529 void	FS_FilenameCompletion( const char *dir, const char *ext, qboolean stripExt, callbackFunc_t callback, qboolean allowNonPureFilesOnDisk );
530 
531 const char *FS_GetCurrentGameDir(bool emptybase=false);
532 
533 void 	QDECL FS_Printf( fileHandle_t f, const char *fmt, ... );
534 // like fprintf
535 
536 int		FS_FOpenFileByMode( const char *qpath, fileHandle_t *f, fsMode_t mode );
537 // opens a file for reading, writing, or appending depending on the value of mode
538 
539 int		FS_Seek( fileHandle_t f, long offset, int origin );
540 // seek on a file
541 
542 qboolean FS_FilenameCompare( const char *s1, const char *s2 );
543 
544 // These 2 are generally only used by the save games, filenames are local (eg "saves/blah.sav")
545 //
546 void		FS_DeleteUserGenFile( const char *filename );
547 qboolean	FS_MoveUserGenFile  ( const char *filename_src, const char *filename_dst );
548 
549 qboolean FS_CheckDirTraversal(const char *checkdir);
550 void FS_Rename( const char *from, const char *to );
551 
552 qboolean FS_WriteToTemporaryFile( const void *data, size_t dataLength, char **tempFileName );
553 
554 /*
555 ==============================================================
556 
557 Edit fields and command line history/completion
558 
559 ==============================================================
560 */
561 
562 #define CONSOLE_PROMPT_CHAR ']'
563 #define	MAX_EDIT_LINE		256
564 #define COMMAND_HISTORY		32
565 
566 typedef struct {
567 	int		cursor;
568 	int		scroll;
569 	int		widthInChars;
570 	char	buffer[MAX_EDIT_LINE];
571 } field_t;
572 
573 void Field_Clear( field_t *edit );
574 void Field_AutoComplete( field_t *edit );
575 void Field_CompleteKeyname( void );
576 void Field_CompleteFilename( const char *dir, const char *ext, qboolean stripExt, qboolean allowNonPureFilesOnDisk );
577 void Field_CompleteCommand( char *cmd, qboolean doCommands, qboolean doCvars );
578 
579 /*
580 ==============================================================
581 
582 MISC
583 
584 ==============================================================
585 */
586 
587 #define RoundUp(N, M) ((N) + ((unsigned int)(M)) - (((unsigned int)(N)) % ((unsigned int)(M))))
588 #define RoundDown(N, M) ((N) - (((unsigned int)(N)) % ((unsigned int)(M))))
589 
590 char		*CopyString( const char *in );
591 void		Info_Print( const char *s );
592 
593 void		Com_BeginRedirect (char *buffer, int buffersize, void (*flush)(char *));
594 void		Com_EndRedirect( void );
595 void 		QDECL Com_Printf( const char *fmt, ... );
596 void 		QDECL Com_DPrintf( const char *fmt, ... );
597 void 		NORETURN QDECL Com_Error( int code, const char *fmt, ... );
598 void 		NORETURN Com_Quit_f( void );
599 int			Com_EventLoop( void );
600 int			Com_Milliseconds( void );	// will be journaled properly
601 uint32_t	Com_BlockChecksum( const void *buffer, int length );
602 int			Com_Filter(const char *filter, const char *name, int casesensitive);
603 int			Com_FilterPath(const char *filter, const char *name, int casesensitive);
604 qboolean	Com_SafeMode( void );
605 void		Com_RunAndTimeServerPacket(netadr_t *evFrom, msg_t *buf);
606 
607 void		Com_StartupVariable( const char *match );
608 // checks for and removes command line "+set var arg" constructs
609 // if match is NULL, all set commands will be executed, otherwise
610 // only a set with the exact name.  Only used during startup.
611 
612 
613 extern	cvar_t	*com_developer;
614 extern	cvar_t	*com_speeds;
615 extern	cvar_t	*com_timescale;
616 extern	cvar_t	*com_sv_running;
617 extern	cvar_t	*com_cl_running;
618 extern	cvar_t	*com_version;
619 extern	cvar_t	*com_homepath;
620 #ifndef _WIN32
621 extern	cvar_t	*com_ansiColor;
622 #endif
623 
624 extern	cvar_t	*com_affinity;
625 extern	cvar_t	*com_busyWait;
626 
627 // both client and server must agree to pause
628 extern	cvar_t	*cl_paused;
629 extern	cvar_t	*sv_paused;
630 
631 // com_speeds times
632 extern	int		time_game;
633 extern	int		time_frontend;
634 extern	int		time_backend;		// renderer backend time
635 
636 extern	int		timeInTrace;
637 extern	int		timeInPVSCheck;
638 extern	int		numTraces;
639 
640 extern	int		com_frameTime;
641 
642 extern	qboolean	com_errorEntered;
643 
644 extern	fileHandle_t	com_journalFile;
645 extern	fileHandle_t	com_journalDataFile;
646 
647 /*
648 
649 --- low memory ----
650 server vm
651 server clipmap
652 ---mark---
653 renderer initialization (shaders, etc)
654 UI vm
655 cgame vm
656 renderer map
657 renderer models
658 
659 ---free---
660 
661 temp file loading
662 --- high memory ---
663 
664 */
665 int  Z_Validate( void );			// also used to insure all of these are paged in
666 int   Z_MemSize	( memtag_t eTag );
667 void  Z_TagFree	( memtag_t eTag );
668 int   Z_Free	( void *ptr );	//returns bytes freed
669 int	  Z_Size	( void *pvAddress);
670 void  Z_MorphMallocTag( void *pvAddress, memtag_t eDesiredTag );
671 qboolean Z_IsFromZone(const void *pvAddress, memtag_t eTag);	//returns size if true
672 
673 #ifdef DEBUG_ZONE_ALLOCS
674 
675 void *_D_Z_Malloc( int iSize, memtag_t eTag, qboolean bZeroit, const char *psFile, int iLine );
676 void *_D_S_Malloc( int iSize, const char *psFile, int iLine );
677 void  Z_Label( const void *pvAddress, const char *pslabel );
678 
679 #define Z_Malloc(iSize, eTag, bZeroit)	_D_Z_Malloc ((iSize), (eTag), (bZeroit), __FILE__, __LINE__)
680 #define S_Malloc(iSize)			_D_S_Malloc	((iSize), __FILE__, __LINE__)	// NOT 0 filled memory only for small allocations
681 
682 #else
683 
684 void *Z_Malloc( int iSize, memtag_t eTag, qboolean bZeroit = qfalse, int iAlign = 4);	// return memory NOT zero-filled by default
685 void *S_Malloc( int iSize );									// NOT 0 filled memory only for small allocations
686 #define Z_Label(_ptr, _label)
687 
688 #endif
689 
690 void Com_InitZoneMemory(void);
691 void Com_InitZoneMemoryVars(void);
692 
693 void Hunk_Clear( void );
694 void Hunk_ClearToMark( void );
695 void Hunk_SetMark( void );
696 // note the opposite default for 'bZeroIt' in Hunk_Alloc to Z_Malloc, since Hunk_Alloc always used to memset(0)...
697 //
698 inline void *Hunk_Alloc( int size, qboolean bZeroIt = qtrue);
699 
700 
701 void Com_TouchMemory( void );
702 
703 // commandLine should not include the executable name (argv[0])
704 void Com_SetOrgAngles(vec3_t org,vec3_t angles);
705 void Com_Init( char *commandLine );
706 void Com_Frame( void );
707 void Com_Shutdown( void );
708 void Com_ShutdownZoneMemory(void);
709 void Com_ShutdownHunkMemory(void);
710 
711 /*
712 ==============================================================
713 
714 CLIENT / SERVER SYSTEMS
715 
716 ==============================================================
717 */
718 
719 //
720 // client interface
721 //
722 void CL_InitKeyCommands( void );
723 // the keyboard binding interface must be setup before execing
724 // config files, but the rest of client startup will happen later
725 
726 void CL_Init( void );
727 void CL_Disconnect( void );
728 void CL_Shutdown( void );
729 void CL_Frame( int msec,float fractionMsec );
730 qboolean CL_GameCommand( void );
731 void CL_KeyEvent (int key, qboolean down, unsigned time);
732 
733 void CL_CharEvent( int key );
734 // char events are for field typing, not game control
735 
736 void CL_MouseEvent( int dx, int dy, int time );
737 
738 void CL_JoystickEvent( int axis, int value, int time );
739 
740 void CL_PacketEvent( netadr_t from, msg_t *msg );
741 
742 void CL_ConsolePrint( char *text );
743 
744 void CL_MapLoading( void );
745 // do a screen update before starting to load a map
746 // when the server is going to load a new map, the entire hunk
747 // will be cleared, so the client must shutdown cgame, ui, and
748 // the renderer
749 
750 void	CL_ForwardCommandToServer( void );
751 // adds the current command line as a clc_clientCommand to the client message.
752 // things like godmode, noclip, etc, are commands directed to the server,
753 // so when they are typed in at the console, they will need to be forwarded.
754 
755 void CL_FlushMemory( void );
756 // dump all memory on an error
757 
758 void CL_StartHunkUsers( void );
759 
760 void Key_KeynameCompletion ( callbackFunc_t callback );
761 // for keyname autocompletion
762 
763 void Key_WriteBindings( fileHandle_t f );
764 // for writing the config files
765 
766 void S_ClearSoundBuffer( void );
767 // call before filesystem access
768 
769 void SCR_DebugGraph (float value, int color);	// FIXME: move logging to common?
770 
771 
772 //
773 // server interface
774 //
775 void SV_Init( void );
776 void SV_Shutdown( const char *finalmsg);
777 void SV_Frame( int msec,float fractionMsec);
778 void SV_PacketEvent( netadr_t from, msg_t *msg );
779 qboolean SV_GameCommand( void );
780 
781 
782 //
783 // UI interface
784 //
785 qboolean UI_GameCommand( void );
786 
787 
788 byte*	SCR_GetScreenshot(qboolean *qValid);
789 #ifdef JK2_MODE
790 void	SCR_SetScreenshot(const byte *pbData, int w, int h);
791 byte*	SCR_TempRawImage_ReadFromFile(const char *psLocalFilename, int *piWidth, int *piHeight, byte *pbReSampleBuffer, qboolean qbVertFlip);
792 void	SCR_TempRawImage_CleanUp();
793 #endif
794 
Round(float value)795 inline int Round(float value)
796 {
797 	return((int)floorf(value + 0.5f));
798 }
799 
800 // Persistent data store API
801 bool PD_Store ( const char *name, const void *data, size_t size );
802 const void *PD_Load ( const char *name, size_t *size );
803 
804 uint32_t ConvertUTF8ToUTF32( char *utf8CurrentChar, char **utf8NextChar );
805 
806 #include "sys/sys_public.h"
807 
808 #endif //__QCOMMON_H__
809