/
commands_client.cpp
233 lines (202 loc) · 8.22 KB
/
commands_client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* =============================================================================
* Source Python
* Copyright (C) 2012-2015 Source Python Development Team. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the Source Python Team gives you permission
* to link the code of this program (as well as its derivative works) to
* "Half-Life 2," the "Source Engine," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, the Source.Python
* Development Team grants this exception to all derivative works.
*/
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
// This is required for accessing m_nFlags without patching convar.h
#define private public
#include "commands_client.h"
#include "commands.h"
#include "edict.h"
#include "convar.h"
#include "engine/iserverplugin.h"
#include "utilities/call_python.h"
#include "boost/python/call.hpp"
#include "boost/shared_array.hpp"
#include "modules/listeners/listeners_manager.h"
#include "utilities/conversions.h"
//-----------------------------------------------------------------------------
// Externals
//-----------------------------------------------------------------------------
extern IPlayerInfoManager* playerinfomanager;
//-----------------------------------------------------------------------------
// Global Client command mapping.
//-----------------------------------------------------------------------------
typedef boost::unordered_map<std::string, CClientCommandManager*> ClientCommandMap;
ClientCommandMap g_ClientCommandMap;
//-----------------------------------------------------------------------------
// Static singletons.
//-----------------------------------------------------------------------------
static CListenerManager s_ClientCommandFilters;
//-----------------------------------------------------------------------------
// Returns a CClientCommandManager for the given command name.
//-----------------------------------------------------------------------------
CClientCommandManager* GetClientCommand(const char* szName)
{
CClientCommandManager* manager = NULL;
ClientCommandMap::iterator iter;
if (!find_manager<ClientCommandMap, ClientCommandMap::iterator>(g_ClientCommandMap, szName, iter))
{
manager = new CClientCommandManager(szName);
g_ClientCommandMap.insert(std::make_pair(szName, manager));
}
else
{
manager = iter->second;
}
return manager;
}
//-----------------------------------------------------------------------------
// Removes a CClientCommandManager instance for the given name.
//-----------------------------------------------------------------------------
void RemoveCClientCommandManager(const char* szName)
{
ClientCommandMap::iterator iter;
if (find_manager<ClientCommandMap, ClientCommandMap::iterator>(g_ClientCommandMap, szName, iter))
{
delete iter->second;
g_ClientCommandMap.erase(iter);
}
}
//-----------------------------------------------------------------------------
// Register function for client command filter.
//-----------------------------------------------------------------------------
void RegisterClientCommandFilter(PyObject* pCallable)
{
s_ClientCommandFilters.RegisterListener(pCallable);
}
//-----------------------------------------------------------------------------
// Unregister function for client command filter.
//-----------------------------------------------------------------------------
void UnregisterClientCommandFilter(PyObject* pCallable)
{
s_ClientCommandFilters.UnregisterListener(pCallable);
}
//-----------------------------------------------------------------------------
// Dispatches a client command.
//-----------------------------------------------------------------------------
PLUGIN_RESULT DispatchClientCommand(edict_t* pEntity, const CCommand &command)
{
unsigned int iIndex;
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);
// 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()
}
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;
}
}
return PLUGIN_CONTINUE;
}
//-----------------------------------------------------------------------------
// CClientCommandManager constructor.
//-----------------------------------------------------------------------------
CClientCommandManager::CClientCommandManager(const char* szName)
{
m_Name = szName;
}
//-----------------------------------------------------------------------------
// CClientCommandManager destructor.
//-----------------------------------------------------------------------------
CClientCommandManager::~CClientCommandManager()
{
}
//-----------------------------------------------------------------------------
// Adds a callable to a CClientCommandManager instance.
//-----------------------------------------------------------------------------
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);
}
}
//-----------------------------------------------------------------------------
// 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);
// Are there any more callbacks registered for this command?
if( !m_vecCallables.Count() )
{
// Remove the CClientCommandManager instance
RemoveCClientCommandManager(m_Name);
}
}
//-----------------------------------------------------------------------------
// Calls all callables for the command when it is called on the client.
//-----------------------------------------------------------------------------
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;
}
END_BOOST_PY_NORET()
}
return CONTINUE;
}
const char* CClientCommandManager::GetName()
{
return m_Name;
}