core-legacy/concommands.cpp
author David Anderson <dvander@alliedmods.net>
Thu Oct 29 02:37:52 2009 -0400 (2009-10-29)
changeset 663 e07fd1dc5290
parent 659 b35a5fdbc654
child 668 648275cfea07
permissions -rw-r--r--
Fixed circular dependency between sourcemm.h/util.h (build bustage).
     1 /* ======== SourceMM ========
     2  * Copyright (C) 2004-2008 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 "vsp_listener.h"
    19 #include "util.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", MMS_FULL_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_bIsBridgedAsVsp && !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 \"Damaged Soul\" 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", MMS_FULL_VERSION);
   145 			CONMSG("Build ID: %s\n", MMS_BUILD_UNIQUEID);
   146 			if (g_bIsBridgedAsVsp)
   147 			{
   148 				CONMSG("Loaded As: Valve Server Plugin\n");
   149 			}
   150 			else
   151 			{
   152 				CONMSG("Loaded As: GameDLL (gameinfo.txt)\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 			if (g_SmmAPI.MsgCacheSuccessful())
   181 			{
   182 				const char *msgname;
   183 				int msgsize;
   184 				int msgcount = g_SmmAPI.GetUserMessageCount();
   185 
   186 				if (msgcount > 0)
   187 				{
   188 					CONMSG("  User Messages:  %-32.31s  %-5s  %-5s\n", "Name", "Index", "Size");
   189 
   190 					for (int i = 0; i < msgcount; i++)
   191 					{
   192 						msgname = g_SmmAPI.GetUserMessage(i, &msgsize);
   193 
   194 						CONMSG("                  %-32.31s  %-5d  %-5d\n", msgname, i, msgsize); 
   195 					}
   196 
   197 					CONMSG("  %d user message%s in total\n", msgcount, (msgcount > 1) ? "s" : "");
   198 				} else {
   199 					CONMSG("  User Messages: None\n");
   200 				}
   201 			} else {
   202 				CONMSG("  User Messages: Failed to get list of user messages\n");
   203 			}
   204 
   205 			return;
   206 		} else if (strcmp(command, "refresh") == 0) {
   207 			char filepath[PATH_SIZE], vdfpath[PATH_SIZE];
   208 			g_SmmAPI.PathFormat(filepath, sizeof(filepath), "%s/%s", g_ModPath.c_str(), GetPluginsFile());
   209 			g_SmmAPI.PathFormat(vdfpath, sizeof(vdfpath), "%s/%s", g_ModPath.c_str(), GetMetamodBaseDir());
   210 
   211 			LoadPlugins(filepath, vdfpath);
   212 
   213 			return;
   214 		} else if (strcmp(command, "list") == 0) {
   215 			size_t len;
   216 			PluginIter i;
   217 			char buffer[255];
   218 			ISmmPlugin *plapi;
   219 			const char *plname;
   220 			SourceMM::CPluginManager::CPlugin *pl;
   221 			unsigned int plnum = g_PluginMngr.GetPluginCount();
   222 
   223 #define IS_STR_FILLED(var) (var != NULL && var[0] != '\0')
   224 
   225 			if (!plnum)
   226 			{
   227 				CONMSG("No plugins loaded.\n");
   228 				return;
   229 			}
   230 			else
   231 			{
   232 				CONMSG("Listing %d plugin%s:\n", plnum, (plnum > 1) ? "s" : "");
   233 			}
   234 
   235 			for (i=g_PluginMngr._begin(); i!=g_PluginMngr._end(); i++)
   236 			{
   237 				pl = (*i);
   238 				if (!pl)
   239 				{
   240 					break;
   241 				}
   242 
   243 				len = 0;
   244 
   245 				if (pl->m_Status != Pl_Running)
   246 				{
   247 					len += UTIL_Format(buffer, sizeof(buffer), "  [%02d] <%s>", pl->m_Id, g_PluginMngr.GetStatusText(pl));
   248 				}
   249 				else
   250 				{
   251 					len += UTIL_Format(buffer, sizeof(buffer), "  [%02d]", pl->m_Id);
   252 				}
   253 
   254 				if ((plapi = pl->m_API))
   255 				{
   256 					plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
   257 					len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
   258 
   259 					if (IS_STR_FILLED(plapi->GetVersion()))
   260 					{
   261 						len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
   262 					}
   263 					if (IS_STR_FILLED(plapi->GetAuthor()))
   264 					{
   265 						UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
   266 					}
   267 				}
   268 
   269 				CONMSG("%s\n", buffer);
   270 			}
   271 
   272 			return;
   273 		} else if (strcmp(command, "cmds") == 0) {
   274 			if (args >= 3)
   275 			{
   276 				int id = atoi(e->Cmd_Argv(2));
   277 				SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   278 
   279 				if (!pl)
   280 				{
   281 					CONMSG("Plugin %d not found.\n", id);
   282 					return;
   283 				}
   284 
   285 				if (!pl->m_API)
   286 				{
   287 					CONMSG("Plugin %d is not loaded.\n", id);
   288 				} else {
   289 					CONMSG("Console commands for %s:\n", pl->m_API->GetName());
   290 					SourceHook::List<ConCommandBase *>::iterator ci;
   291 					size_t count = 0;
   292 
   293 					for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
   294 					{
   295 						count++;
   296 						CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
   297 					}
   298 				}
   299 			} else {
   300 				CONMSG("Usage: meta cmds <id>\n");
   301 			}
   302 
   303 			return;
   304 		} else if (strcmp(command, "cvars") == 0) {
   305 			if (args >= 3)
   306 			{
   307 				int id = atoi(e->Cmd_Argv(2));
   308 				SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   309 
   310 				if (!pl)
   311 				{
   312 					CONMSG("Plugin %d not found.\n", id);
   313 					return;
   314 				}
   315 
   316 				if (!pl->m_API)
   317 				{
   318 					CONMSG("Plugin %d is not loaded.\n", id);
   319 				} else {
   320 					CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
   321 					SourceHook::List<ConCommandBase *>::iterator ci;
   322 					size_t count = 0;
   323 
   324 					for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
   325 					{
   326 						count++;
   327 						CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
   328 					}
   329 				}
   330 			} else {
   331 				CONMSG("Usage: meta cvars <id>\n");
   332 			}
   333 
   334 			return;
   335 		} else if (strcmp(command, "info") == 0) {
   336 			if (args >= 3)
   337 			{
   338 				int id = atoi(e->Cmd_Argv(2));
   339 				SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   340 				if (!pl)
   341 				{
   342 					CONMSG("Plugin %d not found.\n", id);
   343 					return;
   344 				}
   345 
   346 				if (!pl->m_API)
   347 				{
   348 					CONMSG("Plugin %d is not loaded.\n", id);
   349 				} else {
   350 					if (pl->m_Status == Pl_Paused)
   351 					{
   352 						CONMSG("Plugin %d is paused.\n", id);
   353 					} else if (pl->m_Status == Pl_Running) {
   354 						char run_msg[255];
   355 						bool run = false;
   356 						if (pl->m_API && pl->m_API->QueryRunning(run_msg, sizeof(run_msg)-1))
   357 							run = true;
   358 						if (run)
   359 						{
   360 							CONMSG("Plugin %d is running.\n", id);
   361 						} else {
   362 							CONMSG("Plugin %d is stopped: %s\n", id, run_msg);
   363 						}
   364 					}
   365 					CONMSG("  Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
   366 					CONMSG("  Version: %s\n", pl->m_API->GetVersion());
   367 					CONMSG("  Description: %s\n", pl->m_API->GetDescription());
   368 					CONMSG("  License: %s\n", pl->m_API->GetLicense());
   369 					CONMSG("  URL: %s\n", pl->m_API->GetURL());
   370 					CONMSG("  Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
   371 				}
   372 				CONMSG("File: %s\n\n", pl->m_File.c_str());
   373 
   374 				return;
   375 			} else {
   376 				CONMSG("Usage: meta info <id>\n");
   377 
   378 				return;
   379 			}
   380 		} else if (strcmp(command, "pause") == 0) {
   381 			if (args >= 3)
   382 			{
   383 				int id = atoi(e->Cmd_Argv(2));
   384 
   385 				char error[255];
   386 
   387 				if (!g_PluginMngr.Pause(id, error, sizeof(error)))
   388 				{
   389 					CONMSG("Pause failed: %s\n", error);
   390 					return;
   391 				}
   392 				
   393 				CONMSG("Plugin %d has been paused.\n", id);
   394 
   395 				return;
   396 			} else {
   397 				CONMSG("Usage: meta pause <id>\n");
   398 
   399 				return;
   400 			}
   401 		} else if (strcmp(command, "unpause") == 0) {
   402 			if (args >= 3)
   403 			{
   404 				int id = atoi(e->Cmd_Argv(2));
   405 				char error[255];
   406 
   407 				if (!g_PluginMngr.Unpause(id, error, sizeof(error)))
   408 				{
   409 					CONMSG("Unpause failed: %s\n", error);
   410 					return;
   411 				}
   412 
   413 				CONMSG("Plugin %d has been unpaused.\n", id);
   414 
   415 				return;
   416 			} else {
   417 				CONMSG("Usage: meta unpause <id>\n");
   418 
   419 				return;
   420 			}
   421 		} else if (strcmp(command, "load") == 0) {
   422 			if (args >= 3)
   423 			{
   424 				const char *file = e->Cmd_Argv(2);
   425 				char full_path[255];
   426 
   427 				const char *alias = g_PluginMngr.LookupAlias(file);
   428 				if (alias)
   429 				{
   430 					file = alias;
   431 				}
   432 
   433 				if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
   434 				{
   435 					g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s", file);
   436 				} else {
   437 					const char *ext = UTIL_GetExtension(file);
   438 #if defined WIN32 || defined _WIN32
   439 					ext = ext ? "" : ".dll";
   440 #else
   441 					ext = ext ? "" : "_i486.so";
   442 #endif
   443 					g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s%s", g_ModPath.c_str(), file, ext);
   444 				}
   445 
   446 				char error[255]={0};
   447 				bool already;
   448 				SourceMM::CPluginManager::CPlugin *pl;
   449 
   450 				PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error));
   451 				pl = g_PluginMngr.FindById(id);
   452 				if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
   453 				{
   454 					CONMSG("Failed to load plugin %s (%s).\n", file, error);
   455 					return;
   456 				}
   457 
   458 				if (!already)
   459 				{
   460 					CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
   461 				} else {
   462 					CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
   463 				}
   464 				
   465 				return;
   466 			} else {
   467 				CONMSG("Usage: meta load <path>\n");
   468 
   469 				return;
   470 			}
   471 		} else if ( (strcmp(command, "alias") == 0) ||
   472 					(strcmp(command, "aliases") == 0) ) {
   473 			if (args >= 4)
   474 			{
   475 				const char *alias = e->Cmd_Argv(2);
   476 				const char *value = e->Cmd_Argv(3);
   477 
   478 				g_PluginMngr.SetAlias(alias, value);
   479 				if (value[0] == '\0')
   480 				{
   481 					CONMSG("Deleted alias: %s.\n", alias);
   482 				} else {
   483 					CONMSG("Set alias \"%s\" to: %s\n", alias, value);
   484 				}
   485 			} else if (args >= 3) {
   486 				const char *alias = e->Cmd_Argv(2);
   487 				const char *value = g_PluginMngr.LookupAlias(alias);
   488 				if (value)
   489 				{
   490 					CONMSG("Alias \"%s\" is set to: %s\n", alias, value);
   491 				} else {
   492 					CONMSG("Alias \"%s\" was not found.\n", alias);
   493 				}
   494 			} else {
   495 				SourceHook::List<SourceMM::CNameAlias *>::iterator iter, end;
   496 				SourceMM::CNameAlias *p;
   497 
   498 				iter = g_PluginMngr._alias_begin();
   499 				end = g_PluginMngr._alias_end();
   500 				size_t total = 0;
   501 				if (iter != end)
   502 				{
   503 					CONMSG("%-10.9s %s\n", "Alias", "File");
   504 					CONMSG(" --- \n");
   505 					for (; iter!=end; iter++)
   506 					{
   507 						p = (*iter);
   508 						CONMSG("%-10.9s %s\n", p->alias.c_str(), p->value.c_str());
   509 						total++;
   510 					}
   511 					CONMSG(" --- \n");
   512 					CONMSG("%d aliases total.\n", total);
   513 				} else {
   514 					CONMSG("No aliases found.\n");
   515 				}
   516 			}
   517 			return;
   518 		} else if (strcmp(command, "unload") == 0) {
   519 			if (args >= 3)
   520 			{
   521 				const char *file = e->Cmd_Argv(2);
   522 				int id = atoi(file);
   523 				char error[255]={0};
   524 
   525 				if (id == 0 && isalpha(file[0]))
   526 				{
   527 					char full_path[255];
   528 					const char *alias = g_PluginMngr.LookupAlias(file);
   529 
   530 					if (alias)
   531 					{
   532 						file = alias;
   533 					}
   534 
   535 					/* first check if it's a known filename */
   536 					if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
   537 					{
   538 						g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s", file);
   539 					} else {
   540 						const char *ext = UTIL_GetExtension(file);
   541 #if defined WIN32 || defined _WIN32
   542 						ext = ext ? "" : ".dll";
   543 #else
   544 						ext = ext ? "" : "_i486.so";
   545 #endif
   546 						g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s%s", g_ModPath.c_str(), file, ext);
   547 					}
   548 
   549 					SourceHook::List<SourceMM::CPluginManager::CPlugin *>::iterator iter, end;
   550 					SourceMM::CPluginManager::CPlugin *pl;
   551 					iter = g_PluginMngr._begin();
   552 					end = g_PluginMngr._end();
   553 					for (; iter!=end; iter++)
   554 					{
   555 						pl = (*iter);
   556 						if (strcmp(pl->m_File.c_str(), full_path) == 0)
   557 						{
   558 							id = pl->m_Id;
   559 							break;
   560 						}
   561 					}
   562 					if (id == 0)
   563 					{
   564 						CONMSG("Plugin \"%s\" not found.\n", full_path);
   565 						return;
   566 					}
   567 				}
   568 
   569 				if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
   570 				{
   571 					CONMSG("Unload failed: %s\n", error);
   572 					return;
   573 				}
   574 
   575 				CONMSG("Plugin %d unloaded.\n", id);
   576 			} else {
   577 				CONMSG("Usage: meta unload <id>\n");
   578 			}
   579 			return;
   580 		} else if (strcmp(command, "force_unload") == 0) {
   581 			if (args >= 3)
   582 			{
   583 				int id = atoi(e->Cmd_Argv(2));
   584 				char error[255]={0};
   585 
   586 				if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
   587 				{
   588 					CONMSG("Force unload failed: %s\n", error);
   589 					return;
   590 				}
   591 
   592 				CONMSG("Plugin %d force unloaded.\n", id);
   593 
   594 				return;
   595 			} else {
   596 				CONMSG("Usage: meta force_unload <id>\n");
   597 
   598 				return;
   599 			}
   600 		} else if (strcmp(command, "clear") == 0) {
   601 			if (!g_PluginMngr.UnloadAll())
   602 			{
   603 				CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
   604 				return;
   605 			} 
   606 
   607 			CONMSG("All plugins unloaded.\n");
   608 
   609 			return;
   610 		} else if (strcmp(command, "retry") == 0) {
   611 			if (args >= 3)
   612 			{
   613 				int id = atoi(e->Cmd_Argv(2));
   614 				char error[255];
   615 
   616 				if (!g_PluginMngr.Retry(id, error, sizeof(error)))
   617 				{
   618 					CONMSG("Error reloading plugin: %s\n", error);
   619 					return;
   620 				}
   621 
   622 				CONMSG("Plugin %d successfully reloaded.\n", id);
   623 
   624 				return;
   625 			} else {
   626 				CONMSG("Usage: meta retry <id>\n");
   627 
   628 				return;
   629 			}
   630 		}
   631 	}
   632 
   633 	CONMSG("Metamod:Source Menu\n");
   634 	CONMSG("usage: meta <command> [arguments]\n");
   635 	CONMSG("  alias        - List or set an alias\n");
   636 	CONMSG("  clear        - Unload all plugins forcefully\n");
   637 	CONMSG("  cmds         - Show plugin commands\n");
   638 	CONMSG("  cvars        - Show plugin cvars\n");
   639 	CONMSG("  credits      - About Metamod:Source\n");
   640 	CONMSG("  force_unload - Forcefully unload a plugin\n");
   641 	CONMSG("  game         - Information about GameDLL\n");
   642 	CONMSG("  info         - Information about a plugin\n");
   643 	CONMSG("  list         - List plugins\n");
   644 	CONMSG("  load         - Load a plugin\n");
   645 	CONMSG("  pause        - Pause a running plugin\n");
   646 	CONMSG("  refresh      - Reparse plugin files\n");
   647 	CONMSG("  retry        - Attempt to reload a plugin\n");
   648 	CONMSG("  unload       - Unload a loaded plugin\n");
   649 	CONMSG("  unpause      - Unpause a paused plugin\n");
   650 	CONMSG("  version      - Version information\n");
   651 }
   652 
   653 void ClientCommand_handler(edict_t *client)
   654 {
   655 	IVEngineServer *e = g_Engine.engine;
   656 	const char *cmd = e->Cmd_Argv(0);
   657 
   658 	if (strcmp(cmd, "meta") == 0)
   659 	{
   660 		int args = e->Cmd_Argc();
   661 		if (args == 2)
   662 		{
   663 			const char *subcmd = e->Cmd_Argv(1);
   664 
   665 			if (strcmp(subcmd, "credits") == 0)
   666 			{
   667 				CLIENT_CONMSG(client, "Metamod:Source was developed by:\n");
   668 				CLIENT_CONMSG(client, "  SourceHook: Pavol \"PM OnoTo\" Marko\n");
   669 				CLIENT_CONMSG(client, "  Core: David \"BAILOPAN\" Anderson\n");
   670 				CLIENT_CONMSG(client, "  Core: Scott \"Damaged Soul\" Ehlert\n");
   671 				CLIENT_CONMSG(client, "For more information, see the official website\n");
   672 				CLIENT_CONMSG(client, "http://www.metamodsource.net/\n");
   673 
   674 				RETURN_META(MRES_SUPERCEDE);
   675 			} else if(strcmp(subcmd, "version") == 0) {
   676 				CLIENT_CONMSG(client, "Metamod:Source version %s\n", MMS_FULL_VERSION);
   677 				CLIENT_CONMSG(client, "Compiled on: %s\n", SOURCEMM_DATE);
   678 				CLIENT_CONMSG(client, "Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
   679 				CLIENT_CONMSG(client, "SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
   680 				CLIENT_CONMSG(client, "http://www.metamodsource.net/\n");
   681 
   682 				RETURN_META(MRES_SUPERCEDE);
   683 			} else if(strcmp(subcmd, "list") == 0) {
   684 				SourceMM::CPluginManager::CPlugin *pl;
   685 				ISmmPlugin *plapi;
   686 				const char *plname;
   687 				PluginIter i;
   688 				char buffer[256];
   689 				int len = 0;
   690 				int plnum = 0;
   691 
   692 				for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++, len=0)
   693 				{
   694 					pl = (*i);
   695 					if (pl && pl->m_Status == Pl_Running)
   696 					{
   697 						plapi = pl->m_API;
   698 						if (!plapi || !plapi->QueryRunning(NULL, 0))
   699 						{
   700 							continue;
   701 						}
   702 						plnum++;
   703 
   704 						len += UTIL_Format(buffer, sizeof(buffer), "  [%02d]", plnum);
   705 
   706 						plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
   707 						len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
   708 
   709 						if (IS_STR_FILLED(plapi->GetVersion()))
   710 						{
   711 							len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
   712 						}
   713 						if (IS_STR_FILLED(plapi->GetAuthor()))
   714 						{
   715 							UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
   716 						}
   717 
   718 						CLIENT_CONMSG(client, "%s\n", buffer);
   719 					}
   720 				}
   721 
   722 				if (!plnum)
   723 				{
   724 					CLIENT_CONMSG(client, "No active plugins loaded.\n");
   725 				}
   726 
   727 				RETURN_META(MRES_SUPERCEDE);
   728 			}
   729 		}
   730 
   731 		CLIENT_CONMSG(client, "Metamod:Source Menu\n");
   732 		CLIENT_CONMSG(client, "usage: meta <command>\n");
   733 		CLIENT_CONMSG(client, "  credits - About Metamod:Source\n");
   734 		CLIENT_CONMSG(client, "  list    - List plugins\n");
   735 		CLIENT_CONMSG(client, "  version - Version information\n");
   736 
   737 		RETURN_META(MRES_SUPERCEDE);
   738 	}
   739 
   740 	RETURN_META(MRES_IGNORED);
   741 }
   742 
   743 void SMConVarAccessor::UnloadMetamodCommands()
   744 {
   745 	Unregister(&metamod_version);
   746 	Unregister(&mm_pluginsfile);
   747 	Unregister(&mm_basedir);
   748 	Unregister(&meta_command);
   749 }
   750 
   751 const char *GetPluginsFile()
   752 {
   753 	return mm_pluginsfile.GetString();
   754 }
   755 
   756 const char *GetMetamodBaseDir()
   757 {
   758 	return mm_basedir.GetString();
   759 }
   760 
   761 /* Signature for ICvar::GetCommands() in vstdlib for Win32 and Linux.
   762  *
   763  * 20226EE0 A1 50 5C 5A 20   mov         eax,dword ptr ds:[205A5C50h] <-- What we want
   764  * 20226EE5 C3               ret              
   765  */
   766 #define CMDLIST_SIG "\xA1\x2A\x2A\x2A\x2A\xC3"
   767 #define CMDLIST_SIGLEN 6
   768 
   769 /* Linux symbol name of ConCommandBase list in vstdlib */
   770 #define CMDLIST_SYMBOL "_ZN14ConCommandBase18s_pConCommandBasesE"
   771 
   772 /* This function retrieves the address of the var that holds the top of the ConCommandBase list.
   773  * Having this allows us to change the beginning of this list with ease.
   774  *
   775  * This craziness eliminates the need for the eternal command/cvar used previously which
   776  * could have caused a crash as a result of registering commands/cvars more than once.
   777  */
   778 bool SMConVarAccessor::InitConCommandBaseList()
   779 {
   780 	char *vfunc = UTIL_GetOrigFunction(&ICvar::GetCommands, g_Engine.icvar);
   781 
   782 	if (!vfunc)
   783 	{
   784 		return false;
   785 	}
   786 
   787 #ifdef OS_WIN32
   788 	if (UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN))
   789 	{
   790 		/* Skip past 0xA1 and get addr of ConCommandBase list var */
   791 		m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1);
   792 		return true;
   793 	}
   794 #elif defined OS_LINUX
   795 	/* Try dlsym first */
   796 	char path[PATH_SIZE];
   797 	if (GetFileOfAddress((void *)g_Engine.icvar, path, sizeof(path)))
   798 	{
   799 		void *handle = dlopen(path, RTLD_NOW);
   800 		if (handle)
   801 		{
   802 			m_TopConCommandBase = reinterpret_cast<ConCommandBase **>(dlsym(handle, CMDLIST_SYMBOL));
   803 			dlclose(handle);
   804 			return true;
   805 		}
   806 	}
   807 
   808 	/* If dlsym failed, then verify signature of function */
   809 	if (!m_TopConCommandBase && UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN))
   810 	{
   811 		/* Skip past 0xA1 and get addr of ConCommandBase list var */
   812 		m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1);
   813 		return true;
   814 	}
   815 #endif
   816 
   817 	return false;
   818 }