metamod/mlist.cpp
changeset 0 d15304fcb7a7
child 91 36bccf5df521
child 244 7b246a2ad39c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/metamod/mlist.cpp	Tue Apr 06 07:36:14 2004 +0000
     1.3 @@ -0,0 +1,656 @@
     1.4 +// vi: set ts=4 sw=4 :
     1.5 +// vim: set tw=75 :
     1.6 +
     1.7 +// mlist.cpp - functions for list of plugins (class MPluginList)
     1.8 +
     1.9 +/*
    1.10 + * Copyright (c) 2001-2003 Will Day <[email protected]>
    1.11 + *
    1.12 + *    This file is part of Metamod.
    1.13 + *
    1.14 + *    Metamod is free software; you can redistribute it and/or modify it
    1.15 + *    under the terms of the GNU General Public License as published by the
    1.16 + *    Free Software Foundation; either version 2 of the License, or (at
    1.17 + *    your option) any later version.
    1.18 + *
    1.19 + *    Metamod is distributed in the hope that it will be useful, but
    1.20 + *    WITHOUT ANY WARRANTY; without even the implied warranty of
    1.21 + *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.22 + *    General Public License for more details.
    1.23 + *
    1.24 + *    You should have received a copy of the GNU General Public License
    1.25 + *    along with Metamod; if not, write to the Free Software Foundation,
    1.26 + *    Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.27 + *
    1.28 + *    In addition, as a special exception, the author gives permission to
    1.29 + *    link the code of this program with the Half-Life Game Engine ("HL
    1.30 + *    Engine") and Modified Game Libraries ("MODs") developed by Valve,
    1.31 + *    L.L.C ("Valve").  You must obey the GNU General Public License in all
    1.32 + *    respects for all of the code used other than the HL Engine and MODs
    1.33 + *    from Valve.  If you modify this file, you may extend this exception
    1.34 + *    to your version of the file, but you are not obligated to do so.  If
    1.35 + *    you do not wish to do so, delete this exception statement from your
    1.36 + *    version.
    1.37 + *
    1.38 + */
    1.39 +
    1.40 +#include <errno.h>				// errno, etc
    1.41 +
    1.42 +#include <extdll.h>				// always
    1.43 +
    1.44 +#include "mlist.h"				// me
    1.45 +#include "mplugin.h"			// class MPlugin
    1.46 +#include "plinfo.h"				// plid_t, etc
    1.47 +#include "commands_meta.h"		// cmd_meta_pluginlist, etc
    1.48 +#include "metamod.h"			// GameDLL, etc
    1.49 +#include "types_meta.h"			// mBOOL
    1.50 +#include "log_meta.h"			// META_LOG, etc
    1.51 +#include "osdep.h"				// win32 snprintf, normalize_pathname,
    1.52 +
    1.53 +// Constructor
    1.54 +MPluginList::MPluginList(const char *ifile) 
    1.55 +	: size(MAX_PLUGINS), endlist(0)
    1.56 +{
    1.57 +	int i;
    1.58 +	// store filename of ini file
    1.59 +	STRNCPY(inifile, ifile, sizeof(inifile));
    1.60 +	// initialize array
    1.61 +	memset(plist, 0, sizeof(plist));
    1.62 +	for(i=0; i< size; i++) {
    1.63 +		plist[i].index=i+1;		// 1-based
    1.64 +	}
    1.65 +	endlist=0;
    1.66 +}
    1.67 +
    1.68 +// Find a plugin based on the plugin index #.
    1.69 +// meta_errno values:
    1.70 +//  - ME_ARGUMENT	invalid pindex
    1.71 +//  - ME_NOTFOUND	couldn't find a matching plugin
    1.72 +MPlugin *MPluginList::find(int pindex) {
    1.73 +	MPlugin *pfound;
    1.74 +	if(pindex <= 0)
    1.75 +		RETURN_ERRNO(NULL, ME_ARGUMENT);
    1.76 +	pfound=&plist[pindex-1];
    1.77 +	if(pfound->status < PL_VALID)
    1.78 +		RETURN_ERRNO(NULL, ME_NOTFOUND);
    1.79 +	else
    1.80 +		return(pfound);
    1.81 +}
    1.82 +
    1.83 +// Find a plugin with the given plid.
    1.84 +// meta_errno values:
    1.85 +//  - ME_ARGUMENT	null plid_t
    1.86 +//  - ME_NOTFOUND	couldn't find a matching plugin
    1.87 +MPlugin *MPluginList::find(plid_t id) {
    1.88 +	int i;
    1.89 +
    1.90 +	if(!id)
    1.91 +		RETURN_ERRNO(NULL, ME_ARGUMENT);
    1.92 +	for(i=0; i < endlist; i++) {
    1.93 +		if(plist[i].status < PL_VALID)
    1.94 +			continue;
    1.95 +		if(plist[i].info == id)
    1.96 +			return(&plist[i]);
    1.97 +	}
    1.98 +	RETURN_ERRNO(NULL, ME_NOTFOUND);
    1.99 +}
   1.100 +
   1.101 +// Find a plugin with the given pathname.
   1.102 +// meta_errno values:
   1.103 +//  - ME_ARGUMENT	null path
   1.104 +//  - ME_NOTFOUND	couldn't find a matching plugin
   1.105 +MPlugin *MPluginList::find(const char *findpath) {
   1.106 +	int i;
   1.107 +
   1.108 +	if(!findpath)
   1.109 +		RETURN_ERRNO(NULL, ME_ARGUMENT);
   1.110 +	META_DEBUG(8, ("Looking for loaded plugin with dlfnamepath: %s", findpath));
   1.111 +	for(i=0; i < endlist; i++) {
   1.112 +		META_DEBUG(9, ("Looking at: plugin %s loadedpath: %s", plist[i].file, plist[i].pathname));
   1.113 +		if(plist[i].status < PL_VALID)
   1.114 +			continue;
   1.115 +		if(strmatch(plist[i].pathname, findpath)) {
   1.116 +			META_DEBUG(8, ("Found loaded plugin %s", plist[i].file));
   1.117 +			return(&plist[i]);
   1.118 +		}
   1.119 +	}
   1.120 +	META_DEBUG(8, ("No loaded plugin found with path: %s", findpath));
   1.121 +	RETURN_ERRNO(NULL, ME_NOTFOUND);
   1.122 +}
   1.123 +
   1.124 +// Find a plugin that uses the given memory location.
   1.125 +// meta_errno values:
   1.126 +//  - ME_ARGUMENT	null memptr
   1.127 +//  - ME_NOTFOUND	couldn't find a matching plugin
   1.128 +//  - errno's from DLFNAME()
   1.129 +MPlugin *MPluginList::find_memloc(void *memptr) {
   1.130 +	const char *dlfile;
   1.131 +
   1.132 +	if(!memptr)
   1.133 +		RETURN_ERRNO(NULL, ME_ARGUMENT);
   1.134 +	if(!(dlfile=DLFNAME(memptr))) {
   1.135 +		META_DEBUG(8, ("DLFNAME failed to find memloc %d", memptr));
   1.136 +		// meta_errno should be already set in DLFNAME
   1.137 +		return(NULL);
   1.138 +	}
   1.139 +	return(find(dlfile));
   1.140 +}
   1.141 +
   1.142 +// Find a plugin with non-ambiguous prefix string matching desc, file, 
   1.143 +// name, or logtag.
   1.144 +// meta_errno values:
   1.145 +//  - ME_ARGUMENT	null prefix
   1.146 +//  - ME_NOTFOUND	couldn't find a matching plugin
   1.147 +//  - ME_NOTUNIQ	found multiple matches; no unique match
   1.148 +MPlugin *MPluginList::find_match(const char *prefix) {
   1.149 +	int i, len;
   1.150 +	MPlugin *iplug, *pfound;
   1.151 +	char buf[NAME_MAX];
   1.152 +
   1.153 +	if(!prefix)
   1.154 +		RETURN_ERRNO(NULL, ME_ARGUMENT);
   1.155 +	pfound=NULL;
   1.156 +	len=strlen(prefix);
   1.157 +	snprintf(buf, sizeof(buf), "mm_%s", prefix);
   1.158 +	for(i=0; i < endlist; i++) {
   1.159 +		iplug=&plist[i];
   1.160 +		if(iplug->status < PL_VALID)
   1.161 +			continue;
   1.162 +		if(iplug->info && strncasecmp(iplug->info->name, prefix, len) == 0) {
   1.163 +			if(pfound)
   1.164 +				RETURN_ERRNO(NULL, ME_NOTUNIQ);
   1.165 +			pfound=iplug;
   1.166 +			continue;
   1.167 +		}
   1.168 +		else if(strncasecmp(iplug->desc, prefix, len) == 0) {
   1.169 +			if(pfound)
   1.170 +				RETURN_ERRNO(NULL, ME_NOTUNIQ);
   1.171 +			pfound=iplug;
   1.172 +			continue;
   1.173 +		}
   1.174 +		else if(strncasecmp(iplug->file, prefix, len) == 0) {
   1.175 +			if(pfound)
   1.176 +				RETURN_ERRNO(NULL, ME_NOTUNIQ);
   1.177 +			pfound=iplug;
   1.178 +			continue;
   1.179 +		}
   1.180 +		else if(strncasecmp(iplug->file, buf, strlen(buf)) == 0) {
   1.181 +			if(pfound)
   1.182 +				RETURN_ERRNO(NULL, ME_NOTUNIQ);
   1.183 +			pfound=iplug;
   1.184 +			continue;
   1.185 +		}
   1.186 +		else if(iplug->info 
   1.187 +				&& strncasecmp(iplug->info->logtag, prefix, len) == 0) 
   1.188 +		{
   1.189 +			if(pfound)
   1.190 +				RETURN_ERRNO(NULL, ME_NOTUNIQ);
   1.191 +			pfound=iplug;
   1.192 +			continue;
   1.193 +		}
   1.194 +	}
   1.195 +	if(pfound)
   1.196 +		return(pfound);
   1.197 +	else
   1.198 +		RETURN_ERRNO(NULL, ME_NOTFOUND);
   1.199 +}
   1.200 +
   1.201 +// Add a plugin to the list.
   1.202 +// meta_errno values:
   1.203 +//  - ME_MAXREACHED		reached max plugins
   1.204 +MPlugin *MPluginList::add(MPlugin *padd) {
   1.205 +	int i;
   1.206 +	MPlugin *iplug;
   1.207 +
   1.208 +	// Find either:
   1.209 +	//  - a slot in the list that's not being used
   1.210 +	//  - the end of the list
   1.211 +	for(i=0; i < endlist && plist[i].status != PL_EMPTY; i++);
   1.212 +
   1.213 +	// couldn't find a slot to use
   1.214 +	if(i==size) {
   1.215 +		META_ERROR("Couldn't add plugin '%s' to list; reached max plugins (%d)", 
   1.216 +				padd->file, i);
   1.217 +		RETURN_ERRNO(NULL, ME_MAXREACHED);
   1.218 +	}
   1.219 +
   1.220 +	// if we found the end of the list, advance end marker
   1.221 +	if(i==endlist)
   1.222 +		endlist++;
   1.223 +	iplug = &plist[i];
   1.224 +
   1.225 +	// copy filename into this free slot
   1.226 +	STRNCPY(iplug->filename, padd->filename, sizeof(iplug->filename));
   1.227 +	// Copy file offset ptr.
   1.228 +	// Can't just copy ptr, as it points to offset in padd, which will go
   1.229 +	// away; need to point to corresponding offset in iplug.
   1.230 +	iplug->file = iplug->filename + (padd->file - padd->filename);
   1.231 +	// copy description
   1.232 +	STRNCPY(iplug->desc, padd->desc, sizeof(iplug->desc));
   1.233 +	// copy pathname
   1.234 +	STRNCPY(iplug->pathname, padd->pathname, sizeof(iplug->pathname));
   1.235 +	// copy source
   1.236 +	iplug->source=padd->source;
   1.237 +	// copy status
   1.238 +	iplug->status=padd->status;
   1.239 +
   1.240 +	return(iplug);
   1.241 +}
   1.242 +
   1.243 +
   1.244 +// Read plugins.ini at server startup.
   1.245 +// meta_errno values:
   1.246 +//  - ME_NOFILE		ini file missing or empty
   1.247 +mBOOL MPluginList::ini_startup() {
   1.248 +	FILE *fp;
   1.249 +	char line[MAX_STRBUF_LEN];
   1.250 +	int n, ln;
   1.251 +
   1.252 +	if(!valid_gamedir_file(inifile)) {
   1.253 +		META_ERROR("ini: Metamod plugins file empty or missing: %s", inifile);
   1.254 +		RETURN_ERRNO(mFALSE, ME_NOFILE);
   1.255 +	}
   1.256 +	full_gamedir_path(inifile, inifile);
   1.257 +
   1.258 +	fp=fopen(inifile, "r");
   1.259 +	if(!fp) {
   1.260 +		META_ERROR("ini: Unable to open plugins file '%s': %s", inifile, 
   1.261 +				strerror(errno));
   1.262 +		RETURN_ERRNO(mFALSE, ME_NOFILE);
   1.263 +	}
   1.264 +
   1.265 +	META_LOG("ini: Begin reading plugins list: %s", inifile);
   1.266 +	for(n=0, ln=1; 
   1.267 +			!feof(fp) && fgets(line, sizeof(line), fp) && n < size; 
   1.268 +			ln++) 
   1.269 +	{
   1.270 +		// Remove line terminations.
   1.271 +		char *cp;
   1.272 +		if((cp=strrchr(line, '\r')))
   1.273 +			*cp='\0';
   1.274 +		if((cp=strrchr(line, '\n')))
   1.275 +			*cp='\0';
   1.276 +		// Parse directly into next entry in array
   1.277 +		if(!plist[n].ini_parseline(line)) {
   1.278 +			if(meta_errno==ME_FORMAT)
   1.279 +				META_ERROR("ini: Skipping malformed line %d of %s", ln, 
   1.280 +						inifile);
   1.281 +			continue;
   1.282 +		}
   1.283 +		// Check for a duplicate - an existing entry with this pathname.
   1.284 +		if(find(plist[n].pathname)) {
   1.285 +			META_ERROR("ini: Skipping duplicate plugin, line %d of %s: %s", 
   1.286 +					ln, inifile, plist[n].pathname);
   1.287 +			continue;
   1.288 +		}
   1.289 +		plist[n].action=PA_LOAD;
   1.290 +		META_LOG("ini: Read plugin config for: %s", plist[n].desc);
   1.291 +		n++;
   1.292 +		endlist=n;		// mark end of list
   1.293 +	}
   1.294 +	META_LOG("ini: Finished reading plugins list: %s; Found %d plugins to load",
   1.295 +			inifile, n);
   1.296 +
   1.297 +	fclose(fp);
   1.298 +	if(!n) {
   1.299 +		META_ERROR("ini: Warning; no plugins found to load?");
   1.300 +	}
   1.301 +	return(mTRUE);
   1.302 +}
   1.303 +
   1.304 +// Re-read plugins.ini looking for added/deleted/changed plugins.
   1.305 +// meta_errno values:
   1.306 +//  - ME_NOFILE		ini file missing or empty
   1.307 +mBOOL MPluginList::ini_refresh() {
   1.308 +	FILE *fp;
   1.309 +	char line[MAX_STRBUF_LEN];
   1.310 +	int n, ln;
   1.311 +	MPlugin pl_temp;
   1.312 +	MPlugin *pl_found, *pl_added;
   1.313 +
   1.314 +	fp=fopen(inifile, "r");
   1.315 +	if(!fp) {
   1.316 +		META_ERROR("ini: Unable to open plugins file '%s': %s", inifile, 
   1.317 +				strerror(errno));
   1.318 +		RETURN_ERRNO(mFALSE, ME_NOFILE);
   1.319 +	}
   1.320 +
   1.321 +	META_LOG("ini: Begin re-reading plugins list: %s", inifile);
   1.322 +	for(n=0, ln=1; 
   1.323 +			!feof(fp) && fgets(line, sizeof(line), fp) && n < size; 
   1.324 +			ln++) 
   1.325 +	{
   1.326 +		// Remove line terminations.
   1.327 +		char *cp;
   1.328 +		if((cp=strrchr(line, '\r')))
   1.329 +			*cp='\0';
   1.330 +		if((cp=strrchr(line, '\n')))
   1.331 +			*cp='\0';
   1.332 +		// Parse into a temp plugin
   1.333 +		memset(&pl_temp, 0, sizeof(pl_temp));
   1.334 +		if(!pl_temp.ini_parseline(line)) {
   1.335 +			if(meta_errno==ME_FORMAT)
   1.336 +				META_ERROR("ini: Skipping malformed line %d of %s", 
   1.337 +						ln, inifile);
   1.338 +			continue;
   1.339 +		}
   1.340 +		// Try to find plugin with this pathname in the current list of
   1.341 +		// plugins.
   1.342 +		if(!(pl_found=find(pl_temp.pathname))) {
   1.343 +			// new plugin; add to list
   1.344 +			if((pl_added=add(&pl_temp))) {
   1.345 +				// try to load this plugin at the next opportunity
   1.346 +				pl_added->action=PA_LOAD;
   1.347 +			}
   1.348 +			else
   1.349 +				// error details logged in add()
   1.350 +				continue;
   1.351 +		}
   1.352 +		else {
   1.353 +			// This plugin is already in the current list of plugins.
   1.354 +			// Pathname already matches.  Recopy desc, if specified in
   1.355 +			// plugins.ini.
   1.356 +			if(pl_temp.desc[0] != '<')
   1.357 +				STRNCPY(pl_found->desc, pl_temp.desc, sizeof(pl_found->desc));
   1.358 +
   1.359 +			// Check the file to see if it looks like it's been modified
   1.360 +			// since we last loaded it.
   1.361 +			if(!pl_found->newer_file()) {
   1.362 +				if(meta_errno==ME_NOFILE) {
   1.363 +					META_ERROR("ini: Skipping plugin, couldn't stat file '%s': %s",
   1.364 +							pl_found->pathname, strerror(errno));
   1.365 +					continue;
   1.366 +				}
   1.367 +				else {
   1.368 +					// File hasn't been updated.
   1.369 +					// Keep plugin (don't let refresh() unload it).
   1.370 +					pl_found->action=PA_KEEP;
   1.371 +				}
   1.372 +			}
   1.373 +			// Newer file on disk.
   1.374 +			else if(pl_found->status >= PL_OPENED)  {
   1.375 +				META_DEBUG(2, ("ini: Plugin '%s' has newer file on disk", pl_found->desc));
   1.376 +				pl_found->action=PA_RELOAD;
   1.377 +			}
   1.378 +			else
   1.379 +				META_ERROR("ini: Plugin '%s' has newer file, but unexpected status (%s)",
   1.380 +						pl_found->desc, pl_found->str_status());
   1.381 +		}
   1.382 +		META_LOG("ini: Read plugin config for: %s", plist[n].desc);
   1.383 +		n++;
   1.384 +	}
   1.385 +	META_LOG("ini: Finished reading plugins list: %s; Found %d plugins", inifile, n);
   1.386 +
   1.387 +	fclose(fp);
   1.388 +	if(!n) {
   1.389 +		META_ERROR("ini: Warning; no plugins found to load?");
   1.390 +	}
   1.391 +	return(mTRUE);
   1.392 +}
   1.393 +
   1.394 +// Load a plugin from a console command.
   1.395 +// meta_errno values:
   1.396 +//  - errno's from cmd_parseline()
   1.397 +//  - errno's from resolve()
   1.398 +//  - ME_ALREADY	this plugin already loaded
   1.399 +//  - errno's from add()
   1.400 +//  - errno's from load()
   1.401 +mBOOL MPluginList::cmd_addload(const char *args) {
   1.402 +	MPlugin pl_temp;
   1.403 +	MPlugin *pl_found, *pl_added;
   1.404 +
   1.405 +	// XXX move back to comands_meta ?
   1.406 +
   1.407 +	// parse into a temp plugin
   1.408 +	memset(&pl_temp, 0, sizeof(pl_temp));
   1.409 +	if(pl_temp.cmd_parseline(args) != mTRUE) {
   1.410 +		META_CONS("Couldn't parse 'meta load' arguments: %s", args);
   1.411 +		// meta_errno should be already set in cmd_parseline()
   1.412 +		return(mFALSE);
   1.413 +	}
   1.414 +
   1.415 +	// resolve given path into a file; accepts various "shortcut"
   1.416 +	// pathnames.
   1.417 +	if(pl_temp.resolve() != mTRUE) {
   1.418 +		// Couldn't find a matching file on disk
   1.419 +		META_CONS("Couldn't resolve given path into a file: %s", 
   1.420 +				pl_temp.file);
   1.421 +		// meta_errno should be already set in resolve()
   1.422 +		return(mFALSE);
   1.423 +	}
   1.424 +
   1.425 +	// Try to find plugin with this pathname in the current list of
   1.426 +	// plugins.
   1.427 +	if((pl_found=find(pl_temp.pathname))) {
   1.428 +		// Already in list
   1.429 +		META_CONS("Plugin '%s' already in current list; file=%s desc='%s'", 
   1.430 +				pl_temp.file, pl_found->file, pl_found->desc);
   1.431 +		RETURN_ERRNO(mFALSE, ME_ALREADY);
   1.432 +	}
   1.433 +	// new plugin; add to list
   1.434 +	if(!(pl_added=add(&pl_temp))) {
   1.435 +		META_CONS("Couldn't add plugin '%s' to list; see log", pl_temp.desc);
   1.436 +		// meta_errno should be already set in add()
   1.437 +		return(mFALSE);
   1.438 +	}
   1.439 +
   1.440 +	// try to load new plugin
   1.441 +	pl_added->action=PA_LOAD;
   1.442 +	if(!pl_added->load(PT_ANYTIME)) {
   1.443 +		// load failed
   1.444 +		if(meta_errno==ME_DELAYED) 
   1.445 +			META_CONS("Loaded plugin '%s', but will wait to become active, %s", 
   1.446 +					pl_added->desc, pl_added->str_loadable(SL_ALLOWED));
   1.447 +		else if(meta_errno==ME_NOTALLOWED) {
   1.448 +			META_CONS("Plugin '%s' couldn't attach; only allowed %s", 
   1.449 +					pl_added->desc, pl_added->str_loadable(SL_ALLOWED));
   1.450 +			pl_added->clear();
   1.451 +		}
   1.452 +		else if(pl_added->status == PL_OPENED)
   1.453 +			META_CONS("Opened plugin '%s', but failed to attach; see log", pl_added->desc);
   1.454 +		else
   1.455 +			META_CONS("Couldn't load plugin '%s'; see log", pl_added->desc);
   1.456 +		show();
   1.457 +		// meta_errno should be already set in load()
   1.458 +		return(mFALSE);
   1.459 +	}
   1.460 +	META_CONS("Loaded plugin '%s' successfully", pl_added->desc);
   1.461 +	show();
   1.462 +	return(mTRUE);
   1.463 +}
   1.464 +
   1.465 +// Load plugins at startup.
   1.466 +// meta_errno values:
   1.467 +//  - errno's from ini_startup()
   1.468 +mBOOL MPluginList::load() {
   1.469 +	int i, n;
   1.470 +
   1.471 +	if (!ini_startup()) {
   1.472 +		META_ERROR("Problem loading plugins.ini: %s", inifile);
   1.473 +		// meta_errno should be already set in ini_startup()
   1.474 +		return(mFALSE);
   1.475 +	}
   1.476 +
   1.477 +	META_LOG("dll: Loading plugins...");
   1.478 +	for(i=0, n=0; i < endlist; i++) {
   1.479 +		if(plist[i].status < PL_VALID)
   1.480 +			continue;
   1.481 +		if(plist[i].load(PT_STARTUP) == mTRUE)
   1.482 +			n++;
   1.483 +		else
   1.484 +			// all plugins should be loadable at startup...
   1.485 +			META_ERROR("dll: Failed to load plugin '%s'", plist[i].file);
   1.486 +	}
   1.487 +	META_LOG("dll: Finished loading %d plugins", n);
   1.488 +	return(mTRUE);
   1.489 +}
   1.490 +
   1.491 +// Update list of loaded plugins from ini file, and load any new/changed plugins.
   1.492 +// meta_errno values:
   1.493 +//  - errno's from ini_refresh()
   1.494 +mBOOL MPluginList::refresh(PLUG_LOADTIME now) {
   1.495 +	int i, ndone=0, nkept=0, nloaded=0, nunloaded=0, nreloaded=0, ndelayed=0;
   1.496 +	MPlugin *iplug;
   1.497 +
   1.498 +	if (!ini_refresh()) {
   1.499 +		META_ERROR("dll: Problem reloading plugins.ini: %s", inifile);
   1.500 +		// meta_errno should be already set in ini_refresh()
   1.501 +		return(mFALSE);
   1.502 +	}
   1.503 +
   1.504 +	META_LOG("dll: Updating plugins...");
   1.505 +	for(i=0; i < endlist; i++) {
   1.506 +		iplug=&plist[i];
   1.507 +		if(iplug->status < PL_VALID)
   1.508 +			continue;
   1.509 +		switch(iplug->action) {
   1.510 +			case PA_KEEP:
   1.511 +				META_DEBUG(1, ("Keeping plugin '%s'", iplug->desc));
   1.512 +				iplug->action=PA_NONE;
   1.513 +				nkept++;
   1.514 +				break;
   1.515 +			case PA_LOAD:
   1.516 +				META_DEBUG(1, ("Loading plugin '%s'", iplug->desc));
   1.517 +				if(iplug->load(now))
   1.518 +					nloaded++;
   1.519 +				else if(meta_errno==ME_DELAYED)
   1.520 +					ndelayed++;
   1.521 +				break;
   1.522 +			case PA_RELOAD:
   1.523 +				META_DEBUG(1, ("Reloading plugin '%s'", iplug->desc));
   1.524 +				if(iplug->reload(now, PNL_FILE_NEWER))
   1.525 +					nreloaded++;
   1.526 +				else if(meta_errno==ME_DELAYED)
   1.527 +					ndelayed++;
   1.528 +				break;
   1.529 +			case PA_NONE:
   1.530 +				// If previously loaded from ini, but apparently removed from new ini.
   1.531 +				if(iplug->source==PS_INI && iplug->status >= PL_RUNNING) {
   1.532 +					META_DEBUG(1, ("Unloading plugin '%s'", iplug->desc));
   1.533 +					iplug->action=PA_UNLOAD;
   1.534 +					if(iplug->unload(now, PNL_INI_DELETED))
   1.535 +						nunloaded++;
   1.536 +					else if(meta_errno==ME_DELAYED)
   1.537 +						ndelayed++;
   1.538 +				}
   1.539 +				break;
   1.540 +			case PA_ATTACH:
   1.541 +				// Previously requested attach, but was delayed?
   1.542 +				META_DEBUG(1, ("Retrying attach plugin '%s'", iplug->desc));
   1.543 +				if(iplug->retry(now, PNL_DELAYED))
   1.544 +					nloaded++;
   1.545 +				else if(meta_errno==ME_DELAYED)
   1.546 +					ndelayed++;
   1.547 +				break;
   1.548 +			case PA_UNLOAD:
   1.549 +				// Previously requested unload, but was delayed?
   1.550 +				META_DEBUG(1, ("Retrying unload plugin '%s'", iplug->desc));
   1.551 +				if(iplug->retry(now, PNL_DELAYED))
   1.552 +					nunloaded++;
   1.553 +				else if(meta_errno==ME_DELAYED)
   1.554 +					ndelayed++;
   1.555 +				break;
   1.556 +			case PA_NULL:
   1.557 +				META_ERROR("dll: Unexpected action for plugin '%s': '%s'", iplug->desc, iplug->str_action());
   1.558 +				break;
   1.559 +			default:
   1.560 +				META_ERROR("dll: Unrecognized action for plugin '%s': '%s'", iplug->desc, iplug->str_action());
   1.561 +				break;
   1.562 +		}
   1.563 +		ndone++;
   1.564 +	}
   1.565 +	META_LOG("dll: Finished updating %d plugins; kept %d, loaded %d, unloaded %d, reloaded %d, delayed %d", 
   1.566 +			ndone, nkept, nloaded, nunloaded, nreloaded, ndelayed);
   1.567 +	return(mTRUE);
   1.568 +}
   1.569 +
   1.570 +// Re-enable any plugins currently paused.
   1.571 +// meta_errno values:
   1.572 +//  - none
   1.573 +void MPluginList::unpause_all(void) {
   1.574 +	int i;
   1.575 +	MPlugin *iplug;
   1.576 +	for(i=0; i < endlist; i++) {
   1.577 +		iplug=&plist[i];
   1.578 +		if(iplug->status==PL_PAUSED)
   1.579 +			iplug->unpause();
   1.580 +	}
   1.581 +}
   1.582 +
   1.583 +// Retry any pending actions on plugins, for instance load/unload delayed
   1.584 +// until changelevel.
   1.585 +// meta_errno values:
   1.586 +//  - none
   1.587 +void MPluginList::retry_all(PLUG_LOADTIME now) {
   1.588 +	int i;
   1.589 +	MPlugin *iplug;
   1.590 +	for(i=0; i < endlist; i++) {
   1.591 +		iplug=&plist[i];
   1.592 +		if(iplug->action != PA_NONE)
   1.593 +			iplug->retry(now, PNL_DELAYED);
   1.594 +	}
   1.595 +}
   1.596 +
   1.597 +// List plugins and information about them in a formatted table.
   1.598 +// meta_errno values:
   1.599 +//  - none
   1.600 +void MPluginList::show() {
   1.601 +	int i, n=0, r=0;
   1.602 +	MPlugin *pl;
   1.603 +	char desc[15+1], file[16+1], vers[7+1];		// plus 1 for term null
   1.604 +
   1.605 +	META_CONS("Currently loaded plugins:");
   1.606 +	META_CONS("  %*s  %-*s  %-4s %-4s  %-*s  v%-*s  %-3s  %-5s %-5s",
   1.607 +			WIDTH_MAX_PLUGINS, "",
   1.608 +			sizeof(desc)-1, "description",
   1.609 +			"stat", "pend",
   1.610 +			sizeof(file)-1, "file", sizeof(vers)-1, "ers",
   1.611 +			"src", "load ", "unlod");
   1.612 +	for(i=0; i < endlist; i++) {
   1.613 +		pl=&plist[i];
   1.614 +		if(pl->status < PL_VALID)
   1.615 +			continue;
   1.616 +		STRNCPY(desc, pl->desc, sizeof(desc));
   1.617 +		STRNCPY(file, pl->file, sizeof(file));
   1.618 +		if(pl->info && pl->info->version) STRNCPY(vers, pl->info->version, sizeof(vers));
   1.619 +		else STRNCPY(vers, " -", sizeof(vers));
   1.620 +		META_CONS(" [%*d] %-*s  %-4s %-4s  %-*s  v%-*s  %-3s  %-5s %-5s",
   1.621 +				WIDTH_MAX_PLUGINS, pl->index, 
   1.622 +				sizeof(desc)-1, desc,
   1.623 +				pl->str_status(ST_SHOW), pl->str_action(SA_SHOW),
   1.624 +				sizeof(file)-1, file, sizeof(vers)-1, vers,
   1.625 +				pl->str_source(SO_SHOW),
   1.626 +				pl->str_loadable(SL_SHOW), pl->str_unloadable(SL_SHOW));
   1.627 +		if(pl->status == PL_RUNNING)
   1.628 +			r++;
   1.629 +		n++;
   1.630 +	}
   1.631 +	META_CONS("%d plugins, %d running", n, r);
   1.632 +}
   1.633 +
   1.634 +// List plugins and information to Player/client entity.  Differs from the
   1.635 +// "meta list" console command in that:
   1.636 +//  - Shows only "running" plugins, skipping any failed or paused plugins.
   1.637 +//  - Limited info about each plugin, mostly the "public" info (name, author, 
   1.638 +//    etc).
   1.639 +// meta_errno values:
   1.640 +//  - none
   1.641 +void MPluginList::show_client(edict_t *pEntity) {
   1.642 +	int i, n=0;
   1.643 +	MPlugin *pl;
   1.644 +	META_CLIENT(pEntity, "Currently running plugins:");
   1.645 +	for(i=0; i < endlist; i++) {
   1.646 +		pl=&plist[i];
   1.647 +		if(pl->status != PL_RUNNING)
   1.648 +			continue;
   1.649 +		n++;
   1.650 +		META_CLIENT(pEntity, " [%3d] %s, v%s, %s, by %s, see %s",
   1.651 +				n, 
   1.652 +				pl->info->name ? pl->info->name : "<unknown>",
   1.653 +				pl->info->version ? pl->info->version : "<?>",
   1.654 +				pl->info->date ? pl->info->date : "<../../..>",
   1.655 +				pl->info->author ? pl->info->author : "<unknown>",
   1.656 +				pl->info->url ? pl->info->url : "<unknown>");
   1.657 +	}
   1.658 +	META_CLIENT(pEntity, "%d plugins", n);
   1.659 +}