From de8c9b23eb8045f50205d1c5966e817584f9e300 Mon Sep 17 00:00:00 2001 From: oy Date: Mon, 9 Jan 2012 01:25:18 +0100 Subject: [PATCH] added command to toggle config values. Closes #888 --- src/engine/shared/console.cpp | 72 +++++++++++++++++++++++++++++++++++ src/engine/shared/console.h | 2 + 2 files changed, 74 insertions(+) diff --git a/src/engine/shared/console.cpp b/src/engine/shared/console.cpp index 31c2281dc..443c59047 100644 --- a/src/engine/shared/console.cpp +++ b/src/engine/shared/console.cpp @@ -574,6 +574,75 @@ static void StrVariableCommand(IConsole::IResult *pResult, void *pUserData) } } +void CConsole::ConToggle(IConsole::IResult *pResult, void *pUser) +{ + CConsole* pConsole = static_cast(pUser); + char aBuf[128] = {0}; + CCommand *pCommand = pConsole->FindCommand(pResult->GetString(0), pConsole->m_FlagMask); + if(pCommand) + { + FCommandCallback pfnCallback = pCommand->m_pfnCallback; + void *pUserData = pCommand->m_pUserData; + + // check for chain + if(pCommand->m_pfnCallback == Con_Chain) + { + CChain *pChainInfo = static_cast(pCommand->m_pUserData); + pfnCallback = pChainInfo->m_pfnCallback; + pUserData = pChainInfo->m_pCallbackUserData; + } + + if(pfnCallback == IntVariableCommand) + { + CIntVariableData *pData = static_cast(pUserData); + int Val = *(pData->m_pVariable)==pResult->GetInteger(1) ? pResult->GetInteger(2) : pResult->GetInteger(1); + str_format(aBuf, sizeof(aBuf), "%s %i", pResult->GetString(0), Val); + pConsole->ExecuteLine(aBuf); + aBuf[0] = 0; + } + else + str_format(aBuf, sizeof(aBuf), "Invalid command: '%s'.", pResult->GetString(0)); + } + else + str_format(aBuf, sizeof(aBuf), "No such command: '%s'.", pResult->GetString(0)); + + if(aBuf[0] != 0) + pConsole->Print(OUTPUT_LEVEL_STANDARD, "Console", aBuf); +} + +void CConsole::ConToggleStroke(IConsole::IResult *pResult, void *pUser) +{ + CConsole* pConsole = static_cast(pUser); + char aBuf[128] = {0}; + CCommand *pCommand = pConsole->FindCommand(pResult->GetString(1), pConsole->m_FlagMask); + if(pCommand) + { + FCommandCallback pfnCallback = pCommand->m_pfnCallback; + + // check for chain + if(pCommand->m_pfnCallback == Con_Chain) + { + CChain *pChainInfo = static_cast(pCommand->m_pUserData); + pfnCallback = pChainInfo->m_pfnCallback; + } + + if(pfnCallback == IntVariableCommand) + { + int Val = pResult->GetInteger(0)==0 ? pResult->GetInteger(3) : pResult->GetInteger(2); + str_format(aBuf, sizeof(aBuf), "%s %i", pResult->GetString(1), Val); + pConsole->ExecuteLine(aBuf); + aBuf[0] = 0; + } + else + str_format(aBuf, sizeof(aBuf), "Invalid command: '%s'.", pResult->GetString(1)); + } + else + str_format(aBuf, sizeof(aBuf), "No such command: '%s'.", pResult->GetString(1)); + + if(aBuf[0] != 0) + pConsole->Print(OUTPUT_LEVEL_STANDARD, "Console", aBuf); +} + CConsole::CConsole(int FlagMask) { m_FlagMask = FlagMask; @@ -595,6 +664,9 @@ CConsole::CConsole(int FlagMask) Register("echo", "r", CFGFLAG_SERVER|CFGFLAG_CLIENT, Con_Echo, this, "Echo the text"); Register("exec", "r", CFGFLAG_SERVER|CFGFLAG_CLIENT, Con_Exec, this, "Execute the specified file"); + Register("toggle", "sii", CFGFLAG_SERVER|CFGFLAG_CLIENT, ConToggle, this, "Toggle config value"); + Register("+toggle", "sii", CFGFLAG_CLIENT, ConToggleStroke, this, "Toggle config value via keypress"); + Register("mod_command", "s?i", CFGFLAG_SERVER, ConModCommandAccess, this, "Specify command accessibility for moderators"); Register("mod_status", "", CFGFLAG_SERVER, ConModCommandStatus, this, "List all commands which are accessible for moderators"); diff --git a/src/engine/shared/console.h b/src/engine/shared/console.h index 61798c37d..bbe267d42 100644 --- a/src/engine/shared/console.h +++ b/src/engine/shared/console.h @@ -54,6 +54,8 @@ class CConsole : public IConsole static void Con_Chain(IResult *pResult, void *pUserData); static void Con_Echo(IResult *pResult, void *pUserData); static void Con_Exec(IResult *pResult, void *pUserData); + static void ConToggle(IResult *pResult, void *pUser); + static void ConToggleStroke(IResult *pResult, void *pUser); static void ConModCommandAccess(IResult *pResult, void *pUser); static void ConModCommandStatus(IConsole::IResult *pResult, void *pUser);