forked from Source-Python-Dev-Team/Source.Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_command_wrap.cpp
More file actions
236 lines (202 loc) · 8.75 KB
/
Copy pathclient_command_wrap.cpp
File metadata and controls
236 lines (202 loc) · 8.75 KB
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
234
235
236
/**
* =============================================================================
* Source Python
* Copyright (C) 2012 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
//-----------------------------------------------------------------------------
#include "client_command_wrap.h"
#include "command_wrap.h"
#include "edict.h"
#include "convar.h"
#include "engine/iserverplugin.h"
#include "modules/entities/entities_wrap.h"
#include "utility/call_python.h"
#include "boost/python/call.hpp"
#include "boost/shared_array.hpp"
//-----------------------------------------------------------------------------
// Global Client command mapping.
//-----------------------------------------------------------------------------
typedef boost::unordered_map<std::string, CClientCommandManager*> ClientCommandMap;
ClientCommandMap g_ClientCommandMap;
//-----------------------------------------------------------------------------
// Static singletons.
//-----------------------------------------------------------------------------
static BaseFilters s_ClientCommandFilters;
//-----------------------------------------------------------------------------
// Returns a CClientCommandManager for the given command name.
//-----------------------------------------------------------------------------
CClientCommandManager* get_client_command(const char* szName)
{
// Find if the given name is a registered client command
ClientCommandMap::iterator commandMapIter = g_ClientCommandMap.find(szName);
if( commandMapIter == g_ClientCommandMap.end())
{
// If the command is not already registered, add the name and the CClientCommandManager instance to the mapping
g_ClientCommandMap.insert(std::make_pair(szName, new CClientCommandManager(szName)));
// Get the client command in the mapping
commandMapIter = g_ClientCommandMap.find(szName);
}
// Return the CClientCommandManager instance for the command
return commandMapIter->second;
}
//-----------------------------------------------------------------------------
// Removes a CClientCommandManager instance for the given name.
//-----------------------------------------------------------------------------
void RemoveCClientCommandManager(const char* szName)
{
// Find if the given name is a registered client command
ClientCommandMap::iterator commandMapIter = g_ClientCommandMap.find(szName);
if( commandMapIter != g_ClientCommandMap.end())
{
// If the command is registered, delete the CClientCommandManager instance
// and remove the command from the mapping
delete commandMapIter->second;
g_ClientCommandMap.erase(commandMapIter);
}
}
//-----------------------------------------------------------------------------
// Register function for client command filter.
//-----------------------------------------------------------------------------
void register_client_command_filter(PyObject* pCallable)
{
s_ClientCommandFilters.register_filter(pCallable);
}
//-----------------------------------------------------------------------------
// Unregister function for client command filter.
//-----------------------------------------------------------------------------
void unregister_client_command_filter(PyObject* pCallable)
{
s_ClientCommandFilters.unregister_filter(pCallable);
}
//-----------------------------------------------------------------------------
// Dispatches a client command.
//-----------------------------------------------------------------------------
PLUGIN_RESULT DispatchClientCommand(edict_t* pEntity, const CCommand &command)
{
// Get the CEdict instance of the player
CEdict* pEdict = new CEdict(pEntity);
// Get the CPlayerInfo instance of the player
CPlayerInfo* pPlayerInfo = new CPlayerInfo(pEdict);
// Get the CICommand instance of the CCommand
CICommand* ccommand = new CICommand(&command);
// 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, pPlayerInfo, ccommand);
// 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()
}
// Get the command's name
const char* szCommand = command.Arg(0);
// Find if the command exists in the mapping
ClientCommandMap::iterator commandMapIter = g_ClientCommandMap.find(szCommand);
if( commandMapIter != g_ClientCommandMap.end() )
{
// If the command exists, get the CClientCommandManager instance and call its Dispatch method
CClientCommandManager* pCClientCommandManager = commandMapIter->second;
// Does the command need to be blocked?
if( !pCClientCommandManager->Dispatch(pPlayerInfo, ccommand))
{
// 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::add_callback( 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::remove_callback( 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( CPlayerInfo* pPlayerInfo, CICommand* ccommand )
{
// 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, pPlayerInfo, ccommand);
// 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;
}