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