core/sm_srvcmds.cpp
changeset 1952 b7aa2b626807
child 2105 ad190b9bb5af
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/sm_srvcmds.cpp	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,353 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod
     1.8 + * Copyright (C) 2004-2008 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 +#include "ConVarManager.h"
    1.41 +#include "ShareSys.h"
    1.42 +
    1.43 +RootConsoleMenu g_RootMenu;
    1.44 +
    1.45 +ConVar sourcemod_version("sourcemod_version", SVN_FULL_VERSION, FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY, "SourceMod Version");
    1.46 +
    1.47 +RootConsoleMenu::RootConsoleMenu()
    1.48 +{
    1.49 +	m_pCommands = sm_trie_create();
    1.50 +	m_CfgExecDone = false;
    1.51 +}
    1.52 +
    1.53 +RootConsoleMenu::~RootConsoleMenu()
    1.54 +{
    1.55 +	sm_trie_destroy(m_pCommands);
    1.56 +
    1.57 +	List<ConsoleEntry *>::iterator iter;
    1.58 +	for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
    1.59 +	{
    1.60 +		delete (*iter);
    1.61 +	}
    1.62 +	m_Menu.clear();
    1.63 +}
    1.64 +
    1.65 +void RootConsoleMenu::OnSourceModStartup(bool late)
    1.66 +{
    1.67 +#if defined ORANGEBOX_BUILD
    1.68 +	g_pCVar = icvar;
    1.69 +#endif
    1.70 +	CONVAR_REGISTER(this);
    1.71 +	AddRootConsoleCommand("version", "Display version information", this);
    1.72 +	AddRootConsoleCommand("credits", "Display credits listing", this);
    1.73 +}
    1.74 +
    1.75 +void RootConsoleMenu::OnSourceModAllInitialized()
    1.76 +{
    1.77 +	g_ShareSys.AddInterface(NULL, this);
    1.78 +}
    1.79 +
    1.80 +void RootConsoleMenu::OnSourceModShutdown()
    1.81 +{
    1.82 +	RemoveRootConsoleCommand("credits", this);
    1.83 +	RemoveRootConsoleCommand("version", this);
    1.84 +}
    1.85 +
    1.86 +bool RootConsoleMenu::RegisterConCommandBase(ConCommandBase *pCommand)
    1.87 +{
    1.88 +	META_REGCVAR(pCommand);
    1.89 +
    1.90 +	/* Override values of convars created by SourceMod convar manager if specified on command line */
    1.91 +	const char *cmdLineValue = icvar->GetCommandLineValue(pCommand->GetName());
    1.92 +	if (cmdLineValue && !pCommand->IsCommand())
    1.93 +	{
    1.94 +		static_cast<ConVar *>(pCommand)->SetValue(cmdLineValue);
    1.95 +	}
    1.96 +
    1.97 +	return true;
    1.98 +}
    1.99 +
   1.100 +void RootConsoleMenu::ConsolePrint(const char *fmt, ...)
   1.101 +{
   1.102 +	char buffer[512];
   1.103 +
   1.104 +	va_list ap;
   1.105 +	va_start(ap, fmt);
   1.106 +	size_t len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
   1.107 +	va_end(ap);
   1.108 +
   1.109 +	if (len >= sizeof(buffer) - 1)
   1.110 +	{
   1.111 +		buffer[510] = '\n';
   1.112 +		buffer[511] = '\0';
   1.113 +	} else {
   1.114 +		buffer[len++] = '\n';
   1.115 +		buffer[len] = '\0';
   1.116 +	}
   1.117 +	
   1.118 +	META_CONPRINT(buffer);
   1.119 +}
   1.120 +
   1.121 +bool RootConsoleMenu::AddRootConsoleCommand(const char *cmd, const char *text, IRootConsoleCommand *pHandler)
   1.122 +{
   1.123 +	if (sm_trie_retrieve(m_pCommands, cmd, NULL))
   1.124 +	{
   1.125 +		return false;
   1.126 +	}
   1.127 +
   1.128 +	sm_trie_insert(m_pCommands, cmd, pHandler);
   1.129 +
   1.130 +	/* Sort this into the menu */
   1.131 +	List<ConsoleEntry *>::iterator iter = m_Menu.begin();
   1.132 +	ConsoleEntry *pEntry;
   1.133 +	bool inserted = false;
   1.134 +	while (iter != m_Menu.end())
   1.135 +	{
   1.136 +		pEntry = (*iter);
   1.137 +		if (strcmp(cmd, pEntry->command.c_str()) < 0)
   1.138 +		{
   1.139 +			ConsoleEntry *pNew = new ConsoleEntry;
   1.140 +			pNew->command.assign(cmd);
   1.141 +			pNew->description.assign(text);
   1.142 +			m_Menu.insert(iter, pNew);
   1.143 +			inserted = true;
   1.144 +			break;
   1.145 +		}
   1.146 +		iter++;
   1.147 +	}
   1.148 +
   1.149 +	if (!inserted)
   1.150 +	{
   1.151 +		ConsoleEntry *pNew = new ConsoleEntry;
   1.152 +		pNew->command.assign(cmd);
   1.153 +		pNew->description.assign(text);
   1.154 +		m_Menu.push_back(pNew);
   1.155 +	}
   1.156 +
   1.157 +	return true;
   1.158 +}
   1.159 +
   1.160 +bool RootConsoleMenu::RemoveRootConsoleCommand(const char *cmd, IRootConsoleCommand *pHandler)
   1.161 +{
   1.162 +	/* Sanity tests */
   1.163 +	IRootConsoleCommand *object;
   1.164 +	if (sm_trie_retrieve(m_pCommands, cmd, (void **)&object))
   1.165 +	{
   1.166 +		if (object != pHandler)
   1.167 +		{
   1.168 +			return false;
   1.169 +		}
   1.170 +	} else {
   1.171 +		return false;
   1.172 +	}
   1.173 +
   1.174 +	sm_trie_delete(m_pCommands, cmd);
   1.175 +
   1.176 +	List<ConsoleEntry *>::iterator iter;
   1.177 +	ConsoleEntry *pEntry;
   1.178 +	for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
   1.179 +	{
   1.180 +		pEntry = (*iter);
   1.181 +		if (pEntry->command.compare(cmd) == 0)
   1.182 +		{
   1.183 +			delete pEntry;
   1.184 +			m_Menu.erase(iter);
   1.185 +			break;
   1.186 +		}
   1.187 +	}
   1.188 +
   1.189 +	return true;
   1.190 +}
   1.191 +
   1.192 +void RootConsoleMenu::DrawGenericOption(const char *cmd, const char *text)
   1.193 +{
   1.194 +	char buffer[255];
   1.195 +	size_t len, cmdlen = strlen(cmd);
   1.196 +
   1.197 +	len = UTIL_Format(buffer, sizeof(buffer), "    %s", cmd);
   1.198 +	if (cmdlen < 16)
   1.199 +	{
   1.200 +		size_t num = 16 - cmdlen;
   1.201 +		for (size_t i = 0; i < num; i++)
   1.202 +		{
   1.203 +			buffer[len++] = ' ';
   1.204 +		}
   1.205 +		len += snprintf(&buffer[len], sizeof(buffer) - len, " - %s", text);
   1.206 +		ConsolePrint("%s", buffer);
   1.207 +	}
   1.208 +}
   1.209 +
   1.210 +const char *RootConsoleMenu::GetInterfaceName()
   1.211 +{
   1.212 +	return SMINTERFACE_ROOTCONSOLE_NAME;
   1.213 +}
   1.214 +
   1.215 +unsigned int RootConsoleMenu::GetInterfaceVersion()
   1.216 +{
   1.217 +	return SMINTERFACE_ROOTCONSOLE_VERSION;
   1.218 +}
   1.219 +
   1.220 +void RootConsoleMenu::GotRootCmd(const CCommand &cmd)
   1.221 +{
   1.222 +	unsigned int argnum = cmd.ArgC();
   1.223 +
   1.224 +	if (argnum >= 2)
   1.225 +	{
   1.226 +		const char *cmdname = cmd.Arg(1);
   1.227 +		if (strcmp(cmdname, "internal") == 0)
   1.228 +		{
   1.229 +			if (argnum >= 3)
   1.230 +			{
   1.231 +				const char *arg = cmd.Arg(2);
   1.232 +				if (strcmp(arg, "1") == 0)
   1.233 +				{
   1.234 +					SM_ConfigsExecuted_Global();
   1.235 +				}
   1.236 +				else if (strcmp(arg, "2") == 0)
   1.237 +				{
   1.238 +					if (argnum >= 4)
   1.239 +					{
   1.240 +						SM_ConfigsExecuted_Plugin(atoi(cmd.Arg(3)));
   1.241 +					}
   1.242 +				}
   1.243 +			}
   1.244 +			return;
   1.245 +		}
   1.246 +
   1.247 +		IRootConsoleCommand *pHandler;
   1.248 +		if (sm_trie_retrieve(m_pCommands, cmdname, (void **)&pHandler))
   1.249 +		{
   1.250 +			pHandler->OnRootConsoleCommand(cmdname, cmd);
   1.251 +			return;
   1.252 +		}
   1.253 +	}
   1.254 +
   1.255 +	ConsolePrint("SourceMod Menu:");
   1.256 +	ConsolePrint("Usage: sm <command> [arguments]");
   1.257 +
   1.258 +	List<ConsoleEntry *>::iterator iter;
   1.259 +	ConsoleEntry *pEntry;
   1.260 +	for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
   1.261 +	{
   1.262 +		pEntry = (*iter);
   1.263 +		DrawGenericOption(pEntry->command.c_str(), pEntry->description.c_str());
   1.264 +	}
   1.265 +}
   1.266 +
   1.267 +void RootConsoleMenu::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
   1.268 +{
   1.269 +	if (strcmp(cmdname, "credits") == 0)
   1.270 +	{
   1.271 +		ConsolePrint(" SourceMod was developed by AlliedModders, LLC.");
   1.272 +		ConsolePrint(" Development would not have been possible without the following people:");
   1.273 +		ConsolePrint("  David \"BAILOPAN\" Anderson, lead developer");
   1.274 +		ConsolePrint("  Borja \"faluco\" Ferrer, Core developer");
   1.275 +		ConsolePrint("  Scott \"Damaged Soul\" Ehlert, Core developer");
   1.276 +		ConsolePrint("  Pavol \"PM OnoTo\" Marko, SourceHook developer");
   1.277 +		ConsolePrint(" Special thanks to Viper of GameConnect");
   1.278 +		ConsolePrint(" Special thanks to Mani of Mani-Admin-Plugin");
   1.279 +		ConsolePrint(" http://www.sourcemod.net/");
   1.280 +	} else if (strcmp(cmdname, "version") == 0) {
   1.281 +		ConsolePrint(" SourceMod Version Information:");
   1.282 +		ConsolePrint("    SourceMod Version: %s", SVN_FULL_VERSION);
   1.283 +		ConsolePrint("    JIT Version: %s, %s", g_pVM->GetVMName(), g_pVM->GetVersionString());
   1.284 +		ConsolePrint("    JIT Settings: %s", g_pVM->GetCPUOptimizations());
   1.285 +		ConsolePrint("    http://www.sourcemod.net/");
   1.286 +	}
   1.287 +}
   1.288 +
   1.289 +CON_COMMAND(sm, "SourceMod Menu")
   1.290 +{
   1.291 +#if !defined ORANGEBOX_BUILD
   1.292 +	CCommand args;
   1.293 +#endif
   1.294 +	g_RootMenu.GotRootCmd(args);
   1.295 +}
   1.296 +
   1.297 +FILE *g_pHndlLog = NULL;
   1.298 +
   1.299 +void write_handles_to_log(const char *fmt, ...)
   1.300 +{
   1.301 +	va_list ap;
   1.302 +	
   1.303 +	va_start(ap, fmt);
   1.304 +	vfprintf(g_pHndlLog, fmt, ap);
   1.305 +	fprintf(g_pHndlLog, "\n");
   1.306 +	va_end(ap);
   1.307 +}
   1.308 +
   1.309 +void write_handles_to_game(const char *fmt, ...)
   1.310 +{
   1.311 +	size_t len;
   1.312 +	va_list ap;
   1.313 +	char buffer[1024];
   1.314 +	
   1.315 +	va_start(ap, fmt);
   1.316 +	len = UTIL_FormatArgs(buffer, sizeof(buffer)-2, fmt, ap);
   1.317 +	va_end(ap);
   1.318 +
   1.319 +	buffer[len] = '\n';
   1.320 +	buffer[len+1] = '\0';
   1.321 +
   1.322 +	engine->LogPrint(buffer);
   1.323 +}
   1.324 +
   1.325 +CON_COMMAND(sm_dump_handles, "Dumps Handle usage to a file for finding Handle leaks")
   1.326 +{
   1.327 +#if !defined ORANGEBOX_BUILD
   1.328 +	CCommand args;
   1.329 +#endif
   1.330 +	if (args.ArgC() < 2)
   1.331 +	{
   1.332 +		g_RootMenu.ConsolePrint("Usage: sm_dump_handles <file> or <log> for game logs");
   1.333 +		return;
   1.334 +	}
   1.335 +
   1.336 +	if (strcmp(args.Arg(1), "log") != 0)
   1.337 +	{
   1.338 +		const char *arg = args.Arg(1);
   1.339 +		FILE *fp = fopen(arg, "wt");
   1.340 +		if (!fp)
   1.341 +		{
   1.342 +			g_RootMenu.ConsolePrint("Could not find file \"%s\"", arg);
   1.343 +			return;
   1.344 +		}
   1.345 +
   1.346 +		g_pHndlLog = fp;
   1.347 +		g_HandleSys.Dump(write_handles_to_log);
   1.348 +		g_pHndlLog = NULL;
   1.349 +
   1.350 +		fclose(fp);
   1.351 +	}
   1.352 +	else
   1.353 +	{
   1.354 +		g_HandleSys.Dump(write_handles_to_game);
   1.355 +	}
   1.356 +}