core/sm_srvcmds.cpp
author David Anderson <dvander@alliedmods.net>
Wed Jun 06 06:04:16 2007 +0000 (2007-06-06)
changeset 883 f4eff9faf362
parent 657 e9eea6cf8e41
child 900 b38c83f9d526
permissions -rw-r--r--
fixed inheritance not being used in the base menu style when removing clients from the watch list
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  * This file is not open source and may not be copied without explicit
     8  * written permission of AlliedModders LLC.  This file may not be redistributed 
     9  * in whole or significant part.
    10  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 #include "sm_srvcmds.h"
    16 #include "sm_version.h"
    17 #include "sm_stringutil.h"
    18 
    19 RootConsoleMenu g_RootMenu;
    20 
    21 ConVar sourcemod_version("sourcemod_version", SVN_FULL_VERSION, FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY, "SourceMod Version");
    22 
    23 RootConsoleMenu::RootConsoleMenu()
    24 {
    25 	m_pCommands = sm_trie_create();
    26 }
    27 
    28 RootConsoleMenu::~RootConsoleMenu()
    29 {
    30 	sm_trie_destroy(m_pCommands);
    31 
    32 	List<ConsoleEntry *>::iterator iter;
    33 	for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
    34 	{
    35 		delete (*iter);
    36 	}
    37 	m_Menu.clear();
    38 }
    39 
    40 extern void _IntExt_OnHostnameChanged(ConVar *pConVar, char const *oldValue);
    41 
    42 void RootConsoleMenu::OnSourceModStartup(bool late)
    43 {
    44 	ConCommandBaseMgr::OneTimeInit(this);
    45 	AddRootConsoleCommand("version", "Display version information", this);
    46 	AddRootConsoleCommand("credits", "Display credits listing", this);
    47 
    48 	ConVar *pHost = icvar->FindVar("hostname");
    49 	pHost->InstallChangeCallback(_IntExt_OnHostnameChanged);
    50 }
    51 
    52 void RootConsoleMenu::OnSourceModShutdown()
    53 {
    54 	RemoveRootConsoleCommand("credits", this);
    55 	RemoveRootConsoleCommand("version", this);
    56 }
    57 
    58 bool RootConsoleMenu::RegisterConCommandBase(ConCommandBase *pCommand)
    59 {
    60 	META_REGCVAR(pCommand);
    61 
    62 	/* Override values of convars created by SourceMod convar manager if specified on command line */
    63 	const char *cmdLineValue = icvar->GetCommandLineValue(pCommand->GetName());
    64 	if (cmdLineValue && !pCommand->IsCommand())
    65 	{
    66 		static_cast<ConVar *>(pCommand)->SetValue(cmdLineValue);
    67 	}
    68 
    69 	return true;
    70 }
    71 
    72 void RootConsoleMenu::ConsolePrint(const char *fmt, ...)
    73 {
    74 	char buffer[512];
    75 
    76 	va_list ap;
    77 	va_start(ap, fmt);
    78 	size_t len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
    79 	va_end(ap);
    80 
    81 	if (len >= sizeof(buffer) - 1)
    82 	{
    83 		buffer[510] = '\n';
    84 		buffer[511] = '\0';
    85 	} else {
    86 		buffer[len++] = '\n';
    87 		buffer[len] = '\0';
    88 	}
    89 	
    90 	META_CONPRINT(buffer);
    91 }
    92 
    93 bool RootConsoleMenu::AddRootConsoleCommand(const char *cmd, const char *text, IRootConsoleCommand *pHandler)
    94 {
    95 	if (sm_trie_retrieve(m_pCommands, cmd, NULL))
    96 	{
    97 		return false;
    98 	}
    99 
   100 	sm_trie_insert(m_pCommands, cmd, pHandler);
   101 
   102 	/* Sort this into the menu */
   103 	List<ConsoleEntry *>::iterator iter = m_Menu.begin();
   104 	ConsoleEntry *pEntry;
   105 	bool inserted = false;
   106 	while (iter != m_Menu.end())
   107 	{
   108 		pEntry = (*iter);
   109 		if (strcmp(cmd, pEntry->command.c_str()) < 0)
   110 		{
   111 			ConsoleEntry *pNew = new ConsoleEntry;
   112 			pNew->command.assign(cmd);
   113 			pNew->description.assign(text);
   114 			m_Menu.insert(iter, pNew);
   115 			inserted = true;
   116 			break;
   117 		}
   118 		iter++;
   119 	}
   120 
   121 	if (!inserted)
   122 	{
   123 		ConsoleEntry *pNew = new ConsoleEntry;
   124 		pNew->command.assign(cmd);
   125 		pNew->description.assign(text);
   126 		m_Menu.push_back(pNew);
   127 	}
   128 
   129 	return true;
   130 }
   131 
   132 bool RootConsoleMenu::RemoveRootConsoleCommand(const char *cmd, IRootConsoleCommand *pHandler)
   133 {
   134 	/* Sanity tests */
   135 	IRootConsoleCommand *object;
   136 	if (sm_trie_retrieve(m_pCommands, cmd, (void **)&object))
   137 	{
   138 		if (object != pHandler)
   139 		{
   140 			return false;
   141 		}
   142 	} else {
   143 		return false;
   144 	}
   145 
   146 	sm_trie_delete(m_pCommands, cmd);
   147 
   148 	List<ConsoleEntry *>::iterator iter;
   149 	ConsoleEntry *pEntry;
   150 	for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
   151 	{
   152 		pEntry = (*iter);
   153 		if (pEntry->command.compare(cmd) == 0)
   154 		{
   155 			delete pEntry;
   156 			m_Menu.erase(iter);
   157 			break;
   158 		}
   159 	}
   160 
   161 	return true;
   162 }
   163 
   164 void RootConsoleMenu::DrawGenericOption(const char *cmd, const char *text)
   165 {
   166 	char buffer[255];
   167 	size_t len, cmdlen = strlen(cmd);
   168 
   169 	len = UTIL_Format(buffer, sizeof(buffer), "    %s", cmd);
   170 	if (cmdlen < 16)
   171 	{
   172 		size_t num = 16 - cmdlen;
   173 		for (size_t i = 0; i < num; i++)
   174 		{
   175 			buffer[len++] = ' ';
   176 		}
   177 		len += snprintf(&buffer[len], sizeof(buffer) - len, " - %s", text);
   178 		ConsolePrint("%s", buffer);
   179 	}
   180 }
   181 
   182 extern void _IntExt_EnableYams();
   183 
   184 void RootConsoleMenu::GotRootCmd()
   185 {
   186 	unsigned int argnum = GetArgumentCount();
   187 
   188 	if (argnum >= 2)
   189 	{
   190 		const char *cmd = GetArgument(1);
   191 		if (strcmp(cmd, "text") == 0)
   192 		{
   193 			_IntExt_EnableYams();
   194 			return;
   195 		}
   196 		IRootConsoleCommand *pHandler;
   197 		if (sm_trie_retrieve(m_pCommands, cmd, (void **)&pHandler))
   198 		{
   199 			pHandler->OnRootConsoleCommand(cmd, argnum);
   200 			return;
   201 		}
   202 	}
   203 
   204 	ConsolePrint("SourceMod Menu:");
   205 	ConsolePrint("Usage: sm <command> [arguments]");
   206 
   207 	List<ConsoleEntry *>::iterator iter;
   208 	ConsoleEntry *pEntry;
   209 	for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
   210 	{
   211 		pEntry = (*iter);
   212 		DrawGenericOption(pEntry->command.c_str(), pEntry->description.c_str());
   213 	}
   214 }
   215 
   216 const char *RootConsoleMenu::GetArgument(unsigned int argno)
   217 {
   218 	return engine->Cmd_Argv(argno);
   219 }
   220 
   221 const char *RootConsoleMenu::GetArguments()
   222 {
   223 	return engine->Cmd_Args();
   224 }
   225 
   226 unsigned int RootConsoleMenu::GetArgumentCount()
   227 {
   228 	return engine->Cmd_Argc();
   229 }
   230 
   231 void RootConsoleMenu::OnRootConsoleCommand(const char *cmd, unsigned int argcount)
   232 {
   233 	if (strcmp(cmd, "credits") == 0)
   234 	{
   235 		ConsolePrint(" SourceMod was developed by AlliedModders, LLC.");
   236 		ConsolePrint(" Development would not have been possible without the following people:");
   237 		ConsolePrint("  David \"BAILOPAN\" Anderson, lead developer");
   238 		ConsolePrint("  Borja \"faluco\" Ferrer, Core developer");
   239 		ConsolePrint("  Scott \"Damaged Soul\" Ehlert, Core developer");
   240 		ConsolePrint("  Pavol \"PM OnoTo\" Marko, SourceHook developer");
   241 		ConsolePrint(" Special thanks to Viper of GameConnect");
   242 		ConsolePrint(" Special thanks to Mani of Mani-Admin-Plugin");
   243 		ConsolePrint(" http://www.sourcemod.net/");
   244 	} else if (strcmp(cmd, "version") == 0) {
   245 		ConsolePrint(" SourceMod Version Information:");
   246 		ConsolePrint("    SourceMod Version: %s", SVN_FULL_VERSION);
   247 		ConsolePrint("    JIT Version: %s, %s", g_pVM->GetVMName(), g_pVM->GetVersionString());
   248 		ConsolePrint("    JIT Settings: %s", g_pVM->GetCPUOptimizations());
   249 		ConsolePrint("    http://www.sourcemod.net/");
   250 	}
   251 }
   252 
   253 CON_COMMAND(sm, "SourceMod Menu")
   254 {
   255 	g_RootMenu.GotRootCmd();
   256 }