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