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