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