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