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