metamod/mplugin.cpp
author David Anderson <dvander@alliedmods.net>
Tue Feb 12 20:21:43 2013 -0600 (2013-02-12)
changeset 261 9b9bafc4d98f
parent 259 514842915905
child 267 b75724ad6b58
permissions -rw-r--r--
Const-qualify char pointers that take string literals, for GCC 4 support (no bug, r=me, a=florian).
darope@0
     1
// vi: set ts=4 sw=4 :
darope@0
     2
// vim: set tw=75 :
darope@0
     3
darope@0
     4
// mplugin.cpp - functions for individual plugin (class MPlugin)
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
#include <malloc.h>				// malloc, etc
darope@0
    39
#include <sys/types.h>			// stat
darope@0
    40
#include <sys/stat.h>			// stat
darope@0
    41
darope@0
    42
#include <extdll.h>				// always
darope@0
    43
darope@0
    44
#include "mplugin.h"			// me
darope@0
    45
#include "metamod.h"			// GameDLL, etc
darope@0
    46
#include "mreg.h"				// MRegCmdList::show(int), etc
darope@0
    47
#include "mhook.h"				// class MHook
darope@0
    48
#include "h_export.h"			// GIVE_ENGINE_FUNCTIONS_FN, etc
darope@0
    49
#include "dllapi.h"				// FN_GAMEINIT, etc
darope@0
    50
#include "support_meta.h"		// full_gamedir_path,
darope@0
    51
#include "types_meta.h"			// mBOOL
darope@0
    52
#include "log_meta.h"			// logging functions, etc
darope@0
    53
#include "osdep.h"				// win32 snprintf, is_absolute_path,
darope@0
    54
darope@0
    55
darope@0
    56
// Parse a line from plugins.ini into a plugin.
darope@0
    57
// meta_errno values:
darope@0
    58
//  - ME_COMMENT	ignored commented line
darope@0
    59
//  - ME_FORMAT		invalid line format
darope@0
    60
//  - ME_OSNOTSUP	plugin is not for this OS
darope@0
    61
mBOOL MPlugin::ini_parseline(char *line) {
darope@0
    62
	char *token;
darope@0
    63
	char *ptr_token;
darope@0
    64
	char *cp;
darope@0
    65
darope@244
    66
	// skip whitespace at start of line
darope@244
    67
	while(*line==' ' || *line=='\t' ) line++;
darope@244
    68
darope@245
    69
	// remove whitespace at end of line
darope@245
    70
	cp = line + strlen(line) -1;
darope@245
    71
	while(*cp==' ' || *cp=='\t') *cp--='\0';
darope@245
    72
darope@244
    73
	// skip empty lines
darope@244
    74
	if(line[0]=='\0') {
darope@244
    75
		META_DEBUG(7, ("ini: Ignoring empty line: %s", line));
darope@244
    76
		RETURN_ERRNO(mFALSE, ME_BLANK);
darope@244
    77
	}
darope@244
    78
darope@0
    79
	if(line[0]=='#' || line[0]==';' || strstr(line, "//")==line) {
darope@0
    80
		META_DEBUG(7, ("ini: Ignoring commented line: %s", line));
darope@0
    81
		RETURN_ERRNO(mFALSE, ME_COMMENT);
darope@0
    82
	}
darope@0
    83
darope@0
    84
	// grab platform ("win32" or "linux")
darope@0
    85
	token=strtok_r(line, " \t", &ptr_token);
darope@0
    86
	if(!token)
darope@0
    87
		RETURN_ERRNO(mFALSE, ME_FORMAT);
darope@244
    88
	if(strcasecmp(token, PLATFORM) == 0) {
darope@244
    89
		pfspecific=0;
darope@244
    90
	} else if(strcasecmp(token, PLATFORM_SPC) == 0) {
darope@244
    91
		pfspecific=1;
darope@244
    92
	} else {
darope@0
    93
		// plugin is not for this OS
darope@0
    94
		META_DEBUG(7, ("ini: Ignoring entry for %s", token));
darope@0
    95
		RETURN_ERRNO(mFALSE, ME_OSNOTSUP);
darope@0
    96
	}
darope@0
    97
darope@0
    98
	// grab filename
darope@0
    99
	token=strtok_r(NULL, " \t\r\n", &ptr_token);
darope@0
   100
	if(!token)
darope@0
   101
		RETURN_ERRNO(mFALSE, ME_FORMAT);
darope@0
   102
	STRNCPY(filename, token, sizeof(filename));
darope@0
   103
	normalize_pathname(filename);
darope@0
   104
	// Store name of just the actual _file_, without dir components.
darope@0
   105
	cp=strrchr(filename, '/');
darope@0
   106
	if(cp)
darope@0
   107
		file=cp+1;
darope@0
   108
	else
darope@0
   109
		file=filename;
darope@0
   110
darope@0
   111
	// Grab description.
darope@0
   112
	// Just get the the rest of the line, minus line-termination.
darope@0
   113
	token=strtok_r(NULL, "\n\r", &ptr_token);
darope@0
   114
	if(token) {
darope@0
   115
		token=token+strspn(token, " \t");			// skip whitespace
darope@0
   116
		STRNCPY(desc, token, sizeof(desc));
darope@0
   117
	}
darope@0
   118
	else {
darope@0
   119
		// If no description is specified, temporarily use plugin file,
darope@0
   120
		// until plugin can be queried, and desc replaced with info->name.
darope@0
   121
		snprintf(desc, sizeof(desc), "<%s>", file);
darope@0
   122
	}
darope@0
   123
darope@0
   124
	// Make full pathname (from gamedir if relative, remove "..",
darope@0
   125
	// backslashes, etc).
darope@0
   126
	full_gamedir_path(filename, pathname);
darope@0
   127
darope@0
   128
	source=PS_INI;
darope@0
   129
	status=PL_VALID;
darope@0
   130
	return(mTRUE);
darope@0
   131
}
darope@0
   132
darope@248
   133
// Unload a plugin from plugin request
darope@248
   134
// meta_errno values:
darope@248
   135
//  - errno's from unload()
darope@248
   136
mBOOL MPlugin::plugin_unload(plid_t plid, PLUG_LOADTIME now, PL_UNLOAD_REASON reason)
darope@248
   137
{
darope@248
   138
	PLUG_ACTION old_action;
darope@248
   139
	MPlugin *pl_unloader;
darope@248
   140
darope@248
   141
	if ( !(pl_unloader=Plugins->find(plid)) )
darope@248
   142
	{
darope@248
   143
		META_WARNING("dll: Not unloading plugin '%s'; plugin that requested unload is not found.", desc);
darope@248
   144
		RETURN_ERRNO(mFALSE, ME_BADREQ);
darope@248
   145
	} else if (pl_unloader->index == index) {
darope@248
   146
		META_WARNING("dll: Not unloading plugin '%s'; Plugin tried to unload itself.", desc);
darope@248
   147
		RETURN_ERRNO(mFALSE, ME_UNLOAD_SELF);
darope@248
   148
	} else if (is_unloader) {
darope@248
   149
		META_WARNING("dll: Not unloading plugin '%s'; Plugin is unloading plugin that tried to unload it.", desc);
darope@248
   150
		RETURN_ERRNO(mFALSE, ME_UNLOAD_UNLOADER);
darope@248
   151
	} else {
darope@248
   152
		unloader_index = pl_unloader->index;
darope@248
   153
	}
darope@248
   154
darope@248
   155
	//block unloader from being unloaded by other plugin
darope@248
   156
	pl_unloader->is_unloader = mTRUE;
darope@248
   157
darope@248
   158
	//try unload
darope@248
   159
	old_action = action;
darope@248
   160
	action = PA_UNLOAD;
darope@248
   161
	if (unload(now, reason, PNL_PLG_FORCED))
darope@248
   162
	{
darope@248
   163
		META_DEBUG(1,("Unloaded plugin '%s'", desc));
darope@248
   164
		pl_unloader->is_unloader = mFALSE;
darope@248
   165
		return mTRUE;
darope@248
   166
	}
darope@248
   167
darope@248
   168
	pl_unloader->is_unloader = mFALSE;
darope@248
   169
darope@248
   170
	//can't unload plugin now, set delayed
darope@248
   171
	if (meta_errno==ME_DELAYED)
darope@248
   172
	{
darope@248
   173
		action = old_action;
darope@248
   174
		meta_errno = ME_NOTALLOWED;
darope@248
   175
		META_DEBUG(2, ("dll: Failed unload plugin '%s'; can't detach now: allowed=%s; now=%s", desc, str_unloadable(), str_loadtime(PT_ANYTIME, SL_SIMPLE)));
darope@248
   176
	}
darope@248
   177
darope@248
   178
	return mFALSE;
darope@248
   179
}
darope@248
   180
darope@248
   181
// Parse a filename string from PEXT_LOAD_PLUGIN_BY_* function into a plugin.
darope@248
   182
// meta_errno values:
darope@248
   183
mBOOL MPlugin::plugin_parseline(const char *fname, int loader_index)
darope@248
   184
{
darope@248
   185
	char *cp;
darope@248
   186
darope@248
   187
	source_plugin_index = loader_index;
darope@248
   188
darope@248
   189
	STRNCPY(filename, fname, sizeof(filename));
darope@248
   190
	normalize_pathname(filename);
darope@248
   191
darope@248
   192
	//store just name of the actual _file, without path
darope@248
   193
	cp = strrchr(filename, '/');
darope@248
   194
	if (cp)
darope@248
   195
		file = cp+1;
darope@248
   196
	else
darope@248
   197
		file = filename;
darope@248
   198
darope@248
   199
	//grab description
darope@248
   200
	//temporarily use plugin file until plugin can be queried
darope@248
   201
	snprintf(desc, sizeof(desc), "<%s>", file);
darope@248
   202
darope@248
   203
	//make full pathname
darope@248
   204
	full_gamedir_path(filename, pathname);
darope@248
   205
darope@248
   206
	source = PS_PLUGIN;
darope@248
   207
	status = PL_VALID;
darope@248
   208
darope@248
   209
	return mTRUE;
darope@248
   210
}
darope@248
   211
darope@0
   212
// Parse a line from console "load" command into a plugin.
darope@0
   213
// meta_errno values:
darope@0
   214
//  - ME_FORMAT		invalid line format
darope@0
   215
mBOOL MPlugin::cmd_parseline(const char *line) {
darope@0
   216
	char buf[NAME_MAX + PATH_MAX + MAX_DESC_LEN];
darope@0
   217
	char *token;
darope@0
   218
	char *ptr_token;
darope@0
   219
	char *cp;
darope@0
   220
darope@0
   221
	STRNCPY(buf, line, sizeof(buf));
darope@0
   222
darope@0
   223
	// remove "load"
darope@0
   224
	token=strtok_r(buf, " \t", &ptr_token);
darope@0
   225
	if(!token)
darope@0
   226
		RETURN_ERRNO(mFALSE, ME_FORMAT);
darope@0
   227
darope@0
   228
	// grab filename
darope@0
   229
	token=strtok_r(NULL, " \t", &ptr_token);
darope@0
   230
	if(!token)
darope@0
   231
		RETURN_ERRNO(mFALSE, ME_FORMAT);
darope@0
   232
	STRNCPY(filename, token, sizeof(filename));
darope@0
   233
	normalize_pathname(filename);
darope@0
   234
	// store name of just the actual _file_, without dir components
darope@0
   235
	cp=strrchr(filename, '/');
darope@0
   236
	if(cp)
darope@0
   237
		file=cp+1;
darope@0
   238
	else
darope@0
   239
		file=filename;
darope@0
   240
darope@0
   241
	// Grab description.
darope@0
   242
	// Specify no delimiter chars, as we just want the rest of the line.
darope@0
   243
	token=strtok_r(NULL, "", &ptr_token);
darope@0
   244
	if(token) {
darope@0
   245
		token=token+strspn(token, " \t");			// skip whitespace
darope@0
   246
		STRNCPY(desc, token, sizeof(desc));
darope@0
   247
	}
darope@0
   248
	else {
darope@0
   249
		// if no description is specified, temporarily use plugin file,
darope@0
   250
		// until plugin can be queried, and desc replaced with info->name.
darope@0
   251
		snprintf(desc, sizeof(desc), "<%s>", file);
darope@0
   252
	}
darope@0
   253
darope@0
   254
	// Make full pathname (from gamedir if relative, remove "..",
darope@0
   255
	// backslashes, etc).
darope@0
   256
	full_gamedir_path(filename, pathname);
darope@0
   257
darope@0
   258
	source=PS_CMD;
darope@0
   259
	status=PL_VALID;
darope@0
   260
	return(mTRUE);
darope@0
   261
}
darope@0
   262
darope@0
   263
// Make sure this plugin has the necessary minimal information.
darope@0
   264
// meta_errno values:
darope@0
   265
//  - ME_ARGUMENT	missing necessary fields in plugin
darope@0
   266
mBOOL MPlugin::check_input(void) {
darope@0
   267
	// doublecheck our input/state
darope@0
   268
	if(status < PL_VALID) {
darope@0
   269
		META_ERROR("dll: Tried to operate on plugin[%d] with a non-valid status (%d)", index, str_status());
darope@0
   270
		RETURN_ERRNO(mFALSE, ME_ARGUMENT);
darope@0
   271
	}
darope@0
   272
	if(!file || !file[0]) {
darope@0
   273
		META_ERROR("dll: Tried to operate on plugin[%d] with an empty file", 
darope@0
   274
				index);
darope@0
   275
		RETURN_ERRNO(mFALSE, ME_ARGUMENT);
darope@0
   276
	}
darope@0
   277
	if(!filename[0]) {
darope@0
   278
		META_ERROR("dll: Tried to operate on plugin[%d] with an empty filename",
darope@0
   279
				index);
darope@0
   280
		RETURN_ERRNO(mFALSE, ME_ARGUMENT);
darope@0
   281
	}
darope@0
   282
	if(!pathname[0]) {
darope@0
   283
		META_ERROR("dll: Tried to operate on plugin[%d] with an empty pathname",
darope@0
   284
				index);
darope@0
   285
		RETURN_ERRNO(mFALSE, ME_ARGUMENT);
darope@0
   286
	}
darope@0
   287
	if(!desc[0]) {
darope@0
   288
		// if no description is specified, temporarily use plugin file,
darope@0
   289
		// until plugin can be queried, and desc replaced with info->name.
darope@0
   290
		snprintf(desc, sizeof(desc), "<%s>", file);
darope@0
   291
	}
darope@0
   292
	return(mTRUE);
darope@0
   293
}
darope@0
   294
darope@0
   295
// Try to resolve a plugin's filename as a (possibly partial) path to an
darope@0
   296
// actual filename on disk, to facilitate easier console load-command
darope@0
   297
// arguments.  Uses resolve_dirs, resolve_prefix, and resolve_suffix below.  
darope@0
   298
// Example paths that it tries:
darope@0
   299
//    filename
darope@0
   300
//    Gamedir/filename.dll, Gamedir/filename.so
darope@0
   301
//    Gamedir/filename_i386.so
darope@0
   302
//    Gamedir/dlls/mm_filename_i386.so
darope@0
   303
//    Gamedir/dlls/filename_mm_i386.so
darope@0
   304
//    Gamedir/dlls/filename_MM_i386.so
darope@0
   305
// meta_errno values:
darope@0
   306
//  - ME_NOTFOUND	couldn't find a matching file for the partial name
darope@0
   307
//  - errno's from check_input()
darope@0
   308
mBOOL MPlugin::resolve(void) {
darope@0
   309
	char *found;
darope@0
   310
	char *cp;
darope@0
   311
	int len;
darope@0
   312
	if(!check_input()) {
darope@0
   313
		// details logged, meta_errno set in check_input()
darope@0
   314
		return(mFALSE);
darope@0
   315
	}
darope@0
   316
	if(is_absolute_path(filename))
darope@0
   317
		found=resolve_prefix(filename);
darope@0
   318
	else
darope@0
   319
		found=resolve_dirs(filename);
darope@0
   320
darope@0
   321
	if(!found) {
darope@0
   322
		META_DEBUG(2, ("Couldn't resolve '%s' to file", filename));
darope@0
   323
		RETURN_ERRNO(mFALSE, ME_NOTFOUND);
darope@0
   324
	}
darope@0
   325
	META_DEBUG(2, ("Resolved '%s' to file '%s'", filename, found));
darope@0
   326
	// store pathname: the resolved path (should be absolute)
darope@0
   327
	STRNCPY(pathname, found, sizeof(pathname));
darope@0
   328
	// store file: the name of the file (without dir)
darope@0
   329
	cp=strrchr(pathname, '/');
darope@0
   330
	if(cp)
darope@0
   331
		file=cp+1;
darope@0
   332
	else
darope@0
   333
		file=pathname;
darope@0
   334
	// store pathname: the gamedir relative path, or an absolute path
darope@0
   335
	len=strlen(GameDLL.gamedir);
darope@0
   336
	if(strncasecmp(pathname, GameDLL.gamedir, len) == 0)
darope@0
   337
		STRNCPY(filename, pathname+len+1, sizeof(filename));
darope@0
   338
	else
darope@0
   339
		STRNCPY(filename, pathname, sizeof(filename));
darope@0
   340
darope@0
   341
	return(mTRUE);
darope@0
   342
}
darope@0
   343
darope@0
   344
// For the given path, tries to find file in several possible 
darope@0
   345
// directories.
darope@0
   346
// Try:
darope@0
   347
//     GAMEDIR/filename
darope@0
   348
//     GAMEDIR/dlls/filename
darope@0
   349
// meta_errno values:
darope@0
   350
//  - none
darope@0
   351
char *MPlugin::resolve_dirs(char *path) {
darope@0
   352
	struct stat st;
darope@0
   353
	static char buf[PATH_MAX];
darope@0
   354
	char *found;
darope@0
   355
darope@0
   356
	snprintf(buf, sizeof(buf), "%s/%s", GameDLL.gamedir, path);
darope@0
   357
	// try this path
darope@0
   358
	if(stat(buf, &st) == 0 && S_ISREG(st.st_mode))
darope@0
   359
		return(buf);
darope@0
   360
	// try other file prefixes in this path
darope@0
   361
	if((found=resolve_prefix(buf)))
darope@0
   362
		return(found);
darope@0
   363
darope@0
   364
	snprintf(buf, sizeof(buf), "%s/dlls/%s", GameDLL.gamedir, path);
darope@0
   365
	// try this path
darope@0
   366
	if(stat(buf, &st) == 0 && S_ISREG(st.st_mode))
darope@0
   367
		return(buf);
darope@0
   368
	// try other file prefixes for this path
darope@0
   369
	if((found=resolve_prefix(buf)))
darope@0
   370
		return(found);
darope@0
   371
darope@0
   372
	return(NULL);
darope@0
   373
}
darope@0
   374
darope@0
   375
// For the given path, tries several possible filename prefixes.
darope@0
   376
// Try:
darope@0
   377
//     dir/mm_file
darope@0
   378
//     dir/file
darope@0
   379
// meta_errno values:
darope@0
   380
//  - none
darope@0
   381
char *MPlugin::resolve_prefix(char *path) {
darope@0
   382
	struct stat st;
darope@0
   383
	char *cp, *fname;
darope@0
   384
	char dname[PATH_MAX];
darope@0
   385
	static char buf[PATH_MAX];
darope@0
   386
	char *found;
darope@0
   387
darope@0
   388
	// try "mm_" prefix FIRST.
darope@0
   389
	// split into dirname and filename
darope@0
   390
	STRNCPY(dname, path, sizeof(dname));
darope@0
   391
	cp=strrchr(dname, '/');
darope@0
   392
	if(cp) {
darope@0
   393
		*cp='\0';
darope@0
   394
		fname=cp+1;
darope@0
   395
		snprintf(buf, sizeof(buf), "%s/mm_%s", dname, fname);
darope@0
   396
	}
darope@0
   397
	else {
darope@0
   398
		// no directory in given path
darope@0
   399
		snprintf(buf, sizeof(buf), "mm_%s", path);
darope@0
   400
	}
darope@0
   401
	// try this path
darope@0
   402
	if(stat(buf, &st) == 0 && S_ISREG(st.st_mode))
darope@0
   403
		return(buf);
darope@0
   404
	// try other suffixes for this path
darope@0
   405
	if((found=resolve_suffix(buf)))
darope@0
   406
		return(found);
darope@0
   407
darope@0
   408
	// try other suffixes for the original path
darope@0
   409
	if((found=resolve_suffix(path)))
darope@0
   410
		return(found);
darope@0
   411
darope@0
   412
	return(NULL);
darope@0
   413
}
darope@0
   414
darope@0
   415
// For the given path, tries several different filename suffixes.
darope@0
   416
// Try:
darope@0
   417
//     path
darope@0
   418
//     path_mm
darope@0
   419
//     path_MM
darope@0
   420
//     path.so (linux), path.dll (win32)
darope@0
   421
//     path_i386.so, path_i486.so, etc (if linux)
darope@0
   422
// meta_errno values:
darope@0
   423
//  - none
darope@0
   424
char *MPlugin::resolve_suffix(char *path) {
darope@0
   425
	struct stat st;
darope@0
   426
	static char buf[PATH_MAX];
darope@0
   427
	char *found;
darope@0
   428
darope@0
   429
	// Hmm, recursion.
darope@0
   430
	if(!strstr(path, "_mm")) {
darope@0
   431
		char *tmpbuf;
darope@0
   432
		snprintf(buf, sizeof(buf), "%s_mm", path);
darope@0
   433
		tmpbuf=strdup(buf);
darope@0
   434
		found=resolve_suffix(tmpbuf);
darope@0
   435
		free(tmpbuf);
darope@0
   436
		if(found) return(found);
darope@0
   437
	}
darope@0
   438
	if(!strstr(path, "_MM")) {
darope@0
   439
		char *tmpbuf;
darope@0
   440
		snprintf(buf, sizeof(buf), "%s_MM", path);
darope@0
   441
		tmpbuf=strdup(buf);
darope@0
   442
		found=resolve_suffix(tmpbuf);
darope@0
   443
		free(tmpbuf);
darope@0
   444
		if(found) return(found);
darope@0
   445
	}
darope@0
   446
darope@0
   447
#ifdef _WIN32
darope@0
   448
	snprintf(buf, sizeof(buf), "%s.dll", path);
darope@0
   449
#elif defined(linux)
darope@0
   450
	snprintf(buf, sizeof(buf), "%s.so", path);
darope@0
   451
#else
darope@0
   452
#error "OS unrecognized"
darope@0
   453
#endif /* _WIN32 */
darope@0
   454
	if(stat(buf, &st) == 0 && S_ISREG(st.st_mode))
darope@0
   455
		return(buf);
darope@0
   456
darope@0
   457
#ifdef linux
darope@0
   458
	snprintf(buf, sizeof(buf), "%s_i386.so", path);
darope@0
   459
	if(stat(buf, &st) == 0 && S_ISREG(st.st_mode))
darope@0
   460
		return(buf);
darope@0
   461
	snprintf(buf, sizeof(buf), "%s_i486.so", path);
darope@0
   462
	if(stat(buf, &st) == 0 && S_ISREG(st.st_mode))
darope@0
   463
		return(buf);
darope@0
   464
	snprintf(buf, sizeof(buf), "%s_i586.so", path);
darope@0
   465
	if(stat(buf, &st) == 0 && S_ISREG(st.st_mode))
darope@0
   466
		return(buf);
darope@0
   467
	snprintf(buf, sizeof(buf), "%s_i686.so", path);
darope@0
   468
	if(stat(buf, &st) == 0 && S_ISREG(st.st_mode))
darope@0
   469
		return(buf);
darope@244
   470
	snprintf(buf, sizeof(buf), "%s_amd64.so", path);
darope@244
   471
	if(stat(buf, &st) == 0 && S_ISREG(st.st_mode))
darope@244
   472
		return(buf);
darope@0
   473
#endif /* linux */
darope@0
   474
darope@0
   475
	return(NULL);
darope@0
   476
}
darope@0
   477
darope@246
   478
darope@246
   479
// Check if a passed string starts with a known platform postfix.
darope@246
   480
// It does not check beyond the period in order to work for both
darope@246
   481
// Linux and Win32.
darope@246
   482
static mBOOL is_platform_postfix(char* pf)
darope@246
   483
{
darope@246
   484
	if ( NULL == pf ) return mFALSE;
darope@246
   485
darope@246
   486
	if ( 0 == strncmp(pf, "_i386.", 6) ) return mTRUE;
darope@246
   487
	if ( 0 == strncmp(pf, "_i486.", 6) ) return mTRUE;
darope@246
   488
	if ( 0 == strncmp(pf, "_i586.", 6) ) return mTRUE;
darope@246
   489
	if ( 0 == strncmp(pf, "_i686.", 6) ) return mTRUE;
darope@246
   490
	if ( 0 == strncmp(pf, "_amd64.", 7) ) return mTRUE;
darope@246
   491
darope@246
   492
	return mFALSE;
darope@246
   493
}
darope@246
   494
darope@246
   495
darope@244
   496
// Check if a given plugin is the same but possibly for a
darope@244
   497
// different platform. A match is considered to be found if
darope@244
   498
// 1.  the filename without the path is the same, or
darope@244
   499
// 2a. for an attached plugin the logtag is the same, or
darope@244
   500
// 2b. the description is the same, or
darope@244
   501
// 3.  a significant part of the filename is the same.
darope@246
   502
// A significant part of a plugin name is currently defined to:
darope@246
   503
//  the part up to a known platform postfix as determined by 
darope@246
   504
//  the is_platform_postfix() function (see above), or
darope@246
   505
//  the part up to the last dot, if one exists.
darope@244
   506
// meta_errno values:
darope@244
   507
//  - none
darope@244
   508
mBOOL MPlugin::platform_match(MPlugin* other) {
darope@244
   509
	char *end, *other_end;
darope@244
   510
	int prefixlen;
darope@244
   511
darope@244
   512
	if(status == PL_EMPTY || other->status == PL_EMPTY) return mFALSE;
darope@244
   513
darope@244
   514
	if(strcmp(file, other->file) == 0) return mTRUE;
darope@244
   515
darope@244
   516
	if(status >= PL_OPENED && other->status >= PL_OPENED &&
darope@244
   517
       strcmp(info->logtag, other->info->logtag) == 0 ) return mTRUE;
darope@244
   518
	if(*desc != '\0' && strcasecmp(desc,other->desc) == 0) return mTRUE;
darope@244
   519
darope@244
   520
	end=strrchr(file, '_');
darope@246
   521
	if(end == NULL || !is_platform_postfix(end)) end=strrchr(file, '.');
darope@244
   522
	other_end=strrchr(other->file, '_');
darope@246
   523
	if(other_end == NULL || !is_platform_postfix(other_end)) other_end=strrchr(other->file, '.');
darope@244
   524
darope@244
   525
	if(end == NULL || other_end == NULL) return mFALSE;
darope@244
   526
darope@244
   527
	prefixlen=end-file;
darope@244
   528
	if((other_end-other->file) != prefixlen) return mFALSE;
darope@244
   529
darope@244
   530
	if(strncmp(file,other->file, prefixlen) == 0) return mTRUE;
darope@244
   531
darope@244
   532
	return mFALSE;
darope@244
   533
}
darope@244
   534
darope@244
   535
darope@0
   536
// Load a plugin; query, check allowed time, attach.
darope@0
   537
// meta_errno values:
darope@0
   538
//  - ME_ARGUMENT	missing necessary fields in plugin
darope@0
   539
//  - ME_ALREADY	this plugin already loaded
darope@0
   540
//  - ME_BADREQ		plugin not marked for load
darope@0
   541
//  - ME_DELAYED	load request is delayed (till changelevel?)
darope@0
   542
//  - ME_NOTALLOWED	plugin not loadable after startup
darope@0
   543
//  - errno's from query()
darope@0
   544
//  - errno's from attach()
darope@0
   545
//  - errno's from check_input()
darope@0
   546
mBOOL MPlugin::load(PLUG_LOADTIME now) {
darope@0
   547
	if(!check_input()) {
darope@0
   548
		// details logged, meta_errno set in check_input()
darope@0
   549
		RETURN_ERRNO(mFALSE, ME_ARGUMENT);
darope@0
   550
	}
darope@0
   551
	if(status >= PL_RUNNING) {
darope@0
   552
		META_ERROR("dll: Not loading plugin '%s'; already loaded (status=%s)", 
darope@0
   553
				desc, str_status());
darope@0
   554
		RETURN_ERRNO(mFALSE, ME_ALREADY);
darope@0
   555
	}
darope@0
   556
	if(action != PA_LOAD && action != PA_ATTACH) {
darope@0
   557
		META_ERROR("dll: Not loading plugin '%s'; not marked for load (action=%s)", desc, str_action());
darope@0
   558
		RETURN_ERRNO(mFALSE, ME_BADREQ);
darope@0
   559
	}
darope@0
   560
darope@248
   561
	if(status < PL_OPENED) {
darope@0
   562
		// query plugin; open file and get info about it
darope@0
   563
		if(!query()) {
darope@0
   564
			META_ERROR("dll: Skipping plugin '%s'; couldn't query", desc);
darope@0
   565
			if(meta_errno != ME_DLOPEN) {
darope@0
   566
				if(DLCLOSE(handle) != 0) {
darope@0
   567
					META_ERROR("dll: Couldn't close plugin file '%s': %s", 
darope@0
   568
							file, DLERROR());
darope@0
   569
				}
darope@0
   570
				else
darope@0
   571
					handle=NULL;
darope@0
   572
			}
darope@0
   573
			status=PL_BADFILE;
darope@250
   574
			info=NULL;	//prevent crash
darope@0
   575
			// meta_errno should be already set in query()
darope@0
   576
			return(mFALSE);
darope@0
   577
		}
darope@0
   578
		status=PL_OPENED;
darope@0
   579
	}
darope@0
   580
darope@0
   581
	// are we allowed to attach this plugin at this time?
darope@0
   582
	if(info->loadable < now) {
darope@0
   583
		if(info->loadable > PT_STARTUP) {
darope@0
   584
			// will try to attach again at next opportunity
darope@0
   585
			META_DEBUG(2, ("dll: Delaying load plugin '%s'; can't attach now: allowed=%s; now=%s", 
darope@0
   586
						desc, str_loadable(), str_loadtime(now, SL_SIMPLE)));
darope@0
   587
			RETURN_ERRNO(mFALSE, ME_DELAYED);
darope@0
   588
		}
darope@0
   589
		else {
darope@0
   590
			META_DEBUG(2, ("dll: Failed load plugin '%s'; can't attach now: allowed=%s; now=%s", 
darope@0
   591
						desc, str_loadable(), str_loadtime(now, SL_SIMPLE)));
darope@0
   592
			// don't try to attach again later
darope@0
   593
			action=PA_NONE;
darope@0
   594
			RETURN_ERRNO(mFALSE, ME_NOTALLOWED);
darope@0
   595
		}
darope@0
   596
	}
darope@0
   597
darope@0
   598
	// attach plugin; get function tables
darope@0
   599
	if(attach(now) != mTRUE) {
darope@0
   600
		META_ERROR("dll: Failed to attach plugin '%s'", desc);
darope@0
   601
		// Note we don't dlclose() here, since we're returning PL_FAILED,
darope@0
   602
		// which implies that it's been dlopened and queried successfully.
darope@0
   603
		// Doing so causes crashes, because things like "meta list" try to
darope@0
   604
		// look at *info, which is in the DLL memory space and unaccessible
darope@0
   605
		// (segfault) after dlclosed.
darope@0
   606
		status=PL_FAILED;
darope@0
   607
		// meta_errno should be already set in attach()
darope@0
   608
		return(mFALSE);
darope@0
   609
	}
darope@0
   610
	status=PL_RUNNING;
darope@0
   611
	action=PA_NONE;
darope@0
   612
darope@0
   613
	// If not loading at server startup, then need to call plugin's
darope@0
   614
	// GameInit, since we've passed that.
darope@0
   615
	if(now != PT_STARTUP) {
darope@0
   616
		FN_GAMEINIT pfn_gameinit=NULL;
darope@0
   617
		if(dllapi_table && (pfn_gameinit=dllapi_table->pfnGameInit))
darope@0
   618
			pfn_gameinit();
darope@0
   619
	}
darope@0
   620
	// If loading during map, then I'd like to call plugin's
darope@0
   621
	// ServerActivate, since we've passed that.  However, I'm not sure what
darope@0
   622
	// arguments to give it...  So, we'll just have to say for the
darope@0
   623
	// moment that plugins that are loadable during a map can't need to
darope@0
   624
	// hook ServerActivate.
darope@0
   625
darope@0
   626
	META_LOG("dll: Loaded plugin '%s': %s v%s %s, %s", desc, info->name, 
darope@0
   627
			info->version, info->date, info->author);
darope@0
   628
	return(mTRUE);
darope@0
   629
}
darope@0
   630
darope@0
   631
// Query a plugin:
darope@0
   632
//  - dlopen() the file, store the handle
darope@0
   633
//  - dlsym() and call:
darope@0
   634
//	    Meta_Init (if present) - tell dll it'll be used as a metamod plugin
darope@0
   635
//	    GiveFnptrsToDll - give engine function ptrs 
darope@0
   636
//	    Meta_Query - say "hi" and get info about plugin
darope@0
   637
// meta_errno values:
darope@0
   638
//  - ME_DLOPEN		dlopen/loadlibrary failed; see dlerror() for details
darope@0
   639
//  - ME_DLMISSING	couldn't find a query() or giveFuncs() in plugin
darope@0
   640
//  - ME_DLERROR	plugin query() returned error
darope@0
   641
//  - ME_NULLDATA	info struct from query() was null
darope@0
   642
mBOOL MPlugin::query(void) {
darope@0
   643
	META_INIT_FN pfn_init;
darope@0
   644
	GIVE_ENGINE_FUNCTIONS_FN pfn_give_engfuncs;
darope@0
   645
	META_QUERY_FN pfn_query;
darope@0
   646
darope@0
   647
	// open the plugin DLL
darope@0
   648
	if(!(handle=DLOPEN(pathname))) {
darope@0
   649
		META_ERROR("dll: Failed query plugin '%s'; Couldn't open file '%s': %s",
darope@0
   650
				desc, pathname, DLERROR());
darope@0
   651
		RETURN_ERRNO(mFALSE, ME_DLOPEN);
darope@0
   652
	}
darope@0
   653
darope@0
   654
	// First, we check to see if they have a Meta_Query.  We would normally
darope@0
   655
	// dlsym this just prior to calling it, after having called
darope@0
   656
	// GiveFnptrsToDll, but things like standalone-DLL bots do all their
darope@0
   657
	// startup in GiveFnptrsToDll, and trying to load them as metamod
darope@0
   658
	// plugins creates all sorts of crashes.  So, we do a trivial check
darope@0
   659
	// first to see if the DLL looks like a metamod plugin before
darope@0
   660
	// proceeding with the normal order.  Note that we still have to call 
darope@0
   661
	// GiveFnptrsToDll before Meta_Query, because the latter typically uses 
darope@0
   662
	// engine functions like AlertMessage, which have to be passed along via 
darope@0
   663
	// GiveFnptrsToDll.
darope@0
   664
	pfn_query = (META_QUERY_FN) DLSYM(handle, "Meta_Query");
darope@0
   665
	if(!pfn_query) {
darope@0
   666
		META_ERROR("dll: Failed query plugin '%s'; Couldn't find Meta_Query(): %s", desc, DLERROR());
darope@0
   667
		// caller will dlclose()
darope@0
   668
		RETURN_ERRNO(mFALSE, ME_DLMISSING);
darope@0
   669
	}
darope@0
   670
darope@0
   671
	// Call Meta_Init, if present.  This is an optional plugin routine to
darope@0
   672
	// allow plugin to do any special initializing or other processing
darope@0
   673
	// prior to the standard query/attach procedure.  
darope@0
   674
	//
darope@0
   675
	// In particular, this should allow for DLL's that can operate as both
darope@0
   676
	// a standalone dll AND a metamod plugin.  This routine has to be
darope@0
   677
	// called _before_ GiveFnptrsToDll, since the dll will usually do all
darope@0
   678
	// its startup processing in GiveFn, and has to know at that point
darope@0
   679
	// whether it needs to do just mm plugin startup, or all the extra
darope@0
   680
	// startup needed for a standalone DLL.
darope@0
   681
	//
darope@0
   682
	// This passes nothing and returns nothing, and the routine in the
darope@0
   683
	// plugin can NOT use any Engine functions, as they haven't been
darope@0
   684
	// provided yet (done next, in GiveFnptrsToDll).
darope@0
   685
	pfn_init = (META_INIT_FN) DLSYM(handle, "Meta_Init");
darope@0
   686
	if(pfn_init) {
darope@0
   687
		pfn_init();
darope@0
   688
		META_DEBUG(6, ("dll: Plugin '%s': Called Meta_Init()", desc));
darope@0
   689
	}
darope@0
   690
	else {
darope@0
   691
		META_DEBUG(5, ("dll: no Meta_Init present in plugin '%s'", desc));
darope@0
   692
		// don't return; not an error
darope@0
   693
	}
darope@0
   694
	
darope@0
   695
	// pass on engine function table and globals to plugin
darope@0
   696
	if(!(pfn_give_engfuncs = (GIVE_ENGINE_FUNCTIONS_FN) DLSYM(handle, "GiveFnptrsToDll"))) {
darope@0
   697
		// META_ERROR("dll: Couldn't find GiveFnptrsToDll() in plugin '%s': %s", desc, DLERROR());
darope@0
   698
		META_ERROR("dll: Failed query plugin '%s'; Couldn't find GiveFnptrsToDll(): %s", desc, DLERROR());
darope@0
   699
		// caller will dlclose()
darope@0
   700
		RETURN_ERRNO(mFALSE, ME_DLMISSING);
darope@0
   701
	}
darope@0
   702
	pfn_give_engfuncs(Engine.pl_funcs, Engine.globals);
darope@0
   703
	META_DEBUG(6, ("dll: Plugin '%s': Called GiveFnptrsToDll()", desc));
darope@0
   704
darope@0
   705
	// Call plugin's Meta_Query(), to pass our meta interface version, and get
darope@0
   706
	// plugin's info structure.
darope@0
   707
	meta_errno=ME_NOERROR;
darope@0
   708
	info=NULL;
darope@0
   709
	// Make a copy of the meta_util function table for each plugin, for the
darope@0
   710
	// same reason.
darope@0
   711
	memcpy(&mutil_funcs, &MetaUtilFunctions, sizeof(mutil_funcs));
darope@0
   712
darope@0
   713
	if(pfn_query(META_INTERFACE_VERSION, &info, &mutil_funcs) != TRUE) {
darope@0
   714
		META_ERROR("dll: Failed query plugin '%s'; Meta_Query returned error", 
darope@0
   715
				desc);
darope@0
   716
		meta_errno=ME_DLERROR;
darope@0
   717
	}
darope@0
   718
	else {
darope@0
   719
		META_DEBUG(6, ("dll: Plugin '%s': Called Meta_Query() successfully", 
darope@0
   720
					desc));
darope@0
   721
	}
darope@0
   722
darope@0
   723
	// Check for interface differences...  Generally, a difference in major
darope@0
   724
	// version will be incompatible, and a plugin that expects a later
darope@0
   725
	// minor version will be incompatible (it's expecting things that this
darope@0
   726
	// Metamod won't be supplying).  Plugins that use an older minor
darope@0
   727
	// version will still work, as backward-compability within major
darope@0
   728
	// version is expected (forward-compatibility is not).
darope@0
   729
	//
darope@0
   730
	// Note, this check is done regardless of whether meta_query returns an
darope@0
   731
	// error.
darope@0
   732
	if(info && !FStrEq(info->ifvers, META_INTERFACE_VERSION)) {
darope@0
   733
		int mmajor=0, mminor=0, pmajor=0, pminor=0;
darope@0
   734
		META_DEBUG(3, ("dll: Note: Plugin '%s' interface version didn't match; expected %s, found %s", desc, META_INTERFACE_VERSION, info->ifvers));
darope@0
   735
		sscanf(META_INTERFACE_VERSION, "%d:%d", &mmajor, &mminor);
darope@0
   736
		sscanf(info->ifvers, "%d:%d", &pmajor, &pminor);
darope@0
   737
		// If plugin has later interface version, it's incompatible 
darope@0
   738
		// (update metamod).
darope@0
   739
		if(pmajor > mmajor || (pmajor==mmajor && pminor > mminor)) {
darope@0
   740
			META_ERROR("dll: Plugin '%s' requires a newer version of Metamod (Metamod needs at least interface %s not the current %s)", desc, info->ifvers, META_INTERFACE_VERSION);
darope@0
   741
			meta_errno=ME_IFVERSION;
darope@0
   742
		}
darope@0
   743
		// If plugin has older major interface version, it's incompatible
darope@0
   744
		// (update plugin).
darope@0
   745
		else if(pmajor < mmajor) {
darope@0
   746
			META_ERROR("dll: Plugin '%s' is out of date and incompatible with this version of Metamod; please find a newer version of the plugin (plugin needs at least interface %s not the current %s)", desc, META_INTERFACE_VERSION, info->ifvers);
darope@0
   747
			meta_errno=ME_IFVERSION;
darope@0
   748
		}
darope@0
   749
		// Plugin has identical major, with older minor.  This is supposed to be
darope@0
   750
		// backwards compatible, so we warn, but we still accept it.
darope@0
   751
		else if(pmajor==mmajor && pminor < mminor)
darope@0
   752
			META_LOG("dll: Note: plugin '%s' is using an older interface version (%s), not the latest interface version (%s); there might be an updated version of the plugin", desc, info->ifvers, META_INTERFACE_VERSION);
darope@0
   753
		else
darope@0
   754
			META_LOG("dll: Plugin '%s': unexpected version comparision; metavers=%s, mmajor=%d, mminor=%d; plugvers=%s, pmajor=%d, pminor=%d", 
darope@0
   755
					desc, META_INTERFACE_VERSION, mmajor, mminor, info->ifvers, pmajor, pminor);
darope@0
   756
	}
darope@0
   757
darope@0
   758
	if(meta_errno == ME_IFVERSION) {
darope@0
   759
		META_ERROR("dll: Rejected plugin '%s' due to interface version incompatibility (mm=%s, pl=%s)", desc, META_INTERFACE_VERSION, info->ifvers);
darope@0
   760
		// meta_errno is set already above
darope@0
   761
		// caller will dlclose()
darope@0
   762
		return(mFALSE);
darope@0
   763
	}
darope@0
   764
	else if(meta_errno != ME_NOERROR)
darope@0
   765
		// some other error, already logged
darope@0
   766
		return(mFALSE);
darope@0
   767
darope@0
   768
	if(!info) {
darope@0
   769
		META_ERROR("dll: Failed query plugin '%s'; Empty info structure", desc);
darope@0
   770
		// caller will dlclose()
darope@0
   771
		RETURN_ERRNO(mFALSE, ME_NULLRESULT);
darope@0
   772
	}
darope@0
   773
	// Replace temporary desc with plugin's internal name.
darope@0
   774
	if(desc[0] == '<')
darope@0
   775
		STRNCPY(desc, info->name, sizeof(desc));
darope@0
   776
	META_DEBUG(6, ("dll: Plugin '%s': Query successful", desc));
darope@0
   777
	return(mTRUE);
darope@0
   778
}
darope@0
   779
darope@0
   780
// Attach a plugin:
darope@0
   781
//	- dlsym() and call:
darope@0
   782
//	    Meta_Attach - get table of api tables, give meta_globals
darope@0
   783
//  - if provided by plugin, call various "Get" function pointers, 
darope@0
   784
//    and store resulting function tables:
darope@0
   785
//		GetEntityAPI			(std)
darope@0
   786
//		GetEntityAPI2			(std sdk2)
darope@0
   787
//		GetNewDLLFunctions		(std sdk2)
darope@0
   788
//
darope@0
   789
//		GetEntityAPI_Post		(meta)
darope@0
   790
//		GetEntityAPI2_Post		(meta)
darope@0
   791
//		GetNewDLLFunctions_Post	(meta)
darope@0
   792
//
darope@0
   793
//		GetEngineFunctions		(meta)
darope@0
   794
//		GetEngineFunctions_Post	(meta)
darope@0
   795
// meta_errno values:
darope@0
   796
//  - ME_DLMISSING	couldn't find meta_attach() in plugin
darope@0
   797
//  - ME_DLERROR	plugin attach() returned error
darope@0
   798
//  - ME_NOMEM		failed malloc
darope@0
   799
mBOOL MPlugin::attach(PLUG_LOADTIME now) {
darope@0
   800
	int ret;
darope@0
   801
	int iface_vers;
darope@0
   802
darope@0
   803
	META_ATTACH_FN pfn_attach;
darope@0
   804
	META_FUNCTIONS meta_table;
darope@0
   805
darope@0
   806
	// Make copy of gameDLL's function tables for each plugin, so we don't
darope@0
   807
	// risk the plugins screwing with the tables everyone uses.
darope@0
   808
	if(GameDLL.funcs.dllapi_table && !gamedll_funcs.dllapi_table) {
darope@250
   809
		gamedll_funcs.dllapi_table = (DLL_FUNCTIONS *) malloc(sizeof(DLL_FUNCTIONS));
darope@0
   810
		if(!gamedll_funcs.dllapi_table) {
darope@0
   811
			META_ERROR("dll: Failed attach plugin '%s': Failed malloc() for dllapi_table");
darope@0
   812
			RETURN_ERRNO(mFALSE, ME_NOMEM);
darope@0
   813
		}
darope@0
   814
		memcpy(gamedll_funcs.dllapi_table, GameDLL.funcs.dllapi_table, 
darope@0
   815
				sizeof(DLL_FUNCTIONS));
darope@0
   816
	}
darope@0
   817
	if(GameDLL.funcs.newapi_table && !gamedll_funcs.newapi_table) {
darope@250
   818
		gamedll_funcs.newapi_table = (NEW_DLL_FUNCTIONS *) calloc(1, sizeof(meta_new_dll_functions_t));
darope@0
   819
		if(!gamedll_funcs.newapi_table) {
darope@0
   820
			META_ERROR("dll: Failed attach plugin '%s': Failed malloc() for newapi_table");
darope@0
   821
			RETURN_ERRNO(mFALSE, ME_NOMEM);
darope@0
   822
		}
darope@250
   823
		static_cast<meta_new_dll_functions_t*>(gamedll_funcs.newapi_table)->set_from(GameDLL.funcs.newapi_table);
darope@0
   824
	}
darope@0
   825
	if(!(pfn_attach = (META_ATTACH_FN) DLSYM(handle, "Meta_Attach"))) {
darope@0
   826
		META_ERROR("dll: Failed attach plugin '%s': Couldn't find Meta_Attach(): %s", desc, DLERROR());
darope@0
   827
		// caller will dlclose()
darope@0
   828
		RETURN_ERRNO(mFALSE, ME_DLMISSING);
darope@0
   829
	}
darope@0
   830
darope@0
   831
	memset(&meta_table, 0, sizeof(meta_table));
darope@0
   832
	// get table of function tables,
darope@0
   833
	// give public meta globals
darope@0
   834
	ret=pfn_attach(now, &meta_table, &PublicMetaGlobals, &gamedll_funcs);
darope@0
   835
	if(ret != TRUE) {
darope@0
   836
		META_ERROR("dll: Failed attach plugin '%s': Error from Meta_Attach(): %d", desc, ret);
darope@0
   837
		// caller will dlclose()
darope@0
   838
		RETURN_ERRNO(mFALSE, ME_DLERROR);
darope@0
   839
	}
darope@0
   840
	META_DEBUG(6, ("dll: Plugin '%s': Called Meta_Attach() successfully", desc));
darope@0
   841
darope@0
   842
	// Rather than duplicate code, we use another ugly macro.  Again,
darope@0
   843
	// a function isn't an option since we have varying types.
darope@0
   844
#define GET_FUNC_TABLE_FROM_PLUGIN(pfnGetFuncs, STR_GetFuncs, struct_field, API_TYPE, TABLE_TYPE, vers_pass, vers_int, vers_want) \
darope@0
   845
	if(meta_table.pfnGetFuncs) { \
darope@247
   846
		if (!struct_field) \
darope@247
   847
			struct_field = (TABLE_TYPE*) calloc(1, sizeof(TABLE_TYPE)); \
darope@0
   848
		if(meta_table.pfnGetFuncs(struct_field, vers_pass)) { \
darope@0
   849
			META_DEBUG(3, ("dll: Plugin '%s': Found %s", desc, STR_GetFuncs)); \
darope@0
   850
		} \
darope@0
   851
		else { \
darope@0
   852
			META_ERROR("dll: Failure calling %s in plugin '%s'", STR_GetFuncs, desc); \
darope@0
   853
			if(vers_int != vers_want) \
darope@0
   854
				META_ERROR("dll: Interface version didn't match; expected %d, found %d", vers_want, vers_int); \
darope@0
   855
		} \
darope@0
   856
	} \
darope@0
   857
	else { \
darope@0
   858
		META_DEBUG(5, ("dll: Plugin '%s': No %s", desc, STR_GetFuncs)); \
darope@247
   859
		if (struct_field) \
darope@247
   860
			free(struct_field); \
darope@0
   861
		struct_field=NULL; \
darope@0
   862
	}
darope@0
   863
darope@0
   864
	// Look for API-NEW interface in plugin.  We do this before API2/API, because
darope@0
   865
	// that's what the engine appears to do..
darope@250
   866
	// [But we only do this if the gamedll provides these, so that we don't
darope@0
   867
	// give a plugin the idea that we'll call them, when in fact we won't
darope@0
   868
	// (since we don't export them to the engine when the gamedll doesn't
darope@250
   869
	// provide them).]
darope@250
   870
	// We now do this even when the gamedll doesn't provide these because
darope@250
   871
	// the plugins might want to use new functions like CvarValue() also
darope@250
   872
	// with older gamedlls which do not use the API-NEW themselves.
darope@250
   873
	// It is yet unknown if this causes any problems in the engine.
darope@250
   874
	iface_vers=NEW_DLL_FUNCTIONS_VERSION;
darope@250
   875
	GET_FUNC_TABLE_FROM_PLUGIN(pfnGetNewDLLFunctions, 
darope@250
   876
			"GetNewDLLFunctions", newapi_table, 
darope@250
   877
			NEW_DLL_FUNCTIONS_FN, meta_new_dll_functions_t,
darope@250
   878
			&iface_vers, iface_vers, NEW_DLL_FUNCTIONS_VERSION);
darope@250
   879
	iface_vers=NEW_DLL_FUNCTIONS_VERSION;
darope@250
   880
	GET_FUNC_TABLE_FROM_PLUGIN(pfnGetNewDLLFunctions_Post, 
darope@250
   881
			"GetNewDLLFunctions_Post", newapi_post_table, 
darope@250
   882
			NEW_DLL_FUNCTIONS_FN, meta_new_dll_functions_t,
darope@250
   883
			&iface_vers, iface_vers, NEW_DLL_FUNCTIONS_VERSION);
darope@0
   884
darope@0
   885
	// Look for API2 interface in plugin; preferred over API-1.
darope@0
   886
	iface_vers=INTERFACE_VERSION;
darope@0
   887
	GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEntityAPI2, 
darope@0
   888
			"GetEntityAPI2", dllapi_table, 
darope@0
   889
			APIFUNCTION2, DLL_FUNCTIONS,
darope@0
   890
			&iface_vers, iface_vers, INTERFACE_VERSION);
darope@0
   891
	iface_vers=INTERFACE_VERSION;
darope@0
   892
	GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEntityAPI2_Post, 
darope@0
   893
			"GetEntityAPI2_Post", dllapi_post_table, 
darope@0
   894
			APIFUNCTION2, DLL_FUNCTIONS,
darope@0
   895
			&iface_vers, iface_vers, INTERFACE_VERSION);
darope@0
   896
darope@0
   897
	// Look for old-style API in plugin, if API2 interface wasn't found.
darope@0
   898
	if(!dllapi_table && !dllapi_post_table) {
darope@0
   899
		GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEntityAPI, 
darope@0
   900
				"GetEntityAPI", dllapi_table, 
darope@0
   901
				APIFUNCTION, DLL_FUNCTIONS,
darope@0
   902
				INTERFACE_VERSION, INTERFACE_VERSION, INTERFACE_VERSION);
darope@0
   903
		GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEntityAPI_Post, 
darope@0
   904
				"GetEntityAPI_Post", dllapi_post_table, 
darope@0
   905
				APIFUNCTION, DLL_FUNCTIONS,
darope@0
   906
				INTERFACE_VERSION, INTERFACE_VERSION, INTERFACE_VERSION);
darope@0
   907
	}
darope@0
   908
darope@0
   909
	// Look for Engine interface.
darope@0
   910
	iface_vers=ENGINE_INTERFACE_VERSION;
darope@0
   911
	GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEngineFunctions, 
darope@0
   912
			"GetEngineFunctions", engine_table, 
darope@250
   913
			GET_ENGINE_FUNCTIONS_FN, meta_enginefuncs_t,
darope@0
   914
			&iface_vers, iface_vers, ENGINE_INTERFACE_VERSION);
darope@0
   915
	iface_vers=ENGINE_INTERFACE_VERSION;
darope@0
   916
	GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEngineFunctions_Post, 
darope@0
   917
			"GetEngineFunctions_Post", engine_post_table, 
darope@250
   918
			GET_ENGINE_FUNCTIONS_FN, meta_enginefuncs_t,
darope@0
   919
			&iface_vers, iface_vers, ENGINE_INTERFACE_VERSION);
darope@0
   920
darope@0
   921
	if(!dllapi_table && !dllapi_post_table 
darope@0
   922
		&& !newapi_table && !newapi_post_table
darope@0
   923
		&& !engine_table && !engine_post_table)
darope@0
   924
	{
darope@0
   925
		META_LOG("dll: Plugin '%s' isn't catching _any_ functions ??", desc);
darope@0
   926
	}
darope@0
   927
darope@0
   928
	time_loaded=time(NULL);
darope@0
   929
	return(mTRUE);
darope@0
   930
}
darope@0
   931
darope@0
   932
// Unload a plugin.  Check time, detach.
darope@0
   933
// meta_errno values:
darope@0
   934
//  - ME_ARGUMENT	missing necessary fields in plugin
darope@0
   935
//  - ME_ALREADY	this plugin already unloaded
darope@0
   936
//  - ME_BADREQ		plugin not marked for unload
darope@0
   937
//  - ME_DELAYED	unload request is delayed (till changelevel?)
darope@0
   938
//  - ME_NOTALLOWED	plugin not unloadable after startup
darope@0
   939
//  - errno's from check_input()
darope@248
   940
mBOOL MPlugin::unload(PLUG_LOADTIME now, PL_UNLOAD_REASON reason, PL_UNLOAD_REASON real_reason) {
darope@0
   941
	if(!check_input()) {
darope@0
   942
		// details logged, meta_errno set in check_input()
darope@0
   943
		RETURN_ERRNO(mFALSE, ME_ARGUMENT);
darope@0
   944
	}
darope@0
   945
	if(status < PL_RUNNING) {
darope@248
   946
		if (reason != PNL_CMD_FORCED && reason != PNL_RELOAD) {
darope@248
   947
			META_ERROR("dll: Not unloading plugin '%s'; already unloaded (status=%s)", desc, str_status());
darope@248
   948
			RETURN_ERRNO(mFALSE, ME_ALREADY);
darope@248
   949
		}
darope@0
   950
	}
darope@0
   951
	if(action != PA_UNLOAD && action != PA_RELOAD) {
darope@248
   952
		META_WARNING("dll: Not unloading plugin '%s'; not marked for unload (action=%s)", desc, str_action());
darope@0
   953
		RETURN_ERRNO(mFALSE, ME_BADREQ);
darope@0
   954
	}
darope@0
   955
darope@0
   956
	// Are we allowed to detach this plugin at this time?
darope@0
   957
	// If forcing unload, we disregard when plugin wants to be unloaded.
darope@248
   958
	if(info && info->unloadable < now) {
darope@0
   959
		if(reason == PNL_CMD_FORCED) {
darope@0
   960
			META_DEBUG(2, ("dll: Forced unload plugin '%s' overriding allowed times: allowed=%s; now=%s", desc, str_unloadable(), str_loadtime(now, SL_SIMPLE)));
darope@0
   961
		}
darope@0
   962
		else {
darope@0
   963
			if(info->unloadable > PT_STARTUP) {
darope@0
   964
				META_DEBUG(2, ("dll: Delaying unload plugin '%s'; can't detach now: allowed=%s; now=%s", desc, str_unloadable(), str_loadtime(now, SL_SIMPLE)));
darope@0
   965
				// caller should give message to user
darope@0
   966
				// try to unload again at next opportunity
darope@0
   967
				RETURN_ERRNO(mFALSE, ME_DELAYED);
darope@0
   968
			}
darope@0
   969
			else {
darope@0
   970
				META_DEBUG(2, ("dll: Failed unload plugin '%s'; can't detach now: allowed=%s; now=%s", desc, str_unloadable(), str_loadtime(now, SL_SIMPLE)));
darope@0
   971
				// don't try to unload again later
darope@0
   972
				action=PA_NONE;
darope@0
   973
				RETURN_ERRNO(mFALSE, ME_NOTALLOWED);
darope@0
   974
			}
darope@0
   975
		}
darope@0
   976
	}
darope@0
   977
darope@0
   978
	// If unloading during map, then I'd like to call plugin's
darope@0
   979
	// ServerDeactivate.  However, I don't want to do this until I start
darope@0
   980
	// calling ServerActivate when loading during map, since the SDK
darope@0
   981
	// indicates these two routines should match call for call.
darope@0
   982
darope@0
   983
	// detach plugin
darope@0
   984
	if(!detach(now, reason)) {
darope@248
   985
		if (reason == PNL_RELOAD){
darope@248
   986
			META_DEBUG(2, ("dll: Reload plugin '%s' overriding failed detach", desc));
darope@248
   987
		} else if(reason == PNL_CMD_FORCED) {
darope@0
   988
			META_DEBUG(2, ("dll: Forced unload plugin '%s' overriding failed detach"));
darope@0
   989
		}
darope@0
   990
		else {
darope@248
   991
			META_WARNING("dll: Failed to detach plugin '%s'; ", desc);
darope@0
   992
			// meta_errno should be already set in detach()
darope@0
   993
			return(mFALSE);
darope@0
   994
		}
darope@0
   995
	}
darope@0
   996
darope@248
   997
	Plugins->clear_source_plugin_index(index);
darope@248
   998
darope@0
   999
	// successful detach, or forced unload
darope@0
  1000
darope@0
  1001
	// Unmark registered commands for this plugin (by index number).
darope@0
  1002
	RegCmds->disable(index);
darope@0
  1003
	// Unmark registered cvars for this plugin (by index number).
darope@0
  1004
	RegCvars->disable(index);
darope@0
  1005
darope@0
  1006
#ifdef UNFINISHED
darope@0
  1007
	// Remove all requested hooks from this plugin (by index number).
darope@0
  1008
	Hooks->remove_all(info);
darope@0
  1009
#endif /* UNFINISHED */
darope@0
  1010
darope@0
  1011
	// Close the file.  Note: after this, attempts to reference any memory
darope@0
  1012
	// locations in the file will produce a segfault.
darope@0
  1013
	if(DLCLOSE(handle) != 0) {
darope@248
  1014
		// If DLL cannot be closed, OS is badly broken or we are giving invalid handle.
darope@248
  1015
		// So we don't return here but instead remove plugin from our listings.
darope@248
  1016
		META_WARNING("dll: Couldn't close plugin file '%s': %s", file, DLERROR());
darope@0
  1017
	}
darope@0
  1018
	handle=NULL;
darope@0
  1019
darope@0
  1020
	if(action==PA_UNLOAD) {
darope@0
  1021
		status=PL_EMPTY;
darope@0
  1022
		clear();
darope@0
  1023
	}
darope@0
  1024
	else if(action==PA_RELOAD) {
darope@0
  1025
		status=PL_VALID;
darope@0
  1026
		action=PA_LOAD;
darope@247
  1027
		clear();
darope@0
  1028
	}
darope@248
  1029
	META_LOG("dll: Unloaded plugin '%s' for reason '%s'", desc, str_reason(reason, real_reason));
darope@0
  1030
	return(mTRUE);
darope@0
  1031
}
darope@0
  1032
darope@0
  1033
// Inform plugin we're going to unload it.
darope@0
  1034
// meta_errno values:
darope@0
  1035
//  - ME_DLMISSING	couldn't find meta_detach() in plugin
darope@0
  1036
//  - ME_DLERROR	plugin detach() returned error
darope@0
  1037
mBOOL MPlugin::detach(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) {
darope@0
  1038
	int ret;
darope@0
  1039
	META_DETACH_FN pfn_detach;
darope@0
  1040
darope@248
  1041
	// If we have no handle, i.e. no dll loaded, we return true because the
darope@248
  1042
	// dll is obviously detached. We shouldn't call DLSYM() with a NULL
darope@248
  1043
	// handle since this will DLSYM() ourself.
darope@248
  1044
	if(!handle)
darope@248
  1045
		return mTRUE;
darope@248
  1046
darope@0
  1047
	if(!(pfn_detach = (META_DETACH_FN) DLSYM(handle, "Meta_Detach"))) {
darope@250
  1048
		META_ERROR("dll: Error detach plugin '%s': Couldn't find Meta_Detach(): %s", desc, DLERROR());
darope@0
  1049
		// caller will dlclose()
darope@0
  1050
		RETURN_ERRNO(mFALSE, ME_DLMISSING);
darope@0
  1051
	}
darope@0
  1052
darope@0
  1053
	ret=pfn_detach(now, reason);
darope@0
  1054
	if(ret != TRUE) {
darope@0
  1055
		META_ERROR("dll: Failed detach plugin '%s': Error from Meta_Detach(): %d", desc, ret);
darope@0
  1056
		RETURN_ERRNO(mFALSE, ME_DLERROR);
darope@0
  1057
	}
darope@0
  1058
	META_DEBUG(6, ("dll: Plugin '%s': Called Meta_Detach() successfully", desc));
darope@0
  1059
	return(mTRUE);
darope@0
  1060
}
darope@0
  1061
darope@0
  1062
// Reload a plugin; unload and load again.
darope@0
  1063
// meta_errno values:
darope@246
  1064
//  - ME_NOTALLOWED	plugin not loadable after startup
darope@246
  1065
//  - errno's from check_input()
darope@0
  1066
//  - errno's from unload()
darope@0
  1067
//  - errno's from load()
darope@0
  1068
mBOOL MPlugin::reload(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) {
darope@246
  1069
	if(!check_input()) {
darope@246
  1070
		// details logged, meta_errno set in check_input()
darope@246
  1071
		RETURN_ERRNO(mFALSE, ME_ARGUMENT);
darope@246
  1072
	}
darope@246
  1073
	// Are we allowed to load this plugin at this time?
darope@246
  1074
	// If we cannot load the plugin after unloading it, we keep it.
darope@248
  1075
	if (info && info->loadable < now)
darope@248
  1076
	{
darope@246
  1077
		if(info->loadable > PT_STARTUP) {
darope@246
  1078
			META_DEBUG(2, ("dll: Delaying reload plugin '%s'; would not be able to reattach now: allowed=%s; now=%s", desc, str_loadable(), str_loadtime(now, SL_SIMPLE)));
darope@246
  1079
			// caller should give message to user
darope@246
  1080
			// try to reload again at next opportunity
darope@246
  1081
			RETURN_ERRNO(mFALSE, ME_DELAYED);
darope@246
  1082
		}
darope@246
  1083
		else {
darope@246
  1084
			META_DEBUG(2, ("dll: Failed reload plugin '%s'; would not be able to reattach now: allowed=%s; now=%s", desc, str_loadable(), str_loadtime(now, SL_SIMPLE)));
darope@246
  1085
			// don't try to reload again later
darope@246
  1086
			action=PA_NONE;
darope@246
  1087
			RETURN_ERRNO(mFALSE, ME_NOTALLOWED);
darope@246
  1088
		}
darope@246
  1089
	}
darope@246
  1090
darope@248
  1091
	if (status < PL_RUNNING)
darope@248
  1092
	{
darope@248
  1093
		META_WARNING("dll: Plugin '%s' isn't running; Forcing unload plugin for reloading", desc);
darope@248
  1094
		reason = PNL_RELOAD;
darope@248
  1095
	}
darope@248
  1096
	if (!unload(now, reason, reason)) {
darope@248
  1097
		META_WARNING("dll: Failed to unload plugin '%s' for reloading", desc);
darope@0
  1098
		// meta_errno should be set already in unload()
darope@0
  1099
		return(mFALSE);
darope@0
  1100
	}
darope@0
  1101
	if(!load(now)) {
darope@248
  1102
		META_WARNING("dll: Failed to reload plugin '%s' after unloading", desc);
darope@0
  1103
		// meta_errno should be set already in load()
darope@0
  1104
		return(mFALSE);
darope@0
  1105
	}
darope@0
  1106
	return(mTRUE);
darope@0
  1107
}
darope@0
  1108
darope@0
  1109
// Pause a plugin; temporarily disabled for API routines.
darope@0
  1110
// meta_errno values:
darope@0
  1111
//  - ME_ALREADY	this plugin already paused
darope@0
  1112
//  - ME_BADREQ		can't pause; not running
darope@0
  1113
//  - ME_NOTALLOWED	plugin doesn't want to be paused
darope@0
  1114
mBOOL MPlugin::pause(void) {
darope@0
  1115
	if(status == PL_PAUSED) {
darope@0
  1116
		META_ERROR("Not pausing plugin '%s'; already paused", desc);
darope@0
  1117
		RETURN_ERRNO(mFALSE, ME_ALREADY);
darope@0
  1118
	}
darope@0
  1119
	if(status != PL_RUNNING) {
darope@0
  1120
		META_ERROR("Cannot pause plugin '%s'; not currently running (status=%s)", desc, str_status());
darope@0
  1121
		RETURN_ERRNO(mFALSE, ME_BADREQ);
darope@0
  1122
	}
darope@0
  1123
darope@0
  1124
	// are we allowed to pause this plugin?
darope@0
  1125
	if(info->unloadable < PT_ANYPAUSE) {
darope@0
  1126
		META_ERROR("Cannot pause plugin '%s'; not allowed by plugin (allowed=%s)", desc, str_unloadable());
darope@0
  1127
		action=PA_NONE;
darope@0
  1128
		RETURN_ERRNO(mFALSE, ME_NOTALLOWED);
darope@0
  1129
	}
darope@0
  1130
darope@0
  1131
	status=PL_PAUSED;
darope@0
  1132
	META_LOG("Paused plugin '%s'", desc);
darope@0
  1133
	return(mTRUE);
darope@0
  1134
}
darope@0
  1135
darope@0
  1136
// Unpause a plugin.
darope@0
  1137
// meta_errno values:
darope@0
  1138
//  - ME_BADREQ		can't unpause; not paused
darope@0
  1139
mBOOL MPlugin::unpause(void) {
darope@0
  1140
	if(status != PL_PAUSED) {
darope@0
  1141
		META_ERROR("Cannot unpause plugin '%s'; not currently paused (status=%s)", desc, str_status());
darope@0
  1142
		RETURN_ERRNO(mFALSE, ME_BADREQ);
darope@0
  1143
	}
darope@0
  1144
	status=PL_RUNNING;
darope@0
  1145
	META_LOG("Unpaused plugin '%s'", desc);
darope@0
  1146
	return(mTRUE);
darope@0
  1147
}
darope@0
  1148
darope@0
  1149
// Retry pending action, presumably from a previous failure.
darope@0
  1150
// meta_errno values:
darope@0
  1151
//  - ME_BADREQ		no pending action
darope@0
  1152
//  - errno's from load()
darope@0
  1153
//  - errno's from unload()
darope@0
  1154
//  - errno's from reload()
darope@0
  1155
mBOOL MPlugin::retry(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) {
darope@0
  1156
	if(action==PA_LOAD)
darope@0
  1157
		return(load(now));
darope@0
  1158
	else if(action==PA_ATTACH)
darope@0
  1159
		return(load(now));
darope@0
  1160
	else if(action==PA_UNLOAD)
darope@248
  1161
		return(unload(now, reason, reason));
darope@0
  1162
	else if(action==PA_RELOAD)
darope@0
  1163
		return(reload(now, reason));
darope@0
  1164
	else {
darope@0
  1165
		META_ERROR("No pending action to retry for plugin '%s'; (status=%s, action=%s)", desc, str_status(), str_action());
darope@0
  1166
		RETURN_ERRNO(mFALSE, ME_BADREQ);
darope@0
  1167
	}
darope@0
  1168
}
darope@0
  1169
darope@0
  1170
// Clear a plugin (it failed a previous action and should be
darope@0
  1171
// removed from the list, or it's being unloaded).
darope@0
  1172
// meta_errno values:
darope@0
  1173
//  - ME_BADREQ		not marked for clearing
darope@0
  1174
//  - ME_DLERROR	failed to dlclose
darope@0
  1175
mBOOL MPlugin::clear(void) {
darope@0
  1176
	if(status != PL_FAILED && status != PL_BADFILE
darope@0
  1177
			&& status != PL_EMPTY && status != PL_OPENED) {
darope@0
  1178
		META_ERROR("Cannot clear plugin '%s'; not marked as failed, empty, or open (status=%s)", desc, str_status());
darope@0
  1179
		RETURN_ERRNO(mFALSE, ME_BADREQ);
darope@0
  1180
	}
darope@0
  1181
	// If file is open, close the file.  Note: after this, attempts to
darope@0
  1182
	// reference any memory locations in the file will produce a segfault.
darope@0
  1183
	if(handle && DLCLOSE(handle) != 0) {
darope@0
  1184
		META_ERROR("dll: Couldn't close plugin file '%s': %s", file, DLERROR());
darope@0
  1185
		status=PL_FAILED;
darope@0
  1186
		RETURN_ERRNO(mFALSE, ME_DLERROR);
darope@0
  1187
	}
darope@0
  1188
	handle=NULL;
darope@0
  1189
darope@0
  1190
	if(gamedll_funcs.dllapi_table) free(gamedll_funcs.dllapi_table);
darope@0
  1191
	if(gamedll_funcs.newapi_table) free(gamedll_funcs.newapi_table);
darope@246
  1192
	if(dllapi_table) free(dllapi_table);
darope@246
  1193
	if(dllapi_post_table) free(dllapi_post_table);
darope@246
  1194
	if(newapi_table) free(newapi_table);
darope@246
  1195
	if(newapi_post_table) free(newapi_post_table);
darope@246
  1196
	if(engine_table) free(engine_table);
darope@246
  1197
	if(engine_post_table) free(engine_post_table);
darope@0
  1198
darope@0
  1199
	status=PL_EMPTY;
darope@0
  1200
	action=PA_NULL;
darope@0
  1201
	handle=NULL;
darope@0
  1202
	info=NULL;
darope@0
  1203
	time_loaded=0;
darope@0
  1204
	dllapi_table=NULL;
darope@0
  1205
	dllapi_post_table=NULL;
darope@0
  1206
	newapi_table=NULL;
darope@0
  1207
	newapi_post_table=NULL;
darope@0
  1208
	engine_table=NULL;
darope@0
  1209
	engine_post_table=NULL;
darope@246
  1210
	gamedll_funcs.dllapi_table = NULL;
darope@246
  1211
	gamedll_funcs.newapi_table = NULL;
darope@248
  1212
	source_plugin_index = 0;
darope@248
  1213
	unloader_index = 0;
darope@248
  1214
	is_unloader = mFALSE;
darope@0
  1215
	return(mTRUE);
darope@0
  1216
}
darope@0
  1217
darope@0
  1218
// List information about plugin to console.
darope@0
  1219
void MPlugin::show(void) {
darope@0
  1220
	char *cp, *tstr;
darope@0
  1221
	int n, width;
darope@0
  1222
	width=13;
darope@0
  1223
	META_CONS("%*s: %s", width, "name", info ? info->name : "(nil)");
darope@0
  1224
	META_CONS("%*s: %s", width, "desc", desc);
darope@0
  1225
	META_CONS("%*s: %s", width, "status", str_status());
darope@0
  1226
	META_CONS("%*s: %s", width, "action", str_action());
darope@0
  1227
	META_CONS("%*s: %s", width, "filename", filename);
darope@0
  1228
	META_CONS("%*s: %s", width, "file", file);
darope@0
  1229
	META_CONS("%*s: %s", width, "pathname", pathname);
darope@0
  1230
	META_CONS("%*s: %d", width, "index", index);
darope@0
  1231
	META_CONS("%*s: %s", width, "source", str_source());
darope@0
  1232
	META_CONS("%*s: %s", width, "loadable", str_loadable(SL_ALLOWED));
darope@0
  1233
	META_CONS("%*s: %s", width, "unloadable", str_unloadable(SL_ALLOWED));
darope@0
  1234
	META_CONS("%*s: %s", width, "version", info ? info->version : "(nil)");
darope@0
  1235
	META_CONS("%*s: %s", width, "date", info ? info->date : "(nil)");
darope@0
  1236
	META_CONS("%*s: %s", width, "author", info ? info->author : "(nil)");
darope@0
  1237
	META_CONS("%*s: %s", width, "url", info ? info->url : "(nil)");
darope@0
  1238
	META_CONS("%*s: %s", width, "logtag", info ? info->logtag : "(nil)");
darope@248
  1239
	META_CONS("%*s: %s", width, "ifvers", info ? info->ifvers : "(nil)");
darope@0
  1240
	// ctime() includes newline at EOL
darope@0
  1241
	tstr=ctime(&time_loaded);
darope@0
  1242
	if((cp=strchr(tstr, '\n')))
darope@0
  1243
		*cp='\0';
darope@0
  1244
	META_CONS("%*s: %s", width, "last loaded", tstr);
darope@0
  1245
	// XXX show file time ?
darope@0
  1246
darope@0
  1247
	if(dllapi_table) {
darope@0
  1248
		META_CONS("DLLAPI functions:");
darope@0
  1249
		SHOW_DEF_DLLAPI(dllapi_table, "   ", "");
darope@0
  1250
		META_CONS("%d functions (dllapi)", n);
darope@0
  1251
	}
darope@0
  1252
	else
darope@0
  1253
		META_CONS("No DLLAPI functions.");
darope@0
  1254
	if(dllapi_post_table) {
darope@0
  1255
		META_CONS("DLLAPI-Post functions:");
darope@0
  1256
		SHOW_DEF_DLLAPI(dllapi_post_table, "   ", "_Post");
darope@0
  1257
		META_CONS("%d functions (dllapi post)", n);
darope@0
  1258
	}
darope@0
  1259
	else
darope@0
  1260
		META_CONS("No DLLAPI-Post functions.");
darope@0
  1261
darope@0
  1262
	if(newapi_table) {
darope@0
  1263
		META_CONS("NEWAPI functions:");
darope@0
  1264
		SHOW_DEF_NEWAPI(newapi_table, "   ", "");
darope@0
  1265
		META_CONS("%d functions (newapi)", n);
darope@0
  1266
	}
darope@0
  1267
	else
darope@0
  1268
		META_CONS("No NEWAPI functions.");
darope@0
  1269
	if(newapi_post_table) {
darope@0
  1270
		META_CONS("NEWAPI-Post functions:");
darope@0
  1271
		SHOW_DEF_NEWAPI(newapi_post_table, "   ", "_Post");
darope@0
  1272
		META_CONS("%d functions (newapi post)", n);
darope@0
  1273
	}
darope@0
  1274
	else
darope@0
  1275
		META_CONS("No NEWAPI-Post functions.");
darope@0
  1276
darope@0
  1277
	if(engine_table) {
darope@0
  1278
		META_CONS("Engine functions:");
darope@0
  1279
		SHOW_DEF_ENGINE(engine_table, "   ", "");
darope@0
  1280
		META_CONS("%d functions (engine)", n);
darope@0
  1281
	}
darope@0
  1282
	else
darope@0
  1283
		META_CONS("No Engine functions.");
darope@0
  1284
	if(engine_post_table) {
darope@0
  1285
		META_CONS("Engine-Post functions:");
darope@0
  1286
		SHOW_DEF_ENGINE(engine_post_table, "   ", "_Post");
darope@0
  1287
		META_CONS("%d functions (engine post)", n);
darope@0
  1288
	}
darope@0
  1289
	else
darope@0
  1290
		META_CONS("No Engine-Post functions.");
darope@0
  1291
	RegCmds->show(index);
darope@0
  1292
	RegCvars->show(index);
darope@248
  1293
	if (Plugins->found_child_plugins(index))
darope@248
  1294
		Plugins->show(index);
darope@248
  1295
	else
darope@248
  1296
		META_CONS("No child plugins.");
darope@0
  1297
}
darope@0
  1298
darope@0
  1299
// Check whether the file on disk looks like it's been updated since we
darope@0
  1300
// last loaded the plugin.
darope@0
  1301
// meta_errno values:
darope@0
  1302
//  - ME_NOFILE		couldn't find file
darope@0
  1303
//  - ME_NOERROR	no error; false indicates file not newer
darope@0
  1304
mBOOL MPlugin::newer_file(void) {
darope@0
  1305
	struct stat st;
darope@0
  1306
	time_t file_time;
darope@0
  1307
darope@0
  1308
	if(stat(pathname, &st) != 0)
darope@0
  1309
		RETURN_ERRNO(mFALSE, ME_NOFILE);
darope@0
  1310
	file_time=max(st.st_ctime, st.st_mtime);
darope@0
  1311
	META_DEBUG(5, ("newer_file? file=%s; load=%d, file=%d; ctime=%d, mtime=%d",
darope@0
  1312
				file, time_loaded, file_time, st.st_ctime, st.st_mtime));
darope@0
  1313
	if(file_time > time_loaded)
darope@0
  1314
		return(mTRUE);
darope@0
  1315
	else
darope@0
  1316
		RETURN_ERRNO(mFALSE, ME_NOERROR);
darope@0
  1317
}
darope@0
  1318
darope@0
  1319
// Return a string describing status of plugin.
darope@0
  1320
// SIMPLE is the default.
darope@0
  1321
// SHOW is max 4 chars, for "show" output.
darope@0
  1322
// meta_errno values:
darope@0
  1323
//  - none
dvander@261
  1324
const char *MPlugin::str_status(STR_STATUS fmt) {
darope@0
  1325
	switch(status) {
darope@0
  1326
		case PL_EMPTY:
darope@0
  1327
			if(fmt==ST_SHOW) return("empt");
darope@0
  1328
			else return("empty");
darope@0
  1329
		case PL_VALID:
darope@0
  1330
			if(fmt==ST_SHOW) return("info");
darope@0
  1331
			else return("valid");
darope@0
  1332
		case PL_BADFILE:
darope@0
  1333
			if(fmt==ST_SHOW) return("badf");
darope@0
  1334
			else return("badfile");
darope@0
  1335
		case PL_OPENED:
darope@0
  1336
			if(fmt==ST_SHOW) return("open");
darope@0
  1337
			else return("opened");
darope@0
  1338
		case PL_FAILED:
darope@0
  1339
			if(fmt==ST_SHOW) return("fail");
darope@0
  1340
			else return("failed");
darope@0
  1341
		case PL_RUNNING:
darope@0
  1342
			if(fmt==ST_SHOW) return("RUN");
darope@0
  1343
			else return("running");
darope@0
  1344
		case PL_PAUSED:
darope@0
  1345
			if(fmt==ST_SHOW) return("PAUS");
darope@0
  1346
			else return("paused");
darope@0
  1347
		default:
darope@0
  1348
			if(fmt==ST_SHOW) return(UTIL_VarArgs("UNK%d", status));
darope@0
  1349
			return(UTIL_VarArgs("unknown (%d)", status));
darope@0
  1350
	}
darope@0
  1351
}
darope@0
  1352
darope@0
  1353
// Return a string (one word) describing requested action for plugin.
darope@0
  1354
// SIMPLE is the default.
darope@0
  1355
// SHOW is max 4 chars, for "show" output.
darope@0
  1356
// meta_errno values:
darope@0
  1357
//  - none
dvander@261
  1358
const char *MPlugin::str_action(STR_ACTION fmt) {
darope@0
  1359
	switch(action) {
darope@0
  1360
		case PA_NULL:
darope@0
  1361
			if(fmt==SA_SHOW) return("NULL");
darope@0
  1362
			else return("null");
darope@0
  1363
		case PA_NONE:
darope@0
  1364
			if(fmt==SA_SHOW) return(" -  ");
darope@0
  1365
			else return("none");
darope@0
  1366
		case PA_KEEP:
darope@0
  1367
			if(fmt==SA_SHOW) return("keep");
darope@0
  1368
			else return("keep");
darope@0
  1369
		case PA_LOAD:
darope@0
  1370
			if(fmt==SA_SHOW) return("load");
darope@0
  1371
			else return("load");
darope@0
  1372
		case PA_ATTACH:
darope@0
  1373
			if(fmt==SA_SHOW) return("atch");
darope@0
  1374
			else return("attach");
darope@0
  1375
		case PA_UNLOAD:
darope@0
  1376
			if(fmt==SA_SHOW) return("unld");
darope@0
  1377
			else return("unload");
darope@0
  1378
		case PA_RELOAD:
darope@0
  1379
			if(fmt==SA_SHOW) return("relo");
darope@0
  1380
			else return("reload");
darope@0
  1381
		default:
darope@0
  1382
			if(fmt==SA_SHOW) return(UTIL_VarArgs("UNK%d", action));
darope@0
  1383
			else return(UTIL_VarArgs("unknown (%d)", action));
darope@0
  1384
	}
darope@0
  1385
}
darope@0
  1386
darope@0
  1387
// Return a string describing given plugin loadtime.
darope@0
  1388
// SIMPLE is the default.
darope@0
  1389
// SHOW is max 3 chars, for "show" output.
darope@0
  1390
// ALLOWED is in terms of when it's allowed to load/unload.
darope@0
  1391
// NOW is to describe current situation of load/unload attempt.
darope@0
  1392
// meta_errno values:
darope@0
  1393
//  - none
dvander@261
  1394
const char *MPlugin::str_loadtime(PLUG_LOADTIME ptime, STR_LOADTIME fmt) {
darope@0
  1395
	switch(ptime) {
darope@0
  1396
		case PT_NEVER:
darope@0
  1397
			if(fmt==SL_SHOW) return("Never");
darope@0
  1398
			else return("never");
darope@0
  1399
		case PT_STARTUP:
darope@0
  1400
			if(fmt==SL_SHOW) return("Start");
darope@0
  1401
			else if(fmt==SL_ALLOWED) return("at server startup");
darope@0
  1402
			else if(fmt==SL_NOW) return("during server startup");
darope@0
  1403
			else return("startup");
darope@0
  1404
		case PT_CHANGELEVEL:
darope@0
  1405
			if(fmt==SL_SHOW) return("Chlvl");
darope@0
  1406
			else if(fmt==SL_ALLOWED) return("at changelevel");
darope@0
  1407
			else if(fmt==SL_NOW) return("during changelevel");
darope@0
  1408
			else return("changelevel");
darope@0
  1409
		case PT_ANYTIME:
darope@0
  1410
			if(fmt==SL_SHOW) return("ANY");
darope@0
  1411
			else if(fmt==SL_ALLOWED) return("at any time");
darope@0
  1412
			else if(fmt==SL_NOW) return("during map");
darope@0
  1413
			else return("anytime");
darope@0
  1414
		case PT_ANYPAUSE:
darope@0
  1415
			if(fmt==SL_SHOW) return("Pause");
darope@0
  1416
			else if(fmt==SL_ALLOWED) return("at any time, and pausable");
darope@0
  1417
			else if(fmt==SL_NOW) return("for requested pause");
darope@0
  1418
			else return("pausable");
darope@0
  1419
		default:
darope@0
  1420
			if(fmt==SL_SHOW) return(UTIL_VarArgs("UNK-%d", ptime));
darope@0
  1421
			else return(UTIL_VarArgs("unknown (%d)", ptime));
darope@0
  1422
	}
darope@0
  1423
}
darope@0
  1424
darope@0
  1425
// Return a string describing why a plugin is to be unloaded.
darope@0
  1426
// meta_errno values:
darope@0
  1427
//  - none
dvander@261
  1428
const char *MPlugin::str_reason(PL_UNLOAD_REASON preason, PL_UNLOAD_REASON preal_reason) {
darope@248
  1429
	char buf[128];
darope@248
  1430
	
darope@248
  1431
	if(preason == PNL_PLUGIN)
darope@248
  1432
		preason = PNL_NULL;
darope@248
  1433
	if(preason == PNL_PLG_FORCED)
darope@248
  1434
		preason = PNL_NULL;
darope@248
  1435
darope@248
  1436
	switch(preal_reason) {
darope@0
  1437
		case PNL_NULL:
darope@0
  1438
			return("null");
darope@0
  1439
		case PNL_INI_DELETED:
darope@0
  1440
			return("deleted from ini file");
darope@0
  1441
		case PNL_FILE_NEWER:
darope@0
  1442
			return("file on disk is newer");
darope@0
  1443
		case PNL_COMMAND:
darope@0
  1444
			return("server command");
darope@0
  1445
		case PNL_CMD_FORCED:
darope@0
  1446
			return("forced by server command");
darope@248
  1447
		case PNL_PLUGIN:
darope@248
  1448
			STRNCPY(buf, str_reason(PNL_NULL, preason), sizeof(buf));
darope@248
  1449
			return(UTIL_VarArgs("%s (request from plugin[%d])", buf, unloader_index));
darope@248
  1450
		case PNL_PLG_FORCED:
darope@248
  1451
			STRNCPY(buf, str_reason(PNL_NULL, preason), sizeof(buf));
darope@248
  1452
			return(UTIL_VarArgs("%s (forced request from plugin[%d])", buf, unloader_index));
darope@248
  1453
		case PNL_RELOAD:
darope@248
  1454
			return("reloading");
darope@0
  1455
		default:
darope@248
  1456
			return(UTIL_VarArgs("unknown (%d)", preal_reason));
darope@0
  1457
	}
darope@0
  1458
}
darope@0
  1459
darope@0
  1460
// Return a string describing how the plugin was loaded.
darope@0
  1461
// meta_errno values:
darope@0
  1462
//  - none
dvander@261
  1463
const char *MPlugin::str_source(STR_SOURCE fmt) {
darope@0
  1464
	switch(source) {
darope@0
  1465
		case PS_INI:
darope@0
  1466
			if(fmt==SO_SHOW) return("ini");
darope@0
  1467
			else return("ini file");
darope@0
  1468
		case PS_CMD:
darope@0
  1469
			if(fmt==SO_SHOW) return("cmd");
darope@0
  1470
			else return("console command");
darope@248
  1471
		case PS_PLUGIN:
darope@248
  1472
			if (source_plugin_index <= 0) {
darope@248
  1473
				if (fmt==SO_SHOW) 
darope@248
  1474
					return("plUN");
darope@248
  1475
				else 
darope@248
  1476
					return("unloaded plugin");
darope@248
  1477
			} else {
darope@248
  1478
				if(fmt==SO_SHOW) return(UTIL_VarArgs("pl%d", source_plugin_index));
darope@248
  1479
				else return(UTIL_VarArgs("plugin [%d]", source_plugin_index));
darope@248
  1480
			}
darope@0
  1481
		default:
darope@0
  1482
			if(fmt==SO_SHOW) return(UTIL_VarArgs("UNK%d", source));
darope@0
  1483
			else return(UTIL_VarArgs("unknown (%d)", source));
darope@0
  1484
	}
darope@0
  1485
}