forked from Source-Python-Dev-Team/Source.Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsay_command_wrap.cpp
More file actions
380 lines (325 loc) · 13.2 KB
/
Copy pathsay_command_wrap.cpp
File metadata and controls
380 lines (325 loc) · 13.2 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**
* =============================================================================
* 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 <iostream>
#include <string>
#include "convar.h"
#include "say_command_wrap.h"
#include "server_command_wrap.h"
#include "command_wrap.h"
#include "utility/call_python.h"
#include "boost/python/call.hpp"
#include "boost/shared_array.hpp"
#include "core/sp_main.h"
#include "modules/cvar/cvar_wrap.h"
//-----------------------------------------------------------------------------
// Global say command mapping.
//-----------------------------------------------------------------------------
typedef boost::unordered_map<std::string, CSayCommandManager*> SayCommandMap;
SayCommandMap g_SayCommandMap;
//-----------------------------------------------------------------------------
// Externs.
//-----------------------------------------------------------------------------
extern CSourcePython g_SourcePythonPlugin;
extern ICvar* g_pCVar;
//-----------------------------------------------------------------------------
// Static singletons.
//-----------------------------------------------------------------------------
static BaseFilters s_SayFilters;
static BaseSayCommand s_SayCommand;
//-----------------------------------------------------------------------------
// Helper function to get the current command index
//-----------------------------------------------------------------------------
int GetCommandIndex()
{
return g_SourcePythonPlugin.GetCommandIndex() + 1;
}
//-----------------------------------------------------------------------------
// Registers the say and say_team commands.
//-----------------------------------------------------------------------------
void BaseSayCommand::RegisterCommands()
{
// Register the say command.
m_pSayCommand = SayConCommand::CreateCommand("say", "Say command wrapper", 0);
// Register the say_team command.
m_pTeamSayCommand = SayConCommand::CreateCommand("say_team", "Team Say command wrapper", 0);
}
//-----------------------------------------------------------------------------
// Unregisters the say and say_team commands.
//-----------------------------------------------------------------------------
void BaseSayCommand::UnregisterCommands()
{
// Unregister the say command.
delete m_pSayCommand;
// Unregister the say_team command.
delete m_pTeamSayCommand;
}
//-----------------------------------------------------------------------------
// Returns a CSayCommandManager for the given command name.
//-----------------------------------------------------------------------------
CSayCommandManager* get_say_command(const char* szName)
{
// Find if the given name is a registered say command
SayCommandMap::iterator commandMapIter = g_SayCommandMap.find(szName);
if( commandMapIter == g_SayCommandMap.end())
{
// If the command is not already registered, add the name and the CSayCommandManager instance to the mapping
g_SayCommandMap.insert(std::make_pair(szName, new CSayCommandManager(szName)));
// Get the say command in the mapping
commandMapIter = g_SayCommandMap.find(szName);
}
// Return the CSayCommandManager instance for the command
return commandMapIter->second;
}
//-----------------------------------------------------------------------------
// Removes a CSayCommandManager instance for the given name.
//-----------------------------------------------------------------------------
void RemoveCSayCommandManager(const char* szName)
{
// Find if the given name is a registered say command
SayCommandMap::iterator commandMapIter = g_SayCommandMap.find(szName);
if( commandMapIter != g_SayCommandMap.end() )
{
// If the command is registered, delete the CSayCommandManager instance
// and remove the command from the mapping
delete commandMapIter->second;
g_SayCommandMap.erase(commandMapIter);
}
}
//-----------------------------------------------------------------------------
// Register function for say filter.
//-----------------------------------------------------------------------------
void register_say_filter(PyObject* pCallable)
{
s_SayFilters.register_filter(pCallable);
}
//-----------------------------------------------------------------------------
// Unregister function for say filter.
//-----------------------------------------------------------------------------
void unregister_say_filter(PyObject* pCallable)
{
s_SayFilters.unregister_filter(pCallable);
}
//-----------------------------------------------------------------------------
// Returns a SayConCommand instance.
//-----------------------------------------------------------------------------
SayConCommand* SayConCommand::CreateCommand(const char* szName, const char* szHelpText, int iFlags)
{
// Copy the name and store a help text copier value
char* szNameCopy = strdup(szName);
char* szHelpTextCopy = NULL;
// Find if the command already exists
ConCommand* pConCommand = g_pCVar->FindCommand(szName);
if( pConCommand )
{
// Store the current command's help text and flags
szHelpTextCopy = strdup(pConCommand->GetHelpText());
iFlags = pConCommand->GetFlags();
// Unregister the old command
g_pCVar->UnregisterConCommand(pConCommand);
}
else
{
// Store the given help text
szHelpTextCopy = strdup(szHelpText);
}
// Return a new instance of ConCommand
return new SayConCommand(pConCommand, szNameCopy, szHelpTextCopy, iFlags);
}
//-----------------------------------------------------------------------------
// SayConCommand instance.
//-----------------------------------------------------------------------------
SayConCommand::SayConCommand(ConCommand* pConCommand,
const char* szName, const char* szHelpText, int iFlags):
ConCommand(szName, (FnCommandCallback_t)NULL, szHelpText, iFlags),
m_pOldCommand(pConCommand)
{
m_Name = szName;
}
//-----------------------------------------------------------------------------
// SayConCommand destructor.
//-----------------------------------------------------------------------------
SayConCommand::~SayConCommand()
{
// Get the ConCommand instance
ConCommand* pConCommand = g_pCVar->FindCommand(m_Name);
// Unregister the ConCommand
g_pCVar->UnregisterConCommand(pConCommand);
// Was the command registered before we registered it?
if( m_pOldCommand )
{
// Re-register the old command instance
g_pCVar->RegisterConCommand(m_pOldCommand);
}
}
//-----------------------------------------------------------------------------
// SayConCommand Init override.
//-----------------------------------------------------------------------------
void SayConCommand::Init()
{
ConCommand::Init();
}
//-----------------------------------------------------------------------------
// Dispatches the say and say_team commands.
//-----------------------------------------------------------------------------
void SayConCommand::Dispatch( const CCommand &command )
{
// Get the index of the player that used the command
int iIndex = GetCommandIndex();
// Get the CEdict instance of the player
CEdict* pEdict = new CEdict(iIndex);
// Get the CPlayerInfo instance of the player
CPlayerInfo* pPlayerInfo = new CPlayerInfo(pEdict);
// Get whether the command was say or say_team
bool bTeamOnly = command.Arg(0) == "say_team";
// Get a CICommand instance for the CCommand
CICommand* ccommand = new CICommand(&command);
// 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, pPlayerInfo, bTeamOnly, ccommand);
// Does the current Say Filter wish to block the command?
if( !returnValue.is_none() && extract<int>(returnValue) == (int)BLOCK)
{
// Block the command
return;
}
END_BOOST_PY_NORET()
}
// Get the name of the command used
std::string szCommandString (command.Arg(1));
// Copy the string to get a char instance
char * szCopyCommandString = new char [szCommandString.length() + 1];
std::strcpy(szCopyCommandString, szCommandString.c_str());
// Split the command using <space> as the delimiter
// This should be the actual Say Command
char * szCommand = std::strtok(szCopyCommandString, " ");
// Find if the command is registered
SayCommandMap::iterator commandMapIter = g_SayCommandMap.find(szCommand);
if( commandMapIter != g_SayCommandMap.end() )
{
// Get the CSayCommandManager instance for the command
CSayCommandManager* pCSayCommandManager = commandMapIter->second;
// Call the command and see it wants to block the command
if( pCSayCommandManager->Dispatch(pPlayerInfo, bTeamOnly, ccommand) == BLOCK)
{
// Block the command
return;
}
}
// Was the command previously registered?
if( m_pOldCommand )
{
// Call the old command callback
m_pOldCommand->Dispatch(command);
}
}
//-----------------------------------------------------------------------------
// CSayCommandManager constructor.
//-----------------------------------------------------------------------------
CSayCommandManager::CSayCommandManager(const char* szName)
{
m_Name = szName;
}
//-----------------------------------------------------------------------------
// CSayCommandManager destructor.
//-----------------------------------------------------------------------------
CSayCommandManager::~CSayCommandManager()
{
}
//-----------------------------------------------------------------------------
// Adds a callable to a CSayCommandManager instance.
//-----------------------------------------------------------------------------
void CSayCommandManager::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 CSayCommandManager instance.
//-----------------------------------------------------------------------------
void CSayCommandManager::remove_callback( 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);
// Are there any more callbacks registered for this command?
if( !m_vecCallables.Count() )
{
// Remove the CSayCommandManager instance
RemoveCSayCommandManager(m_Name);
}
}
//-----------------------------------------------------------------------------
// Dispatches the say command.
//-----------------------------------------------------------------------------
CommandReturn CSayCommandManager::Dispatch( CPlayerInfo* pPlayerInfo, bool bTeamOnly, CICommand* ccommand )
{
// 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, pPlayerInfo, bTeamOnly, 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;
}
//-----------------------------------------------------------------------------
// Registers the say and say_team commands.
//-----------------------------------------------------------------------------
void RegisterSayCommands()
{
s_SayCommand.RegisterCommands();
}
//-----------------------------------------------------------------------------
// Unregisters the say and say_team commands.
//-----------------------------------------------------------------------------
void UnregisterSayCommands()
{
s_SayCommand.UnregisterCommands();
}