sourcemm/concommands.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Apr 21 02:30:02 2006 +0000 (2006-04-21)
changeset 191 2d0dbd03c503
parent 190 718f99cad77d
child 192 47649a1d49e3
permissions -rw-r--r--
Mm, oopsy. ICvar::GetCommandLineValue could return NULL for the plugins file. That would be bad. :(
(Now fixed with a call to GetPluginsFile in case it is NULL)
     1 /* ======== SourceMM ========
     2 * Copyright (C) 2004-2006 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 /**
    19  * @brief Console Command Implementations
    20  * @file concommands.cpp
    21  */
    22 
    23 CAlwaysRegisterableCommand g_EternalCommand;
    24 SMConVarAccessor g_SMConVarAccessor;
    25 
    26 bool SMConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand)
    27 {
    28 	// Add the FCVAR_GAMEDLL flag 
    29 	// => No crash on exit!
    30 	// UPDATE: Do _not_ add the FCVAR_GAMEDLL flag here, as it
    31 	// causes the command to be unusable on listenservers until you load a map
    32 	// We will set the FCVAR_GAMEDLL flag on all commands we have registered once we are being unloaded
    33 	//pCommand->AddFlags(FCVAR_GAMEDLL);
    34 	m_RegisteredCommands.push_back(pCommand);
    35 
    36 	pCommand->SetNext( NULL );
    37 	g_Engine.icvar->RegisterConCommandBase(pCommand);
    38 
    39 	return true;
    40 }
    41 
    42 bool SMConVarAccessor::Register(ConCommandBase *pCommand)
    43 {
    44 	//simple, don't mark as part of sourcemm!
    45 	pCommand->SetNext( NULL );
    46 	g_Engine.icvar->RegisterConCommandBase(pCommand);
    47 
    48 	return true;
    49 }
    50 
    51 void SMConVarAccessor::MarkCommandsAsGameDLL()
    52 {
    53 	for (SourceHook::List<ConCommandBase*>::iterator iter = m_RegisteredCommands.begin();
    54 		iter != m_RegisteredCommands.end(); ++iter)
    55 	{
    56 		(*iter)->AddFlags(FCVAR_GAMEDLL);
    57 	}
    58 }
    59 
    60 void SMConVarAccessor::Unregister(ConCommandBase *pCommand)
    61 {
    62 	ICvar *cv = g_Engine.icvar;
    63 	ConCommandBase *ptr = cv->GetCommands();
    64 
    65 	if (ptr == pCommand)
    66 	{
    67 		//first in list
    68 		g_EternalCommand.BringToFront();
    69 		g_EternalCommand.SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
    70 	} else {
    71 		//find us and unregister us
    72 		ConCommandBase *pPrev = NULL;
    73 		while (ptr)
    74 		{
    75 			if (ptr == pCommand)
    76 				break;
    77 			pPrev = ptr;
    78 			ptr = const_cast<ConCommandBase *>(ptr->GetNext());
    79 		}
    80 		if (pPrev && ptr == pCommand)
    81 		{
    82 			pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
    83 		}
    84 	}
    85 }
    86 
    87 void SMConVarAccessor::UnregisterGameDLLCommands()
    88 {
    89 	ConCommandBase *begin = g_Engine.icvar->GetCommands();
    90 	ConCommandBase *iter = begin;
    91 	ConCommandBase *prev = NULL;
    92 	while (iter)
    93 	{
    94 		// watch out for the ETERNAL COMMAND!
    95 		if (iter != &g_EternalCommand && iter->IsBitSet(FCVAR_GAMEDLL))
    96 		{
    97 			// Remove it!
    98 			if (iter == begin)
    99 			{
   100 				g_EternalCommand.BringToFront();
   101 				iter = const_cast<ConCommandBase*>(iter->GetNext());
   102 				g_EternalCommand.SetNext(iter);
   103 				prev = &g_EternalCommand;
   104 				continue;
   105 			}
   106 			else
   107 			{
   108 				iter = const_cast<ConCommandBase*>(iter->GetNext());
   109 				prev->SetNext(iter);
   110 				continue;
   111 			}
   112 		}
   113 		prev = iter;
   114 		iter = const_cast<ConCommandBase*>(iter->GetNext());
   115 	}
   116 }
   117 
   118 ConVar metamod_version("metamod_version", SOURCEMM_VERSION, FCVAR_REPLICATED | FCVAR_SPONLY | FCVAR_NOTIFY, "Metamod:Source Version");
   119 #if defined WIN32 || defined _WIN32
   120 ConVar mm_pluginsfile("mm_pluginsfile", "addons\\metamod\\metaplugins.ini", FCVAR_SPONLY, "Metamod:Source Plugins File");
   121 #else
   122 ConVar mm_pluginsfile("mm_pluginsfile", "addons/metamod/metaplugins.ini", FCVAR_SPONLY, "Metamod:Source Plugins File");
   123 #endif
   124 
   125 CON_COMMAND(meta, "Metamod:Source Menu")
   126 {
   127 	IVEngineServer *e = g_Engine.engine;
   128 
   129 	int args = e->Cmd_Argc();
   130 
   131 	if (args >= 2)
   132 	{
   133 		const char *command = e->Cmd_Argv(1);
   134 		if (strcmp(command, "credits") == 0)
   135 		{
   136 			CONMSG("Metamod:Source was developed by:\n");
   137 			CONMSG("  SourceHook: Pavol \"PM OnoTo\" Marko\n");
   138 			CONMSG("  GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
   139 			CONMSG("  GameDLL: Scott \"Damaged Soul\" Ehlert\n");
   140 			CONMSG("For more information, see the official website\n");
   141 			CONMSG("http://www.sourcemm.net/\n");
   142 			
   143 			return;
   144 		} else if (strcmp(command, "version") == 0) {
   145 			CONMSG("Metamod:Source version %s\n", SOURCEMM_VERSION);
   146 			CONMSG("Compiled on: %s\n", SOURCEMM_DATE);
   147 			CONMSG("Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
   148 			CONMSG("SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
   149 			CONMSG("http://www.sourcemm.net/\n");
   150 
   151 			return;
   152 		} else if (strcmp(command, "game") == 0) {
   153 			CONMSG("GameDLL Information\n");
   154 			CONMSG("  Mod path: %s\n", g_ModPath.c_str());
   155 			CONMSG("  Dll path: %s\n", g_BinPath.c_str());
   156 
   157 			return;
   158 		} else if (strcmp(command, "refresh") == 0) {
   159 			char full_path[255];
   160 			g_SmmAPI.PathFormat(full_path, sizeof(full_path) - 1, "%s/%s", g_ModPath.c_str(), GetPluginsFile());
   161 
   162 			LoadPluginsFromFile(full_path);
   163 
   164 			return;
   165 		} else if (strcmp(command, "list") == 0) {
   166 			SourceMM::CPluginManager::CPlugin *pl;
   167 			PluginIter i;
   168 			const char *status="";
   169 			const char *version=NULL;
   170 			const char *name=NULL;
   171 			const char *author=NULL;
   172 
   173 			CONMSG("-Id- %-20.19s  %-10.9s  %-12.11s %-8.7s\n", "Name", "Version", "Author", "Status");
   174 			for (i=g_PluginMngr._begin(); i!=g_PluginMngr._end(); i++)
   175 			{
   176 				pl = (*i);
   177 				if (!pl)
   178 					break;
   179 				if (pl->m_Status == Pl_Paused)
   180 				{
   181 					status = "PAUSE";
   182 				} else if (pl->m_Status == Pl_Running) {
   183 					if (pl->m_API && pl->m_API->QueryRunning(NULL, 0))
   184 						status = "RUN";
   185 					else
   186 						status = "STOPPED";
   187 				} else if (pl->m_Status == Pl_Refused) {
   188 					status = "FAIL";
   189 				} else if (pl->m_Status == Pl_Error) {
   190 					status = "ERROR";
   191 				} else if (pl->m_Status == Pl_NotFound) {
   192 					status = "NOFILE";
   193 				}
   194 
   195 				if (pl->m_API)
   196 				{
   197 					version = pl->m_API->GetVersion();
   198 					author = pl->m_API->GetAuthor();
   199 					name = pl->m_API->GetName();
   200 				} else {
   201 					version = "-";
   202 					author = "-";
   203 					name = "-";
   204 				}
   205 
   206 				if (!version)
   207 					version = "-";
   208 				if (!author)
   209 					author = "-";
   210 				if (!name)
   211 					name = pl->m_File.c_str();
   212 
   213 
   214 				CONMSG("[%02d] %-20.19s  %-10.9s  %-12.11s %-8.7s\n", pl->m_Id, name, version, author, status);
   215 			}
   216 
   217 			//CONMSG("\n");
   218 
   219 			return;
   220 		} else if (strcmp(command, "cmds") == 0) {
   221 			if (args >= 3)
   222 			{
   223 				int id = atoi(e->Cmd_Argv(2));
   224 				SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   225 
   226 				if (!pl)
   227 				{
   228 					CONMSG("Plugin %d not found.\n", id);
   229 					return;
   230 				}
   231 
   232 				if (!pl->m_API)
   233 				{
   234 					CONMSG("Plugin %d is not loaded.\n", id);
   235 				} else {
   236 					CONMSG("Console commands for %s:\n", pl->m_API->GetName());
   237 					SourceHook::List<ConCommandBase *>::iterator ci;
   238 					size_t count = 0;
   239 
   240 					for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
   241 					{
   242 						count++;
   243 						CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
   244 					}
   245 				}
   246 			} else {
   247 				CONMSG("Usage: meta cmds <id>\n");
   248 			}
   249 
   250 			return;
   251 		} else if (strcmp(command, "cvars") == 0) {
   252 			if (args >= 3)
   253 			{
   254 				int id = atoi(e->Cmd_Argv(2));
   255 				SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   256 
   257 				if (!pl)
   258 				{
   259 					CONMSG("Plugin %d not found.\n", id);
   260 					return;
   261 				}
   262 
   263 				if (!pl->m_API)
   264 				{
   265 					CONMSG("Plugin %d is not loaded.\n", id);
   266 				} else {
   267 					CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
   268 					SourceHook::List<ConCommandBase *>::iterator ci;
   269 					size_t count = 0;
   270 
   271 					for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
   272 					{
   273 						count++;
   274 						CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
   275 					}
   276 				}
   277 			} else {
   278 				CONMSG("Usage: meta cvars <id>\n");
   279 			}
   280 
   281 			return;
   282 		} else if (strcmp(command, "info") == 0) {
   283 			if (args >= 3)
   284 			{
   285 				int id = atoi(e->Cmd_Argv(2));
   286 				SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   287 				if (!pl)
   288 				{
   289 					CONMSG("Plugin %d not found.\n", id);
   290 					return;
   291 				}
   292 
   293 				if (!pl->m_API)
   294 				{
   295 					CONMSG("Plugin %d is not loaded.\n", id);
   296 				} else {
   297 					if (pl->m_Status == Pl_Paused)
   298 					{
   299 						CONMSG("Plugin %d is paused.\n", id);
   300 					} else if (pl->m_Status == Pl_Running) {
   301 						char run_msg[255];
   302 						bool run = false;
   303 						if (pl->m_API && pl->m_API->QueryRunning(run_msg, sizeof(run_msg)-1))
   304 							run = true;
   305 						if (run)
   306 						{
   307 							CONMSG("Plugin %d is running.\n", id);
   308 						} else {
   309 							CONMSG("Plugin %d is stopped: %s\n", id, run_msg);
   310 						}
   311 					}
   312 					CONMSG("  Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
   313 					CONMSG("  Version: %s\n", pl->m_API->GetVersion());
   314 					CONMSG("  Description: %s\n", pl->m_API->GetDescription());
   315 					CONMSG("  License: %s\n", pl->m_API->GetLicense());
   316 					CONMSG("  URL: %s\n", pl->m_API->GetURL());
   317 					CONMSG("  Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
   318 				}
   319 				CONMSG("File: %s\n\n", pl->m_File.c_str());
   320 
   321 				return;
   322 			} else {
   323 				CONMSG("Usage: meta info <id>\n");
   324 
   325 				return;
   326 			}
   327 		} else if (strcmp(command, "pause") == 0) {
   328 			if (args >= 3)
   329 			{
   330 				int id = atoi(e->Cmd_Argv(2));
   331 
   332 				char error[255];
   333 
   334 				if (!g_PluginMngr.Pause(id, error, sizeof(error)-1))
   335 				{
   336 					CONMSG("Pause failed: %s\n", error);
   337 					return;
   338 				}
   339 				
   340 				CONMSG("Plugin %d has been paused.\n", id);
   341 
   342 				return;
   343 			} else {
   344 				CONMSG("Usage: meta pause <id>\n");
   345 
   346 				return;
   347 			}
   348 		} else if (strcmp(command, "unpause") == 0) {
   349 			if (args >= 3)
   350 			{
   351 				int id = atoi(e->Cmd_Argv(2));
   352 				char error[255];
   353 
   354 				if (!g_PluginMngr.Unpause(id, error, sizeof(error)-1))
   355 				{
   356 					CONMSG("Unpause failed: %s\n", error);
   357 					return;
   358 				}
   359 
   360 				CONMSG("Plugin %d has been unpaused.\n", id);
   361 
   362 				return;
   363 			} else {
   364 				CONMSG("Usage: meta unpause <id>\n");
   365 
   366 				return;
   367 			}
   368 		} else if (strcmp(command, "load") == 0) {
   369 			if (args >= 3)
   370 			{
   371 				const char *file = e->Cmd_Argv(2);
   372 				char full_path[255];
   373 
   374 				if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
   375 				{
   376 					g_SmmAPI.PathFormat(full_path, sizeof(full_path)-1, "%s", file);
   377 					snprintf(full_path, sizeof(full_path)-1, "%s", file);
   378 				} else {
   379 					const char *ext = UTIL_GetExtension(file);
   380 #if defined WIN32 || defined _WIN32
   381 					ext = ext ? "" : ".dll";
   382 #else
   383 					ext = ext ? "" : "_i486.so";
   384 #endif
   385 					g_SmmAPI.PathFormat(full_path, sizeof(full_path)-1, "%s/%s%s", g_ModPath.c_str(), file, ext);
   386 				}
   387 
   388 				char error[255]={0};
   389 				bool already;
   390 				SourceMM::CPluginManager::CPlugin *pl;
   391 
   392 				PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error)-1);
   393 				pl = g_PluginMngr.FindById(id);
   394 				if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
   395 				{
   396 					CONMSG("Failed to load plugin %s (%s).\n", file, error);
   397 					return;
   398 				}
   399 
   400 				if (!already)
   401 				{
   402 					CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
   403 				} else {
   404 					CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
   405 				}
   406 				
   407 				return;
   408 			} else {
   409 				CONMSG("Usage: meta load <path>\n");
   410 
   411 				return;
   412 			}
   413 		} else if (strcmp(command, "unload") == 0) {
   414 			if (args >= 3)
   415 			{
   416 				int id = atoi(e->Cmd_Argv(2));
   417 				char error[255]={0};
   418 
   419 				if (!g_PluginMngr.Unload(id, false, error, sizeof(error)-1))
   420 				{
   421 					CONMSG("Unload failed: %s\n", error);
   422 					return;
   423 				}
   424 
   425 				CONMSG("Plugin %d unloaded.\n", id);
   426 
   427 				return;
   428 			} else {
   429 				CONMSG("Usage: meta unload <id>\n");
   430 
   431 				return;
   432 			}
   433 		} else if (strcmp(command, "force_unload") == 0) {
   434 			if (args >= 3)
   435 			{
   436 				int id = atoi(e->Cmd_Argv(2));
   437 				char error[255]={0};
   438 
   439 				if (!g_PluginMngr.Unload(id, false, error, sizeof(error)-1))
   440 				{
   441 					CONMSG("Force unload failed: %s\n", error);
   442 					return;
   443 				}
   444 
   445 				CONMSG("Plugin %d force unloaded.\n", id);
   446 
   447 				return;
   448 			} else {
   449 				CONMSG("Usage: meta force_unload <id>\n");
   450 
   451 				return;
   452 			}
   453 		} else if (strcmp(command, "clear") == 0) {
   454 			if (!g_PluginMngr.UnloadAll())
   455 			{
   456 				CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
   457 				return;
   458 			} 
   459 
   460 			CONMSG("All plugins unloaded.\n");
   461 
   462 			return;
   463 		} else if (strcmp(command, "retry") == 0) {
   464 			if (args >= 3)
   465 			{
   466 				int id = atoi(e->Cmd_Argv(2));
   467 				char error[255];
   468 
   469 				if (!g_PluginMngr.Retry(id, error, sizeof(error)-1))
   470 				{
   471 					CONMSG("Error reloading plugin: %s\n", error);
   472 					return;
   473 				}
   474 
   475 				CONMSG("Plugin %d successfully reloaded.\n", id);
   476 
   477 				return;
   478 			} else {
   479 				CONMSG("Usage: meta retry <id>\n");
   480 
   481 				return;
   482 			}
   483 		}
   484 	}
   485 
   486 	CONMSG("Metamod:Source Menu\n");
   487 	CONMSG("usage: meta <command> [arguments]\n");
   488 	CONMSG("  clear        - Unload all plugins forcefully\n");
   489 	CONMSG("  cmds         - Show plugin commands\n");
   490 	CONMSG("  cvars        - Show plugin cvars\n");
   491 	CONMSG("  credits      - About Metamod:Source\n");
   492 	CONMSG("  force_unload - Forcefully unload a plugin\n");
   493 	CONMSG("  game         - Information about GameDLL\n");
   494 	CONMSG("  info         - Information about a plugin\n");
   495 	CONMSG("  list         - List plugins\n");
   496 	CONMSG("  load         - Load a plugin\n");
   497 	CONMSG("  pause        - Pause a running plugin\n");
   498 	CONMSG("  refresh      - Reparse plugins file\n");
   499 	CONMSG("  retry        - Attempt to reload a plugin\n");
   500 	CONMSG("  unload       - Unload a loaded plugin\n");
   501 	CONMSG("  unpause      - Unpause a paused plugin\n");
   502 	CONMSG("  version      - Version information\n");
   503 }
   504 
   505 CAlwaysRegisterableCommand::CAlwaysRegisterableCommand()
   506 {
   507 	Create("", NULL, FCVAR_UNREGISTERED|FCVAR_GAMEDLL);
   508 	m_pICvar = NULL;
   509 }
   510 
   511 bool CAlwaysRegisterableCommand::IsRegistered( void ) const
   512 {
   513 	return false;
   514 }
   515 
   516 void CAlwaysRegisterableCommand::BringToFront()
   517 {
   518 	if (!m_pICvar)
   519 		m_pICvar = g_Engine.icvar;
   520 
   521 	// First, let's try to find us!
   522 	ConCommandBase *pPtr = m_pICvar->GetCommands();
   523 
   524 	if (pPtr == this)
   525 	{
   526 		// We are already at the beginning; Nothing to do
   527 		return;
   528 	}
   529 
   530 	while (pPtr)
   531 	{
   532 		if (pPtr == this && pPtr->IsCommand() && stricmp(GetName(), pPtr->GetName()) == 0)
   533 			break;
   534 		ConCommandBase *pPrev = NULL;
   535 		while (pPtr)
   536 		{
   537 			if (pPtr == this)
   538 				break;
   539 			pPrev = pPtr;
   540 			pPtr = const_cast<ConCommandBase*>(pPtr->GetNext());
   541 		}
   542 		if (pPrev && pPtr == this)
   543 		{
   544 			pPrev->SetNext(m_pNext);		// Remove us from the list
   545 		}
   546 		// Now, register us
   547 		SetNext(NULL);
   548 		m_pICvar->RegisterConCommandBase(this);
   549 	}
   550 }
   551 
   552 const char *GetPluginsFile()
   553 {
   554 	return mm_pluginsfile.GetString();
   555 }
   556