Skip to content

Added server command post hooks #278

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

Merged
merged 2 commits into from
Jun 27, 2019
Merged
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
93 changes: 38 additions & 55 deletions src/core/modules/commands/commands_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
// This is required for accessing m_nFlags without patching convar.h
#define private public

#include "boost/unordered_map.hpp"
#include "commands_client.h"
#include "commands.h"
#include "edict.h"
Expand Down Expand Up @@ -115,36 +116,35 @@ PLUGIN_RESULT DispatchClientCommand(edict_t* pEntity, const CCommand &command)
if (!IndexFromEdict(pEntity, iIndex))
return PLUGIN_CONTINUE;

// Loop through all registered Client Command Filters
for(int i = 0; i < s_ClientCommandFilters.m_vecCallables.Count(); i++)
{
BEGIN_BOOST_PY()

// Get the PyObject instance of the callable
PyObject* pCallable = s_ClientCommandFilters.m_vecCallables[i].ptr();

// Call the callable and store its return value
object returnValue = CALL_PY_FUNC(pCallable, boost::ref(command), iIndex);
bool block = false;

CListenerManager* mngr = &s_ClientCommandFilters;
FOREACH_CALLBACK_WITH_MNGR(
mngr,
object returnValue,
if( !returnValue.is_none() && extract<int>(returnValue) == (int)BLOCK)
{
block = true;
},
boost::ref(command), iIndex
)

// Does the Client Command Filter want to block the command?
if( !returnValue.is_none() && extract<int>(returnValue) == (int)BLOCK)
{
// Block the command
return PLUGIN_STOP;
}

END_BOOST_PY_NORET()
}
if (block)
return PLUGIN_STOP;

block = false;
ClientCommandMap::iterator iter;
if (find_manager<ClientCommandMap, ClientCommandMap::iterator>(g_ClientCommandMap, command.Arg(0), iter))
{
if( !iter->second->Dispatch(command, iIndex))
{
// Block the command
return PLUGIN_STOP;
block = true;
}
}

if (block)
return PLUGIN_STOP;

return PLUGIN_CONTINUE;
}

Expand All @@ -169,30 +169,18 @@ CClientCommandManager::~CClientCommandManager()
//-----------------------------------------------------------------------------
void CClientCommandManager::AddCallback( PyObject* pCallable )
{
// Get the object instance of the callable
object oCallable = object(handle<>(borrowed(pCallable)));

// Is the callable already in the vector?
if( !m_vecCallables.HasElement(oCallable) )
{
// Add the callable to the vector
m_vecCallables.AddToTail(oCallable);
}
m_vecCallables.RegisterListener(pCallable);
}

//-----------------------------------------------------------------------------
// Removes a callable from a CClientCommandManager instance.
//-----------------------------------------------------------------------------
void CClientCommandManager::RemoveCallback( PyObject* pCallable )
{
// Get the object instance of the callable
object oCallable = object(handle<>(borrowed(pCallable)));

// Remove the callback from the CClientCommandManager instance
m_vecCallables.FindAndRemove(oCallable);
m_vecCallables.UnregisterListener(pCallable);

// Are there any more callbacks registered for this command?
if( !m_vecCallables.Count() )
if( !m_vecCallables.GetCount() )
{
// Remove the CClientCommandManager instance
RemoveCClientCommandManager(m_Name);
Expand All @@ -204,26 +192,21 @@ void CClientCommandManager::RemoveCallback( PyObject* pCallable )
//-----------------------------------------------------------------------------
CommandReturn CClientCommandManager::Dispatch( const CCommand& command, int iIndex )
{
// Loop through all callables registered for the CClientCommandManager instance
for(int i = 0; i < m_vecCallables.Count(); i++)
{
BEGIN_BOOST_PY()

// Get the PyObject instance of the callable
PyObject* pCallable = m_vecCallables[i].ptr();

// Call the callable and store its return value
object returnValue = CALL_PY_FUNC(pCallable, boost::ref(command), iIndex);

// Does the callable wish to block the command?
if( !returnValue.is_none() && extract<int>(returnValue) == (int) BLOCK)
{
// Block the command
return BLOCK;
}
bool block = false;

CListenerManager* mngr = &m_vecCallables;
FOREACH_CALLBACK_WITH_MNGR(
mngr,
object returnValue,
if( !returnValue.is_none() && extract<int>(returnValue) == (int) BLOCK)
{
block = true;
},
boost::ref(command), iIndex
)

END_BOOST_PY_NORET()
}
if (block)
return BLOCK;

return CONTINUE;
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/modules/commands/commands_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "boost/unordered_map.hpp"
#include "sp_python.h"
#include "utilities/sp_util.h"
#include "utilities/wrap_macros.h"
#include "utlvector.h"
#include "edict.h"
#include "convar.h"
#include "commands.h"
#include "game/server/iplayerinfo.h"

#include "modules/listeners/listeners_manager.h"


//-----------------------------------------------------------------------------
// Client Command Manager class.
Expand All @@ -57,7 +57,7 @@ class CClientCommandManager
const char* GetName();

private:
CUtlVector<object> m_vecCallables;
CListenerManager m_vecCallables;
const char* m_Name;
};

Expand Down
93 changes: 38 additions & 55 deletions src/core/modules/commands/commands_say.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@
#include "utilities/call_python.h"
#include "boost/python/call.hpp"
#include "boost/shared_array.hpp"
#include "boost/unordered_map.hpp"
#include "sp_main.h"
#include "modules/listeners/listeners_manager.h"
#include "convar.h"


//-----------------------------------------------------------------------------
// Global say command mapping.
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -243,37 +245,35 @@ void SayConCommand::Dispatch( const CCommand& command )
return;
}

// Loop through all registered Say Filter callbacks
for(int i = 0; i < s_SayFilters.m_vecCallables.Count(); i++)
{
BEGIN_BOOST_PY()

// Get the PyObject instance of the callable
PyObject* pCallable = s_SayFilters.m_vecCallables[i].ptr();

// Call the callable and store its return value
object returnValue = CALL_PY_FUNC(pCallable, boost::ref(stripped_command), iIndex, bTeamOnly);
bool block = false;

// Does the current Say Filter wish to block the command?
if( !returnValue.is_none() && extract<int>(returnValue) == (int) BLOCK)
{
// Block the command
return;
}
// Loop through all registered Say Filter callbacks
CListenerManager* mngr = &s_SayFilters;
FOREACH_CALLBACK_WITH_MNGR(
mngr,
object returnValue,
if( !returnValue.is_none() && extract<int>(returnValue) == (int) BLOCK)
{
block = true;
},
boost::ref(stripped_command), iIndex, bTeamOnly
)

END_BOOST_PY_NORET()
}
if (block)
return;


block = false;
SayCommandMap::iterator iter;
if (find_manager<SayCommandMap, SayCommandMap::iterator>(g_SayCommandMap, stripped_command[0], iter))
{
if(iter->second->Dispatch(stripped_command, iIndex, bTeamOnly) == BLOCK)
if(iter->second->Dispatch(stripped_command, iIndex, bTeamOnly) == BLOCK)
{
// Block the command
return;
block = true;
}
}

if (block)
return;

// Was the command previously registered?
if( m_pOldCommand )
Expand Down Expand Up @@ -304,30 +304,18 @@ CSayCommandManager::~CSayCommandManager()
//-----------------------------------------------------------------------------
void CSayCommandManager::AddCallback( PyObject* pCallable )
{
// Get the object instance of the callable
object oCallable = object(handle<>(borrowed(pCallable)));

// Is the callable already in the vector?
if( !m_vecCallables.HasElement(oCallable) )
{
// Add the callable to the vector
m_vecCallables.AddToTail(oCallable);
}
m_vecCallables.RegisterListener(pCallable);
}

//-----------------------------------------------------------------------------
// Removes a callable from a CSayCommandManager instance.
//-----------------------------------------------------------------------------
void CSayCommandManager::RemoveCallback( PyObject* pCallable )
{
// Get the object instance of the callable
object oCallable = object(handle<>(borrowed(pCallable)));

// Remove the callback from the CSayCommandManager instance
m_vecCallables.FindAndRemove(oCallable);
m_vecCallables.UnregisterListener(pCallable);

// Are there any more callbacks registered for this command?
if( !m_vecCallables.Count() )
if( !m_vecCallables.GetCount() )
{
// Remove the CSayCommandManager instance
RemoveCSayCommandManager(m_Name);
Expand All @@ -339,26 +327,21 @@ void CSayCommandManager::RemoveCallback( PyObject* pCallable )
//-----------------------------------------------------------------------------
CommandReturn CSayCommandManager::Dispatch( const CCommand& command, int iIndex, bool bTeamOnly)
{
// Loop through all callables registered for the CSayCommandManager instance
for(int i = 0; i < m_vecCallables.Count(); i++)
{
BEGIN_BOOST_PY()

// Get the PyObject instance of the callable
PyObject* pCallable = m_vecCallables[i].ptr();

// Call the callable and store its return value
object returnValue = CALL_PY_FUNC(pCallable, boost::ref(command), iIndex, bTeamOnly);
bool block = false;

// Does the callable wish to block the command?
if( !returnValue.is_none() && extract<int>(returnValue) == (int) BLOCK)
{
// Block the command
return BLOCK;
}
CListenerManager* mngr = &m_vecCallables;
FOREACH_CALLBACK_WITH_MNGR(
mngr,
object returnValue,
if( !returnValue.is_none() && extract<int>(returnValue) == (int) BLOCK)
{
block = true;
},
boost::ref(command), iIndex, bTeamOnly
)

END_BOOST_PY_NORET()
}
if (block)
return BLOCK;

return CONTINUE;
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/modules/commands/commands_say.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "boost/unordered_map.hpp"
#include "utilities/sp_util.h"
#include "commands.h"
#include "utlvector.h"
#include "edict.h"
#include "game/server/iplayerinfo.h"

#include "modules/listeners/listeners_manager.h"


//-----------------------------------------------------------------------------
// Say ConCommand instance class.
Expand Down Expand Up @@ -88,7 +88,7 @@ class CSayCommandManager

private:
const char* m_Name;
CUtlVector<object> m_vecCallables;
CListenerManager m_vecCallables;
};

#endif // _COMMANDS_SAY_H
Loading