2008-06-07 22:00:00 +02:00
|
|
|
|
//=======================================================================
|
|
|
|
|
// Copyright XashXT Group 2008 <20>
|
2009-11-30 22:00:00 +01:00
|
|
|
|
// engfuncs.c - misc functions used by dlls'
|
2008-06-07 22:00:00 +02:00
|
|
|
|
//=======================================================================
|
|
|
|
|
|
2008-06-09 22:00:00 +02:00
|
|
|
|
#include "common.h"
|
2008-07-23 22:00:00 +02:00
|
|
|
|
#include "byteorder.h"
|
2008-06-08 22:00:00 +02:00
|
|
|
|
#include "mathlib.h"
|
2008-08-05 22:00:00 +02:00
|
|
|
|
#include "const.h"
|
2008-06-29 22:00:00 +02:00
|
|
|
|
#include "client.h"
|
2009-01-22 22:00:00 +01:00
|
|
|
|
#include "cvardef.h"
|
2008-06-08 22:00:00 +02:00
|
|
|
|
|
2010-08-12 22:00:00 +02:00
|
|
|
|
/*
|
|
|
|
|
====================
|
|
|
|
|
Cache_Check
|
|
|
|
|
|
|
|
|
|
consistency check
|
|
|
|
|
====================
|
|
|
|
|
*/
|
|
|
|
|
void *Cache_Check( byte *mempool, cache_user_t *c )
|
|
|
|
|
{
|
|
|
|
|
if( !c->data )
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
if( !Mem_IsAllocated( mempool, c->data ))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
return c->data;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-10 22:00:00 +01:00
|
|
|
|
/*
|
|
|
|
|
==============
|
|
|
|
|
pfnParseToken
|
|
|
|
|
|
|
|
|
|
simple dlls version
|
|
|
|
|
==============
|
|
|
|
|
*/
|
|
|
|
|
char *pfnParseToken( const char **data_p )
|
|
|
|
|
{
|
2009-11-30 22:00:00 +01:00
|
|
|
|
int c, len = 0;
|
2009-11-10 22:00:00 +01:00
|
|
|
|
const char *data;
|
|
|
|
|
static char token[8192];
|
|
|
|
|
|
|
|
|
|
token[0] = 0;
|
|
|
|
|
data = *data_p;
|
|
|
|
|
|
|
|
|
|
if( !data )
|
|
|
|
|
{
|
|
|
|
|
*data_p = NULL;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// skip whitespace
|
|
|
|
|
skipwhite:
|
|
|
|
|
while(( c = *data) <= ' ' )
|
|
|
|
|
{
|
|
|
|
|
if( c == 0 )
|
|
|
|
|
{
|
|
|
|
|
*data_p = NULL;
|
|
|
|
|
return NULL; // end of file;
|
|
|
|
|
}
|
|
|
|
|
data++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// skip // comments
|
|
|
|
|
if( c=='/' && data[1] == '/' )
|
|
|
|
|
{
|
|
|
|
|
while( *data && *data != '\n' )
|
|
|
|
|
data++;
|
|
|
|
|
goto skipwhite;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// skip /* comments
|
|
|
|
|
if( c=='/' && data[1] == '*' )
|
|
|
|
|
{
|
|
|
|
|
while( data[1] && (data[0] != '*' || data[1] != '/' ))
|
|
|
|
|
data++;
|
|
|
|
|
data += 2;
|
|
|
|
|
goto skipwhite;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// handle quoted strings specially
|
|
|
|
|
if( *data == '\"' || *data == '\'' )
|
|
|
|
|
{
|
|
|
|
|
data++;
|
|
|
|
|
while( 1 )
|
|
|
|
|
{
|
|
|
|
|
c = *data++;
|
|
|
|
|
if( c=='\"' || c=='\0' )
|
|
|
|
|
{
|
|
|
|
|
token[len] = 0;
|
|
|
|
|
*data_p = data;
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
token[len] = c;
|
|
|
|
|
len++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// parse single characters
|
|
|
|
|
if( c == '{' || c == '}' || c == ')' || c == '(' || c == '\'' || c == ':' || c == ',' )
|
|
|
|
|
{
|
|
|
|
|
token[len] = c;
|
|
|
|
|
data++;
|
|
|
|
|
len++;
|
|
|
|
|
token[len] = 0;
|
|
|
|
|
*data_p = data;
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// parse a regular word
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
token[len] = c;
|
|
|
|
|
data++;
|
|
|
|
|
len++;
|
|
|
|
|
c = *data;
|
|
|
|
|
if( c == '{' || c == '}'|| c == ')'|| c == '(' || c == '\'' || c == ':' || c == ',' )
|
|
|
|
|
break;
|
|
|
|
|
} while( c > 32 );
|
|
|
|
|
|
|
|
|
|
token[len] = 0;
|
|
|
|
|
*data_p = data;
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnMemFgets
|
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
char *pfnMemFgets( byte *pMemFile, int fileSize, int *filePos, char *pBuffer, int bufferSize )
|
|
|
|
|
{
|
|
|
|
|
int i, last, stop;
|
|
|
|
|
|
|
|
|
|
if( !pMemFile || !pBuffer || !filePos )
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
if( *filePos >= fileSize )
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
i = *filePos;
|
|
|
|
|
last = fileSize;
|
|
|
|
|
|
|
|
|
|
// fgets always NULL terminates, so only read bufferSize-1 characters
|
|
|
|
|
if( last - *filePos > ( bufferSize - 1 ))
|
|
|
|
|
last = *filePos + ( bufferSize - 1);
|
|
|
|
|
|
|
|
|
|
stop = 0;
|
|
|
|
|
|
|
|
|
|
// stop at the next newline (inclusive) or end of buffer
|
|
|
|
|
while( i < last && !stop )
|
|
|
|
|
{
|
|
|
|
|
if( pMemFile[i] == '\n' )
|
|
|
|
|
stop = 1;
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if we actually advanced the pointer, copy it over
|
|
|
|
|
if( i != *filePos )
|
|
|
|
|
{
|
|
|
|
|
// we read in size bytes
|
|
|
|
|
int size = i - *filePos;
|
|
|
|
|
|
|
|
|
|
// copy it out
|
|
|
|
|
Mem_Copy( pBuffer, pMemFile + *filePos, size );
|
|
|
|
|
|
|
|
|
|
// If the buffer isn't full, terminate (this is always true)
|
|
|
|
|
if( size < bufferSize ) pBuffer[size] = 0;
|
|
|
|
|
|
|
|
|
|
// update file pointer
|
|
|
|
|
*filePos = i;
|
|
|
|
|
return pBuffer;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-25 22:00:00 +01:00
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnLoadFile
|
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
byte* pfnLoadFile( const char *filename, int *pLength )
|
|
|
|
|
{
|
|
|
|
|
return FS_LoadFile( filename, pLength );
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-22 22:00:00 +02:00
|
|
|
|
void pfnFreeFile( void *buffer )
|
|
|
|
|
{
|
|
|
|
|
if( buffer ) Mem_Free( buffer );
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-01 22:00:00 +01:00
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnFileExists
|
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
int pfnFileExists( const char *filename )
|
|
|
|
|
{
|
|
|
|
|
return FS_FileExists( filename );
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-25 22:00:00 +01:00
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnRandomLong
|
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
long pfnRandomLong( long lLow, long lHigh )
|
|
|
|
|
{
|
|
|
|
|
return Com_RandomLong( lLow, lHigh );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnRandomFloat
|
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
float pfnRandomFloat( float flLow, float flHigh )
|
|
|
|
|
{
|
|
|
|
|
return Com_RandomFloat( flLow, flHigh );
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-22 22:00:00 +02:00
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnTime
|
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
float pfnTime( void )
|
|
|
|
|
{
|
2010-06-17 22:00:00 +02:00
|
|
|
|
return Sys_DoubleTime();
|
2009-09-22 22:00:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2009-01-22 22:00:00 +01:00
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnCVarRegister
|
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
cvar_t *pfnCVarRegister( const char *szName, const char *szValue, int flags, const char *szDesc )
|
|
|
|
|
{
|
|
|
|
|
int real_flags = 0;
|
|
|
|
|
|
|
|
|
|
if( flags & FCVAR_ARCHIVE ) real_flags |= CVAR_ARCHIVE;
|
|
|
|
|
if( flags & FCVAR_USERINFO ) real_flags |= CVAR_USERINFO;
|
2010-08-15 22:00:00 +02:00
|
|
|
|
if( flags & FCVAR_SERVER ) real_flags |= CVAR_SERVERINFO;
|
|
|
|
|
if( flags & FCVAR_SPONLY ) real_flags |= CVAR_CHEAT;
|
|
|
|
|
if( flags & FCVAR_PRINTABLEONLY ) real_flags |= CVAR_PRINTABLEONLY;
|
2009-01-22 22:00:00 +01:00
|
|
|
|
|
|
|
|
|
return Cvar_Get( szName, szValue, real_flags, szDesc );
|
|
|
|
|
}
|
2009-12-04 22:00:00 +01:00
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnCVarSetString
|
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
void pfnCVarSetString( const char *szName, const char *szValue )
|
|
|
|
|
{
|
|
|
|
|
Cvar_Set( szName, szValue );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnCVarSetValue
|
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
void pfnCVarSetValue( const char *szName, float flValue )
|
|
|
|
|
{
|
|
|
|
|
Cvar_SetValue( szName, flValue );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnCVarGetString
|
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
char* pfnCVarGetString( const char *szName )
|
|
|
|
|
{
|
|
|
|
|
return Cvar_VariableString( szName );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnCVarGetValue
|
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
float pfnCVarGetValue( const char *szName )
|
|
|
|
|
{
|
|
|
|
|
return Cvar_VariableValue( szName );
|
|
|
|
|
}
|
2009-01-22 22:00:00 +01:00
|
|
|
|
|
2010-08-15 22:00:00 +02:00
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnCVarGetPointer
|
|
|
|
|
|
|
|
|
|
can return NULL
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
cvar_t *pfnCVarGetPointer( const char *szVarName )
|
|
|
|
|
{
|
|
|
|
|
return Cvar_FindVar( szVarName );
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-18 22:00:00 +02:00
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnAddCommand
|
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
void pfnAddCommand( const char *cmd_name, xcommand_t func, const char *cmd_desc )
|
|
|
|
|
{
|
|
|
|
|
if( !cmd_name || !*cmd_name ) return;
|
|
|
|
|
if( !cmd_desc ) cmd_desc = ""; // hidden for makehelep system
|
|
|
|
|
|
|
|
|
|
// NOTE: if( func == NULL ) cmd will be forwarded to a server
|
|
|
|
|
Cmd_AddCommand( cmd_name, func, cmd_desc );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnDelCommand
|
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
void pfnDelCommand( const char *cmd_name )
|
|
|
|
|
{
|
|
|
|
|
if( !cmd_name || !*cmd_name ) return;
|
|
|
|
|
|
|
|
|
|
Cmd_RemoveCommand( cmd_name );
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-11 22:00:00 +01:00
|
|
|
|
/*
|
|
|
|
|
=============
|
2009-12-05 22:00:00 +01:00
|
|
|
|
pfnCmd_Args
|
2009-01-11 22:00:00 +01:00
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
2009-12-05 22:00:00 +01:00
|
|
|
|
const char *pfnCmd_Args( void )
|
2009-01-11 22:00:00 +01:00
|
|
|
|
{
|
2009-12-05 22:00:00 +01:00
|
|
|
|
return Cmd_Args();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnCmd_Argv
|
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
const char *pfnCmd_Argv( int argc )
|
|
|
|
|
{
|
|
|
|
|
if( argc >= 0 && argc < Cmd_Argc())
|
|
|
|
|
return Cmd_Argv( argc );
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnCmd_Argc
|
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
int pfnCmd_Argc( void )
|
|
|
|
|
{
|
|
|
|
|
return Cmd_Argc();
|
2009-01-11 22:00:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-08-15 22:00:00 +02:00
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnCon_Printf
|
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
void pfnCon_Printf( char *szFmt, ... )
|
|
|
|
|
{
|
|
|
|
|
char buffer[2048]; // must support > 1k messages
|
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
|
|
va_start( args, szFmt );
|
|
|
|
|
com.vsnprintf( buffer, 2048, szFmt, args );
|
|
|
|
|
va_end( args );
|
|
|
|
|
|
|
|
|
|
com.print( buffer );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnCon_DPrintf
|
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
void pfnCon_DPrintf( char *szFmt, ... )
|
|
|
|
|
{
|
|
|
|
|
char buffer[2048]; // must support > 1k messages
|
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
|
|
if( !host.developer ) return;
|
|
|
|
|
|
|
|
|
|
va_start( args, szFmt );
|
|
|
|
|
com.vsnprintf( buffer, 2048, szFmt, args );
|
|
|
|
|
va_end( args );
|
|
|
|
|
|
|
|
|
|
com.print( buffer );
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-23 22:00:00 +01:00
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnLoadLibrary
|
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
void *pfnLoadLibrary( const char *name )
|
|
|
|
|
{
|
2010-03-27 22:00:00 +01:00
|
|
|
|
return FS_LoadLibrary( name, false );
|
2009-11-23 22:00:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnGetProcAddress
|
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
void *pfnGetProcAddress( void *hInstance, const char *name )
|
|
|
|
|
{
|
2010-03-27 22:00:00 +01:00
|
|
|
|
return FS_GetProcAddress( hInstance, name );
|
2009-11-23 22:00:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
=============
|
|
|
|
|
pfnFreeLibrary
|
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
|
|
|
|
void pfnFreeLibrary( void *hInstance )
|
|
|
|
|
{
|
2010-03-27 22:00:00 +01:00
|
|
|
|
FS_FreeLibrary( hInstance );
|
2009-11-23 22:00:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-25 22:00:00 +01:00
|
|
|
|
/*
|
|
|
|
|
=============
|
2010-04-09 22:00:00 +02:00
|
|
|
|
pfnGetGameDir
|
2008-12-25 22:00:00 +01:00
|
|
|
|
|
|
|
|
|
=============
|
|
|
|
|
*/
|
2010-04-09 22:00:00 +02:00
|
|
|
|
void pfnGetGameDir( char *szGetGameDir )
|
2008-12-25 22:00:00 +01:00
|
|
|
|
{
|
2010-04-09 22:00:00 +02:00
|
|
|
|
if( !szGetGameDir ) return;
|
|
|
|
|
com.strcpy( szGetGameDir, GI->gamedir );
|
2009-11-02 22:00:00 +01:00
|
|
|
|
}
|