1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/metamod/mutil.cpp Tue Apr 06 07:36:14 2004 +0000
1.3 @@ -0,0 +1,351 @@
1.4 +// vi: set ts=4 sw=4 :
1.5 +// vim: set tw=75 :
1.6 +
1.7 +// mutil.cpp - utility functions to provide to plugins
1.8 +
1.9 +/*
1.10 + * Copyright (c) 2001-2003 Will Day <[email protected]>
1.11 + *
1.12 + * This file is part of Metamod.
1.13 + *
1.14 + * Metamod is free software; you can redistribute it and/or modify it
1.15 + * under the terms of the GNU General Public License as published by the
1.16 + * Free Software Foundation; either version 2 of the License, or (at
1.17 + * your option) any later version.
1.18 + *
1.19 + * Metamod is distributed in the hope that it will be useful, but
1.20 + * WITHOUT ANY WARRANTY; without even the implied warranty of
1.21 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.22 + * General Public License for more details.
1.23 + *
1.24 + * You should have received a copy of the GNU General Public License
1.25 + * along with Metamod; if not, write to the Free Software Foundation,
1.26 + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.27 + *
1.28 + * In addition, as a special exception, the author gives permission to
1.29 + * link the code of this program with the Half-Life Game Engine ("HL
1.30 + * Engine") and Modified Game Libraries ("MODs") developed by Valve,
1.31 + * L.L.C ("Valve"). You must obey the GNU General Public License in all
1.32 + * respects for all of the code used other than the HL Engine and MODs
1.33 + * from Valve. If you modify this file, you may extend this exception
1.34 + * to your version of the file, but you are not obligated to do so. If
1.35 + * you do not wish to do so, delete this exception statement from your
1.36 + * version.
1.37 + *
1.38 + */
1.39 +
1.40 +#include <stdio.h> // vsnprintf(), etc
1.41 +#include <stdarg.h> // vs_start(), etc
1.42 +
1.43 +#include <extdll.h> // always
1.44 +
1.45 +#include "mutil.h" // me
1.46 +#include "mhook.h" // class MHookList, etc
1.47 +#include "linkent.h" // ENTITY_FN, etc
1.48 +#include "metamod.h" // Hooks, etc
1.49 +#include "meta_api.h" //
1.50 +#include "types_meta.h" // mBOOL
1.51 +#include "osdep.h" // win32 vsnprintf, etc
1.52 +#include "sdk_util.h" // ALERT, etc
1.53 +
1.54 +hudtextparms_t default_csay_tparms = {
1.55 + -1, 0.25, // x, y
1.56 + 2, // effect
1.57 + 0, 255, 0, 0, // r, g, b, a1
1.58 + 0, 0, 0, 0, // r2, g2, b2, a2
1.59 + 0, 0, 10, 10, // fadein, fadeout, hold, fxtime
1.60 + 1 // channel
1.61 +};
1.62 +
1.63 +// Log to console; newline added.
1.64 +void mutil_LogConsole(plid_t /* plid */, const char *fmt, ...) {
1.65 + va_list ap;
1.66 + char buf[MAX_LOGMSG_LEN];
1.67 + unsigned int len;
1.68 +
1.69 + va_start(ap, fmt);
1.70 + vsnprintf(buf, sizeof(buf), fmt, ap);
1.71 + va_end(ap);
1.72 + // end msg with newline
1.73 + len=strlen(buf);
1.74 + if(len < sizeof(buf)-2) // -1 null, -1 for newline
1.75 + strcat(buf, "\n");
1.76 + else
1.77 + buf[len-1] = '\n';
1.78 +
1.79 + SERVER_PRINT(buf);
1.80 +}
1.81 +
1.82 +// Log regular message to logs; newline added.
1.83 +void mutil_LogMessage(plid_t plid, const char *fmt, ...) {
1.84 + va_list ap;
1.85 + char buf[MAX_LOGMSG_LEN];
1.86 + plugin_info_t *plinfo;
1.87 +
1.88 + plinfo=(plugin_info_t *)plid;
1.89 + va_start(ap, fmt);
1.90 + vsnprintf(buf, sizeof(buf), fmt, ap);
1.91 + va_end(ap);
1.92 + ALERT(at_logged, "[%s] %s\n", plinfo->logtag, buf);
1.93 +}
1.94 +
1.95 +// Log an error message to logs; newline added.
1.96 +void mutil_LogError(plid_t plid, const char *fmt, ...) {
1.97 + va_list ap;
1.98 + char buf[MAX_LOGMSG_LEN];
1.99 + plugin_info_t *plinfo;
1.100 +
1.101 + plinfo=(plugin_info_t *)plid;
1.102 + va_start(ap, fmt);
1.103 + vsnprintf(buf, sizeof(buf), fmt, ap);
1.104 + va_end(ap);
1.105 + ALERT(at_logged, "[%s] ERROR: %s\n", plinfo->logtag, buf);
1.106 +}
1.107 +
1.108 +// Log a message only if cvar "developer" set; newline added.
1.109 +void mutil_LogDeveloper(plid_t plid, const char *fmt, ...) {
1.110 + va_list ap;
1.111 + char buf[MAX_LOGMSG_LEN];
1.112 + plugin_info_t *plinfo;
1.113 +
1.114 + if((int) CVAR_GET_FLOAT("developer") == 0)
1.115 + return;
1.116 +
1.117 + plinfo=(plugin_info_t *)plid;
1.118 + va_start(ap, fmt);
1.119 + vsnprintf(buf, sizeof(buf), fmt, ap);
1.120 + va_end(ap);
1.121 + ALERT(at_logged, "[%s] dev: %s\n", plinfo->logtag, buf);
1.122 +}
1.123 +
1.124 +// Print message on center of all player's screens. Uses default text
1.125 +// parameters (color green, 10 second fade-in).
1.126 +void mutil_CenterSay(plid_t plid, const char *fmt, ...) {
1.127 + va_list ap;
1.128 + va_start(ap, fmt);
1.129 + mutil_CenterSayVarargs(plid, default_csay_tparms, fmt, ap);
1.130 + va_end(ap);
1.131 +}
1.132 +
1.133 +// Print a center-message, with given text parameters.
1.134 +void mutil_CenterSayParms(plid_t plid, hudtextparms_t tparms, const char *fmt, ...) {
1.135 + va_list ap;
1.136 + va_start(ap, fmt);
1.137 + mutil_CenterSayVarargs(plid, tparms, fmt, ap);
1.138 + va_end(ap);
1.139 +}
1.140 +
1.141 +// Print a center-message, with text parameters and varargs. Provides
1.142 +// functionality to the above center_say interfaces.
1.143 +void mutil_CenterSayVarargs(plid_t plid, hudtextparms_t tparms,
1.144 + const char *fmt, va_list ap)
1.145 +{
1.146 + char buf[MAX_LOGMSG_LEN];
1.147 + int n;
1.148 + edict_t *pEntity;
1.149 + CBaseEntity *pPlayer;
1.150 +
1.151 + vsnprintf(buf, sizeof(buf), fmt, ap);
1.152 +
1.153 + mutil_LogMessage(plid, "(centersay) %s", buf);
1.154 + for(n=1; n <= gpGlobals->maxClients; n++) {
1.155 + pEntity=INDEXENT(n);
1.156 + if(FNullEnt(pEntity) || pEntity->free)
1.157 + continue;
1.158 + pPlayer=(CBaseEntity*)GET_PRIVATE(pEntity);
1.159 + UTIL_HudMessage(pPlayer, tparms, buf);
1.160 + }
1.161 +}
1.162 +
1.163 +// Allow plugins to call the entity functions in the GameDLL. In
1.164 +// particular, calling "player()" as needed by most Bots. Suggested by
1.165 +// Jussi Kivilinna.
1.166 +qboolean mutil_CallGameEntity(plid_t plid, const char *entStr, entvars_t *pev) {
1.167 + plugin_info_t *plinfo;
1.168 + ENTITY_FN pfnEntity;
1.169 +
1.170 + plinfo=(plugin_info_t *)plid;
1.171 + META_DEBUG(8, ("Looking up game entity '%s' for plugin '%s'", entStr,
1.172 + plinfo->name));
1.173 + pfnEntity = (ENTITY_FN) DLSYM(GameDLL.handle, entStr);
1.174 + if(!pfnEntity) {
1.175 + META_ERROR("Couldn't find game entity '%s' in game DLL '%s' for plugin '%s'", entStr, GameDLL.name, plinfo->name);
1.176 + return(false);
1.177 + }
1.178 + META_DEBUG(7, ("Calling game entity '%s' for plugin '%s'", entStr,
1.179 + plinfo->name));
1.180 + (*pfnEntity)(pev);
1.181 + return(true);
1.182 +}
1.183 +
1.184 +// Find a usermsg, registered by the gamedll, with the corresponding
1.185 +// msgname, and return remaining info about it (msgid, size).
1.186 +int mutil_GetUserMsgID(plid_t plid, const char *msgname, int *size) {
1.187 + plugin_info_t *plinfo;
1.188 + MRegMsg *umsg;
1.189 +
1.190 + plinfo=(plugin_info_t *)plid;
1.191 + META_DEBUG(8, ("Looking up usermsg name '%s' for plugin '%s'", msgname,
1.192 + plinfo->name));
1.193 + umsg=RegMsgs->find(msgname);
1.194 + if(umsg) {
1.195 + if(size)
1.196 + *size=umsg->size;
1.197 + return(umsg->msgid);
1.198 + }
1.199 + else
1.200 + return(0);
1.201 +}
1.202 +
1.203 +// Find a usermsg, registered by the gamedll, with the corresponding
1.204 +// msgid, and return remaining info about it (msgname, size).
1.205 +const char *mutil_GetUserMsgName(plid_t plid, int msgid, int *size) {
1.206 + plugin_info_t *plinfo;
1.207 + MRegMsg *umsg;
1.208 +
1.209 + plinfo=(plugin_info_t *)plid;
1.210 + META_DEBUG(8, ("Looking up usermsg id '%d' for plugin '%s'", msgid,
1.211 + plinfo->name));
1.212 + // Guess names for any built-in Engine messages mentioned in the SDK;
1.213 + // from dlls/util.h.
1.214 + if(msgid < 64) {
1.215 + switch(msgid) {
1.216 + case SVC_TEMPENTITY:
1.217 + if(size) *size=-1;
1.218 + return("tempentity?");
1.219 + case SVC_INTERMISSION:
1.220 + if(size) *size=-1;
1.221 + return("intermission?");
1.222 + case SVC_CDTRACK:
1.223 + if(size) *size=-1;
1.224 + return("cdtrack?");
1.225 + case SVC_WEAPONANIM:
1.226 + if(size) *size=-1;
1.227 + return("weaponanim?");
1.228 + case SVC_ROOMTYPE:
1.229 + if(size) *size=-1;
1.230 + return("roomtype?");
1.231 + case SVC_DIRECTOR:
1.232 + if(size) *size=-1;
1.233 + return("director?");
1.234 + }
1.235 + }
1.236 + umsg=RegMsgs->find(msgid);
1.237 + if(umsg) {
1.238 + if(size)
1.239 + *size=umsg->size;
1.240 + // 'name' is assumed to be a constant string, allocated in the
1.241 + // gamedll.
1.242 + return(umsg->name);
1.243 + }
1.244 + else
1.245 + return(NULL);
1.246 +}
1.247 +
1.248 +// Return the full path of the plugin's loaded dll/so file.
1.249 +const char *mutil_GetPluginPath(plid_t plid) {
1.250 + static char buf[PATH_MAX];
1.251 + MPlugin *plug;
1.252 +
1.253 + plug=Plugins->find(plid);
1.254 + if(!plug) {
1.255 + META_ERROR("GetPluginPath: couldn't find plugin '%s'",
1.256 + plid->name);
1.257 + return(NULL);
1.258 + }
1.259 + STRNCPY(buf, plug->pathname, sizeof(buf));
1.260 + return(buf);
1.261 +}
1.262 +
1.263 +// Return various string-based info about the game/MOD/gamedll.
1.264 +const char *mutil_GetGameInfo(plid_t plid, ginfo_t type) {
1.265 + static char buf[MAX_STRBUF_LEN];
1.266 + const char *cp;
1.267 + switch(type) {
1.268 + case GINFO_NAME:
1.269 + cp=GameDLL.name;
1.270 + break;
1.271 + case GINFO_DESC:
1.272 + cp=GameDLL.desc;
1.273 + break;
1.274 + case GINFO_GAMEDIR:
1.275 + cp=GameDLL.gamedir;
1.276 + break;
1.277 + case GINFO_DLL_FULLPATH:
1.278 + cp=GameDLL.pathname;
1.279 + break;
1.280 + case GINFO_DLL_FILENAME:
1.281 + cp=GameDLL.file;
1.282 + break;
1.283 + case GINFO_REALDLL_FULLPATH:
1.284 + cp=GameDLL.real_pathname;
1.285 + break;
1.286 + default:
1.287 + META_ERROR("GetGameInfo: invalid request '%d' from plugin '%s'",
1.288 + type, plid->name);
1.289 + return(NULL);
1.290 + }
1.291 + STRNCPY(buf, cp, sizeof(buf));
1.292 + return(buf);
1.293 +}
1.294 +
1.295 +#ifdef UNFINISHED
1.296 +int mutil_HookGameEvent(plid_t plid, game_event_t event,
1.297 + event_func_t pfnHandle)
1.298 +{
1.299 + return(Hooks->add(plid, event, pfnHandle));
1.300 +}
1.301 +
1.302 +int mutil_HookLogTrigger(plid_t plid, const char *trigger,
1.303 + logmatch_func_t pfnHandle)
1.304 +{
1.305 + return(Hooks->add(plid, H_TRIGGER, trigger, pfnHandle));
1.306 +}
1.307 +
1.308 +int mutil_HookLogString(plid_t plid, const char *string,
1.309 + logmatch_func_t pfnHandle)
1.310 +{
1.311 + return(Hooks->add(plid, H_STRING, string, pfnHandle));
1.312 +}
1.313 +
1.314 +int mutil_HookLogRegex(plid_t plid, const char *pattern,
1.315 + logmatch_func_t pfnHandle)
1.316 +{
1.317 + return(Hooks->add(plid, H_STRING, pattern, pfnHandle));
1.318 +}
1.319 +
1.320 +qboolean mutil_RemoveHookID(plid_t plid, int hookid) {
1.321 + mBOOL ret;
1.322 + ret=Hooks->remove(plid, hookid);
1.323 + if(ret==mTRUE) return(true);
1.324 + else return(false);
1.325 +}
1.326 +
1.327 +int mutil_RemoveHookAll(plid_t plid) {
1.328 + return(Hooks->remove_all(plid));
1.329 +}
1.330 +#endif /* UNFINISHED */
1.331 +
1.332 +// Meta Utility Function table.
1.333 +mutil_funcs_t MetaUtilFunctions = {
1.334 + mutil_LogConsole, // pfnLogConsole
1.335 + mutil_LogMessage, // pfnLogMessage
1.336 + mutil_LogError, // pfnLogError
1.337 + mutil_LogDeveloper, // pfnLogDeveloper
1.338 + mutil_CenterSay, // pfnCenterSay
1.339 + mutil_CenterSayParms, // pfnCenterSayParms
1.340 + mutil_CenterSayVarargs, // pfnCenterSayVarargs
1.341 + mutil_CallGameEntity, // pfnCallGameEntity
1.342 + mutil_GetUserMsgID, // pfnGetUserMsgID
1.343 + mutil_GetUserMsgName, // pfnGetUserMsgName
1.344 + mutil_GetPluginPath, // pfnGetPluginPath
1.345 + mutil_GetGameInfo, // pfnGetGameInfo
1.346 +#ifdef UNFINISHED
1.347 + mutil_HookGameEvent, // pfnGameEvent
1.348 + mutil_HookLogTrigger, // pfnLogTrigger
1.349 + mutil_HookLogString, // pfnLogString
1.350 + mutil_HookLogRegex, // pfnLogRegex
1.351 + mutil_RemoveHookID, // pfnRemoveHookID
1.352 + mutil_RemoveHookAll, // pfnRemoveHookAll
1.353 +#endif /* UNFINISHED */
1.354 +};