sourcemm/concommands.cpp
author Scott Ehlert <ds@alliedmods.net>
Sun May 22 04:33:58 2005 +0000 (2005-05-22)
changeset 66 ddefc8bfb9fa
parent 65 27c4d32394a6
child 67 1753f7a9b2f2
permissions -rw-r--r--
Added macro so that ConCommands created with the SDK's CON_COMMAND and CON_COMMAND_F macros can be properly registered and unregisterd
     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 "concommands.h"
    12 #include "CPlugin.h"
    13 
    14 /**
    15  * @brief Console Command Implementations
    16  * @file concommands.cpp
    17  */
    18 
    19 SMConVarAccessor g_SMConVarAccessor;
    20 
    21 bool SMConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand)
    22 {
    23 	// Add the FCVAR_GAMEDLL flag 
    24 	// => No crash on exit!
    25 	// UPDATE: Do _not_ add the FCVAR_GAMEDLL flag here, as it
    26 	// causes the command to be unusable on listenservers until you load a map
    27 	// We will set the FCVAR_GAMEDLL flag on all commands we have registered once we are being unloaded
    28 	//pCommand->AddFlags(FCVAR_GAMEDLL);
    29 	m_RegisteredCommands.push_back(pCommand);
    30 
    31 	pCommand->SetNext( NULL );
    32 	g_Engine.icvar->RegisterConCommandBase(pCommand);
    33 
    34 	return true;
    35 }
    36 
    37 void SMConVarAccessor::MarkCommandsAsGameDLL()
    38 {
    39 	for (std::list<ConCommandBase*>::iterator iter = m_RegisteredCommands.begin();
    40 		iter != m_RegisteredCommands.end(); ++iter)
    41 	{
    42 		(*iter)->AddFlags(FCVAR_GAMEDLL);
    43 	}
    44 }
    45 
    46 void SMConVarAccessor::Unregister(ConCommandBase *pCvar)
    47 {
    48 	ICvar *cv = g_Engine.icvar;
    49 
    50 	ConCommandBase *ptr = cv->GetCommands();
    51 
    52 	if (ptr == pCvar && ptr->GetNext())
    53 	{
    54 		//we're at the beginning of the list
    55 		*ptr = *(ptr->GetNext());
    56 		return;
    57 	}
    58 
    59 	while (ptr)
    60 	{
    61 		ConCommandBase *pNext = const_cast<ConCommandBase *>(ptr->GetNext());
    62 		if (pNext == pCvar)
    63 			break;
    64 		ptr = pNext;
    65 	}
    66 
    67 	if (ptr)
    68 	{
    69 		ptr->SetNext(const_cast<ConCommandBase *>(pCvar->GetNext()));
    70 		pCvar->SetNext(NULL);
    71 	}
    72 
    73 	m_RegisteredCommands.remove(pCvar);
    74 }
    75 
    76 ConVar metamod_version("metamod_version", SOURCEMM_VERSION, FCVAR_REPLICATED | FCVAR_SPONLY, "Metamod:Source Version");
    77 
    78 CON_COMMAND(meta, "Metamod:Source Menu")
    79 {
    80 	IVEngineServer *e = g_Engine.engine;
    81 
    82 	int args = e->Cmd_Argc();
    83 
    84 	if (args >= 2)
    85 	{
    86 		const char *command = e->Cmd_Argv(1);
    87 		if (strcmp(command, "credits") == 0)
    88 		{
    89 			Msg("Metamod:Source was developed by:\n");
    90 			Msg("  SourceHook: Pavol \"PM OnoTo\" Marko\n");
    91 			Msg("  GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
    92 			Msg("  GameDLL: Scott \"Damaged Soul\" Ehlert\n");
    93 			Msg("For more information, see the official website\n");
    94 			Msg("http://www.sourcemm.net/\n\n");
    95 			
    96 			return;
    97 		} else if (strcmp(command, "version") == 0) {
    98 			Msg("Metamod:Source version %s\n", SOURCEMM_VERSION);
    99 			Msg("Compiled on: %s\n", SOURCEMM_DATE);
   100 			Msg("Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
   101 			Msg("SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
   102 			Msg("http://www.sourcemm.net/\n\n");
   103 
   104 			return;
   105 		} else if (strcmp(command, "game") == 0) {
   106 			Msg("GameDLL Information\n");
   107 			Msg("  Mod path: %s\n", g_ModPath.c_str());
   108 			Msg("  Dll path: %s\n", g_BinPath.c_str());
   109 
   110 			return;
   111 		} else if (strcmp(command, "refresh") == 0) {
   112 			char full_path[255];
   113 #if defined WIN32 || defined _WIN32
   114 			snprintf(full_path, sizeof(full_path)-1, "%s\\%s", g_ModPath.c_str(), "metaplugins.ini");
   115 #else
   116 			snprintf(full_path, sizeof(full_path)-1, "%s/%s", g_ModPath.c_str(), "metaplugins.ini");
   117 #endif
   118 			LoadPluginsFromFile(full_path);
   119 
   120 			return;
   121 		} else if (strcmp(command, "list") == 0) {
   122 			SourceMM::CPluginManager::CPlugin *pl;
   123 			PluginIter i;
   124 			const char *status="";
   125 			const char *version=NULL;
   126 			const char *name=NULL;
   127 			const char *author=NULL;
   128 
   129 			Msg("-Id- %-16.15s  %-8.7s  %-12.11s %-8.7s\n", "Name", "Version", "Author", "Status");
   130 			for (i=g_PluginMngr._begin(); i!=g_PluginMngr._end(); i++)
   131 			{
   132 				pl = (*i);
   133 				if (!pl)
   134 					break;
   135 				if (pl->m_Status == Pl_Paused)
   136 				{
   137 					status = "PAUSE";
   138 				} else if (pl->m_Status == Pl_Running) {
   139 					status = "RUN";
   140 				} else if (pl->m_Status == Pl_Refused) {
   141 					status = "FAIL";
   142 				} else if (pl->m_Status == Pl_Error) {
   143 					status = "ERROR";
   144 				} else if (pl->m_Status == Pl_NotFound) {
   145 					status = "NOFILE";
   146 				}
   147 
   148 				if (pl->m_API)
   149 				{
   150 					version = pl->m_API->GetVersion();
   151 					author = pl->m_API->GetAuthor();
   152 					name = pl->m_API->GetName();
   153 				} else {
   154 					version = "-";
   155 					author = "-";
   156 					name = "-";
   157 				}
   158 
   159 				if (!version)
   160 					version = "-";
   161 				if (!author)
   162 					author = "-";
   163 				if (!name)
   164 					name = pl->m_File.c_str();
   165 
   166 
   167 				Msg("[%02d] %-16.15s  %-8.7s  %-12.11s %-8.7s\n", pl->m_Id, name, version, author, status);
   168 			}
   169 
   170 			Msg("\n");
   171 
   172 			return;
   173 		} else if (strcmp(command, "info") == 0) {
   174 			if (args >= 3)
   175 			{
   176 				int id = atoi(e->Cmd_Argv(2));
   177 				SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   178 				if (!pl)
   179 				{
   180 					Msg("Plugin %d not found.\n", id);
   181 					return;
   182 				}
   183 
   184 				if (!pl->m_API)
   185 				{
   186 					Msg("Plugin %d is not loaded.\n", id);
   187 				} else {
   188 					if (pl->m_Status == Pl_Paused)
   189 					{
   190 						Msg("Plugin %d is paused.\n", id);
   191 					} else if (pl->m_Status == Pl_Running) {
   192 						Msg("Plugin %d is running.\n", id);
   193 					}
   194 					Msg("  Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
   195 					Msg("  Version: %s\n", pl->m_API->GetVersion());
   196 					Msg("  Description: %s\n", pl->m_API->GetDescription());
   197 					Msg("  License: %s\n", pl->m_API->GetLicense());
   198 					Msg("  URL: %s\n", pl->m_API->GetURL());
   199 					Msg("  Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
   200 				}
   201 				Msg("File: %s\n\n", pl->m_File.c_str());
   202 
   203 				return;
   204 			} else {
   205 				Msg("Usage: meta info <id>\n");
   206 
   207 				return;
   208 			}
   209 		} else if (strcmp(command, "pause") == 0) {
   210 			if (args >= 3)
   211 			{
   212 				int id = atoi(e->Cmd_Argv(2));
   213 
   214 				char error[255];
   215 
   216 				if (!g_PluginMngr.Pause(id, error, sizeof(error)-1))
   217 				{
   218 					Msg("Pause failed: %s\n", error);
   219 					return;
   220 				}
   221 				
   222 				Msg("Plugin %d has been paused.\n", id);
   223 
   224 				return;
   225 			} else {
   226 				Msg("Usage: meta pause <id>\n");
   227 
   228 				return;
   229 			}
   230 		} else if (strcmp(command, "unpause") == 0) {
   231 			if (args >= 3)
   232 			{
   233 				int id = atoi(e->Cmd_Argv(2));
   234 				char error[255];
   235 
   236 				if (!g_PluginMngr.Unpause(id, error, sizeof(error)-1))
   237 				{
   238 					Msg("Unpause failed: %s\n", error);
   239 					return;
   240 				}
   241 
   242 				Msg("Plugin %d has been unpaused.\n", id);
   243 
   244 				return;
   245 			} else {
   246 				Msg("Usage: meta unpause <id>\n");
   247 
   248 				return;
   249 			}
   250 		} else if (strcmp(command, "load") == 0) {
   251 			if (args >= 3)
   252 			{
   253 				const char *file = e->Cmd_Argv(2);
   254 				char full_path[255];
   255 
   256 				if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
   257 				{
   258 					snprintf(full_path, sizeof(full_path)-1, "%s", file);
   259 				} else {
   260 					const char *ext = UTIL_GetExtension(file);
   261 #if defined WIN32 || defined _WIN32
   262 					ext = ext ? "" : ".dll";
   263 					snprintf(full_path, sizeof(full_path)-1, "%s\\%s%s", g_ModPath.c_str(), file, ext);
   264 #else
   265 					ext = ext ? "" : "_i486.so";
   266 					snprintf(full_path, sizeof(full_path)-1, "%s/%s%s", g_ModPath.c_str(), file, ext);
   267 #endif
   268 				}
   269 
   270 				char error[255]={0};
   271 				bool already;
   272 				SourceMM::CPluginManager::CPlugin *pl;
   273 
   274 				PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error)-1);
   275 				pl = g_PluginMngr.FindById(id);
   276 				if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
   277 				{
   278 					Msg("Failed to load plugin %s (%s).\n", file, error);
   279 					return;
   280 				}
   281 
   282 				if (!already)
   283 				{
   284 					Msg("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
   285 				} else {
   286 					Msg("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
   287 				}
   288 				
   289 				return;
   290 			} else {
   291 				Msg("Usage: meta load <path>\n");
   292 
   293 				return;
   294 			}
   295 		} else if (strcmp(command, "unload") == 0) {
   296 			if (args >= 3)
   297 			{
   298 				int id = atoi(e->Cmd_Argv(2));
   299 				char error[255]={0};
   300 
   301 				if (!g_PluginMngr.Unload(id, false, error, sizeof(error)-1))
   302 				{
   303 					Msg("Unload failed: %s\n", error);
   304 					return;
   305 				}
   306 
   307 				Msg("Plugin %d unloaded.\n", id);
   308 
   309 				return;
   310 			} else {
   311 				Msg("Usage: meta unload <id>\n");
   312 
   313 				return;
   314 			}
   315 		} else if (strcmp(command, "force_unload") == 0) {
   316 			if (args >= 3)
   317 			{
   318 				int id = atoi(e->Cmd_Argv(2));
   319 				char error[255]={0};
   320 
   321 				if (!g_PluginMngr.Unload(id, false, error, sizeof(error)-1))
   322 				{
   323 					Msg("Force unload failed: %s\n", error);
   324 					return;
   325 				}
   326 
   327 				Msg("Plugin %d force unloaded.\n", id);
   328 
   329 				return;
   330 			} else {
   331 				Msg("Usage: meta force_unload <id>\n");
   332 
   333 				return;
   334 			}
   335 		} else if (strcmp(command, "clear") == 0) {
   336 			if (!g_PluginMngr.UnloadAll())
   337 			{
   338 				Msg("One or more plugins resisted removal (cleaned anyway).\n");
   339 				return;
   340 			} 
   341 
   342 			Msg("All plugins unloaded.\n");
   343 
   344 			return;
   345 		} else if (strcmp(command, "retry") == 0) {
   346 			if (args >= 3)
   347 			{
   348 				int id = atoi(e->Cmd_Argv(2));
   349 				char error[255];
   350 
   351 				if (!g_PluginMngr.Retry(id, error, sizeof(error)-1))
   352 				{
   353 					Msg("Error reloading plugin: %s\n", error);
   354 					return;
   355 				}
   356 
   357 				Msg("Plugin %d successfully reloaded.\n", id);
   358 
   359 				return;
   360 			} else {
   361 				Msg("Usage: meta retry <id>\n");
   362 
   363 				return;
   364 			}
   365 		}
   366 	}
   367 
   368 	Msg("Metamod:Source Menu\n");
   369 	Msg("usage: meta <command> [arguments]\n");
   370 	Msg("  clear        - Unload all plugins forcefully\n");
   371 	Msg("  credits      - About Metamod:Source\n");
   372 	Msg("  force_unload - Forcefully unload a plugin\n");
   373 	Msg("  game         - Information about GameDLL\n");
   374 	Msg("  info         - Information about a plugin\n");
   375 	Msg("  list         - List plugins\n");
   376 	Msg("  load         - Load a plugin\n");
   377 	Msg("  pause        - Pause a running plugin\n");
   378 	Msg("  refresh      - Reparse plugins file\n");
   379 	Msg("  retry        - Attempt to reload a plugin\n");
   380 	Msg("  unload       - Unload a loaded plugin\n");
   381 	Msg("  unpause      - Unpause a paused plugin\n");
   382 	Msg("  version      - Version information\n");
   383 	Msg("\n");
   384 }