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