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