metamod/mplugin.cpp
author bailopan
Wed Feb 01 07:59:27 2006 +0000 (2006-02-01)
branchB-work
changeset 203 3d387d8b69ed
parent 196 023c288639f7
child 217 43ad553832f6
permissions -rw-r--r--
Patched bug where newdll wasn't sent for gamedlls without newdll funcs
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@90
    66
	// skip whitespace at start of line
darope@90
    67
	while(*line==' ' || *line=='\t' ) line++;
darope@90
    68
darope@96
    69
	// remove whitespace at end of line
darope@96
    70
	cp = line + strlen(line) -1;
darope@96
    71
	while(*cp==' ' || *cp=='\t') *cp--='\0';
darope@96
    72
darope@90
    73
	// skip empty lines
darope@90
    74
	if(line[0]=='\0') {
darope@90
    75
		META_DEBUG(7, ("ini: Ignoring empty line: %s", line));
darope@90
    76
		RETURN_ERRNO(mFALSE, ME_BLANK);
darope@90
    77
	}
darope@90
    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@90
    88
	if(strcasecmp(token, PLATFORM) == 0) {
darope@90
    89
		pfspecific=0;
darope@90
    90
	} else if(strcasecmp(token, PLATFORM_SPC) == 0) {
darope@90
    91
		pfspecific=1;
darope@90
    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
bailopan@130
   133
// Unload a plugin from plugin request
bailopan@130
   134
// meta_errno values:
bailopan@130
   135
//  - errno's from unload()
bailopan@130
   136
mBOOL MPlugin::plugin_unload(plid_t plid, PLUG_LOADTIME now, PL_UNLOAD_REASON reason)
bailopan@130
   137
{
bailopan@130
   138
	PLUG_ACTION old_action;
bailopan@130
   139
	MPlugin *pl_unloader;
bailopan@130
   140
bailopan@130
   141
	if ( !(pl_unloader=Plugins->find(plid)) )
bailopan@130
   142
	{
bailopan@130
   143
		META_WARNING("dll: Not unloading plugin '%s'; plugin that requested unload is not found.", desc);
bailopan@130
   144
		RETURN_ERRNO(mFALSE, ME_BADREQ);
bailopan@130
   145
	} else if (pl_unloader->index == index) {
bailopan@130
   146
		META_WARNING("dll: Not unloading plugin '%s'; Plugin tried to unload itself.", desc);
bailopan@130
   147
		RETURN_ERRNO(mFALSE, ME_UNLOAD_SELF);
bailopan@130
   148
	} else if (is_unloader) {
bailopan@130
   149
		META_WARNING("dll: Not unloading plugin '%s'; Plugin is unloading plugin that tried to unload it.", desc);
bailopan@130
   150
		RETURN_ERRNO(mFALSE, ME_UNLOAD_UNLOADER);
bailopan@130
   151
	} else {
bailopan@130
   152
		unloader_index = pl_unloader->index;
bailopan@130
   153
	}
bailopan@130
   154
bailopan@130
   155
	//block unloader from being unloaded by other plugin
bailopan@130
   156
	pl_unloader->is_unloader = mTRUE;
bailopan@130
   157
bailopan@130
   158
	//try unload
bailopan@130
   159
	old_action = action;
bailopan@130
   160
	action = PA_UNLOAD;
bailopan@130
   161
	if (unload(now, reason, PNL_PLG_FORCED))
bailopan@130
   162
	{
bailopan@130
   163
		META_DEBUG(1,("Unloaded plugin '%s'", desc));
bailopan@130
   164
		pl_unloader->is_unloader = mFALSE;
bailopan@130
   165
		return mTRUE;
bailopan@130
   166
	}
bailopan@130
   167
bailopan@130
   168
	pl_unloader->is_unloader = mFALSE;
bailopan@130
   169
bailopan@130
   170
	//can't unload plugin now, set delayed
bailopan@130
   171
	if (meta_errno==ME_DELAYED)
bailopan@130
   172
	{
bailopan@130
   173
		action = old_action;
bailopan@130
   174
		meta_errno = ME_NOTALLOWED;
bailopan@130
   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)));
bailopan@130
   176
	}
bailopan@130
   177
bailopan@130
   178
	return mFALSE;
bailopan@130
   179
}
bailopan@130
   180
bailopan@130
   181
// Parse a filename string from PEXT_LOAD_PLUGIN_BY_* function into a plugin.
bailopan@130
   182
// meta_errno values:
bailopan@130
   183
mBOOL MPlugin::plugin_parseline(const char *fname, int loader_index)
bailopan@130
   184
{
bailopan@130
   185
	char *cp;
bailopan@130
   186
bailopan@130
   187
	source_plugin_index = loader_index;
bailopan@130
   188
bailopan@130
   189
	STRNCPY(filename, fname, sizeof(filename));
bailopan@130
   190
	normalize_pathname(filename);
bailopan@130
   191
bailopan@130
   192
	//store just name of the actual _file, without path
bailopan@130
   193
	cp = strrchr(filename, '/');
bailopan@130
   194
	if (cp)
bailopan@130
   195
		file = cp+1;
bailopan@130
   196
	else
bailopan@130
   197
		file = filename;
bailopan@130
   198
bailopan@130
   199
	//grab description
bailopan@130
   200
	//temporarily use plugin file until plugin can be queried
bailopan@130
   201
	snprintf(desc, sizeof(desc), "<%s>", file);
bailopan@130
   202
bailopan@130
   203
	//make full pathname
bailopan@130
   204
	full_gamedir_path(filename, pathname);
bailopan@130
   205
bailopan@130
   206
	source = PS_PLUGIN;
bailopan@130
   207
	status = PL_VALID;
bailopan@130
   208
bailopan@130
   209
	return mTRUE;
bailopan@130
   210
}
bailopan@130
   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@90
   470
	snprintf(buf, sizeof(buf), "%s_amd64.so", path);
darope@90
   471
	if(stat(buf, &st) == 0 && S_ISREG(st.st_mode))
darope@90
   472
		return(buf);
darope@0
   473
#endif /* linux */
darope@0
   474
darope@0
   475
	return(NULL);
darope@0
   476
}
darope@0
   477
darope@105
   478
darope@105
   479
// Check if a passed string starts with a known platform postfix.
darope@105
   480
// It does not check beyond the period in order to work for both
darope@105
   481
// Linux and Win32.
darope@105
   482
static mBOOL is_platform_postfix(char* pf)
darope@105
   483
{
darope@105
   484
	if ( NULL == pf ) return mFALSE;
darope@105
   485
darope@105
   486
	if ( 0 == strncmp(pf, "_i386.", 6) ) return mTRUE;
darope@105
   487
	if ( 0 == strncmp(pf, "_i486.", 6) ) return mTRUE;
darope@105
   488
	if ( 0 == strncmp(pf, "_i586.", 6) ) return mTRUE;
darope@105
   489
	if ( 0 == strncmp(pf, "_i686.", 6) ) return mTRUE;
darope@105
   490
	if ( 0 == strncmp(pf, "_amd64.", 7) ) return mTRUE;
darope@105
   491
darope@105
   492
	return mFALSE;
darope@105
   493
}
darope@105
   494
darope@105
   495
darope@90
   496
// Check if a given plugin is the same but possibly for a
darope@90
   497
// different platform. A match is considered to be found if
darope@90
   498
// 1.  the filename without the path is the same, or
darope@90
   499
// 2a. for an attached plugin the logtag is the same, or
darope@90
   500
// 2b. the description is the same, or
darope@90
   501
// 3.  a significant part of the filename is the same.
darope@105
   502
// A significant part of a plugin name is currently defined to:
darope@105
   503
//  the part up to a known platform postfix as determined by 
darope@105
   504
//  the is_platform_postfix() function (see above), or
darope@105
   505
//  the part up to the last dot, if one exists.
darope@90
   506
// meta_errno values:
darope@90
   507
//  - none
darope@90
   508
mBOOL MPlugin::platform_match(MPlugin* other) {
darope@90
   509
	char *end, *other_end;
darope@90
   510
	int prefixlen;
darope@90
   511
darope@90
   512
	if(status == PL_EMPTY || other->status == PL_EMPTY) return mFALSE;
darope@90
   513
darope@90
   514
	if(strcmp(file, other->file) == 0) return mTRUE;
darope@90
   515
darope@90
   516
	if(status >= PL_OPENED && other->status >= PL_OPENED &&
darope@90
   517
       strcmp(info->logtag, other->info->logtag) == 0 ) return mTRUE;
darope@90
   518
	if(*desc != '\0' && strcasecmp(desc,other->desc) == 0) return mTRUE;
darope@90
   519
darope@90
   520
	end=strrchr(file, '_');
darope@105
   521
	if(end == NULL || !is_platform_postfix(end)) end=strrchr(file, '.');
darope@90
   522
	other_end=strrchr(other->file, '_');
darope@105
   523
	if(other_end == NULL || !is_platform_postfix(other_end)) other_end=strrchr(other->file, '.');
darope@90
   524
darope@90
   525
	if(end == NULL || other_end == NULL) return mFALSE;
darope@90
   526
darope@90
   527
	prefixlen=end-file;
darope@90
   528
	if((other_end-other->file) != prefixlen) return mFALSE;
darope@90
   529
darope@90
   530
	if(strncmp(file,other->file, prefixlen) == 0) return mTRUE;
darope@90
   531
darope@90
   532
	return mFALSE;
darope@90
   533
}
darope@90
   534
darope@90
   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@155
   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;
bailopan@196
   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) {
bailopan@127
   809
		gamedll_funcs.dllapi_table = (DLL_FUNCTIONS *) calloc(1, 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@0
   818
		gamedll_funcs.newapi_table = (NEW_DLL_FUNCTIONS *) malloc(sizeof(NEW_DLL_FUNCTIONS));
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@0
   823
		memcpy(gamedll_funcs.newapi_table, GameDLL.funcs.newapi_table, 
darope@0
   824
				sizeof(NEW_DLL_FUNCTIONS));
darope@0
   825
	}
darope@0
   826
	if(!(pfn_attach = (META_ATTACH_FN) DLSYM(handle, "Meta_Attach"))) {
darope@0
   827
		META_ERROR("dll: Failed attach plugin '%s': Couldn't find Meta_Attach(): %s", desc, DLERROR());
darope@0
   828
		// caller will dlclose()
darope@0
   829
		RETURN_ERRNO(mFALSE, ME_DLMISSING);
darope@0
   830
	}
darope@0
   831
darope@0
   832
	memset(&meta_table, 0, sizeof(meta_table));
darope@0
   833
	// get table of function tables,
darope@0
   834
	// give public meta globals
darope@0
   835
	ret=pfn_attach(now, &meta_table, &PublicMetaGlobals, &gamedll_funcs);
darope@0
   836
	if(ret != TRUE) {
darope@0
   837
		META_ERROR("dll: Failed attach plugin '%s': Error from Meta_Attach(): %d", desc, ret);
darope@0
   838
		// caller will dlclose()
darope@0
   839
		RETURN_ERRNO(mFALSE, ME_DLERROR);
darope@0
   840
	}
darope@0
   841
	META_DEBUG(6, ("dll: Plugin '%s': Called Meta_Attach() successfully", desc));
darope@0
   842
darope@0
   843
	// Rather than duplicate code, we use another ugly macro.  Again,
darope@0
   844
	// a function isn't an option since we have varying types.
darope@0
   845
#define GET_FUNC_TABLE_FROM_PLUGIN(pfnGetFuncs, STR_GetFuncs, struct_field, API_TYPE, TABLE_TYPE, vers_pass, vers_int, vers_want) \
darope@0
   846
	if(meta_table.pfnGetFuncs) { \
bailopan@127
   847
		if (!struct_field) \
bailopan@127
   848
			struct_field = (TABLE_TYPE*) calloc(1, sizeof(TABLE_TYPE)); \
darope@0
   849
		if(meta_table.pfnGetFuncs(struct_field, vers_pass)) { \
darope@0
   850
			META_DEBUG(3, ("dll: Plugin '%s': Found %s", desc, STR_GetFuncs)); \
darope@0
   851
		} \
darope@0
   852
		else { \
darope@0
   853
			META_ERROR("dll: Failure calling %s in plugin '%s'", STR_GetFuncs, desc); \
darope@0
   854
			if(vers_int != vers_want) \
darope@0
   855
				META_ERROR("dll: Interface version didn't match; expected %d, found %d", vers_want, vers_int); \
darope@0
   856
		} \
darope@0
   857
	} \
darope@0
   858
	else { \
darope@0
   859
		META_DEBUG(5, ("dll: Plugin '%s': No %s", desc, STR_GetFuncs)); \
bailopan@127
   860
		if (struct_field) \
bailopan@127
   861
			free(struct_field); \
darope@0
   862
		struct_field=NULL; \
darope@0
   863
	}
darope@0
   864
darope@0
   865
	// Look for API-NEW interface in plugin.  We do this before API2/API, because
darope@0
   866
	// that's what the engine appears to do..
darope@184
   867
	// But we only do this if the gamedll provides these, so that we don't
darope@184
   868
	// give a plugin the idea that we'll call them, when in fact we won't
darope@184
   869
	// (since we don't export them to the engine when the gamedll doesn't
darope@184
   870
	// provide them).
bailopan@203
   871
	iface_vers=NEW_DLL_FUNCTIONS_VERSION;
bailopan@203
   872
	GET_FUNC_TABLE_FROM_PLUGIN(pfnGetNewDLLFunctions, 
bailopan@203
   873
			"GetNewDLLFunctions", newapi_table, 
bailopan@203
   874
			NEW_DLL_FUNCTIONS_FN, NEW_DLL_FUNCTIONS,
bailopan@203
   875
			&iface_vers, iface_vers, NEW_DLL_FUNCTIONS_VERSION);
bailopan@203
   876
	iface_vers=NEW_DLL_FUNCTIONS_VERSION;
bailopan@203
   877
	GET_FUNC_TABLE_FROM_PLUGIN(pfnGetNewDLLFunctions_Post, 
bailopan@203
   878
			"GetNewDLLFunctions_Post", newapi_post_table, 
bailopan@203
   879
			NEW_DLL_FUNCTIONS_FN, NEW_DLL_FUNCTIONS,
bailopan@203
   880
			&iface_vers, iface_vers, NEW_DLL_FUNCTIONS_VERSION);
darope@0
   881
darope@0
   882
	// Look for API2 interface in plugin; preferred over API-1.
darope@0
   883
	iface_vers=INTERFACE_VERSION;
darope@0
   884
	GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEntityAPI2, 
darope@0
   885
			"GetEntityAPI2", dllapi_table, 
darope@0
   886
			APIFUNCTION2, DLL_FUNCTIONS,
darope@0
   887
			&iface_vers, iface_vers, INTERFACE_VERSION);
darope@0
   888
	iface_vers=INTERFACE_VERSION;
darope@0
   889
	GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEntityAPI2_Post, 
darope@0
   890
			"GetEntityAPI2_Post", dllapi_post_table, 
darope@0
   891
			APIFUNCTION2, DLL_FUNCTIONS,
darope@0
   892
			&iface_vers, iface_vers, INTERFACE_VERSION);
darope@0
   893
darope@0
   894
	// Look for old-style API in plugin, if API2 interface wasn't found.
darope@0
   895
	if(!dllapi_table && !dllapi_post_table) {
darope@0
   896
		GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEntityAPI, 
darope@0
   897
				"GetEntityAPI", dllapi_table, 
darope@0
   898
				APIFUNCTION, DLL_FUNCTIONS,
darope@0
   899
				INTERFACE_VERSION, INTERFACE_VERSION, INTERFACE_VERSION);
darope@0
   900
		GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEntityAPI_Post, 
darope@0
   901
				"GetEntityAPI_Post", dllapi_post_table, 
darope@0
   902
				APIFUNCTION, DLL_FUNCTIONS,
darope@0
   903
				INTERFACE_VERSION, INTERFACE_VERSION, INTERFACE_VERSION);
darope@0
   904
	}
darope@0
   905
darope@0
   906
	// Look for Engine interface.
darope@0
   907
	iface_vers=ENGINE_INTERFACE_VERSION;
darope@0
   908
	GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEngineFunctions, 
darope@0
   909
			"GetEngineFunctions", engine_table, 
darope@0
   910
			GET_ENGINE_FUNCTIONS_FN, enginefuncs_t,
darope@0
   911
			&iface_vers, iface_vers, ENGINE_INTERFACE_VERSION);
darope@0
   912
	iface_vers=ENGINE_INTERFACE_VERSION;
darope@0
   913
	GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEngineFunctions_Post, 
darope@0
   914
			"GetEngineFunctions_Post", engine_post_table, 
darope@0
   915
			GET_ENGINE_FUNCTIONS_FN, enginefuncs_t,
darope@0
   916
			&iface_vers, iface_vers, ENGINE_INTERFACE_VERSION);
darope@0
   917
darope@0
   918
	if(!dllapi_table && !dllapi_post_table 
darope@0
   919
		&& !newapi_table && !newapi_post_table
darope@0
   920
		&& !engine_table && !engine_post_table)
darope@0
   921
	{
darope@0
   922
		META_LOG("dll: Plugin '%s' isn't catching _any_ functions ??", desc);
darope@0
   923
	}
darope@0
   924
darope@0
   925
	time_loaded=time(NULL);
darope@0
   926
	return(mTRUE);
darope@0
   927
}
darope@0
   928
darope@0
   929
// Unload a plugin.  Check time, detach.
darope@0
   930
// meta_errno values:
darope@0
   931
//  - ME_ARGUMENT	missing necessary fields in plugin
darope@0
   932
//  - ME_ALREADY	this plugin already unloaded
darope@0
   933
//  - ME_BADREQ		plugin not marked for unload
darope@0
   934
//  - ME_DELAYED	unload request is delayed (till changelevel?)
darope@0
   935
//  - ME_NOTALLOWED	plugin not unloadable after startup
darope@0
   936
//  - errno's from check_input()
bailopan@130
   937
mBOOL MPlugin::unload(PLUG_LOADTIME now, PL_UNLOAD_REASON reason, PL_UNLOAD_REASON real_reason) {
darope@0
   938
	if(!check_input()) {
darope@0
   939
		// details logged, meta_errno set in check_input()
darope@0
   940
		RETURN_ERRNO(mFALSE, ME_ARGUMENT);
darope@0
   941
	}
darope@0
   942
	if(status < PL_RUNNING) {
bailopan@143
   943
		if (reason != PNL_CMD_FORCED && reason != PNL_RELOAD) {
bailopan@143
   944
			META_ERROR("dll: Not unloading plugin '%s'; already unloaded (status=%s)", desc, str_status());
bailopan@143
   945
			RETURN_ERRNO(mFALSE, ME_ALREADY);
bailopan@143
   946
		}
darope@0
   947
	}
darope@0
   948
	if(action != PA_UNLOAD && action != PA_RELOAD) {
bailopan@143
   949
		META_WARNING("dll: Not unloading plugin '%s'; not marked for unload (action=%s)", desc, str_action());
darope@0
   950
		RETURN_ERRNO(mFALSE, ME_BADREQ);
darope@0
   951
	}
darope@0
   952
darope@0
   953
	// Are we allowed to detach this plugin at this time?
darope@0
   954
	// If forcing unload, we disregard when plugin wants to be unloaded.
bailopan@143
   955
	if(info && info->unloadable < now) {
darope@0
   956
		if(reason == PNL_CMD_FORCED) {
darope@0
   957
			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
   958
		}
darope@0
   959
		else {
darope@0
   960
			if(info->unloadable > PT_STARTUP) {
darope@0
   961
				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
   962
				// caller should give message to user
darope@0
   963
				// try to unload again at next opportunity
darope@0
   964
				RETURN_ERRNO(mFALSE, ME_DELAYED);
darope@0
   965
			}
darope@0
   966
			else {
darope@0
   967
				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
   968
				// don't try to unload again later
darope@0
   969
				action=PA_NONE;
darope@0
   970
				RETURN_ERRNO(mFALSE, ME_NOTALLOWED);
darope@0
   971
			}
darope@0
   972
		}
darope@0
   973
	}
darope@0
   974
darope@0
   975
	// If unloading during map, then I'd like to call plugin's
darope@0
   976
	// ServerDeactivate.  However, I don't want to do this until I start
darope@0
   977
	// calling ServerActivate when loading during map, since the SDK
darope@0
   978
	// indicates these two routines should match call for call.
darope@0
   979
darope@0
   980
	// detach plugin
darope@0
   981
	if(!detach(now, reason)) {
darope@155
   982
		if (reason == PNL_RELOAD){
bailopan@143
   983
			META_DEBUG(2, ("dll: Reload plugin '%s' overriding failed detach", desc));
bailopan@143
   984
		} else if(reason == PNL_CMD_FORCED) {
darope@0
   985
			META_DEBUG(2, ("dll: Forced unload plugin '%s' overriding failed detach"));
darope@0
   986
		}
darope@0
   987
		else {
bailopan@143
   988
			META_WARNING("dll: Failed to detach plugin '%s'; ", desc);
darope@0
   989
			// meta_errno should be already set in detach()
darope@0
   990
			return(mFALSE);
darope@0
   991
		}
darope@0
   992
	}
darope@0
   993
bailopan@143
   994
	Plugins->clear_source_plugin_index(index);
bailopan@143
   995
darope@0
   996
	// successful detach, or forced unload
darope@0
   997
darope@0
   998
	// Unmark registered commands for this plugin (by index number).
darope@0
   999
	RegCmds->disable(index);
darope@0
  1000
	// Unmark registered cvars for this plugin (by index number).
darope@0
  1001
	RegCvars->disable(index);
darope@0
  1002
darope@0
  1003
#ifdef UNFINISHED
darope@0
  1004
	// Remove all requested hooks from this plugin (by index number).
darope@0
  1005
	Hooks->remove_all(info);
darope@0
  1006
#endif /* UNFINISHED */
darope@0
  1007
darope@0
  1008
	// Close the file.  Note: after this, attempts to reference any memory
darope@0
  1009
	// locations in the file will produce a segfault.
darope@0
  1010
	if(DLCLOSE(handle) != 0) {
bailopan@143
  1011
		// If DLL cannot be closed, OS is badly broken or we are giving invalid handle.
bailopan@143
  1012
		// So we don't return here but instead remove plugin from our listings.
bailopan@143
  1013
		META_WARNING("dll: Couldn't close plugin file '%s': %s", file, DLERROR());
darope@0
  1014
	}
darope@0
  1015
	handle=NULL;
darope@0
  1016
darope@0
  1017
	if(action==PA_UNLOAD) {
darope@0
  1018
		status=PL_EMPTY;
darope@0
  1019
		clear();
darope@0
  1020
	}
darope@0
  1021
	else if(action==PA_RELOAD) {
darope@0
  1022
		status=PL_VALID;
darope@0
  1023
		action=PA_LOAD;
bailopan@127
  1024
		clear();
darope@0
  1025
	}
bailopan@130
  1026
	META_LOG("dll: Unloaded plugin '%s' for reason '%s'", desc, str_reason(reason, real_reason));
darope@0
  1027
	return(mTRUE);
darope@0
  1028
}
darope@0
  1029
darope@0
  1030
// Inform plugin we're going to unload it.
darope@0
  1031
// meta_errno values:
darope@0
  1032
//  - ME_DLMISSING	couldn't find meta_detach() in plugin
darope@0
  1033
//  - ME_DLERROR	plugin detach() returned error
darope@0
  1034
mBOOL MPlugin::detach(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) {
darope@0
  1035
	int ret;
darope@0
  1036
	META_DETACH_FN pfn_detach;
darope@0
  1037
darope@155
  1038
	// If we have no handle, i.e. no dll loaded, we return true because the
darope@155
  1039
	// dll is obviously detached. We shouldn't call DLSYM() with a NULL
darope@155
  1040
	// handle since this will DLSYM() ourself.
darope@155
  1041
	if(!handle)
darope@155
  1042
		return mTRUE;
darope@155
  1043
darope@0
  1044
	if(!(pfn_detach = (META_DETACH_FN) DLSYM(handle, "Meta_Detach"))) {
bailopan@194
  1045
		META_ERROR("dll: Error detach plugin '%s': Couldn't find Meta_Detach(): %s", desc, DLERROR());
darope@0
  1046
		// caller will dlclose()
darope@0
  1047
		RETURN_ERRNO(mFALSE, ME_DLMISSING);
darope@0
  1048
	}
darope@0
  1049
darope@0
  1050
	ret=pfn_detach(now, reason);
darope@0
  1051
	if(ret != TRUE) {
darope@0
  1052
		META_ERROR("dll: Failed detach plugin '%s': Error from Meta_Detach(): %d", desc, ret);
darope@0
  1053
		RETURN_ERRNO(mFALSE, ME_DLERROR);
darope@0
  1054
	}
darope@0
  1055
	META_DEBUG(6, ("dll: Plugin '%s': Called Meta_Detach() successfully", desc));
darope@0
  1056
	return(mTRUE);
darope@0
  1057
}
darope@0
  1058
darope@0
  1059
// Reload a plugin; unload and load again.
darope@0
  1060
// meta_errno values:
darope@107
  1061
//  - ME_NOTALLOWED	plugin not loadable after startup
darope@107
  1062
//  - errno's from check_input()
darope@0
  1063
//  - errno's from unload()
darope@0
  1064
//  - errno's from load()
darope@0
  1065
mBOOL MPlugin::reload(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) {
darope@107
  1066
	if(!check_input()) {
darope@107
  1067
		// details logged, meta_errno set in check_input()
darope@107
  1068
		RETURN_ERRNO(mFALSE, ME_ARGUMENT);
darope@107
  1069
	}
darope@107
  1070
	// Are we allowed to load this plugin at this time?
darope@107
  1071
	// If we cannot load the plugin after unloading it, we keep it.
bailopan@130
  1072
	if (info && info->loadable < now)
bailopan@130
  1073
	{
darope@107
  1074
		if(info->loadable > PT_STARTUP) {
darope@107
  1075
			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@107
  1076
			// caller should give message to user
darope@107
  1077
			// try to reload again at next opportunity
darope@107
  1078
			RETURN_ERRNO(mFALSE, ME_DELAYED);
darope@107
  1079
		}
darope@107
  1080
		else {
darope@107
  1081
			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@107
  1082
			// don't try to reload again later
darope@107
  1083
			action=PA_NONE;
darope@107
  1084
			RETURN_ERRNO(mFALSE, ME_NOTALLOWED);
darope@107
  1085
		}
darope@107
  1086
	}
darope@107
  1087
bailopan@143
  1088
	if (status < PL_RUNNING)
bailopan@143
  1089
	{
bailopan@143
  1090
		META_WARNING("dll: Plugin '%s' isn't running; Forcing unload plugin for reloading", desc);
bailopan@143
  1091
		reason = PNL_RELOAD;
bailopan@143
  1092
	}
bailopan@143
  1093
	if (!unload(now, reason, reason)) {
bailopan@143
  1094
		META_WARNING("dll: Failed to unload plugin '%s' for reloading", desc);
darope@0
  1095
		// meta_errno should be set already in unload()
darope@0
  1096
		return(mFALSE);
darope@0
  1097
	}
darope@0
  1098
	if(!load(now)) {
bailopan@143
  1099
		META_WARNING("dll: Failed to reload plugin '%s' after unloading", desc);
darope@0
  1100
		// meta_errno should be set already in load()
darope@0
  1101
		return(mFALSE);
darope@0
  1102
	}
darope@0
  1103
	return(mTRUE);
darope@0
  1104
}
darope@0
  1105
darope@0
  1106
// Pause a plugin; temporarily disabled for API routines.
darope@0
  1107
// meta_errno values:
darope@0
  1108
//  - ME_ALREADY	this plugin already paused
darope@0
  1109
//  - ME_BADREQ		can't pause; not running
darope@0
  1110
//  - ME_NOTALLOWED	plugin doesn't want to be paused
darope@0
  1111
mBOOL MPlugin::pause(void) {
darope@0
  1112
	if(status == PL_PAUSED) {
darope@0
  1113
		META_ERROR("Not pausing plugin '%s'; already paused", desc);
darope@0
  1114
		RETURN_ERRNO(mFALSE, ME_ALREADY);
darope@0
  1115
	}
darope@0
  1116
	if(status != PL_RUNNING) {
darope@0
  1117
		META_ERROR("Cannot pause plugin '%s'; not currently running (status=%s)", desc, str_status());
darope@0
  1118
		RETURN_ERRNO(mFALSE, ME_BADREQ);
darope@0
  1119
	}
darope@0
  1120
darope@0
  1121
	// are we allowed to pause this plugin?
darope@0
  1122
	if(info->unloadable < PT_ANYPAUSE) {
darope@0
  1123
		META_ERROR("Cannot pause plugin '%s'; not allowed by plugin (allowed=%s)", desc, str_unloadable());
darope@0
  1124
		action=PA_NONE;
darope@0
  1125
		RETURN_ERRNO(mFALSE, ME_NOTALLOWED);
darope@0
  1126
	}
darope@0
  1127
darope@0
  1128
	status=PL_PAUSED;
darope@0
  1129
	META_LOG("Paused plugin '%s'", desc);
darope@0
  1130
	return(mTRUE);
darope@0
  1131
}
darope@0
  1132
darope@0
  1133
// Unpause a plugin.
darope@0
  1134
// meta_errno values:
darope@0
  1135
//  - ME_BADREQ		can't unpause; not paused
darope@0
  1136
mBOOL MPlugin::unpause(void) {
darope@0
  1137
	if(status != PL_PAUSED) {
darope@0
  1138
		META_ERROR("Cannot unpause plugin '%s'; not currently paused (status=%s)", desc, str_status());
darope@0
  1139
		RETURN_ERRNO(mFALSE, ME_BADREQ);
darope@0
  1140
	}
darope@0
  1141
	status=PL_RUNNING;
darope@0
  1142
	META_LOG("Unpaused plugin '%s'", desc);
darope@0
  1143
	return(mTRUE);
darope@0
  1144
}
darope@0
  1145
darope@0
  1146
// Retry pending action, presumably from a previous failure.
darope@0
  1147
// meta_errno values:
darope@0
  1148
//  - ME_BADREQ		no pending action
darope@0
  1149
//  - errno's from load()
darope@0
  1150
//  - errno's from unload()
darope@0
  1151
//  - errno's from reload()
darope@0
  1152
mBOOL MPlugin::retry(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) {
darope@0
  1153
	if(action==PA_LOAD)
darope@0
  1154
		return(load(now));
darope@0
  1155
	else if(action==PA_ATTACH)
darope@0
  1156
		return(load(now));
darope@0
  1157
	else if(action==PA_UNLOAD)
bailopan@130
  1158
		return(unload(now, reason, reason));
darope@0
  1159
	else if(action==PA_RELOAD)
darope@0
  1160
		return(reload(now, reason));
darope@0
  1161
	else {
darope@0
  1162
		META_ERROR("No pending action to retry for plugin '%s'; (status=%s, action=%s)", desc, str_status(), str_action());
darope@0
  1163
		RETURN_ERRNO(mFALSE, ME_BADREQ);
darope@0
  1164
	}
darope@0
  1165
}
darope@0
  1166
darope@0
  1167
// Clear a plugin (it failed a previous action and should be
darope@0
  1168
// removed from the list, or it's being unloaded).
darope@0
  1169
// meta_errno values:
darope@0
  1170
//  - ME_BADREQ		not marked for clearing
darope@0
  1171
//  - ME_DLERROR	failed to dlclose
darope@0
  1172
mBOOL MPlugin::clear(void) {
darope@0
  1173
	if(status != PL_FAILED && status != PL_BADFILE
darope@0
  1174
			&& status != PL_EMPTY && status != PL_OPENED) {
darope@0
  1175
		META_ERROR("Cannot clear plugin '%s'; not marked as failed, empty, or open (status=%s)", desc, str_status());
darope@0
  1176
		RETURN_ERRNO(mFALSE, ME_BADREQ);
darope@0
  1177
	}
darope@0
  1178
	// If file is open, close the file.  Note: after this, attempts to
darope@0
  1179
	// reference any memory locations in the file will produce a segfault.
darope@0
  1180
	if(handle && DLCLOSE(handle) != 0) {
darope@0
  1181
		META_ERROR("dll: Couldn't close plugin file '%s': %s", file, DLERROR());
darope@0
  1182
		status=PL_FAILED;
darope@0
  1183
		RETURN_ERRNO(mFALSE, ME_DLERROR);
darope@0
  1184
	}
darope@0
  1185
	handle=NULL;
darope@0
  1186
darope@0
  1187
	if(gamedll_funcs.dllapi_table) free(gamedll_funcs.dllapi_table);
darope@0
  1188
	if(gamedll_funcs.newapi_table) free(gamedll_funcs.newapi_table);
bailopan@115
  1189
	if(dllapi_table) free(dllapi_table);
bailopan@115
  1190
	if(dllapi_post_table) free(dllapi_post_table);
bailopan@115
  1191
	if(newapi_table) free(newapi_table);
bailopan@115
  1192
	if(newapi_post_table) free(newapi_post_table);
bailopan@115
  1193
	if(engine_table) free(engine_table);
bailopan@115
  1194
	if(engine_post_table) free(engine_post_table);
darope@0
  1195
darope@0
  1196
	status=PL_EMPTY;
darope@0
  1197
	action=PA_NULL;
darope@0
  1198
	handle=NULL;
darope@0
  1199
	info=NULL;
darope@0
  1200
	time_loaded=0;
darope@0
  1201
	dllapi_table=NULL;
darope@0
  1202
	dllapi_post_table=NULL;
darope@0
  1203
	newapi_table=NULL;
darope@0
  1204
	newapi_post_table=NULL;
darope@0
  1205
	engine_table=NULL;
darope@0
  1206
	engine_post_table=NULL;
bailopan@115
  1207
	gamedll_funcs.dllapi_table = NULL;
bailopan@115
  1208
	gamedll_funcs.newapi_table = NULL;
bailopan@139
  1209
	source_plugin_index = 0;
bailopan@139
  1210
	unloader_index = 0;
bailopan@139
  1211
	is_unloader = mFALSE;
darope@0
  1212
	return(mTRUE);
darope@0
  1213
}
darope@0
  1214
darope@0
  1215
// List information about plugin to console.
darope@0
  1216
void MPlugin::show(void) {
darope@0
  1217
	char *cp, *tstr;
darope@0
  1218
	int n, width;
darope@0
  1219
	width=13;
darope@0
  1220
	META_CONS("%*s: %s", width, "name", info ? info->name : "(nil)");
darope@0
  1221
	META_CONS("%*s: %s", width, "desc", desc);
darope@0
  1222
	META_CONS("%*s: %s", width, "status", str_status());
darope@0
  1223
	META_CONS("%*s: %s", width, "action", str_action());
darope@0
  1224
	META_CONS("%*s: %s", width, "filename", filename);
darope@0
  1225
	META_CONS("%*s: %s", width, "file", file);
darope@0
  1226
	META_CONS("%*s: %s", width, "pathname", pathname);
darope@0
  1227
	META_CONS("%*s: %d", width, "index", index);
darope@0
  1228
	META_CONS("%*s: %s", width, "source", str_source());
darope@0
  1229
	META_CONS("%*s: %s", width, "loadable", str_loadable(SL_ALLOWED));
darope@0
  1230
	META_CONS("%*s: %s", width, "unloadable", str_unloadable(SL_ALLOWED));
darope@0
  1231
	META_CONS("%*s: %s", width, "version", info ? info->version : "(nil)");
darope@0
  1232
	META_CONS("%*s: %s", width, "date", info ? info->date : "(nil)");
darope@0
  1233
	META_CONS("%*s: %s", width, "author", info ? info->author : "(nil)");
darope@0
  1234
	META_CONS("%*s: %s", width, "url", info ? info->url : "(nil)");
darope@0
  1235
	META_CONS("%*s: %s", width, "logtag", info ? info->logtag : "(nil)");
bailopan@133
  1236
	META_CONS("%*s: %s", width, "ifvers", info ? info->ifvers : "(nil)");
darope@0
  1237
	// ctime() includes newline at EOL
darope@0
  1238
	tstr=ctime(&time_loaded);
darope@0
  1239
	if((cp=strchr(tstr, '\n')))
darope@0
  1240
		*cp='\0';
darope@0
  1241
	META_CONS("%*s: %s", width, "last loaded", tstr);
darope@0
  1242
	// XXX show file time ?
darope@0
  1243
darope@0
  1244
	if(dllapi_table) {
darope@0
  1245
		META_CONS("DLLAPI functions:");
darope@0
  1246
		SHOW_DEF_DLLAPI(dllapi_table, "   ", "");
darope@0
  1247
		META_CONS("%d functions (dllapi)", n);
darope@0
  1248
	}
darope@0
  1249
	else
darope@0
  1250
		META_CONS("No DLLAPI functions.");
darope@0
  1251
	if(dllapi_post_table) {
darope@0
  1252
		META_CONS("DLLAPI-Post functions:");
darope@0
  1253
		SHOW_DEF_DLLAPI(dllapi_post_table, "   ", "_Post");
darope@0
  1254
		META_CONS("%d functions (dllapi post)", n);
darope@0
  1255
	}
darope@0
  1256
	else
darope@0
  1257
		META_CONS("No DLLAPI-Post functions.");
darope@0
  1258
darope@0
  1259
	if(newapi_table) {
darope@0
  1260
		META_CONS("NEWAPI functions:");
darope@0
  1261
		SHOW_DEF_NEWAPI(newapi_table, "   ", "");
darope@0
  1262
		META_CONS("%d functions (newapi)", n);
darope@0
  1263
	}
darope@0
  1264
	else
darope@0
  1265
		META_CONS("No NEWAPI functions.");
darope@0
  1266
	if(newapi_post_table) {
darope@0
  1267
		META_CONS("NEWAPI-Post functions:");
darope@0
  1268
		SHOW_DEF_NEWAPI(newapi_post_table, "   ", "_Post");
darope@0
  1269
		META_CONS("%d functions (newapi post)", n);
darope@0
  1270
	}
darope@0
  1271
	else
darope@0
  1272
		META_CONS("No NEWAPI-Post functions.");
darope@0
  1273
darope@0
  1274
	if(engine_table) {
darope@0
  1275
		META_CONS("Engine functions:");
darope@0
  1276
		SHOW_DEF_ENGINE(engine_table, "   ", "");
darope@0
  1277
		META_CONS("%d functions (engine)", n);
darope@0
  1278
	}
darope@0
  1279
	else
darope@0
  1280
		META_CONS("No Engine functions.");
darope@0
  1281
	if(engine_post_table) {
darope@0
  1282
		META_CONS("Engine-Post functions:");
darope@0
  1283
		SHOW_DEF_ENGINE(engine_post_table, "   ", "_Post");
darope@0
  1284
		META_CONS("%d functions (engine post)", n);
darope@0
  1285
	}
darope@0
  1286
	else
darope@0
  1287
		META_CONS("No Engine-Post functions.");
darope@0
  1288
	RegCmds->show(index);
darope@0
  1289
	RegCvars->show(index);
bailopan@139
  1290
	if (Plugins->found_child_plugins(index))
bailopan@139
  1291
		Plugins->show(index);
bailopan@139
  1292
	else
bailopan@139
  1293
		META_CONS("No child plugins.");
darope@0
  1294
}
darope@0
  1295
darope@0
  1296
// Check whether the file on disk looks like it's been updated since we
darope@0
  1297
// last loaded the plugin.
darope@0
  1298
// meta_errno values:
darope@0
  1299
//  - ME_NOFILE		couldn't find file
darope@0
  1300
//  - ME_NOERROR	no error; false indicates file not newer
darope@0
  1301
mBOOL MPlugin::newer_file(void) {
darope@0
  1302
	struct stat st;
darope@0
  1303
	time_t file_time;
darope@0
  1304
darope@0
  1305
	if(stat(pathname, &st) != 0)
darope@0
  1306
		RETURN_ERRNO(mFALSE, ME_NOFILE);
darope@0
  1307
	file_time=max(st.st_ctime, st.st_mtime);
darope@0
  1308
	META_DEBUG(5, ("newer_file? file=%s; load=%d, file=%d; ctime=%d, mtime=%d",
darope@0
  1309
				file, time_loaded, file_time, st.st_ctime, st.st_mtime));
darope@0
  1310
	if(file_time > time_loaded)
darope@0
  1311
		return(mTRUE);
darope@0
  1312
	else
darope@0
  1313
		RETURN_ERRNO(mFALSE, ME_NOERROR);
darope@0
  1314
}
darope@0
  1315
darope@0
  1316
// Return a string describing status of plugin.
darope@0
  1317
// SIMPLE is the default.
darope@0
  1318
// SHOW is max 4 chars, for "show" output.
darope@0
  1319
// meta_errno values:
darope@0
  1320
//  - none
darope@0
  1321
char *MPlugin::str_status(STR_STATUS fmt) {
darope@0
  1322
	switch(status) {
darope@0
  1323
		case PL_EMPTY:
darope@0
  1324
			if(fmt==ST_SHOW) return("empt");
darope@0
  1325
			else return("empty");
darope@0
  1326
		case PL_VALID:
darope@0
  1327
			if(fmt==ST_SHOW) return("info");
darope@0
  1328
			else return("valid");
darope@0
  1329
		case PL_BADFILE:
darope@0
  1330
			if(fmt==ST_SHOW) return("badf");
darope@0
  1331
			else return("badfile");
darope@0
  1332
		case PL_OPENED:
darope@0
  1333
			if(fmt==ST_SHOW) return("open");
darope@0
  1334
			else return("opened");
darope@0
  1335
		case PL_FAILED:
darope@0
  1336
			if(fmt==ST_SHOW) return("fail");
darope@0
  1337
			else return("failed");
darope@0
  1338
		case PL_RUNNING:
darope@0
  1339
			if(fmt==ST_SHOW) return("RUN");
darope@0
  1340
			else return("running");
darope@0
  1341
		case PL_PAUSED:
darope@0
  1342
			if(fmt==ST_SHOW) return("PAUS");
darope@0
  1343
			else return("paused");
darope@0
  1344
		default:
darope@0
  1345
			if(fmt==ST_SHOW) return(UTIL_VarArgs("UNK%d", status));
darope@0
  1346
			return(UTIL_VarArgs("unknown (%d)", status));
darope@0
  1347
	}
darope@0
  1348
}
darope@0
  1349
darope@0
  1350
// Return a string (one word) describing requested action for plugin.
darope@0
  1351
// SIMPLE is the default.
darope@0
  1352
// SHOW is max 4 chars, for "show" output.
darope@0
  1353
// meta_errno values:
darope@0
  1354
//  - none
darope@0
  1355
char *MPlugin::str_action(STR_ACTION fmt) {
darope@0
  1356
	switch(action) {
darope@0
  1357
		case PA_NULL:
darope@0
  1358
			if(fmt==SA_SHOW) return("NULL");
darope@0
  1359
			else return("null");
darope@0
  1360
		case PA_NONE:
darope@0
  1361
			if(fmt==SA_SHOW) return(" -  ");
darope@0
  1362
			else return("none");
darope@0
  1363
		case PA_KEEP:
darope@0
  1364
			if(fmt==SA_SHOW) return("keep");
darope@0
  1365
			else return("keep");
darope@0
  1366
		case PA_LOAD:
darope@0
  1367
			if(fmt==SA_SHOW) return("load");
darope@0
  1368
			else return("load");
darope@0
  1369
		case PA_ATTACH:
darope@0
  1370
			if(fmt==SA_SHOW) return("atch");
darope@0
  1371
			else return("attach");
darope@0
  1372
		case PA_UNLOAD:
darope@0
  1373
			if(fmt==SA_SHOW) return("unld");
darope@0
  1374
			else return("unload");
darope@0
  1375
		case PA_RELOAD:
darope@0
  1376
			if(fmt==SA_SHOW) return("relo");
darope@0
  1377
			else return("reload");
darope@0
  1378
		default:
darope@0
  1379
			if(fmt==SA_SHOW) return(UTIL_VarArgs("UNK%d", action));
darope@0
  1380
			else return(UTIL_VarArgs("unknown (%d)", action));
darope@0
  1381
	}
darope@0
  1382
}
darope@0
  1383
darope@0
  1384
// Return a string describing given plugin loadtime.
darope@0
  1385
// SIMPLE is the default.
darope@0
  1386
// SHOW is max 3 chars, for "show" output.
darope@0
  1387
// ALLOWED is in terms of when it's allowed to load/unload.
darope@0
  1388
// NOW is to describe current situation of load/unload attempt.
darope@0
  1389
// meta_errno values:
darope@0
  1390
//  - none
darope@0
  1391
char *MPlugin::str_loadtime(PLUG_LOADTIME ptime, STR_LOADTIME fmt) {
darope@0
  1392
	switch(ptime) {
darope@0
  1393
		case PT_NEVER:
darope@0
  1394
			if(fmt==SL_SHOW) return("Never");
darope@0
  1395
			else return("never");
darope@0
  1396
		case PT_STARTUP:
darope@0
  1397
			if(fmt==SL_SHOW) return("Start");
darope@0
  1398
			else if(fmt==SL_ALLOWED) return("at server startup");
darope@0
  1399
			else if(fmt==SL_NOW) return("during server startup");
darope@0
  1400
			else return("startup");
darope@0
  1401
		case PT_CHANGELEVEL:
darope@0
  1402
			if(fmt==SL_SHOW) return("Chlvl");
darope@0
  1403
			else if(fmt==SL_ALLOWED) return("at changelevel");
darope@0
  1404
			else if(fmt==SL_NOW) return("during changelevel");
darope@0
  1405
			else return("changelevel");
darope@0
  1406
		case PT_ANYTIME:
darope@0
  1407
			if(fmt==SL_SHOW) return("ANY");
darope@0
  1408
			else if(fmt==SL_ALLOWED) return("at any time");
darope@0
  1409
			else if(fmt==SL_NOW) return("during map");
darope@0
  1410
			else return("anytime");
darope@0
  1411
		case PT_ANYPAUSE:
darope@0
  1412
			if(fmt==SL_SHOW) return("Pause");
darope@0
  1413
			else if(fmt==SL_ALLOWED) return("at any time, and pausable");
darope@0
  1414
			else if(fmt==SL_NOW) return("for requested pause");
darope@0
  1415
			else return("pausable");
darope@0
  1416
		default:
darope@0
  1417
			if(fmt==SL_SHOW) return(UTIL_VarArgs("UNK-%d", ptime));
darope@0
  1418
			else return(UTIL_VarArgs("unknown (%d)", ptime));
darope@0
  1419
	}
darope@0
  1420
}
darope@0
  1421
darope@0
  1422
// Return a string describing why a plugin is to be unloaded.
darope@0
  1423
// meta_errno values:
darope@0
  1424
//  - none
bailopan@130
  1425
char *MPlugin::str_reason(PL_UNLOAD_REASON preason, PL_UNLOAD_REASON preal_reason) {
bailopan@130
  1426
	char buf[128];
bailopan@130
  1427
	
bailopan@130
  1428
	if(preason == PNL_PLUGIN)
bailopan@130
  1429
		preason = PNL_NULL;
bailopan@130
  1430
	if(preason == PNL_PLG_FORCED)
bailopan@130
  1431
		preason = PNL_NULL;
bailopan@130
  1432
bailopan@130
  1433
	switch(preal_reason) {
darope@0
  1434
		case PNL_NULL:
darope@0
  1435
			return("null");
darope@0
  1436
		case PNL_INI_DELETED:
darope@0
  1437
			return("deleted from ini file");
darope@0
  1438
		case PNL_FILE_NEWER:
darope@0
  1439
			return("file on disk is newer");
darope@0
  1440
		case PNL_COMMAND:
darope@0
  1441
			return("server command");
darope@0
  1442
		case PNL_CMD_FORCED:
darope@0
  1443
			return("forced by server command");
bailopan@130
  1444
		case PNL_PLUGIN:
bailopan@130
  1445
			STRNCPY(buf, str_reason(PNL_NULL, preason), sizeof(buf));
bailopan@130
  1446
			return(UTIL_VarArgs("%s (request from plugin[%d])", buf, unloader_index));
bailopan@130
  1447
		case PNL_PLG_FORCED:
bailopan@130
  1448
			STRNCPY(buf, str_reason(PNL_NULL, preason), sizeof(buf));
bailopan@130
  1449
			return(UTIL_VarArgs("%s (forced request from plugin[%d])", buf, unloader_index));
bailopan@143
  1450
		case PNL_RELOAD:
darope@155
  1451
			return("reloading");
darope@0
  1452
		default:
bailopan@130
  1453
			return(UTIL_VarArgs("unknown (%d)", preal_reason));
darope@0
  1454
	}
darope@0
  1455
}
darope@0
  1456
darope@0
  1457
// Return a string describing how the plugin was loaded.
darope@0
  1458
// meta_errno values:
darope@0
  1459
//  - none
darope@0
  1460
char *MPlugin::str_source(STR_SOURCE fmt) {
darope@0
  1461
	switch(source) {
darope@0
  1462
		case PS_INI:
darope@0
  1463
			if(fmt==SO_SHOW) return("ini");
darope@0
  1464
			else return("ini file");
darope@0
  1465
		case PS_CMD:
darope@0
  1466
			if(fmt==SO_SHOW) return("cmd");
darope@0
  1467
			else return("console command");
bailopan@130
  1468
		case PS_PLUGIN:
bailopan@133
  1469
			if (source_plugin_index <= 0) {
bailopan@133
  1470
				if (fmt==SO_SHOW) 
bailopan@133
  1471
					return("plUN");
bailopan@133
  1472
				else 
bailopan@133
  1473
					return("unloaded plugin");
bailopan@130
  1474
			} else {
bailopan@130
  1475
				if(fmt==SO_SHOW) return(UTIL_VarArgs("pl%d", source_plugin_index));
bailopan@130
  1476
				else return(UTIL_VarArgs("plugin [%d]", source_plugin_index));
bailopan@130
  1477
			}
darope@0
  1478
		default:
darope@0
  1479
			if(fmt==SO_SHOW) return(UTIL_VarArgs("UNK%d", source));
darope@0
  1480
			else return(UTIL_VarArgs("unknown (%d)", source));
darope@0
  1481
	}
darope@0
  1482
}