metamod/mutil.cpp
author darope
Mon Apr 17 17:04:06 2006 +0000 (2006-04-17)
changeset 250 1f5fb7d1d9b3
parent 248 f72386298cba
permissions -rw-r--r--
Merged working branch to maintrunk for release 1.19
darope@0
     1
// vi: set ts=4 sw=4 :
darope@0
     2
// vim: set tw=75 :
darope@0
     3
darope@0
     4
// mutil.cpp - utility functions to provide to plugins
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 <stdio.h>			// vsnprintf(), etc
darope@0
    38
#include <stdarg.h>			// vs_start(), etc
darope@248
    39
#include <stdlib.h>			// strtol()
darope@0
    40
darope@0
    41
#include <extdll.h>			// always
darope@0
    42
darope@0
    43
#include "mutil.h"			// me
darope@0
    44
#include "mhook.h"			// class MHookList, etc
darope@0
    45
#include "linkent.h"		// ENTITY_FN, etc
darope@0
    46
#include "metamod.h"		// Hooks, etc
darope@0
    47
#include "meta_api.h"		// 
darope@0
    48
#include "types_meta.h"		// mBOOL
darope@0
    49
#include "osdep.h"			// win32 vsnprintf, etc
darope@0
    50
#include "sdk_util.h"		// ALERT, etc
darope@0
    51
darope@0
    52
hudtextparms_t default_csay_tparms = {
darope@0
    53
	-1, 0.25,			// x, y
darope@0
    54
	2,					// effect
darope@0
    55
	0, 255, 0,	0,		// r, g, b,  a1
darope@0
    56
	0, 0, 0,	0,		// r2, g2, b2,  a2
darope@0
    57
	0, 0, 10, 10,		// fadein, fadeout, hold, fxtime
darope@0
    58
	1					// channel
darope@0
    59
};
darope@0
    60
darope@0
    61
// Log to console; newline added.
darope@0
    62
void mutil_LogConsole(plid_t /* plid */, const char *fmt, ...) {
darope@0
    63
	va_list ap;
darope@0
    64
	char buf[MAX_LOGMSG_LEN];
darope@0
    65
	unsigned int len;
darope@0
    66
darope@0
    67
	va_start(ap, fmt);
darope@0
    68
	vsnprintf(buf, sizeof(buf), fmt, ap);
darope@0
    69
	va_end(ap);
darope@0
    70
	// end msg with newline
darope@0
    71
	len=strlen(buf);
darope@0
    72
	if(len < sizeof(buf)-2)		// -1 null, -1 for newline
darope@0
    73
		strcat(buf, "\n");
darope@0
    74
	else
darope@0
    75
		buf[len-1] = '\n';
darope@0
    76
darope@0
    77
	SERVER_PRINT(buf);
darope@0
    78
}
darope@0
    79
darope@0
    80
// Log regular message to logs; newline added.
darope@0
    81
void mutil_LogMessage(plid_t plid, const char *fmt, ...) {
darope@0
    82
	va_list ap;
darope@0
    83
	char buf[MAX_LOGMSG_LEN];
darope@0
    84
	plugin_info_t *plinfo;
darope@0
    85
darope@0
    86
	plinfo=(plugin_info_t *)plid;
darope@0
    87
	va_start(ap, fmt);
darope@0
    88
	vsnprintf(buf, sizeof(buf), fmt, ap);
darope@0
    89
	va_end(ap);
darope@0
    90
	ALERT(at_logged, "[%s] %s\n", plinfo->logtag, buf);
darope@0
    91
}
darope@0
    92
darope@0
    93
// Log an error message to logs; newline added.
darope@0
    94
void mutil_LogError(plid_t plid, const char *fmt, ...) {
darope@0
    95
	va_list ap;
darope@0
    96
	char buf[MAX_LOGMSG_LEN];
darope@0
    97
	plugin_info_t *plinfo;
darope@0
    98
darope@0
    99
	plinfo=(plugin_info_t *)plid;
darope@0
   100
	va_start(ap, fmt);
darope@0
   101
	vsnprintf(buf, sizeof(buf), fmt, ap);
darope@0
   102
	va_end(ap);
darope@0
   103
	ALERT(at_logged, "[%s] ERROR: %s\n", plinfo->logtag, buf);
darope@0
   104
}
darope@0
   105
darope@0
   106
// Log a message only if cvar "developer" set; newline added.
darope@0
   107
void mutil_LogDeveloper(plid_t plid, const char *fmt, ...) {
darope@0
   108
	va_list ap;
darope@0
   109
	char buf[MAX_LOGMSG_LEN];
darope@0
   110
	plugin_info_t *plinfo;
darope@0
   111
darope@0
   112
	if((int) CVAR_GET_FLOAT("developer") == 0)
darope@0
   113
		return;
darope@0
   114
darope@0
   115
	plinfo=(plugin_info_t *)plid;
darope@0
   116
	va_start(ap, fmt);
darope@0
   117
	vsnprintf(buf, sizeof(buf), fmt, ap);
darope@0
   118
	va_end(ap);
darope@0
   119
	ALERT(at_logged, "[%s] dev: %s\n", plinfo->logtag, buf);
darope@0
   120
}
darope@0
   121
darope@0
   122
// Print message on center of all player's screens.  Uses default text
darope@0
   123
// parameters (color green, 10 second fade-in).
darope@0
   124
void mutil_CenterSay(plid_t plid, const char *fmt, ...) {
darope@0
   125
	va_list ap;
darope@0
   126
	va_start(ap, fmt);
darope@0
   127
	mutil_CenterSayVarargs(plid, default_csay_tparms, fmt, ap);
darope@0
   128
	va_end(ap);
darope@0
   129
}
darope@0
   130
darope@0
   131
// Print a center-message, with given text parameters.
darope@0
   132
void mutil_CenterSayParms(plid_t plid, hudtextparms_t tparms, const char *fmt, ...) {
darope@0
   133
	va_list ap;
darope@0
   134
	va_start(ap, fmt);
darope@0
   135
	mutil_CenterSayVarargs(plid, tparms, fmt, ap);
darope@0
   136
	va_end(ap);
darope@0
   137
}
darope@0
   138
darope@0
   139
// Print a center-message, with text parameters and varargs.  Provides
darope@0
   140
// functionality to the above center_say interfaces.
darope@0
   141
void mutil_CenterSayVarargs(plid_t plid, hudtextparms_t tparms, 
darope@0
   142
		const char *fmt, va_list ap) 
darope@0
   143
{
darope@0
   144
	char buf[MAX_LOGMSG_LEN];
darope@0
   145
	int n;
darope@0
   146
	edict_t *pEntity;
darope@0
   147
darope@0
   148
	vsnprintf(buf, sizeof(buf), fmt, ap);
darope@0
   149
darope@0
   150
	mutil_LogMessage(plid, "(centersay) %s", buf);
darope@0
   151
	for(n=1; n <= gpGlobals->maxClients; n++) {
darope@0
   152
		pEntity=INDEXENT(n);
darope@0
   153
		if(FNullEnt(pEntity) || pEntity->free)
darope@0
   154
			continue;
darope@236
   155
		META_UTIL_HudMessage(pEntity, tparms, buf);
darope@0
   156
	}
darope@0
   157
}
darope@0
   158
darope@0
   159
// Allow plugins to call the entity functions in the GameDLL.  In
darope@0
   160
// particular, calling "player()" as needed by most Bots.  Suggested by
darope@0
   161
// Jussi Kivilinna.
darope@0
   162
qboolean mutil_CallGameEntity(plid_t plid, const char *entStr, entvars_t *pev) {
darope@0
   163
	plugin_info_t *plinfo;
darope@0
   164
	ENTITY_FN pfnEntity;
darope@0
   165
darope@0
   166
	plinfo=(plugin_info_t *)plid;
darope@0
   167
	META_DEBUG(8, ("Looking up game entity '%s' for plugin '%s'", entStr,
darope@0
   168
				plinfo->name));
darope@0
   169
	pfnEntity = (ENTITY_FN) DLSYM(GameDLL.handle, entStr);
darope@0
   170
	if(!pfnEntity) {
darope@0
   171
		META_ERROR("Couldn't find game entity '%s' in game DLL '%s' for plugin '%s'", entStr, GameDLL.name, plinfo->name);
darope@0
   172
		return(false);
darope@0
   173
	}
darope@0
   174
	META_DEBUG(7, ("Calling game entity '%s' for plugin '%s'", entStr,
darope@0
   175
				plinfo->name));
darope@0
   176
	(*pfnEntity)(pev);
darope@0
   177
	return(true);
darope@0
   178
}
darope@0
   179
darope@0
   180
// Find a usermsg, registered by the gamedll, with the corresponding
darope@0
   181
// msgname, and return remaining info about it (msgid, size).
darope@0
   182
int mutil_GetUserMsgID(plid_t plid, const char *msgname, int *size) {
darope@0
   183
	plugin_info_t *plinfo;
darope@0
   184
	MRegMsg *umsg;
darope@0
   185
darope@0
   186
	plinfo=(plugin_info_t *)plid;
darope@0
   187
	META_DEBUG(8, ("Looking up usermsg name '%s' for plugin '%s'", msgname,
darope@0
   188
				plinfo->name));
darope@0
   189
	umsg=RegMsgs->find(msgname);
darope@0
   190
	if(umsg) {
darope@0
   191
		if(size)
darope@0
   192
			*size=umsg->size;
darope@0
   193
		return(umsg->msgid);
darope@0
   194
	}
darope@0
   195
	else
darope@0
   196
		return(0);
darope@0
   197
}
darope@0
   198
darope@0
   199
// Find a usermsg, registered by the gamedll, with the corresponding
darope@0
   200
// msgid, and return remaining info about it (msgname, size).
darope@0
   201
const char *mutil_GetUserMsgName(plid_t plid, int msgid, int *size) {
darope@0
   202
	plugin_info_t *plinfo;
darope@0
   203
	MRegMsg *umsg;
darope@0
   204
darope@0
   205
	plinfo=(plugin_info_t *)plid;
darope@0
   206
	META_DEBUG(8, ("Looking up usermsg id '%d' for plugin '%s'", msgid,
darope@0
   207
				plinfo->name));
darope@0
   208
	// Guess names for any built-in Engine messages mentioned in the SDK;
darope@0
   209
	// from dlls/util.h.
darope@0
   210
	if(msgid < 64) {
darope@0
   211
		switch(msgid) {
darope@0
   212
			case SVC_TEMPENTITY:
darope@0
   213
				if(size) *size=-1;
darope@0
   214
				return("tempentity?");
darope@0
   215
			case SVC_INTERMISSION:
darope@0
   216
				if(size) *size=-1;
darope@0
   217
				return("intermission?");
darope@0
   218
			case SVC_CDTRACK:
darope@0
   219
				if(size) *size=-1;
darope@0
   220
				return("cdtrack?");
darope@0
   221
			case SVC_WEAPONANIM:
darope@0
   222
				if(size) *size=-1;
darope@0
   223
				return("weaponanim?");
darope@0
   224
			case SVC_ROOMTYPE:
darope@0
   225
				if(size) *size=-1;
darope@0
   226
				return("roomtype?");
darope@0
   227
			case SVC_DIRECTOR:
darope@0
   228
				if(size) *size=-1;
darope@0
   229
				return("director?");
darope@0
   230
		}
darope@0
   231
	}
darope@0
   232
	umsg=RegMsgs->find(msgid);
darope@0
   233
	if(umsg) {
darope@0
   234
		if(size)
darope@0
   235
			*size=umsg->size;
darope@0
   236
		// 'name' is assumed to be a constant string, allocated in the
darope@0
   237
		// gamedll.
darope@0
   238
		return(umsg->name);
darope@0
   239
	}
darope@0
   240
	else
darope@0
   241
		return(NULL);
darope@0
   242
}
darope@0
   243
darope@0
   244
// Return the full path of the plugin's loaded dll/so file.
darope@0
   245
const char *mutil_GetPluginPath(plid_t plid) {
darope@0
   246
	static char buf[PATH_MAX];
darope@0
   247
	MPlugin *plug;
darope@0
   248
darope@0
   249
	plug=Plugins->find(plid);
darope@0
   250
	if(!plug) {
darope@0
   251
		META_ERROR("GetPluginPath: couldn't find plugin '%s'",
darope@0
   252
				plid->name);
darope@0
   253
		return(NULL);
darope@0
   254
	}
darope@0
   255
	STRNCPY(buf, plug->pathname, sizeof(buf));
darope@0
   256
	return(buf);
darope@0
   257
}
darope@0
   258
darope@0
   259
// Return various string-based info about the game/MOD/gamedll.
darope@0
   260
const char *mutil_GetGameInfo(plid_t plid, ginfo_t type) {
darope@0
   261
	static char buf[MAX_STRBUF_LEN];
darope@0
   262
	const char *cp;
darope@0
   263
	switch(type) {
darope@0
   264
		case GINFO_NAME:
darope@0
   265
			cp=GameDLL.name;
darope@0
   266
			break;
darope@0
   267
		case GINFO_DESC:
darope@0
   268
			cp=GameDLL.desc;
darope@0
   269
			break;
darope@0
   270
		case GINFO_GAMEDIR:
darope@0
   271
			cp=GameDLL.gamedir;
darope@0
   272
			break;
darope@0
   273
		case GINFO_DLL_FULLPATH:
darope@0
   274
			cp=GameDLL.pathname;
darope@0
   275
			break;
darope@0
   276
		case GINFO_DLL_FILENAME:
darope@0
   277
			cp=GameDLL.file;
darope@0
   278
			break;
darope@0
   279
		case GINFO_REALDLL_FULLPATH:
darope@0
   280
			cp=GameDLL.real_pathname;
darope@0
   281
			break;
darope@0
   282
		default:
darope@0
   283
			META_ERROR("GetGameInfo: invalid request '%d' from plugin '%s'",
darope@0
   284
					type, plid->name);
darope@0
   285
			return(NULL);
darope@0
   286
	}
darope@0
   287
	STRNCPY(buf, cp, sizeof(buf));
darope@0
   288
	return(buf);
darope@0
   289
}
darope@0
   290
darope@248
   291
int mutil_LoadMetaPlugin(plid_t plid, const char *fname, PLUG_LOADTIME now, void **plugin_handle)
darope@248
   292
{
darope@248
   293
	MPlugin *pl_loaded;
darope@248
   294
	
darope@248
   295
	if (NULL == fname)
darope@248
   296
	{
darope@248
   297
		return ME_ARGUMENT;
darope@248
   298
	}
darope@248
   299
darope@248
   300
	meta_errno = ME_NOERROR;
darope@248
   301
	if (! (pl_loaded=Plugins->plugin_addload(plid, fname, now)) )
darope@248
   302
	{
darope@248
   303
		if (plugin_handle)
darope@248
   304
			*plugin_handle = NULL;
darope@248
   305
		return meta_errno;
darope@248
   306
	} else {
darope@248
   307
		if (plugin_handle)
darope@248
   308
			*plugin_handle = (void *)pl_loaded->handle;
darope@248
   309
		return 0;
darope@248
   310
	}
darope@248
   311
}
darope@248
   312
darope@248
   313
int mutil_UnloadMetaPlugin(plid_t plid, const char *fname, PLUG_LOADTIME now, PL_UNLOAD_REASON reason)
darope@248
   314
{
darope@248
   315
	MPlugin *findp = NULL;
darope@248
   316
	int pindex;
darope@248
   317
	char* endptr;
darope@248
   318
darope@248
   319
	if (NULL == fname)
darope@248
   320
	{
darope@248
   321
		return ME_ARGUMENT;
darope@248
   322
	}
darope@248
   323
darope@248
   324
	pindex = strtol(fname, &endptr, 10);
darope@248
   325
	if (*fname != '\0' && *endptr == '\0')
darope@248
   326
		findp = Plugins->find(pindex);
darope@248
   327
	else
darope@248
   328
		findp = Plugins->find_match(fname);
darope@248
   329
darope@248
   330
	if (!findp)
darope@248
   331
		return meta_errno;
darope@248
   332
darope@248
   333
	meta_errno = ME_NOERROR;
darope@248
   334
darope@248
   335
	if (findp->plugin_unload(plid, now, reason))
darope@248
   336
		return 0;
darope@248
   337
	
darope@248
   338
	return meta_errno;
darope@248
   339
}
darope@248
   340
darope@248
   341
int mutil_UnloadMetaPluginByHandle(plid_t plid, void *plugin_handle, PLUG_LOADTIME now, PL_UNLOAD_REASON reason)
darope@248
   342
{
darope@248
   343
	MPlugin *findp;
darope@248
   344
darope@248
   345
	if (NULL == plugin_handle)
darope@248
   346
	{
darope@248
   347
		return ME_ARGUMENT;
darope@248
   348
	}
darope@248
   349
darope@248
   350
	if (!(findp=Plugins->find((DLHANDLE)plugin_handle)))
darope@248
   351
		return ME_NOTFOUND;
darope@248
   352
	
darope@248
   353
	meta_errno = ME_NOERROR;
darope@248
   354
darope@248
   355
	if (findp->plugin_unload(plid, now, reason))
darope@248
   356
		return 0;
darope@248
   357
darope@248
   358
	return meta_errno;
darope@248
   359
}
darope@248
   360
darope@248
   361
const char *mutil_IsQueryingClientCvar(plid_t /*plid*/, const edict_t *pEdict)
darope@248
   362
{
darope@248
   363
	return g_Players.is_querying_cvar(pEdict);
darope@248
   364
}
darope@248
   365
darope@250
   366
int mutil_MakeRequestId(plid_t /*plid*/)
darope@250
   367
{
darope@250
   368
	//the offset is to distinguish from gamedll requests, if any
darope@250
   369
	return (abs(0xbeef<<16)+(++requestid_counter));
darope@250
   370
}
darope@250
   371
darope@250
   372
void mutil_GetHookTables(plid_t /*plid*/, enginefuncs_t **peng, DLL_FUNCTIONS **pdll, NEW_DLL_FUNCTIONS **pnewdll)
darope@250
   373
{
darope@250
   374
	if (peng)
darope@250
   375
		*peng = &meta_engfuncs;
darope@250
   376
	if (pdll)
darope@250
   377
		*pdll = pHookedDllFunctions;
darope@250
   378
	if (pnewdll)
darope@250
   379
		*pnewdll = pHookedNewDllFunctions;
darope@250
   380
}
darope@248
   381
darope@0
   382
#ifdef UNFINISHED
darope@0
   383
int mutil_HookGameEvent(plid_t plid, game_event_t event, 
darope@0
   384
		event_func_t pfnHandle) 
darope@0
   385
{
darope@0
   386
	return(Hooks->add(plid, event, pfnHandle));
darope@0
   387
}
darope@0
   388
darope@0
   389
int mutil_HookLogTrigger(plid_t plid, const char *trigger, 
darope@0
   390
		logmatch_func_t pfnHandle) 
darope@0
   391
{
darope@0
   392
	return(Hooks->add(plid, H_TRIGGER, trigger, pfnHandle));
darope@0
   393
}
darope@0
   394
darope@0
   395
int mutil_HookLogString(plid_t plid, const char *string, 
darope@0
   396
		logmatch_func_t pfnHandle) 
darope@0
   397
{
darope@0
   398
	return(Hooks->add(plid, H_STRING, string, pfnHandle));
darope@0
   399
}
darope@0
   400
darope@0
   401
int mutil_HookLogRegex(plid_t plid, const char *pattern, 
darope@0
   402
		logmatch_func_t pfnHandle) 
darope@0
   403
{
darope@0
   404
	return(Hooks->add(plid, H_STRING, pattern, pfnHandle));
darope@0
   405
}
darope@0
   406
darope@0
   407
qboolean mutil_RemoveHookID(plid_t plid, int hookid) {
darope@0
   408
	mBOOL ret;
darope@0
   409
	ret=Hooks->remove(plid, hookid);
darope@0
   410
	if(ret==mTRUE) return(true);
darope@0
   411
	else return(false);
darope@0
   412
}
darope@0
   413
darope@0
   414
int mutil_RemoveHookAll(plid_t plid) {
darope@0
   415
	return(Hooks->remove_all(plid));
darope@0
   416
}
darope@0
   417
#endif /* UNFINISHED */
darope@0
   418
darope@0
   419
// Meta Utility Function table.
darope@0
   420
mutil_funcs_t MetaUtilFunctions = {
darope@0
   421
	mutil_LogConsole,		// pfnLogConsole
darope@0
   422
	mutil_LogMessage,		// pfnLogMessage
darope@0
   423
	mutil_LogError,			// pfnLogError
darope@0
   424
	mutil_LogDeveloper,		// pfnLogDeveloper
darope@0
   425
	mutil_CenterSay,		// pfnCenterSay
darope@0
   426
	mutil_CenterSayParms,	// pfnCenterSayParms
darope@0
   427
	mutil_CenterSayVarargs,	// pfnCenterSayVarargs
darope@0
   428
	mutil_CallGameEntity,	// pfnCallGameEntity
darope@0
   429
	mutil_GetUserMsgID,		// pfnGetUserMsgID
darope@0
   430
	mutil_GetUserMsgName,	// pfnGetUserMsgName
darope@0
   431
	mutil_GetPluginPath,	// pfnGetPluginPath
darope@0
   432
	mutil_GetGameInfo,		// pfnGetGameInfo
darope@248
   433
	mutil_LoadMetaPlugin,	// pfnLoadPlugin
darope@248
   434
	mutil_UnloadMetaPlugin, // pfnUnloadPlugin
darope@248
   435
	mutil_UnloadMetaPluginByHandle, //pfnUnloadPluginByHandle
darope@248
   436
	mutil_IsQueryingClientCvar, //pfnIsQueryingClientCvar
darope@250
   437
	mutil_MakeRequestId,	// pfnMakeRequestId
darope@250
   438
	mutil_GetHookTables,    // pfnGetHookTables
darope@0
   439
#ifdef UNFINISHED
darope@0
   440
	mutil_HookGameEvent,	// pfnGameEvent
darope@0
   441
	mutil_HookLogTrigger,	// pfnLogTrigger
darope@0
   442
	mutil_HookLogString,	// pfnLogString
darope@0
   443
	mutil_HookLogRegex,		// pfnLogRegex
darope@0
   444
	mutil_RemoveHookID,		// pfnRemoveHookID
darope@0
   445
	mutil_RemoveHookAll,	// pfnRemoveHookAll
darope@0
   446
#endif /* UNFINISHED */
darope@0
   447
};