core-1.4/concommands.cpp
changeset 541 35a8cda28485
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core-1.4/concommands.cpp	Fri Nov 14 04:25:40 2008 -0600
     1.3 @@ -0,0 +1,803 @@
     1.4 +/* ======== SourceMM ========
     1.5 + * Copyright (C) 2004-2008 Metamod:Source Development Team
     1.6 + * No warranties of any kind
     1.7 + *
     1.8 + * License: zlib/libpng
     1.9 + *
    1.10 + * Author(s): David "BAILOPAN" Anderson
    1.11 + * ============================
    1.12 + */
    1.13 +
    1.14 +#include <ctype.h>
    1.15 +#include "convar_smm.h"
    1.16 +#include "CSmmAPI.h"
    1.17 +#include "concommands.h"
    1.18 +#include "CPlugin.h"
    1.19 +#include "sh_string.h"
    1.20 +#include "sh_list.h"
    1.21 +#include "vsp_listener.h"
    1.22 +
    1.23 +/**
    1.24 + * @brief Console Command Implementations
    1.25 + * @file concommands.cpp
    1.26 + */
    1.27 +
    1.28 +SMConVarAccessor g_SMConVarAccessor;
    1.29 +
    1.30 +SMConVarAccessor::SMConVarAccessor()
    1.31 +{
    1.32 +	m_TopConCommandBase = NULL;
    1.33 +}
    1.34 +
    1.35 +bool SMConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand)
    1.36 +{
    1.37 +	/* Add the FCVAR_GAMEDLL flag 
    1.38 +	 * => No crash on exit!
    1.39 +	 * UPDATE: Do _not_ add the FCVAR_GAMEDLL flag here, as it
    1.40 +	 * causes the command to be unusable on listen servers until you load a map
    1.41 +	 * We will set the FCVAR_GAMEDLL flag on all commands we have registered once we are being unloaded
    1.42 +	 */
    1.43 +	// pCommand->AddFlags(FCVAR_GAMEDLL);
    1.44 +	m_RegisteredCommands.push_back(pCommand);
    1.45 +
    1.46 +	pCommand->SetNext(NULL);
    1.47 +	g_Engine.icvar->RegisterConCommandBase(pCommand);
    1.48 +
    1.49 +	return true;
    1.50 +}
    1.51 +
    1.52 +bool SMConVarAccessor::Register(ConCommandBase *pCommand)
    1.53 +{
    1.54 +	/* Simple, don't mark as part of sourcemm! */
    1.55 +	pCommand->SetNext(NULL);
    1.56 +	g_Engine.icvar->RegisterConCommandBase(pCommand);
    1.57 +
    1.58 +	return true;
    1.59 +}
    1.60 +
    1.61 +void SMConVarAccessor::MarkCommandsAsGameDLL()
    1.62 +{
    1.63 +	for (SourceHook::List<ConCommandBase*>::iterator iter = m_RegisteredCommands.begin();
    1.64 +		iter != m_RegisteredCommands.end(); ++iter)
    1.65 +	{
    1.66 +		(*iter)->AddFlags(FCVAR_GAMEDLL);
    1.67 +	}
    1.68 +}
    1.69 +
    1.70 +void SMConVarAccessor::Unregister(ConCommandBase *pCommand)
    1.71 +{
    1.72 +	ConCommandBase *pCur = NULL;
    1.73 +	ConCommandBase *pPrev = NULL;
    1.74 +
    1.75 +	if (!pCommand || !pCommand->IsRegistered())
    1.76 +	{
    1.77 +		return;
    1.78 +	}
    1.79 +
    1.80 +	pCur = g_Engine.icvar->GetCommands();
    1.81 +	pCommand->SetRegistered(false);
    1.82 +
    1.83 +	if (!m_TopConCommandBase || !pCur)
    1.84 +	{
    1.85 +		return;
    1.86 +	}
    1.87 +
    1.88 +	if (pCur == pCommand)
    1.89 +	{
    1.90 +		*m_TopConCommandBase = const_cast<ConCommandBase *>(pCommand->GetNext());
    1.91 +		pCommand->SetNext(NULL);
    1.92 +		return;
    1.93 +	}
    1.94 +	
    1.95 +	pPrev = pCur;
    1.96 +	pCur = const_cast<ConCommandBase *>(pCur->GetNext());
    1.97 +
    1.98 +	while (pCur)
    1.99 +	{
   1.100 +		if (pCur == pCommand)
   1.101 +		{
   1.102 +			pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
   1.103 +			pCommand->SetNext(NULL);
   1.104 +		}
   1.105 +
   1.106 +		pPrev = pCur;
   1.107 +		pCur = const_cast<ConCommandBase *>(pCur->GetNext());
   1.108 +	}
   1.109 +}
   1.110 +
   1.111 +ConVar metamod_version("metamod_version", SOURCEMM_VERSION, FCVAR_SPONLY | FCVAR_NOTIFY, "Metamod:Source Version");
   1.112 +#ifdef OS_WIN32
   1.113 +ConVar mm_pluginsfile("mm_pluginsfile", "addons\\metamod\\metaplugins.ini", FCVAR_SPONLY, "Metamod:Source Plugins File");
   1.114 +ConVar mm_basedir("mm_basedir", "addons\\metamod", FCVAR_SPONLY, "Metamod:Source base folder");
   1.115 +#else
   1.116 +ConVar mm_pluginsfile("mm_pluginsfile", "addons/metamod/metaplugins.ini", FCVAR_SPONLY, "Metamod:Source Plugins File");
   1.117 +ConVar mm_basedir("mm_basedir", "addons/metamod", FCVAR_SPONLY, "Metamod:Source base folder");
   1.118 +#endif
   1.119 +
   1.120 +CON_COMMAND(meta, "Metamod:Source Menu")
   1.121 +{
   1.122 +	IVEngineServer *e = g_Engine.engine;
   1.123 +
   1.124 +	int args = e->Cmd_Argc();
   1.125 +
   1.126 +	if (g_VspListener.IsRootLoadMethod() && !g_bLevelChanged)
   1.127 +	{
   1.128 +		CONMSG("WARNING: You must change the map to activate Metamod:Source.\n");
   1.129 +		return;
   1.130 +	}
   1.131 +
   1.132 +	if (args >= 2)
   1.133 +	{
   1.134 +		const char *command = e->Cmd_Argv(1);
   1.135 +		if (strcmp(command, "credits") == 0)
   1.136 +		{
   1.137 +			CONMSG("Metamod:Source was developed by:\n");
   1.138 +			CONMSG("  SourceHook: Pavol \"PM OnoTo\" Marko\n");
   1.139 +			CONMSG("  GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
   1.140 +			CONMSG("  GameDLL: Scott \"Damaged Soul\" Ehlert\n");
   1.141 +			CONMSG("For more information, see the official website\n");
   1.142 +			CONMSG("http://www.metamodsource.net/\n");
   1.143 +			
   1.144 +			return;
   1.145 +		} else if (strcmp(command, "version") == 0) {
   1.146 +			CONMSG("Metamod:Source version %s\n", SOURCEMM_VERSION);
   1.147 +			if (g_VspListener.IsRootLoadMethod())
   1.148 +			{
   1.149 +				CONMSG("Loaded As: Valve Server Plugin\n");
   1.150 +			}
   1.151 +			else
   1.152 +			{
   1.153 +				CONMSG("Loaded As: GameDLL (gameinfo.txt)\n");
   1.154 +			}
   1.155 +			CONMSG("Compiled on: %s\n", SOURCEMM_DATE);
   1.156 +			CONMSG("Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
   1.157 +			CONMSG("SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
   1.158 +			CONMSG("http://www.metamodsource.net/\n");
   1.159 +
   1.160 +			return;
   1.161 +		} else if (strcmp(command, "game") == 0) {
   1.162 +			CONMSG("GameDLL Information\n");
   1.163 +			CONMSG("  Description: %s\n", g_GameDll.pGameDLL->GetGameDescription());
   1.164 +			CONMSG("  Mod Path: %s\n", g_ModPath.c_str());
   1.165 +			CONMSG("  DLL Path: %s\n", g_BinPath.c_str());
   1.166 +			CONMSG("  Interface: ServerGameDLL%03d\n", g_GameDllVersion);
   1.167 +
   1.168 +			// Display user messages
   1.169 +			if (g_SmmAPI.MsgCacheSuccessful())
   1.170 +			{
   1.171 +				const char *msgname;
   1.172 +				int msgsize;
   1.173 +				int msgcount = g_SmmAPI.GetUserMessageCount();
   1.174 +
   1.175 +				if (msgcount > 0)
   1.176 +				{
   1.177 +					CONMSG("  User Messages:  %-32.31s  %-5s  %-5s\n", "Name", "Index", "Size");
   1.178 +
   1.179 +					for (int i = 0; i < msgcount; i++)
   1.180 +					{
   1.181 +						msgname = g_SmmAPI.GetUserMessage(i, &msgsize);
   1.182 +
   1.183 +						CONMSG("                  %-32.31s  %-5d  %-5d\n", msgname, i, msgsize); 
   1.184 +					}
   1.185 +
   1.186 +					CONMSG("  %d user message%s in total\n", msgcount, (msgcount > 1) ? "s" : "");
   1.187 +				} else {
   1.188 +					CONMSG("  User Messages: None\n");
   1.189 +				}
   1.190 +			} else {
   1.191 +				CONMSG("  User Messages: Failed to get list of user messages\n");
   1.192 +			}
   1.193 +
   1.194 +			return;
   1.195 +		} else if (strcmp(command, "refresh") == 0) {
   1.196 +			char full_path[255];
   1.197 +			g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s", g_ModPath.c_str(), GetPluginsFile());
   1.198 +
   1.199 +			LoadPluginsFromFile(full_path);
   1.200 +
   1.201 +			return;
   1.202 +		} else if (strcmp(command, "list") == 0) {
   1.203 +			size_t len;
   1.204 +			PluginIter i;
   1.205 +			char buffer[255];
   1.206 +			ISmmPlugin *plapi;
   1.207 +			const char *plname;
   1.208 +			SourceMM::CPluginManager::CPlugin *pl;
   1.209 +			unsigned int plnum = g_PluginMngr.GetPluginCount();
   1.210 +
   1.211 +#define IS_STR_FILLED(var) (var != NULL && var[0] != '\0')
   1.212 +
   1.213 +			if (!plnum)
   1.214 +			{
   1.215 +				CONMSG("No plugins loaded.\n");
   1.216 +				return;
   1.217 +			}
   1.218 +			else
   1.219 +			{
   1.220 +				CONMSG("Listing %d plugin%s:\n", plnum, (plnum > 1) ? "s" : "");
   1.221 +			}
   1.222 +
   1.223 +			for (i=g_PluginMngr._begin(); i!=g_PluginMngr._end(); i++)
   1.224 +			{
   1.225 +				pl = (*i);
   1.226 +				if (!pl)
   1.227 +				{
   1.228 +					break;
   1.229 +				}
   1.230 +
   1.231 +				len = 0;
   1.232 +
   1.233 +				if (pl->m_Status != Pl_Running)
   1.234 +				{
   1.235 +					len += UTIL_Format(buffer, sizeof(buffer), "  [%02d] <%s>", pl->m_Id, g_PluginMngr.GetStatusText(pl));
   1.236 +				}
   1.237 +				else
   1.238 +				{
   1.239 +					len += UTIL_Format(buffer, sizeof(buffer), "  [%02d]", pl->m_Id);
   1.240 +				}
   1.241 +
   1.242 +				if ((plapi = pl->m_API))
   1.243 +				{
   1.244 +					plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
   1.245 +					len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
   1.246 +
   1.247 +					if (IS_STR_FILLED(plapi->GetVersion()))
   1.248 +					{
   1.249 +						len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
   1.250 +					}
   1.251 +					if (IS_STR_FILLED(plapi->GetAuthor()))
   1.252 +					{
   1.253 +						UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
   1.254 +					}
   1.255 +				}
   1.256 +
   1.257 +				CONMSG("%s\n", buffer);
   1.258 +			}
   1.259 +
   1.260 +			return;
   1.261 +		} else if (strcmp(command, "cmds") == 0) {
   1.262 +			if (args >= 3)
   1.263 +			{
   1.264 +				int id = atoi(e->Cmd_Argv(2));
   1.265 +				SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   1.266 +
   1.267 +				if (!pl)
   1.268 +				{
   1.269 +					CONMSG("Plugin %d not found.\n", id);
   1.270 +					return;
   1.271 +				}
   1.272 +
   1.273 +				if (!pl->m_API)
   1.274 +				{
   1.275 +					CONMSG("Plugin %d is not loaded.\n", id);
   1.276 +				} else {
   1.277 +					CONMSG("Console commands for %s:\n", pl->m_API->GetName());
   1.278 +					SourceHook::List<ConCommandBase *>::iterator ci;
   1.279 +					size_t count = 0;
   1.280 +
   1.281 +					for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
   1.282 +					{
   1.283 +						count++;
   1.284 +						CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
   1.285 +					}
   1.286 +				}
   1.287 +			} else {
   1.288 +				CONMSG("Usage: meta cmds <id>\n");
   1.289 +			}
   1.290 +
   1.291 +			return;
   1.292 +		} else if (strcmp(command, "cvars") == 0) {
   1.293 +			if (args >= 3)
   1.294 +			{
   1.295 +				int id = atoi(e->Cmd_Argv(2));
   1.296 +				SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   1.297 +
   1.298 +				if (!pl)
   1.299 +				{
   1.300 +					CONMSG("Plugin %d not found.\n", id);
   1.301 +					return;
   1.302 +				}
   1.303 +
   1.304 +				if (!pl->m_API)
   1.305 +				{
   1.306 +					CONMSG("Plugin %d is not loaded.\n", id);
   1.307 +				} else {
   1.308 +					CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
   1.309 +					SourceHook::List<ConCommandBase *>::iterator ci;
   1.310 +					size_t count = 0;
   1.311 +
   1.312 +					for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
   1.313 +					{
   1.314 +						count++;
   1.315 +						CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
   1.316 +					}
   1.317 +				}
   1.318 +			} else {
   1.319 +				CONMSG("Usage: meta cvars <id>\n");
   1.320 +			}
   1.321 +
   1.322 +			return;
   1.323 +		} else if (strcmp(command, "info") == 0) {
   1.324 +			if (args >= 3)
   1.325 +			{
   1.326 +				int id = atoi(e->Cmd_Argv(2));
   1.327 +				SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   1.328 +				if (!pl)
   1.329 +				{
   1.330 +					CONMSG("Plugin %d not found.\n", id);
   1.331 +					return;
   1.332 +				}
   1.333 +
   1.334 +				if (!pl->m_API)
   1.335 +				{
   1.336 +					CONMSG("Plugin %d is not loaded.\n", id);
   1.337 +				} else {
   1.338 +					if (pl->m_Status == Pl_Paused)
   1.339 +					{
   1.340 +						CONMSG("Plugin %d is paused.\n", id);
   1.341 +					} else if (pl->m_Status == Pl_Running) {
   1.342 +						char run_msg[255];
   1.343 +						bool run = false;
   1.344 +						if (pl->m_API && pl->m_API->QueryRunning(run_msg, sizeof(run_msg)-1))
   1.345 +							run = true;
   1.346 +						if (run)
   1.347 +						{
   1.348 +							CONMSG("Plugin %d is running.\n", id);
   1.349 +						} else {
   1.350 +							CONMSG("Plugin %d is stopped: %s\n", id, run_msg);
   1.351 +						}
   1.352 +					}
   1.353 +					CONMSG("  Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
   1.354 +					CONMSG("  Version: %s\n", pl->m_API->GetVersion());
   1.355 +					CONMSG("  Description: %s\n", pl->m_API->GetDescription());
   1.356 +					CONMSG("  License: %s\n", pl->m_API->GetLicense());
   1.357 +					CONMSG("  URL: %s\n", pl->m_API->GetURL());
   1.358 +					CONMSG("  Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
   1.359 +				}
   1.360 +				CONMSG("File: %s\n\n", pl->m_File.c_str());
   1.361 +
   1.362 +				return;
   1.363 +			} else {
   1.364 +				CONMSG("Usage: meta info <id>\n");
   1.365 +
   1.366 +				return;
   1.367 +			}
   1.368 +		} else if (strcmp(command, "pause") == 0) {
   1.369 +			if (args >= 3)
   1.370 +			{
   1.371 +				int id = atoi(e->Cmd_Argv(2));
   1.372 +
   1.373 +				char error[255];
   1.374 +
   1.375 +				if (!g_PluginMngr.Pause(id, error, sizeof(error)))
   1.376 +				{
   1.377 +					CONMSG("Pause failed: %s\n", error);
   1.378 +					return;
   1.379 +				}
   1.380 +				
   1.381 +				CONMSG("Plugin %d has been paused.\n", id);
   1.382 +
   1.383 +				return;
   1.384 +			} else {
   1.385 +				CONMSG("Usage: meta pause <id>\n");
   1.386 +
   1.387 +				return;
   1.388 +			}
   1.389 +		} else if (strcmp(command, "unpause") == 0) {
   1.390 +			if (args >= 3)
   1.391 +			{
   1.392 +				int id = atoi(e->Cmd_Argv(2));
   1.393 +				char error[255];
   1.394 +
   1.395 +				if (!g_PluginMngr.Unpause(id, error, sizeof(error)))
   1.396 +				{
   1.397 +					CONMSG("Unpause failed: %s\n", error);
   1.398 +					return;
   1.399 +				}
   1.400 +
   1.401 +				CONMSG("Plugin %d has been unpaused.\n", id);
   1.402 +
   1.403 +				return;
   1.404 +			} else {
   1.405 +				CONMSG("Usage: meta unpause <id>\n");
   1.406 +
   1.407 +				return;
   1.408 +			}
   1.409 +		} else if (strcmp(command, "load") == 0) {
   1.410 +			if (args >= 3)
   1.411 +			{
   1.412 +				const char *file = e->Cmd_Argv(2);
   1.413 +				char full_path[255];
   1.414 +
   1.415 +				const char *alias = g_PluginMngr.LookupAlias(file);
   1.416 +				if (alias)
   1.417 +				{
   1.418 +					file = alias;
   1.419 +				}
   1.420 +
   1.421 +				if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
   1.422 +				{
   1.423 +					g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s", file);
   1.424 +				} else {
   1.425 +					const char *ext = UTIL_GetExtension(file);
   1.426 +#if defined WIN32 || defined _WIN32
   1.427 +					ext = ext ? "" : ".dll";
   1.428 +#else
   1.429 +					ext = ext ? "" : "_i486.so";
   1.430 +#endif
   1.431 +					g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s%s", g_ModPath.c_str(), file, ext);
   1.432 +				}
   1.433 +
   1.434 +				char error[255]={0};
   1.435 +				bool already;
   1.436 +				SourceMM::CPluginManager::CPlugin *pl;
   1.437 +
   1.438 +				PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error));
   1.439 +				pl = g_PluginMngr.FindById(id);
   1.440 +				if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
   1.441 +				{
   1.442 +					CONMSG("Failed to load plugin %s (%s).\n", file, error);
   1.443 +					return;
   1.444 +				}
   1.445 +
   1.446 +				if (!already)
   1.447 +				{
   1.448 +					CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
   1.449 +				} else {
   1.450 +					CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
   1.451 +				}
   1.452 +				
   1.453 +				return;
   1.454 +			} else {
   1.455 +				CONMSG("Usage: meta load <path>\n");
   1.456 +
   1.457 +				return;
   1.458 +			}
   1.459 +		} else if ( (strcmp(command, "alias") == 0) ||
   1.460 +					(strcmp(command, "aliases") == 0) ) {
   1.461 +			if (args >= 4)
   1.462 +			{
   1.463 +				const char *alias = e->Cmd_Argv(2);
   1.464 +				const char *value = e->Cmd_Argv(3);
   1.465 +
   1.466 +				g_PluginMngr.SetAlias(alias, value);
   1.467 +				if (value[0] == '\0')
   1.468 +				{
   1.469 +					CONMSG("Deleted alias: %s.\n", alias);
   1.470 +				} else {
   1.471 +					CONMSG("Set alias \"%s\" to: %s\n", alias, value);
   1.472 +				}
   1.473 +			} else if (args >= 3) {
   1.474 +				const char *alias = e->Cmd_Argv(2);
   1.475 +				const char *value = g_PluginMngr.LookupAlias(alias);
   1.476 +				if (value)
   1.477 +				{
   1.478 +					CONMSG("Alias \"%s\" is set to: %s\n", alias, value);
   1.479 +				} else {
   1.480 +					CONMSG("Alias \"%s\" was not found.\n", alias);
   1.481 +				}
   1.482 +			} else {
   1.483 +				SourceHook::List<SourceMM::CNameAlias *>::iterator iter, end;
   1.484 +				SourceMM::CNameAlias *p;
   1.485 +
   1.486 +				iter = g_PluginMngr._alias_begin();
   1.487 +				end = g_PluginMngr._alias_end();
   1.488 +				size_t total = 0;
   1.489 +				if (iter != end)
   1.490 +				{
   1.491 +					CONMSG("%-10.9s %s\n", "Alias", "File");
   1.492 +					CONMSG(" --- \n");
   1.493 +					for (; iter!=end; iter++)
   1.494 +					{
   1.495 +						p = (*iter);
   1.496 +						CONMSG("%-10.9s %s\n", p->alias.c_str(), p->value.c_str());
   1.497 +						total++;
   1.498 +					}
   1.499 +					CONMSG(" --- \n");
   1.500 +					CONMSG("%d aliases total.\n", total);
   1.501 +				} else {
   1.502 +					CONMSG("No aliases found.\n");
   1.503 +				}
   1.504 +			}
   1.505 +			return;
   1.506 +		} else if (strcmp(command, "unload") == 0) {
   1.507 +			if (args >= 3)
   1.508 +			{
   1.509 +				const char *file = e->Cmd_Argv(2);
   1.510 +				int id = atoi(file);
   1.511 +				char error[255]={0};
   1.512 +
   1.513 +				if (id == 0 && isalpha(file[0]))
   1.514 +				{
   1.515 +					char full_path[255];
   1.516 +					const char *alias = g_PluginMngr.LookupAlias(file);
   1.517 +
   1.518 +					if (alias)
   1.519 +					{
   1.520 +						file = alias;
   1.521 +					}
   1.522 +
   1.523 +					/* first check if it's a known filename */
   1.524 +					if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
   1.525 +					{
   1.526 +						g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s", file);
   1.527 +					} else {
   1.528 +						const char *ext = UTIL_GetExtension(file);
   1.529 +#if defined WIN32 || defined _WIN32
   1.530 +						ext = ext ? "" : ".dll";
   1.531 +#else
   1.532 +						ext = ext ? "" : "_i486.so";
   1.533 +#endif
   1.534 +						g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s%s", g_ModPath.c_str(), file, ext);
   1.535 +					}
   1.536 +
   1.537 +					SourceHook::List<SourceMM::CPluginManager::CPlugin *>::iterator iter, end;
   1.538 +					SourceMM::CPluginManager::CPlugin *pl;
   1.539 +					iter = g_PluginMngr._begin();
   1.540 +					end = g_PluginMngr._end();
   1.541 +					for (; iter!=end; iter++)
   1.542 +					{
   1.543 +						pl = (*iter);
   1.544 +						if (strcmp(pl->m_File.c_str(), full_path) == 0)
   1.545 +						{
   1.546 +							id = pl->m_Id;
   1.547 +							break;
   1.548 +						}
   1.549 +					}
   1.550 +					if (id == 0)
   1.551 +					{
   1.552 +						CONMSG("Plugin \"%s\" not found.\n", full_path);
   1.553 +						return;
   1.554 +					}
   1.555 +				}
   1.556 +
   1.557 +				if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
   1.558 +				{
   1.559 +					CONMSG("Unload failed: %s\n", error);
   1.560 +					return;
   1.561 +				}
   1.562 +
   1.563 +				CONMSG("Plugin %d unloaded.\n", id);
   1.564 +			} else {
   1.565 +				CONMSG("Usage: meta unload <id>\n");
   1.566 +			}
   1.567 +			return;
   1.568 +		} else if (strcmp(command, "force_unload") == 0) {
   1.569 +			if (args >= 3)
   1.570 +			{
   1.571 +				int id = atoi(e->Cmd_Argv(2));
   1.572 +				char error[255]={0};
   1.573 +
   1.574 +				if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
   1.575 +				{
   1.576 +					CONMSG("Force unload failed: %s\n", error);
   1.577 +					return;
   1.578 +				}
   1.579 +
   1.580 +				CONMSG("Plugin %d force unloaded.\n", id);
   1.581 +
   1.582 +				return;
   1.583 +			} else {
   1.584 +				CONMSG("Usage: meta force_unload <id>\n");
   1.585 +
   1.586 +				return;
   1.587 +			}
   1.588 +		} else if (strcmp(command, "clear") == 0) {
   1.589 +			if (!g_PluginMngr.UnloadAll())
   1.590 +			{
   1.591 +				CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
   1.592 +				return;
   1.593 +			} 
   1.594 +
   1.595 +			CONMSG("All plugins unloaded.\n");
   1.596 +
   1.597 +			return;
   1.598 +		} else if (strcmp(command, "retry") == 0) {
   1.599 +			if (args >= 3)
   1.600 +			{
   1.601 +				int id = atoi(e->Cmd_Argv(2));
   1.602 +				char error[255];
   1.603 +
   1.604 +				if (!g_PluginMngr.Retry(id, error, sizeof(error)))
   1.605 +				{
   1.606 +					CONMSG("Error reloading plugin: %s\n", error);
   1.607 +					return;
   1.608 +				}
   1.609 +
   1.610 +				CONMSG("Plugin %d successfully reloaded.\n", id);
   1.611 +
   1.612 +				return;
   1.613 +			} else {
   1.614 +				CONMSG("Usage: meta retry <id>\n");
   1.615 +
   1.616 +				return;
   1.617 +			}
   1.618 +		}
   1.619 +	}
   1.620 +
   1.621 +	CONMSG("Metamod:Source Menu\n");
   1.622 +	CONMSG("usage: meta <command> [arguments]\n");
   1.623 +	CONMSG("  alias        - List or set an alias\n");
   1.624 +	CONMSG("  clear        - Unload all plugins forcefully\n");
   1.625 +	CONMSG("  cmds         - Show plugin commands\n");
   1.626 +	CONMSG("  cvars        - Show plugin cvars\n");
   1.627 +	CONMSG("  credits      - About Metamod:Source\n");
   1.628 +	CONMSG("  force_unload - Forcefully unload a plugin\n");
   1.629 +	CONMSG("  game         - Information about GameDLL\n");
   1.630 +	CONMSG("  info         - Information about a plugin\n");
   1.631 +	CONMSG("  list         - List plugins\n");
   1.632 +	CONMSG("  load         - Load a plugin\n");
   1.633 +	CONMSG("  pause        - Pause a running plugin\n");
   1.634 +	CONMSG("  refresh      - Reparse plugins file\n");
   1.635 +	CONMSG("  retry        - Attempt to reload a plugin\n");
   1.636 +	CONMSG("  unload       - Unload a loaded plugin\n");
   1.637 +	CONMSG("  unpause      - Unpause a paused plugin\n");
   1.638 +	CONMSG("  version      - Version information\n");
   1.639 +}
   1.640 +
   1.641 +void ClientCommand_handler(edict_t *client)
   1.642 +{
   1.643 +	IVEngineServer *e = g_Engine.engine;
   1.644 +	const char *cmd = e->Cmd_Argv(0);
   1.645 +
   1.646 +	if (strcmp(cmd, "meta") == 0)
   1.647 +	{
   1.648 +		int args = e->Cmd_Argc();
   1.649 +		if (args == 2)
   1.650 +		{
   1.651 +			const char *subcmd = e->Cmd_Argv(1);
   1.652 +
   1.653 +			if (strcmp(subcmd, "credits") == 0)
   1.654 +			{
   1.655 +				CLIENT_CONMSG(client, "Metamod:Source was developed by:\n");
   1.656 +				CLIENT_CONMSG(client, "  SourceHook: Pavol \"PM OnoTo\" Marko\n");
   1.657 +				CLIENT_CONMSG(client, "  Core: David \"BAILOPAN\" Anderson\n");
   1.658 +				CLIENT_CONMSG(client, "  Core: Scott \"Damaged Soul\" Ehlert\n");
   1.659 +				CLIENT_CONMSG(client, "For more information, see the official website\n");
   1.660 +				CLIENT_CONMSG(client, "http://www.metamodsource.net/\n");
   1.661 +
   1.662 +				RETURN_META(MRES_SUPERCEDE);
   1.663 +			} else if(strcmp(subcmd, "version") == 0) {
   1.664 +				CLIENT_CONMSG(client, "Metamod:Source version %s\n", SOURCEMM_VERSION);
   1.665 +				CLIENT_CONMSG(client, "Compiled on: %s\n", SOURCEMM_DATE);
   1.666 +				CLIENT_CONMSG(client, "Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
   1.667 +				CLIENT_CONMSG(client, "SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
   1.668 +				CLIENT_CONMSG(client, "http://www.metamodsource.net/\n");
   1.669 +
   1.670 +				RETURN_META(MRES_SUPERCEDE);
   1.671 +			} else if(strcmp(subcmd, "list") == 0) {
   1.672 +				SourceMM::CPluginManager::CPlugin *pl;
   1.673 +				ISmmPlugin *plapi;
   1.674 +				const char *plname;
   1.675 +				PluginIter i;
   1.676 +				char buffer[256];
   1.677 +				int len = 0;
   1.678 +				int plnum = 0;
   1.679 +
   1.680 +				for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++, len=0)
   1.681 +				{
   1.682 +					pl = (*i);
   1.683 +					if (pl && pl->m_Status == Pl_Running)
   1.684 +					{
   1.685 +						plapi = pl->m_API;
   1.686 +						if (!plapi || !plapi->QueryRunning(NULL, 0))
   1.687 +						{
   1.688 +							continue;
   1.689 +						}
   1.690 +						plnum++;
   1.691 +
   1.692 +						len += UTIL_Format(buffer, sizeof(buffer), "  [%02d]", plnum);
   1.693 +
   1.694 +						plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
   1.695 +						len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
   1.696 +
   1.697 +						if (IS_STR_FILLED(plapi->GetVersion()))
   1.698 +						{
   1.699 +							len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
   1.700 +						}
   1.701 +						if (IS_STR_FILLED(plapi->GetAuthor()))
   1.702 +						{
   1.703 +							UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
   1.704 +						}
   1.705 +
   1.706 +						CLIENT_CONMSG(client, "%s\n", buffer);
   1.707 +					}
   1.708 +				}
   1.709 +
   1.710 +				if (!plnum)
   1.711 +				{
   1.712 +					CLIENT_CONMSG(client, "No active plugins loaded.\n");
   1.713 +				}
   1.714 +
   1.715 +				RETURN_META(MRES_SUPERCEDE);
   1.716 +			}
   1.717 +		}
   1.718 +
   1.719 +		CLIENT_CONMSG(client, "Metamod:Source Menu\n");
   1.720 +		CLIENT_CONMSG(client, "usage: meta <command>\n");
   1.721 +		CLIENT_CONMSG(client, "  credits - About Metamod:Source\n");
   1.722 +		CLIENT_CONMSG(client, "  list    - List plugins\n");
   1.723 +		CLIENT_CONMSG(client, "  version - Version information\n");
   1.724 +
   1.725 +		RETURN_META(MRES_SUPERCEDE);
   1.726 +	}
   1.727 +
   1.728 +	RETURN_META(MRES_IGNORED);
   1.729 +}
   1.730 +
   1.731 +void SMConVarAccessor::UnloadMetamodCommands()
   1.732 +{
   1.733 +	Unregister(&metamod_version);
   1.734 +	Unregister(&mm_pluginsfile);
   1.735 +	Unregister(&mm_basedir);
   1.736 +	Unregister(&meta_command);
   1.737 +}
   1.738 +
   1.739 +const char *GetPluginsFile()
   1.740 +{
   1.741 +	return mm_pluginsfile.GetString();
   1.742 +}
   1.743 +
   1.744 +const char *GetMetamodBaseDir()
   1.745 +{
   1.746 +	return mm_basedir.GetString();
   1.747 +}
   1.748 +
   1.749 +/* Signature for ICvar::GetCommands() in vstdlib for Win32 and Linux.
   1.750 + *
   1.751 + * 20226EE0 A1 50 5C 5A 20   mov         eax,dword ptr ds:[205A5C50h] <-- What we want
   1.752 + * 20226EE5 C3               ret              
   1.753 + */
   1.754 +#define CMDLIST_SIG "\xA1\x2A\x2A\x2A\x2A\xC3"
   1.755 +#define CMDLIST_SIGLEN 6
   1.756 +
   1.757 +/* Linux symbol name of ConCommandBase list in vstdlib */
   1.758 +#define CMDLIST_SYMBOL "_ZN14ConCommandBase18s_pConCommandBasesE"
   1.759 +
   1.760 +/* This function retrieves the address of the var that holds the top of the ConCommandBase list.
   1.761 + * Having this allows us to change the beginning of this list with ease.
   1.762 + *
   1.763 + * This craziness eliminates the need for the eternal command/cvar used previously which
   1.764 + * could have caused a crash as a result of registering commands/cvars more than once.
   1.765 + */
   1.766 +bool SMConVarAccessor::InitConCommandBaseList()
   1.767 +{
   1.768 +	char *vfunc = UTIL_GetOrigFunction(&ICvar::GetCommands, g_Engine.icvar, g_CvarPatch);
   1.769 +
   1.770 +	if (!vfunc)
   1.771 +	{
   1.772 +		return false;
   1.773 +	}
   1.774 +
   1.775 +#ifdef OS_WIN32
   1.776 +	if (UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN))
   1.777 +	{
   1.778 +		/* Skip past 0xA1 and get addr of ConCommandBase list var */
   1.779 +		m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1);
   1.780 +		return true;
   1.781 +	}
   1.782 +#elif defined OS_LINUX
   1.783 +	/* Try dlsym first */
   1.784 +	char path[PATH_SIZE];
   1.785 +	if (GetFileOfAddress((void *)g_Engine.icvar, path, sizeof(path)))
   1.786 +	{
   1.787 +		void *handle = dlopen(path, RTLD_NOW);
   1.788 +		if (handle)
   1.789 +		{
   1.790 +			m_TopConCommandBase = reinterpret_cast<ConCommandBase **>(dlsym(handle, CMDLIST_SYMBOL));
   1.791 +			dlclose(handle);
   1.792 +			return true;
   1.793 +		}
   1.794 +	}
   1.795 +
   1.796 +	/* If dlsym failed, then verify signature of function */
   1.797 +	if (!m_TopConCommandBase && UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN))
   1.798 +	{
   1.799 +		/* Skip past 0xA1 and get addr of ConCommandBase list var */
   1.800 +		m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1);
   1.801 +		return true;
   1.802 +	}
   1.803 +#endif
   1.804 +
   1.805 +	return false;
   1.806 +}