forked from Source-Python-Dev-Team/Source.Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_command_wrap.cpp
More file actions
272 lines (238 loc) · 9.77 KB
/
Copy pathserver_command_wrap.cpp
File metadata and controls
272 lines (238 loc) · 9.77 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
/**
* =============================================================================
* 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 "server_command_wrap.h"
#include "command_wrap.h"
#include "utility/call_python.h"
#include "boost/python/call.hpp"
#include "boost/shared_array.hpp"
#include "modules/cvar/cvar_wrap.h"
//-----------------------------------------------------------------------------
// Externs.
//-----------------------------------------------------------------------------
extern ICvar* g_pCVar;
//-----------------------------------------------------------------------------
// ConVar Accessor class and registration
//
// NOTE: This is used to ensure any new ConCommands created get registered to the
// active ICVar instance.
//-----------------------------------------------------------------------------
class CPluginConVarAccessor : public IConCommandBaseAccessor
{
public:
virtual bool RegisterConCommandBase(ConCommandBase* pCommand)
{
g_pCVar->RegisterConCommand(pCommand);
return true;
}
};
CPluginConVarAccessor g_ConVarAccessor;
//-----------------------------------------------------------------------------
// Registers the ConVar accessor above to take in new ConCommand requests.
//-----------------------------------------------------------------------------
void InitServerCommands()
{
ConVar_Register(0, &g_ConVarAccessor);
}
//-----------------------------------------------------------------------------
// Global server command mapping.
//-----------------------------------------------------------------------------
typedef boost::unordered_map<std::string, CServerCommandManager*> ServerCommandMap;
ServerCommandMap g_ServerCommandMap;
//-----------------------------------------------------------------------------
// Returns a CServerCommandManager for the given command name.
//-----------------------------------------------------------------------------
CServerCommandManager* get_server_command(const char* szName,
const char* szHelpText = 0, int iFlags = 0)
{
// Find if the given name is a registered server command
ServerCommandMap::iterator commandMapIter = g_ServerCommandMap.find(szName);
if( commandMapIter == g_ServerCommandMap.end())
{
// If the command is not already registered, add the name and the CServerCommandManager instance to the mapping
g_ServerCommandMap.insert(std::make_pair(szName, CServerCommandManager::CreateCommand(szName, szHelpText, iFlags)));
// Get the server command in the mapping
commandMapIter = g_ServerCommandMap.find(szName);
}
// Return the CServerCommandManager instance for the command
return commandMapIter->second;
}
//-----------------------------------------------------------------------------
// Removes a CServerCommandManager instance for the given name.
//-----------------------------------------------------------------------------
void RemoveCServerCommandManager(const char* szName)
{
// Find if the given name is a registered server command
ServerCommandMap::iterator commandMapIter = g_ServerCommandMap.find(szName);
if( commandMapIter != g_ServerCommandMap.end())
{
// If the command is registered, delete the CServerCommandManager instance
// and remove the command from the mapping
delete commandMapIter->second;
g_ServerCommandMap.erase(commandMapIter);
}
}
//-----------------------------------------------------------------------------
// Returns a CServerCommandManager instance.
//-----------------------------------------------------------------------------
CServerCommandManager* CServerCommandManager::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 CServerCommandManager(pConCommand, szNameCopy, szHelpTextCopy, iFlags);
}
//-----------------------------------------------------------------------------
// CServerCommandManager constructor.
//-----------------------------------------------------------------------------
CServerCommandManager::CServerCommandManager(ConCommand* pConCommand,
const char* szName, const char* szHelpText, int iFlags):
ConCommand(szName, (FnCommandCallback_t)NULL, szHelpText, iFlags),
m_pOldCommand(pConCommand)
{
m_Name = szName;
}
//-----------------------------------------------------------------------------
// CServerCommandManager destructor.
//-----------------------------------------------------------------------------
CServerCommandManager::~CServerCommandManager()
{
// 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);
}
}
//-----------------------------------------------------------------------------
// CServerCommandManager Init override.
//-----------------------------------------------------------------------------
void CServerCommandManager::Init()
{
ConCommand::Init();
}
//-----------------------------------------------------------------------------
// Adds a callable to a CServerCommandManager instance.
//-----------------------------------------------------------------------------
void CServerCommandManager::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 CServerCommandManager instance.
//-----------------------------------------------------------------------------
void CServerCommandManager::remove_callback( PyObject* pCallable )
{
// Get the object instance of the callable
object oCallable = object(handle<>(borrowed(pCallable)));
// Remove the callback from the CServerCommandManager instance
m_vecCallables.FindAndRemove(oCallable);
// Are there any more callbacks registered for this command?
if( !m_vecCallables.Count() )
{
// Remove the CServerCommandManager instance
RemoveCServerCommandManager(m_Name);
}
}
//-----------------------------------------------------------------------------
// Calls all callables for the command when it is called on the server.
//-----------------------------------------------------------------------------
void CServerCommandManager::Dispatch( const CCommand &command )
{
// Get the CICommand instance for the CCommand instance
CICommand* ccommand = new CICommand(&command);
// Loop through all registered callbacks for the command
// (use equals also to know when to call the old callback)
for(int i = 0; i <= m_vecCallables.Count(); i++)
{
// Is the current iteration for a registered callback?
if( i < m_vecCallables.Count() )
{
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, ccommand);
// Does the callable wish to block the command?
if( !returnValue.is_none() && extract<int>(returnValue) == (int)BLOCK)
{
// Block the command
break;
}
END_BOOST_PY_NORET()
}
// Was the command previously registered?
else if(m_pOldCommand)
{
// Call the old callback
m_pOldCommand->Dispatch(command);
}
}
}
//-----------------------------------------------------------------------------
// Removes all CServerCommandManager instances.
//-----------------------------------------------------------------------------
void ClearAllServerCommands()
{
// Loop through all items in the mapping
for(ServerCommandMap::iterator commandMapIter = g_ServerCommandMap.begin(); commandMapIter != g_ServerCommandMap.end(); ++commandMapIter)
{
// Remove the CServerCommandManager instance
delete commandMapIter->second;
}
// Clear the mapping
g_ServerCommandMap.clear();
}