Skip to content

Commit

Permalink
Added CommandLink and Callback system from idTech5
Browse files Browse the repository at this point in the history
  • Loading branch information
FriskTheFallenHuman committed Aug 30, 2024
1 parent 6829b9c commit ec5b679
Show file tree
Hide file tree
Showing 14 changed files with 505 additions and 177 deletions.
33 changes: 33 additions & 0 deletions neo/framework/CVarSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class idInternalCVar : public idCVar {
idStr valueString; // value
idStr descriptionString; // description

virtual const char * InternalGetResetString() const;

virtual void InternalSetString( const char *newValue );
virtual void InternalServerSetString( const char *newValue );
virtual void InternalSetBool( const bool newValue );
Expand Down Expand Up @@ -364,6 +366,15 @@ void idInternalCVar::Reset( void ) {
UpdateValue();
}

/*
============
idInternalCVar::InternalGetResetString
============
*/
const char * idInternalCVar::InternalGetResetString() const {
return resetString;
}

/*
============
idInternalCVar::InternalSetString
Expand Down Expand Up @@ -482,6 +493,7 @@ class idCVarSystemLocal : public idCVarSystem {
static void ListByFlags( const idCmdArgs &args, cvarFlags_t flags );
static void List_f( const idCmdArgs &args );
static void Restart_f( const idCmdArgs &args );
static void CvarAdd_f( const idCmdArgs &args );
};

idCVarSystemLocal localCVarSystem;
Expand Down Expand Up @@ -581,6 +593,7 @@ void idCVarSystemLocal::Init( void ) {
cmdSystem->AddCommand( "reset", Reset_f, CMD_FL_SYSTEM, "resets a cvar" );
cmdSystem->AddCommand( "listCvars", List_f, CMD_FL_SYSTEM, "lists cvars" );
cmdSystem->AddCommand( "cvar_restart", Restart_f, CMD_FL_SYSTEM, "restart the cvar system" );
cmdSystem->AddCommand( "cvarAdd", CvarAdd_f, CMD_FL_SYSTEM, "adds a value to a numeric cvar" );

initialized = true;
}
Expand Down Expand Up @@ -1052,6 +1065,26 @@ void idCVarSystemLocal::Reset_f( const idCmdArgs &args ) {
cvar->Reset();
}

/*
============
idCVarSystemLocal::CvarAdd_f
============
*/
void idCVarSystemLocal::CvarAdd_f( const idCmdArgs &args ) {
if ( args.Argc() != 3 ) {
common->Printf ("usage: cvarAdd <variable> <value>\n");
}

idInternalCVar *cvar = localCVarSystem.FindInternal( args.Argv( 1 ) );
if ( !cvar ) {
return;
}

const float newValue = cvar->GetFloat() + atof( args.Argv( 2 ) );
cvar->SetFloat( newValue );
common->Printf( "%s = %f\n", cvar->GetName(), newValue );
}

/*
============
idCVarSystemLocal::ListByFlags
Expand Down
3 changes: 3 additions & 0 deletions neo/framework/CVarSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class idCVar {
void SetModified( void ) { internalVar->flags |= CVAR_MODIFIED; }
void ClearModified( void ) { internalVar->flags &= ~CVAR_MODIFIED; }

const char * GetDefaultString( void ) const { return internalVar->InternalGetResetString(); }
const char * GetString( void ) const { return internalVar->value; }
bool GetBool( void ) const { return ( internalVar->integerValue != 0 ); }
int GetInteger( void ) const { return internalVar->integerValue; }
Expand Down Expand Up @@ -175,6 +176,8 @@ class idCVar {
virtual void InternalSetInteger( const int newValue ) {}
virtual void InternalSetFloat( const float newValue ) {}

virtual const char * InternalGetResetString() const { return value; }

static idCVar * staticVars;
};

Expand Down
15 changes: 12 additions & 3 deletions neo/framework/CmdSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ typedef struct commandDef_s {
char * description;
} commandDef_t;


/*
================================================
idCmdSystemLocal
================================================
*/
class idCmdSystemLocal : public idCmdSystem {
public:
virtual void Init( void );
Expand All @@ -58,6 +62,8 @@ class idCmdSystemLocal : public idCmdSystem {

virtual void CommandCompletion( void(*callback)( const char *s ) );
virtual void ArgCompletion( const char *cmdString, void(*callback)( const char *s ) );
virtual void ExecuteCommandText( const char * text );
virtual void AppendCommandText( const char * text );

virtual void BufferCommandText( cmdExecution_t exec, const char *text );
virtual void ExecuteCommandBuffer( void );
Expand Down Expand Up @@ -93,9 +99,7 @@ class idCmdSystemLocal : public idCmdSystem {

private:
void ExecuteTokenizedString( const idCmdArgs &args );
void ExecuteCommandText( const char *text );
void InsertCommandText( const char *text );
void AppendCommandText( const char *text );

static void ListByFlags( const idCmdArgs &args, cmdFlags_t flags );
static void List_f( const idCmdArgs &args );
Expand Down Expand Up @@ -329,6 +333,11 @@ void idCmdSystemLocal::Init( void ) {
AddCommand( "parse", Parse_f, CMD_FL_SYSTEM, "prints tokenized string" );
AddCommand( "wait", Wait_f, CMD_FL_SYSTEM, "delays remaining buffered commands one or more frames" );

// link in all the commands declared with static idCommandLink variables or CONSOLE_COMMAND macros
for ( idCommandLink * link = CommandLinks(); link != NULL; link = link->next ) {
AddCommand( link->cmdName_, link->function_, CMD_FL_SYSTEM, link->description_, link->argCompletion_ );
}

completionString = "*";

textLength = 0;
Expand Down
76 changes: 76 additions & 0 deletions neo/framework/CmdSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,74 @@ typedef void (*cmdFunction_t)( const idCmdArgs &args );
// argument completion function
typedef void (*argCompletion_t)( const idCmdArgs &args, void(*callback)( const char *s ) );

/*
================================================
idCommandLink is a convenient way to get a function registered as a
ConsoleCommand without having to add an explicit call to idCmdSystem->AddCommand() in a startup
function somewhere. Simply declare a static variable with the parameters and it will get
executed before main(). For example:
static idCommandLink sys_dumpMemory( "sys_dumpMemory", Sys_DumpMemory_f, "Walks the heap and reports stats" );
================================================
*/

class idCommandLink {
public:
idCommandLink( const char *cmdName, cmdFunction_t function,
const char *description, argCompletion_t argCompletion = NULL );
idCommandLink * next;
const char * cmdName_;
cmdFunction_t function_;
const char * description_;
argCompletion_t argCompletion_;
};

// The command system will create commands for all the static definitions
// when it initializes.
idCommandLink *CommandLinks( idCommandLink *cl = NULL );

/*
================================================
The CONSOLE_COMMAND macro is an even easier way to create a console command by
automatically generating the idCommandLink variable, and it also allows all the
command code to be stripped from a build with a single define. For example:
CONSOLE_COMMAND( Sys_DumpMemory, "Walks the heap and reports stats" ) {
// do stuff
}
NOTE: All CONSOLE_COMMANDs will be stripped with the shipping build unless it's
created using the CONSOLE_COMMAND_SHIP macro.
================================================
*/

#if defined ( ID_RETAIL )
#define CONSOLE_COMMAND_SHIP CONSOLE_COMMAND_COMPILE
#define CONSOLE_COMMAND CONSOLE_COMMAND_NO_COMPILE
// We need to disable this warning to get commands that were made friends
// of classes to compile as inline.
// warning C4211: nonstandard extension used : redefined extern to static
#pragma warning( disable : 4211 )
// warning C4505: 'xxx' : unreferenced local function has been removed
#pragma warning( disable : 4505 )
#else
#define CONSOLE_COMMAND_SHIP CONSOLE_COMMAND_COMPILE
#define CONSOLE_COMMAND CONSOLE_COMMAND_COMPILE
#endif

// Turn console commands into static inline code, which will cause them to be
// removed from the build.
#define CONSOLE_COMMAND_NO_COMPILE( name, comment, completion ) \
static inline void name ## _f( const idCmdArgs &args )

// lint incorrectly gives this for all console commands: Issue 1568: (Warning -- Variable 'TestAtomicString_v' accesses variable 'atomicStringManager' before the latter is initialized through calls: 'TestAtomicString_f() => idAtomicString::FreeDynamic()')
// I can't figure out how to disable this just around CONSOLE_COMMAND, so it must stay disabled everywhere,
// which is a shame.
//lint -e1568
#define CONSOLE_COMMAND_COMPILE( name, comment, completion ) \
void name ## _f( const idCmdArgs &args ); \
idCommandLink name ## _v( #name, name ## _f, comment, completion ); \
void name ## _f( const idCmdArgs &args )

class idCmdSystem {
public:
Expand All @@ -87,6 +155,9 @@ class idCmdSystem {
virtual void CommandCompletion( void(*callback)( const char *s ) ) = 0;
virtual void ArgCompletion( const char *cmdString, void(*callback)( const char *s ) ) = 0;

virtual void ExecuteCommandText( const char * text ) = 0;
virtual void AppendCommandText( const char * text ) = 0;

// Adds command text to the command buffer, does not add a final \n
virtual void BufferCommandText( cmdExecution_t exec, const char *text ) = 0;
// Pulls off \n \r or ; terminated lines of text from the command buffer and
Expand Down Expand Up @@ -119,6 +190,7 @@ class idCmdSystem {
static void ArgCompletion_ModelName( const idCmdArgs &args, void(*callback)( const char *s ) );
static void ArgCompletion_SoundName( const idCmdArgs &args, void(*callback)( const char *s ) );
static void ArgCompletion_ImageName( const idCmdArgs &args, void(*callback)( const char *s ) );
static void ArgCompletion_GuiName( const idCmdArgs &args, void(*callback)( const char *s ) );
static void ArgCompletion_VideoName( const idCmdArgs &args, void(*callback)( const char *s ) );
static void ArgCompletion_ConfigName( const idCmdArgs &args, void(*callback)( const char *s ) );
static void ArgCompletion_SaveGame( const idCmdArgs &args, void(*callback)( const char *s ) );
Expand Down Expand Up @@ -169,6 +241,10 @@ ID_INLINE void idCmdSystem::ArgCompletion_ImageName( const idCmdArgs &args, void
cmdSystem->ArgCompletion_FolderExtension( args, callback, "/", false, ".tga", ".dds", ".jpg", ".png", NULL );
}

ID_INLINE void idCmdSystem::ArgCompletion_GuiName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
cmdSystem->ArgCompletion_FolderExtension( args, callback, "guis/", false, ".gui", NULL );
}

ID_INLINE void idCmdSystem::ArgCompletion_VideoName( const idCmdArgs &args, void(*callback)( const char *s ) ) {
cmdSystem->ArgCompletion_FolderExtension( args, callback, "video/", false, ".roq", NULL );
}
Expand Down
Loading

0 comments on commit ec5b679

Please sign in to comment.