forked from Source-Python-Dev-Team/Source.Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands_say.cpp
More file actions
370 lines (322 loc) · 12 KB
/
Copy pathcommands_say.cpp
File metadata and controls
370 lines (322 loc) · 12 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
/**
* =============================================================================
* 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.
//-----------------------------------------------------------------------------
#include <iostream>
#include <string>
#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 "utilities/convar.h"
#include "commands_say.h"
#include "commands.h"
#include "modules/cvars/cvars.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 CListenerManager 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* GetSayCommand(const char* szName)
{
CSayCommandManager* manager = NULL;
SayCommandMap::iterator iter;
if (!find_manager<SayCommandMap, SayCommandMap::iterator>(g_SayCommandMap, szName, iter))
{
manager = new CSayCommandManager(szName);
g_SayCommandMap.insert(std::make_pair(manager->m_Name, manager));
}
else
{
manager = iter->second;
}
return manager;
}
//-----------------------------------------------------------------------------
// Removes a CSayCommandManager instance for the given name.
//-----------------------------------------------------------------------------
void RemoveCSayCommandManager(const char* szName)
{
SayCommandMap::iterator iter;
if (find_manager<SayCommandMap, SayCommandMap::iterator>(g_SayCommandMap, szName, iter))
{
delete iter->second;
g_SayCommandMap.erase(iter);
}
}
//-----------------------------------------------------------------------------
// Register function for say filter.
//-----------------------------------------------------------------------------
void RegisterSayFilter(PyObject* pCallable)
{
s_SayFilters.RegisterListener(pCallable);
}
//-----------------------------------------------------------------------------
// Unregister function for say filter.
//-----------------------------------------------------------------------------
void UnregisterSayFilter(PyObject* pCallable)
{
s_SayFilters.UnregisterListener(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 = GetConCommandFlags(pConCommand);
// 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);
// Make sure we only unregister ourselves
if (pConCommand == this)
{
// 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 )
{
// This is the case if just "say" or "say_team" was entered into the server
if (command.ArgC() == 1)
return;
// Get the index of the player that used the command
int iIndex = GetCommandIndex();
// Get whether the command was say or say_team
bool bTeamOnly = strcmp(command.Arg(0), "say_team") == 0;
// Create a new CCommand object that does not contain the first argument
// (say or say_team) and is properly splitted
CCommand stripped_command = CCommand();
char szTempCommand[512];
strcpy(szTempCommand, command.ArgS());
char* szCommand = szTempCommand;
// Remove quotes (if existant), so the arguments are not recognized as a
// single argument.
int iLastCharPos = strlen(szCommand) - 1;
if (szCommand[0] == '"' && szCommand[iLastCharPos] == '"') {
szCommand[iLastCharPos] = '\0';
szCommand++;
}
if (!stripped_command.Tokenize(szCommand)) {
PythonLog(0, "Failed to tokenize '%s'.", command.GetCommandString());
return;
}
bool block = false;
// 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
)
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)
{
block = true;
}
}
if (block)
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 = strdup(szName);
}
//-----------------------------------------------------------------------------
// CSayCommandManager destructor.
//-----------------------------------------------------------------------------
CSayCommandManager::~CSayCommandManager()
{
free((char*)m_Name);
}
//-----------------------------------------------------------------------------
// Adds a callable to a CSayCommandManager instance.
//-----------------------------------------------------------------------------
void CSayCommandManager::AddCallback( PyObject* pCallable )
{
m_vecCallables.RegisterListener(pCallable);
}
//-----------------------------------------------------------------------------
// Removes a callable from a CSayCommandManager instance.
//-----------------------------------------------------------------------------
void CSayCommandManager::RemoveCallback( PyObject* pCallable )
{
m_vecCallables.UnregisterListener(pCallable);
// Are there any more callbacks registered for this command?
if( !m_vecCallables.GetCount() )
{
// Remove the CSayCommandManager instance
RemoveCSayCommandManager(m_Name);
}
}
//-----------------------------------------------------------------------------
// Dispatches the say command.
//-----------------------------------------------------------------------------
CommandReturn CSayCommandManager::Dispatch( const CCommand& command, int iIndex, bool bTeamOnly)
{
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, bTeamOnly
)
if (block)
return BLOCK;
return CONTINUE;
}
const char* CSayCommandManager::GetName()
{
return m_Name;
}
//-----------------------------------------------------------------------------
// Registers the say and say_team commands.
//-----------------------------------------------------------------------------
void RegisterSayCommands()
{
s_SayCommand.RegisterCommands();
}
//-----------------------------------------------------------------------------
// Unregisters the say and say_team commands.
//-----------------------------------------------------------------------------
void UnregisterSayCommands()
{
s_SayCommand.UnregisterCommands();
}