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