metamod/mplugin.cpp
author darope
Sat Aug 21 16:13:14 2004 +0000 (2004-08-21)
changeset 245 e2e3739f6ccd
parent 244 7b246a2ad39c
child 105 0363d2c6d92f
child 246 962d2947c6d6
permissions -rw-r--r--
Merged with working branch
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@244
   399
// Check if a given plugin is the same but possibly for a
darope@244
   400
// different platform. A match is considered to be found if
darope@244
   401
// 1.  the filename without the path is the same, or
darope@244
   402
// 2a. for an attached plugin the logtag is the same, or
darope@244
   403
// 2b. the description is the same, or
darope@244
   404
// 3.  a significant part of the filename is the same.
darope@244
   405
// A significant part of a plugin name is currently defined to
darope@244
   406
// the the part up to the last underscore, if one exists.
darope@244
   407
// meta_errno values:
darope@244
   408
//  - none
darope@244
   409
mBOOL MPlugin::platform_match(MPlugin* other) {
darope@244
   410
	char *end, *other_end;
darope@244
   411
	int prefixlen;
darope@244
   412
darope@244
   413
	if(status == PL_EMPTY || other->status == PL_EMPTY) return mFALSE;
darope@244
   414
darope@244
   415
	if(strcmp(file, other->file) == 0) return mTRUE;
darope@244
   416
darope@244
   417
	if(status >= PL_OPENED && other->status >= PL_OPENED &&
darope@244
   418
       strcmp(info->logtag, other->info->logtag) == 0 ) return mTRUE;
darope@244
   419
	if(*desc != '\0' && strcasecmp(desc,other->desc) == 0) return mTRUE;
darope@244
   420
darope@244
   421
	end=strrchr(file, '_');
darope@244
   422
	if(end == NULL) end=strrchr(file, '.');
darope@244
   423
	other_end=strrchr(other->file, '_');
darope@244
   424
	if(other_end == NULL) other_end=strrchr(other->file, '.');
darope@244
   425
darope@244
   426
	if(end == NULL || other_end == NULL) return mFALSE;
darope@244
   427
darope@244
   428
	prefixlen=end-file;
darope@244
   429
	if((other_end-other->file) != prefixlen) return mFALSE;
darope@244
   430
darope@244
   431
	if(strncmp(file,other->file, prefixlen) == 0) return mTRUE;
darope@244
   432
darope@244
   433
	return mFALSE;
darope@244
   434
}
darope@244
   435
darope@244
   436
darope@0
   437
// Load a plugin; query, check allowed time, attach.
darope@0
   438
// meta_errno values:
darope@0
   439
//  - ME_ARGUMENT	missing necessary fields in plugin
darope@0
   440
//  - ME_ALREADY	this plugin already loaded
darope@0
   441
//  - ME_BADREQ		plugin not marked for load
darope@0
   442
//  - ME_DELAYED	load request is delayed (till changelevel?)
darope@0
   443
//  - ME_NOTALLOWED	plugin not loadable after startup
darope@0
   444
//  - errno's from query()
darope@0
   445
//  - errno's from attach()
darope@0
   446
//  - errno's from check_input()
darope@0
   447
mBOOL MPlugin::load(PLUG_LOADTIME now) {
darope@0
   448
	if(!check_input()) {
darope@0
   449
		// details logged, meta_errno set in check_input()
darope@0
   450
		RETURN_ERRNO(mFALSE, ME_ARGUMENT);
darope@0
   451
	}
darope@0
   452
	if(status >= PL_RUNNING) {
darope@0
   453
		META_ERROR("dll: Not loading plugin '%s'; already loaded (status=%s)", 
darope@0
   454
				desc, str_status());
darope@0
   455
		RETURN_ERRNO(mFALSE, ME_ALREADY);
darope@0
   456
	}
darope@0
   457
	if(action != PA_LOAD && action != PA_ATTACH) {
darope@0
   458
		META_ERROR("dll: Not loading plugin '%s'; not marked for load (action=%s)", desc, str_action());
darope@0
   459
		RETURN_ERRNO(mFALSE, ME_BADREQ);
darope@0
   460
	}
darope@0
   461
darope@0
   462
	if(status != PL_OPENED) {
darope@0
   463
		// query plugin; open file and get info about it
darope@0
   464
		if(!query()) {
darope@0
   465
			META_ERROR("dll: Skipping plugin '%s'; couldn't query", desc);
darope@0
   466
			if(meta_errno != ME_DLOPEN) {
darope@0
   467
				if(DLCLOSE(handle) != 0) {
darope@0
   468
					META_ERROR("dll: Couldn't close plugin file '%s': %s", 
darope@0
   469
							file, DLERROR());
darope@0
   470
				}
darope@0
   471
				else
darope@0
   472
					handle=NULL;
darope@0
   473
			}
darope@0
   474
			status=PL_BADFILE;
darope@0
   475
			// meta_errno should be already set in query()
darope@0
   476
			return(mFALSE);
darope@0
   477
		}
darope@0
   478
		status=PL_OPENED;
darope@0
   479
	}
darope@0
   480
darope@0
   481
	// are we allowed to attach this plugin at this time?
darope@0
   482
	if(info->loadable < now) {
darope@0
   483
		if(info->loadable > PT_STARTUP) {
darope@0
   484
			// will try to attach again at next opportunity
darope@0
   485
			META_DEBUG(2, ("dll: Delaying load plugin '%s'; can't attach now: allowed=%s; now=%s", 
darope@0
   486
						desc, str_loadable(), str_loadtime(now, SL_SIMPLE)));
darope@0
   487
			RETURN_ERRNO(mFALSE, ME_DELAYED);
darope@0
   488
		}
darope@0
   489
		else {
darope@0
   490
			META_DEBUG(2, ("dll: Failed load plugin '%s'; can't attach now: allowed=%s; now=%s", 
darope@0
   491
						desc, str_loadable(), str_loadtime(now, SL_SIMPLE)));
darope@0
   492
			// don't try to attach again later
darope@0
   493
			action=PA_NONE;
darope@0
   494
			RETURN_ERRNO(mFALSE, ME_NOTALLOWED);
darope@0
   495
		}
darope@0
   496
	}
darope@0
   497
darope@0
   498
	// attach plugin; get function tables
darope@0
   499
	if(attach(now) != mTRUE) {
darope@0
   500
		META_ERROR("dll: Failed to attach plugin '%s'", desc);
darope@0
   501
		// Note we don't dlclose() here, since we're returning PL_FAILED,
darope@0
   502
		// which implies that it's been dlopened and queried successfully.
darope@0
   503
		// Doing so causes crashes, because things like "meta list" try to
darope@0
   504
		// look at *info, which is in the DLL memory space and unaccessible
darope@0
   505
		// (segfault) after dlclosed.
darope@0
   506
		status=PL_FAILED;
darope@0
   507
		// meta_errno should be already set in attach()
darope@0
   508
		return(mFALSE);
darope@0
   509
	}
darope@0
   510
	status=PL_RUNNING;
darope@0
   511
	action=PA_NONE;
darope@0
   512
darope@0
   513
	// If not loading at server startup, then need to call plugin's
darope@0
   514
	// GameInit, since we've passed that.
darope@0
   515
	if(now != PT_STARTUP) {
darope@0
   516
		FN_GAMEINIT pfn_gameinit=NULL;
darope@0
   517
		if(dllapi_table && (pfn_gameinit=dllapi_table->pfnGameInit))
darope@0
   518
			pfn_gameinit();
darope@0
   519
	}
darope@0
   520
	// If loading during map, then I'd like to call plugin's
darope@0
   521
	// ServerActivate, since we've passed that.  However, I'm not sure what
darope@0
   522
	// arguments to give it...  So, we'll just have to say for the
darope@0
   523
	// moment that plugins that are loadable during a map can't need to
darope@0
   524
	// hook ServerActivate.
darope@0
   525
darope@0
   526
	META_LOG("dll: Loaded plugin '%s': %s v%s %s, %s", desc, info->name, 
darope@0
   527
			info->version, info->date, info->author);
darope@0
   528
	return(mTRUE);
darope@0
   529
}
darope@0
   530
darope@0
   531
// Query a plugin:
darope@0
   532
//  - dlopen() the file, store the handle
darope@0
   533
//  - dlsym() and call:
darope@0
   534
//	    Meta_Init (if present) - tell dll it'll be used as a metamod plugin
darope@0
   535
//	    GiveFnptrsToDll - give engine function ptrs 
darope@0
   536
//	    Meta_Query - say "hi" and get info about plugin
darope@0
   537
// meta_errno values:
darope@0
   538
//  - ME_DLOPEN		dlopen/loadlibrary failed; see dlerror() for details
darope@0
   539
//  - ME_DLMISSING	couldn't find a query() or giveFuncs() in plugin
darope@0
   540
//  - ME_DLERROR	plugin query() returned error
darope@0
   541
//  - ME_NULLDATA	info struct from query() was null
darope@0
   542
mBOOL MPlugin::query(void) {
darope@0
   543
	META_INIT_FN pfn_init;
darope@0
   544
	GIVE_ENGINE_FUNCTIONS_FN pfn_give_engfuncs;
darope@0
   545
	META_QUERY_FN pfn_query;
darope@0
   546
darope@0
   547
	// open the plugin DLL
darope@0
   548
	if(!(handle=DLOPEN(pathname))) {
darope@0
   549
		META_ERROR("dll: Failed query plugin '%s'; Couldn't open file '%s': %s",
darope@0
   550
				desc, pathname, DLERROR());
darope@0
   551
		RETURN_ERRNO(mFALSE, ME_DLOPEN);
darope@0
   552
	}
darope@0
   553
darope@0
   554
	// First, we check to see if they have a Meta_Query.  We would normally
darope@0
   555
	// dlsym this just prior to calling it, after having called
darope@0
   556
	// GiveFnptrsToDll, but things like standalone-DLL bots do all their
darope@0
   557
	// startup in GiveFnptrsToDll, and trying to load them as metamod
darope@0
   558
	// plugins creates all sorts of crashes.  So, we do a trivial check
darope@0
   559
	// first to see if the DLL looks like a metamod plugin before
darope@0
   560
	// proceeding with the normal order.  Note that we still have to call 
darope@0
   561
	// GiveFnptrsToDll before Meta_Query, because the latter typically uses 
darope@0
   562
	// engine functions like AlertMessage, which have to be passed along via 
darope@0
   563
	// GiveFnptrsToDll.
darope@0
   564
	pfn_query = (META_QUERY_FN) DLSYM(handle, "Meta_Query");
darope@0
   565
	if(!pfn_query) {
darope@0
   566
		META_ERROR("dll: Failed query plugin '%s'; Couldn't find Meta_Query(): %s", desc, DLERROR());
darope@0
   567
		// caller will dlclose()
darope@0
   568
		RETURN_ERRNO(mFALSE, ME_DLMISSING);
darope@0
   569
	}
darope@0
   570
darope@0
   571
	// Call Meta_Init, if present.  This is an optional plugin routine to
darope@0
   572
	// allow plugin to do any special initializing or other processing
darope@0
   573
	// prior to the standard query/attach procedure.  
darope@0
   574
	//
darope@0
   575
	// In particular, this should allow for DLL's that can operate as both
darope@0
   576
	// a standalone dll AND a metamod plugin.  This routine has to be
darope@0
   577
	// called _before_ GiveFnptrsToDll, since the dll will usually do all
darope@0
   578
	// its startup processing in GiveFn, and has to know at that point
darope@0
   579
	// whether it needs to do just mm plugin startup, or all the extra
darope@0
   580
	// startup needed for a standalone DLL.
darope@0
   581
	//
darope@0
   582
	// This passes nothing and returns nothing, and the routine in the
darope@0
   583
	// plugin can NOT use any Engine functions, as they haven't been
darope@0
   584
	// provided yet (done next, in GiveFnptrsToDll).
darope@0
   585
	pfn_init = (META_INIT_FN) DLSYM(handle, "Meta_Init");
darope@0
   586
	if(pfn_init) {
darope@0
   587
		pfn_init();
darope@0
   588
		META_DEBUG(6, ("dll: Plugin '%s': Called Meta_Init()", desc));
darope@0
   589
	}
darope@0
   590
	else {
darope@0
   591
		META_DEBUG(5, ("dll: no Meta_Init present in plugin '%s'", desc));
darope@0
   592
		// don't return; not an error
darope@0
   593
	}
darope@0
   594
	
darope@0
   595
	// pass on engine function table and globals to plugin
darope@0
   596
	if(!(pfn_give_engfuncs = (GIVE_ENGINE_FUNCTIONS_FN) DLSYM(handle, "GiveFnptrsToDll"))) {
darope@0
   597
		// META_ERROR("dll: Couldn't find GiveFnptrsToDll() in plugin '%s': %s", desc, DLERROR());
darope@0
   598
		META_ERROR("dll: Failed query plugin '%s'; Couldn't find GiveFnptrsToDll(): %s", desc, DLERROR());
darope@0
   599
		// caller will dlclose()
darope@0
   600
		RETURN_ERRNO(mFALSE, ME_DLMISSING);
darope@0
   601
	}
darope@0
   602
	pfn_give_engfuncs(Engine.pl_funcs, Engine.globals);
darope@0
   603
	META_DEBUG(6, ("dll: Plugin '%s': Called GiveFnptrsToDll()", desc));
darope@0
   604
darope@0
   605
	// Call plugin's Meta_Query(), to pass our meta interface version, and get
darope@0
   606
	// plugin's info structure.
darope@0
   607
	meta_errno=ME_NOERROR;
darope@0
   608
	info=NULL;
darope@0
   609
	// Make a copy of the meta_util function table for each plugin, for the
darope@0
   610
	// same reason.
darope@0
   611
	memcpy(&mutil_funcs, &MetaUtilFunctions, sizeof(mutil_funcs));
darope@0
   612
darope@0
   613
	if(pfn_query(META_INTERFACE_VERSION, &info, &mutil_funcs) != TRUE) {
darope@0
   614
		META_ERROR("dll: Failed query plugin '%s'; Meta_Query returned error", 
darope@0
   615
				desc);
darope@0
   616
		meta_errno=ME_DLERROR;
darope@0
   617
	}
darope@0
   618
	else {
darope@0
   619
		META_DEBUG(6, ("dll: Plugin '%s': Called Meta_Query() successfully", 
darope@0
   620
					desc));
darope@0
   621
	}
darope@0
   622
darope@0
   623
	// Check for interface differences...  Generally, a difference in major
darope@0
   624
	// version will be incompatible, and a plugin that expects a later
darope@0
   625
	// minor version will be incompatible (it's expecting things that this
darope@0
   626
	// Metamod won't be supplying).  Plugins that use an older minor
darope@0
   627
	// version will still work, as backward-compability within major
darope@0
   628
	// version is expected (forward-compatibility is not).
darope@0
   629
	//
darope@0
   630
	// Note, this check is done regardless of whether meta_query returns an
darope@0
   631
	// error.
darope@0
   632
	if(info && !FStrEq(info->ifvers, META_INTERFACE_VERSION)) {
darope@0
   633
		int mmajor=0, mminor=0, pmajor=0, pminor=0;
darope@0
   634
		META_DEBUG(3, ("dll: Note: Plugin '%s' interface version didn't match; expected %s, found %s", desc, META_INTERFACE_VERSION, info->ifvers));
darope@0
   635
		sscanf(META_INTERFACE_VERSION, "%d:%d", &mmajor, &mminor);
darope@0
   636
		sscanf(info->ifvers, "%d:%d", &pmajor, &pminor);
darope@0
   637
		// If plugin has later interface version, it's incompatible 
darope@0
   638
		// (update metamod).
darope@0
   639
		if(pmajor > mmajor || (pmajor==mmajor && pminor > mminor)) {
darope@0
   640
			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
   641
			meta_errno=ME_IFVERSION;
darope@0
   642
		}
darope@0
   643
		// If plugin has older major interface version, it's incompatible
darope@0
   644
		// (update plugin).
darope@0
   645
		else if(pmajor < mmajor) {
darope@0
   646
			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
   647
			meta_errno=ME_IFVERSION;
darope@0
   648
		}
darope@0
   649
		// Plugin has identical major, with older minor.  This is supposed to be
darope@0
   650
		// backwards compatible, so we warn, but we still accept it.
darope@0
   651
		else if(pmajor==mmajor && pminor < mminor)
darope@0
   652
			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
   653
		else
darope@0
   654
			META_LOG("dll: Plugin '%s': unexpected version comparision; metavers=%s, mmajor=%d, mminor=%d; plugvers=%s, pmajor=%d, pminor=%d", 
darope@0
   655
					desc, META_INTERFACE_VERSION, mmajor, mminor, info->ifvers, pmajor, pminor);
darope@0
   656
	}
darope@0
   657
darope@0
   658
	if(meta_errno == ME_IFVERSION) {
darope@0
   659
		META_ERROR("dll: Rejected plugin '%s' due to interface version incompatibility (mm=%s, pl=%s)", desc, META_INTERFACE_VERSION, info->ifvers);
darope@0
   660
		// meta_errno is set already above
darope@0
   661
		// caller will dlclose()
darope@0
   662
		return(mFALSE);
darope@0
   663
	}
darope@0
   664
	else if(meta_errno != ME_NOERROR)
darope@0
   665
		// some other error, already logged
darope@0
   666
		return(mFALSE);
darope@0
   667
darope@0
   668
	if(!info) {
darope@0
   669
		META_ERROR("dll: Failed query plugin '%s'; Empty info structure", desc);
darope@0
   670
		// caller will dlclose()
darope@0
   671
		RETURN_ERRNO(mFALSE, ME_NULLRESULT);
darope@0
   672
	}
darope@0
   673
	// Replace temporary desc with plugin's internal name.
darope@0
   674
	if(desc[0] == '<')
darope@0
   675
		STRNCPY(desc, info->name, sizeof(desc));
darope@0
   676
	META_DEBUG(6, ("dll: Plugin '%s': Query successful", desc));
darope@0
   677
	return(mTRUE);
darope@0
   678
}
darope@0
   679
darope@0
   680
// Attach a plugin:
darope@0
   681
//	- dlsym() and call:
darope@0
   682
//	    Meta_Attach - get table of api tables, give meta_globals
darope@0
   683
//  - if provided by plugin, call various "Get" function pointers, 
darope@0
   684
//    and store resulting function tables:
darope@0
   685
//		GetEntityAPI			(std)
darope@0
   686
//		GetEntityAPI2			(std sdk2)
darope@0
   687
//		GetNewDLLFunctions		(std sdk2)
darope@0
   688
//
darope@0
   689
//		GetEntityAPI_Post		(meta)
darope@0
   690
//		GetEntityAPI2_Post		(meta)
darope@0
   691
//		GetNewDLLFunctions_Post	(meta)
darope@0
   692
//
darope@0
   693
//		GetEngineFunctions		(meta)
darope@0
   694
//		GetEngineFunctions_Post	(meta)
darope@0
   695
// meta_errno values:
darope@0
   696
//  - ME_DLMISSING	couldn't find meta_attach() in plugin
darope@0
   697
//  - ME_DLERROR	plugin attach() returned error
darope@0
   698
//  - ME_NOMEM		failed malloc
darope@0
   699
mBOOL MPlugin::attach(PLUG_LOADTIME now) {
darope@0
   700
	int ret;
darope@0
   701
	int iface_vers;
darope@0
   702
darope@0
   703
	META_ATTACH_FN pfn_attach;
darope@0
   704
	META_FUNCTIONS meta_table;
darope@0
   705
darope@0
   706
	// Make copy of gameDLL's function tables for each plugin, so we don't
darope@0
   707
	// risk the plugins screwing with the tables everyone uses.
darope@0
   708
	if(GameDLL.funcs.dllapi_table && !gamedll_funcs.dllapi_table) {
darope@0
   709
		gamedll_funcs.dllapi_table = (DLL_FUNCTIONS *) malloc(sizeof(DLL_FUNCTIONS));
darope@0
   710
		if(!gamedll_funcs.dllapi_table) {
darope@0
   711
			META_ERROR("dll: Failed attach plugin '%s': Failed malloc() for dllapi_table");
darope@0
   712
			RETURN_ERRNO(mFALSE, ME_NOMEM);
darope@0
   713
		}
darope@0
   714
		memcpy(gamedll_funcs.dllapi_table, GameDLL.funcs.dllapi_table, 
darope@0
   715
				sizeof(DLL_FUNCTIONS));
darope@0
   716
	}
darope@0
   717
	if(GameDLL.funcs.newapi_table && !gamedll_funcs.newapi_table) {
darope@0
   718
		gamedll_funcs.newapi_table = (NEW_DLL_FUNCTIONS *) malloc(sizeof(NEW_DLL_FUNCTIONS));
darope@0
   719
		if(!gamedll_funcs.newapi_table) {
darope@0
   720
			META_ERROR("dll: Failed attach plugin '%s': Failed malloc() for newapi_table");
darope@0
   721
			RETURN_ERRNO(mFALSE, ME_NOMEM);
darope@0
   722
		}
darope@0
   723
		memcpy(gamedll_funcs.newapi_table, GameDLL.funcs.newapi_table, 
darope@0
   724
				sizeof(NEW_DLL_FUNCTIONS));
darope@0
   725
	}
darope@0
   726
	if(!(pfn_attach = (META_ATTACH_FN) DLSYM(handle, "Meta_Attach"))) {
darope@0
   727
		META_ERROR("dll: Failed attach plugin '%s': Couldn't find Meta_Attach(): %s", desc, DLERROR());
darope@0
   728
		// caller will dlclose()
darope@0
   729
		RETURN_ERRNO(mFALSE, ME_DLMISSING);
darope@0
   730
	}
darope@0
   731
darope@0
   732
	memset(&meta_table, 0, sizeof(meta_table));
darope@0
   733
	// get table of function tables,
darope@0
   734
	// give public meta globals
darope@0
   735
	ret=pfn_attach(now, &meta_table, &PublicMetaGlobals, &gamedll_funcs);
darope@0
   736
	if(ret != TRUE) {
darope@0
   737
		META_ERROR("dll: Failed attach plugin '%s': Error from Meta_Attach(): %d", desc, ret);
darope@0
   738
		// caller will dlclose()
darope@0
   739
		RETURN_ERRNO(mFALSE, ME_DLERROR);
darope@0
   740
	}
darope@0
   741
	META_DEBUG(6, ("dll: Plugin '%s': Called Meta_Attach() successfully", desc));
darope@0
   742
darope@0
   743
	// Rather than duplicate code, we use another ugly macro.  Again,
darope@0
   744
	// a function isn't an option since we have varying types.
darope@0
   745
#define GET_FUNC_TABLE_FROM_PLUGIN(pfnGetFuncs, STR_GetFuncs, struct_field, API_TYPE, TABLE_TYPE, vers_pass, vers_int, vers_want) \
darope@0
   746
	if(meta_table.pfnGetFuncs) { \
darope@236
   747
		struct_field = (TABLE_TYPE*) calloc(1, sizeof(TABLE_TYPE)); \
darope@0
   748
		if(meta_table.pfnGetFuncs(struct_field, vers_pass)) { \
darope@0
   749
			META_DEBUG(3, ("dll: Plugin '%s': Found %s", desc, STR_GetFuncs)); \
darope@0
   750
		} \
darope@0
   751
		else { \
darope@0
   752
			META_ERROR("dll: Failure calling %s in plugin '%s'", STR_GetFuncs, desc); \
darope@0
   753
			if(vers_int != vers_want) \
darope@0
   754
				META_ERROR("dll: Interface version didn't match; expected %d, found %d", vers_want, vers_int); \
darope@0
   755
		} \
darope@0
   756
	} \
darope@0
   757
	else { \
darope@0
   758
		META_DEBUG(5, ("dll: Plugin '%s': No %s", desc, STR_GetFuncs)); \
darope@0
   759
		struct_field=NULL; \
darope@0
   760
	}
darope@0
   761
darope@0
   762
	// Look for API-NEW interface in plugin.  We do this before API2/API, because
darope@0
   763
	// that's what the engine appears to do..
darope@0
   764
	// But we only do this if the gamedll provides these, so that we don't
darope@0
   765
	// give a plugin the idea that we'll call them, when in fact we won't
darope@0
   766
	// (since we don't export them to the engine when the gamedll doesn't
darope@0
   767
	// provide them).
darope@0
   768
	if(GameDLL.funcs.newapi_table) {
darope@0
   769
		iface_vers=NEW_DLL_FUNCTIONS_VERSION;
darope@0
   770
		GET_FUNC_TABLE_FROM_PLUGIN(pfnGetNewDLLFunctions, 
darope@0
   771
				"GetNewDLLFunctions", newapi_table, 
darope@0
   772
				NEW_DLL_FUNCTIONS_FN, NEW_DLL_FUNCTIONS,
darope@0
   773
				&iface_vers, iface_vers, NEW_DLL_FUNCTIONS_VERSION);
darope@0
   774
		iface_vers=NEW_DLL_FUNCTIONS_VERSION;
darope@0
   775
		GET_FUNC_TABLE_FROM_PLUGIN(pfnGetNewDLLFunctions_Post, 
darope@0
   776
				"GetNewDLLFunctions_Post", newapi_post_table, 
darope@0
   777
				NEW_DLL_FUNCTIONS_FN, NEW_DLL_FUNCTIONS,
darope@0
   778
				&iface_vers, iface_vers, NEW_DLL_FUNCTIONS_VERSION);
darope@0
   779
	}
darope@0
   780
darope@0
   781
	// Look for API2 interface in plugin; preferred over API-1.
darope@0
   782
	iface_vers=INTERFACE_VERSION;
darope@0
   783
	GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEntityAPI2, 
darope@0
   784
			"GetEntityAPI2", dllapi_table, 
darope@0
   785
			APIFUNCTION2, DLL_FUNCTIONS,
darope@0
   786
			&iface_vers, iface_vers, INTERFACE_VERSION);
darope@0
   787
	iface_vers=INTERFACE_VERSION;
darope@0
   788
	GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEntityAPI2_Post, 
darope@0
   789
			"GetEntityAPI2_Post", dllapi_post_table, 
darope@0
   790
			APIFUNCTION2, DLL_FUNCTIONS,
darope@0
   791
			&iface_vers, iface_vers, INTERFACE_VERSION);
darope@0
   792
darope@0
   793
	// Look for old-style API in plugin, if API2 interface wasn't found.
darope@0
   794
	if(!dllapi_table && !dllapi_post_table) {
darope@0
   795
		GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEntityAPI, 
darope@0
   796
				"GetEntityAPI", dllapi_table, 
darope@0
   797
				APIFUNCTION, DLL_FUNCTIONS,
darope@0
   798
				INTERFACE_VERSION, INTERFACE_VERSION, INTERFACE_VERSION);
darope@0
   799
		GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEntityAPI_Post, 
darope@0
   800
				"GetEntityAPI_Post", dllapi_post_table, 
darope@0
   801
				APIFUNCTION, DLL_FUNCTIONS,
darope@0
   802
				INTERFACE_VERSION, INTERFACE_VERSION, INTERFACE_VERSION);
darope@0
   803
	}
darope@0
   804
darope@0
   805
	// Look for Engine interface.
darope@0
   806
	iface_vers=ENGINE_INTERFACE_VERSION;
darope@0
   807
	GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEngineFunctions, 
darope@0
   808
			"GetEngineFunctions", engine_table, 
darope@0
   809
			GET_ENGINE_FUNCTIONS_FN, enginefuncs_t,
darope@0
   810
			&iface_vers, iface_vers, ENGINE_INTERFACE_VERSION);
darope@0
   811
	iface_vers=ENGINE_INTERFACE_VERSION;
darope@0
   812
	GET_FUNC_TABLE_FROM_PLUGIN(pfnGetEngineFunctions_Post, 
darope@0
   813
			"GetEngineFunctions_Post", engine_post_table, 
darope@0
   814
			GET_ENGINE_FUNCTIONS_FN, enginefuncs_t,
darope@0
   815
			&iface_vers, iface_vers, ENGINE_INTERFACE_VERSION);
darope@0
   816
darope@0
   817
	if(!dllapi_table && !dllapi_post_table 
darope@0
   818
		&& !newapi_table && !newapi_post_table
darope@0
   819
		&& !engine_table && !engine_post_table)
darope@0
   820
	{
darope@0
   821
		META_LOG("dll: Plugin '%s' isn't catching _any_ functions ??", desc);
darope@0
   822
	}
darope@0
   823
darope@0
   824
	time_loaded=time(NULL);
darope@0
   825
	return(mTRUE);
darope@0
   826
}
darope@0
   827
darope@0
   828
// Unload a plugin.  Check time, detach.
darope@0
   829
// meta_errno values:
darope@0
   830
//  - ME_ARGUMENT	missing necessary fields in plugin
darope@0
   831
//  - ME_ALREADY	this plugin already unloaded
darope@0
   832
//  - ME_BADREQ		plugin not marked for unload
darope@0
   833
//  - ME_DELAYED	unload request is delayed (till changelevel?)
darope@0
   834
//  - ME_NOTALLOWED	plugin not unloadable after startup
darope@0
   835
//  - errno's from check_input()
darope@0
   836
mBOOL MPlugin::unload(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) {
darope@0
   837
	if(!check_input()) {
darope@0
   838
		// details logged, meta_errno set in check_input()
darope@0
   839
		RETURN_ERRNO(mFALSE, ME_ARGUMENT);
darope@0
   840
	}
darope@0
   841
	if(status < PL_RUNNING) {
darope@0
   842
		META_ERROR("dll: Not unloading plugin '%s'; already unloaded (status=%s)", desc, str_status());
darope@0
   843
		RETURN_ERRNO(mFALSE, ME_ALREADY);
darope@0
   844
	}
darope@0
   845
	if(action != PA_UNLOAD && action != PA_RELOAD) {
darope@0
   846
		META_ERROR("dll: Not unloading plugin '%s'; not marked for unload (action=%s)", desc, str_action());
darope@0
   847
		RETURN_ERRNO(mFALSE, ME_BADREQ);
darope@0
   848
	}
darope@0
   849
darope@0
   850
	// Are we allowed to detach this plugin at this time?
darope@0
   851
	// If forcing unload, we disregard when plugin wants to be unloaded.
darope@0
   852
	if(info->unloadable < now) {
darope@0
   853
		if(reason == PNL_CMD_FORCED) {
darope@0
   854
			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
   855
		}
darope@0
   856
		else {
darope@0
   857
			if(info->unloadable > PT_STARTUP) {
darope@0
   858
				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
   859
				// caller should give message to user
darope@0
   860
				// try to unload again at next opportunity
darope@0
   861
				RETURN_ERRNO(mFALSE, ME_DELAYED);
darope@0
   862
			}
darope@0
   863
			else {
darope@0
   864
				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
   865
				// don't try to unload again later
darope@0
   866
				action=PA_NONE;
darope@0
   867
				RETURN_ERRNO(mFALSE, ME_NOTALLOWED);
darope@0
   868
			}
darope@0
   869
		}
darope@0
   870
	}
darope@0
   871
darope@0
   872
	// If unloading during map, then I'd like to call plugin's
darope@0
   873
	// ServerDeactivate.  However, I don't want to do this until I start
darope@0
   874
	// calling ServerActivate when loading during map, since the SDK
darope@0
   875
	// indicates these two routines should match call for call.
darope@0
   876
darope@0
   877
	// detach plugin
darope@0
   878
	if(!detach(now, reason)) {
darope@0
   879
		if(reason == PNL_CMD_FORCED) {
darope@0
   880
			META_DEBUG(2, ("dll: Forced unload plugin '%s' overriding failed detach"));
darope@0
   881
		}
darope@0
   882
		else {
darope@0
   883
			META_ERROR("dll: Failed to detach plugin '%s'; ", desc);
darope@0
   884
			// meta_errno should be already set in detach()
darope@0
   885
			return(mFALSE);
darope@0
   886
		}
darope@0
   887
	}
darope@0
   888
darope@0
   889
	// successful detach, or forced unload
darope@0
   890
darope@0
   891
	// Unmark registered commands for this plugin (by index number).
darope@0
   892
	RegCmds->disable(index);
darope@0
   893
	// Unmark registered cvars for this plugin (by index number).
darope@0
   894
	RegCvars->disable(index);
darope@0
   895
darope@0
   896
#ifdef UNFINISHED
darope@0
   897
	// Remove all requested hooks from this plugin (by index number).
darope@0
   898
	Hooks->remove_all(info);
darope@0
   899
#endif /* UNFINISHED */
darope@0
   900
darope@0
   901
	// Close the file.  Note: after this, attempts to reference any memory
darope@0
   902
	// locations in the file will produce a segfault.
darope@0
   903
	if(DLCLOSE(handle) != 0) {
darope@0
   904
		META_ERROR("dll: Couldn't close plugin file '%s': %s", file, DLERROR());
darope@0
   905
		status=PL_FAILED;
darope@0
   906
		RETURN_ERRNO(mFALSE, ME_DLERROR);
darope@0
   907
	}
darope@0
   908
	handle=NULL;
darope@0
   909
darope@0
   910
	if(action==PA_UNLOAD) {
darope@0
   911
		status=PL_EMPTY;
darope@0
   912
		clear();
darope@0
   913
	}
darope@0
   914
	else if(action==PA_RELOAD) {
darope@0
   915
		status=PL_VALID;
darope@0
   916
		action=PA_LOAD;
darope@0
   917
	}
darope@0
   918
	META_LOG("dll: Unloaded plugin '%s' for reason '%s'", desc, str_reason(reason));
darope@0
   919
	return(mTRUE);
darope@0
   920
}
darope@0
   921
darope@0
   922
// Inform plugin we're going to unload it.
darope@0
   923
// meta_errno values:
darope@0
   924
//  - ME_DLMISSING	couldn't find meta_detach() in plugin
darope@0
   925
//  - ME_DLERROR	plugin detach() returned error
darope@0
   926
mBOOL MPlugin::detach(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) {
darope@0
   927
	int ret;
darope@0
   928
	META_DETACH_FN pfn_detach;
darope@0
   929
darope@0
   930
	if(!(pfn_detach = (META_DETACH_FN) DLSYM(handle, "Meta_Detach"))) {
darope@0
   931
		META_ERROR("dll: Error detach plugin '%s': Couldn't find Meta_detach(): %s", desc, DLERROR());
darope@0
   932
		// caller will dlclose()
darope@0
   933
		RETURN_ERRNO(mFALSE, ME_DLMISSING);
darope@0
   934
	}
darope@0
   935
darope@0
   936
	ret=pfn_detach(now, reason);
darope@0
   937
	if(ret != TRUE) {
darope@0
   938
		META_ERROR("dll: Failed detach plugin '%s': Error from Meta_Detach(): %d", desc, ret);
darope@0
   939
		RETURN_ERRNO(mFALSE, ME_DLERROR);
darope@0
   940
	}
darope@0
   941
	META_DEBUG(6, ("dll: Plugin '%s': Called Meta_Detach() successfully", desc));
darope@0
   942
	return(mTRUE);
darope@0
   943
}
darope@0
   944
darope@0
   945
// Reload a plugin; unload and load again.
darope@0
   946
// meta_errno values:
darope@0
   947
//  - errno's from unload()
darope@0
   948
//  - errno's from load()
darope@0
   949
mBOOL MPlugin::reload(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) {
darope@0
   950
	if(!unload(now, reason)) {
darope@0
   951
		META_ERROR("dll: Failed to unload plugin '%s' for reloading", desc);
darope@0
   952
		// meta_errno should be set already in unload()
darope@0
   953
		return(mFALSE);
darope@0
   954
	}
darope@0
   955
	if(!load(now)) {
darope@0
   956
		META_ERROR("dll: Failed to reload plugin '%s' after unloading", desc);
darope@0
   957
		// meta_errno should be set already in load()
darope@0
   958
		return(mFALSE);
darope@0
   959
	}
darope@0
   960
	return(mTRUE);
darope@0
   961
}
darope@0
   962
darope@0
   963
// Pause a plugin; temporarily disabled for API routines.
darope@0
   964
// meta_errno values:
darope@0
   965
//  - ME_ALREADY	this plugin already paused
darope@0
   966
//  - ME_BADREQ		can't pause; not running
darope@0
   967
//  - ME_NOTALLOWED	plugin doesn't want to be paused
darope@0
   968
mBOOL MPlugin::pause(void) {
darope@0
   969
	if(status == PL_PAUSED) {
darope@0
   970
		META_ERROR("Not pausing plugin '%s'; already paused", desc);
darope@0
   971
		RETURN_ERRNO(mFALSE, ME_ALREADY);
darope@0
   972
	}
darope@0
   973
	if(status != PL_RUNNING) {
darope@0
   974
		META_ERROR("Cannot pause plugin '%s'; not currently running (status=%s)", desc, str_status());
darope@0
   975
		RETURN_ERRNO(mFALSE, ME_BADREQ);
darope@0
   976
	}
darope@0
   977
darope@0
   978
	// are we allowed to pause this plugin?
darope@0
   979
	if(info->unloadable < PT_ANYPAUSE) {
darope@0
   980
		META_ERROR("Cannot pause plugin '%s'; not allowed by plugin (allowed=%s)", desc, str_unloadable());
darope@0
   981
		action=PA_NONE;
darope@0
   982
		RETURN_ERRNO(mFALSE, ME_NOTALLOWED);
darope@0
   983
	}
darope@0
   984
darope@0
   985
	status=PL_PAUSED;
darope@0
   986
	META_LOG("Paused plugin '%s'", desc);
darope@0
   987
	return(mTRUE);
darope@0
   988
}
darope@0
   989
darope@0
   990
// Unpause a plugin.
darope@0
   991
// meta_errno values:
darope@0
   992
//  - ME_BADREQ		can't unpause; not paused
darope@0
   993
mBOOL MPlugin::unpause(void) {
darope@0
   994
	if(status != PL_PAUSED) {
darope@0
   995
		META_ERROR("Cannot unpause plugin '%s'; not currently paused (status=%s)", desc, str_status());
darope@0
   996
		RETURN_ERRNO(mFALSE, ME_BADREQ);
darope@0
   997
	}
darope@0
   998
	status=PL_RUNNING;
darope@0
   999
	META_LOG("Unpaused plugin '%s'", desc);
darope@0
  1000
	return(mTRUE);
darope@0
  1001
}
darope@0
  1002
darope@0
  1003
// Retry pending action, presumably from a previous failure.
darope@0
  1004
// meta_errno values:
darope@0
  1005
//  - ME_BADREQ		no pending action
darope@0
  1006
//  - errno's from load()
darope@0
  1007
//  - errno's from unload()
darope@0
  1008
//  - errno's from reload()
darope@0
  1009
mBOOL MPlugin::retry(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) {
darope@0
  1010
	if(action==PA_LOAD)
darope@0
  1011
		return(load(now));
darope@0
  1012
	else if(action==PA_ATTACH)
darope@0
  1013
		return(load(now));
darope@0
  1014
	else if(action==PA_UNLOAD)
darope@0
  1015
		return(unload(now, reason));
darope@0
  1016
	else if(action==PA_RELOAD)
darope@0
  1017
		return(reload(now, reason));
darope@0
  1018
	else {
darope@0
  1019
		META_ERROR("No pending action to retry for plugin '%s'; (status=%s, action=%s)", desc, str_status(), str_action());
darope@0
  1020
		RETURN_ERRNO(mFALSE, ME_BADREQ);
darope@0
  1021
	}
darope@0
  1022
}
darope@0
  1023
darope@0
  1024
// Clear a plugin (it failed a previous action and should be
darope@0
  1025
// removed from the list, or it's being unloaded).
darope@0
  1026
// meta_errno values:
darope@0
  1027
//  - ME_BADREQ		not marked for clearing
darope@0
  1028
//  - ME_DLERROR	failed to dlclose
darope@0
  1029
mBOOL MPlugin::clear(void) {
darope@0
  1030
	if(status != PL_FAILED && status != PL_BADFILE
darope@0
  1031
			&& status != PL_EMPTY && status != PL_OPENED) {
darope@0
  1032
		META_ERROR("Cannot clear plugin '%s'; not marked as failed, empty, or open (status=%s)", desc, str_status());
darope@0
  1033
		RETURN_ERRNO(mFALSE, ME_BADREQ);
darope@0
  1034
	}
darope@0
  1035
	// If file is open, close the file.  Note: after this, attempts to
darope@0
  1036
	// reference any memory locations in the file will produce a segfault.
darope@0
  1037
	if(handle && DLCLOSE(handle) != 0) {
darope@0
  1038
		META_ERROR("dll: Couldn't close plugin file '%s': %s", file, DLERROR());
darope@0
  1039
		status=PL_FAILED;
darope@0
  1040
		RETURN_ERRNO(mFALSE, ME_DLERROR);
darope@0
  1041
	}
darope@0
  1042
	handle=NULL;
darope@0
  1043
darope@0
  1044
	if(gamedll_funcs.dllapi_table) free(gamedll_funcs.dllapi_table);
darope@0
  1045
	if(gamedll_funcs.newapi_table) free(gamedll_funcs.newapi_table);
darope@0
  1046
darope@0
  1047
	status=PL_EMPTY;
darope@0
  1048
	action=PA_NULL;
darope@0
  1049
	handle=NULL;
darope@0
  1050
	info=NULL;
darope@0
  1051
	time_loaded=0;
darope@0
  1052
	dllapi_table=NULL;
darope@0
  1053
	dllapi_post_table=NULL;
darope@0
  1054
	newapi_table=NULL;
darope@0
  1055
	newapi_post_table=NULL;
darope@0
  1056
	engine_table=NULL;
darope@0
  1057
	engine_post_table=NULL;
darope@0
  1058
	return(mTRUE);
darope@0
  1059
}
darope@0
  1060
darope@0
  1061
// List information about plugin to console.
darope@0
  1062
void MPlugin::show(void) {
darope@0
  1063
	char *cp, *tstr;
darope@0
  1064
	int n, width;
darope@0
  1065
	width=13;
darope@0
  1066
	META_CONS("%*s: %s", width, "name", info ? info->name : "(nil)");
darope@0
  1067
	META_CONS("%*s: %s", width, "desc", desc);
darope@0
  1068
	META_CONS("%*s: %s", width, "status", str_status());
darope@0
  1069
	META_CONS("%*s: %s", width, "action", str_action());
darope@0
  1070
	META_CONS("%*s: %s", width, "filename", filename);
darope@0
  1071
	META_CONS("%*s: %s", width, "file", file);
darope@0
  1072
	META_CONS("%*s: %s", width, "pathname", pathname);
darope@0
  1073
	META_CONS("%*s: %d", width, "index", index);
darope@0
  1074
	META_CONS("%*s: %s", width, "source", str_source());
darope@0
  1075
	META_CONS("%*s: %s", width, "loadable", str_loadable(SL_ALLOWED));
darope@0
  1076
	META_CONS("%*s: %s", width, "unloadable", str_unloadable(SL_ALLOWED));
darope@0
  1077
	META_CONS("%*s: %s", width, "version", info ? info->version : "(nil)");
darope@0
  1078
	META_CONS("%*s: %s", width, "date", info ? info->date : "(nil)");
darope@0
  1079
	META_CONS("%*s: %s", width, "author", info ? info->author : "(nil)");
darope@0
  1080
	META_CONS("%*s: %s", width, "url", info ? info->url : "(nil)");
darope@0
  1081
	META_CONS("%*s: %s", width, "logtag", info ? info->logtag : "(nil)");
darope@0
  1082
	// ctime() includes newline at EOL
darope@0
  1083
	tstr=ctime(&time_loaded);
darope@0
  1084
	if((cp=strchr(tstr, '\n')))
darope@0
  1085
		*cp='\0';
darope@0
  1086
	META_CONS("%*s: %s", width, "last loaded", tstr);
darope@0
  1087
	// XXX show file time ?
darope@0
  1088
darope@0
  1089
	if(dllapi_table) {
darope@0
  1090
		META_CONS("DLLAPI functions:");
darope@0
  1091
		SHOW_DEF_DLLAPI(dllapi_table, "   ", "");
darope@0
  1092
		META_CONS("%d functions (dllapi)", n);
darope@0
  1093
	}
darope@0
  1094
	else
darope@0
  1095
		META_CONS("No DLLAPI functions.");
darope@0
  1096
	if(dllapi_post_table) {
darope@0
  1097
		META_CONS("DLLAPI-Post functions:");
darope@0
  1098
		SHOW_DEF_DLLAPI(dllapi_post_table, "   ", "_Post");
darope@0
  1099
		META_CONS("%d functions (dllapi post)", n);
darope@0
  1100
	}
darope@0
  1101
	else
darope@0
  1102
		META_CONS("No DLLAPI-Post functions.");
darope@0
  1103
darope@0
  1104
	if(newapi_table) {
darope@0
  1105
		META_CONS("NEWAPI functions:");
darope@0
  1106
		SHOW_DEF_NEWAPI(newapi_table, "   ", "");
darope@0
  1107
		META_CONS("%d functions (newapi)", n);
darope@0
  1108
	}
darope@0
  1109
	else
darope@0
  1110
		META_CONS("No NEWAPI functions.");
darope@0
  1111
	if(newapi_post_table) {
darope@0
  1112
		META_CONS("NEWAPI-Post functions:");
darope@0
  1113
		SHOW_DEF_NEWAPI(newapi_post_table, "   ", "_Post");
darope@0
  1114
		META_CONS("%d functions (newapi post)", n);
darope@0
  1115
	}
darope@0
  1116
	else
darope@0
  1117
		META_CONS("No NEWAPI-Post functions.");
darope@0
  1118
darope@0
  1119
	if(engine_table) {
darope@0
  1120
		META_CONS("Engine functions:");
darope@0
  1121
		SHOW_DEF_ENGINE(engine_table, "   ", "");
darope@0
  1122
		META_CONS("%d functions (engine)", n);
darope@0
  1123
	}
darope@0
  1124
	else
darope@0
  1125
		META_CONS("No Engine functions.");
darope@0
  1126
	if(engine_post_table) {
darope@0
  1127
		META_CONS("Engine-Post functions:");
darope@0
  1128
		SHOW_DEF_ENGINE(engine_post_table, "   ", "_Post");
darope@0
  1129
		META_CONS("%d functions (engine post)", n);
darope@0
  1130
	}
darope@0
  1131
	else
darope@0
  1132
		META_CONS("No Engine-Post functions.");
darope@0
  1133
	RegCmds->show(index);
darope@0
  1134
	RegCvars->show(index);
darope@0
  1135
}
darope@0
  1136
darope@0
  1137
// Check whether the file on disk looks like it's been updated since we
darope@0
  1138
// last loaded the plugin.
darope@0
  1139
// meta_errno values:
darope@0
  1140
//  - ME_NOFILE		couldn't find file
darope@0
  1141
//  - ME_NOERROR	no error; false indicates file not newer
darope@0
  1142
mBOOL MPlugin::newer_file(void) {
darope@0
  1143
	struct stat st;
darope@0
  1144
	time_t file_time;
darope@0
  1145
darope@0
  1146
	if(stat(pathname, &st) != 0)
darope@0
  1147
		RETURN_ERRNO(mFALSE, ME_NOFILE);
darope@0
  1148
	file_time=max(st.st_ctime, st.st_mtime);
darope@0
  1149
	META_DEBUG(5, ("newer_file? file=%s; load=%d, file=%d; ctime=%d, mtime=%d",
darope@0
  1150
				file, time_loaded, file_time, st.st_ctime, st.st_mtime));
darope@0
  1151
	if(file_time > time_loaded)
darope@0
  1152
		return(mTRUE);
darope@0
  1153
	else
darope@0
  1154
		RETURN_ERRNO(mFALSE, ME_NOERROR);
darope@0
  1155
}
darope@0
  1156
darope@0
  1157
// Return a string describing status of plugin.
darope@0
  1158
// SIMPLE is the default.
darope@0
  1159
// SHOW is max 4 chars, for "show" output.
darope@0
  1160
// meta_errno values:
darope@0
  1161
//  - none
darope@0
  1162
char *MPlugin::str_status(STR_STATUS fmt) {
darope@0
  1163
	switch(status) {
darope@0
  1164
		case PL_EMPTY:
darope@0
  1165
			if(fmt==ST_SHOW) return("empt");
darope@0
  1166
			else return("empty");
darope@0
  1167
		case PL_VALID:
darope@0
  1168
			if(fmt==ST_SHOW) return("info");
darope@0
  1169
			else return("valid");
darope@0
  1170
		case PL_BADFILE:
darope@0
  1171
			if(fmt==ST_SHOW) return("badf");
darope@0
  1172
			else return("badfile");
darope@0
  1173
		case PL_OPENED:
darope@0
  1174
			if(fmt==ST_SHOW) return("open");
darope@0
  1175
			else return("opened");
darope@0
  1176
		case PL_FAILED:
darope@0
  1177
			if(fmt==ST_SHOW) return("fail");
darope@0
  1178
			else return("failed");
darope@0
  1179
		case PL_RUNNING:
darope@0
  1180
			if(fmt==ST_SHOW) return("RUN");
darope@0
  1181
			else return("running");
darope@0
  1182
		case PL_PAUSED:
darope@0
  1183
			if(fmt==ST_SHOW) return("PAUS");
darope@0
  1184
			else return("paused");
darope@0
  1185
		default:
darope@0
  1186
			if(fmt==ST_SHOW) return(UTIL_VarArgs("UNK%d", status));
darope@0
  1187
			return(UTIL_VarArgs("unknown (%d)", status));
darope@0
  1188
	}
darope@0
  1189
}
darope@0
  1190
darope@0
  1191
// Return a string (one word) describing requested action for plugin.
darope@0
  1192
// SIMPLE is the default.
darope@0
  1193
// SHOW is max 4 chars, for "show" output.
darope@0
  1194
// meta_errno values:
darope@0
  1195
//  - none
darope@0
  1196
char *MPlugin::str_action(STR_ACTION fmt) {
darope@0
  1197
	switch(action) {
darope@0
  1198
		case PA_NULL:
darope@0
  1199
			if(fmt==SA_SHOW) return("NULL");
darope@0
  1200
			else return("null");
darope@0
  1201
		case PA_NONE:
darope@0
  1202
			if(fmt==SA_SHOW) return(" -  ");
darope@0
  1203
			else return("none");
darope@0
  1204
		case PA_KEEP:
darope@0
  1205
			if(fmt==SA_SHOW) return("keep");
darope@0
  1206
			else return("keep");
darope@0
  1207
		case PA_LOAD:
darope@0
  1208
			if(fmt==SA_SHOW) return("load");
darope@0
  1209
			else return("load");
darope@0
  1210
		case PA_ATTACH:
darope@0
  1211
			if(fmt==SA_SHOW) return("atch");
darope@0
  1212
			else return("attach");
darope@0
  1213
		case PA_UNLOAD:
darope@0
  1214
			if(fmt==SA_SHOW) return("unld");
darope@0
  1215
			else return("unload");
darope@0
  1216
		case PA_RELOAD:
darope@0
  1217
			if(fmt==SA_SHOW) return("relo");
darope@0
  1218
			else return("reload");
darope@0
  1219
		default:
darope@0
  1220
			if(fmt==SA_SHOW) return(UTIL_VarArgs("UNK%d", action));
darope@0
  1221
			else return(UTIL_VarArgs("unknown (%d)", action));
darope@0
  1222
	}
darope@0
  1223
}
darope@0
  1224
darope@0
  1225
// Return a string describing given plugin loadtime.
darope@0
  1226
// SIMPLE is the default.
darope@0
  1227
// SHOW is max 3 chars, for "show" output.
darope@0
  1228
// ALLOWED is in terms of when it's allowed to load/unload.
darope@0
  1229
// NOW is to describe current situation of load/unload attempt.
darope@0
  1230
// meta_errno values:
darope@0
  1231
//  - none
darope@0
  1232
char *MPlugin::str_loadtime(PLUG_LOADTIME ptime, STR_LOADTIME fmt) {
darope@0
  1233
	switch(ptime) {
darope@0
  1234
		case PT_NEVER:
darope@0
  1235
			if(fmt==SL_SHOW) return("Never");
darope@0
  1236
			else return("never");
darope@0
  1237
		case PT_STARTUP:
darope@0
  1238
			if(fmt==SL_SHOW) return("Start");
darope@0
  1239
			else if(fmt==SL_ALLOWED) return("at server startup");
darope@0
  1240
			else if(fmt==SL_NOW) return("during server startup");
darope@0
  1241
			else return("startup");
darope@0
  1242
		case PT_CHANGELEVEL:
darope@0
  1243
			if(fmt==SL_SHOW) return("Chlvl");
darope@0
  1244
			else if(fmt==SL_ALLOWED) return("at changelevel");
darope@0
  1245
			else if(fmt==SL_NOW) return("during changelevel");
darope@0
  1246
			else return("changelevel");
darope@0
  1247
		case PT_ANYTIME:
darope@0
  1248
			if(fmt==SL_SHOW) return("ANY");
darope@0
  1249
			else if(fmt==SL_ALLOWED) return("at any time");
darope@0
  1250
			else if(fmt==SL_NOW) return("during map");
darope@0
  1251
			else return("anytime");
darope@0
  1252
		case PT_ANYPAUSE:
darope@0
  1253
			if(fmt==SL_SHOW) return("Pause");
darope@0
  1254
			else if(fmt==SL_ALLOWED) return("at any time, and pausable");
darope@0
  1255
			else if(fmt==SL_NOW) return("for requested pause");
darope@0
  1256
			else return("pausable");
darope@0
  1257
		default:
darope@0
  1258
			if(fmt==SL_SHOW) return(UTIL_VarArgs("UNK-%d", ptime));
darope@0
  1259
			else return(UTIL_VarArgs("unknown (%d)", ptime));
darope@0
  1260
	}
darope@0
  1261
}
darope@0
  1262
darope@0
  1263
// Return a string describing why a plugin is to be unloaded.
darope@0
  1264
// meta_errno values:
darope@0
  1265
//  - none
darope@0
  1266
char *MPlugin::str_reason(PL_UNLOAD_REASON preason) {
darope@0
  1267
	switch(preason) {
darope@0
  1268
		case PNL_NULL:
darope@0
  1269
			return("null");
darope@0
  1270
		case PNL_INI_DELETED:
darope@0
  1271
			return("deleted from ini file");
darope@0
  1272
		case PNL_FILE_NEWER:
darope@0
  1273
			return("file on disk is newer");
darope@0
  1274
		case PNL_COMMAND:
darope@0
  1275
			return("server command");
darope@0
  1276
		case PNL_CMD_FORCED:
darope@0
  1277
			return("forced by server command");
darope@0
  1278
		default:
darope@0
  1279
			return(UTIL_VarArgs("unknown (%d)", preason));
darope@0
  1280
	}
darope@0
  1281
}
darope@0
  1282
darope@0
  1283
// Return a string describing how the plugin was loaded.
darope@0
  1284
// meta_errno values:
darope@0
  1285
//  - none
darope@0
  1286
char *MPlugin::str_source(STR_SOURCE fmt) {
darope@0
  1287
	switch(source) {
darope@0
  1288
		case PS_INI:
darope@0
  1289
			if(fmt==SO_SHOW) return("ini");
darope@0
  1290
			else return("ini file");
darope@0
  1291
		case PS_CMD:
darope@0
  1292
			if(fmt==SO_SHOW) return("cmd");
darope@0
  1293
			else return("console command");
darope@0
  1294
		default:
darope@0
  1295
			if(fmt==SO_SHOW) return(UTIL_VarArgs("UNK%d", source));
darope@0
  1296
			else return(UTIL_VarArgs("unknown (%d)", source));
darope@0
  1297
	}
darope@0
  1298
}