metamod/mlist.cpp
author darope
Tue Aug 23 20:31:28 2005 +0000 (2005-08-23)
changeset 248 f72386298cba
parent 247 2b1bf332bf41
permissions -rw-r--r--
Merged with working branch (bt-release-1_17_4) for release 1.18
     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 handle.
    66 // meta_errno values:
    67 //  - ME_ARGUMENT	invalid pindex
    68 //  - ME_NOTFOUND	couldn't find a matching plugin
    69 MPlugin *MPluginList::find(DLHANDLE handle)
    70 {
    71 	int i;
    72 
    73 	if(!handle)
    74 		RETURN_ERRNO(NULL, ME_ARGUMENT);
    75 	for(i=0; i < endlist; i++) {
    76 		if(plist[i].status < PL_VALID)
    77 			continue;
    78 		if(plist[i].handle == handle)
    79 			return(&plist[i]);
    80 	}
    81 	RETURN_ERRNO(NULL, ME_NOTFOUND);
    82 }
    83 
    84 // Find a plugin based on the plugin index #.
    85 // meta_errno values:
    86 //  - ME_ARGUMENT	invalid pindex
    87 //  - ME_NOTFOUND	couldn't find a matching plugin
    88 MPlugin *MPluginList::find(int pindex) {
    89 	MPlugin *pfound;
    90 	if(pindex <= 0)
    91 		RETURN_ERRNO(NULL, ME_ARGUMENT);
    92 	pfound=&plist[pindex-1];
    93 	if(pfound->status < PL_VALID)
    94 		RETURN_ERRNO(NULL, ME_NOTFOUND);
    95 	else
    96 		return(pfound);
    97 }
    98 
    99 // Find a plugin with the given plid.
   100 // meta_errno values:
   101 //  - ME_ARGUMENT	null plid_t
   102 //  - ME_NOTFOUND	couldn't find a matching plugin
   103 MPlugin *MPluginList::find(plid_t id) {
   104 	int i;
   105 
   106 	if(!id)
   107 		RETURN_ERRNO(NULL, ME_ARGUMENT);
   108 	for(i=0; i < endlist; i++) {
   109 		if(plist[i].status < PL_VALID)
   110 			continue;
   111 		if(plist[i].info == id)
   112 			return(&plist[i]);
   113 	}
   114 	RETURN_ERRNO(NULL, ME_NOTFOUND);
   115 }
   116 
   117 // Find a plugin with the given pathname.
   118 // meta_errno values:
   119 //  - ME_ARGUMENT	null path
   120 //  - ME_NOTFOUND	couldn't find a matching plugin
   121 MPlugin *MPluginList::find(const char *findpath) {
   122 	int i;
   123 
   124 	if(!findpath)
   125 		RETURN_ERRNO(NULL, ME_ARGUMENT);
   126 	META_DEBUG(8, ("Looking for loaded plugin with dlfnamepath: %s", findpath));
   127 	for(i=0; i < endlist; i++) {
   128 		META_DEBUG(9, ("Looking at: plugin %s loadedpath: %s", plist[i].file, plist[i].pathname));
   129 		if(plist[i].status < PL_VALID)
   130 			continue;
   131 		if(strmatch(plist[i].pathname, findpath)) {
   132 			META_DEBUG(8, ("Found loaded plugin %s", plist[i].file));
   133 			return(&plist[i]);
   134 		}
   135 	}
   136 	META_DEBUG(8, ("No loaded plugin found with path: %s", findpath));
   137 	RETURN_ERRNO(NULL, ME_NOTFOUND);
   138 }
   139 
   140 // Find a plugin that uses the given memory location.
   141 // meta_errno values:
   142 //  - ME_ARGUMENT	null memptr
   143 //  - ME_NOTFOUND	couldn't find a matching plugin
   144 //  - errno's from DLFNAME()
   145 MPlugin *MPluginList::find_memloc(void *memptr) {
   146 	const char *dlfile;
   147 
   148 	if(!memptr)
   149 		RETURN_ERRNO(NULL, ME_ARGUMENT);
   150 	if(!(dlfile=DLFNAME(memptr))) {
   151 		META_DEBUG(8, ("DLFNAME failed to find memloc %d", memptr));
   152 		// meta_errno should be already set in DLFNAME
   153 		return(NULL);
   154 	}
   155 	return(find(dlfile));
   156 }
   157 
   158 // Find a plugin with non-ambiguous prefix string matching desc, file, 
   159 // name, or logtag.
   160 // meta_errno values:
   161 //  - ME_ARGUMENT	null prefix
   162 //  - ME_NOTFOUND	couldn't find a matching plugin
   163 //  - ME_NOTUNIQ	found multiple matches; no unique match
   164 MPlugin *MPluginList::find_match(const char *prefix) {
   165 	int i, len;
   166 	MPlugin *iplug, *pfound;
   167 	char buf[NAME_MAX];
   168 
   169 	if(!prefix)
   170 		RETURN_ERRNO(NULL, ME_ARGUMENT);
   171 	pfound=NULL;
   172 	len=strlen(prefix);
   173 	snprintf(buf, sizeof(buf), "mm_%s", prefix);
   174 	for(i=0; i < endlist; i++) {
   175 		iplug=&plist[i];
   176 		if(iplug->status < PL_VALID)
   177 			continue;
   178 		if(iplug->info && strncasecmp(iplug->info->name, prefix, len) == 0) {
   179 			if(pfound)
   180 				RETURN_ERRNO(NULL, ME_NOTUNIQ);
   181 			pfound=iplug;
   182 			continue;
   183 		}
   184 		else if(strncasecmp(iplug->desc, prefix, len) == 0) {
   185 			if(pfound)
   186 				RETURN_ERRNO(NULL, ME_NOTUNIQ);
   187 			pfound=iplug;
   188 			continue;
   189 		}
   190 		else if(strncasecmp(iplug->file, prefix, len) == 0) {
   191 			if(pfound)
   192 				RETURN_ERRNO(NULL, ME_NOTUNIQ);
   193 			pfound=iplug;
   194 			continue;
   195 		}
   196 		else if(strncasecmp(iplug->file, buf, strlen(buf)) == 0) {
   197 			if(pfound)
   198 				RETURN_ERRNO(NULL, ME_NOTUNIQ);
   199 			pfound=iplug;
   200 			continue;
   201 		}
   202 		else if(iplug->info 
   203 				&& strncasecmp(iplug->info->logtag, prefix, len) == 0) 
   204 		{
   205 			if(pfound)
   206 				RETURN_ERRNO(NULL, ME_NOTUNIQ);
   207 			pfound=iplug;
   208 			continue;
   209 		}
   210 	}
   211 	if(pfound)
   212 		return(pfound);
   213 	else
   214 		RETURN_ERRNO(NULL, ME_NOTFOUND);
   215 }
   216 
   217 
   218 // Find a plugin with same file, logtag, desc or significant 
   219 // prefix of file. Uses the platform_match() method of MPlugin.
   220 // meta_errno values:
   221 //  - ME_ARGUMENT	null prefix
   222 //  - ME_NOTFOUND	couldn't find a matching plugin
   223 MPlugin *MPluginList::find_match(MPlugin *pmatch) {
   224 	int i;
   225 	MPlugin *iplug, *pfound;
   226 	if(!pmatch)
   227 		RETURN_ERRNO(NULL, ME_ARGUMENT);
   228 	pfound=NULL;
   229 	for(i=0; i < endlist; i++) {
   230 		iplug=&plist[i];
   231 		if(pmatch->platform_match(iplug)) {
   232 			pfound=iplug;
   233 			break;
   234 		}
   235 	}
   236 
   237 	if(pfound)
   238 		return(pfound);
   239 	else
   240 		RETURN_ERRNO(NULL, ME_NOTFOUND);
   241 }
   242 
   243 MPlugin * MPluginList::plugin_addload(plid_t plid, const char *fname, PLUG_LOADTIME now)
   244 {
   245 	MPlugin pl_temp;
   246 	MPlugin *pl_found, *pl_added, *pl_loader;
   247 
   248 	if ( !(pl_loader=find(plid)) )
   249 	{
   250 		META_DEBUG(1, ("Couldn't find plugin that gave this loading request!"));
   251 		RETURN_ERRNO(NULL, ME_BADREQ);
   252 	}
   253 
   254 	memset(&pl_temp, 0, sizeof(pl_temp));
   255 
   256 	if (!pl_temp.plugin_parseline(fname, pl_loader->index))
   257 	{
   258 		RETURN_ERRNO(NULL, ME_NOTFOUND);
   259 	}
   260 
   261 	if (pl_temp.resolve() != mTRUE)
   262 	{
   263 		META_DEBUG(1, ("Couldn't resolve given path into a file: %s", pl_temp.file));
   264 		RETURN_ERRNO(NULL, ME_NOTFOUND);
   265 	}
   266 
   267 	if ( (pl_found=find(pl_temp.pathname)) )
   268 	{
   269 		META_DEBUG(1, ("Plugin '%s' already in current list; file=%s desc='%s'", 
   270 				pl_temp.file, pl_found->file, pl_found->desc));
   271 		RETURN_ERRNO(NULL, ME_ALREADY);
   272 	}
   273 
   274 	if (!(pl_added=add(&pl_temp)))
   275 	{
   276 		META_DEBUG(1, ("Couldn't add plugin '%s' to list; see log", pl_temp.desc));
   277 		return NULL;
   278 	}
   279 
   280 	pl_added->action = PA_LOAD;
   281 	if (!pl_added->load(now))
   282 	{
   283 		if (meta_errno==ME_NOTALLOWED || meta_errno==ME_DELAYED)
   284 		{
   285 			META_DEBUG(1, ("Plugin '%s' couldn't attach; only allowed %s", 
   286 					pl_added->desc, pl_added->str_loadable(SL_ALLOWED)));
   287 			pl_added->clear();
   288 		} else if (pl_added->status == PL_OPENED) {
   289 			META_DEBUG(1, ("Opened plugin '%s', but failed to attach; see log", pl_added->desc));
   290 		} else {
   291 			META_DEBUG(1, ("Couldn't load plugin '%s'; see log", pl_added->desc));
   292 		}
   293 		return NULL;
   294 	}
   295 
   296 	META_DEBUG(1, ("Loaded plugin '%s' successfully", pl_added->desc));
   297 	meta_errno = ME_NOERROR;
   298 
   299 	return pl_added;
   300 }
   301 
   302 // Add a plugin to the list.
   303 // meta_errno values:
   304 //  - ME_MAXREACHED		reached max plugins
   305 MPlugin *MPluginList::add(MPlugin *padd) {
   306 	int i;
   307 	MPlugin *iplug;
   308 
   309 	// Find either:
   310 	//  - a slot in the list that's not being used
   311 	//  - the end of the list
   312 	for(i=0; i < endlist && plist[i].status != PL_EMPTY; i++);
   313 
   314 	// couldn't find a slot to use
   315 	if(i==size) {
   316 		META_ERROR("Couldn't add plugin '%s' to list; reached max plugins (%d)", 
   317 				padd->file, i);
   318 		RETURN_ERRNO(NULL, ME_MAXREACHED);
   319 	}
   320 
   321 	// if we found the end of the list, advance end marker
   322 	if(i==endlist)
   323 		endlist++;
   324 	iplug = &plist[i];
   325 
   326 	// copy filename into this free slot
   327 	STRNCPY(iplug->filename, padd->filename, sizeof(iplug->filename));
   328 	// Copy file offset ptr.
   329 	// Can't just copy ptr, as it points to offset in padd, which will go
   330 	// away; need to point to corresponding offset in iplug.
   331 	iplug->file = iplug->filename + (padd->file - padd->filename);
   332 	// copy description
   333 	STRNCPY(iplug->desc, padd->desc, sizeof(iplug->desc));
   334 	// copy pathname
   335 	STRNCPY(iplug->pathname, padd->pathname, sizeof(iplug->pathname));
   336 	// copy source
   337 	iplug->source=padd->source;
   338 	// copy status
   339 	iplug->status=padd->status;	
   340 	//copy other things
   341 	iplug->source_plugin_index = padd->source_plugin_index;
   342 
   343 	return(iplug);
   344 }
   345 
   346 
   347 // Read plugins.ini at server startup.
   348 // meta_errno values:
   349 //  - ME_NOFILE		ini file missing or empty
   350 mBOOL MPluginList::ini_startup() {
   351 	FILE *fp;
   352 	char line[MAX_STRBUF_LEN];
   353 	int n, ln;
   354 	MPlugin *pmatch;
   355 
   356 	if(!valid_gamedir_file(inifile)) {
   357 		META_ERROR("ini: Metamod plugins file empty or missing: %s", inifile);
   358 		RETURN_ERRNO(mFALSE, ME_NOFILE);
   359 	}
   360 	full_gamedir_path(inifile, inifile);
   361 
   362 	fp=fopen(inifile, "r");
   363 	if(!fp) {
   364 		META_ERROR("ini: Unable to open plugins file '%s': %s", inifile, 
   365 				strerror(errno));
   366 		RETURN_ERRNO(mFALSE, ME_NOFILE);
   367 	}
   368 
   369 	META_LOG("ini: Begin reading plugins list: %s", inifile);
   370 	for(n=0, ln=1; 
   371 			!feof(fp) && fgets(line, sizeof(line), fp) && n < size; 
   372 			ln++) 
   373 	{
   374 		// Remove line terminations.
   375 		char *cp;
   376 		if((cp=strrchr(line, '\r')))
   377 			*cp='\0';
   378 		if((cp=strrchr(line, '\n')))
   379 			*cp='\0';
   380 		// Parse directly into next entry in array
   381 		if(!plist[n].ini_parseline(line)) {
   382 			if(meta_errno==ME_FORMAT)
   383 				META_ERROR("ini: Skipping malformed line %d of %s", ln, 
   384 						inifile);
   385 			continue;
   386 		}
   387 		// Check for a duplicate - an existing entry with this pathname.
   388 		if(find(plist[n].pathname)) {
   389 			// Should we check platform specific level here?
   390 			META_INFO("ini: Skipping duplicate plugin, line %d of %s: %s", 
   391 					ln, inifile, plist[n].pathname);
   392 			continue;
   393 		}
   394 		// Check for a matching platform with different platform specifics
   395 		// level.
   396 		if(NULL != (pmatch=find_match(&plist[n]))) {
   397 			if(pmatch->pfspecific >= plist[n].pfspecific) {
   398 				META_DEBUG(1, ("ini: Skipping plugin, line %d of %s: plugin with higher platform specific level already exists. (%d >= %d)",
   399                          ln, inifile, pmatch->pfspecific, plist[n].pfspecific)); 
   400 				continue;
   401 			}
   402 			META_DEBUG(1, ("ini: Plugin in line %d overrides existing plugin with lower platform specific level %d, ours %d",
   403 					ln, pmatch->pfspecific, plist[n].pfspecific));
   404 			int _index = pmatch->index;
   405 			memset(pmatch, 0, sizeof(MPlugin));
   406 			pmatch->index = _index;
   407 		}
   408 		plist[n].action=PA_LOAD;
   409 		META_LOG("ini: Read plugin config for: %s", plist[n].desc);
   410 		n++;
   411 		endlist=n;		// mark end of list
   412 	}
   413 	META_LOG("ini: Finished reading plugins list: %s; Found %d plugins to load",
   414 			inifile, n);
   415 
   416 	fclose(fp);
   417 	if(!n) {
   418 		META_ERROR("ini: Warning; no plugins found to load?");
   419 	}
   420 	return(mTRUE);
   421 }
   422 
   423 // Re-read plugins.ini looking for added/deleted/changed plugins.
   424 // meta_errno values:
   425 //  - ME_NOFILE		ini file missing or empty
   426 mBOOL MPluginList::ini_refresh() {
   427 	FILE *fp;
   428 	char line[MAX_STRBUF_LEN];
   429 	int n, ln;
   430 	MPlugin pl_temp;
   431 	MPlugin *pl_found, *pl_added;
   432 
   433 	fp=fopen(inifile, "r");
   434 	if(!fp) {
   435 		META_ERROR("ini: Unable to open plugins file '%s': %s", inifile, 
   436 				strerror(errno));
   437 		RETURN_ERRNO(mFALSE, ME_NOFILE);
   438 	}
   439 
   440 	META_LOG("ini: Begin re-reading plugins list: %s", inifile);
   441 	for(n=0, ln=1; 
   442 			!feof(fp) && fgets(line, sizeof(line), fp) && n < size; 
   443 			ln++) 
   444 	{
   445 		// Remove line terminations.
   446 		char *cp;
   447 		if((cp=strrchr(line, '\r')))
   448 			*cp='\0';
   449 		if((cp=strrchr(line, '\n')))
   450 			*cp='\0';
   451 		// Parse into a temp plugin
   452 		memset(&pl_temp, 0, sizeof(pl_temp));
   453 		if(!pl_temp.ini_parseline(line)) {
   454 			if(meta_errno==ME_FORMAT)
   455 				META_ERROR("ini: Skipping malformed line %d of %s", 
   456 						ln, inifile);
   457 			continue;
   458 		}
   459 		// Try to find plugin with this pathname in the current list of
   460 		// plugins.
   461 		if(!(pl_found=find(pl_temp.pathname))) {
   462 			// Check for a matching platform with higher platform specifics
   463 			// level.
   464 			if(NULL != (pl_found=find_match(&pl_temp))) {
   465 				if(pl_found->pfspecific >= pl_temp.pfspecific) {
   466 					META_DEBUG(1, ("ini: Skipping plugin, line %d of %s: plugin with higher platform specific level already exists. (%d >= %d)",
   467                        		 ln, inifile, pl_found->pfspecific, pl_temp.pfspecific)); 
   468 					continue;
   469 				}
   470 				if(PA_LOAD == pl_found->action) {
   471 					META_DEBUG(1, ("ini: Plugin in line %d overrides loading of plugin with lower platform specific level %d, ours %d",
   472 								ln, pl_found->pfspecific, pl_temp.pfspecific));
   473 					int _index = pl_found->index;
   474 					memset(pl_found, 0, sizeof(MPlugin));
   475 					pl_found->index = _index;
   476 				}
   477 				else {
   478 					META_DEBUG(1, ("ini: Plugin in line %d should override existing plugin with lower platform specific level %d, ours %d. Unable to comply.",
   479 								ln, pl_found->pfspecific, pl_temp.pfspecific));
   480 					continue;
   481 				}
   482 			}
   483 			// new plugin; add to list
   484 			if((pl_added=add(&pl_temp))) {
   485 				// try to load this plugin at the next opportunity
   486 				pl_added->action=PA_LOAD;
   487 			}
   488 			else
   489 				// error details logged in add()
   490 				continue;
   491 		}
   492 		else {
   493 			// This plugin is already in the current list of plugins.
   494 			// Pathname already matches.  Recopy desc, if specified in
   495 			// plugins.ini.
   496 			if(pl_temp.desc[0] != '<')
   497 				STRNCPY(pl_found->desc, pl_temp.desc, sizeof(pl_found->desc));
   498 
   499 			// Check the file to see if it looks like it's been modified
   500 			// since we last loaded it.
   501 			if(!pl_found->newer_file()) {
   502 				if(meta_errno==ME_NOFILE) {
   503 					META_ERROR("ini: Skipping plugin, couldn't stat file '%s': %s",
   504 							pl_found->pathname, strerror(errno));
   505 					continue;
   506 				}
   507 				else {
   508 					// File hasn't been updated.
   509 					// Keep plugin (don't let refresh() unload it).
   510 					pl_found->action=PA_KEEP;
   511 				}
   512 			}
   513 			// Newer file on disk.
   514 			else if(pl_found->status >= PL_OPENED)  {
   515 				META_DEBUG(2, ("ini: Plugin '%s' has newer file on disk", pl_found->desc));
   516 				pl_found->action=PA_RELOAD;
   517 			}
   518 			else
   519 				META_ERROR("ini: Plugin '%s' has newer file, but unexpected status (%s)",
   520 						pl_found->desc, pl_found->str_status());
   521 		}
   522 		if(NULL != pl_found) {
   523 			META_LOG("ini: Read plugin config for: %s", pl_found->desc);
   524 		}
   525 		else {
   526 			META_LOG("ini: Read plugin config for: %s", pl_temp.desc);
   527 		}
   528 		n++;
   529 	}
   530 	META_LOG("ini: Finished reading plugins list: %s; Found %d plugins", inifile, n);
   531 
   532 	fclose(fp);
   533 	if(!n) {
   534 		META_ERROR("ini: Warning; no plugins found to load?");
   535 	}
   536 	return(mTRUE);
   537 }
   538 
   539 // Load a plugin from a console command.
   540 // meta_errno values:
   541 //  - errno's from cmd_parseline()
   542 //  - errno's from resolve()
   543 //  - ME_ALREADY	this plugin already loaded
   544 //  - errno's from add()
   545 //  - errno's from load()
   546 mBOOL MPluginList::cmd_addload(const char *args) {
   547 	MPlugin pl_temp;
   548 	MPlugin *pl_found, *pl_added;
   549 
   550 	// XXX move back to comands_meta ?
   551 
   552 	// parse into a temp plugin
   553 	memset(&pl_temp, 0, sizeof(pl_temp));
   554 	if(pl_temp.cmd_parseline(args) != mTRUE) {
   555 		META_CONS("Couldn't parse 'meta load' arguments: %s", args);
   556 		// meta_errno should be already set in cmd_parseline()
   557 		return(mFALSE);
   558 	}
   559 
   560 	// resolve given path into a file; accepts various "shortcut"
   561 	// pathnames.
   562 	if(pl_temp.resolve() != mTRUE) {
   563 		// Couldn't find a matching file on disk
   564 		META_CONS("Couldn't resolve given path into a file: %s", 
   565 				pl_temp.file);
   566 		// meta_errno should be already set in resolve()
   567 		return(mFALSE);
   568 	}
   569 
   570 	// Try to find plugin with this pathname in the current list of
   571 	// plugins.
   572 	if((pl_found=find(pl_temp.pathname))) {
   573 		// Already in list
   574 		META_CONS("Plugin '%s' already in current list; file=%s desc='%s'", 
   575 				pl_temp.file, pl_found->file, pl_found->desc);
   576 		RETURN_ERRNO(mFALSE, ME_ALREADY);
   577 	}
   578 	// new plugin; add to list
   579 	if(!(pl_added=add(&pl_temp))) {
   580 		META_CONS("Couldn't add plugin '%s' to list; see log", pl_temp.desc);
   581 		// meta_errno should be already set in add()
   582 		return(mFALSE);
   583 	}
   584 
   585 	// try to load new plugin
   586 	pl_added->action=PA_LOAD;
   587 	if(!pl_added->load(PT_ANYTIME)) {
   588 		// load failed
   589 		if(meta_errno==ME_DELAYED) 
   590 			META_CONS("Loaded plugin '%s', but will wait to become active, %s", 
   591 					pl_added->desc, pl_added->str_loadable(SL_ALLOWED));
   592 		else if(meta_errno==ME_NOTALLOWED) {
   593 			META_CONS("Plugin '%s' couldn't attach; only allowed %s", 
   594 					pl_added->desc, pl_added->str_loadable(SL_ALLOWED));
   595 			pl_added->clear();
   596 		}
   597 		else if(pl_added->status == PL_OPENED)
   598 			META_CONS("Opened plugin '%s', but failed to attach; see log", pl_added->desc);
   599 		else
   600 			META_CONS("Couldn't load plugin '%s'; see log", pl_added->desc);
   601 		show(0);
   602 		// meta_errno should be already set in load()
   603 		return(mFALSE);
   604 	}
   605 	META_CONS("Loaded plugin '%s' successfully", pl_added->desc);
   606 	show(0);
   607 	return(mTRUE);
   608 }
   609 
   610 // Load plugins at startup.
   611 // meta_errno values:
   612 //  - errno's from ini_startup()
   613 mBOOL MPluginList::load() {
   614 	int i, n;
   615 
   616 	if (!ini_startup()) {
   617 		META_ERROR("Problem loading plugins.ini: %s", inifile);
   618 		// meta_errno should be already set in ini_startup()
   619 		return(mFALSE);
   620 	}
   621 
   622 	META_LOG("dll: Loading plugins...");
   623 	for(i=0, n=0; i < endlist; i++) {
   624 		if(plist[i].status < PL_VALID)
   625 			continue;
   626 		if(plist[i].load(PT_STARTUP) == mTRUE)
   627 			n++;
   628 		else
   629 			// all plugins should be loadable at startup...
   630 			META_ERROR("dll: Failed to load plugin '%s'", plist[i].file);
   631 	}
   632 	META_LOG("dll: Finished loading %d plugins", n);
   633 	return(mTRUE);
   634 }
   635 
   636 // Update list of loaded plugins from ini file, and load any new/changed plugins.
   637 // meta_errno values:
   638 //  - errno's from ini_refresh()
   639 mBOOL MPluginList::refresh(PLUG_LOADTIME now) {
   640 	int i, ndone=0, nkept=0, nloaded=0, nunloaded=0, nreloaded=0, ndelayed=0;
   641 	MPlugin *iplug;
   642 
   643 	if (!ini_refresh()) {
   644 		META_ERROR("dll: Problem reloading plugins.ini: %s", inifile);
   645 		// meta_errno should be already set in ini_refresh()
   646 		return(mFALSE);
   647 	}
   648 
   649 	META_LOG("dll: Updating plugins...");
   650 	for(i=0; i < endlist; i++) {
   651 		iplug=&plist[i];
   652 		if(iplug->status < PL_VALID)
   653 			continue;
   654 		switch(iplug->action) {
   655 			case PA_KEEP:
   656 				META_DEBUG(1, ("Keeping plugin '%s'", iplug->desc));
   657 				iplug->action=PA_NONE;
   658 				nkept++;
   659 				break;
   660 			case PA_LOAD:
   661 				META_DEBUG(1, ("Loading plugin '%s'", iplug->desc));
   662 				if(iplug->load(now))
   663 					nloaded++;
   664 				else if(meta_errno==ME_DELAYED)
   665 					ndelayed++;
   666 				break;
   667 			case PA_RELOAD:
   668 				META_DEBUG(1, ("Reloading plugin '%s'", iplug->desc));
   669 				if(iplug->reload(now, PNL_FILE_NEWER))
   670 					nreloaded++;
   671 				else if(meta_errno==ME_DELAYED)
   672 					ndelayed++;
   673 				break;
   674 			case PA_NONE:
   675 				// If previously loaded from ini, but apparently removed from new ini.
   676 				if(iplug->source==PS_INI && iplug->status >= PL_RUNNING) {
   677 					META_DEBUG(1, ("Unloading plugin '%s'", iplug->desc));
   678 					iplug->action=PA_UNLOAD;
   679 					if(iplug->unload(now, PNL_INI_DELETED, PNL_INI_DELETED))
   680 						nunloaded++;
   681 					else if(meta_errno==ME_DELAYED)
   682 						ndelayed++;
   683 				}
   684 				break;
   685 			case PA_ATTACH:
   686 				// Previously requested attach, but was delayed?
   687 				META_DEBUG(1, ("Retrying attach plugin '%s'", iplug->desc));
   688 				if(iplug->retry(now, PNL_DELAYED))
   689 					nloaded++;
   690 				else if(meta_errno==ME_DELAYED)
   691 					ndelayed++;
   692 				break;
   693 			case PA_UNLOAD:
   694 				// Previously requested unload, but was delayed?
   695 				META_DEBUG(1, ("Retrying unload plugin '%s'", iplug->desc));
   696 				if(iplug->retry(now, PNL_DELAYED))
   697 					nunloaded++;
   698 				else if(meta_errno==ME_DELAYED)
   699 					ndelayed++;
   700 				break;
   701 			case PA_NULL:
   702 				META_ERROR("dll: Unexpected action for plugin '%s': '%s'", iplug->desc, iplug->str_action());
   703 				break;
   704 			default:
   705 				META_ERROR("dll: Unrecognized action for plugin '%s': '%s'", iplug->desc, iplug->str_action());
   706 				break;
   707 		}
   708 		ndone++;
   709 	}
   710 	META_LOG("dll: Finished updating %d plugins; kept %d, loaded %d, unloaded %d, reloaded %d, delayed %d", 
   711 			ndone, nkept, nloaded, nunloaded, nreloaded, ndelayed);
   712 	return(mTRUE);
   713 }
   714 
   715 // Re-enable any plugins currently paused.
   716 // meta_errno values:
   717 //  - none
   718 void MPluginList::unpause_all(void) {
   719 	int i;
   720 	MPlugin *iplug;
   721 	for(i=0; i < endlist; i++) {
   722 		iplug=&plist[i];
   723 		if(iplug->status==PL_PAUSED)
   724 			iplug->unpause();
   725 	}
   726 }
   727 
   728 // Retry any pending actions on plugins, for instance load/unload delayed
   729 // until changelevel.
   730 // meta_errno values:
   731 //  - none
   732 void MPluginList::retry_all(PLUG_LOADTIME now) {
   733 	int i;
   734 	MPlugin *iplug;
   735 	for(i=0; i < endlist; i++) {
   736 		iplug=&plist[i];
   737 		if(iplug->action != PA_NONE)
   738 			iplug->retry(now, PNL_DELAYED);
   739 	}
   740 }
   741 
   742 // List plugins and information about them in a formatted table.
   743 // meta_errno values:
   744 //  - none
   745 void MPluginList::show(int source_index) {
   746 	int i, n=0, r=0;
   747 	MPlugin *pl;
   748 	char desc[15+1], file[16+1], vers[7+1];		// plus 1 for term null
   749 
   750 	if (source_index <= 0)
   751 		META_CONS("Currently loaded plugins:");
   752 	else
   753 		META_CONS("Child plugins:");
   754 
   755 	META_CONS("  %*s  %-*s  %-4s %-4s  %-*s  v%-*s  %-3s  %-5s %-5s",
   756 			WIDTH_MAX_PLUGINS, "",
   757 			sizeof(desc)-1, "description",
   758 			"stat", "pend",
   759 			sizeof(file)-1, "file", sizeof(vers)-1, "ers",
   760 			"src", "load ", "unlod");
   761 	for(i=0; i < endlist; i++) {
   762 		pl=&plist[i];
   763 		if(pl->status < PL_VALID)
   764 			continue;
   765 		if ((source_index > 0) && (pl->source_plugin_index != source_index))
   766 			continue;
   767 		STRNCPY(desc, pl->desc, sizeof(desc));
   768 		STRNCPY(file, pl->file, sizeof(file));
   769 		if(pl->info && pl->info->version) STRNCPY(vers, pl->info->version, sizeof(vers));
   770 		else STRNCPY(vers, " -", sizeof(vers));
   771 		META_CONS(" [%*d] %-*s  %-4s %-4s  %-*s  v%-*s  %-3s  %-5s %-5s",
   772 				WIDTH_MAX_PLUGINS, pl->index, 
   773 				sizeof(desc)-1, desc,
   774 				pl->str_status(ST_SHOW), pl->str_action(SA_SHOW),
   775 				sizeof(file)-1, file, sizeof(vers)-1, vers,
   776 				pl->str_source(SO_SHOW),
   777 				pl->str_loadable(SL_SHOW), pl->str_unloadable(SL_SHOW));
   778 		if(pl->status == PL_RUNNING)
   779 			r++;
   780 		n++;
   781 	}
   782 	META_CONS("%d plugins, %d running", n, r);
   783 }
   784 
   785 // List plugins and information to Player/client entity.  Differs from the
   786 // "meta list" console command in that:
   787 //  - Shows only "running" plugins, skipping any failed or paused plugins.
   788 //  - Limited info about each plugin, mostly the "public" info (name, author, 
   789 //    etc).
   790 // meta_errno values:
   791 //  - none
   792 void MPluginList::show_client(edict_t *pEntity) {
   793 	int i, n=0;
   794 	MPlugin *pl;
   795 	META_CLIENT(pEntity, "Currently running plugins:");
   796 	for(i=0; i < endlist; i++) {
   797 		pl=&plist[i];
   798 		if(pl->status != PL_RUNNING)
   799 			continue;
   800 		n++;
   801 		META_CLIENT(pEntity, " [%3d] %s, v%s, %s, by %s, see %s",
   802 				n, 
   803 				pl->info->name ? pl->info->name : "<unknown>",
   804 				pl->info->version ? pl->info->version : "<?>",
   805 				pl->info->date ? pl->info->date : "<../../..>",
   806 				pl->info->author ? pl->info->author : "<unknown>",
   807 				pl->info->url ? pl->info->url : "<unknown>");
   808 	}
   809 	META_CLIENT(pEntity, "%d plugins", n);
   810 }
   811 
   812 mBOOL MPluginList::found_child_plugins(int source_index)
   813 {
   814 	int i;
   815 
   816 	if (source_index <= 0)
   817 		return mFALSE;
   818 
   819 	for (i=0; i<endlist; i++)
   820 	{
   821 		if (plist[i].status < PL_VALID)
   822 			continue;
   823 		if (plist[i].source_plugin_index == source_index)
   824 			return mTRUE;
   825 	}
   826 
   827 	return mFALSE;
   828 }
   829 
   830 void MPluginList::clear_source_plugin_index(int source_index)
   831 {
   832 	int i;
   833 
   834 	if (source_index <= 0)
   835 		return;
   836 
   837 	for (i=0; i<endlist; i++)
   838 	{
   839 		if (plist[i].status < PL_VALID)
   840 			continue;
   841 		if (plist[i].source_plugin_index == source_index)
   842 			plist[i].source_plugin_index = -1;
   843 	}
   844 }