metamod/mutil.cpp
author darope
Sun Aug 21 21:52:35 2005 +0000 (2005-08-21)
branchB-work
changeset 158 02716ab98019
parent 149 610def0c5b51
child 195 c7737ac9af4f
permissions -rw-r--r--
Rewrote mplayer to use a MPlayer class and a MPlayerList class because the
previous implementation seemed to be too ambiguous so that it was used
incorrectly, and in expectation of a more advanced system in the future.
Fixed usage to work since before the cvar name was never set and thus
the IsQueryingClientCvar() call would always ahve returned NULL.

Changed the return value of some of the (Un-)LoadMetaPlugin*() functions
to use the meta_errno value from the find() call, i.e. ME_NOTFOUND or
ME_NOTUNIQ accordingly.
     1 // vi: set ts=4 sw=4 :
     2 // vim: set tw=75 :
     3 
     4 // mutil.cpp - utility functions to provide to plugins
     5 
     6 /*
     7  * Copyright (c) 2001-2003 Will Day <[email protected]>
     8  *
     9  *    This file is part of Metamod.
    10  *
    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.
    15  *
    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.
    20  *
    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
    24  *
    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
    33  *    version.
    34  *
    35  */
    36 
    37 #include <stdio.h>			// vsnprintf(), etc
    38 #include <stdarg.h>			// vs_start(), etc
    39 #include <stdlib.h>			// strtol()
    40 
    41 #include <extdll.h>			// always
    42 
    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
    51 
    52 hudtextparms_t default_csay_tparms = {
    53 	-1, 0.25,			// x, y
    54 	2,					// effect
    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
    58 	1					// channel
    59 };
    60 
    61 // Log to console; newline added.
    62 void mutil_LogConsole(plid_t /* plid */, const char *fmt, ...) {
    63 	va_list ap;
    64 	char buf[MAX_LOGMSG_LEN];
    65 	unsigned int len;
    66 
    67 	va_start(ap, fmt);
    68 	vsnprintf(buf, sizeof(buf), fmt, ap);
    69 	va_end(ap);
    70 	// end msg with newline
    71 	len=strlen(buf);
    72 	if(len < sizeof(buf)-2)		// -1 null, -1 for newline
    73 		strcat(buf, "\n");
    74 	else
    75 		buf[len-1] = '\n';
    76 
    77 	SERVER_PRINT(buf);
    78 }
    79 
    80 // Log regular message to logs; newline added.
    81 void mutil_LogMessage(plid_t plid, const char *fmt, ...) {
    82 	va_list ap;
    83 	char buf[MAX_LOGMSG_LEN];
    84 	plugin_info_t *plinfo;
    85 
    86 	plinfo=(plugin_info_t *)plid;
    87 	va_start(ap, fmt);
    88 	vsnprintf(buf, sizeof(buf), fmt, ap);
    89 	va_end(ap);
    90 	ALERT(at_logged, "[%s] %s\n", plinfo->logtag, buf);
    91 }
    92 
    93 // Log an error message to logs; newline added.
    94 void mutil_LogError(plid_t plid, const char *fmt, ...) {
    95 	va_list ap;
    96 	char buf[MAX_LOGMSG_LEN];
    97 	plugin_info_t *plinfo;
    98 
    99 	plinfo=(plugin_info_t *)plid;
   100 	va_start(ap, fmt);
   101 	vsnprintf(buf, sizeof(buf), fmt, ap);
   102 	va_end(ap);
   103 	ALERT(at_logged, "[%s] ERROR: %s\n", plinfo->logtag, buf);
   104 }
   105 
   106 // Log a message only if cvar "developer" set; newline added.
   107 void mutil_LogDeveloper(plid_t plid, const char *fmt, ...) {
   108 	va_list ap;
   109 	char buf[MAX_LOGMSG_LEN];
   110 	plugin_info_t *plinfo;
   111 
   112 	if((int) CVAR_GET_FLOAT("developer") == 0)
   113 		return;
   114 
   115 	plinfo=(plugin_info_t *)plid;
   116 	va_start(ap, fmt);
   117 	vsnprintf(buf, sizeof(buf), fmt, ap);
   118 	va_end(ap);
   119 	ALERT(at_logged, "[%s] dev: %s\n", plinfo->logtag, buf);
   120 }
   121 
   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, ...) {
   125 	va_list ap;
   126 	va_start(ap, fmt);
   127 	mutil_CenterSayVarargs(plid, default_csay_tparms, fmt, ap);
   128 	va_end(ap);
   129 }
   130 
   131 // Print a center-message, with given text parameters.
   132 void mutil_CenterSayParms(plid_t plid, hudtextparms_t tparms, const char *fmt, ...) {
   133 	va_list ap;
   134 	va_start(ap, fmt);
   135 	mutil_CenterSayVarargs(plid, tparms, fmt, ap);
   136 	va_end(ap);
   137 }
   138 
   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) 
   143 {
   144 	char buf[MAX_LOGMSG_LEN];
   145 	int n;
   146 	edict_t *pEntity;
   147 
   148 	vsnprintf(buf, sizeof(buf), fmt, ap);
   149 
   150 	mutil_LogMessage(plid, "(centersay) %s", buf);
   151 	for(n=1; n <= gpGlobals->maxClients; n++) {
   152 		pEntity=INDEXENT(n);
   153 		if(FNullEnt(pEntity) || pEntity->free)
   154 			continue;
   155 		META_UTIL_HudMessage(pEntity, tparms, buf);
   156 	}
   157 }
   158 
   159 // Allow plugins to call the entity functions in the GameDLL.  In
   160 // particular, calling "player()" as needed by most Bots.  Suggested by
   161 // Jussi Kivilinna.
   162 qboolean mutil_CallGameEntity(plid_t plid, const char *entStr, entvars_t *pev) {
   163 	plugin_info_t *plinfo;
   164 	ENTITY_FN pfnEntity;
   165 
   166 	plinfo=(plugin_info_t *)plid;
   167 	META_DEBUG(8, ("Looking up game entity '%s' for plugin '%s'", entStr,
   168 				plinfo->name));
   169 	pfnEntity = (ENTITY_FN) DLSYM(GameDLL.handle, entStr);
   170 	if(!pfnEntity) {
   171 		META_ERROR("Couldn't find game entity '%s' in game DLL '%s' for plugin '%s'", entStr, GameDLL.name, plinfo->name);
   172 		return(false);
   173 	}
   174 	META_DEBUG(7, ("Calling game entity '%s' for plugin '%s'", entStr,
   175 				plinfo->name));
   176 	(*pfnEntity)(pev);
   177 	return(true);
   178 }
   179 
   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;
   184 	MRegMsg *umsg;
   185 
   186 	plinfo=(plugin_info_t *)plid;
   187 	META_DEBUG(8, ("Looking up usermsg name '%s' for plugin '%s'", msgname,
   188 				plinfo->name));
   189 	umsg=RegMsgs->find(msgname);
   190 	if(umsg) {
   191 		if(size)
   192 			*size=umsg->size;
   193 		return(umsg->msgid);
   194 	}
   195 	else
   196 		return(0);
   197 }
   198 
   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;
   203 	MRegMsg *umsg;
   204 
   205 	plinfo=(plugin_info_t *)plid;
   206 	META_DEBUG(8, ("Looking up usermsg id '%d' for plugin '%s'", msgid,
   207 				plinfo->name));
   208 	// Guess names for any built-in Engine messages mentioned in the SDK;
   209 	// from dlls/util.h.
   210 	if(msgid < 64) {
   211 		switch(msgid) {
   212 			case SVC_TEMPENTITY:
   213 				if(size) *size=-1;
   214 				return("tempentity?");
   215 			case SVC_INTERMISSION:
   216 				if(size) *size=-1;
   217 				return("intermission?");
   218 			case SVC_CDTRACK:
   219 				if(size) *size=-1;
   220 				return("cdtrack?");
   221 			case SVC_WEAPONANIM:
   222 				if(size) *size=-1;
   223 				return("weaponanim?");
   224 			case SVC_ROOMTYPE:
   225 				if(size) *size=-1;
   226 				return("roomtype?");
   227 			case SVC_DIRECTOR:
   228 				if(size) *size=-1;
   229 				return("director?");
   230 		}
   231 	}
   232 	umsg=RegMsgs->find(msgid);
   233 	if(umsg) {
   234 		if(size)
   235 			*size=umsg->size;
   236 		// 'name' is assumed to be a constant string, allocated in the
   237 		// gamedll.
   238 		return(umsg->name);
   239 	}
   240 	else
   241 		return(NULL);
   242 }
   243 
   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];
   247 	MPlugin *plug;
   248 
   249 	plug=Plugins->find(plid);
   250 	if(!plug) {
   251 		META_ERROR("GetPluginPath: couldn't find plugin '%s'",
   252 				plid->name);
   253 		return(NULL);
   254 	}
   255 	STRNCPY(buf, plug->pathname, sizeof(buf));
   256 	return(buf);
   257 }
   258 
   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];
   262 	const char *cp;
   263 	switch(type) {
   264 		case GINFO_NAME:
   265 			cp=GameDLL.name;
   266 			break;
   267 		case GINFO_DESC:
   268 			cp=GameDLL.desc;
   269 			break;
   270 		case GINFO_GAMEDIR:
   271 			cp=GameDLL.gamedir;
   272 			break;
   273 		case GINFO_DLL_FULLPATH:
   274 			cp=GameDLL.pathname;
   275 			break;
   276 		case GINFO_DLL_FILENAME:
   277 			cp=GameDLL.file;
   278 			break;
   279 		case GINFO_REALDLL_FULLPATH:
   280 			cp=GameDLL.real_pathname;
   281 			break;
   282 		default:
   283 			META_ERROR("GetGameInfo: invalid request '%d' from plugin '%s'",
   284 					type, plid->name);
   285 			return(NULL);
   286 	}
   287 	STRNCPY(buf, cp, sizeof(buf));
   288 	return(buf);
   289 }
   290 
   291 int mutil_LoadMetaPlugin(plid_t plid, const char *fname, PLUG_LOADTIME now, void **plugin_handle)
   292 {
   293 	MPlugin *pl_loaded;
   294 	
   295 	if (NULL == fname)
   296 	{
   297 		return ME_ARGUMENT;
   298 	}
   299 
   300 	meta_errno = ME_NOERROR;
   301 	if (! (pl_loaded=Plugins->plugin_addload(plid, fname, now)) )
   302 	{
   303 		if (plugin_handle)
   304 			*plugin_handle = NULL;
   305 		return meta_errno;
   306 	} else {
   307 		if (plugin_handle)
   308 			*plugin_handle = (void *)pl_loaded->handle;
   309 		return 0;
   310 	}
   311 }
   312 
   313 int mutil_UnloadMetaPlugin(plid_t plid, const char *fname, PLUG_LOADTIME now, PL_UNLOAD_REASON reason)
   314 {
   315 	MPlugin *findp = NULL;
   316 	int pindex;
   317 	char* endptr;
   318 
   319 	if (NULL == fname)
   320 	{
   321 		return ME_ARGUMENT;
   322 	}
   323 
   324 	pindex = strtol(fname, &endptr, 10);
   325 	if (*fname != '\0' && *endptr == '\0')
   326 		findp = Plugins->find(pindex);
   327 	else
   328 		findp = Plugins->find_match(fname);
   329 
   330 	if (!findp)
   331 		return meta_errno;
   332 
   333 	meta_errno = ME_NOERROR;
   334 
   335 	if (findp->plugin_unload(plid, now, reason))
   336 		return 0;
   337 	
   338 	return meta_errno;
   339 }
   340 
   341 int mutil_UnloadMetaPluginByHandle(plid_t plid, void *plugin_handle, PLUG_LOADTIME now, PL_UNLOAD_REASON reason)
   342 {
   343 	MPlugin *findp;
   344 
   345 	if (NULL == plugin_handle)
   346 	{
   347 		return ME_ARGUMENT;
   348 	}
   349 
   350 	if (!(findp=Plugins->find((DLHANDLE)plugin_handle)))
   351 		return ME_NOTFOUND;
   352 	
   353 	meta_errno = ME_NOERROR;
   354 
   355 	if (findp->plugin_unload(plid, now, reason))
   356 		return 0;
   357 
   358 	return meta_errno;
   359 }
   360 
   361 const char *mutil_IsQueryingClientCvar(plid_t /*plid*/, const edict_t *pEdict)
   362 {
   363 	return g_Players.is_querying_cvar(pEdict);
   364 }
   365 
   366 
   367 #ifdef UNFINISHED
   368 int mutil_HookGameEvent(plid_t plid, game_event_t event, 
   369 		event_func_t pfnHandle) 
   370 {
   371 	return(Hooks->add(plid, event, pfnHandle));
   372 }
   373 
   374 int mutil_HookLogTrigger(plid_t plid, const char *trigger, 
   375 		logmatch_func_t pfnHandle) 
   376 {
   377 	return(Hooks->add(plid, H_TRIGGER, trigger, pfnHandle));
   378 }
   379 
   380 int mutil_HookLogString(plid_t plid, const char *string, 
   381 		logmatch_func_t pfnHandle) 
   382 {
   383 	return(Hooks->add(plid, H_STRING, string, pfnHandle));
   384 }
   385 
   386 int mutil_HookLogRegex(plid_t plid, const char *pattern, 
   387 		logmatch_func_t pfnHandle) 
   388 {
   389 	return(Hooks->add(plid, H_STRING, pattern, pfnHandle));
   390 }
   391 
   392 qboolean mutil_RemoveHookID(plid_t plid, int hookid) {
   393 	mBOOL ret;
   394 	ret=Hooks->remove(plid, hookid);
   395 	if(ret==mTRUE) return(true);
   396 	else return(false);
   397 }
   398 
   399 int mutil_RemoveHookAll(plid_t plid) {
   400 	return(Hooks->remove_all(plid));
   401 }
   402 #endif /* UNFINISHED */
   403 
   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
   422 #ifdef UNFINISHED
   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 */
   430 };