sourcemm/concommands.cpp
author David Anderson <dvander@alliedmods.net>
Thu Sep 29 23:05:01 2005 +0000 (2005-09-29)
changeset 116 65ce756bf3cc
parent 114 6dcc66e9c3cc
child 124 b04971393ee4
permissions -rw-r--r--
Re-bumped all version numbers again (SourceHook=2, Plugin=7/7)
Fixed bug where gamedlls setting new game names would not work
Moved PM's virtual destructor to bottom of vtable
     1 /* ======== SourceMM ========
     2 * Copyright (C) 2004-2005 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 #if defined WIN32 || defined _WIN32
   161 			snprintf(full_path, sizeof(full_path)-1, "%s\\addons\\metamod\\%s", g_ModPath.c_str(), "metaplugins.ini");
   162 #else
   163 			snprintf(full_path, sizeof(full_path)-1, "%s/addons/metamod/%s", g_ModPath.c_str(), "metaplugins.ini");
   164 #endif
   165 			LoadPluginsFromFile(full_path);
   166 
   167 			return;
   168 		} else if (strcmp(command, "list") == 0) {
   169 			SourceMM::CPluginManager::CPlugin *pl;
   170 			PluginIter i;
   171 			const char *status="";
   172 			const char *version=NULL;
   173 			const char *name=NULL;
   174 			const char *author=NULL;
   175 
   176 			CONMSG("-Id- %-16.15s  %-8.7s  %-12.11s %-8.7s\n", "Name", "Version", "Author", "Status");
   177 			for (i=g_PluginMngr._begin(); i!=g_PluginMngr._end(); i++)
   178 			{
   179 				pl = (*i);
   180 				if (!pl)
   181 					break;
   182 				if (pl->m_Status == Pl_Paused)
   183 				{
   184 					status = "PAUSE";
   185 				} else if (pl->m_Status == Pl_Running) {
   186 					status = "RUN";
   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] %-16.15s  %-8.7s  %-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 						CONMSG("Plugin %d is running.\n", id);
   302 					}
   303 					CONMSG("  Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
   304 					CONMSG("  Version: %s\n", pl->m_API->GetVersion());
   305 					CONMSG("  Description: %s\n", pl->m_API->GetDescription());
   306 					CONMSG("  License: %s\n", pl->m_API->GetLicense());
   307 					CONMSG("  URL: %s\n", pl->m_API->GetURL());
   308 					CONMSG("  Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
   309 				}
   310 				CONMSG("File: %s\n\n", pl->m_File.c_str());
   311 
   312 				return;
   313 			} else {
   314 				CONMSG("Usage: meta info <id>\n");
   315 
   316 				return;
   317 			}
   318 		} else if (strcmp(command, "pause") == 0) {
   319 			if (args >= 3)
   320 			{
   321 				int id = atoi(e->Cmd_Argv(2));
   322 
   323 				char error[255];
   324 
   325 				if (!g_PluginMngr.Pause(id, error, sizeof(error)-1))
   326 				{
   327 					CONMSG("Pause failed: %s\n", error);
   328 					return;
   329 				}
   330 				
   331 				CONMSG("Plugin %d has been paused.\n", id);
   332 
   333 				return;
   334 			} else {
   335 				CONMSG("Usage: meta pause <id>\n");
   336 
   337 				return;
   338 			}
   339 		} else if (strcmp(command, "unpause") == 0) {
   340 			if (args >= 3)
   341 			{
   342 				int id = atoi(e->Cmd_Argv(2));
   343 				char error[255];
   344 
   345 				if (!g_PluginMngr.Unpause(id, error, sizeof(error)-1))
   346 				{
   347 					CONMSG("Unpause failed: %s\n", error);
   348 					return;
   349 				}
   350 
   351 				CONMSG("Plugin %d has been unpaused.\n", id);
   352 
   353 				return;
   354 			} else {
   355 				CONMSG("Usage: meta unpause <id>\n");
   356 
   357 				return;
   358 			}
   359 		} else if (strcmp(command, "load") == 0) {
   360 			if (args >= 3)
   361 			{
   362 				const char *file = e->Cmd_Argv(2);
   363 				char full_path[255];
   364 
   365 				if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
   366 				{
   367 					snprintf(full_path, sizeof(full_path)-1, "%s", file);
   368 				} else {
   369 					const char *ext = UTIL_GetExtension(file);
   370 #if defined WIN32 || defined _WIN32
   371 					ext = ext ? "" : ".dll";
   372 					snprintf(full_path, sizeof(full_path)-1, "%s\\%s%s", g_ModPath.c_str(), file, ext);
   373 #else
   374 					ext = ext ? "" : "_i486.so";
   375 					snprintf(full_path, sizeof(full_path)-1, "%s/%s%s", g_ModPath.c_str(), file, ext);
   376 #endif
   377 				}
   378 
   379 				char error[255]={0};
   380 				bool already;
   381 				SourceMM::CPluginManager::CPlugin *pl;
   382 
   383 				PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error)-1);
   384 				pl = g_PluginMngr.FindById(id);
   385 				if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
   386 				{
   387 					CONMSG("Failed to load plugin %s (%s).\n", file, error);
   388 					return;
   389 				}
   390 
   391 				if (!already)
   392 				{
   393 					CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
   394 				} else {
   395 					CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
   396 				}
   397 				
   398 				return;
   399 			} else {
   400 				CONMSG("Usage: meta load <path>\n");
   401 
   402 				return;
   403 			}
   404 		} else if (strcmp(command, "unload") == 0) {
   405 			if (args >= 3)
   406 			{
   407 				int id = atoi(e->Cmd_Argv(2));
   408 				char error[255]={0};
   409 
   410 				if (!g_PluginMngr.Unload(id, false, error, sizeof(error)-1))
   411 				{
   412 					CONMSG("Unload failed: %s\n", error);
   413 					return;
   414 				}
   415 
   416 				CONMSG("Plugin %d unloaded.\n", id);
   417 
   418 				return;
   419 			} else {
   420 				CONMSG("Usage: meta unload <id>\n");
   421 
   422 				return;
   423 			}
   424 		} else if (strcmp(command, "force_unload") == 0) {
   425 			if (args >= 3)
   426 			{
   427 				int id = atoi(e->Cmd_Argv(2));
   428 				char error[255]={0};
   429 
   430 				if (!g_PluginMngr.Unload(id, false, error, sizeof(error)-1))
   431 				{
   432 					CONMSG("Force unload failed: %s\n", error);
   433 					return;
   434 				}
   435 
   436 				CONMSG("Plugin %d force unloaded.\n", id);
   437 
   438 				return;
   439 			} else {
   440 				CONMSG("Usage: meta force_unload <id>\n");
   441 
   442 				return;
   443 			}
   444 		} else if (strcmp(command, "clear") == 0) {
   445 			if (!g_PluginMngr.UnloadAll())
   446 			{
   447 				CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
   448 				return;
   449 			} 
   450 
   451 			CONMSG("All plugins unloaded.\n");
   452 
   453 			return;
   454 		} else if (strcmp(command, "retry") == 0) {
   455 			if (args >= 3)
   456 			{
   457 				int id = atoi(e->Cmd_Argv(2));
   458 				char error[255];
   459 
   460 				if (!g_PluginMngr.Retry(id, error, sizeof(error)-1))
   461 				{
   462 					CONMSG("Error reloading plugin: %s\n", error);
   463 					return;
   464 				}
   465 
   466 				CONMSG("Plugin %d successfully reloaded.\n", id);
   467 
   468 				return;
   469 			} else {
   470 				CONMSG("Usage: meta retry <id>\n");
   471 
   472 				return;
   473 			}
   474 		}
   475 	}
   476 
   477 	CONMSG("Metamod:Source Menu\n");
   478 	CONMSG("usage: meta <command> [arguments]\n");
   479 	CONMSG("  clear        - Unload all plugins forcefully\n");
   480 	CONMSG("  cmds         - Show plugin commands\n");
   481 	CONMSG("  cvars        - Show plugin cvars\n");
   482 	CONMSG("  credits      - About Metamod:Source\n");
   483 	CONMSG("  force_unload - Forcefully unload a plugin\n");
   484 	CONMSG("  game         - Information about GameDLL\n");
   485 	CONMSG("  info         - Information about a plugin\n");
   486 	CONMSG("  list         - List plugins\n");
   487 	CONMSG("  load         - Load a plugin\n");
   488 	CONMSG("  pause        - Pause a running plugin\n");
   489 	CONMSG("  refresh      - Reparse plugins file\n");
   490 	CONMSG("  retry        - Attempt to reload a plugin\n");
   491 	CONMSG("  unload       - Unload a loaded plugin\n");
   492 	CONMSG("  unpause      - Unpause a paused plugin\n");
   493 	CONMSG("  version      - Version information\n");
   494 }
   495 
   496 CAlwaysRegisterableCommand::CAlwaysRegisterableCommand()
   497 {
   498 	Create("", NULL, FCVAR_UNREGISTERED|FCVAR_GAMEDLL);
   499 	m_pICvar = NULL;
   500 }
   501 
   502 bool CAlwaysRegisterableCommand::IsRegistered( void ) const
   503 {
   504 	return false;
   505 }
   506 
   507 void CAlwaysRegisterableCommand::BringToFront()
   508 {
   509 	if (!m_pICvar)
   510 		m_pICvar = g_Engine.icvar;
   511 
   512 	// First, let's try to find us!
   513 	ConCommandBase *pPtr = m_pICvar->GetCommands();
   514 
   515 	if (pPtr == this)
   516 	{
   517 		// We are already at the beginning; Nothing to do
   518 		return;
   519 	}
   520 
   521 	while (pPtr)
   522 	{
   523 		if (pPtr == this && pPtr->IsCommand() && stricmp(GetName(), pPtr->GetName()) == 0)
   524 			break;
   525 		ConCommandBase *pPrev = NULL;
   526 		while (pPtr)
   527 		{
   528 			if (pPtr == this)
   529 				break;
   530 			pPrev = pPtr;
   531 			pPtr = const_cast<ConCommandBase*>(pPtr->GetNext());
   532 		}
   533 		if (pPrev && pPtr == this)
   534 		{
   535 			pPrev->SetNext(m_pNext);		// Remove us from the list
   536 		}
   537 		// Now, register us
   538 		m_pICvar->RegisterConCommandBase(this);
   539 	}
   540 }
   541 
   542 const char *GetPluginsFile()
   543 {
   544 	return mm_pluginsfile.GetString();
   545 }
   546