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