sourcemm/concommands.cpp
author Pavol Marko <pavolmarko@gmail.com>
Fri Oct 14 21:09:12 2005 +0000 (2005-10-14)
changeset 136 0df65778613e
parent 132 05523b624b73
child 175 b497b7777f6b
permissions -rw-r--r--
:D
     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 					if (pl->m_API && pl->m_API->QueryRunning(NULL, 0))
   187 						status = "RUN";
   188 					else
   189 						status = "STOPPED";
   190 				} else if (pl->m_Status == Pl_Refused) {
   191 					status = "FAIL";
   192 				} else if (pl->m_Status == Pl_Error) {
   193 					status = "ERROR";
   194 				} else if (pl->m_Status == Pl_NotFound) {
   195 					status = "NOFILE";
   196 				}
   197 
   198 				if (pl->m_API)
   199 				{
   200 					version = pl->m_API->GetVersion();
   201 					author = pl->m_API->GetAuthor();
   202 					name = pl->m_API->GetName();
   203 				} else {
   204 					version = "-";
   205 					author = "-";
   206 					name = "-";
   207 				}
   208 
   209 				if (!version)
   210 					version = "-";
   211 				if (!author)
   212 					author = "-";
   213 				if (!name)
   214 					name = pl->m_File.c_str();
   215 
   216 
   217 				CONMSG("[%02d] %-16.15s  %-8.7s  %-12.11s %-8.7s\n", pl->m_Id, name, version, author, status);
   218 			}
   219 
   220 			//CONMSG("\n");
   221 
   222 			return;
   223 		} else if (strcmp(command, "cmds") == 0) {
   224 			if (args >= 3)
   225 			{
   226 				int id = atoi(e->Cmd_Argv(2));
   227 				SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   228 
   229 				if (!pl)
   230 				{
   231 					CONMSG("Plugin %d not found.\n", id);
   232 					return;
   233 				}
   234 
   235 				if (!pl->m_API)
   236 				{
   237 					CONMSG("Plugin %d is not loaded.\n", id);
   238 				} else {
   239 					CONMSG("Console commands for %s:\n", pl->m_API->GetName());
   240 					SourceHook::List<ConCommandBase *>::iterator ci;
   241 					size_t count = 0;
   242 
   243 					for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
   244 					{
   245 						count++;
   246 						CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
   247 					}
   248 				}
   249 			} else {
   250 				CONMSG("Usage: meta cmds <id>\n");
   251 			}
   252 
   253 			return;
   254 		} else if (strcmp(command, "cvars") == 0) {
   255 			if (args >= 3)
   256 			{
   257 				int id = atoi(e->Cmd_Argv(2));
   258 				SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   259 
   260 				if (!pl)
   261 				{
   262 					CONMSG("Plugin %d not found.\n", id);
   263 					return;
   264 				}
   265 
   266 				if (!pl->m_API)
   267 				{
   268 					CONMSG("Plugin %d is not loaded.\n", id);
   269 				} else {
   270 					CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
   271 					SourceHook::List<ConCommandBase *>::iterator ci;
   272 					size_t count = 0;
   273 
   274 					for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
   275 					{
   276 						count++;
   277 						CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
   278 					}
   279 				}
   280 			} else {
   281 				CONMSG("Usage: meta cvars <id>\n");
   282 			}
   283 
   284 			return;
   285 		} else if (strcmp(command, "info") == 0) {
   286 			if (args >= 3)
   287 			{
   288 				int id = atoi(e->Cmd_Argv(2));
   289 				SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   290 				if (!pl)
   291 				{
   292 					CONMSG("Plugin %d not found.\n", id);
   293 					return;
   294 				}
   295 
   296 				if (!pl->m_API)
   297 				{
   298 					CONMSG("Plugin %d is not loaded.\n", id);
   299 				} else {
   300 					if (pl->m_Status == Pl_Paused)
   301 					{
   302 						CONMSG("Plugin %d is paused.\n", id);
   303 					} else if (pl->m_Status == Pl_Running) {
   304 						char run_msg[255];
   305 						bool run = false;
   306 						if (pl->m_API && pl->m_API->QueryRunning(run_msg, sizeof(run_msg)-1))
   307 							run = true;
   308 						if (run)
   309 						{
   310 							CONMSG("Plugin %d is running.\n", id);
   311 						} else {
   312 							CONMSG("Plugin %d is stopped: %s\n", id, run_msg);
   313 						}
   314 					}
   315 					CONMSG("  Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
   316 					CONMSG("  Version: %s\n", pl->m_API->GetVersion());
   317 					CONMSG("  Description: %s\n", pl->m_API->GetDescription());
   318 					CONMSG("  License: %s\n", pl->m_API->GetLicense());
   319 					CONMSG("  URL: %s\n", pl->m_API->GetURL());
   320 					CONMSG("  Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
   321 				}
   322 				CONMSG("File: %s\n\n", pl->m_File.c_str());
   323 
   324 				return;
   325 			} else {
   326 				CONMSG("Usage: meta info <id>\n");
   327 
   328 				return;
   329 			}
   330 		} else if (strcmp(command, "pause") == 0) {
   331 			if (args >= 3)
   332 			{
   333 				int id = atoi(e->Cmd_Argv(2));
   334 
   335 				char error[255];
   336 
   337 				if (!g_PluginMngr.Pause(id, error, sizeof(error)-1))
   338 				{
   339 					CONMSG("Pause failed: %s\n", error);
   340 					return;
   341 				}
   342 				
   343 				CONMSG("Plugin %d has been paused.\n", id);
   344 
   345 				return;
   346 			} else {
   347 				CONMSG("Usage: meta pause <id>\n");
   348 
   349 				return;
   350 			}
   351 		} else if (strcmp(command, "unpause") == 0) {
   352 			if (args >= 3)
   353 			{
   354 				int id = atoi(e->Cmd_Argv(2));
   355 				char error[255];
   356 
   357 				if (!g_PluginMngr.Unpause(id, error, sizeof(error)-1))
   358 				{
   359 					CONMSG("Unpause failed: %s\n", error);
   360 					return;
   361 				}
   362 
   363 				CONMSG("Plugin %d has been unpaused.\n", id);
   364 
   365 				return;
   366 			} else {
   367 				CONMSG("Usage: meta unpause <id>\n");
   368 
   369 				return;
   370 			}
   371 		} else if (strcmp(command, "load") == 0) {
   372 			if (args >= 3)
   373 			{
   374 				const char *file = e->Cmd_Argv(2);
   375 				char full_path[255];
   376 
   377 				if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
   378 				{
   379 					snprintf(full_path, sizeof(full_path)-1, "%s", file);
   380 				} else {
   381 					const char *ext = UTIL_GetExtension(file);
   382 #if defined WIN32 || defined _WIN32
   383 					ext = ext ? "" : ".dll";
   384 					snprintf(full_path, sizeof(full_path)-1, "%s\\%s%s", g_ModPath.c_str(), file, ext);
   385 #else
   386 					ext = ext ? "" : "_i486.so";
   387 					snprintf(full_path, sizeof(full_path)-1, "%s/%s%s", g_ModPath.c_str(), file, ext);
   388 #endif
   389 				}
   390 
   391 				char error[255]={0};
   392 				bool already;
   393 				SourceMM::CPluginManager::CPlugin *pl;
   394 
   395 				PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error)-1);
   396 				pl = g_PluginMngr.FindById(id);
   397 				if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
   398 				{
   399 					CONMSG("Failed to load plugin %s (%s).\n", file, error);
   400 					return;
   401 				}
   402 
   403 				if (!already)
   404 				{
   405 					CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
   406 				} else {
   407 					CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
   408 				}
   409 				
   410 				return;
   411 			} else {
   412 				CONMSG("Usage: meta load <path>\n");
   413 
   414 				return;
   415 			}
   416 		} else if (strcmp(command, "unload") == 0) {
   417 			if (args >= 3)
   418 			{
   419 				int id = atoi(e->Cmd_Argv(2));
   420 				char error[255]={0};
   421 
   422 				if (!g_PluginMngr.Unload(id, false, error, sizeof(error)-1))
   423 				{
   424 					CONMSG("Unload failed: %s\n", error);
   425 					return;
   426 				}
   427 
   428 				CONMSG("Plugin %d unloaded.\n", id);
   429 
   430 				return;
   431 			} else {
   432 				CONMSG("Usage: meta unload <id>\n");
   433 
   434 				return;
   435 			}
   436 		} else if (strcmp(command, "force_unload") == 0) {
   437 			if (args >= 3)
   438 			{
   439 				int id = atoi(e->Cmd_Argv(2));
   440 				char error[255]={0};
   441 
   442 				if (!g_PluginMngr.Unload(id, false, error, sizeof(error)-1))
   443 				{
   444 					CONMSG("Force unload failed: %s\n", error);
   445 					return;
   446 				}
   447 
   448 				CONMSG("Plugin %d force unloaded.\n", id);
   449 
   450 				return;
   451 			} else {
   452 				CONMSG("Usage: meta force_unload <id>\n");
   453 
   454 				return;
   455 			}
   456 		} else if (strcmp(command, "clear") == 0) {
   457 			if (!g_PluginMngr.UnloadAll())
   458 			{
   459 				CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
   460 				return;
   461 			} 
   462 
   463 			CONMSG("All plugins unloaded.\n");
   464 
   465 			return;
   466 		} else if (strcmp(command, "retry") == 0) {
   467 			if (args >= 3)
   468 			{
   469 				int id = atoi(e->Cmd_Argv(2));
   470 				char error[255];
   471 
   472 				if (!g_PluginMngr.Retry(id, error, sizeof(error)-1))
   473 				{
   474 					CONMSG("Error reloading plugin: %s\n", error);
   475 					return;
   476 				}
   477 
   478 				CONMSG("Plugin %d successfully reloaded.\n", id);
   479 
   480 				return;
   481 			} else {
   482 				CONMSG("Usage: meta retry <id>\n");
   483 
   484 				return;
   485 			}
   486 		}
   487 	}
   488 
   489 	CONMSG("Metamod:Source Menu\n");
   490 	CONMSG("usage: meta <command> [arguments]\n");
   491 	CONMSG("  clear        - Unload all plugins forcefully\n");
   492 	CONMSG("  cmds         - Show plugin commands\n");
   493 	CONMSG("  cvars        - Show plugin cvars\n");
   494 	CONMSG("  credits      - About Metamod:Source\n");
   495 	CONMSG("  force_unload - Forcefully unload a plugin\n");
   496 	CONMSG("  game         - Information about GameDLL\n");
   497 	CONMSG("  info         - Information about a plugin\n");
   498 	CONMSG("  list         - List plugins\n");
   499 	CONMSG("  load         - Load a plugin\n");
   500 	CONMSG("  pause        - Pause a running plugin\n");
   501 	CONMSG("  refresh      - Reparse plugins file\n");
   502 	CONMSG("  retry        - Attempt to reload a plugin\n");
   503 	CONMSG("  unload       - Unload a loaded plugin\n");
   504 	CONMSG("  unpause      - Unpause a paused plugin\n");
   505 	CONMSG("  version      - Version information\n");
   506 }
   507 
   508 CAlwaysRegisterableCommand::CAlwaysRegisterableCommand()
   509 {
   510 	Create("", NULL, FCVAR_UNREGISTERED|FCVAR_GAMEDLL);
   511 	m_pICvar = NULL;
   512 }
   513 
   514 bool CAlwaysRegisterableCommand::IsRegistered( void ) const
   515 {
   516 	return false;
   517 }
   518 
   519 void CAlwaysRegisterableCommand::BringToFront()
   520 {
   521 	if (!m_pICvar)
   522 		m_pICvar = g_Engine.icvar;
   523 
   524 	// First, let's try to find us!
   525 	ConCommandBase *pPtr = m_pICvar->GetCommands();
   526 
   527 	if (pPtr == this)
   528 	{
   529 		// We are already at the beginning; Nothing to do
   530 		return;
   531 	}
   532 
   533 	while (pPtr)
   534 	{
   535 		if (pPtr == this && pPtr->IsCommand() && stricmp(GetName(), pPtr->GetName()) == 0)
   536 			break;
   537 		ConCommandBase *pPrev = NULL;
   538 		while (pPtr)
   539 		{
   540 			if (pPtr == this)
   541 				break;
   542 			pPrev = pPtr;
   543 			pPtr = const_cast<ConCommandBase*>(pPtr->GetNext());
   544 		}
   545 		if (pPrev && pPtr == this)
   546 		{
   547 			pPrev->SetNext(m_pNext);		// Remove us from the list
   548 		}
   549 		// Now, register us
   550 		SetNext(NULL);
   551 		m_pICvar->RegisterConCommandBase(this);
   552 	}
   553 }
   554 
   555 const char *GetPluginsFile()
   556 {
   557 	return mm_pluginsfile.GetString();
   558 }
   559