sourcemm/concommands.cpp
author David Anderson <dvander@alliedmods.net>
Mon May 16 20:15:57 2005 +0000 (2005-05-16)
changeset 64 ad98b9f2d80c
parent 63 ee7abd0da4dd
child 65 27c4d32394a6
permissions -rw-r--r--
Added experimental code for cvar management
     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 
    74 ConVar metamod_version("metamod_version", SOURCEMM_VERSION, FCVAR_REPLICATED | FCVAR_SPONLY, "Metamod:Source Version");
    75 
    76 CON_COMMAND(meta, "Metamod:Source Menu")
    77 {
    78 	IVEngineServer *e = g_Engine.engine;
    79 
    80 	int args = e->Cmd_Argc();
    81 
    82 	if (args >= 2)
    83 	{
    84 		const char *command = e->Cmd_Argv(1);
    85 		if (strcmp(command, "credits") == 0)
    86 		{
    87 			Msg("Metamod:Source was developed by:\n");
    88 			Msg("  SourceHook: Pavol \"PM OnoTo\" Marko\n");
    89 			Msg("  GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
    90 			Msg("  GameDLL: Scott \"Damaged Soul\" Ehlert\n");
    91 			Msg("For more information, see the official website\n");
    92 			Msg("http://www.sourcemm.net/\n\n");
    93 			
    94 			return;
    95 		} else if (strcmp(command, "version") == 0) {
    96 			Msg("Metamod:Source version %s\n", SOURCEMM_VERSION);
    97 			Msg("Compiled on: %s\n", SOURCEMM_DATE);
    98 			Msg("Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
    99 			Msg("SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
   100 			Msg("http://www.sourcemm.net/\n\n");
   101 
   102 			return;
   103 		} else if (strcmp(command, "game") == 0) {
   104 			Msg("GameDLL Information\n");
   105 			Msg("  Mod path: %s\n", g_ModPath.c_str());
   106 			Msg("  Dll path: %s\n", g_BinPath.c_str());
   107 
   108 			return;
   109 		} else if (strcmp(command, "refresh") == 0) {
   110 			char full_path[255];
   111 #if defined WIN32 || defined _WIN32
   112 			snprintf(full_path, sizeof(full_path)-1, "%s\\%s", g_ModPath.c_str(), "metaplugins.ini");
   113 #else
   114 			snprintf(full_path, sizeof(full_path)-1, "%s/%s", g_ModPath.c_str(), "metaplugins.ini");
   115 #endif
   116 			LoadPluginsFromFile(full_path);
   117 
   118 			return;
   119 		} else if (strcmp(command, "list") == 0) {
   120 			SourceMM::CPluginManager::CPlugin *pl;
   121 			PluginIter i;
   122 			const char *status="";
   123 			const char *version=NULL;
   124 			const char *name=NULL;
   125 			const char *author=NULL;
   126 
   127 			Msg("-Id- %-16.15s  %-8.7s  %-12.11s %-8.7s\n", "Name", "Version", "Author", "Status");
   128 			for (i=g_PluginMngr._begin(); i!=g_PluginMngr._end(); i++)
   129 			{
   130 				pl = (*i);
   131 				if (!pl)
   132 					break;
   133 				if (pl->m_Status == Pl_Paused)
   134 				{
   135 					status = "PAUSE";
   136 				} else if (pl->m_Status == Pl_Running) {
   137 					status = "RUN";
   138 				} else if (pl->m_Status == Pl_Refused) {
   139 					status = "FAIL";
   140 				} else if (pl->m_Status == Pl_Error) {
   141 					status = "ERROR";
   142 				} else if (pl->m_Status == Pl_NotFound) {
   143 					status = "NOFILE";
   144 				}
   145 
   146 				if (pl->m_API)
   147 				{
   148 					version = pl->m_API->GetVersion();
   149 					author = pl->m_API->GetAuthor();
   150 					name = pl->m_API->GetName();
   151 				} else {
   152 					version = "-";
   153 					author = "-";
   154 					name = "-";
   155 				}
   156 
   157 				if (!version)
   158 					version = "-";
   159 				if (!author)
   160 					author = "-";
   161 				if (!name)
   162 					name = pl->m_File.c_str();
   163 
   164 
   165 				Msg("[%02d] %-16.15s  %-8.7s  %-12.11s %-8.7s\n", pl->m_Id, name, version, author, status);
   166 			}
   167 
   168 			Msg("\n");
   169 
   170 			return;
   171 		} else if (strcmp(command, "info") == 0) {
   172 			if (args >= 3)
   173 			{
   174 				int id = atoi(e->Cmd_Argv(2));
   175 				SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   176 				if (!pl)
   177 				{
   178 					Msg("Plugin %d not found.\n", id);
   179 					return;
   180 				}
   181 
   182 				if (!pl->m_API)
   183 				{
   184 					Msg("Plugin %d is not loaded.\n", id);
   185 				} else {
   186 					if (pl->m_Status == Pl_Paused)
   187 					{
   188 						Msg("Plugin %d is paused.\n", id);
   189 					} else if (pl->m_Status == Pl_Running) {
   190 						Msg("Plugin %d is running.\n", id);
   191 					}
   192 					Msg("  Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
   193 					Msg("  Version: %s\n", pl->m_API->GetVersion());
   194 					Msg("  Description: %s\n", pl->m_API->GetDescription());
   195 					Msg("  License: %s\n", pl->m_API->GetLicense());
   196 					Msg("  URL: %s\n", pl->m_API->GetURL());
   197 					Msg("  Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
   198 				}
   199 				Msg("File: %s\n\n", pl->m_File.c_str());
   200 
   201 				return;
   202 			} else {
   203 				Msg("Usage: meta info <id>\n");
   204 
   205 				return;
   206 			}
   207 		} else if (strcmp(command, "pause") == 0) {
   208 			if (args >= 3)
   209 			{
   210 				int id = atoi(e->Cmd_Argv(2));
   211 
   212 				char error[255];
   213 
   214 				if (!g_PluginMngr.Pause(id, error, sizeof(error)-1))
   215 				{
   216 					Msg("Pause failed: %s\n", error);
   217 					return;
   218 				}
   219 				
   220 				Msg("Plugin %d has been paused.\n", id);
   221 
   222 				return;
   223 			} else {
   224 				Msg("Usage: meta pause <id>\n");
   225 
   226 				return;
   227 			}
   228 		} else if (strcmp(command, "unpause") == 0) {
   229 			if (args >= 3)
   230 			{
   231 				int id = atoi(e->Cmd_Argv(2));
   232 				char error[255];
   233 
   234 				if (!g_PluginMngr.Unpause(id, error, sizeof(error)-1))
   235 				{
   236 					Msg("Unpause failed: %s\n", error);
   237 					return;
   238 				}
   239 
   240 				Msg("Plugin %d has been unpaused.\n", id);
   241 
   242 				return;
   243 			} else {
   244 				Msg("Usage: meta unpause <id>\n");
   245 
   246 				return;
   247 			}
   248 		} else if (strcmp(command, "load") == 0) {
   249 			if (args >= 3)
   250 			{
   251 				const char *file = e->Cmd_Argv(2);
   252 				char full_path[255];
   253 
   254 				if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
   255 				{
   256 					snprintf(full_path, sizeof(full_path)-1, "%s", file);
   257 				} else {
   258 					const char *ext = UTIL_GetExtension(file);
   259 #if defined WIN32 || defined _WIN32
   260 					ext = ext ? "" : ".dll";
   261 					snprintf(full_path, sizeof(full_path)-1, "%s\\%s%s", g_ModPath.c_str(), file, ext);
   262 #else
   263 					ext = ext ? "" : "_i486.so";
   264 					snprintf(full_path, sizeof(full_path)-1, "%s/%s%s", g_ModPath.c_str(), file, ext);
   265 #endif
   266 				}
   267 
   268 				char error[255]={0};
   269 				bool already;
   270 				SourceMM::CPluginManager::CPlugin *pl;
   271 
   272 				PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error)-1);
   273 				pl = g_PluginMngr.FindById(id);
   274 				if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
   275 				{
   276 					Msg("Failed to load plugin %s (%s).\n", file, error);
   277 					return;
   278 				}
   279 
   280 				if (!already)
   281 				{
   282 					Msg("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
   283 				} else {
   284 					Msg("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
   285 				}
   286 				
   287 				return;
   288 			} else {
   289 				Msg("Usage: meta load <path>\n");
   290 
   291 				return;
   292 			}
   293 		} else if (strcmp(command, "unload") == 0) {
   294 			if (args >= 3)
   295 			{
   296 				int id = atoi(e->Cmd_Argv(2));
   297 				char error[255]={0};
   298 
   299 				if (!g_PluginMngr.Unload(id, false, error, sizeof(error)-1))
   300 				{
   301 					Msg("Unload failed: %s\n", error);
   302 					return;
   303 				}
   304 
   305 				Msg("Plugin %d unloaded.\n", id);
   306 
   307 				return;
   308 			} else {
   309 				Msg("Usage: meta unload <id>\n");
   310 
   311 				return;
   312 			}
   313 		} else if (strcmp(command, "force_unload") == 0) {
   314 			if (args >= 3)
   315 			{
   316 				int id = atoi(e->Cmd_Argv(2));
   317 				char error[255]={0};
   318 
   319 				if (!g_PluginMngr.Unload(id, false, error, sizeof(error)-1))
   320 				{
   321 					Msg("Force unload failed: %s\n", error);
   322 					return;
   323 				}
   324 
   325 				Msg("Plugin %d force unloaded.\n", id);
   326 
   327 				return;
   328 			} else {
   329 				Msg("Usage: meta force_unload <id>\n");
   330 
   331 				return;
   332 			}
   333 		} else if (strcmp(command, "clear") == 0) {
   334 			if (!g_PluginMngr.UnloadAll())
   335 			{
   336 				Msg("One or more plugins resisted removal (cleaned anyway).\n");
   337 				return;
   338 			} 
   339 
   340 			Msg("All plugins unloaded.\n");
   341 
   342 			return;
   343 		} else if (strcmp(command, "retry") == 0) {
   344 			if (args >= 3)
   345 			{
   346 				int id = atoi(e->Cmd_Argv(2));
   347 				char error[255];
   348 
   349 				if (!g_PluginMngr.Retry(id, error, sizeof(error)-1))
   350 				{
   351 					Msg("Error reloading plugin: %s\n", error);
   352 					return;
   353 				}
   354 
   355 				Msg("Plugin %d successfully reloaded.\n", id);
   356 
   357 				return;
   358 			} else {
   359 				Msg("Usage: meta retry <id>\n");
   360 
   361 				return;
   362 			}
   363 		}
   364 	}
   365 
   366 	Msg("Metamod:Source Menu\n");
   367 	Msg("usage: meta <command> [arguments]\n");
   368 	Msg("  clear        - Unload all plugins forcefully\n");
   369 	Msg("  credits      - About Metamod:Source\n");
   370 	Msg("  force_unload - Forcefully unload a plugin\n");
   371 	Msg("  game         - Information about GameDLL\n");
   372 	Msg("  info         - Information about a plugin\n");
   373 	Msg("  list         - List plugins\n");
   374 	Msg("  load         - Load a plugin\n");
   375 	Msg("  pause        - Pause a running plugin\n");
   376 	Msg("  refresh      - Reparse plugins file\n");
   377 	Msg("  retry        - Attempt to reload a plugin\n");
   378 	Msg("  unload       - Unload a loaded plugin\n");
   379 	Msg("  unpause      - Unpause a paused plugin\n");
   380 	Msg("  version      - Version information\n");
   381 	Msg("\n");
   382 }