added command to toggle config values. Closes #888

This commit is contained in:
oy 2012-01-09 01:25:18 +01:00
parent 4fde2cf7f2
commit de8c9b23eb
2 changed files with 74 additions and 0 deletions

View file

@ -574,6 +574,75 @@ static void StrVariableCommand(IConsole::IResult *pResult, void *pUserData)
}
}
void CConsole::ConToggle(IConsole::IResult *pResult, void *pUser)
{
CConsole* pConsole = static_cast<CConsole *>(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<CChain *>(pCommand->m_pUserData);
pfnCallback = pChainInfo->m_pfnCallback;
pUserData = pChainInfo->m_pCallbackUserData;
}
if(pfnCallback == IntVariableCommand)
{
CIntVariableData *pData = static_cast<CIntVariableData *>(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<CConsole *>(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<CChain *>(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");

View file

@ -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);