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