Skip to content

Commit

Permalink
Fix #406: improve help command in server console (#1639)
Browse files Browse the repository at this point in the history
* Improve help command in server console

* Add more definitions from the wiki

* Update CMainConfig.h

* Update CConsole.h

* Update CConsoleCommand.h

* refactor from string to const char*
  • Loading branch information
Unde-R committed Oct 30, 2020
1 parent 30eda4f commit a4267de
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 82 deletions.
4 changes: 2 additions & 2 deletions Server/mods/deathmatch/logic/CConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ bool CConsole::HandleInput(const char* szCommand, CClient* pClient, CClient* pEc
return false;
}

void CConsole::AddCommand(FCommandHandler* pHandler, const char* szCommand, bool bRestricted)
void CConsole::AddCommand(FCommandHandler* pHandler, const char* szCommand, bool bRestricted, const char* szConsoleHelpText)
{
// Make a command class and add it to the list
CConsoleCommand* pCommand = new CConsoleCommand(pHandler, szCommand, bRestricted);
CConsoleCommand* pCommand = new CConsoleCommand(pHandler, szCommand, bRestricted, szConsoleHelpText);
m_Commands.push_back(pCommand);
}

Expand Down
3 changes: 2 additions & 1 deletion Server/mods/deathmatch/logic/CConsole.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ class CConsole

bool HandleInput(const char* szCommand, CClient* pClient, CClient* pEchoClient);

void AddCommand(FCommandHandler* pHandler, const char* szCommand, bool bRestricted);
void AddCommand(FCommandHandler* pHandler, const char* szCommand, bool bRestricted, const char* szConsoleHelpText);
void DeleteCommand(const char* szCommand);
void DeleteAllCommands();
CConsoleCommand* GetCommand(const char* szKey);

list<CConsoleCommand*>::const_iterator CommandsBegin() { return m_Commands.begin(); };
list<CConsoleCommand*>::const_iterator CommandsEnd() { return m_Commands.end(); };
const auto& CommandsList() { return m_Commands; }

class CBlipManager* GetBlipManager() { return m_pBlipManager; };
class CLuaManager* GetLuaManager() { return m_pLuaManager; };
Expand Down
3 changes: 2 additions & 1 deletion Server/mods/deathmatch/logic/CConsoleCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#include "StdInc.h"

CConsoleCommand::CConsoleCommand(FCommandHandler* pHandler, const char* szCommand, bool bRestricted)
CConsoleCommand::CConsoleCommand(FCommandHandler* pHandler, const char* szCommand, bool bRestricted, const char* szConsoleHelpText)
{
// Init
m_pHandler = pHandler;
Expand All @@ -20,6 +20,7 @@ CConsoleCommand::CConsoleCommand(FCommandHandler* pHandler, const char* szComman
m_szCommand = new char[strlen(szCommand) + 1];
strcpy(m_szCommand, szCommand);
m_bRestricted = bRestricted;
m_szConsoleHelpText = szConsoleHelpText;
}

bool CConsoleCommand::operator()(CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient)
Expand Down
4 changes: 3 additions & 1 deletion Server/mods/deathmatch/logic/CConsoleCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ typedef bool(FCommandHandler)(class CConsole*, const char*, CClient*, CClient*);
class CConsoleCommand
{
public:
CConsoleCommand(FCommandHandler* pHandler, const char* szCommand, bool bRestricted);
CConsoleCommand(FCommandHandler* pHandler, const char* szCommand, bool bRestricted, const char* szConsoleHelpText);
~CConsoleCommand() { delete[] m_szCommand; };

bool operator()(class CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient);
FCommandHandler* GetHandler() { return m_pHandler; };
const char* GetCommand() { return m_szCommand; };
bool IsRestricted() { return m_bRestricted; };
const char* GetHelp() { return m_szConsoleHelpText; };

private:
FCommandHandler* m_pHandler;
char* m_szCommand;
bool m_bRestricted;
const char* m_szConsoleHelpText;
};
61 changes: 40 additions & 21 deletions Server/mods/deathmatch/logic/CConsoleCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1283,32 +1283,51 @@ bool CConsoleCommands::DebugScript(CConsole* pConsole, const char* szArguments,
bool CConsoleCommands::Help(CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient)
{
// Help string
std::string strHelpText = "Available commands:\n\n";

// Loop through all added commands
int iCount = 0;
list<CConsoleCommand*>::const_iterator iter = pConsole->CommandsBegin();
for (; iter != pConsole->CommandsEnd(); iter++)
if (!szArguments)
{
// Add a new line every third command
if (iCount == 3)
std::string strHelpText = "Available commands:\n\n";
pEchoClient->SendConsole("help [command]");

// Loop through all added commands
int iCount = 0;
for (CConsoleCommand* command : pConsole->CommandsList())
{
iCount = 0;
strHelpText.append("\n");
}
// Add a new line every third command
if (iCount == 3)
{
iCount = 0;
strHelpText.append("\n");
}

// Add the commandname and pad it to 20 letters with spaces
const char* szCommand = (*iter)->GetCommand();
strHelpText.append(szCommand);
strHelpText.append(25 - strlen(szCommand), ' ');
// Add the commandname and pad it to 20 letters with spaces
const char* szCommand = (command)->GetCommand();
strHelpText.append(szCommand);
strHelpText.append(25 - strlen(szCommand), ' ');

// Increment count so we can keep track of how many we've put at one line
++iCount;
}
// Increment count so we can keep track of how many we've put at one line
++iCount;
}

// Show it
pEchoClient->SendConsole(strHelpText.c_str());
return true;
// Show it
pEchoClient->SendConsole(strHelpText.c_str());
return true;
}
else
{
// help [command]
if (szArguments && !strcmp(szArguments, "help") == 0)
{
CConsoleCommand* pConsoleCommand = pConsole->GetCommand(szArguments);
if (pConsoleCommand)
{
pEchoClient->SendConsole(pConsoleCommand->GetHelp());
return true;
}
else
pEchoClient->SendConsole("Couldn't find the command.");
}
}
return false;
}

bool CConsoleCommands::ReloadBans(CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient)
Expand Down
110 changes: 55 additions & 55 deletions Server/mods/deathmatch/logic/CMainConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,59 +740,59 @@ bool CMainConfig::LoadExtended()
CLogger::SetMinLogLevel(LOGLEVEL_LOW);

// Register the commands
RegisterCommand("start", CConsoleCommands::StartResource, false);
RegisterCommand("stop", CConsoleCommands::StopResource, false);
RegisterCommand("stopall", CConsoleCommands::StopAllResources, false);
RegisterCommand("restart", CConsoleCommands::RestartResource, false);
RegisterCommand("refresh", CConsoleCommands::RefreshResources, false);
RegisterCommand("refreshall", CConsoleCommands::RefreshAllResources, false);
RegisterCommand("list", CConsoleCommands::ListResources, false);
RegisterCommand("info", CConsoleCommands::ResourceInfo, false);
RegisterCommand("upgrade", CConsoleCommands::UpgradeResources, false);
RegisterCommand("check", CConsoleCommands::CheckResources, false);

RegisterCommand("say", CConsoleCommands::Say, false);
RegisterCommand("teamsay", CConsoleCommands::TeamSay, false);
RegisterCommand("msg", CConsoleCommands::Msg, false);
RegisterCommand("me", CConsoleCommands::Me, false);
RegisterCommand("nick", CConsoleCommands::Nick, false);

RegisterCommand("login", CConsoleCommands::LogIn, false);
RegisterCommand("logout", CConsoleCommands::LogOut, false);
RegisterCommand("chgmypass", CConsoleCommands::ChgMyPass, false);

RegisterCommand("addaccount", CConsoleCommands::AddAccount, false);
RegisterCommand("delaccount", CConsoleCommands::DelAccount, false);
RegisterCommand("chgpass", CConsoleCommands::ChgPass, false);
RegisterCommand("shutdown", CConsoleCommands::Shutdown, false);

RegisterCommand("aexec", CConsoleCommands::AExec, false);

RegisterCommand("whois", CConsoleCommands::WhoIs, false);

RegisterCommand("debugscript", CConsoleCommands::DebugScript, false);

RegisterCommand("help", CConsoleCommands::Help, false);

RegisterCommand("loadmodule", CConsoleCommands::LoadModule, false);
RegisterCommand("unloadmodule", CConsoleCommands::UnloadModule, false);
RegisterCommand("reloadmodule", CConsoleCommands::ReloadModule, false);

RegisterCommand("ver", CConsoleCommands::Ver, false);
RegisterCommand("sver", CConsoleCommands::Ver, false);
RegisterCommand("ase", CConsoleCommands::Ase, false);
RegisterCommand("openports", CConsoleCommands::OpenPortsTest, false);

RegisterCommand("debugdb", CConsoleCommands::SetDbLogLevel, false);

RegisterCommand("reloadbans", CConsoleCommands::ReloadBans, false);

RegisterCommand("aclrequest", CConsoleCommands::AclRequest, false);
RegisterCommand("authserial", CConsoleCommands::AuthorizeSerial, false);
RegisterCommand("reloadacl", CConsoleCommands::ReloadAcl, false);
RegisterCommand("debugjoinflood", CConsoleCommands::DebugJoinFlood, false);
RegisterCommand("debuguptime", CConsoleCommands::DebugUpTime, false);
RegisterCommand("sfakelag", CConsoleCommands::FakeLag, false);
RegisterCommand("start", CConsoleCommands::StartResource, false, "Usage: start <resource-name>\nStart a loaded resource eg: start admin");
RegisterCommand("stop", CConsoleCommands::StopResource, false, "Usage: stop <resource-name>\nStop a resource eg: stop admin");
RegisterCommand("stopall", CConsoleCommands::StopAllResources, false, "Stop all running resources");
RegisterCommand("restart", CConsoleCommands::RestartResource, false, "Usage: restart <resource-name>\nRestarts a running resource eg: restart admin");
RegisterCommand("refresh", CConsoleCommands::RefreshResources, false, "Refresh resource list to find new resources");
RegisterCommand("refreshall", CConsoleCommands::RefreshAllResources, false, "Refresh resources and restart any changed resources");
RegisterCommand("list", CConsoleCommands::ListResources, false, "Shows a list of resources");
RegisterCommand("info", CConsoleCommands::ResourceInfo, false, "Usage: info <resource-name>\nGet info for a resource eg: info admin");
RegisterCommand("upgrade", CConsoleCommands::UpgradeResources, false, "Usage: upgrade [ all | <resource-name> ]\nPerform a basic upgrade of all resources.");
RegisterCommand("check", CConsoleCommands::CheckResources, false, "Usage: check [ all | <resource-name> ]\nChecks which files would be changed with upgrade command. Does not modify anything.");

RegisterCommand("say", CConsoleCommands::Say, false, "Usage: say <text>\nShow a message to all players on the server eg: say hello");
RegisterCommand("teamsay", CConsoleCommands::TeamSay, false, "Usage: teamsay <test>\nSend a message to all players on the same team");
RegisterCommand("msg", CConsoleCommands::Msg, false, "Usage: msg <nick> <text>\nSend a message to a player eg: msg playername hello");
RegisterCommand("me", CConsoleCommands::Me, false, "Usage: me <text>\nShow a message to all players on the server, with your nick prepended");
RegisterCommand("nick", CConsoleCommands::Nick, false, "Usage: nick <old-nick> <new-nick>\nChange your ingame nickname");

RegisterCommand("login", CConsoleCommands::LogIn, false, "Usage: login <accountname> <password>\nLogin to an account eg: login accountname password");
RegisterCommand("logout", CConsoleCommands::LogOut, false, "Log out of the current account");
RegisterCommand("chgmypass", CConsoleCommands::ChgMyPass, false, "Usage: chgmypass <oldpass> <newpass>\nChange your password eg: chgmypass oldpw newpw");

RegisterCommand("addaccount", CConsoleCommands::AddAccount, false, "Usage: addaccount <accountname> <password>\nAdd an account eg: addaccount accountname password");
RegisterCommand("delaccount", CConsoleCommands::DelAccount, false, "Usage: delaccount <accountname>\nDelete an account eg: delaccount accountname");
RegisterCommand("chgpass", CConsoleCommands::ChgPass, false, "Usage: chgpass <accountname> <password>\nChange an accounts password eg: chgpass account newpw");
RegisterCommand("shutdown", CConsoleCommands::Shutdown, false, "Usage: shutdown <reason>\nShutdown the server eg: shutdown put reason here");

RegisterCommand("aexec", CConsoleCommands::AExec, false, "Usage: aexec <nick> <command>\nForce a player to execute a command eg: aexec playername say hello");

RegisterCommand("whois", CConsoleCommands::WhoIs, false, "Usage: whois <nick>\nGet the IP of a player currently connected (use whowas for IP/serial/version)");

RegisterCommand("debugscript", CConsoleCommands::DebugScript, false, "Usage: debugscript <0-3>\nRemove (This does not work 'Incorrect client type for this command')");

RegisterCommand("help", CConsoleCommands::Help, false, "");

RegisterCommand("loadmodule", CConsoleCommands::LoadModule, false, "Usage: loadmodule <module-filename>\nLoad a module eg: loadmodule ml_sockets.dll");
RegisterCommand("unloadmodule", CConsoleCommands::UnloadModule, false, "Usage: unloadmodule <module-filename>\nUnload a module eg: unloadmodule ml_sockets.dll");
RegisterCommand("reloadmodule", CConsoleCommands::ReloadModule, false, "Usage: reloadmodule <module-filename>\nReload a module eg: reloadmodule ml_sockets.dll");

RegisterCommand("ver", CConsoleCommands::Ver, false, "Get the MTA version");
RegisterCommand("sver", CConsoleCommands::Ver, false, "Get the server MTA version");
RegisterCommand("ase", CConsoleCommands::Ase, false, "See the amount of master server list queries");
RegisterCommand("openports", CConsoleCommands::OpenPortsTest, false, "Test if server ports are open");

RegisterCommand("debugdb", CConsoleCommands::SetDbLogLevel, false, "Usage: debugdb <0-2>\nSet logging level for database functions. [0-Off 1-Errors only 2-All]");

RegisterCommand("reloadbans", CConsoleCommands::ReloadBans, false, "Reloads all the bans from banlist.xml.");

RegisterCommand("aclrequest", CConsoleCommands::AclRequest, false, "Usage: aclrequest [ list | allow | deny ] <resource-name> [ <right> | all ]\nManage ACL requests from resources implementing <aclrequest> in their meta.xml");
RegisterCommand("authserial", CConsoleCommands::AuthorizeSerial, false, "Usage: authserial <account-name> [list|removelast|httppass]\nManage serial authentication for an account.");
RegisterCommand("reloadacl", CConsoleCommands::ReloadAcl, false, "Perform a simple ACL reload");
RegisterCommand("debugjoinflood", CConsoleCommands::DebugJoinFlood, false, "Shows debug information regarding the join flood mitigation feature.");
RegisterCommand("debuguptime", CConsoleCommands::DebugUpTime, false, "Shows how many days the server has been up and running.");
RegisterCommand("sfakelag", CConsoleCommands::FakeLag, false, "Usage: sfakelag <packet loss> <extra ping> <ping variance> [<KBPS limit>]\nOnly available if enabled in the mtaserver.conf file.\nAdds artificial packet loss, ping, jitter and bandwidth limits to the server-client connections.");
return true;
}

Expand Down Expand Up @@ -914,10 +914,10 @@ bool CMainConfig::SetFPSLimit(unsigned short usFPS, bool bSave)
return false;
}

void CMainConfig::RegisterCommand(const char* szName, FCommandHandler* pFunction, bool bRestricted)
void CMainConfig::RegisterCommand(const char* szName, FCommandHandler* pFunction, bool bRestricted, const char* szConsoleHelpText)
{
// Register the function with the given name and function pointer
m_pConsole->AddCommand(pFunction, szName, bRestricted);
m_pConsole->AddCommand(pFunction, szName, bRestricted, szConsoleHelpText);
}

void CMainConfig::SetCommandLineParser(CCommandLineParser* pCommandLineParser)
Expand Down
2 changes: 1 addition & 1 deletion Server/mods/deathmatch/logic/CMainConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class CMainConfig : public CXMLConfig
void OnAseSettingChange();

private:
void RegisterCommand(const char* szName, FCommandHandler* pFunction, bool bRestricted);
void RegisterCommand(const char* szName, FCommandHandler* pFunction, bool bRestricted, const char* szConsoleHelpText);
bool GetSettingTable(const SString& strName, const char** szAttribNames, uint uiNumAttribNames, CLuaArguments* outTable);
bool AddMissingSettings();

Expand Down

0 comments on commit a4267de

Please sign in to comment.