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