Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Client/mods/deathmatch/ClientCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*****************************************************************************/

#include "StdInc.h"
#include "logic/CRegisteredCommands.h"
#include <game/CWeapon.h>
#include <game/CTaskManager.h>
#include <game/Task.h>
Expand Down Expand Up @@ -57,7 +58,12 @@ bool COMMAND_Executed(const char* szCommand, const char* szArguments, bool bHand
strClumpedCommandUTF = strClumpedCommandUTF.substr(0, MAX_COMMAND_LENGTH);
strClumpedCommand = UTF16ToMbUTF8(strClumpedCommandUTF);

g_pClientGame->GetRegisteredCommands()->ProcessCommand(szCommandBufferPointer, szArguments);
CommandExecutionResult commandResult = g_pClientGame->GetRegisteredCommands()->ProcessCommand(szCommandBufferPointer, szArguments, false);

if (commandResult.wasCancelled)
{
return true;
}

// Call the onClientConsole event
CClientPlayer* localPlayer = g_pClientGame->GetLocalPlayer();
Expand Down Expand Up @@ -108,7 +114,7 @@ bool COMMAND_Executed(const char* szCommand, const char* szArguments, bool bHand

// Call our comand-handlers for core-executed commands too, if allowed
if (bAllowScriptedBind)
g_pClientGame->GetRegisteredCommands()->ProcessCommand(szCommand, szArguments);
g_pClientGame->GetRegisteredCommands()->ProcessCommand(szCommand, szArguments, false);
}
return false;
}
Expand Down
1 change: 1 addition & 0 deletions Client/mods/deathmatch/logic/CClientGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2730,6 +2730,7 @@ void CClientGame::AddBuiltInEvents()
// Console events
m_Events.AddEvent("onClientConsole", "text", NULL, false);
m_Events.AddEvent("onClientCoreCommand", "command", NULL, false);
m_Events.AddEvent("onClientCommand", "command, ..., executedByFunction", NULL, false);

// Chat events
m_Events.AddEvent("onClientChatMessage", "text, r, g, b, messageType", NULL, false);
Expand Down
32 changes: 27 additions & 5 deletions Client/mods/deathmatch/logic/CRegisteredCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,36 @@ bool CRegisteredCommands::CommandExists(const char* szKey, CLuaMain* pLuaMain)
return GetCommand(szKey, pLuaMain) != nullptr;
}

bool CRegisteredCommands::ProcessCommand(const char* szKey, const char* szArguments)
CommandExecutionResult CRegisteredCommands::ProcessCommand(const char* szKey, const char* szArguments, bool executedByFunction)
{
assert(szKey);

CommandExecutionResult result;

CLuaArguments arguments;
arguments.PushString(szKey);

if (szArguments && *szArguments)
{
const std::string_view argsView{szArguments};
std::istringstream stream{std::string{argsView}};

for (std::string arg; stream >> arg;)
{
arguments.PushString(arg.c_str());
}
}

arguments.PushBoolean(executedByFunction);

g_pClientGame->GetRootEntity()->CallEvent("onClientCommand", arguments, true);
result.wasCancelled = g_pClientGame->GetEvents()->WasEventCancelled();

if (result.wasCancelled)
return result;

// Call the handler for every virtual machine that matches the given key
int iCompareResult;
bool bHandled = false;
m_bIteratingList = true;
list<SCommand*>::const_iterator iter = m_Commands.begin();
for (; iter != m_Commands.end(); iter++)
Expand All @@ -171,14 +194,13 @@ bool CRegisteredCommands::ProcessCommand(const char* szKey, const char* szArgume
{
// Call it
CallCommandHandler((*iter)->pLuaMain, (*iter)->iLuaFunction, (*iter)->strKey, szArguments);
bHandled = true;
result.wasExecuted = true;
}
}
m_bIteratingList = false;
TakeOutTheTrash();

// Return whether some handler was called or not
return bHandled;
return result;
}

CRegisteredCommands::SCommand* CRegisteredCommands::GetCommand(const char* szKey, class CLuaMain* pLuaMain)
Expand Down
8 changes: 7 additions & 1 deletion Client/mods/deathmatch/logic/CRegisteredCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ enum class MultiCommandHandlerPolicy : std::uint8_t
ALLOW = 2
};

struct CommandExecutionResult
{
bool wasCancelled = false;
bool wasExecuted = false;
};

class CRegisteredCommands
{
struct SCommand
Expand All @@ -48,7 +54,7 @@ class CRegisteredCommands
void GetCommands(lua_State* luaVM);
void GetCommands(lua_State* luaVM, CLuaMain* pTargetLuaMain);

bool ProcessCommand(const char* szKey, const char* szArguments);
CommandExecutionResult ProcessCommand(const char* szKey, const char* szArguments, bool executedByFunction = false);

private:
SCommand* GetCommand(const char* szKey, class CLuaMain* pLuaMain = NULL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*****************************************************************************/

#include "StdInc.h"
#include "CRegisteredCommands.h"

int CLuaFunctionDefs::AddCommandHandler(lua_State* luaVM)
{
Expand Down Expand Up @@ -90,7 +91,8 @@ int CLuaFunctionDefs::ExecuteCommandHandler(lua_State* luaVM)
if (pLuaMain)
{
// Call it
if (m_pRegisteredCommands->ProcessCommand(strKey, strArgs))
CommandExecutionResult result = m_pRegisteredCommands->ProcessCommand(strKey, strArgs, true);
if (result.wasExecuted && !result.wasCancelled)
{
lua_pushboolean(luaVM, true);
return 1;
Expand Down
1 change: 1 addition & 0 deletions Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class CLuaFunctionDefinitions;
#include "CLuaTimerManager.h"

class CRegisteredCommands;
struct CommandExecutionResult;

#define LUA_DECLARE(x) static int x ( lua_State * luaVM );

Expand Down