core/sm_srvcmds.cpp
branchsourcemod-1.1.0
changeset 2283 03a67e41afbc
parent 1951 c7b4237fff50
child 2287 017a725c9d17
     1.1 --- a/core/sm_srvcmds.cpp	Fri Mar 28 06:21:34 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,352 +0,0 @@
     1.4 -/**
     1.5 - * vim: set ts=4 :
     1.6 - * =============================================================================
     1.7 - * SourceMod
     1.8 - * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     1.9 - * =============================================================================
    1.10 - *
    1.11 - * This program is free software; you can redistribute it and/or modify it under
    1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 - * Free Software Foundation.
    1.14 - * 
    1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 - * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 - * details.
    1.19 - *
    1.20 - * You should have received a copy of the GNU General Public License along with
    1.21 - * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 - *
    1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 - * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 - * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 - * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 - * or <http://www.sourcemod.net/license.php>.
    1.31 - *
    1.32 - * Version: $Id$
    1.33 - */
    1.34 -
    1.35 -#include "sm_srvcmds.h"
    1.36 -#include "sm_version.h"
    1.37 -#include "sm_stringutil.h"
    1.38 -#include "HandleSys.h"
    1.39 -#include "CoreConfig.h"
    1.40 -
    1.41 -RootConsoleMenu g_RootMenu;
    1.42 -
    1.43 -ConVar sourcemod_version("sourcemod_version", SVN_FULL_VERSION, FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY, "SourceMod Version");
    1.44 -
    1.45 -RootConsoleMenu::RootConsoleMenu()
    1.46 -{
    1.47 -	m_pCommands = sm_trie_create();
    1.48 -	m_CfgExecDone = false;
    1.49 -}
    1.50 -
    1.51 -RootConsoleMenu::~RootConsoleMenu()
    1.52 -{
    1.53 -	sm_trie_destroy(m_pCommands);
    1.54 -
    1.55 -	List<ConsoleEntry *>::iterator iter;
    1.56 -	for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
    1.57 -	{
    1.58 -		delete (*iter);
    1.59 -	}
    1.60 -	m_Menu.clear();
    1.61 -}
    1.62 -
    1.63 -#if defined ORANGEBOX_BUILD
    1.64 -	extern void _IntExt_OnHostnameChanged(IConVar *var, const char *pOldValue, float flOldValue);
    1.65 -#else
    1.66 -	extern void _IntExt_OnHostnameChanged(ConVar *pConVar, char const *oldValue);
    1.67 -#endif
    1.68 -
    1.69 -void RootConsoleMenu::OnSourceModStartup(bool late)
    1.70 -{
    1.71 -#if defined ORANGEBOX_BUILD
    1.72 -	g_pCVar = icvar;
    1.73 -#endif
    1.74 -	CONVAR_REGISTER(this);
    1.75 -	AddRootConsoleCommand("version", "Display version information", this);
    1.76 -	AddRootConsoleCommand("credits", "Display credits listing", this);
    1.77 -
    1.78 -	ConVar *pHost = icvar->FindVar("hostname");
    1.79 -	pHost->InstallChangeCallback(_IntExt_OnHostnameChanged);
    1.80 -}
    1.81 -
    1.82 -void RootConsoleMenu::OnSourceModShutdown()
    1.83 -{
    1.84 -	RemoveRootConsoleCommand("credits", this);
    1.85 -	RemoveRootConsoleCommand("version", this);
    1.86 -}
    1.87 -
    1.88 -bool RootConsoleMenu::RegisterConCommandBase(ConCommandBase *pCommand)
    1.89 -{
    1.90 -	META_REGCVAR(pCommand);
    1.91 -
    1.92 -	/* Override values of convars created by SourceMod convar manager if specified on command line */
    1.93 -	const char *cmdLineValue = icvar->GetCommandLineValue(pCommand->GetName());
    1.94 -	if (cmdLineValue && !pCommand->IsCommand())
    1.95 -	{
    1.96 -		static_cast<ConVar *>(pCommand)->SetValue(cmdLineValue);
    1.97 -	}
    1.98 -
    1.99 -	return true;
   1.100 -}
   1.101 -
   1.102 -void RootConsoleMenu::ConsolePrint(const char *fmt, ...)
   1.103 -{
   1.104 -	char buffer[512];
   1.105 -
   1.106 -	va_list ap;
   1.107 -	va_start(ap, fmt);
   1.108 -	size_t len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
   1.109 -	va_end(ap);
   1.110 -
   1.111 -	if (len >= sizeof(buffer) - 1)
   1.112 -	{
   1.113 -		buffer[510] = '\n';
   1.114 -		buffer[511] = '\0';
   1.115 -	} else {
   1.116 -		buffer[len++] = '\n';
   1.117 -		buffer[len] = '\0';
   1.118 -	}
   1.119 -	
   1.120 -	META_CONPRINT(buffer);
   1.121 -}
   1.122 -
   1.123 -bool RootConsoleMenu::AddRootConsoleCommand(const char *cmd, const char *text, IRootConsoleCommand *pHandler)
   1.124 -{
   1.125 -	if (sm_trie_retrieve(m_pCommands, cmd, NULL))
   1.126 -	{
   1.127 -		return false;
   1.128 -	}
   1.129 -
   1.130 -	sm_trie_insert(m_pCommands, cmd, pHandler);
   1.131 -
   1.132 -	/* Sort this into the menu */
   1.133 -	List<ConsoleEntry *>::iterator iter = m_Menu.begin();
   1.134 -	ConsoleEntry *pEntry;
   1.135 -	bool inserted = false;
   1.136 -	while (iter != m_Menu.end())
   1.137 -	{
   1.138 -		pEntry = (*iter);
   1.139 -		if (strcmp(cmd, pEntry->command.c_str()) < 0)
   1.140 -		{
   1.141 -			ConsoleEntry *pNew = new ConsoleEntry;
   1.142 -			pNew->command.assign(cmd);
   1.143 -			pNew->description.assign(text);
   1.144 -			m_Menu.insert(iter, pNew);
   1.145 -			inserted = true;
   1.146 -			break;
   1.147 -		}
   1.148 -		iter++;
   1.149 -	}
   1.150 -
   1.151 -	if (!inserted)
   1.152 -	{
   1.153 -		ConsoleEntry *pNew = new ConsoleEntry;
   1.154 -		pNew->command.assign(cmd);
   1.155 -		pNew->description.assign(text);
   1.156 -		m_Menu.push_back(pNew);
   1.157 -	}
   1.158 -
   1.159 -	return true;
   1.160 -}
   1.161 -
   1.162 -bool RootConsoleMenu::RemoveRootConsoleCommand(const char *cmd, IRootConsoleCommand *pHandler)
   1.163 -{
   1.164 -	/* Sanity tests */
   1.165 -	IRootConsoleCommand *object;
   1.166 -	if (sm_trie_retrieve(m_pCommands, cmd, (void **)&object))
   1.167 -	{
   1.168 -		if (object != pHandler)
   1.169 -		{
   1.170 -			return false;
   1.171 -		}
   1.172 -	} else {
   1.173 -		return false;
   1.174 -	}
   1.175 -
   1.176 -	sm_trie_delete(m_pCommands, cmd);
   1.177 -
   1.178 -	List<ConsoleEntry *>::iterator iter;
   1.179 -	ConsoleEntry *pEntry;
   1.180 -	for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
   1.181 -	{
   1.182 -		pEntry = (*iter);
   1.183 -		if (pEntry->command.compare(cmd) == 0)
   1.184 -		{
   1.185 -			delete pEntry;
   1.186 -			m_Menu.erase(iter);
   1.187 -			break;
   1.188 -		}
   1.189 -	}
   1.190 -
   1.191 -	return true;
   1.192 -}
   1.193 -
   1.194 -void RootConsoleMenu::DrawGenericOption(const char *cmd, const char *text)
   1.195 -{
   1.196 -	char buffer[255];
   1.197 -	size_t len, cmdlen = strlen(cmd);
   1.198 -
   1.199 -	len = UTIL_Format(buffer, sizeof(buffer), "    %s", cmd);
   1.200 -	if (cmdlen < 16)
   1.201 -	{
   1.202 -		size_t num = 16 - cmdlen;
   1.203 -		for (size_t i = 0; i < num; i++)
   1.204 -		{
   1.205 -			buffer[len++] = ' ';
   1.206 -		}
   1.207 -		len += snprintf(&buffer[len], sizeof(buffer) - len, " - %s", text);
   1.208 -		ConsolePrint("%s", buffer);
   1.209 -	}
   1.210 -}
   1.211 -
   1.212 -extern void _IntExt_EnableYams();
   1.213 -
   1.214 -void RootConsoleMenu::GotRootCmd(const CCommand &cmd)
   1.215 -{
   1.216 -	unsigned int argnum = cmd.ArgC();
   1.217 -
   1.218 -	if (argnum >= 2)
   1.219 -	{
   1.220 -		const char *cmdname = cmd.Arg(1);
   1.221 -		if (strcmp(cmdname, "text") == 0)
   1.222 -		{
   1.223 -			_IntExt_EnableYams();
   1.224 -			return;
   1.225 -		}
   1.226 -		else if (strcmp(cmdname, "internal") == 0)
   1.227 -		{
   1.228 -			if (argnum >= 3)
   1.229 -			{
   1.230 -				const char *arg = cmd.Arg(2);
   1.231 -				if (strcmp(arg, "1") == 0)
   1.232 -				{
   1.233 -					SM_ConfigsExecuted_Global();
   1.234 -				}
   1.235 -				else if (strcmp(arg, "2") == 0)
   1.236 -				{
   1.237 -					if (argnum >= 4)
   1.238 -					{
   1.239 -						SM_ConfigsExecuted_Plugin(atoi(cmd.Arg(3)));
   1.240 -					}
   1.241 -				}
   1.242 -			}
   1.243 -			return;
   1.244 -		}
   1.245 -
   1.246 -		IRootConsoleCommand *pHandler;
   1.247 -		if (sm_trie_retrieve(m_pCommands, cmdname, (void **)&pHandler))
   1.248 -		{
   1.249 -			pHandler->OnRootConsoleCommand(cmdname, cmd);
   1.250 -			return;
   1.251 -		}
   1.252 -	}
   1.253 -
   1.254 -	ConsolePrint("SourceMod Menu:");
   1.255 -	ConsolePrint("Usage: sm <command> [arguments]");
   1.256 -
   1.257 -	List<ConsoleEntry *>::iterator iter;
   1.258 -	ConsoleEntry *pEntry;
   1.259 -	for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
   1.260 -	{
   1.261 -		pEntry = (*iter);
   1.262 -		DrawGenericOption(pEntry->command.c_str(), pEntry->description.c_str());
   1.263 -	}
   1.264 -}
   1.265 -
   1.266 -void RootConsoleMenu::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
   1.267 -{
   1.268 -	if (strcmp(cmdname, "credits") == 0)
   1.269 -	{
   1.270 -		ConsolePrint(" SourceMod was developed by AlliedModders, LLC.");
   1.271 -		ConsolePrint(" Development would not have been possible without the following people:");
   1.272 -		ConsolePrint("  David \"BAILOPAN\" Anderson, lead developer");
   1.273 -		ConsolePrint("  Borja \"faluco\" Ferrer, Core developer");
   1.274 -		ConsolePrint("  Scott \"Damaged Soul\" Ehlert, Core developer");
   1.275 -		ConsolePrint("  Pavol \"PM OnoTo\" Marko, SourceHook developer");
   1.276 -		ConsolePrint(" Special thanks to Viper of GameConnect");
   1.277 -		ConsolePrint(" Special thanks to Mani of Mani-Admin-Plugin");
   1.278 -		ConsolePrint(" http://www.sourcemod.net/");
   1.279 -	} else if (strcmp(cmdname, "version") == 0) {
   1.280 -		ConsolePrint(" SourceMod Version Information:");
   1.281 -		ConsolePrint("    SourceMod Version: %s", SVN_FULL_VERSION);
   1.282 -		ConsolePrint("    JIT Version: %s, %s", g_pVM->GetVMName(), g_pVM->GetVersionString());
   1.283 -		ConsolePrint("    JIT Settings: %s", g_pVM->GetCPUOptimizations());
   1.284 -		ConsolePrint("    http://www.sourcemod.net/");
   1.285 -	}
   1.286 -}
   1.287 -
   1.288 -CON_COMMAND(sm, "SourceMod Menu")
   1.289 -{
   1.290 -#if !defined ORANGEBOX_BUILD
   1.291 -	CCommand args;
   1.292 -#endif
   1.293 -	g_RootMenu.GotRootCmd(args);
   1.294 -}
   1.295 -
   1.296 -FILE *g_pHndlLog = NULL;
   1.297 -
   1.298 -void write_handles_to_log(const char *fmt, ...)
   1.299 -{
   1.300 -	va_list ap;
   1.301 -	
   1.302 -	va_start(ap, fmt);
   1.303 -	vfprintf(g_pHndlLog, fmt, ap);
   1.304 -	fprintf(g_pHndlLog, "\n");
   1.305 -	va_end(ap);
   1.306 -}
   1.307 -
   1.308 -void write_handles_to_game(const char *fmt, ...)
   1.309 -{
   1.310 -	size_t len;
   1.311 -	va_list ap;
   1.312 -	char buffer[1024];
   1.313 -	
   1.314 -	va_start(ap, fmt);
   1.315 -	len = UTIL_FormatArgs(buffer, sizeof(buffer)-2, fmt, ap);
   1.316 -	va_end(ap);
   1.317 -
   1.318 -	buffer[len] = '\n';
   1.319 -	buffer[len+1] = '\0';
   1.320 -
   1.321 -	engine->LogPrint(buffer);
   1.322 -}
   1.323 -
   1.324 -CON_COMMAND(sm_dump_handles, "Dumps Handle usage to a file for finding Handle leaks")
   1.325 -{
   1.326 -#if !defined ORANGEBOX_BUILD
   1.327 -	CCommand args;
   1.328 -#endif
   1.329 -	if (args.ArgC() < 2)
   1.330 -	{
   1.331 -		g_RootMenu.ConsolePrint("Usage: sm_dump_handles <file> or <log> for game logs");
   1.332 -		return;
   1.333 -	}
   1.334 -
   1.335 -	if (strcmp(args.Arg(1), "log") != 0)
   1.336 -	{
   1.337 -		const char *arg = args.Arg(1);
   1.338 -		FILE *fp = fopen(arg, "wt");
   1.339 -		if (!fp)
   1.340 -		{
   1.341 -			g_RootMenu.ConsolePrint("Could not find file \"%s\"", arg);
   1.342 -			return;
   1.343 -		}
   1.344 -
   1.345 -		g_pHndlLog = fp;
   1.346 -		g_HandleSys.Dump(write_handles_to_log);
   1.347 -		g_pHndlLog = NULL;
   1.348 -
   1.349 -		fclose(fp);
   1.350 -	}
   1.351 -	else
   1.352 -	{
   1.353 -		g_HandleSys.Dump(write_handles_to_game);
   1.354 -	}
   1.355 -}