Optimized event name

Use a static char*, instead of an allocated str
This commit is contained in:
smallmodel 2023-09-07 18:05:49 +02:00
parent e8d2a2a461
commit 47a3c0bf55
No known key found for this signature in database
GPG key ID: 9F2D623CEDF08512
7 changed files with 99 additions and 80 deletions

View file

@ -265,11 +265,14 @@ void Animate::NewAnim(int animnum, int slot, float weight)
for (ii = 0; ii < cmds.num_cmds; ii++) {
const tiki_singlecmd_t& single_cmd = cmds.cmds[ii];
char* cmdName = (char*)alloca(strlen(single_cmd.args[0]) + 8 + 1);
int eventNum;
strcpy(cmdName, "_client_");
strcpy(cmdName + 8, single_cmd.args[0]);
AnimationEvent ev(cmdName, single_cmd.num_args);
eventNum = Event::FindEventNum(cmdName);
AnimationEvent ev(eventNum, single_cmd.num_args);
if (!ev.eventnum) {
continue;
}
@ -328,11 +331,14 @@ void Animate::NewAnim(int animnum, int slot, float weight)
for (ii = 0; ii < cmds.num_cmds; ii++) {
const tiki_singlecmd_t& single_cmd = cmds.cmds[ii];
char* cmdName = (char*)alloca(strlen(single_cmd.args[0]) + 8 + 1);
int eventNum;
strcpy(cmdName, "_client_");
strcpy(cmdName + 8, single_cmd.args[0]);
AnimationEvent ev(cmdName, single_cmd.num_args);
eventNum = Event::FindEventNum(cmdName);
AnimationEvent ev(eventNum, single_cmd.num_args);
if (!ev.eventnum) {
continue;
}
@ -487,11 +493,14 @@ void Animate::DoExitCommands(int slot)
for (ii = 0; ii < cmds.num_cmds; ii++) {
const tiki_singlecmd_t& single_cmd = cmds.cmds[ii];
char* cmdName = (char*)alloca(strlen(single_cmd.args[0]) + 8 + 1);
int eventNum;
strcpy(cmdName, "_client_");
strcpy(cmdName + 8, single_cmd.args[0]);
AnimationEvent ev(cmdName, single_cmd.num_args);
eventNum = Event::FindEventNum(cmdName);
AnimationEvent ev(eventNum, single_cmd.num_args);
if (!ev.eventnum) {
continue;
}

View file

@ -29,13 +29,20 @@ CLASS_DECLARATION(Event, AnimationEvent, NULL) {
{NULL, NULL}
};
AnimationEvent::AnimationEvent(str command, int numArgs)
AnimationEvent::AnimationEvent(const char* command, int numArgs)
: Event(command, numArgs)
{
anim_number = 0;
anim_frame = 0;
}
AnimationEvent::AnimationEvent(int eventNum, int numArgs)
: Event(eventNum, numArgs)
{
anim_number = 0;
anim_frame = 0;
}
AnimationEvent::AnimationEvent(const Event& ev)
: Event(ev)
{

View file

@ -35,7 +35,8 @@ public:
void *operator new(size_t size);
void operator delete(void *ptr);
AnimationEvent(str command, int numArgs = 0);
AnimationEvent(const char* command, int numArgs = 0);
AnimationEvent(int eventNum, int numArgs = 0);
AnimationEvent(const Event& ev);
AnimationEvent();

View file

@ -5092,7 +5092,7 @@ qboolean Entity::CheckEventFlags(Event *event)
ent = (Entity *)this;
gi.SendServerCommand(
ent->edict - g_entities, "print \"Command '%s' not available from console\n\"", event->getName().c_str()
ent->edict - g_entities, "print \"Command '%s' not available from console\n\"", event->getName()
);
}

View file

@ -347,6 +347,40 @@ Event EV_Listener_WaitTillAnyTimeout
EV_NORMAL
);
CLASS_DECLARATION( Class, Listener, NULL )
{
{ &EV_Listener_CommandDelay, &Listener::CommandDelay },
{ &EV_Delete, &Listener::EventDelete },
{ &EV_Remove, &Listener::EventDelete },
{ &EV_ScriptRemove, &Listener::EventDelete },
{ &EV_Listener_Classname, &Listener::GetClassname },
{ &EV_Listener_InheritsFrom, &Listener::EventInheritsFrom },
{ &EV_Listener_IsInheritedBy, &Listener::EventIsInheritedBy },
#ifdef WITH_SCRIPT_ENGINE
{ &EV_Listener_CancelFor, &Listener::CancelFor },
{ &EV_Listener_CreateReturnThread, &Listener::CreateReturnThread },
{ &EV_Listener_CreateThread, &Listener::CreateThread },
{ &EV_Listener_ExecuteReturnScript, &Listener::ExecuteReturnScript },
{ &EV_Listener_ExecuteScript, &Listener::ExecuteScript },
{ &EV_Listener_EndOn, &Listener::EventEndOn },
{ &EV_Listener_GetOwner, &Listener::EventGetOwner },
{ &EV_Listener_Notify, &Listener::EventNotify },
{ &EV_DelayThrow, &Listener::EventDelayThrow },
{ &EV_Throw, &Listener::EventThrow },
{ &EV_Listener_Unregister, &Listener::EventUnregister },
{ &EV_Listener_WaitCreateReturnThread, &Listener::WaitCreateReturnThread },
{ &EV_Listener_WaitCreateThread, &Listener::WaitCreateThread },
{ &EV_Listener_WaitExecuteReturnScript, &Listener::WaitExecuteReturnScript },
{ &EV_Listener_WaitExecuteScript, &Listener::WaitExecuteScript },
{ &EV_Listener_WaitTill, &Listener::WaitTill },
{ &EV_Listener_WaitTillTimeout, &Listener::WaitTillTimeout },
{ &EV_Listener_WaitTillAny, &Listener::WaitTillAny },
{ &EV_Listener_WaitTillAnyTimeout, &Listener::WaitTillAnyTimeout },
#endif
{ NULL, NULL }
};
cvar_t *g_showevents;
cvar_t *g_eventlimit;
cvar_t *g_timeevents;
@ -1184,14 +1218,9 @@ void Event::operator delete( void *ptr )
FindEventNum
=======================
*/
unsigned int Event::FindEventNum( str s )
unsigned int Event::FindEventNum( const char* s )
{
command_t cmd;
cmd.command = s;
cmd.flags = 0;
cmd.type = -1;
command_t cmd(s, EV_NORMAL);
return commandList.findKeyIndex( cmd );
}
@ -1259,7 +1288,7 @@ void Event::ListCommands
continue;
}
if ( mask && Q_stricmpn( command->command.c_str(), mask, l ) )
if ( mask && Q_stricmpn( command->command, mask, l ) )
{
continue;
}
@ -1281,7 +1310,7 @@ void Event::ListCommands
text[ p++ ] = '%';
}
EVENT_Printf( "%4d : %s%s\n", eventnum, text.c_str(), command->command.c_str() );
EVENT_Printf( "%4d : %s%s\n", eventnum, text.c_str(), command->command );
}
EVENT_Printf( "\n* = console command.\nC = cheat command.\n%% = cache command.\n\n"
@ -1410,7 +1439,7 @@ void Event::PendingEvents
assert( event );
assert( event->m_sourceobject );
if ( !mask || !Q_stricmpn( event->event->getName().c_str(), mask, l ) )
if ( !mask || !Q_stricmpn( event->event->getName(), mask, l ) )
{
num++;
//Event::PrintEvent( event );
@ -1653,7 +1682,7 @@ GetEventName
Returns the specified event name
=======================
*/
str& Event::GetEventName( int eventnum )
const char* Event::GetEventName( int eventnum )
{
command_t *cmd;
@ -1687,7 +1716,7 @@ int Event::compareEvents( const void *arg1, const void *arg2 )
command_t *cmd1 = &commandList[ num1 ];
command_t *cmd2 = &commandList[ num2 ];
return Q_stricmp( cmd1->command.c_str(), cmd2->command.c_str() );
return Q_stricmp( cmd1->command, cmd2->command );
}
/*
@ -1945,20 +1974,12 @@ Event
Initializes the event with the specified command
=======================
*/
Event::Event( str command, int numArgs )
Event::Event(const char* command, int numArgs)
{
command_t c;
c.command = command.c_str();
c.flags = 0;
c.type = EV_NORMAL;
c.command.tolower();
eventnum = commandList.findKeyIndex( c );
eventnum = FindEventNum(command);
if( !eventnum )
{
EVENT_DPrintf( "^~^~^ Event '%s' does not exist.\n", command.c_str() );
EVENT_DPrintf( "^~^~^ Event '%s' does not exist.\n", command );
}
fromScript = qfalse;
@ -2474,7 +2495,7 @@ EventDef *Event::getInfo()
getName
=======================
*/
str& Event::getName() const
const char* Event::getName() const
{
return GetEventName( eventnum );
}
@ -2733,7 +2754,7 @@ EventQueueNode *Listener::PostEventInternal( Event *ev, float delay, int flags )
if( !ev->eventnum )
{
#ifdef _DEBUG
EVENT_DPrintf( "^~^~^ Failed execution of event '%s' for class '%s'\n", ev->name.c_str(), getClassname() );
EVENT_DPrintf( "^~^~^ Failed execution of event '%s' for class '%s'\n", ev->name, getClassname() );
#else
EVENT_DPrintf( "^~^~^ Failed execution of event for class '%s'\n", getClassname() );
#endif
@ -2935,7 +2956,7 @@ ScriptVariable& Listener::ProcessEventReturn( Event *ev )
if( !ev->eventnum )
{
#ifdef _DEBUG
EVENT_Printf( "^~^~^ Failed execution of event '%s' for class '%s'\n", ev->name.c_str(), c->classname );
EVENT_Printf( "^~^~^ Failed execution of event '%s' for class '%s'\n", ev->name, c->classname );
#else
EVENT_Printf( "^~^~^ Failed execution of event for class '%s'\n", c->classname );
#endif
@ -2949,7 +2970,7 @@ ScriptVariable& Listener::ProcessEventReturn( Event *ev )
if( responses == NULL )
{
EVENT_Printf( "^~^~^ Failed execution of command '%s' for class '%s'\n", Event::GetEventName( ev->eventnum ).c_str(), c->classname );
EVENT_Printf( "^~^~^ Failed execution of command '%s' for class '%s'\n", Event::GetEventName( ev->eventnum ), c->classname );
delete ev;
return m_Return;
}
@ -3000,7 +3021,7 @@ bool Listener::ProcessScriptEvent( Event &ev )
if( !ev.eventnum )
{
#ifdef _DEBUG
EVENT_Printf( "^~^~^ Failed execution of event '%s' for class '%s'\n", ev.name.c_str(), c->classname );
EVENT_Printf( "^~^~^ Failed execution of event '%s' for class '%s'\n", ev.name, c->classname );
#else
EVENT_Printf( "^~^~^ Failed execution of event for class '%s'\n", c->classname );
#endif
@ -4494,36 +4515,13 @@ void Listener::ExecuteThread( str scriptName, str labelName, Event& params )
#endif
CLASS_DECLARATION( Class, Listener, NULL )
command_t::command_t()
{
{ &EV_Listener_CommandDelay, &Listener::CommandDelay },
{ &EV_Delete, &Listener::EventDelete },
{ &EV_Remove, &Listener::EventDelete },
{ &EV_ScriptRemove, &Listener::EventDelete },
{ &EV_Listener_Classname, &Listener::GetClassname },
{ &EV_Listener_InheritsFrom, &Listener::EventInheritsFrom },
{ &EV_Listener_IsInheritedBy, &Listener::EventIsInheritedBy },
}
#ifdef WITH_SCRIPT_ENGINE
{ &EV_Listener_CancelFor, &Listener::CancelFor },
{ &EV_Listener_CreateReturnThread, &Listener::CreateReturnThread },
{ &EV_Listener_CreateThread, &Listener::CreateThread },
{ &EV_Listener_ExecuteReturnScript, &Listener::ExecuteReturnScript },
{ &EV_Listener_ExecuteScript, &Listener::ExecuteScript },
{ &EV_Listener_EndOn, &Listener::EventEndOn },
{ &EV_Listener_GetOwner, &Listener::EventGetOwner },
{ &EV_Listener_Notify, &Listener::EventNotify },
{ &EV_DelayThrow, &Listener::EventDelayThrow },
{ &EV_Throw, &Listener::EventThrow },
{ &EV_Listener_Unregister, &Listener::EventUnregister },
{ &EV_Listener_WaitCreateReturnThread, &Listener::WaitCreateReturnThread },
{ &EV_Listener_WaitCreateThread, &Listener::WaitCreateThread },
{ &EV_Listener_WaitExecuteReturnScript, &Listener::WaitExecuteReturnScript },
{ &EV_Listener_WaitExecuteScript, &Listener::WaitExecuteScript },
{ &EV_Listener_WaitTill, &Listener::WaitTill },
{ &EV_Listener_WaitTillTimeout, &Listener::WaitTillTimeout },
{ &EV_Listener_WaitTillAny, &Listener::WaitTillAny },
{ &EV_Listener_WaitTillAnyTimeout, &Listener::WaitTillAnyTimeout },
#endif
{ NULL, NULL }
};
command_t::command_t(const char* name, byte t)
: command(name)
, type(t)
{
}

View file

@ -232,28 +232,32 @@ public:
class command_t {
public:
str command;
const char* command;
int flags;
uchar type;
byte type;
friend bool operator==(const str& name, const command_t& command);
public:
command_t();
command_t(const char* name, byte t);
friend bool operator==(const char* name, const command_t& command);
friend bool operator==(const command_t& cmd1, const command_t& cmd2);
};
inline bool operator==(const str& name, const command_t& command)
inline bool operator==(const char* name, const command_t& command)
{
return command.command == name;
return !str::icmp(name, command.command);
}
#ifdef WITH_SCRIPT_ENGINE
inline bool operator==(const command_t& cmd1, const command_t& cmd2)
{
return (!cmd2.command.icmp(cmd1.command) && (cmd2.type == (uchar)-1 || cmd2.type == cmd1.type));
return (!str::icmp(cmd1.command, cmd2.command) && (cmd2.type == (uchar)-1 || cmd2.type == cmd1.type));
}
#else
inline bool operator==(const command_t& cmd1, const command_t& cmd2)
{
return (!cmd2.command.icmp(cmd1.command));
return (!str::icmp(cmd1.command, cmd2.command));
}
#endif
@ -268,7 +272,7 @@ public:
#ifdef _DEBUG
// should be used only for debugging purposes
str name;
const char* name;
#endif
private:
@ -301,14 +305,14 @@ public:
static command_t *GetEventInfo( int eventnum );
static int GetEventFlags( int eventnum );
static str& GetEventName( int index );
static const char* GetEventName( int index );
static int compareEvents( const void *arg1, const void *arg2 );
static void SortEventList( Container< int > *sortedList );
virtual void ErrorInternal( Listener *l, str text ) const;
static unsigned int FindEventNum( str s );
static unsigned int FindEventNum( const char* s );
static unsigned int FindNormalEventNum( const_str s );
static unsigned int FindNormalEventNum( str s );
static unsigned int FindReturnEventNum( const_str s );
@ -331,7 +335,7 @@ public:
Event( const Event &ev );
Event( int index );
Event( int index, int numArgs );
Event( str command, int numArgs = 0 );
Event( const char* command, int numArgs = 0 );
Event
(
const char *command,
@ -355,7 +359,7 @@ public:
EventDef *getInfo();
#endif
str& getName() const;
const char* getName() const;
void AddContainer( Container< SafePtr< Listener > > *container );
void AddEntity( Entity * ent );

View file

@ -598,7 +598,7 @@ void ScriptVM::execCmdMethodCommon(op_parmNum_t param)
const size_t arraysize = a.arraysize();
if (arraysize == (size_t)-1) {
throw ScriptException("command '%s' applied to NIL", Event::GetEventName(eventNum).c_str());
throw ScriptException("command '%s' applied to NIL", Event::GetEventName(eventNum));
}
if (arraysize > 1) {
@ -634,7 +634,7 @@ void ScriptVM::execCmdMethodCommon(op_parmNum_t param)
// avoid useless allocations of const array
Listener *const listener = a.listenerValue();
if (!listener) {
throw ScriptException("command '%s' applied to NULL listener", Event::GetEventName(eventNum).c_str());
throw ScriptException("command '%s' applied to NULL listener", Event::GetEventName(eventNum));
}
executeCommand<true>(listener, param, eventNum);
@ -653,7 +653,7 @@ void ScriptVM::execMethodCommon(op_parmNum_t param)
Listener *const listener = a.listenerValue();
if (!listener) {
m_VMStack.GetTop().Clear();
throw ScriptException("command '%s' applied to NULL listener", Event::GetEventName(eventNum).c_str());
throw ScriptException("command '%s' applied to NULL listener", Event::GetEventName(eventNum));
}
executeCommand<true, true>(listener, param, eventNum);