core-legacy/concommands.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Wed May 21 17:26:09 2014 -0400 (2014-05-21)
changeset 917 95639ee45d3d
parent 887 e30d4a37f5e5
permissions -rw-r--r--
Trigger build for hl2sdk-l4d changes.
     1 /* ======== SourceMM ========
     2  * Copyright (C) 2004-2010 Metamod:Source Development Team
     3  * No warranties of any kind
     4  *
     5  * License: zlib/libpng
     6  *
     7  * Author(s): David "BAILOPAN" Anderson
     8  * ============================
     9  */
    10 
    11 #include <ctype.h>
    12 #include "convar_smm.h"
    13 #include "CSmmAPI.h"
    14 #include "concommands.h"
    15 #include "CPlugin.h"
    16 #include "sh_string.h"
    17 #include "sh_list.h"
    18 #include "util.h"
    19 #include <versionlib.h>
    20 
    21 /**
    22  * @brief Console Command Implementations
    23  * @file concommands.cpp
    24  */
    25 
    26 SMConVarAccessor g_SMConVarAccessor;
    27 
    28 SMConVarAccessor::SMConVarAccessor()
    29 {
    30 	m_TopConCommandBase = NULL;
    31 }
    32 
    33 bool SMConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand)
    34 {
    35 	/* Add the FCVAR_GAMEDLL flag 
    36 	 * => No crash on exit!
    37 	 * UPDATE: Do _not_ add the FCVAR_GAMEDLL flag here, as it
    38 	 * causes the command to be unusable on listen servers until you load a map
    39 	 * We will set the FCVAR_GAMEDLL flag on all commands we have registered once we are being unloaded
    40 	 */
    41 	// pCommand->AddFlags(FCVAR_GAMEDLL);
    42 	m_RegisteredCommands.push_back(pCommand);
    43 
    44 	pCommand->SetNext(NULL);
    45 	g_Engine.icvar->RegisterConCommandBase(pCommand);
    46 
    47 	return true;
    48 }
    49 
    50 bool SMConVarAccessor::Register(ConCommandBase *pCommand)
    51 {
    52 	/* Simple, don't mark as part of sourcemm! */
    53 	pCommand->SetNext(NULL);
    54 	g_Engine.icvar->RegisterConCommandBase(pCommand);
    55 
    56 	return true;
    57 }
    58 
    59 void SMConVarAccessor::MarkCommandsAsGameDLL()
    60 {
    61 	for (SourceHook::List<ConCommandBase*>::iterator iter = m_RegisteredCommands.begin();
    62 		iter != m_RegisteredCommands.end(); ++iter)
    63 	{
    64 		(*iter)->AddFlags(FCVAR_GAMEDLL);
    65 	}
    66 }
    67 
    68 void SMConVarAccessor::Unregister(ConCommandBase *pCommand)
    69 {
    70 	ConCommandBase *pCur = NULL;
    71 	ConCommandBase *pPrev = NULL;
    72 
    73 	if (!pCommand || !pCommand->IsRegistered())
    74 	{
    75 		return;
    76 	}
    77 
    78 	pCur = g_Engine.icvar->GetCommands();
    79 	pCommand->SetRegistered(false);
    80 
    81 	if (!m_TopConCommandBase || !pCur)
    82 	{
    83 		return;
    84 	}
    85 
    86 	if (pCur == pCommand)
    87 	{
    88 		*m_TopConCommandBase = const_cast<ConCommandBase *>(pCommand->GetNext());
    89 		pCommand->SetNext(NULL);
    90 		return;
    91 	}
    92 	
    93 	pPrev = pCur;
    94 	pCur = const_cast<ConCommandBase *>(pCur->GetNext());
    95 
    96 	while (pCur)
    97 	{
    98 		if (pCur == pCommand)
    99 		{
   100 			pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
   101 			pCommand->SetNext(NULL);
   102 		}
   103 
   104 		pPrev = pCur;
   105 		pCur = const_cast<ConCommandBase *>(pCur->GetNext());
   106 	}
   107 }
   108 
   109 ConVar metamod_version("metamod_version", METAMOD_VERSION, FCVAR_SPONLY | FCVAR_NOTIFY, "Metamod:Source Version");
   110 #ifdef OS_WIN32
   111 ConVar mm_pluginsfile("mm_pluginsfile", "addons\\metamod\\metaplugins.ini", FCVAR_SPONLY, "Metamod:Source Plugins File");
   112 ConVar mm_basedir("mm_basedir", "addons\\metamod", FCVAR_SPONLY, "Metamod:Source base folder");
   113 #else
   114 ConVar mm_pluginsfile("mm_pluginsfile", "addons/metamod/metaplugins.ini", FCVAR_SPONLY, "Metamod:Source Plugins File");
   115 ConVar mm_basedir("mm_basedir", "addons/metamod", FCVAR_SPONLY, "Metamod:Source base folder");
   116 #endif
   117 
   118 CON_COMMAND(meta, "Metamod:Source Menu")
   119 {
   120 	IVEngineServer *e = g_Engine.engine;
   121 
   122 	int args = e->Cmd_Argc();
   123 
   124 	if (!g_GameDll.loaded && !g_bLevelChanged)
   125 	{
   126 		CONMSG("WARNING: You must change the map to activate Metamod:Source.\n");
   127 		return;
   128 	}
   129 
   130 	if (args >= 2)
   131 	{
   132 		const char *command = e->Cmd_Argv(1);
   133 		if (strcmp(command, "credits") == 0)
   134 		{
   135 			CONMSG("Metamod:Source was developed by:\n");
   136 			CONMSG("  SourceHook: Pavol \"PM OnoTo\" Marko\n");
   137 			CONMSG("  GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
   138 			CONMSG("  GameDLL: Scott \"DS\" Ehlert\n");
   139 			CONMSG("For more information, see the official website\n");
   140 			CONMSG("http://www.metamodsource.net/\n");
   141 			
   142 			return;
   143 		} else if (strcmp(command, "version") == 0) {
   144 			CONMSG("Metamod:Source version %s\n", METAMOD_VERSION);
   145 			CONMSG("Build ID: %s\n", METAMOD_BUILD_ID);
   146 			if (g_GameDll.loaded)
   147 			{
   148 				CONMSG("Loaded As: GameDLL (gameinfo.txt)\n");
   149 			}
   150 			else
   151 			{
   152 				CONMSG("Loaded As: Valve Server Plugin\n");
   153 			}
   154 			CONMSG("Compiled on: %s\n", SOURCEMM_DATE);
   155 			CONMSG("Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
   156 			CONMSG("SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
   157 			CONMSG("http://www.metamodsource.net/\n");
   158 
   159 			return;
   160 		} else if (strcmp(command, "game") == 0) {
   161 			char bin_path[PATH_SIZE];
   162 			GetFileOfAddress((void*)g_GameDll.factory, bin_path, sizeof(bin_path));
   163 
   164 			CONMSG("GameDLL Information\n");
   165 			CONMSG("  Description: %s\n", g_GameDll.pGameDLL->GetGameDescription());
   166 			CONMSG("  Mod Path: %s\n", g_ModPath.c_str());
   167 			CONMSG("  DLL Path: %s\n", bin_path);
   168 			CONMSG("  Interface: ServerGameDLL%03d\n", g_GameDllVersion);
   169 
   170 			if (g_Engine.original)
   171 			{
   172 				CONMSG("  Engine: Original (pre-Episode 1)\n");
   173 			}
   174 			else
   175 			{
   176 				CONMSG("  Engine: Episode 1 (2004)\n");
   177 			}
   178 
   179 			// Display user messages
   180 			const char *msgname;
   181 			int msgsize;
   182 			int msgcount = g_SmmAPI.GetUserMessageCount();
   183 
   184 			if (msgcount > 0)
   185 			{
   186 				CONMSG("  User Messages:  %-32.31s  %-5s  %-5s\n", "Name", "Index", "Size");
   187 
   188 				for (int i = 0; i < msgcount; i++)
   189 				{
   190 					msgname = g_SmmAPI.GetUserMessage(i, &msgsize);
   191 
   192 					CONMSG("                  %-32.31s  %-5d  %-5d\n", msgname, i, msgsize); 
   193 				}
   194 
   195 				CONMSG("  %d user message%s in total\n", msgcount, (msgcount > 1) ? "s" : "");
   196 			} else {
   197 				CONMSG("  User Messages: None\n");
   198 			}
   199 
   200 			return;
   201 		} else if (strcmp(command, "refresh") == 0) {
   202 			char filepath[PATH_SIZE], vdfpath[PATH_SIZE];
   203 			g_SmmAPI.PathFormat(filepath, sizeof(filepath), "%s/%s", g_ModPath.c_str(), GetPluginsFile());
   204 			g_SmmAPI.PathFormat(vdfpath, sizeof(vdfpath), "%s/%s", g_ModPath.c_str(), GetMetamodBaseDir());
   205 
   206 			LoadPlugins(filepath, vdfpath);
   207 
   208 			return;
   209 		} else if (strcmp(command, "list") == 0) {
   210 			size_t len;
   211 			PluginIter i;
   212 			char buffer[255];
   213 			ISmmPlugin *plapi;
   214 			const char *plname;
   215 			SourceMM::CPluginManager::CPlugin *pl;
   216 			unsigned int plnum = g_PluginMngr.GetPluginCount();
   217 
   218 #define IS_STR_FILLED(var) (var != NULL && var[0] != '\0')
   219 
   220 			if (!plnum)
   221 			{
   222 				CONMSG("No plugins loaded.\n");
   223 				return;
   224 			}
   225 			else
   226 			{
   227 				CONMSG("Listing %d plugin%s:\n", plnum, (plnum > 1) ? "s" : "");
   228 			}
   229 
   230 			for (i=g_PluginMngr._begin(); i!=g_PluginMngr._end(); i++)
   231 			{
   232 				pl = (*i);
   233 				if (!pl)
   234 				{
   235 					break;
   236 				}
   237 
   238 				len = 0;
   239 
   240 				if (pl->m_Status != Pl_Running)
   241 				{
   242 					len += UTIL_Format(buffer, sizeof(buffer), "  [%02d] <%s>", pl->m_Id, g_PluginMngr.GetStatusText(pl));
   243 				}
   244 				else
   245 				{
   246 					len += UTIL_Format(buffer, sizeof(buffer), "  [%02d]", pl->m_Id);
   247 				}
   248 
   249 				if ((plapi = pl->m_API))
   250 				{
   251 					plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
   252 					len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
   253 
   254 					if (IS_STR_FILLED(plapi->GetVersion()))
   255 					{
   256 						len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
   257 					}
   258 					if (IS_STR_FILLED(plapi->GetAuthor()))
   259 					{
   260 						UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
   261 					}
   262 				}
   263 
   264 				CONMSG("%s\n", buffer);
   265 			}
   266 
   267 			return;
   268 		} else if (strcmp(command, "cmds") == 0) {
   269 			if (args >= 3)
   270 			{
   271 				int id = atoi(e->Cmd_Argv(2));
   272 				SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   273 
   274 				if (!pl)
   275 				{
   276 					CONMSG("Plugin %d not found.\n", id);
   277 					return;
   278 				}
   279 
   280 				if (!pl->m_API)
   281 				{
   282 					CONMSG("Plugin %d is not loaded.\n", id);
   283 				} else {
   284 					CONMSG("Console commands for %s:\n", pl->m_API->GetName());
   285 					SourceHook::List<ConCommandBase *>::iterator ci;
   286 					size_t count = 0;
   287 
   288 					for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
   289 					{
   290 						count++;
   291 						CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
   292 					}
   293 				}
   294 			} else {
   295 				CONMSG("Usage: meta cmds <id>\n");
   296 			}
   297 
   298 			return;
   299 		} else if (strcmp(command, "cvars") == 0) {
   300 			if (args >= 3)
   301 			{
   302 				int id = atoi(e->Cmd_Argv(2));
   303 				SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   304 
   305 				if (!pl)
   306 				{
   307 					CONMSG("Plugin %d not found.\n", id);
   308 					return;
   309 				}
   310 
   311 				if (!pl->m_API)
   312 				{
   313 					CONMSG("Plugin %d is not loaded.\n", id);
   314 				} else {
   315 					CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
   316 					SourceHook::List<ConCommandBase *>::iterator ci;
   317 					size_t count = 0;
   318 
   319 					for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
   320 					{
   321 						count++;
   322 						CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
   323 					}
   324 				}
   325 			} else {
   326 				CONMSG("Usage: meta cvars <id>\n");
   327 			}
   328 
   329 			return;
   330 		} else if (strcmp(command, "info") == 0) {
   331 			if (args >= 3)
   332 			{
   333 				int id = atoi(e->Cmd_Argv(2));
   334 				SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   335 				if (!pl)
   336 				{
   337 					CONMSG("Plugin %d not found.\n", id);
   338 					return;
   339 				}
   340 
   341 				if (!pl->m_API)
   342 				{
   343 					CONMSG("Plugin %d is not loaded.\n", id);
   344 				} else {
   345 					if (pl->m_Status == Pl_Paused)
   346 					{
   347 						CONMSG("Plugin %d is paused.\n", id);
   348 					} else if (pl->m_Status == Pl_Running) {
   349 						char run_msg[255];
   350 						bool run = false;
   351 						if (pl->m_API && pl->m_API->QueryRunning(run_msg, sizeof(run_msg)-1))
   352 							run = true;
   353 						if (run)
   354 						{
   355 							CONMSG("Plugin %d is running.\n", id);
   356 						} else {
   357 							CONMSG("Plugin %d is stopped: %s\n", id, run_msg);
   358 						}
   359 					}
   360 					CONMSG("  Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
   361 					CONMSG("  Version: %s\n", pl->m_API->GetVersion());
   362 					CONMSG("  Description: %s\n", pl->m_API->GetDescription());
   363 					CONMSG("  License: %s\n", pl->m_API->GetLicense());
   364 					CONMSG("  URL: %s\n", pl->m_API->GetURL());
   365 					CONMSG("  Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
   366 				}
   367 				CONMSG("File: %s\n\n", pl->m_File.c_str());
   368 
   369 				return;
   370 			} else {
   371 				CONMSG("Usage: meta info <id>\n");
   372 
   373 				return;
   374 			}
   375 		} else if (strcmp(command, "pause") == 0) {
   376 			if (args >= 3)
   377 			{
   378 				int id = atoi(e->Cmd_Argv(2));
   379 
   380 				char error[255];
   381 
   382 				if (!g_PluginMngr.Pause(id, error, sizeof(error)))
   383 				{
   384 					CONMSG("Pause failed: %s\n", error);
   385 					return;
   386 				}
   387 				
   388 				CONMSG("Plugin %d has been paused.\n", id);
   389 
   390 				return;
   391 			} else {
   392 				CONMSG("Usage: meta pause <id>\n");
   393 
   394 				return;
   395 			}
   396 		} else if (strcmp(command, "unpause") == 0) {
   397 			if (args >= 3)
   398 			{
   399 				int id = atoi(e->Cmd_Argv(2));
   400 				char error[255];
   401 
   402 				if (!g_PluginMngr.Unpause(id, error, sizeof(error)))
   403 				{
   404 					CONMSG("Unpause failed: %s\n", error);
   405 					return;
   406 				}
   407 
   408 				CONMSG("Plugin %d has been unpaused.\n", id);
   409 
   410 				return;
   411 			} else {
   412 				CONMSG("Usage: meta unpause <id>\n");
   413 
   414 				return;
   415 			}
   416 		} else if (strcmp(command, "load") == 0) {
   417 			if (args >= 3)
   418 			{
   419 				const char *file = e->Cmd_Argv(2);
   420 				char full_path[255];
   421 
   422 				const char *alias = g_PluginMngr.LookupAlias(file);
   423 				if (alias)
   424 				{
   425 					file = alias;
   426 				}
   427 
   428 				g_SmmAPI.GetFullPluginPath(file, full_path, sizeof(full_path));
   429 
   430 				char error[255]={0};
   431 				bool already;
   432 				SourceMM::CPluginManager::CPlugin *pl;
   433 
   434 				PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error));
   435 				pl = g_PluginMngr.FindById(id);
   436 				if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
   437 				{
   438 					CONMSG("Failed to load plugin %s (%s).\n", file, error);
   439 					return;
   440 				}
   441 
   442 				if (!already)
   443 				{
   444 					CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
   445 				} else {
   446 					CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
   447 				}
   448 				
   449 				return;
   450 			} else {
   451 				CONMSG("Usage: meta load <path>\n");
   452 
   453 				return;
   454 			}
   455 		} else if ( (strcmp(command, "alias") == 0) ||
   456 					(strcmp(command, "aliases") == 0) ) {
   457 			if (args >= 4)
   458 			{
   459 				const char *alias = e->Cmd_Argv(2);
   460 				const char *value = e->Cmd_Argv(3);
   461 
   462 				g_PluginMngr.SetAlias(alias, value);
   463 				if (value[0] == '\0')
   464 				{
   465 					CONMSG("Deleted alias: %s.\n", alias);
   466 				} else {
   467 					CONMSG("Set alias \"%s\" to: %s\n", alias, value);
   468 				}
   469 			} else if (args >= 3) {
   470 				const char *alias = e->Cmd_Argv(2);
   471 				const char *value = g_PluginMngr.LookupAlias(alias);
   472 				if (value)
   473 				{
   474 					CONMSG("Alias \"%s\" is set to: %s\n", alias, value);
   475 				} else {
   476 					CONMSG("Alias \"%s\" was not found.\n", alias);
   477 				}
   478 			} else {
   479 				SourceHook::List<SourceMM::CNameAlias *>::iterator iter, end;
   480 				SourceMM::CNameAlias *p;
   481 
   482 				iter = g_PluginMngr._alias_begin();
   483 				end = g_PluginMngr._alias_end();
   484 				size_t total = 0;
   485 				if (iter != end)
   486 				{
   487 					CONMSG("%-10.9s %s\n", "Alias", "File");
   488 					CONMSG(" --- \n");
   489 					for (; iter!=end; iter++)
   490 					{
   491 						p = (*iter);
   492 						CONMSG("%-10.9s %s\n", p->alias.c_str(), p->value.c_str());
   493 						total++;
   494 					}
   495 					CONMSG(" --- \n");
   496 					CONMSG("%d aliases total.\n", total);
   497 				} else {
   498 					CONMSG("No aliases found.\n");
   499 				}
   500 			}
   501 			return;
   502 		} else if (strcmp(command, "unload") == 0) {
   503 			if (args >= 3)
   504 			{
   505 				const char *file = e->Cmd_Argv(2);
   506 				int id = atoi(file);
   507 				char error[255]={0};
   508 
   509 				if (id == 0 && isalpha(file[0]))
   510 				{
   511 					char full_path[255];
   512 					const char *alias = g_PluginMngr.LookupAlias(file);
   513 
   514 					if (alias)
   515 					{
   516 						file = alias;
   517 					}
   518 
   519 					g_SmmAPI.GetFullPluginPath(file, full_path, sizeof(full_path));
   520 
   521 					SourceHook::List<SourceMM::CPluginManager::CPlugin *>::iterator iter, end;
   522 					SourceMM::CPluginManager::CPlugin *pl;
   523 					iter = g_PluginMngr._begin();
   524 					end = g_PluginMngr._end();
   525 					for (; iter!=end; iter++)
   526 					{
   527 						pl = (*iter);
   528 						if (strcmp(pl->m_File.c_str(), full_path) == 0)
   529 						{
   530 							id = pl->m_Id;
   531 							break;
   532 						}
   533 					}
   534 					if (id == 0)
   535 					{
   536 						CONMSG("Plugin \"%s\" not found.\n", full_path);
   537 						return;
   538 					}
   539 				}
   540 
   541 				if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
   542 				{
   543 					CONMSG("Unload failed: %s\n", error);
   544 					return;
   545 				}
   546 
   547 				CONMSG("Plugin %d unloaded.\n", id);
   548 			} else {
   549 				CONMSG("Usage: meta unload <id>\n");
   550 			}
   551 			return;
   552 		} else if (strcmp(command, "force_unload") == 0) {
   553 			if (args >= 3)
   554 			{
   555 				int id = atoi(e->Cmd_Argv(2));
   556 				char error[255]={0};
   557 
   558 				if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
   559 				{
   560 					CONMSG("Force unload failed: %s\n", error);
   561 					return;
   562 				}
   563 
   564 				CONMSG("Plugin %d force unloaded.\n", id);
   565 
   566 				return;
   567 			} else {
   568 				CONMSG("Usage: meta force_unload <id>\n");
   569 
   570 				return;
   571 			}
   572 		} else if (strcmp(command, "clear") == 0) {
   573 			if (!g_PluginMngr.UnloadAll())
   574 			{
   575 				CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
   576 				return;
   577 			} 
   578 
   579 			CONMSG("All plugins unloaded.\n");
   580 
   581 			return;
   582 		} else if (strcmp(command, "retry") == 0) {
   583 			if (args >= 3)
   584 			{
   585 				int id = atoi(e->Cmd_Argv(2));
   586 				char error[255];
   587 
   588 				if (!g_PluginMngr.Retry(id, error, sizeof(error)))
   589 				{
   590 					CONMSG("Error reloading plugin: %s\n", error);
   591 					return;
   592 				}
   593 
   594 				CONMSG("Plugin %d successfully reloaded.\n", id);
   595 
   596 				return;
   597 			} else {
   598 				CONMSG("Usage: meta retry <id>\n");
   599 
   600 				return;
   601 			}
   602 		}
   603 	}
   604 
   605 	CONMSG("Metamod:Source Menu\n");
   606 	CONMSG("usage: meta <command> [arguments]\n");
   607 	CONMSG("  alias        - List or set an alias\n");
   608 	CONMSG("  clear        - Unload all plugins forcefully\n");
   609 	CONMSG("  cmds         - Show plugin commands\n");
   610 	CONMSG("  cvars        - Show plugin cvars\n");
   611 	CONMSG("  credits      - About Metamod:Source\n");
   612 	CONMSG("  force_unload - Forcefully unload a plugin\n");
   613 	CONMSG("  game         - Information about GameDLL\n");
   614 	CONMSG("  info         - Information about a plugin\n");
   615 	CONMSG("  list         - List plugins\n");
   616 	CONMSG("  load         - Load a plugin\n");
   617 	CONMSG("  pause        - Pause a running plugin\n");
   618 	CONMSG("  refresh      - Reparse plugin files\n");
   619 	CONMSG("  retry        - Attempt to reload a plugin\n");
   620 	CONMSG("  unload       - Unload a loaded plugin\n");
   621 	CONMSG("  unpause      - Unpause a paused plugin\n");
   622 	CONMSG("  version      - Version information\n");
   623 }
   624 
   625 void ClientCommand_handler(edict_t *client)
   626 {
   627 	IVEngineServer *e = g_Engine.engine;
   628 	const char *cmd = e->Cmd_Argv(0);
   629 
   630 	if (strcmp(cmd, "meta") == 0)
   631 	{
   632 		int args = e->Cmd_Argc();
   633 		if (args == 2)
   634 		{
   635 			const char *subcmd = e->Cmd_Argv(1);
   636 
   637 			if (strcmp(subcmd, "credits") == 0)
   638 			{
   639 				CLIENT_CONMSG(client, "Metamod:Source was developed by:\n");
   640 				CLIENT_CONMSG(client, "  SourceHook: Pavol \"PM OnoTo\" Marko\n");
   641 				CLIENT_CONMSG(client, "  Core: David \"BAILOPAN\" Anderson\n");
   642 				CLIENT_CONMSG(client, "  Core: Scott \"DS\" Ehlert\n");
   643 				CLIENT_CONMSG(client, "For more information, see the official website\n");
   644 				CLIENT_CONMSG(client, "http://www.metamodsource.net/\n");
   645 
   646 				RETURN_META(MRES_SUPERCEDE);
   647 			} else if(strcmp(subcmd, "version") == 0) {
   648 				CLIENT_CONMSG(client, "Metamod:Source version %s\n", METAMOD_VERSION);
   649 				CLIENT_CONMSG(client, "Compiled on: %s\n", SOURCEMM_DATE);
   650 				CLIENT_CONMSG(client, "Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
   651 				CLIENT_CONMSG(client, "SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
   652 				CLIENT_CONMSG(client, "http://www.metamodsource.net/\n");
   653 
   654 				RETURN_META(MRES_SUPERCEDE);
   655 			} else if(strcmp(subcmd, "list") == 0) {
   656 				SourceMM::CPluginManager::CPlugin *pl;
   657 				ISmmPlugin *plapi;
   658 				const char *plname;
   659 				PluginIter i;
   660 				char buffer[256];
   661 				int len = 0;
   662 				int plnum = 0;
   663 
   664 				for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++, len=0)
   665 				{
   666 					pl = (*i);
   667 					if (pl && pl->m_Status == Pl_Running)
   668 					{
   669 						plapi = pl->m_API;
   670 						if (!plapi || !plapi->QueryRunning(NULL, 0))
   671 						{
   672 							continue;
   673 						}
   674 						plnum++;
   675 
   676 						len += UTIL_Format(buffer, sizeof(buffer), "  [%02d]", plnum);
   677 
   678 						plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
   679 						len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
   680 
   681 						if (IS_STR_FILLED(plapi->GetVersion()))
   682 						{
   683 							len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
   684 						}
   685 						if (IS_STR_FILLED(plapi->GetAuthor()))
   686 						{
   687 							UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
   688 						}
   689 
   690 						CLIENT_CONMSG(client, "%s\n", buffer);
   691 					}
   692 				}
   693 
   694 				if (!plnum)
   695 				{
   696 					CLIENT_CONMSG(client, "No active plugins loaded.\n");
   697 				}
   698 
   699 				RETURN_META(MRES_SUPERCEDE);
   700 			}
   701 		}
   702 
   703 		CLIENT_CONMSG(client, "Metamod:Source Menu\n");
   704 		CLIENT_CONMSG(client, "usage: meta <command>\n");
   705 		CLIENT_CONMSG(client, "  credits - About Metamod:Source\n");
   706 		CLIENT_CONMSG(client, "  list    - List plugins\n");
   707 		CLIENT_CONMSG(client, "  version - Version information\n");
   708 
   709 		RETURN_META(MRES_SUPERCEDE);
   710 	}
   711 
   712 	RETURN_META(MRES_IGNORED);
   713 }
   714 
   715 void SMConVarAccessor::UnloadMetamodCommands()
   716 {
   717 	Unregister(&metamod_version);
   718 	Unregister(&mm_pluginsfile);
   719 	Unregister(&mm_basedir);
   720 	Unregister(&meta_command);
   721 }
   722 
   723 const char *GetPluginsFile()
   724 {
   725 	return mm_pluginsfile.GetString();
   726 }
   727 
   728 const char *GetMetamodBaseDir()
   729 {
   730 	return mm_basedir.GetString();
   731 }
   732 
   733 /* Signature for ICvar::GetCommands() in vstdlib for Win32 and Linux.
   734  *
   735  * 20226EE0 A1 50 5C 5A 20   mov         eax,dword ptr ds:[205A5C50h] <-- What we want
   736  * 20226EE5 C3               ret              
   737  */
   738 #define CMDLIST_SIG "\xA1\x2A\x2A\x2A\x2A\xC3"
   739 #define CMDLIST_SIGLEN 6
   740 
   741 /* Linux symbol name of ConCommandBase list in vstdlib */
   742 #define CMDLIST_SYMBOL "_ZN14ConCommandBase18s_pConCommandBasesE"
   743 
   744 /* This function retrieves the address of the var that holds the top of the ConCommandBase list.
   745  * Having this allows us to change the beginning of this list with ease.
   746  *
   747  * This craziness eliminates the need for the eternal command/cvar used previously which
   748  * could have caused a crash as a result of registering commands/cvars more than once.
   749  */
   750 bool SMConVarAccessor::InitConCommandBaseList()
   751 {
   752 	char *vfunc = UTIL_GetOrigFunction(&ICvar::GetCommands, g_Engine.icvar);
   753 
   754 	if (!vfunc)
   755 	{
   756 		return false;
   757 	}
   758 
   759 #ifdef OS_WIN32
   760 	if (UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN))
   761 	{
   762 		/* Skip past 0xA1 and get addr of ConCommandBase list var */
   763 		m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1);
   764 		return true;
   765 	}
   766 #elif defined OS_LINUX
   767 	/* Try dlsym first */
   768 	char path[PATH_SIZE];
   769 	if (GetFileOfAddress((void *)g_Engine.icvar, path, sizeof(path)))
   770 	{
   771 		void *handle = dlopen(path, RTLD_NOW);
   772 		if (handle)
   773 		{
   774 			m_TopConCommandBase = reinterpret_cast<ConCommandBase **>(dlsym(handle, CMDLIST_SYMBOL));
   775 			dlclose(handle);
   776 			return true;
   777 		}
   778 	}
   779 
   780 	/* If dlsym failed, then verify signature of function */
   781 	if (!m_TopConCommandBase && UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN))
   782 	{
   783 		/* Skip past 0xA1 and get addr of ConCommandBase list var */
   784 		m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1);
   785 		return true;
   786 	}
   787 #endif
   788 
   789 	return false;
   790 }