Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MP] implement PC_AddGlobalDefine, PC_RemoveGlobalDefine when DEFINEHASHING is defined #1222

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions codemp/botlib/be_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -910,5 +910,7 @@ botlib_export_t *GetBotLibAPI(int apiVersion, botlib_import_t *import) {
be_botlib_export.BotLibUpdateEntity = Export_BotLibUpdateEntity;
be_botlib_export.Test = BotExportTest;

PC_Init();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little bit ugly to call it here, but necessary. Otherwise we'd have to drill it out further through other exported APIs and just call it from there 🤷
Let's just assume if you're getting any botlib exports, you want it to be ready for use.


return &be_botlib_export;
}
2 changes: 1 addition & 1 deletion codemp/botlib/botlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ typedef struct botlib_export_s
int (*BotLibVarGet)(char *var_name, char *value, int size);

//sets a C-like define returns BLERR_
int (*PC_AddGlobalDefine)(char *string);
int (*PC_AddGlobalDefine)(const char *string);
int (*PC_LoadSourceHandle)(const char *filename);
int (*PC_FreeSourceHandle)(int handle);
int (*PC_ReadTokenHandle)(int handle, pc_token_t *pc_token);
Expand Down
57 changes: 50 additions & 7 deletions codemp/botlib/l_precomp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ void QDECL SourceWarning(source_t *source, char *str, ...)
Log_Print("warning: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text);
#endif //BSPC
} //end of the function ScriptWarning

void PC_Init(void) {
// PC_InitTokenHeap();

#if DEFINEHASHING
if (!globaldefines) {
globaldefines = (struct define_s **)GetClearedMemory(DEFINEHASHSIZE * sizeof(define_t *));
}
#endif
}

//============================================================================
//
// Parameter: -
Expand Down Expand Up @@ -1130,7 +1141,9 @@ int PC_Directive_undef(source_t *source)
{
token_t token;
define_t *define, *lastdefine;
#if DEFINEHASHING
int hash;
#endif

if (source->skip > 0) return qtrue;
//
Expand Down Expand Up @@ -1341,12 +1354,15 @@ int PC_Directive_define(source_t *source)
// Returns: -
// Changes Globals: -
//============================================================================
define_t *PC_DefineFromString(char *string)
define_t *PC_DefineFromString(const char *string)
{
script_t *script;
source_t src;
token_t *t;
int res, i;
int res;
#if DEFINEHASHING
int i;
#endif // DEFINEHASHING
define_t *def;

PC_InitTokenHeap();
Expand All @@ -1367,7 +1383,7 @@ define_t *PC_DefineFromString(char *string)
src.tokens = src.tokens->next;
PC_FreeToken(t);
} //end for
#ifdef DEFINEHASHING
#if DEFINEHASHING
def = NULL;
for (i = 0; i < DEFINEHASHSIZE; i++)
{
Expand Down Expand Up @@ -1399,7 +1415,7 @@ define_t *PC_DefineFromString(char *string)
// Returns: -
// Changes Globals: -
//============================================================================
int PC_AddDefine(source_t *source, char *string)
int PC_AddDefine(source_t *source, const char *string)
{
define_t *define;

Expand All @@ -1425,9 +1441,19 @@ int PC_AddDefine(source_t *source, char *string)
// Returns: -
// Changes Globals: -
//============================================================================
int PC_AddGlobalDefine(char *string)
int PC_AddGlobalDefine(const char *string)
{
#if !DEFINEHASHING
#if DEFINEHASHING
define_t *define = PC_DefineFromString(string);
if (!define) {
return qfalse;
}

qboolean savedAGD = addGlobalDefine;
addGlobalDefine = qtrue;
PC_AddDefineToHash(define, NULL);
addGlobalDefine = savedAGD;
#else
define_t *define;

define = PC_DefineFromString(string);
Expand All @@ -1446,7 +1472,22 @@ int PC_AddGlobalDefine(char *string)
//============================================================================
int PC_RemoveGlobalDefine(char *name)
{
#if !DEFINEHASHING
#if DEFINEHASHING
if (globaldefines) {
int i;
for (i = 0; i < DEFINEHASHSIZE; i++) {
define_t *define;
for (define = globaldefines[i]; define; define = define->globalnext) {
if (strcmp(define->name, name)) {
globaldefines[i] = define->globalnext;
PC_FreeDefine(define);
return qtrue;
}
}
}
}
return qfalse;
#else
define_t *define;

define = PC_FindDefine(globaldefines, name);
Expand Down Expand Up @@ -3143,8 +3184,10 @@ void FreeSource(source_t *source)
token_t *token;
define_t *define;
indent_t *indent;
#if DEFINEHASHING
define_t *nextdefine;
int i;
#endif

//PC_PrintDefineHashTable(source->definehash);
//free all the scripts
Expand Down
7 changes: 4 additions & 3 deletions codemp/botlib/l_precomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ typedef struct source_s
token_t token; //last read token
} source_t;


// initialise the precompiler
void PC_Init(void);
//read a token from the source
int PC_ReadToken(source_t *source, token_t *token);
//expect a certain token
Expand All @@ -139,9 +140,9 @@ int PC_ReadLine(source_t *source, token_t *token);
//returns true if there was a white space in front of the token
int PC_WhiteSpaceBeforeToken(token_t *token);
//add a define to the source
int PC_AddDefine(source_t *source, char *string);
int PC_AddDefine(source_t *source, const char *string);
//add a globals define that will be added to all opened sources
int PC_AddGlobalDefine(char *string);
int PC_AddGlobalDefine(const char *string);
//remove the given global define
int PC_RemoveGlobalDefine(char *name);
//remove all globals defines
Expand Down
2 changes: 1 addition & 1 deletion codemp/botlib/l_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1399,7 +1399,7 @@ script_t *LoadScriptFile(const char *filename)
// Returns: -
// Changes Globals: -
//============================================================================
script_t *LoadScriptMemory(char *ptr, int length, char *name)
script_t *LoadScriptMemory(const char *ptr, int length, char *name)
{
void *buffer;
script_t *script;
Expand Down
2 changes: 1 addition & 1 deletion codemp/botlib/l_script.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ const char *PunctuationFromNum(script_t *script, int num);
//load a script from the given file at the given offset with the given length
script_t *LoadScriptFile(const char *filename);
//load a script from the given memory with the given length
script_t *LoadScriptMemory(char *ptr, int length, char *name);
script_t *LoadScriptMemory(const char *ptr, int length, char *name);
//free a script
void FreeScript(script_t *script);
//set the base folder to load files from
Expand Down
2 changes: 1 addition & 1 deletion codemp/cgame/cg_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ typedef struct cgameImport_s {
void (*Key_SetCatcher) ( int catcher );

// preprocessor (botlib_export->PC_***)
int (*PC_AddGlobalDefine) ( char *string );
int (*PC_AddGlobalDefine) ( const char *string );
int (*PC_FreeSource) ( int handle );
int (*PC_LoadGlobalDefines) ( const char *filename );
int (*PC_LoadSource) ( const char *filename );
Expand Down
2 changes: 1 addition & 1 deletion codemp/cgame/cg_syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ void trap_Key_SetCatcher( int catcher ) {
int trap_Key_GetKey( const char *binding ) {
return Q_syscall( CG_KEY_GETKEY, binding );
}
int trap_PC_AddGlobalDefine( char *define ) {
int trap_PC_AddGlobalDefine( const char *define ) {
return Q_syscall( CG_PC_ADD_GLOBAL_DEFINE, define );
}
int trap_PC_LoadSource( const char *filename ) {
Expand Down
2 changes: 1 addition & 1 deletion codemp/client/cl_cgameapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ intptr_t CL_CgameSystemCalls( intptr_t *args ) {
return Key_GetKey( (const char *)VMA(1) );

case CG_PC_ADD_GLOBAL_DEFINE:
return botlib_export->PC_AddGlobalDefine( (char *)VMA(1) );
return botlib_export->PC_AddGlobalDefine( (const char *)VMA(1) );

case CG_PC_LOAD_SOURCE:
return botlib_export->PC_LoadSourceHandle( (const char *)VMA(1) );
Expand Down
2 changes: 1 addition & 1 deletion codemp/client/cl_uiapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ intptr_t CL_UISystemCalls( intptr_t *args ) {
return re->AnyLanguage_ReadCharFromString( (const char *)VMA(1), (int *) VMA(2), (qboolean *) VMA(3) );

case UI_PC_ADD_GLOBAL_DEFINE:
return botlib_export->PC_AddGlobalDefine( (char *)VMA(1) );
return botlib_export->PC_AddGlobalDefine( (const char *)VMA(1) );

case UI_PC_LOAD_SOURCE:
return botlib_export->PC_LoadSourceHandle( (const char *)VMA(1) );
Expand Down
2 changes: 1 addition & 1 deletion codemp/game/g_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ typedef struct gameImport_s {
int (*BotLibShutdown) ( void );
int (*BotLibVarSet) ( char *var_name, char *value );
int (*BotLibVarGet) ( char *var_name, char *value, int size );
int (*BotLibDefine) ( char *string );
int (*BotLibDefine) ( const char *string );
int (*BotLibStartFrame) ( float time );
int (*BotLibLoadMap) ( const char *mapname );
int (*BotLibUpdateEntity) ( int ent, void *bue );
Expand Down
2 changes: 1 addition & 1 deletion codemp/game/g_syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ int trap_BotLibVarSet(char *var_name, char *value) {
int trap_BotLibVarGet(char *var_name, char *value, int size) {
return Q_syscall( BOTLIB_LIBVAR_GET, var_name, value, size );
}
int trap_BotLibDefine(char *string) {
int trap_BotLibDefine(const char *string) {
return Q_syscall( BOTLIB_PC_ADD_GLOBAL_DEFINE, string );
}
int trap_BotLibStartFrame(float time) {
Expand Down
4 changes: 2 additions & 2 deletions codemp/server/sv_gameapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ static int SV_BotLibVarGet( char *var_name, char *value, int size ) {
return botlib_export->BotLibVarGet( var_name, value, size );
}

static int SV_BotLibDefine( char *string ) {
static int SV_BotLibDefine( const char *string ) {
return botlib_export->PC_AddGlobalDefine( string );
}

Expand Down Expand Up @@ -2226,7 +2226,7 @@ intptr_t SV_GameSystemCalls( intptr_t *args ) {
return botlib_export->BotLibVarGet( (char *)VMA(1), (char *)VMA(2), args[3] );

case BOTLIB_PC_ADD_GLOBAL_DEFINE:
return botlib_export->PC_AddGlobalDefine( (char *)VMA(1) );
return botlib_export->PC_AddGlobalDefine( (const char *)VMA(1) );
case BOTLIB_PC_LOAD_SOURCE:
return botlib_export->PC_LoadSourceHandle( (const char *)VMA(1) );
case BOTLIB_PC_FREE_SOURCE:
Expand Down
2 changes: 1 addition & 1 deletion codemp/ui/ui_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ typedef struct uiImport_s {
void (*Key_SetCatcher) ( int catcher );
void (*Key_SetOverstrikeMode) ( qboolean state );

int (*PC_AddGlobalDefine) ( char *define );
int (*PC_AddGlobalDefine) ( const char *define );
int (*PC_FreeSource) ( int handle );
int (*PC_LoadGlobalDefines) ( const char* filename );
int (*PC_LoadSource) ( const char *filename );
Expand Down
2 changes: 1 addition & 1 deletion codemp/ui/ui_syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ int trap_LAN_CompareServers( int source, int sortKey, int sortDir, int s1, int s
int trap_MemoryRemaining( void ) {
return Q_syscall( UI_MEMORY_REMAINING );
}
int trap_PC_AddGlobalDefine( char *define ) {
int trap_PC_AddGlobalDefine( const char *define ) {
return Q_syscall( UI_PC_ADD_GLOBAL_DEFINE, define );
}
int trap_PC_LoadSource( const char *filename ) {
Expand Down
Loading