metamod/mlist.cpp
author darope
Sat Aug 21 16:13:14 2004 +0000 (2004-08-21)
changeset 245 e2e3739f6ccd
parent 244 7b246a2ad39c
child 247 2b1bf332bf41
permissions -rw-r--r--
Merged with working branch
     1 // vi: set ts=4 sw=4 :
     2 // vim: set tw=75 :
     3 
     4 // mlist.cpp - functions for list of plugins (class MPluginList)
     5 
     6 /*
     7  * Copyright (c) 2001-2003 Will Day <[email protected]>
     8  *
     9  *    This file is part of Metamod.
    10  *
    11  *    Metamod is free software; you can redistribute it and/or modify it
    12  *    under the terms of the GNU General Public License as published by the
    13  *    Free Software Foundation; either version 2 of the License, or (at
    14  *    your option) any later version.
    15  *
    16  *    Metamod is distributed in the hope that it will be useful, but
    17  *    WITHOUT ANY WARRANTY; without even the implied warranty of
    18  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    19  *    General Public License for more details.
    20  *
    21  *    You should have received a copy of the GNU General Public License
    22  *    along with Metamod; if not, write to the Free Software Foundation,
    23  *    Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    24  *
    25  *    In addition, as a special exception, the author gives permission to
    26  *    link the code of this program with the Half-Life Game Engine ("HL
    27  *    Engine") and Modified Game Libraries ("MODs") developed by Valve,
    28  *    L.L.C ("Valve").  You must obey the GNU General Public License in all
    29  *    respects for all of the code used other than the HL Engine and MODs
    30  *    from Valve.  If you modify this file, you may extend this exception
    31  *    to your version of the file, but you are not obligated to do so.  If
    32  *    you do not wish to do so, delete this exception statement from your
    33  *    version.
    34  *
    35  */
    36 
    37 #include <errno.h>				// errno, etc
    38 
    39 #include <extdll.h>				// always
    40 
    41 #include "mlist.h"				// me
    42 #include "mplugin.h"			// class MPlugin
    43 #include "plinfo.h"				// plid_t, etc
    44 #include "commands_meta.h"		// cmd_meta_pluginlist, etc
    45 #include "metamod.h"			// GameDLL, etc
    46 #include "types_meta.h"			// mBOOL
    47 #include "log_meta.h"			// META_LOG, etc
    48 #include "osdep.h"				// win32 snprintf, normalize_pathname,
    49 
    50 // Constructor
    51 MPluginList::MPluginList(const char *ifile) 
    52 	: size(MAX_PLUGINS), endlist(0)
    53 {
    54 	int i;
    55 	// store filename of ini file
    56 	STRNCPY(inifile, ifile, sizeof(inifile));
    57 	// initialize array
    58 	memset(plist, 0, sizeof(plist));
    59 	for(i=0; i< size; i++) {
    60 		plist[i].index=i+1;		// 1-based
    61 	}
    62 	endlist=0;
    63 }
    64 
    65 // Find a plugin based on the plugin index #.
    66 // meta_errno values:
    67 //  - ME_ARGUMENT	invalid pindex
    68 //  - ME_NOTFOUND	couldn't find a matching plugin
    69 MPlugin *MPluginList::find(int pindex) {
    70 	MPlugin *pfound;
    71 	if(pindex <= 0)
    72 		RETURN_ERRNO(NULL, ME_ARGUMENT);
    73 	pfound=&plist[pindex-1];
    74 	if(pfound->status < PL_VALID)
    75 		RETURN_ERRNO(NULL, ME_NOTFOUND);
    76 	else
    77 		return(pfound);
    78 }
    79 
    80 // Find a plugin with the given plid.
    81 // meta_errno values:
    82 //  - ME_ARGUMENT	null plid_t
    83 //  - ME_NOTFOUND	couldn't find a matching plugin
    84 MPlugin *MPluginList::find(plid_t id) {
    85 	int i;
    86 
    87 	if(!id)
    88 		RETURN_ERRNO(NULL, ME_ARGUMENT);
    89 	for(i=0; i < endlist; i++) {
    90 		if(plist[i].status < PL_VALID)
    91 			continue;
    92 		if(plist[i].info == id)
    93 			return(&plist[i]);
    94 	}
    95 	RETURN_ERRNO(NULL, ME_NOTFOUND);
    96 }
    97 
    98 // Find a plugin with the given pathname.
    99 // meta_errno values:
   100 //  - ME_ARGUMENT	null path
   101 //  - ME_NOTFOUND	couldn't find a matching plugin
   102 MPlugin *MPluginList::find(const char *findpath) {
   103 	int i;
   104 
   105 	if(!findpath)
   106 		RETURN_ERRNO(NULL, ME_ARGUMENT);
   107 	META_DEBUG(8, ("Looking for loaded plugin with dlfnamepath: %s", findpath));
   108 	for(i=0; i < endlist; i++) {
   109 		META_DEBUG(9, ("Looking at: plugin %s loadedpath: %s", plist[i].file, plist[i].pathname));
   110 		if(plist[i].status < PL_VALID)
   111 			continue;
   112 		if(strmatch(plist[i].pathname, findpath)) {
   113 			META_DEBUG(8, ("Found loaded plugin %s", plist[i].file));
   114 			return(&plist[i]);
   115 		}
   116 	}
   117 	META_DEBUG(8, ("No loaded plugin found with path: %s", findpath));
   118 	RETURN_ERRNO(NULL, ME_NOTFOUND);
   119 }
   120 
   121 // Find a plugin that uses the given memory location.
   122 // meta_errno values:
   123 //  - ME_ARGUMENT	null memptr
   124 //  - ME_NOTFOUND	couldn't find a matching plugin
   125 //  - errno's from DLFNAME()
   126 MPlugin *MPluginList::find_memloc(void *memptr) {
   127 	const char *dlfile;
   128 
   129 	if(!memptr)
   130 		RETURN_ERRNO(NULL, ME_ARGUMENT);
   131 	if(!(dlfile=DLFNAME(memptr))) {
   132 		META_DEBUG(8, ("DLFNAME failed to find memloc %d", memptr));
   133 		// meta_errno should be already set in DLFNAME
   134 		return(NULL);
   135 	}
   136 	return(find(dlfile));
   137 }
   138 
   139 // Find a plugin with non-ambiguous prefix string matching desc, file, 
   140 // name, or logtag.
   141 // meta_errno values:
   142 //  - ME_ARGUMENT	null prefix
   143 //  - ME_NOTFOUND	couldn't find a matching plugin
   144 //  - ME_NOTUNIQ	found multiple matches; no unique match
   145 MPlugin *MPluginList::find_match(const char *prefix) {
   146 	int i, len;
   147 	MPlugin *iplug, *pfound;
   148 	char buf[NAME_MAX];
   149 
   150 	if(!prefix)
   151 		RETURN_ERRNO(NULL, ME_ARGUMENT);
   152 	pfound=NULL;
   153 	len=strlen(prefix);
   154 	snprintf(buf, sizeof(buf), "mm_%s", prefix);
   155 	for(i=0; i < endlist; i++) {
   156 		iplug=&plist[i];
   157 		if(iplug->status < PL_VALID)
   158 			continue;
   159 		if(iplug->info && strncasecmp(iplug->info->name, prefix, len) == 0) {
   160 			if(pfound)
   161 				RETURN_ERRNO(NULL, ME_NOTUNIQ);
   162 			pfound=iplug;
   163 			continue;
   164 		}
   165 		else if(strncasecmp(iplug->desc, prefix, len) == 0) {
   166 			if(pfound)
   167 				RETURN_ERRNO(NULL, ME_NOTUNIQ);
   168 			pfound=iplug;
   169 			continue;
   170 		}
   171 		else if(strncasecmp(iplug->file, prefix, len) == 0) {
   172 			if(pfound)
   173 				RETURN_ERRNO(NULL, ME_NOTUNIQ);
   174 			pfound=iplug;
   175 			continue;
   176 		}
   177 		else if(strncasecmp(iplug->file, buf, strlen(buf)) == 0) {
   178 			if(pfound)
   179 				RETURN_ERRNO(NULL, ME_NOTUNIQ);
   180 			pfound=iplug;
   181 			continue;
   182 		}
   183 		else if(iplug->info 
   184 				&& strncasecmp(iplug->info->logtag, prefix, len) == 0) 
   185 		{
   186 			if(pfound)
   187 				RETURN_ERRNO(NULL, ME_NOTUNIQ);
   188 			pfound=iplug;
   189 			continue;
   190 		}
   191 	}
   192 	if(pfound)
   193 		return(pfound);
   194 	else
   195 		RETURN_ERRNO(NULL, ME_NOTFOUND);
   196 }
   197 
   198 
   199 // Find a plugin with same file, logtag, desc or significant 
   200 // prefix of file. Uses the platform_match() method of MPlugin.
   201 // meta_errno values:
   202 //  - ME_ARGUMENT	null prefix
   203 //  - ME_NOTFOUND	couldn't find a matching plugin
   204 MPlugin *MPluginList::find_match(MPlugin *pmatch) {
   205 	int i;
   206 	MPlugin *iplug, *pfound;
   207 	if(!pmatch)
   208 		RETURN_ERRNO(NULL, ME_ARGUMENT);
   209 	pfound=NULL;
   210 	for(i=0; i < endlist; i++) {
   211 		iplug=&plist[i];
   212 		if(pmatch->platform_match(iplug)) {
   213 			pfound=iplug;
   214 			break;
   215 		}
   216 	}
   217 
   218 	if(pfound)
   219 		return(pfound);
   220 	else
   221 		RETURN_ERRNO(NULL, ME_NOTFOUND);
   222 }
   223 
   224 
   225 // Add a plugin to the list.
   226 // meta_errno values:
   227 //  - ME_MAXREACHED		reached max plugins
   228 MPlugin *MPluginList::add(MPlugin *padd) {
   229 	int i;
   230 	MPlugin *iplug;
   231 
   232 	// Find either:
   233 	//  - a slot in the list that's not being used
   234 	//  - the end of the list
   235 	for(i=0; i < endlist && plist[i].status != PL_EMPTY; i++);
   236 
   237 	// couldn't find a slot to use
   238 	if(i==size) {
   239 		META_ERROR("Couldn't add plugin '%s' to list; reached max plugins (%d)", 
   240 				padd->file, i);
   241 		RETURN_ERRNO(NULL, ME_MAXREACHED);
   242 	}
   243 
   244 	// if we found the end of the list, advance end marker
   245 	if(i==endlist)
   246 		endlist++;
   247 	iplug = &plist[i];
   248 
   249 	// copy filename into this free slot
   250 	STRNCPY(iplug->filename, padd->filename, sizeof(iplug->filename));
   251 	// Copy file offset ptr.
   252 	// Can't just copy ptr, as it points to offset in padd, which will go
   253 	// away; need to point to corresponding offset in iplug.
   254 	iplug->file = iplug->filename + (padd->file - padd->filename);
   255 	// copy description
   256 	STRNCPY(iplug->desc, padd->desc, sizeof(iplug->desc));
   257 	// copy pathname
   258 	STRNCPY(iplug->pathname, padd->pathname, sizeof(iplug->pathname));
   259 	// copy source
   260 	iplug->source=padd->source;
   261 	// copy status
   262 	iplug->status=padd->status;
   263 
   264 	return(iplug);
   265 }
   266 
   267 
   268 // Read plugins.ini at server startup.
   269 // meta_errno values:
   270 //  - ME_NOFILE		ini file missing or empty
   271 mBOOL MPluginList::ini_startup() {
   272 	FILE *fp;
   273 	char line[MAX_STRBUF_LEN];
   274 	int n, ln;
   275 	MPlugin *pmatch;
   276 
   277 	if(!valid_gamedir_file(inifile)) {
   278 		META_ERROR("ini: Metamod plugins file empty or missing: %s", inifile);
   279 		RETURN_ERRNO(mFALSE, ME_NOFILE);
   280 	}
   281 	full_gamedir_path(inifile, inifile);
   282 
   283 	fp=fopen(inifile, "r");
   284 	if(!fp) {
   285 		META_ERROR("ini: Unable to open plugins file '%s': %s", inifile, 
   286 				strerror(errno));
   287 		RETURN_ERRNO(mFALSE, ME_NOFILE);
   288 	}
   289 
   290 	META_LOG("ini: Begin reading plugins list: %s", inifile);
   291 	for(n=0, ln=1; 
   292 			!feof(fp) && fgets(line, sizeof(line), fp) && n < size; 
   293 			ln++) 
   294 	{
   295 		// Remove line terminations.
   296 		char *cp;
   297 		if((cp=strrchr(line, '\r')))
   298 			*cp='\0';
   299 		if((cp=strrchr(line, '\n')))
   300 			*cp='\0';
   301 		// Parse directly into next entry in array
   302 		if(!plist[n].ini_parseline(line)) {
   303 			if(meta_errno==ME_FORMAT)
   304 				META_ERROR("ini: Skipping malformed line %d of %s", ln, 
   305 						inifile);
   306 			continue;
   307 		}
   308 		// Check for a duplicate - an existing entry with this pathname.
   309 		if(find(plist[n].pathname)) {
   310 			// Should we check platform specific level here?
   311 			META_INFO("ini: Skipping duplicate plugin, line %d of %s: %s", 
   312 					ln, inifile, plist[n].pathname);
   313 			continue;
   314 		}
   315 		// Check for a matching platform with different platform specifics
   316 		// level.
   317 		if(NULL != (pmatch=find_match(&plist[n]))) {
   318 			if(pmatch->pfspecific >= plist[n].pfspecific) {
   319 				META_DEBUG(1, ("ini: Skipping plugin, line %d of %s: plugin with higher platform specific level already exists. (%d >= %d)",
   320                          ln, inifile, pmatch->pfspecific, plist[n].pfspecific)); 
   321 				continue;
   322 			}
   323 			META_DEBUG(1, ("ini: Plugin in line %d overrides existing plugin with lower platform specific level %d, ours %d",
   324 					ln, pmatch->pfspecific, plist[n].pfspecific));
   325 			memset(pmatch, 0, sizeof(MPlugin));
   326 		}
   327 		plist[n].action=PA_LOAD;
   328 		META_LOG("ini: Read plugin config for: %s", plist[n].desc);
   329 		n++;
   330 		endlist=n;		// mark end of list
   331 	}
   332 	META_LOG("ini: Finished reading plugins list: %s; Found %d plugins to load",
   333 			inifile, n);
   334 
   335 	fclose(fp);
   336 	if(!n) {
   337 		META_ERROR("ini: Warning; no plugins found to load?");
   338 	}
   339 	return(mTRUE);
   340 }
   341 
   342 // Re-read plugins.ini looking for added/deleted/changed plugins.
   343 // meta_errno values:
   344 //  - ME_NOFILE		ini file missing or empty
   345 mBOOL MPluginList::ini_refresh() {
   346 	FILE *fp;
   347 	char line[MAX_STRBUF_LEN];
   348 	int n, ln;
   349 	MPlugin pl_temp;
   350 	MPlugin *pl_found, *pl_added;
   351 
   352 	fp=fopen(inifile, "r");
   353 	if(!fp) {
   354 		META_ERROR("ini: Unable to open plugins file '%s': %s", inifile, 
   355 				strerror(errno));
   356 		RETURN_ERRNO(mFALSE, ME_NOFILE);
   357 	}
   358 
   359 	META_LOG("ini: Begin re-reading plugins list: %s", inifile);
   360 	for(n=0, ln=1; 
   361 			!feof(fp) && fgets(line, sizeof(line), fp) && n < size; 
   362 			ln++) 
   363 	{
   364 		// Remove line terminations.
   365 		char *cp;
   366 		if((cp=strrchr(line, '\r')))
   367 			*cp='\0';
   368 		if((cp=strrchr(line, '\n')))
   369 			*cp='\0';
   370 		// Parse into a temp plugin
   371 		memset(&pl_temp, 0, sizeof(pl_temp));
   372 		if(!pl_temp.ini_parseline(line)) {
   373 			if(meta_errno==ME_FORMAT)
   374 				META_ERROR("ini: Skipping malformed line %d of %s", 
   375 						ln, inifile);
   376 			continue;
   377 		}
   378 		// Try to find plugin with this pathname in the current list of
   379 		// plugins.
   380 		if(!(pl_found=find(pl_temp.pathname))) {
   381 			// Check for a matching platform with higher platform specifics
   382 			// level.
   383 			if(NULL != (pl_found=find_match(&pl_temp))) {
   384 				if(pl_found->pfspecific >= pl_temp.pfspecific) {
   385 					META_DEBUG(1, ("ini: Skipping plugin, line %d of %s: plugin with higher platform specific level already exists. (%d >= %d)",
   386                        		 ln, inifile, pl_found->pfspecific, pl_temp.pfspecific)); 
   387 					continue;
   388 				}
   389 				if(PA_LOAD == pl_found->action) {
   390 					META_DEBUG(1, ("ini: Plugin in line %d overrides loading of plugin with lower platform specific level %d, ours %d",
   391 								ln, pl_found->pfspecific, pl_temp.pfspecific));
   392 					memset(pl_found, 0, sizeof(MPlugin));
   393 				}
   394 				else {
   395 					META_DEBUG(1, ("ini: Plugin in line %d should override existing plugin with lower platform specific level %d, ours %d. Unable to comply.",
   396 								ln, pl_found->pfspecific, pl_temp.pfspecific));
   397 					continue;
   398 				}
   399 			}
   400 			// new plugin; add to list
   401 			if((pl_added=add(&pl_temp))) {
   402 				// try to load this plugin at the next opportunity
   403 				pl_added->action=PA_LOAD;
   404 			}
   405 			else
   406 				// error details logged in add()
   407 				continue;
   408 		}
   409 		else {
   410 			// This plugin is already in the current list of plugins.
   411 			// Pathname already matches.  Recopy desc, if specified in
   412 			// plugins.ini.
   413 			if(pl_temp.desc[0] != '<')
   414 				STRNCPY(pl_found->desc, pl_temp.desc, sizeof(pl_found->desc));
   415 
   416 			// Check the file to see if it looks like it's been modified
   417 			// since we last loaded it.
   418 			if(!pl_found->newer_file()) {
   419 				if(meta_errno==ME_NOFILE) {
   420 					META_ERROR("ini: Skipping plugin, couldn't stat file '%s': %s",
   421 							pl_found->pathname, strerror(errno));
   422 					continue;
   423 				}
   424 				else {
   425 					// File hasn't been updated.
   426 					// Keep plugin (don't let refresh() unload it).
   427 					pl_found->action=PA_KEEP;
   428 				}
   429 			}
   430 			// Newer file on disk.
   431 			else if(pl_found->status >= PL_OPENED)  {
   432 				META_DEBUG(2, ("ini: Plugin '%s' has newer file on disk", pl_found->desc));
   433 				pl_found->action=PA_RELOAD;
   434 			}
   435 			else
   436 				META_ERROR("ini: Plugin '%s' has newer file, but unexpected status (%s)",
   437 						pl_found->desc, pl_found->str_status());
   438 		}
   439 		if(NULL != pl_found) {
   440 			META_LOG("ini: Read plugin config for: %s", pl_found->desc);
   441 		}
   442 		else {
   443 			META_LOG("ini: Read plugin config for: %s", pl_temp.desc);
   444 		}
   445 		n++;
   446 	}
   447 	META_LOG("ini: Finished reading plugins list: %s; Found %d plugins", inifile, n);
   448 
   449 	fclose(fp);
   450 	if(!n) {
   451 		META_ERROR("ini: Warning; no plugins found to load?");
   452 	}
   453 	return(mTRUE);
   454 }
   455 
   456 // Load a plugin from a console command.
   457 // meta_errno values:
   458 //  - errno's from cmd_parseline()
   459 //  - errno's from resolve()
   460 //  - ME_ALREADY	this plugin already loaded
   461 //  - errno's from add()
   462 //  - errno's from load()
   463 mBOOL MPluginList::cmd_addload(const char *args) {
   464 	MPlugin pl_temp;
   465 	MPlugin *pl_found, *pl_added;
   466 
   467 	// XXX move back to comands_meta ?
   468 
   469 	// parse into a temp plugin
   470 	memset(&pl_temp, 0, sizeof(pl_temp));
   471 	if(pl_temp.cmd_parseline(args) != mTRUE) {
   472 		META_CONS("Couldn't parse 'meta load' arguments: %s", args);
   473 		// meta_errno should be already set in cmd_parseline()
   474 		return(mFALSE);
   475 	}
   476 
   477 	// resolve given path into a file; accepts various "shortcut"
   478 	// pathnames.
   479 	if(pl_temp.resolve() != mTRUE) {
   480 		// Couldn't find a matching file on disk
   481 		META_CONS("Couldn't resolve given path into a file: %s", 
   482 				pl_temp.file);
   483 		// meta_errno should be already set in resolve()
   484 		return(mFALSE);
   485 	}
   486 
   487 	// Try to find plugin with this pathname in the current list of
   488 	// plugins.
   489 	if((pl_found=find(pl_temp.pathname))) {
   490 		// Already in list
   491 		META_CONS("Plugin '%s' already in current list; file=%s desc='%s'", 
   492 				pl_temp.file, pl_found->file, pl_found->desc);
   493 		RETURN_ERRNO(mFALSE, ME_ALREADY);
   494 	}
   495 	// new plugin; add to list
   496 	if(!(pl_added=add(&pl_temp))) {
   497 		META_CONS("Couldn't add plugin '%s' to list; see log", pl_temp.desc);
   498 		// meta_errno should be already set in add()
   499 		return(mFALSE);
   500 	}
   501 
   502 	// try to load new plugin
   503 	pl_added->action=PA_LOAD;
   504 	if(!pl_added->load(PT_ANYTIME)) {
   505 		// load failed
   506 		if(meta_errno==ME_DELAYED) 
   507 			META_CONS("Loaded plugin '%s', but will wait to become active, %s", 
   508 					pl_added->desc, pl_added->str_loadable(SL_ALLOWED));
   509 		else if(meta_errno==ME_NOTALLOWED) {
   510 			META_CONS("Plugin '%s' couldn't attach; only allowed %s", 
   511 					pl_added->desc, pl_added->str_loadable(SL_ALLOWED));
   512 			pl_added->clear();
   513 		}
   514 		else if(pl_added->status == PL_OPENED)
   515 			META_CONS("Opened plugin '%s', but failed to attach; see log", pl_added->desc);
   516 		else
   517 			META_CONS("Couldn't load plugin '%s'; see log", pl_added->desc);
   518 		show();
   519 		// meta_errno should be already set in load()
   520 		return(mFALSE);
   521 	}
   522 	META_CONS("Loaded plugin '%s' successfully", pl_added->desc);
   523 	show();
   524 	return(mTRUE);
   525 }
   526 
   527 // Load plugins at startup.
   528 // meta_errno values:
   529 //  - errno's from ini_startup()
   530 mBOOL MPluginList::load() {
   531 	int i, n;
   532 
   533 	if (!ini_startup()) {
   534 		META_ERROR("Problem loading plugins.ini: %s", inifile);
   535 		// meta_errno should be already set in ini_startup()
   536 		return(mFALSE);
   537 	}
   538 
   539 	META_LOG("dll: Loading plugins...");
   540 	for(i=0, n=0; i < endlist; i++) {
   541 		if(plist[i].status < PL_VALID)
   542 			continue;
   543 		if(plist[i].load(PT_STARTUP) == mTRUE)
   544 			n++;
   545 		else
   546 			// all plugins should be loadable at startup...
   547 			META_ERROR("dll: Failed to load plugin '%s'", plist[i].file);
   548 	}
   549 	META_LOG("dll: Finished loading %d plugins", n);
   550 	return(mTRUE);
   551 }
   552 
   553 // Update list of loaded plugins from ini file, and load any new/changed plugins.
   554 // meta_errno values:
   555 //  - errno's from ini_refresh()
   556 mBOOL MPluginList::refresh(PLUG_LOADTIME now) {
   557 	int i, ndone=0, nkept=0, nloaded=0, nunloaded=0, nreloaded=0, ndelayed=0;
   558 	MPlugin *iplug;
   559 
   560 	if (!ini_refresh()) {
   561 		META_ERROR("dll: Problem reloading plugins.ini: %s", inifile);
   562 		// meta_errno should be already set in ini_refresh()
   563 		return(mFALSE);
   564 	}
   565 
   566 	META_LOG("dll: Updating plugins...");
   567 	for(i=0; i < endlist; i++) {
   568 		iplug=&plist[i];
   569 		if(iplug->status < PL_VALID)
   570 			continue;
   571 		switch(iplug->action) {
   572 			case PA_KEEP:
   573 				META_DEBUG(1, ("Keeping plugin '%s'", iplug->desc));
   574 				iplug->action=PA_NONE;
   575 				nkept++;
   576 				break;
   577 			case PA_LOAD:
   578 				META_DEBUG(1, ("Loading plugin '%s'", iplug->desc));
   579 				if(iplug->load(now))
   580 					nloaded++;
   581 				else if(meta_errno==ME_DELAYED)
   582 					ndelayed++;
   583 				break;
   584 			case PA_RELOAD:
   585 				META_DEBUG(1, ("Reloading plugin '%s'", iplug->desc));
   586 				if(iplug->reload(now, PNL_FILE_NEWER))
   587 					nreloaded++;
   588 				else if(meta_errno==ME_DELAYED)
   589 					ndelayed++;
   590 				break;
   591 			case PA_NONE:
   592 				// If previously loaded from ini, but apparently removed from new ini.
   593 				if(iplug->source==PS_INI && iplug->status >= PL_RUNNING) {
   594 					META_DEBUG(1, ("Unloading plugin '%s'", iplug->desc));
   595 					iplug->action=PA_UNLOAD;
   596 					if(iplug->unload(now, PNL_INI_DELETED))
   597 						nunloaded++;
   598 					else if(meta_errno==ME_DELAYED)
   599 						ndelayed++;
   600 				}
   601 				break;
   602 			case PA_ATTACH:
   603 				// Previously requested attach, but was delayed?
   604 				META_DEBUG(1, ("Retrying attach plugin '%s'", iplug->desc));
   605 				if(iplug->retry(now, PNL_DELAYED))
   606 					nloaded++;
   607 				else if(meta_errno==ME_DELAYED)
   608 					ndelayed++;
   609 				break;
   610 			case PA_UNLOAD:
   611 				// Previously requested unload, but was delayed?
   612 				META_DEBUG(1, ("Retrying unload plugin '%s'", iplug->desc));
   613 				if(iplug->retry(now, PNL_DELAYED))
   614 					nunloaded++;
   615 				else if(meta_errno==ME_DELAYED)
   616 					ndelayed++;
   617 				break;
   618 			case PA_NULL:
   619 				META_ERROR("dll: Unexpected action for plugin '%s': '%s'", iplug->desc, iplug->str_action());
   620 				break;
   621 			default:
   622 				META_ERROR("dll: Unrecognized action for plugin '%s': '%s'", iplug->desc, iplug->str_action());
   623 				break;
   624 		}
   625 		ndone++;
   626 	}
   627 	META_LOG("dll: Finished updating %d plugins; kept %d, loaded %d, unloaded %d, reloaded %d, delayed %d", 
   628 			ndone, nkept, nloaded, nunloaded, nreloaded, ndelayed);
   629 	return(mTRUE);
   630 }
   631 
   632 // Re-enable any plugins currently paused.
   633 // meta_errno values:
   634 //  - none
   635 void MPluginList::unpause_all(void) {
   636 	int i;
   637 	MPlugin *iplug;
   638 	for(i=0; i < endlist; i++) {
   639 		iplug=&plist[i];
   640 		if(iplug->status==PL_PAUSED)
   641 			iplug->unpause();
   642 	}
   643 }
   644 
   645 // Retry any pending actions on plugins, for instance load/unload delayed
   646 // until changelevel.
   647 // meta_errno values:
   648 //  - none
   649 void MPluginList::retry_all(PLUG_LOADTIME now) {
   650 	int i;
   651 	MPlugin *iplug;
   652 	for(i=0; i < endlist; i++) {
   653 		iplug=&plist[i];
   654 		if(iplug->action != PA_NONE)
   655 			iplug->retry(now, PNL_DELAYED);
   656 	}
   657 }
   658 
   659 // List plugins and information about them in a formatted table.
   660 // meta_errno values:
   661 //  - none
   662 void MPluginList::show() {
   663 	int i, n=0, r=0;
   664 	MPlugin *pl;
   665 	char desc[15+1], file[16+1], vers[7+1];		// plus 1 for term null
   666 
   667 	META_CONS("Currently loaded plugins:");
   668 	META_CONS("  %*s  %-*s  %-4s %-4s  %-*s  v%-*s  %-3s  %-5s %-5s",
   669 			WIDTH_MAX_PLUGINS, "",
   670 			sizeof(desc)-1, "description",
   671 			"stat", "pend",
   672 			sizeof(file)-1, "file", sizeof(vers)-1, "ers",
   673 			"src", "load ", "unlod");
   674 	for(i=0; i < endlist; i++) {
   675 		pl=&plist[i];
   676 		if(pl->status < PL_VALID)
   677 			continue;
   678 		STRNCPY(desc, pl->desc, sizeof(desc));
   679 		STRNCPY(file, pl->file, sizeof(file));
   680 		if(pl->info && pl->info->version) STRNCPY(vers, pl->info->version, sizeof(vers));
   681 		else STRNCPY(vers, " -", sizeof(vers));
   682 		META_CONS(" [%*d] %-*s  %-4s %-4s  %-*s  v%-*s  %-3s  %-5s %-5s",
   683 				WIDTH_MAX_PLUGINS, pl->index, 
   684 				sizeof(desc)-1, desc,
   685 				pl->str_status(ST_SHOW), pl->str_action(SA_SHOW),
   686 				sizeof(file)-1, file, sizeof(vers)-1, vers,
   687 				pl->str_source(SO_SHOW),
   688 				pl->str_loadable(SL_SHOW), pl->str_unloadable(SL_SHOW));
   689 		if(pl->status == PL_RUNNING)
   690 			r++;
   691 		n++;
   692 	}
   693 	META_CONS("%d plugins, %d running", n, r);
   694 }
   695 
   696 // List plugins and information to Player/client entity.  Differs from the
   697 // "meta list" console command in that:
   698 //  - Shows only "running" plugins, skipping any failed or paused plugins.
   699 //  - Limited info about each plugin, mostly the "public" info (name, author, 
   700 //    etc).
   701 // meta_errno values:
   702 //  - none
   703 void MPluginList::show_client(edict_t *pEntity) {
   704 	int i, n=0;
   705 	MPlugin *pl;
   706 	META_CLIENT(pEntity, "Currently running plugins:");
   707 	for(i=0; i < endlist; i++) {
   708 		pl=&plist[i];
   709 		if(pl->status != PL_RUNNING)
   710 			continue;
   711 		n++;
   712 		META_CLIENT(pEntity, " [%3d] %s, v%s, %s, by %s, see %s",
   713 				n, 
   714 				pl->info->name ? pl->info->name : "<unknown>",
   715 				pl->info->version ? pl->info->version : "<?>",
   716 				pl->info->date ? pl->info->date : "<../../..>",
   717 				pl->info->author ? pl->info->author : "<unknown>",
   718 				pl->info->url ? pl->info->url : "<unknown>");
   719 	}
   720 	META_CLIENT(pEntity, "%d plugins", n);
   721 }