metamod/mlist.cpp
author darope
Mon Aug 08 20:08:37 2005 +0000 (2005-08-08)
branchB-work
changeset 140 71a284715890
parent 139 2995153b596c
child 143 c8bb186f5851
permissions -rw-r--r--
Removed double console message 'Currently loaded plugins:'
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@133
   298
bailopan@130
   299
	return pl_added;
bailopan@130
   300
}
darope@91
   301
darope@0
   302
// Add a plugin to the list.
darope@0
   303
// meta_errno values:
darope@0
   304
//  - ME_MAXREACHED		reached max plugins
darope@0
   305
MPlugin *MPluginList::add(MPlugin *padd) {
darope@0
   306
	int i;
darope@0
   307
	MPlugin *iplug;
darope@0
   308
darope@0
   309
	// Find either:
darope@0
   310
	//  - a slot in the list that's not being used
darope@0
   311
	//  - the end of the list
darope@0
   312
	for(i=0; i < endlist && plist[i].status != PL_EMPTY; i++);
darope@0
   313
darope@0
   314
	// couldn't find a slot to use
darope@0
   315
	if(i==size) {
darope@0
   316
		META_ERROR("Couldn't add plugin '%s' to list; reached max plugins (%d)", 
darope@0
   317
				padd->file, i);
darope@0
   318
		RETURN_ERRNO(NULL, ME_MAXREACHED);
darope@0
   319
	}
darope@0
   320
darope@0
   321
	// if we found the end of the list, advance end marker
darope@0
   322
	if(i==endlist)
darope@0
   323
		endlist++;
darope@0
   324
	iplug = &plist[i];
darope@0
   325
darope@0
   326
	// copy filename into this free slot
darope@0
   327
	STRNCPY(iplug->filename, padd->filename, sizeof(iplug->filename));
darope@0
   328
	// Copy file offset ptr.
darope@0
   329
	// Can't just copy ptr, as it points to offset in padd, which will go
darope@0
   330
	// away; need to point to corresponding offset in iplug.
darope@0
   331
	iplug->file = iplug->filename + (padd->file - padd->filename);
darope@0
   332
	// copy description
darope@0
   333
	STRNCPY(iplug->desc, padd->desc, sizeof(iplug->desc));
darope@0
   334
	// copy pathname
darope@0
   335
	STRNCPY(iplug->pathname, padd->pathname, sizeof(iplug->pathname));
darope@0
   336
	// copy source
darope@0
   337
	iplug->source=padd->source;
darope@0
   338
	// copy status
bailopan@139
   339
	iplug->status=padd->status;	
bailopan@139
   340
	//copy other things
bailopan@139
   341
	iplug->source_plugin_index = padd->source_plugin_index;
darope@0
   342
darope@0
   343
	return(iplug);
darope@0
   344
}
darope@0
   345
darope@0
   346
darope@0
   347
// Read plugins.ini at server startup.
darope@0
   348
// meta_errno values:
darope@0
   349
//  - ME_NOFILE		ini file missing or empty
darope@0
   350
mBOOL MPluginList::ini_startup() {
darope@0
   351
	FILE *fp;
darope@0
   352
	char line[MAX_STRBUF_LEN];
darope@0
   353
	int n, ln;
darope@91
   354
	MPlugin *pmatch;
darope@0
   355
darope@0
   356
	if(!valid_gamedir_file(inifile)) {
darope@0
   357
		META_ERROR("ini: Metamod plugins file empty or missing: %s", inifile);
darope@0
   358
		RETURN_ERRNO(mFALSE, ME_NOFILE);
darope@0
   359
	}
darope@0
   360
	full_gamedir_path(inifile, inifile);
darope@0
   361
darope@0
   362
	fp=fopen(inifile, "r");
darope@0
   363
	if(!fp) {
darope@0
   364
		META_ERROR("ini: Unable to open plugins file '%s': %s", inifile, 
darope@0
   365
				strerror(errno));
darope@0
   366
		RETURN_ERRNO(mFALSE, ME_NOFILE);
darope@0
   367
	}
darope@0
   368
darope@0
   369
	META_LOG("ini: Begin reading plugins list: %s", inifile);
darope@0
   370
	for(n=0, ln=1; 
darope@0
   371
			!feof(fp) && fgets(line, sizeof(line), fp) && n < size; 
darope@0
   372
			ln++) 
darope@0
   373
	{
darope@0
   374
		// Remove line terminations.
darope@0
   375
		char *cp;
darope@0
   376
		if((cp=strrchr(line, '\r')))
darope@0
   377
			*cp='\0';
darope@0
   378
		if((cp=strrchr(line, '\n')))
darope@0
   379
			*cp='\0';
darope@0
   380
		// Parse directly into next entry in array
darope@0
   381
		if(!plist[n].ini_parseline(line)) {
darope@0
   382
			if(meta_errno==ME_FORMAT)
darope@0
   383
				META_ERROR("ini: Skipping malformed line %d of %s", ln, 
darope@0
   384
						inifile);
darope@0
   385
			continue;
darope@0
   386
		}
darope@0
   387
		// Check for a duplicate - an existing entry with this pathname.
darope@0
   388
		if(find(plist[n].pathname)) {
darope@91
   389
			// Should we check platform specific level here?
darope@98
   390
			META_INFO("ini: Skipping duplicate plugin, line %d of %s: %s", 
darope@0
   391
					ln, inifile, plist[n].pathname);
darope@0
   392
			continue;
darope@0
   393
		}
darope@91
   394
		// Check for a matching platform with different platform specifics
darope@91
   395
		// level.
darope@91
   396
		if(NULL != (pmatch=find_match(&plist[n]))) {
darope@91
   397
			if(pmatch->pfspecific >= plist[n].pfspecific) {
darope@91
   398
				META_DEBUG(1, ("ini: Skipping plugin, line %d of %s: plugin with higher platform specific level already exists. (%d >= %d)",
darope@98
   399
                         ln, inifile, pmatch->pfspecific, plist[n].pfspecific)); 
darope@91
   400
				continue;
darope@91
   401
			}
darope@91
   402
			META_DEBUG(1, ("ini: Plugin in line %d overrides existing plugin with lower platform specific level %d, ours %d",
darope@98
   403
					ln, pmatch->pfspecific, plist[n].pfspecific));
bailopan@128
   404
			int _index = pmatch->index;
darope@91
   405
			memset(pmatch, 0, sizeof(MPlugin));
bailopan@128
   406
			pmatch->index = _index;
darope@91
   407
		}
darope@0
   408
		plist[n].action=PA_LOAD;
darope@0
   409
		META_LOG("ini: Read plugin config for: %s", plist[n].desc);
darope@0
   410
		n++;
darope@0
   411
		endlist=n;		// mark end of list
darope@0
   412
	}
darope@0
   413
	META_LOG("ini: Finished reading plugins list: %s; Found %d plugins to load",
darope@0
   414
			inifile, n);
darope@0
   415
darope@0
   416
	fclose(fp);
darope@0
   417
	if(!n) {
darope@0
   418
		META_ERROR("ini: Warning; no plugins found to load?");
darope@0
   419
	}
darope@0
   420
	return(mTRUE);
darope@0
   421
}
darope@0
   422
darope@0
   423
// Re-read plugins.ini looking for added/deleted/changed plugins.
darope@0
   424
// meta_errno values:
darope@0
   425
//  - ME_NOFILE		ini file missing or empty
darope@0
   426
mBOOL MPluginList::ini_refresh() {
darope@0
   427
	FILE *fp;
darope@0
   428
	char line[MAX_STRBUF_LEN];
darope@0
   429
	int n, ln;
darope@0
   430
	MPlugin pl_temp;
darope@0
   431
	MPlugin *pl_found, *pl_added;
darope@0
   432
darope@0
   433
	fp=fopen(inifile, "r");
darope@0
   434
	if(!fp) {
darope@0
   435
		META_ERROR("ini: Unable to open plugins file '%s': %s", inifile, 
darope@0
   436
				strerror(errno));
darope@0
   437
		RETURN_ERRNO(mFALSE, ME_NOFILE);
darope@0
   438
	}
darope@0
   439
darope@0
   440
	META_LOG("ini: Begin re-reading plugins list: %s", inifile);
darope@0
   441
	for(n=0, ln=1; 
darope@0
   442
			!feof(fp) && fgets(line, sizeof(line), fp) && n < size; 
darope@0
   443
			ln++) 
darope@0
   444
	{
darope@0
   445
		// Remove line terminations.
darope@0
   446
		char *cp;
darope@0
   447
		if((cp=strrchr(line, '\r')))
darope@0
   448
			*cp='\0';
darope@0
   449
		if((cp=strrchr(line, '\n')))
darope@0
   450
			*cp='\0';
darope@0
   451
		// Parse into a temp plugin
darope@0
   452
		memset(&pl_temp, 0, sizeof(pl_temp));
darope@0
   453
		if(!pl_temp.ini_parseline(line)) {
darope@0
   454
			if(meta_errno==ME_FORMAT)
darope@0
   455
				META_ERROR("ini: Skipping malformed line %d of %s", 
darope@0
   456
						ln, inifile);
darope@0
   457
			continue;
darope@0
   458
		}
darope@0
   459
		// Try to find plugin with this pathname in the current list of
darope@0
   460
		// plugins.
darope@0
   461
		if(!(pl_found=find(pl_temp.pathname))) {
darope@93
   462
			// Check for a matching platform with higher platform specifics
darope@93
   463
			// level.
darope@93
   464
			if(NULL != (pl_found=find_match(&pl_temp))) {
darope@93
   465
				if(pl_found->pfspecific >= pl_temp.pfspecific) {
darope@93
   466
					META_DEBUG(1, ("ini: Skipping plugin, line %d of %s: plugin with higher platform specific level already exists. (%d >= %d)",
darope@93
   467
                       		 ln, inifile, pl_found->pfspecific, pl_temp.pfspecific)); 
darope@93
   468
					continue;
darope@93
   469
				}
darope@98
   470
				if(PA_LOAD == pl_found->action) {
darope@98
   471
					META_DEBUG(1, ("ini: Plugin in line %d overrides loading of plugin with lower platform specific level %d, ours %d",
darope@98
   472
								ln, pl_found->pfspecific, pl_temp.pfspecific));
bailopan@128
   473
					int _index = pl_found->index;
darope@98
   474
					memset(pl_found, 0, sizeof(MPlugin));
bailopan@128
   475
					pl_found->index = _index;
darope@98
   476
				}
darope@98
   477
				else {
darope@98
   478
					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
   479
								ln, pl_found->pfspecific, pl_temp.pfspecific));
darope@98
   480
					continue;
darope@98
   481
				}
darope@93
   482
			}
darope@0
   483
			// new plugin; add to list
darope@0
   484
			if((pl_added=add(&pl_temp))) {
darope@0
   485
				// try to load this plugin at the next opportunity
darope@0
   486
				pl_added->action=PA_LOAD;
darope@0
   487
			}
darope@0
   488
			else
darope@0
   489
				// error details logged in add()
darope@0
   490
				continue;
darope@0
   491
		}
darope@0
   492
		else {
darope@0
   493
			// This plugin is already in the current list of plugins.
darope@0
   494
			// Pathname already matches.  Recopy desc, if specified in
darope@0
   495
			// plugins.ini.
darope@0
   496
			if(pl_temp.desc[0] != '<')
darope@0
   497
				STRNCPY(pl_found->desc, pl_temp.desc, sizeof(pl_found->desc));
darope@0
   498
darope@0
   499
			// Check the file to see if it looks like it's been modified
darope@0
   500
			// since we last loaded it.
darope@0
   501
			if(!pl_found->newer_file()) {
darope@0
   502
				if(meta_errno==ME_NOFILE) {
darope@0
   503
					META_ERROR("ini: Skipping plugin, couldn't stat file '%s': %s",
darope@0
   504
							pl_found->pathname, strerror(errno));
darope@0
   505
					continue;
darope@0
   506
				}
darope@0
   507
				else {
darope@0
   508
					// File hasn't been updated.
darope@0
   509
					// Keep plugin (don't let refresh() unload it).
darope@0
   510
					pl_found->action=PA_KEEP;
darope@0
   511
				}
darope@0
   512
			}
darope@0
   513
			// Newer file on disk.
darope@0
   514
			else if(pl_found->status >= PL_OPENED)  {
darope@0
   515
				META_DEBUG(2, ("ini: Plugin '%s' has newer file on disk", pl_found->desc));
darope@0
   516
				pl_found->action=PA_RELOAD;
darope@0
   517
			}
darope@0
   518
			else
darope@0
   519
				META_ERROR("ini: Plugin '%s' has newer file, but unexpected status (%s)",
darope@0
   520
						pl_found->desc, pl_found->str_status());
darope@0
   521
		}
darope@98
   522
		if(NULL != pl_found) {
darope@98
   523
			META_LOG("ini: Read plugin config for: %s", pl_found->desc);
darope@98
   524
		}
darope@98
   525
		else {
darope@98
   526
			META_LOG("ini: Read plugin config for: %s", pl_temp.desc);
darope@98
   527
		}
darope@0
   528
		n++;
darope@0
   529
	}
darope@0
   530
	META_LOG("ini: Finished reading plugins list: %s; Found %d plugins", inifile, n);
darope@0
   531
darope@0
   532
	fclose(fp);
darope@0
   533
	if(!n) {
darope@0
   534
		META_ERROR("ini: Warning; no plugins found to load?");
darope@0
   535
	}
darope@0
   536
	return(mTRUE);
darope@0
   537
}
darope@0
   538
darope@0
   539
// Load a plugin from a console command.
darope@0
   540
// meta_errno values:
darope@0
   541
//  - errno's from cmd_parseline()
darope@0
   542
//  - errno's from resolve()
darope@0
   543
//  - ME_ALREADY	this plugin already loaded
darope@0
   544
//  - errno's from add()
darope@0
   545
//  - errno's from load()
darope@0
   546
mBOOL MPluginList::cmd_addload(const char *args) {
darope@0
   547
	MPlugin pl_temp;
darope@0
   548
	MPlugin *pl_found, *pl_added;
darope@0
   549
darope@0
   550
	// XXX move back to comands_meta ?
darope@0
   551
darope@0
   552
	// parse into a temp plugin
darope@0
   553
	memset(&pl_temp, 0, sizeof(pl_temp));
darope@0
   554
	if(pl_temp.cmd_parseline(args) != mTRUE) {
darope@0
   555
		META_CONS("Couldn't parse 'meta load' arguments: %s", args);
darope@0
   556
		// meta_errno should be already set in cmd_parseline()
darope@0
   557
		return(mFALSE);
darope@0
   558
	}
darope@0
   559
darope@0
   560
	// resolve given path into a file; accepts various "shortcut"
darope@0
   561
	// pathnames.
darope@0
   562
	if(pl_temp.resolve() != mTRUE) {
darope@0
   563
		// Couldn't find a matching file on disk
darope@0
   564
		META_CONS("Couldn't resolve given path into a file: %s", 
darope@0
   565
				pl_temp.file);
darope@0
   566
		// meta_errno should be already set in resolve()
darope@0
   567
		return(mFALSE);
darope@0
   568
	}
darope@0
   569
darope@0
   570
	// Try to find plugin with this pathname in the current list of
darope@0
   571
	// plugins.
darope@0
   572
	if((pl_found=find(pl_temp.pathname))) {
darope@0
   573
		// Already in list
darope@0
   574
		META_CONS("Plugin '%s' already in current list; file=%s desc='%s'", 
darope@0
   575
				pl_temp.file, pl_found->file, pl_found->desc);
darope@0
   576
		RETURN_ERRNO(mFALSE, ME_ALREADY);
darope@0
   577
	}
darope@0
   578
	// new plugin; add to list
darope@0
   579
	if(!(pl_added=add(&pl_temp))) {
darope@0
   580
		META_CONS("Couldn't add plugin '%s' to list; see log", pl_temp.desc);
darope@0
   581
		// meta_errno should be already set in add()
darope@0
   582
		return(mFALSE);
darope@0
   583
	}
darope@0
   584
darope@0
   585
	// try to load new plugin
darope@0
   586
	pl_added->action=PA_LOAD;
darope@0
   587
	if(!pl_added->load(PT_ANYTIME)) {
darope@0
   588
		// load failed
darope@0
   589
		if(meta_errno==ME_DELAYED) 
darope@0
   590
			META_CONS("Loaded plugin '%s', but will wait to become active, %s", 
darope@0
   591
					pl_added->desc, pl_added->str_loadable(SL_ALLOWED));
darope@0
   592
		else if(meta_errno==ME_NOTALLOWED) {
darope@0
   593
			META_CONS("Plugin '%s' couldn't attach; only allowed %s", 
darope@0
   594
					pl_added->desc, pl_added->str_loadable(SL_ALLOWED));
darope@0
   595
			pl_added->clear();
darope@0
   596
		}
darope@0
   597
		else if(pl_added->status == PL_OPENED)
darope@0
   598
			META_CONS("Opened plugin '%s', but failed to attach; see log", pl_added->desc);
darope@0
   599
		else
darope@0
   600
			META_CONS("Couldn't load plugin '%s'; see log", pl_added->desc);
bailopan@139
   601
		show(0);
darope@0
   602
		// meta_errno should be already set in load()
darope@0
   603
		return(mFALSE);
darope@0
   604
	}
darope@0
   605
	META_CONS("Loaded plugin '%s' successfully", pl_added->desc);
bailopan@139
   606
	show(0);
darope@0
   607
	return(mTRUE);
darope@0
   608
}
darope@0
   609
darope@0
   610
// Load plugins at startup.
darope@0
   611
// meta_errno values:
darope@0
   612
//  - errno's from ini_startup()
darope@0
   613
mBOOL MPluginList::load() {
darope@0
   614
	int i, n;
darope@0
   615
darope@0
   616
	if (!ini_startup()) {
darope@0
   617
		META_ERROR("Problem loading plugins.ini: %s", inifile);
darope@0
   618
		// meta_errno should be already set in ini_startup()
darope@0
   619
		return(mFALSE);
darope@0
   620
	}
darope@0
   621
darope@0
   622
	META_LOG("dll: Loading plugins...");
darope@0
   623
	for(i=0, n=0; i < endlist; i++) {
darope@0
   624
		if(plist[i].status < PL_VALID)
darope@0
   625
			continue;
darope@0
   626
		if(plist[i].load(PT_STARTUP) == mTRUE)
darope@0
   627
			n++;
darope@0
   628
		else
darope@0
   629
			// all plugins should be loadable at startup...
darope@0
   630
			META_ERROR("dll: Failed to load plugin '%s'", plist[i].file);
darope@0
   631
	}
darope@0
   632
	META_LOG("dll: Finished loading %d plugins", n);
darope@0
   633
	return(mTRUE);
darope@0
   634
}
darope@0
   635
darope@0
   636
// Update list of loaded plugins from ini file, and load any new/changed plugins.
darope@0
   637
// meta_errno values:
darope@0
   638
//  - errno's from ini_refresh()
darope@0
   639
mBOOL MPluginList::refresh(PLUG_LOADTIME now) {
darope@0
   640
	int i, ndone=0, nkept=0, nloaded=0, nunloaded=0, nreloaded=0, ndelayed=0;
darope@0
   641
	MPlugin *iplug;
darope@0
   642
darope@0
   643
	if (!ini_refresh()) {
darope@0
   644
		META_ERROR("dll: Problem reloading plugins.ini: %s", inifile);
darope@0
   645
		// meta_errno should be already set in ini_refresh()
darope@0
   646
		return(mFALSE);
darope@0
   647
	}
darope@0
   648
darope@0
   649
	META_LOG("dll: Updating plugins...");
darope@0
   650
	for(i=0; i < endlist; i++) {
darope@0
   651
		iplug=&plist[i];
darope@0
   652
		if(iplug->status < PL_VALID)
darope@0
   653
			continue;
darope@0
   654
		switch(iplug->action) {
darope@0
   655
			case PA_KEEP:
darope@0
   656
				META_DEBUG(1, ("Keeping plugin '%s'", iplug->desc));
darope@0
   657
				iplug->action=PA_NONE;
darope@0
   658
				nkept++;
darope@0
   659
				break;
darope@0
   660
			case PA_LOAD:
darope@0
   661
				META_DEBUG(1, ("Loading plugin '%s'", iplug->desc));
darope@0
   662
				if(iplug->load(now))
darope@0
   663
					nloaded++;
darope@0
   664
				else if(meta_errno==ME_DELAYED)
darope@0
   665
					ndelayed++;
darope@0
   666
				break;
darope@0
   667
			case PA_RELOAD:
darope@0
   668
				META_DEBUG(1, ("Reloading plugin '%s'", iplug->desc));
darope@0
   669
				if(iplug->reload(now, PNL_FILE_NEWER))
darope@0
   670
					nreloaded++;
darope@0
   671
				else if(meta_errno==ME_DELAYED)
darope@0
   672
					ndelayed++;
darope@0
   673
				break;
darope@0
   674
			case PA_NONE:
darope@0
   675
				// If previously loaded from ini, but apparently removed from new ini.
darope@0
   676
				if(iplug->source==PS_INI && iplug->status >= PL_RUNNING) {
darope@0
   677
					META_DEBUG(1, ("Unloading plugin '%s'", iplug->desc));
darope@0
   678
					iplug->action=PA_UNLOAD;
bailopan@130
   679
					if(iplug->unload(now, PNL_INI_DELETED, PNL_INI_DELETED))
darope@0
   680
						nunloaded++;
darope@0
   681
					else if(meta_errno==ME_DELAYED)
darope@0
   682
						ndelayed++;
darope@0
   683
				}
darope@0
   684
				break;
darope@0
   685
			case PA_ATTACH:
darope@0
   686
				// Previously requested attach, but was delayed?
darope@0
   687
				META_DEBUG(1, ("Retrying attach plugin '%s'", iplug->desc));
darope@0
   688
				if(iplug->retry(now, PNL_DELAYED))
darope@0
   689
					nloaded++;
darope@0
   690
				else if(meta_errno==ME_DELAYED)
darope@0
   691
					ndelayed++;
darope@0
   692
				break;
darope@0
   693
			case PA_UNLOAD:
darope@0
   694
				// Previously requested unload, but was delayed?
darope@0
   695
				META_DEBUG(1, ("Retrying unload plugin '%s'", iplug->desc));
darope@0
   696
				if(iplug->retry(now, PNL_DELAYED))
darope@0
   697
					nunloaded++;
darope@0
   698
				else if(meta_errno==ME_DELAYED)
darope@0
   699
					ndelayed++;
darope@0
   700
				break;
darope@0
   701
			case PA_NULL:
darope@0
   702
				META_ERROR("dll: Unexpected action for plugin '%s': '%s'", iplug->desc, iplug->str_action());
darope@0
   703
				break;
darope@0
   704
			default:
darope@0
   705
				META_ERROR("dll: Unrecognized action for plugin '%s': '%s'", iplug->desc, iplug->str_action());
darope@0
   706
				break;
darope@0
   707
		}
darope@0
   708
		ndone++;
darope@0
   709
	}
darope@0
   710
	META_LOG("dll: Finished updating %d plugins; kept %d, loaded %d, unloaded %d, reloaded %d, delayed %d", 
darope@0
   711
			ndone, nkept, nloaded, nunloaded, nreloaded, ndelayed);
darope@0
   712
	return(mTRUE);
darope@0
   713
}
darope@0
   714
darope@0
   715
// Re-enable any plugins currently paused.
darope@0
   716
// meta_errno values:
darope@0
   717
//  - none
darope@0
   718
void MPluginList::unpause_all(void) {
darope@0
   719
	int i;
darope@0
   720
	MPlugin *iplug;
darope@0
   721
	for(i=0; i < endlist; i++) {
darope@0
   722
		iplug=&plist[i];
darope@0
   723
		if(iplug->status==PL_PAUSED)
darope@0
   724
			iplug->unpause();
darope@0
   725
	}
darope@0
   726
}
darope@0
   727
darope@0
   728
// Retry any pending actions on plugins, for instance load/unload delayed
darope@0
   729
// until changelevel.
darope@0
   730
// meta_errno values:
darope@0
   731
//  - none
darope@0
   732
void MPluginList::retry_all(PLUG_LOADTIME now) {
darope@0
   733
	int i;
darope@0
   734
	MPlugin *iplug;
darope@0
   735
	for(i=0; i < endlist; i++) {
darope@0
   736
		iplug=&plist[i];
darope@0
   737
		if(iplug->action != PA_NONE)
darope@0
   738
			iplug->retry(now, PNL_DELAYED);
darope@0
   739
	}
darope@0
   740
}
darope@0
   741
darope@0
   742
// List plugins and information about them in a formatted table.
darope@0
   743
// meta_errno values:
darope@0
   744
//  - none
bailopan@139
   745
void MPluginList::show(int source_index) {
darope@0
   746
	int i, n=0, r=0;
darope@0
   747
	MPlugin *pl;
darope@0
   748
	char desc[15+1], file[16+1], vers[7+1];		// plus 1 for term null
darope@0
   749
bailopan@139
   750
	if (source_index <= 0)
bailopan@139
   751
		META_CONS("Currently loaded plugins:");
bailopan@139
   752
	else
bailopan@139
   753
		META_CONS("Child plugins:");
bailopan@139
   754
darope@0
   755
	META_CONS("  %*s  %-*s  %-4s %-4s  %-*s  v%-*s  %-3s  %-5s %-5s",
darope@0
   756
			WIDTH_MAX_PLUGINS, "",
darope@0
   757
			sizeof(desc)-1, "description",
darope@0
   758
			"stat", "pend",
darope@0
   759
			sizeof(file)-1, "file", sizeof(vers)-1, "ers",
darope@0
   760
			"src", "load ", "unlod");
darope@0
   761
	for(i=0; i < endlist; i++) {
darope@0
   762
		pl=&plist[i];
darope@0
   763
		if(pl->status < PL_VALID)
darope@0
   764
			continue;
bailopan@139
   765
		if ((source_index > 0) && (pl->source_plugin_index != source_index))
bailopan@139
   766
			continue;
darope@0
   767
		STRNCPY(desc, pl->desc, sizeof(desc));
darope@0
   768
		STRNCPY(file, pl->file, sizeof(file));
darope@0
   769
		if(pl->info && pl->info->version) STRNCPY(vers, pl->info->version, sizeof(vers));
darope@0
   770
		else STRNCPY(vers, " -", sizeof(vers));
darope@0
   771
		META_CONS(" [%*d] %-*s  %-4s %-4s  %-*s  v%-*s  %-3s  %-5s %-5s",
darope@0
   772
				WIDTH_MAX_PLUGINS, pl->index, 
darope@0
   773
				sizeof(desc)-1, desc,
darope@0
   774
				pl->str_status(ST_SHOW), pl->str_action(SA_SHOW),
darope@0
   775
				sizeof(file)-1, file, sizeof(vers)-1, vers,
darope@0
   776
				pl->str_source(SO_SHOW),
darope@0
   777
				pl->str_loadable(SL_SHOW), pl->str_unloadable(SL_SHOW));
darope@0
   778
		if(pl->status == PL_RUNNING)
darope@0
   779
			r++;
darope@0
   780
		n++;
darope@0
   781
	}
darope@0
   782
	META_CONS("%d plugins, %d running", n, r);
darope@0
   783
}
darope@0
   784
darope@0
   785
// List plugins and information to Player/client entity.  Differs from the
darope@0
   786
// "meta list" console command in that:
darope@0
   787
//  - Shows only "running" plugins, skipping any failed or paused plugins.
darope@0
   788
//  - Limited info about each plugin, mostly the "public" info (name, author, 
darope@0
   789
//    etc).
darope@0
   790
// meta_errno values:
darope@0
   791
//  - none
darope@0
   792
void MPluginList::show_client(edict_t *pEntity) {
darope@0
   793
	int i, n=0;
darope@0
   794
	MPlugin *pl;
darope@0
   795
	META_CLIENT(pEntity, "Currently running plugins:");
darope@0
   796
	for(i=0; i < endlist; i++) {
darope@0
   797
		pl=&plist[i];
darope@0
   798
		if(pl->status != PL_RUNNING)
darope@0
   799
			continue;
darope@0
   800
		n++;
darope@0
   801
		META_CLIENT(pEntity, " [%3d] %s, v%s, %s, by %s, see %s",
darope@0
   802
				n, 
darope@0
   803
				pl->info->name ? pl->info->name : "<unknown>",
darope@0
   804
				pl->info->version ? pl->info->version : "<?>",
darope@0
   805
				pl->info->date ? pl->info->date : "<../../..>",
darope@0
   806
				pl->info->author ? pl->info->author : "<unknown>",
darope@0
   807
				pl->info->url ? pl->info->url : "<unknown>");
darope@0
   808
	}
darope@0
   809
	META_CLIENT(pEntity, "%d plugins", n);
darope@0
   810
}
bailopan@139
   811
bailopan@139
   812
mBOOL MPluginList::found_child_plugins(int source_index)
bailopan@139
   813
{
bailopan@139
   814
	int i;
bailopan@139
   815
bailopan@139
   816
	if (source_index <= 0)
bailopan@139
   817
		return mFALSE;
bailopan@139
   818
bailopan@139
   819
	for (i=0; i<endlist; i++)
bailopan@139
   820
	{
bailopan@139
   821
		if (plist[i].status < PL_VALID)
bailopan@139
   822
			continue;
bailopan@139
   823
		if (plist[i].source_plugin_index == source_index)
bailopan@139
   824
			return mTRUE;
bailopan@139
   825
	}
bailopan@139
   826
bailopan@139
   827
	return mFALSE;
bailopan@139
   828
}
bailopan@139
   829
bailopan@139
   830