Skip to content

Commit 7ef0093

Browse files
committed
Say commands are now properly splitted
1 parent ab547c5 commit 7ef0093

File tree

3 files changed

+35
-36
lines changed

3 files changed

+35
-36
lines changed

src/core/modules/commands/commands.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ class CCommandExt
6969
const char* szValue = command.GetCommandString();
7070
return PyUnicode_DecodeUTF8(szValue, strlen(szValue), "ignore");
7171
}
72+
73+
static bool Tokenize(CCommand& command, const char* szCommand)
74+
{
75+
return command.Tokenize(szCommand);
76+
}
7277
};
7378

7479

src/core/modules/commands/commands_say.cpp

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,22 @@ void SayConCommand::Dispatch( const CCommand& command )
225225
bool bTeamOnly = strcmp(command.Arg(0), "say_team") == 0;
226226

227227
// Create a new CCommand object that does not contain the first argument
228-
// (say or say_team) as that is redundant
229-
//CCommand( int nArgC, const char **ppArgV )
230-
const char** argv = command.ArgV();
231-
argv++;
232-
const CCommand stripped_command = CCommand(command.ArgC() - 1, argv);
228+
// (say or say_team) and is properly splitted
229+
CCommand stripped_command = CCommand();
230+
char* szCommand = (char*) command.ArgS();
231+
232+
// Remove quotes (if existant), so the arguments are not recognized as a
233+
// single argument.
234+
int iLastCharPos = strlen(szCommand) - 1;
235+
if (szCommand[0] == '"' && szCommand[iLastCharPos] == '"') {
236+
szCommand[iLastCharPos] = '\0';
237+
szCommand++;
238+
}
239+
240+
if (!stripped_command.Tokenize(szCommand)) {
241+
PythonLog(0, "Failed to tokenize '%s'.", command.GetCommandString());
242+
return;
243+
}
233244

234245
// Loop through all registered Say Filter callbacks
235246
for(int i = 0; i < s_SayFilters.m_vecCallables.Count(); i++)
@@ -252,39 +263,18 @@ void SayConCommand::Dispatch( const CCommand& command )
252263
END_BOOST_PY_NORET()
253264
}
254265

255-
// Get the name of the command used
256-
std::string szCommandString (stripped_command.Arg(0));
257-
258-
// Don't handle empty command strings. This would cause a crash.
259-
if (!szCommandString.empty())
266+
// Find if the command is registered
267+
SayCommandMap::iterator commandMapIter = g_SayCommandMap.find(szCommand);
268+
if( commandMapIter != g_SayCommandMap.end() )
260269
{
261-
262-
// Copy the string to get a char instance
263-
char * szCopyCommandString = new char [szCommandString.length() + 1];
264-
std::strcpy(szCopyCommandString, szCommandString.c_str());
265-
266-
// Split the command using <space> as the delimiter
267-
// This should be the actual Say Command
268-
char * szCommand = std::strtok(szCopyCommandString, " ");
269-
270-
// Is there a command?
271-
// This check fixes https://github.com/Source-Python-Dev-Team/Source.Python/issues/17
272-
if (szCommand)
273-
{
274-
// Find if the command is registered
275-
SayCommandMap::iterator commandMapIter = g_SayCommandMap.find(szCommand);
276-
if( commandMapIter != g_SayCommandMap.end() )
277-
{
278-
// Get the CSayCommandManager instance for the command
279-
CSayCommandManager* pCSayCommandManager = commandMapIter->second;
270+
// Get the CSayCommandManager instance for the command
271+
CSayCommandManager* pCSayCommandManager = commandMapIter->second;
280272

281-
// Call the command and see it wants to block the command
282-
if( pCSayCommandManager->Dispatch(stripped_command, iIndex, bTeamOnly) == BLOCK)
283-
{
284-
// Block the command
285-
return;
286-
}
287-
}
273+
// Call the command and see it wants to block the command
274+
if( pCSayCommandManager->Dispatch(stripped_command, iIndex, bTeamOnly) == BLOCK)
275+
{
276+
// Block the command
277+
return;
288278
}
289279
}
290280

src/core/modules/commands/commands_wrap.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ void export_command(scope _commands)
123123
args("index")
124124
)
125125

126+
.def("tokenize",
127+
&CCommandExt::Tokenize
128+
)
129+
126130
.def("get_max_command_length",
127131
&CCommand::MaxCommandLength
128132
)

0 commit comments

Comments
 (0)