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