metamod/mlist.cpp
author darope
Sat Aug 21 16:05:39 2004 +0000 (2004-08-21)
branchB-work
changeset 98 fcec63b539c6
parent 93 5ade74e57e67
child 128 a6c7b16a6b92
permissions -rw-r--r--
Made skipping of duplicate lines on inital plugins.ini parsing an info
instead of an error.

Prevent loading of duplicate plugins when e.g. lin32 and linux lines are
added to plugins.ini between map changes. The logic leaves many a thing to
be desired, but making it waterproof takes too much time now.
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@91
   198
darope@91
   199
// Find a plugin with same file, logtag, desc or significant 
darope@91
   200
// prefix of file. Uses the platform_match() method of MPlugin.
darope@91
   201
// meta_errno values:
darope@91
   202
//  - ME_ARGUMENT	null prefix
darope@91
   203
//  - ME_NOTFOUND	couldn't find a matching plugin
darope@91
   204
MPlugin *MPluginList::find_match(MPlugin *pmatch) {
darope@91
   205
	int i;
darope@91
   206
	MPlugin *iplug, *pfound;
darope@91
   207
	if(!pmatch)
darope@91
   208
		RETURN_ERRNO(NULL, ME_ARGUMENT);
darope@91
   209
	pfound=NULL;
darope@91
   210
	for(i=0; i < endlist; i++) {
darope@91
   211
		iplug=&plist[i];
darope@91
   212
		if(pmatch->platform_match(iplug)) {
darope@91
   213
			pfound=iplug;
darope@91
   214
			break;
darope@91
   215
		}
darope@91
   216
	}
darope@91
   217
darope@91
   218
	if(pfound)
darope@91
   219
		return(pfound);
darope@91
   220
	else
darope@91
   221
		RETURN_ERRNO(NULL, ME_NOTFOUND);
darope@91
   222
}
darope@91
   223
darope@91
   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@91
   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@91
   310
			// Should we check platform specific level here?
darope@98
   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@91
   315
		// Check for a matching platform with different platform specifics
darope@91
   316
		// level.
darope@91
   317
		if(NULL != (pmatch=find_match(&plist[n]))) {
darope@91
   318
			if(pmatch->pfspecific >= plist[n].pfspecific) {
darope@91
   319
				META_DEBUG(1, ("ini: Skipping plugin, line %d of %s: plugin with higher platform specific level already exists. (%d >= %d)",
darope@98
   320
                         ln, inifile, pmatch->pfspecific, plist[n].pfspecific)); 
darope@91
   321
				continue;
darope@91
   322
			}
darope@91
   323
			META_DEBUG(1, ("ini: Plugin in line %d overrides existing plugin with lower platform specific level %d, ours %d",
darope@98
   324
					ln, pmatch->pfspecific, plist[n].pfspecific));
darope@91
   325
			memset(pmatch, 0, sizeof(MPlugin));
darope@91
   326
		}
darope@0
   327
		plist[n].action=PA_LOAD;
darope@0
   328
		META_LOG("ini: Read plugin config for: %s", plist[n].desc);
darope@0
   329
		n++;
darope@0
   330
		endlist=n;		// mark end of list
darope@0
   331
	}
darope@0
   332
	META_LOG("ini: Finished reading plugins list: %s; Found %d plugins to load",
darope@0
   333
			inifile, n);
darope@0
   334
darope@0
   335
	fclose(fp);
darope@0
   336
	if(!n) {
darope@0
   337
		META_ERROR("ini: Warning; no plugins found to load?");
darope@0
   338
	}
darope@0
   339
	return(mTRUE);
darope@0
   340
}
darope@0
   341
darope@0
   342
// Re-read plugins.ini looking for added/deleted/changed plugins.
darope@0
   343
// meta_errno values:
darope@0
   344
//  - ME_NOFILE		ini file missing or empty
darope@0
   345
mBOOL MPluginList::ini_refresh() {
darope@0
   346
	FILE *fp;
darope@0
   347
	char line[MAX_STRBUF_LEN];
darope@0
   348
	int n, ln;
darope@0
   349
	MPlugin pl_temp;
darope@0
   350
	MPlugin *pl_found, *pl_added;
darope@0
   351
darope@0
   352
	fp=fopen(inifile, "r");
darope@0
   353
	if(!fp) {
darope@0
   354
		META_ERROR("ini: Unable to open plugins file '%s': %s", inifile, 
darope@0
   355
				strerror(errno));
darope@0
   356
		RETURN_ERRNO(mFALSE, ME_NOFILE);
darope@0
   357
	}
darope@0
   358
darope@0
   359
	META_LOG("ini: Begin re-reading plugins list: %s", inifile);
darope@0
   360
	for(n=0, ln=1; 
darope@0
   361
			!feof(fp) && fgets(line, sizeof(line), fp) && n < size; 
darope@0
   362
			ln++) 
darope@0
   363
	{
darope@0
   364
		// Remove line terminations.
darope@0
   365
		char *cp;
darope@0
   366
		if((cp=strrchr(line, '\r')))
darope@0
   367
			*cp='\0';
darope@0
   368
		if((cp=strrchr(line, '\n')))
darope@0
   369
			*cp='\0';
darope@0
   370
		// Parse into a temp plugin
darope@0
   371
		memset(&pl_temp, 0, sizeof(pl_temp));
darope@0
   372
		if(!pl_temp.ini_parseline(line)) {
darope@0
   373
			if(meta_errno==ME_FORMAT)
darope@0
   374
				META_ERROR("ini: Skipping malformed line %d of %s", 
darope@0
   375
						ln, inifile);
darope@0
   376
			continue;
darope@0
   377
		}
darope@0
   378
		// Try to find plugin with this pathname in the current list of
darope@0
   379
		// plugins.
darope@0
   380
		if(!(pl_found=find(pl_temp.pathname))) {
darope@93
   381
			// Check for a matching platform with higher platform specifics
darope@93
   382
			// level.
darope@93
   383
			if(NULL != (pl_found=find_match(&pl_temp))) {
darope@93
   384
				if(pl_found->pfspecific >= pl_temp.pfspecific) {
darope@93
   385
					META_DEBUG(1, ("ini: Skipping plugin, line %d of %s: plugin with higher platform specific level already exists. (%d >= %d)",
darope@93
   386
                       		 ln, inifile, pl_found->pfspecific, pl_temp.pfspecific)); 
darope@93
   387
					continue;
darope@93
   388
				}
darope@98
   389
				if(PA_LOAD == pl_found->action) {
darope@98
   390
					META_DEBUG(1, ("ini: Plugin in line %d overrides loading of plugin with lower platform specific level %d, ours %d",
darope@98
   391
								ln, pl_found->pfspecific, pl_temp.pfspecific));
darope@98
   392
					memset(pl_found, 0, sizeof(MPlugin));
darope@98
   393
				}
darope@98
   394
				else {
darope@98
   395
					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
   396
								ln, pl_found->pfspecific, pl_temp.pfspecific));
darope@98
   397
					continue;
darope@98
   398
				}
darope@93
   399
			}
darope@0
   400
			// new plugin; add to list
darope@0
   401
			if((pl_added=add(&pl_temp))) {
darope@0
   402
				// try to load this plugin at the next opportunity
darope@0
   403
				pl_added->action=PA_LOAD;
darope@0
   404
			}
darope@0
   405
			else
darope@0
   406
				// error details logged in add()
darope@0
   407
				continue;
darope@0
   408
		}
darope@0
   409
		else {
darope@0
   410
			// This plugin is already in the current list of plugins.
darope@0
   411
			// Pathname already matches.  Recopy desc, if specified in
darope@0
   412
			// plugins.ini.
darope@0
   413
			if(pl_temp.desc[0] != '<')
darope@0
   414
				STRNCPY(pl_found->desc, pl_temp.desc, sizeof(pl_found->desc));
darope@0
   415
darope@0
   416
			// Check the file to see if it looks like it's been modified
darope@0
   417
			// since we last loaded it.
darope@0
   418
			if(!pl_found->newer_file()) {
darope@0
   419
				if(meta_errno==ME_NOFILE) {
darope@0
   420
					META_ERROR("ini: Skipping plugin, couldn't stat file '%s': %s",
darope@0
   421
							pl_found->pathname, strerror(errno));
darope@0
   422
					continue;
darope@0
   423
				}
darope@0
   424
				else {
darope@0
   425
					// File hasn't been updated.
darope@0
   426
					// Keep plugin (don't let refresh() unload it).
darope@0
   427
					pl_found->action=PA_KEEP;
darope@0
   428
				}
darope@0
   429
			}
darope@0
   430
			// Newer file on disk.
darope@0
   431
			else if(pl_found->status >= PL_OPENED)  {
darope@0
   432
				META_DEBUG(2, ("ini: Plugin '%s' has newer file on disk", pl_found->desc));
darope@0
   433
				pl_found->action=PA_RELOAD;
darope@0
   434
			}
darope@0
   435
			else
darope@0
   436
				META_ERROR("ini: Plugin '%s' has newer file, but unexpected status (%s)",
darope@0
   437
						pl_found->desc, pl_found->str_status());
darope@0
   438
		}
darope@98
   439
		if(NULL != pl_found) {
darope@98
   440
			META_LOG("ini: Read plugin config for: %s", pl_found->desc);
darope@98
   441
		}
darope@98
   442
		else {
darope@98
   443
			META_LOG("ini: Read plugin config for: %s", pl_temp.desc);
darope@98
   444
		}
darope@0
   445
		n++;
darope@0
   446
	}
darope@0
   447
	META_LOG("ini: Finished reading plugins list: %s; Found %d plugins", inifile, n);
darope@0
   448
darope@0
   449
	fclose(fp);
darope@0
   450
	if(!n) {
darope@0
   451
		META_ERROR("ini: Warning; no plugins found to load?");
darope@0
   452
	}
darope@0
   453
	return(mTRUE);
darope@0
   454
}
darope@0
   455
darope@0
   456
// Load a plugin from a console command.
darope@0
   457
// meta_errno values:
darope@0
   458
//  - errno's from cmd_parseline()
darope@0
   459
//  - errno's from resolve()
darope@0
   460
//  - ME_ALREADY	this plugin already loaded
darope@0
   461
//  - errno's from add()
darope@0
   462
//  - errno's from load()
darope@0
   463
mBOOL MPluginList::cmd_addload(const char *args) {
darope@0
   464
	MPlugin pl_temp;
darope@0
   465
	MPlugin *pl_found, *pl_added;
darope@0
   466
darope@0
   467
	// XXX move back to comands_meta ?
darope@0
   468
darope@0
   469
	// parse into a temp plugin
darope@0
   470
	memset(&pl_temp, 0, sizeof(pl_temp));
darope@0
   471
	if(pl_temp.cmd_parseline(args) != mTRUE) {
darope@0
   472
		META_CONS("Couldn't parse 'meta load' arguments: %s", args);
darope@0
   473
		// meta_errno should be already set in cmd_parseline()
darope@0
   474
		return(mFALSE);
darope@0
   475
	}
darope@0
   476
darope@0
   477
	// resolve given path into a file; accepts various "shortcut"
darope@0
   478
	// pathnames.
darope@0
   479
	if(pl_temp.resolve() != mTRUE) {
darope@0
   480
		// Couldn't find a matching file on disk
darope@0
   481
		META_CONS("Couldn't resolve given path into a file: %s", 
darope@0
   482
				pl_temp.file);
darope@0
   483
		// meta_errno should be already set in resolve()
darope@0
   484
		return(mFALSE);
darope@0
   485
	}
darope@0
   486
darope@0
   487
	// Try to find plugin with this pathname in the current list of
darope@0
   488
	// plugins.
darope@0
   489
	if((pl_found=find(pl_temp.pathname))) {
darope@0
   490
		// Already in list
darope@0
   491
		META_CONS("Plugin '%s' already in current list; file=%s desc='%s'", 
darope@0
   492
				pl_temp.file, pl_found->file, pl_found->desc);
darope@0
   493
		RETURN_ERRNO(mFALSE, ME_ALREADY);
darope@0
   494
	}
darope@0
   495
	// new plugin; add to list
darope@0
   496
	if(!(pl_added=add(&pl_temp))) {
darope@0
   497
		META_CONS("Couldn't add plugin '%s' to list; see log", pl_temp.desc);
darope@0
   498
		// meta_errno should be already set in add()
darope@0
   499
		return(mFALSE);
darope@0
   500
	}
darope@0
   501
darope@0
   502
	// try to load new plugin
darope@0
   503
	pl_added->action=PA_LOAD;
darope@0
   504
	if(!pl_added->load(PT_ANYTIME)) {
darope@0
   505
		// load failed
darope@0
   506
		if(meta_errno==ME_DELAYED) 
darope@0
   507
			META_CONS("Loaded plugin '%s', but will wait to become active, %s", 
darope@0
   508
					pl_added->desc, pl_added->str_loadable(SL_ALLOWED));
darope@0
   509
		else if(meta_errno==ME_NOTALLOWED) {
darope@0
   510
			META_CONS("Plugin '%s' couldn't attach; only allowed %s", 
darope@0
   511
					pl_added->desc, pl_added->str_loadable(SL_ALLOWED));
darope@0
   512
			pl_added->clear();
darope@0
   513
		}
darope@0
   514
		else if(pl_added->status == PL_OPENED)
darope@0
   515
			META_CONS("Opened plugin '%s', but failed to attach; see log", pl_added->desc);
darope@0
   516
		else
darope@0
   517
			META_CONS("Couldn't load plugin '%s'; see log", pl_added->desc);
darope@0
   518
		show();
darope@0
   519
		// meta_errno should be already set in load()
darope@0
   520
		return(mFALSE);
darope@0
   521
	}
darope@0
   522
	META_CONS("Loaded plugin '%s' successfully", pl_added->desc);
darope@0
   523
	show();
darope@0
   524
	return(mTRUE);
darope@0
   525
}
darope@0
   526
darope@0
   527
// Load plugins at startup.
darope@0
   528
// meta_errno values:
darope@0
   529
//  - errno's from ini_startup()
darope@0
   530
mBOOL MPluginList::load() {
darope@0
   531
	int i, n;
darope@0
   532
darope@0
   533
	if (!ini_startup()) {
darope@0
   534
		META_ERROR("Problem loading plugins.ini: %s", inifile);
darope@0
   535
		// meta_errno should be already set in ini_startup()
darope@0
   536
		return(mFALSE);
darope@0
   537
	}
darope@0
   538
darope@0
   539
	META_LOG("dll: Loading plugins...");
darope@0
   540
	for(i=0, n=0; i < endlist; i++) {
darope@0
   541
		if(plist[i].status < PL_VALID)
darope@0
   542
			continue;
darope@0
   543
		if(plist[i].load(PT_STARTUP) == mTRUE)
darope@0
   544
			n++;
darope@0
   545
		else
darope@0
   546
			// all plugins should be loadable at startup...
darope@0
   547
			META_ERROR("dll: Failed to load plugin '%s'", plist[i].file);
darope@0
   548
	}
darope@0
   549
	META_LOG("dll: Finished loading %d plugins", n);
darope@0
   550
	return(mTRUE);
darope@0
   551
}
darope@0
   552
darope@0
   553
// Update list of loaded plugins from ini file, and load any new/changed plugins.
darope@0
   554
// meta_errno values:
darope@0
   555
//  - errno's from ini_refresh()
darope@0
   556
mBOOL MPluginList::refresh(PLUG_LOADTIME now) {
darope@0
   557
	int i, ndone=0, nkept=0, nloaded=0, nunloaded=0, nreloaded=0, ndelayed=0;
darope@0
   558
	MPlugin *iplug;
darope@0
   559
darope@0
   560
	if (!ini_refresh()) {
darope@0
   561
		META_ERROR("dll: Problem reloading plugins.ini: %s", inifile);
darope@0
   562
		// meta_errno should be already set in ini_refresh()
darope@0
   563
		return(mFALSE);
darope@0
   564
	}
darope@0
   565
darope@0
   566
	META_LOG("dll: Updating plugins...");
darope@0
   567
	for(i=0; i < endlist; i++) {
darope@0
   568
		iplug=&plist[i];
darope@0
   569
		if(iplug->status < PL_VALID)
darope@0
   570
			continue;
darope@0
   571
		switch(iplug->action) {
darope@0
   572
			case PA_KEEP:
darope@0
   573
				META_DEBUG(1, ("Keeping plugin '%s'", iplug->desc));
darope@0
   574
				iplug->action=PA_NONE;
darope@0
   575
				nkept++;
darope@0
   576
				break;
darope@0
   577
			case PA_LOAD:
darope@0
   578
				META_DEBUG(1, ("Loading plugin '%s'", iplug->desc));
darope@0
   579
				if(iplug->load(now))
darope@0
   580
					nloaded++;
darope@0
   581
				else if(meta_errno==ME_DELAYED)
darope@0
   582
					ndelayed++;
darope@0
   583
				break;
darope@0
   584
			case PA_RELOAD:
darope@0
   585
				META_DEBUG(1, ("Reloading plugin '%s'", iplug->desc));
darope@0
   586
				if(iplug->reload(now, PNL_FILE_NEWER))
darope@0
   587
					nreloaded++;
darope@0
   588
				else if(meta_errno==ME_DELAYED)
darope@0
   589
					ndelayed++;
darope@0
   590
				break;
darope@0
   591
			case PA_NONE:
darope@0
   592
				// If previously loaded from ini, but apparently removed from new ini.
darope@0
   593
				if(iplug->source==PS_INI && iplug->status >= PL_RUNNING) {
darope@0
   594
					META_DEBUG(1, ("Unloading plugin '%s'", iplug->desc));
darope@0
   595
					iplug->action=PA_UNLOAD;
darope@0
   596
					if(iplug->unload(now, PNL_INI_DELETED))
darope@0
   597
						nunloaded++;
darope@0
   598
					else if(meta_errno==ME_DELAYED)
darope@0
   599
						ndelayed++;
darope@0
   600
				}
darope@0
   601
				break;
darope@0
   602
			case PA_ATTACH:
darope@0
   603
				// Previously requested attach, but was delayed?
darope@0
   604
				META_DEBUG(1, ("Retrying attach plugin '%s'", iplug->desc));
darope@0
   605
				if(iplug->retry(now, PNL_DELAYED))
darope@0
   606
					nloaded++;
darope@0
   607
				else if(meta_errno==ME_DELAYED)
darope@0
   608
					ndelayed++;
darope@0
   609
				break;
darope@0
   610
			case PA_UNLOAD:
darope@0
   611
				// Previously requested unload, but was delayed?
darope@0
   612
				META_DEBUG(1, ("Retrying unload plugin '%s'", iplug->desc));
darope@0
   613
				if(iplug->retry(now, PNL_DELAYED))
darope@0
   614
					nunloaded++;
darope@0
   615
				else if(meta_errno==ME_DELAYED)
darope@0
   616
					ndelayed++;
darope@0
   617
				break;
darope@0
   618
			case PA_NULL:
darope@0
   619
				META_ERROR("dll: Unexpected action for plugin '%s': '%s'", iplug->desc, iplug->str_action());
darope@0
   620
				break;
darope@0
   621
			default:
darope@0
   622
				META_ERROR("dll: Unrecognized action for plugin '%s': '%s'", iplug->desc, iplug->str_action());
darope@0
   623
				break;
darope@0
   624
		}
darope@0
   625
		ndone++;
darope@0
   626
	}
darope@0
   627
	META_LOG("dll: Finished updating %d plugins; kept %d, loaded %d, unloaded %d, reloaded %d, delayed %d", 
darope@0
   628
			ndone, nkept, nloaded, nunloaded, nreloaded, ndelayed);
darope@0
   629
	return(mTRUE);
darope@0
   630
}
darope@0
   631
darope@0
   632
// Re-enable any plugins currently paused.
darope@0
   633
// meta_errno values:
darope@0
   634
//  - none
darope@0
   635
void MPluginList::unpause_all(void) {
darope@0
   636
	int i;
darope@0
   637
	MPlugin *iplug;
darope@0
   638
	for(i=0; i < endlist; i++) {
darope@0
   639
		iplug=&plist[i];
darope@0
   640
		if(iplug->status==PL_PAUSED)
darope@0
   641
			iplug->unpause();
darope@0
   642
	}
darope@0
   643
}
darope@0
   644
darope@0
   645
// Retry any pending actions on plugins, for instance load/unload delayed
darope@0
   646
// until changelevel.
darope@0
   647
// meta_errno values:
darope@0
   648
//  - none
darope@0
   649
void MPluginList::retry_all(PLUG_LOADTIME now) {
darope@0
   650
	int i;
darope@0
   651
	MPlugin *iplug;
darope@0
   652
	for(i=0; i < endlist; i++) {
darope@0
   653
		iplug=&plist[i];
darope@0
   654
		if(iplug->action != PA_NONE)
darope@0
   655
			iplug->retry(now, PNL_DELAYED);
darope@0
   656
	}
darope@0
   657
}
darope@0
   658
darope@0
   659
// List plugins and information about them in a formatted table.
darope@0
   660
// meta_errno values:
darope@0
   661
//  - none
darope@0
   662
void MPluginList::show() {
darope@0
   663
	int i, n=0, r=0;
darope@0
   664
	MPlugin *pl;
darope@0
   665
	char desc[15+1], file[16+1], vers[7+1];		// plus 1 for term null
darope@0
   666
darope@0
   667
	META_CONS("Currently loaded plugins:");
darope@0
   668
	META_CONS("  %*s  %-*s  %-4s %-4s  %-*s  v%-*s  %-3s  %-5s %-5s",
darope@0
   669
			WIDTH_MAX_PLUGINS, "",
darope@0
   670
			sizeof(desc)-1, "description",
darope@0
   671
			"stat", "pend",
darope@0
   672
			sizeof(file)-1, "file", sizeof(vers)-1, "ers",
darope@0
   673
			"src", "load ", "unlod");
darope@0
   674
	for(i=0; i < endlist; i++) {
darope@0
   675
		pl=&plist[i];
darope@0
   676
		if(pl->status < PL_VALID)
darope@0
   677
			continue;
darope@0
   678
		STRNCPY(desc, pl->desc, sizeof(desc));
darope@0
   679
		STRNCPY(file, pl->file, sizeof(file));
darope@0
   680
		if(pl->info && pl->info->version) STRNCPY(vers, pl->info->version, sizeof(vers));
darope@0
   681
		else STRNCPY(vers, " -", sizeof(vers));
darope@0
   682
		META_CONS(" [%*d] %-*s  %-4s %-4s  %-*s  v%-*s  %-3s  %-5s %-5s",
darope@0
   683
				WIDTH_MAX_PLUGINS, pl->index, 
darope@0
   684
				sizeof(desc)-1, desc,
darope@0
   685
				pl->str_status(ST_SHOW), pl->str_action(SA_SHOW),
darope@0
   686
				sizeof(file)-1, file, sizeof(vers)-1, vers,
darope@0
   687
				pl->str_source(SO_SHOW),
darope@0
   688
				pl->str_loadable(SL_SHOW), pl->str_unloadable(SL_SHOW));
darope@0
   689
		if(pl->status == PL_RUNNING)
darope@0
   690
			r++;
darope@0
   691
		n++;
darope@0
   692
	}
darope@0
   693
	META_CONS("%d plugins, %d running", n, r);
darope@0
   694
}
darope@0
   695
darope@0
   696
// List plugins and information to Player/client entity.  Differs from the
darope@0
   697
// "meta list" console command in that:
darope@0
   698
//  - Shows only "running" plugins, skipping any failed or paused plugins.
darope@0
   699
//  - Limited info about each plugin, mostly the "public" info (name, author, 
darope@0
   700
//    etc).
darope@0
   701
// meta_errno values:
darope@0
   702
//  - none
darope@0
   703
void MPluginList::show_client(edict_t *pEntity) {
darope@0
   704
	int i, n=0;
darope@0
   705
	MPlugin *pl;
darope@0
   706
	META_CLIENT(pEntity, "Currently running plugins:");
darope@0
   707
	for(i=0; i < endlist; i++) {
darope@0
   708
		pl=&plist[i];
darope@0
   709
		if(pl->status != PL_RUNNING)
darope@0
   710
			continue;
darope@0
   711
		n++;
darope@0
   712
		META_CLIENT(pEntity, " [%3d] %s, v%s, %s, by %s, see %s",
darope@0
   713
				n, 
darope@0
   714
				pl->info->name ? pl->info->name : "<unknown>",
darope@0
   715
				pl->info->version ? pl->info->version : "<?>",
darope@0
   716
				pl->info->date ? pl->info->date : "<../../..>",
darope@0
   717
				pl->info->author ? pl->info->author : "<unknown>",
darope@0
   718
				pl->info->url ? pl->info->url : "<unknown>");
darope@0
   719
	}
darope@0
   720
	META_CLIENT(pEntity, "%d plugins", n);
darope@0
   721
}