4 // mutil.cpp - utility functions to provide to plugins
9 * This file is part of Metamod.
11 * Metamod is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
16 * Metamod is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Metamod; if not, write to the Free Software Foundation,
23 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * In addition, as a special exception, the author gives permission to
26 * link the code of this program with the Half-Life Game Engine ("HL
27 * Engine") and Modified Game Libraries ("MODs") developed by Valve,
28 * L.L.C ("Valve"). You must obey the GNU General Public License in all
29 * respects for all of the code used other than the HL Engine and MODs
30 * from Valve. If you modify this file, you may extend this exception
31 * to your version of the file, but you are not obligated to do so. If
32 * you do not wish to do so, delete this exception statement from your
37 #include <stdio.h> // vsnprintf(), etc
38 #include <stdarg.h> // vs_start(), etc
39 #include <stdlib.h> // strtol()
41 #include <extdll.h> // always
43 #include "mutil.h" // me
44 #include "mhook.h" // class MHookList, etc
45 #include "linkent.h" // ENTITY_FN, etc
46 #include "metamod.h" // Hooks, etc
47 #include "meta_api.h" //
48 #include "types_meta.h" // mBOOL
49 #include "osdep.h" // win32 vsnprintf, etc
50 #include "sdk_util.h" // ALERT, etc
52 hudtextparms_t default_csay_tparms = {
55 0, 255, 0, 0, // r, g, b, a1
56 0, 0, 0, 0, // r2, g2, b2, a2
57 0, 0, 10, 10, // fadein, fadeout, hold, fxtime
61 // Log to console; newline added.
62 void mutil_LogConsole(plid_t /* plid */, const char *fmt, ...) {
64 char buf[MAX_LOGMSG_LEN];
68 vsnprintf(buf, sizeof(buf), fmt, ap);
70 // end msg with newline
72 if(len < sizeof(buf)-2) // -1 null, -1 for newline
80 // Log regular message to logs; newline added.
81 void mutil_LogMessage(plid_t plid, const char *fmt, ...) {
83 char buf[MAX_LOGMSG_LEN];
84 plugin_info_t *plinfo;
86 plinfo=(plugin_info_t *)plid;
88 vsnprintf(buf, sizeof(buf), fmt, ap);
90 ALERT(at_logged, "[%s] %s\n", plinfo->logtag, buf);
93 // Log an error message to logs; newline added.
94 void mutil_LogError(plid_t plid, const char *fmt, ...) {
96 char buf[MAX_LOGMSG_LEN];
97 plugin_info_t *plinfo;
99 plinfo=(plugin_info_t *)plid;
101 vsnprintf(buf, sizeof(buf), fmt, ap);
103 ALERT(at_logged, "[%s] ERROR: %s\n", plinfo->logtag, buf);
106 // Log a message only if cvar "developer" set; newline added.
107 void mutil_LogDeveloper(plid_t plid, const char *fmt, ...) {
109 char buf[MAX_LOGMSG_LEN];
110 plugin_info_t *plinfo;
112 if((int) CVAR_GET_FLOAT("developer") == 0)
115 plinfo=(plugin_info_t *)plid;
117 vsnprintf(buf, sizeof(buf), fmt, ap);
119 ALERT(at_logged, "[%s] dev: %s\n", plinfo->logtag, buf);
122 // Print message on center of all player's screens. Uses default text
123 // parameters (color green, 10 second fade-in).
124 void mutil_CenterSay(plid_t plid, const char *fmt, ...) {
127 mutil_CenterSayVarargs(plid, default_csay_tparms, fmt, ap);
131 // Print a center-message, with given text parameters.
132 void mutil_CenterSayParms(plid_t plid, hudtextparms_t tparms, const char *fmt, ...) {
135 mutil_CenterSayVarargs(plid, tparms, fmt, ap);
139 // Print a center-message, with text parameters and varargs. Provides
140 // functionality to the above center_say interfaces.
141 void mutil_CenterSayVarargs(plid_t plid, hudtextparms_t tparms,
142 const char *fmt, va_list ap)
144 char buf[MAX_LOGMSG_LEN];
148 vsnprintf(buf, sizeof(buf), fmt, ap);
150 mutil_LogMessage(plid, "(centersay) %s", buf);
151 for(n=1; n <= gpGlobals->maxClients; n++) {
153 if(FNullEnt(pEntity) || pEntity->free)
155 META_UTIL_HudMessage(pEntity, tparms, buf);
159 // Allow plugins to call the entity functions in the GameDLL. In
160 // particular, calling "player()" as needed by most Bots. Suggested by
162 qboolean mutil_CallGameEntity(plid_t plid, const char *entStr, entvars_t *pev) {
163 plugin_info_t *plinfo;
166 plinfo=(plugin_info_t *)plid;
167 META_DEBUG(8, ("Looking up game entity '%s' for plugin '%s'", entStr,
169 pfnEntity = (ENTITY_FN) DLSYM(GameDLL.handle, entStr);
171 META_ERROR("Couldn't find game entity '%s' in game DLL '%s' for plugin '%s'", entStr, GameDLL.name, plinfo->name);
174 META_DEBUG(7, ("Calling game entity '%s' for plugin '%s'", entStr,
180 // Find a usermsg, registered by the gamedll, with the corresponding
181 // msgname, and return remaining info about it (msgid, size).
182 int mutil_GetUserMsgID(plid_t plid, const char *msgname, int *size) {
183 plugin_info_t *plinfo;
186 plinfo=(plugin_info_t *)plid;
187 META_DEBUG(8, ("Looking up usermsg name '%s' for plugin '%s'", msgname,
189 umsg=RegMsgs->find(msgname);
199 // Find a usermsg, registered by the gamedll, with the corresponding
200 // msgid, and return remaining info about it (msgname, size).
201 const char *mutil_GetUserMsgName(plid_t plid, int msgid, int *size) {
202 plugin_info_t *plinfo;
205 plinfo=(plugin_info_t *)plid;
206 META_DEBUG(8, ("Looking up usermsg id '%d' for plugin '%s'", msgid,
208 // Guess names for any built-in Engine messages mentioned in the SDK;
214 return("tempentity?");
215 case SVC_INTERMISSION:
217 return("intermission?");
223 return("weaponanim?");
232 umsg=RegMsgs->find(msgid);
236 // 'name' is assumed to be a constant string, allocated in the
244 // Return the full path of the plugin's loaded dll/so file.
245 const char *mutil_GetPluginPath(plid_t plid) {
246 static char buf[PATH_MAX];
249 plug=Plugins->find(plid);
251 META_ERROR("GetPluginPath: couldn't find plugin '%s'",
255 STRNCPY(buf, plug->pathname, sizeof(buf));
259 // Return various string-based info about the game/MOD/gamedll.
260 const char *mutil_GetGameInfo(plid_t plid, ginfo_t type) {
261 static char buf[MAX_STRBUF_LEN];
273 case GINFO_DLL_FULLPATH:
276 case GINFO_DLL_FILENAME:
279 case GINFO_REALDLL_FULLPATH:
280 cp=GameDLL.real_pathname;
283 META_ERROR("GetGameInfo: invalid request '%d' from plugin '%s'",
287 STRNCPY(buf, cp, sizeof(buf));
291 int mutil_LoadMetaPlugin(plid_t plid, const char *fname, PLUG_LOADTIME now, void **plugin_handle)
300 meta_errno = ME_NOERROR;
301 if (! (pl_loaded=Plugins->plugin_addload(plid, fname, now)) )
304 *plugin_handle = NULL;
308 *plugin_handle = (void *)pl_loaded->handle;
313 int mutil_UnloadMetaPlugin(plid_t plid, const char *fname, PLUG_LOADTIME now, PL_UNLOAD_REASON reason)
315 MPlugin *findp = NULL;
324 pindex = strtol(fname, &endptr, 10);
325 if (*fname != '\0' && *endptr == '\0')
326 findp = Plugins->find(pindex);
328 findp = Plugins->find_match(fname);
333 meta_errno = ME_NOERROR;
335 if (findp->plugin_unload(plid, now, reason))
341 int mutil_UnloadMetaPluginByHandle(plid_t plid, void *plugin_handle, PLUG_LOADTIME now, PL_UNLOAD_REASON reason)
345 if (NULL == plugin_handle)
350 if (!(findp=Plugins->find((DLHANDLE)plugin_handle)))
353 meta_errno = ME_NOERROR;
355 if (findp->plugin_unload(plid, now, reason))
361 const char *mutil_IsQueryingClientCvar(plid_t /*plid*/, const edict_t *pEdict)
363 return g_Players.is_querying_cvar(pEdict);
368 int mutil_HookGameEvent(plid_t plid, game_event_t event,
369 event_func_t pfnHandle)
371 return(Hooks->add(plid, event, pfnHandle));
374 int mutil_HookLogTrigger(plid_t plid, const char *trigger,
375 logmatch_func_t pfnHandle)
377 return(Hooks->add(plid, H_TRIGGER, trigger, pfnHandle));
380 int mutil_HookLogString(plid_t plid, const char *string,
381 logmatch_func_t pfnHandle)
383 return(Hooks->add(plid, H_STRING, string, pfnHandle));
386 int mutil_HookLogRegex(plid_t plid, const char *pattern,
387 logmatch_func_t pfnHandle)
389 return(Hooks->add(plid, H_STRING, pattern, pfnHandle));
392 qboolean mutil_RemoveHookID(plid_t plid, int hookid) {
394 ret=Hooks->remove(plid, hookid);
395 if(ret==mTRUE) return(true);
399 int mutil_RemoveHookAll(plid_t plid) {
400 return(Hooks->remove_all(plid));
402 #endif /* UNFINISHED */
404 // Meta Utility Function table.
405 mutil_funcs_t MetaUtilFunctions = {
406 mutil_LogConsole, // pfnLogConsole
407 mutil_LogMessage, // pfnLogMessage
408 mutil_LogError, // pfnLogError
409 mutil_LogDeveloper, // pfnLogDeveloper
410 mutil_CenterSay, // pfnCenterSay
411 mutil_CenterSayParms, // pfnCenterSayParms
412 mutil_CenterSayVarargs, // pfnCenterSayVarargs
413 mutil_CallGameEntity, // pfnCallGameEntity
414 mutil_GetUserMsgID, // pfnGetUserMsgID
415 mutil_GetUserMsgName, // pfnGetUserMsgName
416 mutil_GetPluginPath, // pfnGetPluginPath
417 mutil_GetGameInfo, // pfnGetGameInfo
418 mutil_LoadMetaPlugin, // pfnLoadPlugin
419 mutil_UnloadMetaPlugin, // pfnUnloadPlugin
420 mutil_UnloadMetaPluginByHandle, //pfnUnloadPluginByHandle
421 mutil_IsQueryingClientCvar, //pfnIsQueryingClientCvar
423 mutil_HookGameEvent, // pfnGameEvent
424 mutil_HookLogTrigger, // pfnLogTrigger
425 mutil_HookLogString, // pfnLogString
426 mutil_HookLogRegex, // pfnLogRegex
427 mutil_RemoveHookID, // pfnRemoveHookID
428 mutil_RemoveHookAll, // pfnRemoveHookAll
429 #endif /* UNFINISHED */