metamod/mutil.cpp
author darope
Mon May 31 19:03:03 2004 +0000 (2004-05-31)
changeset 236 2c14a498e4a7
parent 0 d15304fcb7a7
child 130 4bfb3f26c519
child 248 f72386298cba
permissions -rw-r--r--
Merged with B-work for release 1.17.1
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@0
    39
darope@0
    40
#include <extdll.h>			// always
darope@0
    41
darope@0
    42
#include "mutil.h"			// me
darope@0
    43
#include "mhook.h"			// class MHookList, etc
darope@0
    44
#include "linkent.h"		// ENTITY_FN, etc
darope@0
    45
#include "metamod.h"		// Hooks, etc
darope@0
    46
#include "meta_api.h"		// 
darope@0
    47
#include "types_meta.h"		// mBOOL
darope@0
    48
#include "osdep.h"			// win32 vsnprintf, etc
darope@0
    49
#include "sdk_util.h"		// ALERT, etc
darope@0
    50
darope@0
    51
hudtextparms_t default_csay_tparms = {
darope@0
    52
	-1, 0.25,			// x, y
darope@0
    53
	2,					// effect
darope@0
    54
	0, 255, 0,	0,		// r, g, b,  a1
darope@0
    55
	0, 0, 0,	0,		// r2, g2, b2,  a2
darope@0
    56
	0, 0, 10, 10,		// fadein, fadeout, hold, fxtime
darope@0
    57
	1					// channel
darope@0
    58
};
darope@0
    59
darope@0
    60
// Log to console; newline added.
darope@0
    61
void mutil_LogConsole(plid_t /* plid */, const char *fmt, ...) {
darope@0
    62
	va_list ap;
darope@0
    63
	char buf[MAX_LOGMSG_LEN];
darope@0
    64
	unsigned int len;
darope@0
    65
darope@0
    66
	va_start(ap, fmt);
darope@0
    67
	vsnprintf(buf, sizeof(buf), fmt, ap);
darope@0
    68
	va_end(ap);
darope@0
    69
	// end msg with newline
darope@0
    70
	len=strlen(buf);
darope@0
    71
	if(len < sizeof(buf)-2)		// -1 null, -1 for newline
darope@0
    72
		strcat(buf, "\n");
darope@0
    73
	else
darope@0
    74
		buf[len-1] = '\n';
darope@0
    75
darope@0
    76
	SERVER_PRINT(buf);
darope@0
    77
}
darope@0
    78
darope@0
    79
// Log regular message to logs; newline added.
darope@0
    80
void mutil_LogMessage(plid_t plid, const char *fmt, ...) {
darope@0
    81
	va_list ap;
darope@0
    82
	char buf[MAX_LOGMSG_LEN];
darope@0
    83
	plugin_info_t *plinfo;
darope@0
    84
darope@0
    85
	plinfo=(plugin_info_t *)plid;
darope@0
    86
	va_start(ap, fmt);
darope@0
    87
	vsnprintf(buf, sizeof(buf), fmt, ap);
darope@0
    88
	va_end(ap);
darope@0
    89
	ALERT(at_logged, "[%s] %s\n", plinfo->logtag, buf);
darope@0
    90
}
darope@0
    91
darope@0
    92
// Log an error message to logs; newline added.
darope@0
    93
void mutil_LogError(plid_t plid, const char *fmt, ...) {
darope@0
    94
	va_list ap;
darope@0
    95
	char buf[MAX_LOGMSG_LEN];
darope@0
    96
	plugin_info_t *plinfo;
darope@0
    97
darope@0
    98
	plinfo=(plugin_info_t *)plid;
darope@0
    99
	va_start(ap, fmt);
darope@0
   100
	vsnprintf(buf, sizeof(buf), fmt, ap);
darope@0
   101
	va_end(ap);
darope@0
   102
	ALERT(at_logged, "[%s] ERROR: %s\n", plinfo->logtag, buf);
darope@0
   103
}
darope@0
   104
darope@0
   105
// Log a message only if cvar "developer" set; newline added.
darope@0
   106
void mutil_LogDeveloper(plid_t plid, const char *fmt, ...) {
darope@0
   107
	va_list ap;
darope@0
   108
	char buf[MAX_LOGMSG_LEN];
darope@0
   109
	plugin_info_t *plinfo;
darope@0
   110
darope@0
   111
	if((int) CVAR_GET_FLOAT("developer") == 0)
darope@0
   112
		return;
darope@0
   113
darope@0
   114
	plinfo=(plugin_info_t *)plid;
darope@0
   115
	va_start(ap, fmt);
darope@0
   116
	vsnprintf(buf, sizeof(buf), fmt, ap);
darope@0
   117
	va_end(ap);
darope@0
   118
	ALERT(at_logged, "[%s] dev: %s\n", plinfo->logtag, buf);
darope@0
   119
}
darope@0
   120
darope@0
   121
// Print message on center of all player's screens.  Uses default text
darope@0
   122
// parameters (color green, 10 second fade-in).
darope@0
   123
void mutil_CenterSay(plid_t plid, const char *fmt, ...) {
darope@0
   124
	va_list ap;
darope@0
   125
	va_start(ap, fmt);
darope@0
   126
	mutil_CenterSayVarargs(plid, default_csay_tparms, fmt, ap);
darope@0
   127
	va_end(ap);
darope@0
   128
}
darope@0
   129
darope@0
   130
// Print a center-message, with given text parameters.
darope@0
   131
void mutil_CenterSayParms(plid_t plid, hudtextparms_t tparms, const char *fmt, ...) {
darope@0
   132
	va_list ap;
darope@0
   133
	va_start(ap, fmt);
darope@0
   134
	mutil_CenterSayVarargs(plid, tparms, fmt, ap);
darope@0
   135
	va_end(ap);
darope@0
   136
}
darope@0
   137
darope@0
   138
// Print a center-message, with text parameters and varargs.  Provides
darope@0
   139
// functionality to the above center_say interfaces.
darope@0
   140
void mutil_CenterSayVarargs(plid_t plid, hudtextparms_t tparms, 
darope@0
   141
		const char *fmt, va_list ap) 
darope@0
   142
{
darope@0
   143
	char buf[MAX_LOGMSG_LEN];
darope@0
   144
	int n;
darope@0
   145
	edict_t *pEntity;
darope@0
   146
darope@0
   147
	vsnprintf(buf, sizeof(buf), fmt, ap);
darope@0
   148
darope@0
   149
	mutil_LogMessage(plid, "(centersay) %s", buf);
darope@0
   150
	for(n=1; n <= gpGlobals->maxClients; n++) {
darope@0
   151
		pEntity=INDEXENT(n);
darope@0
   152
		if(FNullEnt(pEntity) || pEntity->free)
darope@0
   153
			continue;
darope@236
   154
		META_UTIL_HudMessage(pEntity, tparms, buf);
darope@0
   155
	}
darope@0
   156
}
darope@0
   157
darope@0
   158
// Allow plugins to call the entity functions in the GameDLL.  In
darope@0
   159
// particular, calling "player()" as needed by most Bots.  Suggested by
darope@0
   160
// Jussi Kivilinna.
darope@0
   161
qboolean mutil_CallGameEntity(plid_t plid, const char *entStr, entvars_t *pev) {
darope@0
   162
	plugin_info_t *plinfo;
darope@0
   163
	ENTITY_FN pfnEntity;
darope@0
   164
darope@0
   165
	plinfo=(plugin_info_t *)plid;
darope@0
   166
	META_DEBUG(8, ("Looking up game entity '%s' for plugin '%s'", entStr,
darope@0
   167
				plinfo->name));
darope@0
   168
	pfnEntity = (ENTITY_FN) DLSYM(GameDLL.handle, entStr);
darope@0
   169
	if(!pfnEntity) {
darope@0
   170
		META_ERROR("Couldn't find game entity '%s' in game DLL '%s' for plugin '%s'", entStr, GameDLL.name, plinfo->name);
darope@0
   171
		return(false);
darope@0
   172
	}
darope@0
   173
	META_DEBUG(7, ("Calling game entity '%s' for plugin '%s'", entStr,
darope@0
   174
				plinfo->name));
darope@0
   175
	(*pfnEntity)(pev);
darope@0
   176
	return(true);
darope@0
   177
}
darope@0
   178
darope@0
   179
// Find a usermsg, registered by the gamedll, with the corresponding
darope@0
   180
// msgname, and return remaining info about it (msgid, size).
darope@0
   181
int mutil_GetUserMsgID(plid_t plid, const char *msgname, int *size) {
darope@0
   182
	plugin_info_t *plinfo;
darope@0
   183
	MRegMsg *umsg;
darope@0
   184
darope@0
   185
	plinfo=(plugin_info_t *)plid;
darope@0
   186
	META_DEBUG(8, ("Looking up usermsg name '%s' for plugin '%s'", msgname,
darope@0
   187
				plinfo->name));
darope@0
   188
	umsg=RegMsgs->find(msgname);
darope@0
   189
	if(umsg) {
darope@0
   190
		if(size)
darope@0
   191
			*size=umsg->size;
darope@0
   192
		return(umsg->msgid);
darope@0
   193
	}
darope@0
   194
	else
darope@0
   195
		return(0);
darope@0
   196
}
darope@0
   197
darope@0
   198
// Find a usermsg, registered by the gamedll, with the corresponding
darope@0
   199
// msgid, and return remaining info about it (msgname, size).
darope@0
   200
const char *mutil_GetUserMsgName(plid_t plid, int msgid, int *size) {
darope@0
   201
	plugin_info_t *plinfo;
darope@0
   202
	MRegMsg *umsg;
darope@0
   203
darope@0
   204
	plinfo=(plugin_info_t *)plid;
darope@0
   205
	META_DEBUG(8, ("Looking up usermsg id '%d' for plugin '%s'", msgid,
darope@0
   206
				plinfo->name));
darope@0
   207
	// Guess names for any built-in Engine messages mentioned in the SDK;
darope@0
   208
	// from dlls/util.h.
darope@0
   209
	if(msgid < 64) {
darope@0
   210
		switch(msgid) {
darope@0
   211
			case SVC_TEMPENTITY:
darope@0
   212
				if(size) *size=-1;
darope@0
   213
				return("tempentity?");
darope@0
   214
			case SVC_INTERMISSION:
darope@0
   215
				if(size) *size=-1;
darope@0
   216
				return("intermission?");
darope@0
   217
			case SVC_CDTRACK:
darope@0
   218
				if(size) *size=-1;
darope@0
   219
				return("cdtrack?");
darope@0
   220
			case SVC_WEAPONANIM:
darope@0
   221
				if(size) *size=-1;
darope@0
   222
				return("weaponanim?");
darope@0
   223
			case SVC_ROOMTYPE:
darope@0
   224
				if(size) *size=-1;
darope@0
   225
				return("roomtype?");
darope@0
   226
			case SVC_DIRECTOR:
darope@0
   227
				if(size) *size=-1;
darope@0
   228
				return("director?");
darope@0
   229
		}
darope@0
   230
	}
darope@0
   231
	umsg=RegMsgs->find(msgid);
darope@0
   232
	if(umsg) {
darope@0
   233
		if(size)
darope@0
   234
			*size=umsg->size;
darope@0
   235
		// 'name' is assumed to be a constant string, allocated in the
darope@0
   236
		// gamedll.
darope@0
   237
		return(umsg->name);
darope@0
   238
	}
darope@0
   239
	else
darope@0
   240
		return(NULL);
darope@0
   241
}
darope@0
   242
darope@0
   243
// Return the full path of the plugin's loaded dll/so file.
darope@0
   244
const char *mutil_GetPluginPath(plid_t plid) {
darope@0
   245
	static char buf[PATH_MAX];
darope@0
   246
	MPlugin *plug;
darope@0
   247
darope@0
   248
	plug=Plugins->find(plid);
darope@0
   249
	if(!plug) {
darope@0
   250
		META_ERROR("GetPluginPath: couldn't find plugin '%s'",
darope@0
   251
				plid->name);
darope@0
   252
		return(NULL);
darope@0
   253
	}
darope@0
   254
	STRNCPY(buf, plug->pathname, sizeof(buf));
darope@0
   255
	return(buf);
darope@0
   256
}
darope@0
   257
darope@0
   258
// Return various string-based info about the game/MOD/gamedll.
darope@0
   259
const char *mutil_GetGameInfo(plid_t plid, ginfo_t type) {
darope@0
   260
	static char buf[MAX_STRBUF_LEN];
darope@0
   261
	const char *cp;
darope@0
   262
	switch(type) {
darope@0
   263
		case GINFO_NAME:
darope@0
   264
			cp=GameDLL.name;
darope@0
   265
			break;
darope@0
   266
		case GINFO_DESC:
darope@0
   267
			cp=GameDLL.desc;
darope@0
   268
			break;
darope@0
   269
		case GINFO_GAMEDIR:
darope@0
   270
			cp=GameDLL.gamedir;
darope@0
   271
			break;
darope@0
   272
		case GINFO_DLL_FULLPATH:
darope@0
   273
			cp=GameDLL.pathname;
darope@0
   274
			break;
darope@0
   275
		case GINFO_DLL_FILENAME:
darope@0
   276
			cp=GameDLL.file;
darope@0
   277
			break;
darope@0
   278
		case GINFO_REALDLL_FULLPATH:
darope@0
   279
			cp=GameDLL.real_pathname;
darope@0
   280
			break;
darope@0
   281
		default:
darope@0
   282
			META_ERROR("GetGameInfo: invalid request '%d' from plugin '%s'",
darope@0
   283
					type, plid->name);
darope@0
   284
			return(NULL);
darope@0
   285
	}
darope@0
   286
	STRNCPY(buf, cp, sizeof(buf));
darope@0
   287
	return(buf);
darope@0
   288
}
darope@0
   289
darope@0
   290
#ifdef UNFINISHED
darope@0
   291
int mutil_HookGameEvent(plid_t plid, game_event_t event, 
darope@0
   292
		event_func_t pfnHandle) 
darope@0
   293
{
darope@0
   294
	return(Hooks->add(plid, event, pfnHandle));
darope@0
   295
}
darope@0
   296
darope@0
   297
int mutil_HookLogTrigger(plid_t plid, const char *trigger, 
darope@0
   298
		logmatch_func_t pfnHandle) 
darope@0
   299
{
darope@0
   300
	return(Hooks->add(plid, H_TRIGGER, trigger, pfnHandle));
darope@0
   301
}
darope@0
   302
darope@0
   303
int mutil_HookLogString(plid_t plid, const char *string, 
darope@0
   304
		logmatch_func_t pfnHandle) 
darope@0
   305
{
darope@0
   306
	return(Hooks->add(plid, H_STRING, string, pfnHandle));
darope@0
   307
}
darope@0
   308
darope@0
   309
int mutil_HookLogRegex(plid_t plid, const char *pattern, 
darope@0
   310
		logmatch_func_t pfnHandle) 
darope@0
   311
{
darope@0
   312
	return(Hooks->add(plid, H_STRING, pattern, pfnHandle));
darope@0
   313
}
darope@0
   314
darope@0
   315
qboolean mutil_RemoveHookID(plid_t plid, int hookid) {
darope@0
   316
	mBOOL ret;
darope@0
   317
	ret=Hooks->remove(plid, hookid);
darope@0
   318
	if(ret==mTRUE) return(true);
darope@0
   319
	else return(false);
darope@0
   320
}
darope@0
   321
darope@0
   322
int mutil_RemoveHookAll(plid_t plid) {
darope@0
   323
	return(Hooks->remove_all(plid));
darope@0
   324
}
darope@0
   325
#endif /* UNFINISHED */
darope@0
   326
darope@0
   327
// Meta Utility Function table.
darope@0
   328
mutil_funcs_t MetaUtilFunctions = {
darope@0
   329
	mutil_LogConsole,		// pfnLogConsole
darope@0
   330
	mutil_LogMessage,		// pfnLogMessage
darope@0
   331
	mutil_LogError,			// pfnLogError
darope@0
   332
	mutil_LogDeveloper,		// pfnLogDeveloper
darope@0
   333
	mutil_CenterSay,		// pfnCenterSay
darope@0
   334
	mutil_CenterSayParms,	// pfnCenterSayParms
darope@0
   335
	mutil_CenterSayVarargs,	// pfnCenterSayVarargs
darope@0
   336
	mutil_CallGameEntity,	// pfnCallGameEntity
darope@0
   337
	mutil_GetUserMsgID,		// pfnGetUserMsgID
darope@0
   338
	mutil_GetUserMsgName,	// pfnGetUserMsgName
darope@0
   339
	mutil_GetPluginPath,	// pfnGetPluginPath
darope@0
   340
	mutil_GetGameInfo,		// pfnGetGameInfo
darope@0
   341
#ifdef UNFINISHED
darope@0
   342
	mutil_HookGameEvent,	// pfnGameEvent
darope@0
   343
	mutil_HookLogTrigger,	// pfnLogTrigger
darope@0
   344
	mutil_HookLogString,	// pfnLogString
darope@0
   345
	mutil_HookLogRegex,		// pfnLogRegex
darope@0
   346
	mutil_RemoveHookID,		// pfnRemoveHookID
darope@0
   347
	mutil_RemoveHookAll,	// pfnRemoveHookAll
darope@0
   348
#endif /* UNFINISHED */
darope@0
   349
};