4 // reg_support.cpp - support for things "registered" by plugins (console
5 // cmds, cvars, msgs, etc)
10 * This file is part of Metamod.
12 * Metamod is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * Metamod is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Metamod; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * In addition, as a special exception, the author gives permission to
27 * link the code of this program with the Half-Life Game Engine ("HL
28 * Engine") and Modified Game Libraries ("MODs") developed by Valve,
29 * L.L.C ("Valve"). You must obey the GNU General Public License in all
30 * respects for all of the code used other than the HL Engine and MODs
31 * from Valve. If you modify this file, you may extend this exception
32 * to your version of the file, but you are not obligated to do so. If
33 * you do not wish to do so, delete this exception statement from your
39 // enable extra routines in system header files, like strsignal
45 #include <string.h> // strsignal, etc
47 #include <extdll.h> // always
48 #include "sdk_util.h" // REG_SVR_COMMAND, etc
50 #include "reg_support.h" // me
51 #include "metamod.h" // RegCmds, etc
52 #include "log_meta.h" // META_ERROR, etc
54 // "Register" support.
56 // This code is necessary to support the different "register" engine
57 // functions that can be called by plugins, so that the plugins can be
58 // safely unloaded without incurring segmentation faults.
60 // The problem was that, when a plugin called, say:
61 // REG_SVR_COMMAND("stub_version", svr_stub_version);
63 // the engine stored the address to the "stub_version" string, as well as,
64 // of course, the svr_stub_version() function pointer. If the plugin was
65 // unloaded, and dlclose()'d, the memory locations for both the string and
66 // function became invalid and unavailable. If the engine tried to
67 // strcmp() against the string, or tried to call the function pointer, it
68 // generated a segfault and killed the process.
70 // The solution used here is to create strings and functions allocated
71 // locally, in metamod.dll, which won't become unavailable or invalid due
72 // to a dlclose(). These local copies are passed to the engine instead of
73 // those allocated in the plugin DLLs. Thus, when a plugin is later
74 // unloaded and dlclose'd, the engine can still reference the memory
75 // locations of registered strings and functions, without segfaulting.
77 // Now, what happens with a registered string or function after a plugin is
78 // unloaded not quite as nice as I'd like.
80 // Console commands will print a message along the lines of:
81 // [metamod: command '%s' unavailable; plugin unloaded]
83 // Cvars though, have their value unaffected. This can be convenient when
84 // reloading a plugin, as the cvars keep their old values. However it can
85 // also be confusing, as there is no indication when viewing/setting cvars
86 // from the console that they no longer have an effect.
88 // Also note, the console commands for listing registered cmds and cvars
89 // will try to show the name of the associated plugin. However, this only
90 // works under linux; under windows it will say "(unknown)". Linux
91 // provides a routine "dladdr()" which can indicate (during Reg request)
92 // which plugin it's coming from, and this is stored with the cvar entry.
93 // Windows didn't seem to have a similar routine, and I couldn't find
94 // another way to get the information..
97 // Generic command handler, passed to the engine for any AddServerCommand
98 // calls made by the plugin. It finds the appropriate plugin function
99 // pointer to call based on CMD_ARGV(0).
100 void meta_command_handler(void) {
104 META_DEBUG(5, ("called: meta_command_handler; arg0=%s args='%s'", CMD_ARGV(0), CMD_ARGS()));
107 META_ERROR("Null command name in meta_command_handler() ??");
111 icmd=RegCmds->find(cmd);
113 META_ERROR("Couldn't find registered plugin command: %s", cmd);
116 if(icmd->call() != mTRUE)
117 META_CONS("[metamod: command '%s' unavailable; plugin unloaded]", cmd);
121 // Replacement for engine routine AddServerCommand; called by plugins.
122 // Rather then handing the engine the plugin's command string and function
123 // pointer (both of which are allocated in the plugin DLL), this hands the
124 // engine a command string and function pointer allocated locally (in the
127 // The string handed to the engine is just a strdup() of the plugin's
128 // string. The function pointer handed to the engine is actually a pointer
129 // to a generic command-handler function (see above).
130 void meta_AddServerCommand(char *cmd_name, void (*function) (void)) {
131 MPlugin *iplug=NULL; MRegCmd *icmd=NULL;
133 META_DEBUG(4, ("called: meta_AddServerCommand; cmd_name=%s, function=%d", cmd_name, function));
135 // try to find which plugin is registering this command
136 if(!(iplug=Plugins->find_memloc((void *)function))) {
137 // if this isn't supported on this OS, don't log an error
138 if(meta_errno != ME_OSNOTSUP)
139 META_ERROR("Failed to find memloc for regcmd '%s'", cmd_name);
142 // See if this command was previously registered, ie a "reloaded" plugin.
143 icmd=RegCmds->find(cmd_name);
145 // If not found, add.
146 icmd=RegCmds->add(cmd_name);
148 // error details logged in add()
151 // Only register if not previously registered..
152 REG_SVR_COMMAND(icmd->name, meta_command_handler);
155 icmd->pfnCmd=function;
156 icmd->status=RG_VALID;
157 // Store which plugin this is for, if we know. We can use '0' for
158 // unknown plugin, since plugin index starts at 1.
160 icmd->plugid = iplug->index;
166 // Replacement for engine routine CVarRegister; called by plugins. Rather
167 // then handing the engine the plugin's cvar structure (which is allocated
168 // in the plugin DLL), this hands the engine a cvar structure allocated
169 // locally (in the metamod DLL).
171 // The cvar handed to the engine is globally allocated in the metamod.dll;
172 // the "name" and "string" fields are strdup()'s of the plugin's strings.
173 // Note that, once this is done, the cvar_t allocated in the plugin is no
174 // longer used for _anything_. As long as everything sets/gets the cvar
175 // values via the engine functions, this will work fine. If the plugin
176 // code tries to _directly_ read/set the fields of its own cvar structures,
177 // it will fail to work properly.
178 void meta_CVarRegister(cvar_t *pCvar) {
180 MRegCvar *icvar=NULL;
182 META_DEBUG(4, ("called: meta_CVarRegister; name=%s", pCvar->name));
184 // try to find which plugin is registering this cvar
185 if(!(iplug=Plugins->find_memloc((void *)pCvar))) {
186 // if this isn't supported on this OS, don't log an error
187 if(meta_errno != ME_OSNOTSUP)
188 // Note: if cvar_t was malloc'd by the plugin, we can't
189 // determine the calling plugin. Thus, this becomes a Debug
190 // rather than Error message.
191 META_DEBUG(1, ("Failed to find memloc for regcvar '%s'",
195 // See if this cvar was previously registered, ie a "reloaded" plugin.
196 icvar=RegCvars->find(pCvar->name);
198 // If not found, add.
199 icvar=RegCvars->add(pCvar->name);
201 // error details logged in add()
204 // Reset to given value
206 CVAR_REGISTER(icvar->data);
208 // Note: if not a new cvar, then we don't set the values, and just keep
209 // the pre-existing value.
211 icvar->status=RG_VALID;
212 // Store which plugin this is for, if we know. Use '0' for unknown
213 // plugin, as plugin index starts at 1.
215 icvar->plugid = iplug->index;
221 // Replacement for engine routine RegUserMsg; called by plugins. Rather
222 // than handing the engine the plugin's string (which is allocated in the
223 // plugin DLL), this hands the engine a string allocated from the stack.
225 // Note that while the above two functions maintain a list of registered
226 // strings/funcs/cvars from the plugins, this is not done here as I
227 // couldn't identify a need for it. Since the engine function merely maps
228 // a string to an int (msgid) for subsequent use in MessageBegin/etc,
229 // there's no function that needs to be provided and caught (like the
230 // commands) nor any useful actions to perform upon plugin unload (like the
231 // commands and cvars). This merely provides differently located storage
234 int meta_RegUserMsg(const char *pszName, int iSize) {
238 return(REG_USER_MSG(cp, iSize));