2
0
mirror of https://github.com/rehlds/metamod-r.git synced 2024-12-28 15:45:37 +03:00
metamod-r/metamod/src/mutil.cpp

368 lines
8.9 KiB
C++
Raw Normal View History

2016-07-26 03:22:47 +03:00
#include "precompiled.h"
2016-07-04 09:07:29 +03:00
hudtextparms_t default_csay_tparms = {
-1, 0.25, // x, y
2, // effect
0, 255, 0, 0, // r, g, b, a1
0, 0, 0, 0, // r2, g2, b2, a2
0, 0, 10, 10, // fadein, fadeout, hold, fxtime
1 // channel
2016-07-04 09:07:29 +03:00
};
// Log to console; newline added.
2017-01-06 22:33:36 +03:00
void EXT_FUNC mutil_LogConsole(plid_t plid, const char *fmt, ...)
{
2016-07-04 09:07:29 +03:00
va_list ap;
char buf[MAX_LOGMSG_LEN];
unsigned int len;
2016-07-04 09:07:29 +03:00
va_start(ap, fmt);
2016-07-26 19:31:47 +03:00
Q_vsnprintf(buf, sizeof(buf), fmt, ap);
2016-07-04 09:07:29 +03:00
va_end(ap);
2016-07-26 19:31:47 +03:00
2016-07-04 09:07:29 +03:00
// end msg with newline
2016-07-26 19:31:47 +03:00
len = Q_strlen(buf);
if (len < sizeof(buf) - 2) // -1 null, -1 for newline
2016-07-26 19:31:47 +03:00
Q_strcat(buf, "\n");
2016-07-04 09:07:29 +03:00
else
buf[len - 1] = '\n';
2016-07-04 09:07:29 +03:00
SERVER_PRINT(buf);
}
// Log regular message to logs; newline added.
2017-01-06 22:33:36 +03:00
void EXT_FUNC mutil_LogMessage(plid_t plid, const char *fmt, ...)
{
2016-07-04 09:07:29 +03:00
va_list ap;
char buf[MAX_LOGMSG_LEN];
2016-07-26 19:31:47 +03:00
plugin_info_t *plinfo = (plugin_info_t *)plid;
2016-07-04 09:07:29 +03:00
va_start(ap, fmt);
2016-07-26 19:31:47 +03:00
Q_vsnprintf(buf, sizeof(buf), fmt, ap);
2016-07-04 09:07:29 +03:00
va_end(ap);
ALERT(at_logged, "[%s] %s\n", plinfo->logtag, buf);
}
// Log an error message to logs; newline added.
2017-01-06 22:33:36 +03:00
void EXT_FUNC mutil_LogError(plid_t plid, const char *fmt, ...)
{
2016-07-04 09:07:29 +03:00
va_list ap;
char buf[MAX_LOGMSG_LEN];
2016-07-26 19:31:47 +03:00
plugin_info_t *plinfo = (plugin_info_t *)plid;
2016-07-04 09:07:29 +03:00
va_start(ap, fmt);
2016-07-26 19:31:47 +03:00
Q_vsnprintf(buf, sizeof(buf), fmt, ap);
2016-07-04 09:07:29 +03:00
va_end(ap);
ALERT(at_logged, "[%s] ERROR: %s\n", plinfo->logtag, buf);
}
// Log a message only if cvar "developer" set; newline added.
2017-01-06 22:33:36 +03:00
void EXT_FUNC mutil_LogDeveloper(plid_t plid, const char* fmt, ...)
{
2016-07-04 09:07:29 +03:00
va_list ap;
char buf[MAX_LOGMSG_LEN];
plugin_info_t* plinfo;
if ((int) CVAR_GET_FLOAT("developer") == 0)
2016-07-04 09:07:29 +03:00
return;
plinfo = (plugin_info_t *)plid;
2016-07-04 09:07:29 +03:00
va_start(ap, fmt);
2016-07-26 19:31:47 +03:00
Q_vsnprintf(buf, sizeof(buf), fmt, ap);
2016-07-04 09:07:29 +03:00
va_end(ap);
ALERT(at_logged, "[%s] dev: %s\n", plinfo->logtag, buf);
}
// Print message on center of all player's screens. Uses default text
// parameters (color green, 10 second fade-in).
2017-01-06 22:33:36 +03:00
void EXT_FUNC mutil_CenterSay(plid_t plid, const char* fmt, ...)
{
2016-07-04 09:07:29 +03:00
va_list ap;
va_start(ap, fmt);
mutil_CenterSayVarargs(plid, default_csay_tparms, fmt, ap);
va_end(ap);
}
// Print a center-message, with given text parameters.
2017-01-06 22:33:36 +03:00
void EXT_FUNC mutil_CenterSayParms(plid_t plid, hudtextparms_t tparms, const char* fmt, ...)
{
2016-07-04 09:07:29 +03:00
va_list ap;
va_start(ap, fmt);
mutil_CenterSayVarargs(plid, tparms, fmt, ap);
va_end(ap);
}
// Print a center-message, with text parameters and varargs. Provides
// functionality to the above center_say interfaces.
2017-01-06 22:33:36 +03:00
void EXT_FUNC mutil_CenterSayVarargs(plid_t plid, hudtextparms_t tparms, const char* fmt, va_list ap)
{
char buf[MAX_LOGMSG_LEN];
int n;
2016-07-26 19:31:47 +03:00
edict_t *pEntity;
2016-07-26 19:31:47 +03:00
Q_vsnprintf(buf, sizeof(buf), fmt, ap);
mutil_LogMessage(plid, "(centersay) %s", buf);
2016-07-26 19:31:47 +03:00
for (n = 1; n <= gpGlobals->maxClients; n++)
{
pEntity = INDEXENT(n);
if (FNullEnt(pEntity) || pEntity->free)
continue;
//META_UTIL_HudMessage(pEntity, tparms, buf); // TODO
}
}
2016-07-04 09:07:29 +03:00
// Allow plugins to call the entity functions in the GameDLL. In
// particular, calling "player()" as needed by most Bots. Suggested by
// Jussi Kivilinna.
2017-01-06 22:33:36 +03:00
qboolean EXT_FUNC mutil_CallGameEntity(plid_t plid, const char *entStr, entvars_t *pev)
{
2016-07-26 19:31:47 +03:00
plugin_info_t *plinfo = (plugin_info_t *)plid;
META_DEBUG(8, ("Looking up game entity '%s' for plugin '%s'", entStr, plinfo->name));
2017-01-07 01:24:40 +03:00
ENTITY_FN pfnEntity = (ENTITY_FN)GameDLL.sys_module.getsym(entStr);
2016-07-26 19:31:47 +03:00
if (!pfnEntity)
{
META_ERROR("Couldn't find game entity '%s' in game DLL '%s' for plugin '%s'", entStr, GameDLL.name, plinfo->name);
2016-07-26 19:31:47 +03:00
return false;
2016-07-04 09:07:29 +03:00
}
2016-07-26 19:31:47 +03:00
META_DEBUG(7, ("Calling game entity '%s' for plugin '%s'", entStr, plinfo->name));
2016-07-04 09:07:29 +03:00
(*pfnEntity)(pev);
2016-07-26 19:31:47 +03:00
return true;
2016-07-04 09:07:29 +03:00
}
// Find a usermsg, registered by the gamedll, with the corresponding
// msgname, and return remaining info about it (msgid, size).
2017-01-06 22:33:36 +03:00
int EXT_FUNC mutil_GetUserMsgID(plid_t plid, const char* msgname, int* size)
{
2016-07-26 19:31:47 +03:00
plugin_info_t *plinfo = (plugin_info_t *)plid;
META_DEBUG(8, ("Looking up usermsg name '%s' for plugin '%s'", msgname, plinfo->name));
2016-07-04 09:07:29 +03:00
2016-07-26 19:31:47 +03:00
MRegMsg *umsg = g_regMsgs->find(msgname);
if (umsg)
{
2016-07-26 03:22:47 +03:00
if (size)
*size = umsg->size;
2016-07-26 19:31:47 +03:00
return umsg->msgid;
2016-07-04 09:07:29 +03:00
}
else
2016-07-26 19:31:47 +03:00
return 0;
2016-07-04 09:07:29 +03:00
}
// Find a usermsg, registered by the gamedll, with the corresponding
// msgid, and return remaining info about it (msgname, size).
2017-01-06 22:33:36 +03:00
const char* EXT_FUNC mutil_GetUserMsgName(plid_t plid, int msgid, int *size)
{
2016-07-26 19:31:47 +03:00
plugin_info_t *plinfo = (plugin_info_t *)plid;
META_DEBUG(8, ("Looking up usermsg id '%d' for plugin '%s'", msgid, plinfo->name));
2016-07-04 09:07:29 +03:00
// Guess names for any built-in g_engine messages mentioned in the SDK;
2016-07-04 09:07:29 +03:00
// from dlls/util.h.
2016-07-26 19:31:47 +03:00
if (msgid < 64)
{
switch (msgid)
{
2016-07-26 03:22:47 +03:00
case SVC_TEMPENTITY:
if (size) *size = -1;
2016-07-26 19:31:47 +03:00
return "tempentity?";
2016-07-26 03:22:47 +03:00
case SVC_INTERMISSION:
if (size) *size = -1;
2016-07-26 19:31:47 +03:00
return "intermission?";
2016-07-26 03:22:47 +03:00
case SVC_CDTRACK:
if (size) *size = -1;
2016-07-26 19:31:47 +03:00
return "cdtrack?";
2016-07-26 03:22:47 +03:00
case SVC_WEAPONANIM:
if (size) *size = -1;
2016-07-26 19:31:47 +03:00
return "weaponanim?";
2016-07-26 03:22:47 +03:00
case SVC_ROOMTYPE:
if (size) *size = -1;
2016-07-26 19:31:47 +03:00
return "roomtype?";
2016-07-26 03:22:47 +03:00
case SVC_DIRECTOR:
if (size) *size = -1;
2016-07-26 19:31:47 +03:00
return "director?";
2016-07-04 09:07:29 +03:00
}
}
2016-07-26 19:31:47 +03:00
MRegMsg *umsg = g_regMsgs->find(msgid);
if (umsg)
{
2016-07-26 03:22:47 +03:00
if (size)
*size = umsg->size;
2016-07-04 09:07:29 +03:00
// 'name' is assumed to be a constant string, allocated in the
// gamedll.
2016-07-26 19:31:47 +03:00
return umsg->name;
2016-07-04 09:07:29 +03:00
}
else
2016-07-26 19:31:47 +03:00
return NULL;
2016-07-04 09:07:29 +03:00
}
// Return the full path of the plugin's loaded dll/so file.
2017-01-06 22:33:36 +03:00
const char* EXT_FUNC mutil_GetPluginPath(plid_t plid)
{
static char buf[PATH_MAX ];
2016-07-26 19:31:47 +03:00
MPlugin *plug;
plug = g_plugins->find(plid);
2016-07-26 19:31:47 +03:00
if (!plug)
{
META_ERROR("GetPluginPath: couldn't find plugin '%s'", plid->name);
return NULL;
2016-07-04 09:07:29 +03:00
}
2016-07-26 19:31:47 +03:00
Q_strncpy(buf, plug->pathname, sizeof buf - 1);
buf[sizeof buf - 1] = '\0';
2016-07-26 03:22:47 +03:00
return buf;
2016-07-04 09:07:29 +03:00
}
// Return various string-based info about the game/MOD/gamedll.
2017-01-06 22:33:36 +03:00
const char* EXT_FUNC mutil_GetGameInfo(plid_t plid, ginfo_t type)
2016-07-26 03:22:47 +03:00
{
2016-07-04 09:07:29 +03:00
static char buf[MAX_STRBUF_LEN];
2016-07-26 19:31:47 +03:00
const char *cp;
switch (type)
{
2016-07-26 03:22:47 +03:00
case GINFO_NAME:
cp = GameDLL.name;
break;
case GINFO_DESC:
cp = GameDLL.desc;
break;
case GINFO_GAMEDIR:
cp = GameDLL.gamedir;
break;
case GINFO_DLL_FULLPATH:
cp = GameDLL.pathname;
break;
case GINFO_DLL_FILENAME:
cp = GameDLL.file;
break;
case GINFO_REALDLL_FULLPATH:
cp = GameDLL.real_pathname;
break;
default:
2016-07-26 19:31:47 +03:00
META_ERROR("GetGameInfo: invalid request '%d' from plugin '%s'", type, plid->name);
return NULL;
2016-07-04 09:07:29 +03:00
}
2016-07-26 19:31:47 +03:00
Q_strncpy(buf, cp, sizeof buf - 1);
buf[sizeof buf - 1] = '\0';
2016-07-26 03:22:47 +03:00
return buf;
2016-07-04 09:07:29 +03:00
}
2017-01-06 22:33:36 +03:00
int EXT_FUNC mutil_LoadMetaPlugin(plid_t plid, const char* fname, PLUG_LOADTIME now, void **plugin_handle)
2016-07-04 09:07:29 +03:00
{
2016-07-26 19:31:47 +03:00
MPlugin *pl_loaded;
if (!fname)
{
2016-07-26 03:22:47 +03:00
return ME_ARGUMENT;
2016-07-04 09:07:29 +03:00
}
meta_errno = ME_NOERROR;
2016-07-26 19:31:47 +03:00
if (!(pl_loaded = g_plugins->plugin_addload(plid, fname, now)))
{
2016-07-26 03:22:47 +03:00
if (plugin_handle)
2016-07-26 19:31:47 +03:00
*plugin_handle = nullptr;
2016-07-26 03:22:47 +03:00
return meta_errno;
}
2016-07-26 19:31:47 +03:00
else
{
2016-07-26 03:22:47 +03:00
if (plugin_handle)
2017-01-07 01:24:40 +03:00
*plugin_handle = (void *)pl_loaded->sys_module.gethandle();
2016-07-26 03:22:47 +03:00
return 0;
2016-07-04 09:07:29 +03:00
}
}
2017-01-06 22:33:36 +03:00
int EXT_FUNC mutil_UnloadMetaPlugin(plid_t plid, const char *fname, PLUG_LOADTIME now, PL_UNLOAD_REASON reason)
2016-07-04 09:07:29 +03:00
{
2016-07-26 19:31:47 +03:00
MPlugin *findp = nullptr;
2016-07-04 09:07:29 +03:00
int pindex;
2016-07-26 19:31:47 +03:00
char *endptr;
2016-07-04 09:07:29 +03:00
2016-07-26 19:31:47 +03:00
if (!fname)
{
2016-07-26 03:22:47 +03:00
return ME_ARGUMENT;
2016-07-04 09:07:29 +03:00
}
pindex = strtol(fname, &endptr, 10);
2016-07-26 03:22:47 +03:00
if (*fname != '\0' && *endptr == '\0')
findp = g_plugins->find(pindex);
2016-07-04 09:07:29 +03:00
else
findp = g_plugins->find_match(fname);
2016-07-04 09:07:29 +03:00
2016-07-26 03:22:47 +03:00
if (!findp)
return meta_errno;
2016-07-04 09:07:29 +03:00
meta_errno = ME_NOERROR;
2016-07-26 03:22:47 +03:00
if (findp->plugin_unload(plid, now, reason))
return 0;
return meta_errno;
2016-07-04 09:07:29 +03:00
}
2017-01-06 22:33:36 +03:00
int EXT_FUNC mutil_UnloadMetaPluginByHandle(plid_t plid, void *plugin_handle, PLUG_LOADTIME now, PL_UNLOAD_REASON reason)
2016-07-04 09:07:29 +03:00
{
2016-07-26 19:31:47 +03:00
MPlugin *findp;
2016-07-04 09:07:29 +03:00
2016-07-26 19:31:47 +03:00
if (!plugin_handle)
{
2016-07-26 03:22:47 +03:00
return ME_ARGUMENT;
2016-07-04 09:07:29 +03:00
}
2017-01-07 01:24:40 +03:00
if (!(findp = g_plugins->find((module_handle_t)plugin_handle)))
2016-07-26 03:22:47 +03:00
return ME_NOTFOUND;
2016-07-04 09:07:29 +03:00
meta_errno = ME_NOERROR;
2016-07-26 03:22:47 +03:00
if (findp->plugin_unload(plid, now, reason))
return 0;
2016-07-04 09:07:29 +03:00
2016-07-26 03:22:47 +03:00
return meta_errno;
2016-07-04 09:07:29 +03:00
}
2017-01-06 22:33:36 +03:00
const char* EXT_FUNC mutil_IsQueryingClientCvar(plid_t plid, const edict_t* pEdict)
{
2016-07-30 02:03:01 +03:00
return g_players.is_querying_cvar(pEdict);
2016-07-04 09:07:29 +03:00
}
2017-01-06 22:33:36 +03:00
int EXT_FUNC mutil_MakeRequestId(plid_t plid)
{
//the offset is to distinguish from gamedll requests, if any
2016-07-26 19:31:47 +03:00
return abs(0xbeef << 16) + (++requestid_counter);
2016-07-04 09:07:29 +03:00
}
2017-01-06 22:33:36 +03:00
void EXT_FUNC mutil_GetHookTables(plid_t plid, enginefuncs_t** peng, DLL_FUNCTIONS** pdll, NEW_DLL_FUNCTIONS** pnewdll)
{
2016-07-04 09:07:29 +03:00
if (peng)
*peng = &meta_engfuncs;
2016-07-26 19:31:47 +03:00
2016-07-04 09:07:29 +03:00
if (pdll)
*pdll = pHookedDllFunctions;
2016-07-26 19:31:47 +03:00
2016-07-04 09:07:29 +03:00
if (pnewdll)
*pnewdll = pHookedNewDllFunctions;
}
2016-07-04 09:07:29 +03:00
// Meta Utility Function table.
mutil_funcs_t MetaUtilFunctions = {
2016-07-26 19:31:47 +03:00
mutil_LogConsole, // pfnLogConsole
mutil_LogMessage, // pfnLogMessage
mutil_LogError, // pfnLogError
mutil_LogDeveloper, // pfnLogDeveloper
mutil_CenterSay, // pfnCenterSay
mutil_CenterSayParms, // pfnCenterSayParms
mutil_CenterSayVarargs, // pfnCenterSayVarargs
mutil_CallGameEntity, // pfnCallGameEntity
mutil_GetUserMsgID, // pfnGetUserMsgID
mutil_GetUserMsgName, // pfnGetUserMsgName
mutil_GetPluginPath, // pfnGetPluginPath
mutil_GetGameInfo, // pfnGetGameInfo
mutil_LoadMetaPlugin, // pfnLoadPlugin
mutil_UnloadMetaPlugin, // pfnUnloadPlugin
mutil_UnloadMetaPluginByHandle, // pfnUnloadPluginByHandle
mutil_IsQueryingClientCvar, // pfnIsQueryingClientCvar
mutil_MakeRequestId, // pfnMakeRequestId
mutil_GetHookTables, // pfnGetHookTables
2016-07-04 09:07:29 +03:00
};