core/sm_srvcmds.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Mar 16 17:30:36 2007 +0000 (2007-03-16)
changeset 627 c04c30bc8ff4
parent 626 ad539443ad7f
child 651 7129f1566129
permissions -rw-r--r--
Fixed minor bug...
     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 extern void _IntExt_OnHostnameChanged(ConVar *pConVar, char const *oldValue);
    40 
    41 void RootConsoleMenu::OnSourceModStartup(bool late)
    42 {
    43 	ConCommandBaseMgr::OneTimeInit(this);
    44 	AddRootConsoleCommand("version", "Display version information", this);
    45 	AddRootConsoleCommand("credits", "Display credits listing", this);
    46 
    47 	ConVar *pHost = icvar->FindVar("hostname");
    48 	pHost->InstallChangeCallback(_IntExt_OnHostnameChanged);
    49 }
    50 
    51 void RootConsoleMenu::OnSourceModShutdown()
    52 {
    53 	RemoveRootConsoleCommand("credits", this);
    54 	RemoveRootConsoleCommand("version", this);
    55 }
    56 
    57 bool RootConsoleMenu::RegisterConCommandBase(ConCommandBase *pCommand)
    58 {
    59 	META_REGCVAR(pCommand);
    60 
    61 	return true;
    62 }
    63 
    64 void RootConsoleMenu::ConsolePrint(const char *fmt, ...)
    65 {
    66 	char buffer[512];
    67 
    68 	va_list ap;
    69 	va_start(ap, fmt);
    70 	size_t len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
    71 	va_end(ap);
    72 
    73 	if (len >= sizeof(buffer) - 1)
    74 	{
    75 		buffer[510] = '\n';
    76 		buffer[511] = '\0';
    77 	} else {
    78 		buffer[len++] = '\n';
    79 		buffer[len] = '\0';
    80 	}
    81 	
    82 	META_CONPRINT(buffer);
    83 }
    84 
    85 bool RootConsoleMenu::AddRootConsoleCommand(const char *cmd, const char *text, IRootConsoleCommand *pHandler)
    86 {
    87 	if (sm_trie_retrieve(m_pCommands, cmd, NULL))
    88 	{
    89 		return false;
    90 	}
    91 
    92 	sm_trie_insert(m_pCommands, cmd, pHandler);
    93 
    94 	/* Sort this into the menu */
    95 	List<ConsoleEntry *>::iterator iter = m_Menu.begin();
    96 	ConsoleEntry *pEntry;
    97 	bool inserted = false;
    98 	while (iter != m_Menu.end())
    99 	{
   100 		pEntry = (*iter);
   101 		if (strcmp(cmd, pEntry->command.c_str()) < 0)
   102 		{
   103 			ConsoleEntry *pNew = new ConsoleEntry;
   104 			pNew->command.assign(cmd);
   105 			pNew->description.assign(text);
   106 			m_Menu.insert(iter, pNew);
   107 			inserted = true;
   108 			break;
   109 		}
   110 		iter++;
   111 	}
   112 
   113 	if (!inserted)
   114 	{
   115 		ConsoleEntry *pNew = new ConsoleEntry;
   116 		pNew->command.assign(cmd);
   117 		pNew->description.assign(text);
   118 		m_Menu.push_back(pNew);
   119 	}
   120 
   121 	return true;
   122 }
   123 
   124 bool RootConsoleMenu::RemoveRootConsoleCommand(const char *cmd, IRootConsoleCommand *pHandler)
   125 {
   126 	/* Sanity tests */
   127 	IRootConsoleCommand *object;
   128 	if (sm_trie_retrieve(m_pCommands, cmd, (void **)&object))
   129 	{
   130 		if (object != pHandler)
   131 		{
   132 			return false;
   133 		}
   134 	} else {
   135 		return false;
   136 	}
   137 
   138 	sm_trie_delete(m_pCommands, cmd);
   139 
   140 	List<ConsoleEntry *>::iterator iter;
   141 	ConsoleEntry *pEntry;
   142 	for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
   143 	{
   144 		pEntry = (*iter);
   145 		if (pEntry->command.compare(cmd) == 0)
   146 		{
   147 			delete pEntry;
   148 			m_Menu.erase(iter);
   149 			break;
   150 		}
   151 	}
   152 
   153 	return true;
   154 }
   155 
   156 void RootConsoleMenu::DrawGenericOption(const char *cmd, const char *text)
   157 {
   158 	char buffer[255];
   159 	size_t len, cmdlen = strlen(cmd);
   160 
   161 	len = snprintf(buffer, sizeof(buffer), "    %s", cmd);
   162 	if (cmdlen < 16)
   163 	{
   164 		size_t num = 16 - cmdlen;
   165 		for (size_t i = 0; i < num; i++)
   166 		{
   167 			buffer[len++] = ' ';
   168 		}
   169 		len += snprintf(&buffer[len], sizeof(buffer) - len, " - %s", text);
   170 		ConsolePrint("%s", buffer);
   171 	}
   172 }
   173 
   174 extern void _IntExt_EnableYams();
   175 
   176 void RootConsoleMenu::GotRootCmd()
   177 {
   178 	unsigned int argnum = GetArgumentCount();
   179 
   180 	if (argnum >= 2)
   181 	{
   182 		const char *cmd = GetArgument(1);
   183 		if (strcmp(cmd, "text") == 0)
   184 		{
   185 			_IntExt_EnableYams();
   186 			return;
   187 		}
   188 		IRootConsoleCommand *pHandler;
   189 		if (sm_trie_retrieve(m_pCommands, cmd, (void **)&pHandler))
   190 		{
   191 			pHandler->OnRootConsoleCommand(cmd, argnum);
   192 			return;
   193 		}
   194 	}
   195 
   196 	ConsolePrint("SourceMod Menu:");
   197 	ConsolePrint("Usage: sm <command> [arguments]");
   198 
   199 	List<ConsoleEntry *>::iterator iter;
   200 	ConsoleEntry *pEntry;
   201 	for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
   202 	{
   203 		pEntry = (*iter);
   204 		DrawGenericOption(pEntry->command.c_str(), pEntry->description.c_str());
   205 	}
   206 }
   207 
   208 const char *RootConsoleMenu::GetArgument(unsigned int argno)
   209 {
   210 	return engine->Cmd_Argv(argno);
   211 }
   212 
   213 const char *RootConsoleMenu::GetArguments()
   214 {
   215 	return engine->Cmd_Args();
   216 }
   217 
   218 unsigned int RootConsoleMenu::GetArgumentCount()
   219 {
   220 	return engine->Cmd_Argc();
   221 }
   222 
   223 void RootConsoleMenu::OnRootConsoleCommand(const char *cmd, unsigned int argcount)
   224 {
   225 	if (strcmp(cmd, "credits") == 0)
   226 	{
   227 		ConsolePrint(" SourceMod was developed by AlliedModders, LLC.");
   228 		ConsolePrint(" Development would not have been possible without the following people:");
   229 		ConsolePrint("  David \"BAILOPAN\" Anderson, lead developer");
   230 		ConsolePrint("  Borja \"faluco\" Ferrer, Core developer");
   231 		ConsolePrint("  Scott \"Damaged Soul\" Ehlert, Core developer");
   232 		ConsolePrint("  Pavol \"PM OnoTo\" Marko, SourceHook developer");
   233 		ConsolePrint(" Special thanks to Viper of GameConnect");
   234 		ConsolePrint(" Special thanks to Mani of Mani-Admin-Plugin");
   235 		ConsolePrint(" http://www.sourcemod.net/");
   236 	} else if (strcmp(cmd, "version") == 0) {
   237 		ConsolePrint(" SourceMod Version Information:");
   238 		ConsolePrint("    SourceMod Version: %s", SVN_FULL_VERSION);
   239 		ConsolePrint("    JIT Version: %s, %s", g_pVM->GetVMName(), g_pVM->GetVersionString());
   240 		ConsolePrint("    JIT Settings: %s", g_pVM->GetCPUOptimizations());
   241 		ConsolePrint("    http://www.sourcemod.net/");
   242 	}
   243 }
   244 
   245 CON_COMMAND(sm, "SourceMod Menu")
   246 {
   247 	g_RootMenu.GotRootCmd();
   248 }