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