This repository has been archived on 2022-06-27. You can view files and clone it, but cannot push or open issues or pull requests.
Xash3DArchive/engine/common/engfuncs.c

447 lines
7.0 KiB
C
Raw Normal View History

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
2009-11-10 22:00:00 +01:00
/*
==============
2010-08-20 22:00:00 +02:00
COM_ParseFile
2009-11-10 22:00:00 +01:00
simple dlls version
==============
*/
2010-08-20 22:00:00 +02:00
char *COM_ParseFile( char *data, char *token )
2009-11-10 22:00:00 +01:00
{
2010-08-20 22:00:00 +02:00
int c, len;
if( !token )
return NULL;
2009-11-10 22:00:00 +01:00
2010-08-20 22:00:00 +02:00
len = 0;
2009-11-10 22:00:00 +01:00
token[0] = 0;
2010-08-20 22:00:00 +02:00
if( !data )
2009-11-10 22:00:00 +01:00
return NULL;
2010-08-20 22:00:00 +02:00
// skip whitespace
2009-11-10 22:00:00 +01:00
skipwhite:
2010-08-20 22:00:00 +02:00
while(( c = *data ) <= ' ' )
2009-11-10 22:00:00 +01:00
{
if( c == 0 )
2010-08-20 22:00:00 +02:00
return NULL; // end of file;
2009-11-10 22:00:00 +01:00
data++;
}
// skip // comments
if( c=='/' && data[1] == '/' )
{
while( *data && *data != '\n' )
data++;
goto skipwhite;
}
// handle quoted strings specially
2010-08-20 22:00:00 +02:00
if( c == '\"' )
2009-11-10 22:00:00 +01:00
{
data++;
while( 1 )
{
c = *data++;
2010-08-20 22:00:00 +02:00
if( c == '\"' || !c )
2009-11-10 22:00:00 +01:00
{
token[len] = 0;
2010-08-20 22:00:00 +02:00
return data;
2009-11-10 22:00:00 +01:00
}
token[len] = c;
len++;
}
}
// parse single characters
2010-08-20 22:00:00 +02:00
if( c == '{' || c == '}'|| c == ')'|| c == '(' || c == '\'' || c == ',' )
2009-11-10 22:00:00 +01:00
{
token[len] = c;
len++;
token[len] = 0;
2010-08-20 22:00:00 +02:00
return data + 1;
2009-11-10 22:00:00 +01:00
}
// parse a regular word
do
{
token[len] = c;
data++;
len++;
c = *data;
2010-08-20 22:00:00 +02:00
if( c == '{' || c == '}' || c == ')' || c == '(' || c == '\'' || c == ',' )
2009-11-10 22:00:00 +01:00
break;
} while( c > 32 );
token[len] = 0;
2010-08-20 22:00:00 +02:00
return data;
2009-11-10 22:00:00 +01:00
}
/*
=============
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;
}
2010-08-20 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;
}
2008-12-25 22:00:00 +01:00
/*
=============
pfnLoadFile
=============
*/
byte* pfnLoadFile( const char *filename, int *pLength )
{
2010-08-18 22:00:00 +02:00
if( !filename || !*filename )
{
if( pLength ) *pLength = 0;
return NULL;
}
2008-12-25 22:00:00 +01:00
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_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
/*
=============
2010-08-20 22:00:00 +02:00
Con_Printf
2010-08-15 22:00:00 +02:00
=============
*/
2010-08-20 22:00:00 +02:00
void Con_Printf( char *szFmt, ... )
2010-08-15 22:00:00 +02:00
{
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 );
}
/*
=============
2010-08-20 22:00:00 +02:00
Con_DPrintf
2010-08-15 22:00:00 +02:00
=============
*/
2010-08-20 22:00:00 +02:00
void Con_DPrintf( char *szFmt, ... )
2010-08-15 22:00:00 +02:00
{
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
}