sourcemm/concommands.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Aug 10 23:43:16 2007 +0000 (2007-08-10)
changeset 346 d0868271663a
parent 345 600e30820ea7
permissions -rw-r--r--
Reformatted output of meta list command
     1 /* ======== SourceMM ========
     2  * Copyright (C) 2004-2007 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 "CSmmAPI.h"
    13 #include "concommands.h"
    14 #include "CPlugin.h"
    15 #include "sh_string.h"
    16 #include "sh_list.h"
    17 
    18 using namespace SourceMM;
    19 using namespace SourceHook;
    20 
    21 /**
    22  * @brief Console Command Implementations
    23  * @file concommands.cpp
    24  */
    25 
    26 CAlwaysRegisterableCommand g_EternalCommand;
    27 SMConVarAccessor g_SMConVarAccessor;
    28 
    29 bool SMConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand)
    30 {
    31 	// Add the FCVAR_GAMEDLL flag 
    32 	// => No crash on exit!
    33 	// UPDATE: Do _not_ add the FCVAR_GAMEDLL flag here, as it
    34 	// causes the command to be unusable on listenservers until you load a map
    35 	// We will set the FCVAR_GAMEDLL flag on all commands we have registered once we are being unloaded
    36 	//pCommand->AddFlags(FCVAR_GAMEDLL);
    37 	m_RegisteredCommands.push_back(pCommand);
    38 
    39 	pCommand->SetNext( NULL );
    40 	g_Engine.icvar->RegisterConCommandBase(pCommand);
    41 
    42 	return true;
    43 }
    44 
    45 bool SMConVarAccessor::Register(ConCommandBase *pCommand)
    46 {
    47 	//simple, don't mark as part of sourcemm!
    48 	pCommand->SetNext( NULL );
    49 	g_Engine.icvar->RegisterConCommandBase(pCommand);
    50 
    51 	return true;
    52 }
    53 
    54 void SMConVarAccessor::MarkCommandsAsGameDLL()
    55 {
    56 	for (List<ConCommandBase*>::iterator iter = m_RegisteredCommands.begin();
    57 		iter != m_RegisteredCommands.end(); ++iter)
    58 	{
    59 		(*iter)->AddFlags(FCVAR_GAMEDLL);
    60 	}
    61 }
    62 
    63 void SMConVarAccessor::Unregister(PluginId id, ConCommandBase *pCommand)
    64 {
    65 	/* Notify via IMetamodListener */
    66 	PluginIter iter;
    67 	CPluginManager::CPlugin *pPlugin;
    68 	List<IMetamodListener *>::iterator event;
    69 	IMetamodListener *pML;
    70 	for (iter=g_PluginMngr._begin(); iter!=g_PluginMngr._end(); iter++)
    71 	{
    72 		pPlugin = (*iter);
    73 		if (pPlugin->m_Status < Pl_Paused)
    74 		{
    75 			continue;
    76 		}
    77 		/* Only valid for plugins >= 12 (v1:6, SourceMM 1.5) */
    78 		if (pPlugin->m_API->GetApiVersion() < 12)
    79 		{
    80 			continue;
    81 		}
    82 		for (event=pPlugin->m_Events.begin();
    83 			event!=pPlugin->m_Events.end();
    84 			event++)
    85 		{
    86 			pML = (*event);
    87 			pML->OnUnlinkConCommandBase(id, pCommand);
    88 		}
    89 	}
    90 
    91 	ICvar *cv = g_Engine.icvar;
    92 	ConCommandBase *ptr = cv->GetCommands();
    93 
    94 	if (ptr == pCommand)
    95 	{
    96 		/* First in list */
    97 		g_EternalCommand.BringToFront();
    98 		g_EternalCommand.SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
    99 	} else {
   100 		/* Find us and unregister us */
   101 		ConCommandBase *pPrev = NULL;
   102 		while (ptr)
   103 		{
   104 			if (ptr == pCommand)
   105 				break;
   106 			pPrev = ptr;
   107 			ptr = const_cast<ConCommandBase *>(ptr->GetNext());
   108 		}
   109 		if (pPrev && ptr == pCommand)
   110 		{
   111 			pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
   112 		}
   113 	}
   114 }
   115 
   116 void SMConVarAccessor::UnregisterGameDLLCommands()
   117 {
   118 	ConCommandBase *begin = g_Engine.icvar->GetCommands();
   119 	ConCommandBase *iter = begin;
   120 	ConCommandBase *prev = NULL;
   121 	while (iter)
   122 	{
   123 		/* Watch out for the ETERNAL COMMAND! */
   124 		if (iter != &g_EternalCommand && iter->IsBitSet(FCVAR_GAMEDLL))
   125 		{
   126 			/* Remove it! */
   127 			if (iter == begin)
   128 			{
   129 				g_EternalCommand.BringToFront();
   130 				iter = const_cast<ConCommandBase*>(iter->GetNext());
   131 				g_EternalCommand.SetNext(iter);
   132 				prev = &g_EternalCommand;
   133 				continue;
   134 			}
   135 			else
   136 			{
   137 				iter = const_cast<ConCommandBase*>(iter->GetNext());
   138 				prev->SetNext(iter);
   139 				continue;
   140 			}
   141 		}
   142 		prev = iter;
   143 		iter = const_cast<ConCommandBase*>(iter->GetNext());
   144 	}
   145 }
   146 
   147 ConVar metamod_version("metamod_version", SOURCEMM_VERSION, FCVAR_REPLICATED | FCVAR_SPONLY | FCVAR_NOTIFY, "Metamod:Source Version");
   148 #if defined WIN32 || defined _WIN32
   149 ConVar mm_pluginsfile("mm_pluginsfile", "addons\\metamod\\metaplugins.ini", FCVAR_SPONLY, "Metamod:Source Plugins File");
   150 #else
   151 ConVar mm_pluginsfile("mm_pluginsfile", "addons/metamod/metaplugins.ini", FCVAR_SPONLY, "Metamod:Source Plugins File");
   152 #endif
   153 
   154 CON_COMMAND(meta, "Metamod:Source Menu")
   155 {
   156 	IVEngineServer *e = g_Engine.engine;
   157 
   158 	int args = e->Cmd_Argc();
   159 
   160 	if (args >= 2)
   161 	{
   162 		const char *command = e->Cmd_Argv(1);
   163 		if (strcmp(command, "credits") == 0)
   164 		{
   165 			CONMSG("Metamod:Source was developed by:\n");
   166 			CONMSG("  SourceHook: Pavol \"PM OnoTo\" Marko\n");
   167 			CONMSG("  GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
   168 			CONMSG("  GameDLL: Scott \"Damaged Soul\" Ehlert\n");
   169 			CONMSG("For more information, see the official website\n");
   170 			CONMSG("http://www.sourcemm.net/\n");
   171 			
   172 			return;
   173 		} else if (strcmp(command, "version") == 0) {
   174 			CONMSG("Metamod:Source version %s\n", SOURCEMM_VERSION);
   175 			CONMSG("Compiled on: %s\n", SOURCEMM_DATE);
   176 			CONMSG("Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
   177 			CONMSG("SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
   178 			CONMSG("http://www.sourcemm.net/\n");
   179 
   180 			return;
   181 		} else if (strcmp(command, "game") == 0) {
   182 			CONMSG("GameDLL Information\n");
   183 			CONMSG("  Description: %s\n", g_GameDll.pGameDLL->GetGameDescription());
   184 			CONMSG("  Mod Path: %s\n", g_ModPath.c_str());
   185 			CONMSG("  DLL Path: %s\n", g_BinPath.c_str());
   186 			CONMSG("  Interface: ServerGameDLL%03d, ServerGameClients%03d\n", g_GameDllVersion, g_GameClientsVersion);
   187 
   188 			// Display user messages
   189 			if (g_SmmAPI.MsgCacheSuccessful())
   190 			{
   191 				const char *msgname;
   192 				int msgsize;
   193 				int msgcount = g_SmmAPI.GetUserMessageCount();
   194 
   195 				if (msgcount > 0)
   196 				{
   197 					CONMSG("  User Messages:  %-32.31s  %-5s  %-5s\n", "Name", "Index", "Size");
   198 
   199 					for (int i = 0; i < msgcount; i++)
   200 					{
   201 						msgname = g_SmmAPI.GetUserMessage(i, &msgsize);
   202 
   203 						CONMSG("                  %-32.31s  %-5d  %-5d\n", msgname, i, msgsize); 
   204 					}
   205 
   206 					CONMSG("  %d user message%s in total\n", msgcount, (msgcount > 1) ? "s" : "");
   207 				} else {
   208 					CONMSG("  User Messages: None\n");
   209 				}
   210 			} else {
   211 				CONMSG("  User Messages: Failed to get list of user messages\n");
   212 			}
   213 
   214 			return;
   215 		} else if (strcmp(command, "refresh") == 0) {
   216 			char full_path[255];
   217 			g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s", g_ModPath.c_str(), GetPluginsFile());
   218 
   219 			LoadPluginsFromFile(full_path);
   220 
   221 			return;
   222 		} else if (strcmp(command, "list") == 0) {
   223 			CPluginManager::CPlugin *pl;
   224 			ISmmPlugin *plapi;
   225 			const char *plname;
   226 			PluginIter i;
   227 			char buffer[256];
   228 			int len;
   229 			int plnum = g_PluginMngr.GetPluginCount();
   230 
   231 			if (!plnum)
   232 			{
   233 				CONMSG("No plugins loaded.\n");
   234 				return;
   235 			} else {
   236 				CONMSG("Listing %d plugin%s:\n", plnum, (plnum > 1) ? "s" : "");
   237 			}
   238 
   239 			for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++)
   240 			{
   241 				pl = (*i);
   242 				if (!pl)
   243 				{
   244 					break;
   245 				}
   246 
   247 				len = 0;
   248 
   249 				if (pl->m_Status != Pl_Running)
   250 				{
   251 					len += UTIL_Format(buffer, sizeof(buffer), "  [%02d] <%s>", pl->m_Id, g_PluginMngr.GetStatusText(pl));
   252 				} else {
   253 					len += UTIL_Format(buffer, sizeof(buffer), "  [%02d]", pl->m_Id);
   254 				}
   255 
   256 				if ((plapi = pl->m_API))
   257 				{
   258 					plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
   259 					len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
   260 
   261 					if (IS_STR_FILLED(plapi->GetVersion()))
   262 					{
   263 						len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
   264 					}
   265 					if (IS_STR_FILLED(plapi->GetAuthor()))
   266 					{
   267 						UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
   268 					}
   269 				}
   270 
   271 				CONMSG("%s\n", buffer);
   272 			}
   273 
   274 			return;
   275 		} else if (strcmp(command, "cmds") == 0) {
   276 			if (args >= 3)
   277 			{
   278 				int id = atoi(e->Cmd_Argv(2));
   279 				CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   280 
   281 				if (!pl)
   282 				{
   283 					CONMSG("Plugin %d not found.\n", id);
   284 					return;
   285 				}
   286 
   287 				if (!pl->m_API)
   288 				{
   289 					CONMSG("Plugin %d is not loaded.\n", id);
   290 				} else {
   291 					CONMSG("Console commands for %s:\n", pl->m_API->GetName());
   292 					List<ConCommandBase *>::iterator ci;
   293 					size_t count = 0;
   294 
   295 					for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
   296 					{
   297 						count++;
   298 						CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
   299 					}
   300 				}
   301 			} else {
   302 				CONMSG("Usage: meta cmds <id>\n");
   303 			}
   304 
   305 			return;
   306 		} else if (strcmp(command, "cvars") == 0) {
   307 			if (args >= 3)
   308 			{
   309 				int id = atoi(e->Cmd_Argv(2));
   310 				CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   311 
   312 				if (!pl)
   313 				{
   314 					CONMSG("Plugin %d not found.\n", id);
   315 					return;
   316 				}
   317 
   318 				if (!pl->m_API)
   319 				{
   320 					CONMSG("Plugin %d is not loaded.\n", id);
   321 				} else {
   322 					CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
   323 					List<ConCommandBase *>::iterator ci;
   324 					size_t count = 0;
   325 
   326 					for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
   327 					{
   328 						count++;
   329 						CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
   330 					}
   331 				}
   332 			} else {
   333 				CONMSG("Usage: meta cvars <id>\n");
   334 			}
   335 
   336 			return;
   337 		} else if (strcmp(command, "info") == 0) {
   338 			if (args >= 3)
   339 			{
   340 				int id = atoi(e->Cmd_Argv(2));
   341 				CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   342 				if (!pl)
   343 				{
   344 					CONMSG("Plugin %d not found.\n", id);
   345 					return;
   346 				}
   347 
   348 				if (!pl->m_API)
   349 				{
   350 					CONMSG("Plugin %d is not loaded.\n", id);
   351 				} else {
   352 					if (pl->m_Status == Pl_Paused)
   353 					{
   354 						CONMSG("Plugin %d is paused.\n", id);
   355 					} else if (pl->m_Status == Pl_Running) {
   356 						char run_msg[255];
   357 						bool run = false;
   358 						if (pl->m_API && pl->m_API->QueryRunning(run_msg, sizeof(run_msg)-1))
   359 							run = true;
   360 						if (run)
   361 						{
   362 							CONMSG("Plugin %d is running.\n", id);
   363 						} else {
   364 							CONMSG("Plugin %d is stopped: %s\n", id, run_msg);
   365 						}
   366 					}
   367 					CONMSG("  Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
   368 					CONMSG("  Version: %s\n", pl->m_API->GetVersion());
   369 					CONMSG("  Description: %s\n", pl->m_API->GetDescription());
   370 					CONMSG("  License: %s\n", pl->m_API->GetLicense());
   371 					CONMSG("  URL: %s\n", pl->m_API->GetURL());
   372 					CONMSG("  Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
   373 				}
   374 				CONMSG("File: %s\n\n", pl->m_File.c_str());
   375 
   376 				return;
   377 			} else {
   378 				CONMSG("Usage: meta info <id>\n");
   379 
   380 				return;
   381 			}
   382 		} else if (strcmp(command, "pause") == 0) {
   383 			if (args >= 3)
   384 			{
   385 				int id = atoi(e->Cmd_Argv(2));
   386 
   387 				char error[255];
   388 
   389 				if (!g_PluginMngr.Pause(id, error, sizeof(error)))
   390 				{
   391 					CONMSG("Pause failed: %s\n", error);
   392 					return;
   393 				}
   394 				
   395 				CONMSG("Plugin %d has been paused.\n", id);
   396 
   397 				return;
   398 			} else {
   399 				CONMSG("Usage: meta pause <id>\n");
   400 
   401 				return;
   402 			}
   403 		} else if (strcmp(command, "unpause") == 0) {
   404 			if (args >= 3)
   405 			{
   406 				int id = atoi(e->Cmd_Argv(2));
   407 				char error[255];
   408 
   409 				if (!g_PluginMngr.Unpause(id, error, sizeof(error)))
   410 				{
   411 					CONMSG("Unpause failed: %s\n", error);
   412 					return;
   413 				}
   414 
   415 				CONMSG("Plugin %d has been unpaused.\n", id);
   416 
   417 				return;
   418 			} else {
   419 				CONMSG("Usage: meta unpause <id>\n");
   420 
   421 				return;
   422 			}
   423 		} else if (strcmp(command, "load") == 0) {
   424 			if (args >= 3)
   425 			{
   426 				const char *file = e->Cmd_Argv(2);
   427 				char full_path[255];
   428 
   429 				const char *alias = g_PluginMngr.LookupAlias(file);
   430 				if (alias)
   431 				{
   432 					file = alias;
   433 				}
   434 
   435 				if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
   436 				{
   437 					g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s", file);
   438 				} else {
   439 					const char *ext = UTIL_GetExtension(file);
   440 #if defined WIN32 || defined _WIN32
   441 					ext = ext ? "" : ".dll";
   442 #else
   443 					ext = ext ? "" : "_i486.so";
   444 #endif
   445 					g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s%s", g_ModPath.c_str(), file, ext);
   446 				}
   447 
   448 				char error[255]={0};
   449 				bool already;
   450 				CPluginManager::CPlugin *pl;
   451 
   452 				PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error));
   453 				pl = g_PluginMngr.FindById(id);
   454 				if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
   455 				{
   456 					CONMSG("Failed to load plugin %s (%s).\n", file, error);
   457 					return;
   458 				}
   459 
   460 				if (!already)
   461 				{
   462 					CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
   463 				} else {
   464 					CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
   465 				}
   466 				
   467 				return;
   468 			} else {
   469 				CONMSG("Usage: meta load <path>\n");
   470 
   471 				return;
   472 			}
   473 		} else if ( (strcmp(command, "alias") == 0) ||
   474 					(strcmp(command, "aliases") == 0) ) {
   475 			if (args >= 4)
   476 			{
   477 				const char *alias = e->Cmd_Argv(2);
   478 				const char *value = e->Cmd_Argv(3);
   479 
   480 				g_PluginMngr.SetAlias(alias, value);
   481 				if (value[0] == '\0')
   482 				{
   483 					CONMSG("Deleted alias: %s.\n", alias);
   484 				} else {
   485 					CONMSG("Set alias \"%s\" to: %s\n", alias, value);
   486 				}
   487 			} else if (args >= 3) {
   488 				const char *alias = e->Cmd_Argv(2);
   489 				const char *value = g_PluginMngr.LookupAlias(alias);
   490 				if (value)
   491 				{
   492 					CONMSG("Alias \"%s\" is set to: %s\n", alias, value);
   493 				} else {
   494 					CONMSG("Alias \"%s\" was not found.\n", alias);
   495 				}
   496 			} else {
   497 				List<CNameAlias *>::iterator iter, end;
   498 				CNameAlias *p;
   499 
   500 				iter = g_PluginMngr._alias_begin();
   501 				end = g_PluginMngr._alias_end();
   502 				size_t total = 0;
   503 				if (iter != end)
   504 				{
   505 					CONMSG("%-10.9s %s\n", "Alias", "File");
   506 					CONMSG(" --- \n");
   507 					for (; iter!=end; iter++)
   508 					{
   509 						p = (*iter);
   510 						CONMSG("%-10.9s %s\n", p->alias.c_str(), p->value.c_str());
   511 						total++;
   512 					}
   513 					CONMSG(" --- \n");
   514 					CONMSG("%d aliases total.\n", total);
   515 				} else {
   516 					CONMSG("No aliases found.\n");
   517 				}
   518 			}
   519 			return;
   520 		} else if (strcmp(command, "unload") == 0) {
   521 			if (args >= 3)
   522 			{
   523 				const char *file = e->Cmd_Argv(2);
   524 				int id = atoi(file);
   525 				char error[255]={0};
   526 
   527 				if (id == 0 && isalpha(file[0]))
   528 				{
   529 					char full_path[255];
   530 					const char *alias = g_PluginMngr.LookupAlias(file);
   531 
   532 					if (alias)
   533 					{
   534 						file = alias;
   535 					}
   536 
   537 					/* first check if it's a known filename */
   538 					if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
   539 					{
   540 						g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s", file);
   541 					} else {
   542 						const char *ext = UTIL_GetExtension(file);
   543 #if defined WIN32 || defined _WIN32
   544 						ext = ext ? "" : ".dll";
   545 #else
   546 						ext = ext ? "" : "_i486.so";
   547 #endif
   548 						g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s%s", g_ModPath.c_str(), file, ext);
   549 					}
   550 
   551 					List<CPluginManager::CPlugin *>::iterator iter, end;
   552 					CPluginManager::CPlugin *pl;
   553 					iter = g_PluginMngr._begin();
   554 					end = g_PluginMngr._end();
   555 					for (; iter!=end; iter++)
   556 					{
   557 						pl = (*iter);
   558 						if (strcmp(pl->m_File.c_str(), full_path) == 0)
   559 						{
   560 							id = pl->m_Id;
   561 							break;
   562 						}
   563 					}
   564 					if (id == 0)
   565 					{
   566 						CONMSG("Plugin \"%s\" not found.\n", full_path);
   567 						return;
   568 					}
   569 				}
   570 
   571 				if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
   572 				{
   573 					CONMSG("Unload failed: %s\n", error);
   574 					return;
   575 				}
   576 
   577 				CONMSG("Plugin %d unloaded.\n", id);
   578 			} else {
   579 				CONMSG("Usage: meta unload <id>\n");
   580 			}
   581 			return;
   582 		} else if (strcmp(command, "force_unload") == 0) {
   583 			if (args >= 3)
   584 			{
   585 				int id = atoi(e->Cmd_Argv(2));
   586 				char error[255]={0};
   587 
   588 				if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
   589 				{
   590 					CONMSG("Force unload failed: %s\n", error);
   591 					return;
   592 				}
   593 
   594 				CONMSG("Plugin %d force unloaded.\n", id);
   595 
   596 				return;
   597 			} else {
   598 				CONMSG("Usage: meta force_unload <id>\n");
   599 
   600 				return;
   601 			}
   602 		} else if (strcmp(command, "clear") == 0) {
   603 			if (!g_PluginMngr.UnloadAll())
   604 			{
   605 				CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
   606 				return;
   607 			} 
   608 
   609 			CONMSG("All plugins unloaded.\n");
   610 
   611 			return;
   612 		} else if (strcmp(command, "retry") == 0) {
   613 			if (args >= 3)
   614 			{
   615 				int id = atoi(e->Cmd_Argv(2));
   616 				char error[255];
   617 
   618 				if (!g_PluginMngr.Retry(id, error, sizeof(error)))
   619 				{
   620 					CONMSG("Error reloading plugin: %s\n", error);
   621 					return;
   622 				}
   623 
   624 				CONMSG("Plugin %d successfully reloaded.\n", id);
   625 
   626 				return;
   627 			} else {
   628 				CONMSG("Usage: meta retry <id>\n");
   629 
   630 				return;
   631 			}
   632 		}
   633 	}
   634 
   635 	CONMSG("Metamod:Source Menu\n");
   636 	CONMSG("usage: meta <command> [arguments]\n");
   637 	CONMSG("  alias        - List or set an alias\n");
   638 	CONMSG("  clear        - Unload all plugins forcefully\n");
   639 	CONMSG("  cmds         - Show plugin commands\n");
   640 	CONMSG("  cvars        - Show plugin cvars\n");
   641 	CONMSG("  credits      - About Metamod:Source\n");
   642 	CONMSG("  force_unload - Forcefully unload a plugin\n");
   643 	CONMSG("  game         - Information about GameDLL\n");
   644 	CONMSG("  info         - Information about a plugin\n");
   645 	CONMSG("  list         - List plugins\n");
   646 	CONMSG("  load         - Load a plugin\n");
   647 	CONMSG("  pause        - Pause a running plugin\n");
   648 	CONMSG("  refresh      - Reparse plugins file\n");
   649 	CONMSG("  retry        - Attempt to reload a plugin\n");
   650 	CONMSG("  unload       - Unload a loaded plugin\n");
   651 	CONMSG("  unpause      - Unpause a paused plugin\n");
   652 	CONMSG("  version      - Version information\n");
   653 }
   654 
   655 CAlwaysRegisterableCommand::CAlwaysRegisterableCommand()
   656 {
   657 	Create("", NULL, FCVAR_UNREGISTERED|FCVAR_GAMEDLL);
   658 	m_pICvar = NULL;
   659 }
   660 
   661 bool CAlwaysRegisterableCommand::IsRegistered( void ) const
   662 {
   663 	return false;
   664 }
   665 
   666 void CAlwaysRegisterableCommand::BringToFront()
   667 {
   668 	if (!m_pICvar)
   669 		m_pICvar = g_Engine.icvar;
   670 
   671 	// First, let's try to find us!
   672 	ConCommandBase *pPtr = m_pICvar->GetCommands();
   673 
   674 	if (pPtr == this)
   675 	{
   676 		// We are already at the beginning; Nothing to do
   677 		return;
   678 	}
   679 
   680 	while (pPtr)
   681 	{
   682 		if (pPtr == this && pPtr->IsCommand() && stricmp(GetName(), pPtr->GetName()) == 0)
   683 			break;
   684 		ConCommandBase *pPrev = NULL;
   685 		while (pPtr)
   686 		{
   687 			if (pPtr == this)
   688 				break;
   689 			pPrev = pPtr;
   690 			pPtr = const_cast<ConCommandBase*>(pPtr->GetNext());
   691 		}
   692 		if (pPrev && pPtr == this)
   693 		{
   694 			pPrev->SetNext(m_pNext);		// Remove us from the list
   695 		}
   696 		// Now, register us
   697 		SetNext(NULL);
   698 		m_pICvar->RegisterConCommandBase(this);
   699 	}
   700 }
   701 
   702 void ClientCommand_handler(edict_t *client)
   703 {
   704 	IVEngineServer *e = g_Engine.engine;
   705 	const char *cmd = e->Cmd_Argv(0);
   706 
   707 	if (strcmp(cmd, "meta") == 0)
   708 	{
   709 		int args = e->Cmd_Argc();
   710 		if (args == 2)
   711 		{
   712 			const char *subcmd = e->Cmd_Argv(1);
   713 
   714 			if (strcmp(subcmd, "credits") == 0)
   715 			{
   716 				CLIENT_CONMSG(client, "Metamod:Source was developed by:\n");
   717 				CLIENT_CONMSG(client, "  SourceHook: Pavol \"PM OnoTo\" Marko\n");
   718 				CLIENT_CONMSG(client, "  GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
   719 				CLIENT_CONMSG(client, "  GameDLL: Scott \"Damaged Soul\" Ehlert\n");
   720 				CLIENT_CONMSG(client, "For more information, see the official website\n");
   721 				CLIENT_CONMSG(client, "http://www.sourcemm.net/\n");
   722 
   723 				RETURN_META(MRES_SUPERCEDE);
   724 			} else if(strcmp(subcmd, "version") == 0) {
   725 				CLIENT_CONMSG(client, "Metamod:Source version %s\n", SOURCEMM_VERSION);
   726 				CLIENT_CONMSG(client, "Compiled on: %s\n", SOURCEMM_DATE);
   727 				CLIENT_CONMSG(client, "Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
   728 				CLIENT_CONMSG(client, "SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
   729 				CLIENT_CONMSG(client, "http://www.sourcemm.net/\n");
   730 
   731 				RETURN_META(MRES_SUPERCEDE);
   732 			} else if(strcmp(subcmd, "list") == 0) {
   733 				CPluginManager::CPlugin *pl;
   734 				ISmmPlugin *plapi;
   735 				const char *plname;
   736 				PluginIter i;
   737 				char buffer[256];
   738 				int len = 0;
   739 				int plnum = 0;
   740 
   741 				for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++, len=0)
   742 				{
   743 					pl = (*i);
   744 					if (pl && pl->m_Status == Pl_Running)
   745 					{
   746 						plapi = pl->m_API;
   747 						if (!plapi || !plapi->QueryRunning(NULL, 0))
   748 						{
   749 							continue;
   750 						}
   751 						plnum++;
   752 
   753 						len += UTIL_Format(buffer, sizeof(buffer), "  [%02d]", plnum);
   754 
   755 						plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
   756 						len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
   757 
   758 						if (IS_STR_FILLED(plapi->GetVersion()))
   759 						{
   760 							len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
   761 						}
   762 						if (IS_STR_FILLED(plapi->GetAuthor()))
   763 						{
   764 							UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
   765 						}
   766 
   767 						CLIENT_CONMSG(client, "%s\n", buffer);
   768 					}
   769 				}
   770 
   771 				if (!plnum)
   772 				{
   773 					CLIENT_CONMSG(client, "No active plugins loaded.\n");
   774 				}
   775 
   776 				RETURN_META(MRES_SUPERCEDE);
   777 			}
   778 		}
   779 
   780 		CLIENT_CONMSG(client, "Metamod:Source Menu\n");
   781 		CLIENT_CONMSG(client, "usage: meta <command>\n");
   782 		CLIENT_CONMSG(client, "  credits - About Metamod:Source\n");
   783 		CLIENT_CONMSG(client, "  list    - List plugins\n");
   784 		CLIENT_CONMSG(client, "  version - Version information\n");
   785 
   786 		RETURN_META(MRES_SUPERCEDE);
   787 	}
   788 
   789 	RETURN_META(MRES_IGNORED);
   790 }
   791 
   792 const char *GetPluginsFile()
   793 {
   794 	return mm_pluginsfile.GetString();
   795 }