2
0
mirror of https://github.com/rehlds/metamod-r.git synced 2025-02-11 14:18:51 +03:00
metamod-r/metamod/src/reg_support.cpp

103 lines
3.8 KiB
C++
Raw Normal View History

2016-07-26 07:22:47 +07:00
#include "precompiled.h"
2016-07-04 12:07:29 +06:00
2017-01-06 22:33:36 +03:00
void EXT_FUNC meta_command_handler()
2016-07-26 07:22:47 +07:00
{
2017-05-09 19:31:09 +03:00
const char* cmd = CMD_ARGV(0);
MRegCmd* icmd = g_regCmds->find(cmd);
2017-01-07 03:20:55 +03:00
2017-05-09 19:31:09 +03:00
if (!icmd) {
META_ERROR("Couldn't find registered plugin command: %s", cmd);
2016-07-04 12:07:29 +06:00
return;
}
2016-07-26 23:31:47 +07:00
2017-05-09 19:31:09 +03:00
if (!icmd->call()) {
2016-07-04 12:07:29 +06:00
META_CONS("[metamod: command '%s' unavailable; plugin unloaded]", cmd);
2016-07-26 23:31:47 +07:00
}
2016-07-04 12:07:29 +06:00
}
// Replacement for engine routine AddServerCommand; called by plugins.
// Rather then handing the engine the plugin's command string and function
// pointer (both of which are allocated in the plugin DLL), this hands the
// engine a command string and function pointer allocated locally (in the
// metamod DLL).
//
2016-07-26 23:31:47 +07:00
// The string handed to the engine is just a Q_strdup() of the plugin's
2016-07-04 12:07:29 +06:00
// string. The function pointer handed to the engine is actually a pointer
// to a generic command-handler function (see above).
2017-05-09 19:31:09 +03:00
void EXT_FUNC meta_AddServerCommand(char* cmd_name, void (*function)())
2016-07-26 07:22:47 +07:00
{
2017-05-09 19:31:09 +03:00
MPlugin* plug = g_plugins->find_memloc(function);
2016-07-04 12:07:29 +06:00
2017-05-09 18:34:55 +03:00
META_DEBUG(4, "called: meta_AddServerCommand; cmd_name=%s, function=%d, plugin=%s", cmd_name, function, plug ? plug->file() : "unknown");
2016-07-04 12:07:29 +06:00
2017-01-07 21:03:16 +03:00
if (!plug) {
2017-01-07 03:20:55 +03:00
META_ERROR("Failed to find memloc for regcmd '%s'", cmd_name);
2016-07-04 12:07:29 +06:00
}
// See if this command was previously registered, ie a "reloaded" plugin.
auto cmd = g_regCmds->find(cmd_name);
2017-05-09 19:31:09 +03:00
if (!cmd) {
2016-07-04 12:07:29 +06:00
// If not found, add.
cmd = g_regCmds->add(cmd_name, function, plug);
REG_SVR_COMMAND(cmd->getname(), g_RehldsFuncs ? cmd->gethandler() : meta_command_handler);
2016-07-04 12:07:29 +06:00
}
}
// Replacement for engine routine CVarRegister; called by plugins. Rather
// then handing the engine the plugin's cvar structure (which is allocated
// in the plugin DLL), this hands the engine a cvar structure allocated
// locally (in the metamod DLL).
//
// The cvar handed to the engine is globally allocated in the metamod.dll;
2016-07-26 23:31:47 +07:00
// the "name" and "string" fields are Q_strdup()'s of the plugin's strings.
2016-07-04 12:07:29 +06:00
// Note that, once this is done, the cvar_t allocated in the plugin is no
// longer used for _anything_. As long as everything sets/gets the cvar
// values via the engine functions, this will work fine. If the plugin
// code tries to _directly_ read/set the fields of its own cvar structures,
// it will fail to work properly.
2017-05-09 19:31:09 +03:00
void EXT_FUNC meta_CVarRegister(cvar_t* pCvar)
2016-07-26 07:22:47 +07:00
{
2017-05-09 19:31:09 +03:00
MPlugin* plug = g_plugins->find_memloc(pCvar);
2016-07-04 12:07:29 +06:00
META_DEBUG(4, "called: meta_CVarRegister; name=%s", pCvar->name);
2016-07-04 12:07:29 +06:00
// try to find which plugin is registering this cvar
2017-05-09 19:31:09 +03:00
if (!plug) {
META_DEBUG(1, "Failed to find memloc for regcvar '%s'", pCvar->name);
2016-07-04 12:07:29 +06:00
}
// See if this cvar was previously registered, ie a "reloaded" plugin.
auto reg = g_regCvars->find(pCvar->name);
2016-07-26 23:31:47 +07:00
2017-05-09 19:31:09 +03:00
if (!reg) {
reg = g_regCvars->add(pCvar, plug);
CVAR_REGISTER(reg->getcvar());
2016-07-04 12:07:29 +06:00
}
}
// Replacement for engine routine RegUserMsg; called by plugins. Rather
// than handing the engine the plugin's string (which is allocated in the
// plugin DLL), this hands the engine a string allocated from the stack.
//
// Note that while the above two functions maintain a list of registered
// strings/funcs/cvars from the plugins, this is not done here as I
// couldn't identify a need for it. Since the engine function merely maps
// a string to an int (msgid) for subsequent use in MessageBegin/etc,
// there's no function that needs to be provided and caught (like the
// commands) nor any useful actions to perform upon plugin unload (like the
// commands and cvars). This merely provides differently located storage
// for the string.
2017-05-09 19:31:09 +03:00
int EXT_FUNC meta_RegUserMsg(const char* pszName, int iSize)
{
2017-05-09 19:31:09 +03:00
char* cp = Q_strdup(pszName);
2016-07-26 23:31:47 +07:00
return REG_USER_MSG(cp, iSize);
2016-07-04 12:07:29 +06:00
}
// Intercept and record queries
2017-05-09 19:31:09 +03:00
void EXT_FUNC meta_QueryClientCvarValue(const edict_t* player, const char* cvarName)
2016-07-04 13:11:20 +06:00
{
2016-07-30 02:03:01 +03:00
g_players.set_player_cvar_query(player, cvarName);
2017-01-06 22:33:36 +03:00
g_engfuncs.pfnQueryClientCvarValue(player, cvarName);
2016-07-04 12:07:29 +06:00
}