metamod/reg_support.cpp
changeset 0 d15304fcb7a7
child 62 ecbb47c671a4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/metamod/reg_support.cpp	Tue Apr 06 07:36:14 2004 +0000
     1.3 @@ -0,0 +1,237 @@
     1.4 +// vi: set ts=4 sw=4 :
     1.5 +// vim: set tw=75 :
     1.6 +
     1.7 +// reg_support.cpp - support for things "registered" by plugins (console
     1.8 +//                   cmds, cvars, msgs, etc)
     1.9 +
    1.10 +/*
    1.11 + * Copyright (c) 2001-2003 Will Day <[email protected]>
    1.12 + *
    1.13 + *    This file is part of Metamod.
    1.14 + *
    1.15 + *    Metamod is free software; you can redistribute it and/or modify it
    1.16 + *    under the terms of the GNU General Public License as published by the
    1.17 + *    Free Software Foundation; either version 2 of the License, or (at
    1.18 + *    your option) any later version.
    1.19 + *
    1.20 + *    Metamod is distributed in the hope that it will be useful, but
    1.21 + *    WITHOUT ANY WARRANTY; without even the implied warranty of
    1.22 + *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.23 + *    General Public License for more details.
    1.24 + *
    1.25 + *    You should have received a copy of the GNU General Public License
    1.26 + *    along with Metamod; if not, write to the Free Software Foundation,
    1.27 + *    Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.28 + *
    1.29 + *    In addition, as a special exception, the author gives permission to
    1.30 + *    link the code of this program with the Half-Life Game Engine ("HL
    1.31 + *    Engine") and Modified Game Libraries ("MODs") developed by Valve,
    1.32 + *    L.L.C ("Valve").  You must obey the GNU General Public License in all
    1.33 + *    respects for all of the code used other than the HL Engine and MODs
    1.34 + *    from Valve.  If you modify this file, you may extend this exception
    1.35 + *    to your version of the file, but you are not obligated to do so.  If
    1.36 + *    you do not wish to do so, delete this exception statement from your
    1.37 + *    version.
    1.38 + *
    1.39 + */
    1.40 +
    1.41 +#ifdef linux
    1.42 +// enable extra routines in system header files, like strsignal
    1.43 +#define _GNU_SOURCE
    1.44 +#endif /* linux */
    1.45 +
    1.46 +#include <string.h>			// strsignal, etc
    1.47 +
    1.48 +#include <extdll.h>			// always
    1.49 +#include "sdk_util.h"		// REG_SVR_COMMAND, etc
    1.50 +
    1.51 +#include "reg_support.h"	// me
    1.52 +#include "metamod.h"		// RegCmds, etc
    1.53 +#include "log_meta.h"		// META_ERROR, etc
    1.54 +
    1.55 +// "Register" support.
    1.56 +//
    1.57 +// This code is necessary to support the different "register" engine
    1.58 +// functions that can be called by plugins, so that the plugins can be
    1.59 +// safely unloaded without incurring segmentation faults.
    1.60 +//
    1.61 +// The problem was that, when a plugin called, say:
    1.62 +//    REG_SVR_COMMAND("stub_version", svr_stub_version);
    1.63 +//
    1.64 +// the engine stored the address to the "stub_version" string, as well as,
    1.65 +// of course, the svr_stub_version() function pointer.  If the plugin was
    1.66 +// unloaded, and dlclose()'d, the memory locations for both the string and
    1.67 +// function became invalid and unavailable.  If the engine tried to
    1.68 +// strcmp() against the string, or tried to call the function pointer, it
    1.69 +// generated a segfault and killed the process.
    1.70 +//
    1.71 +// The solution used here is to create strings and functions allocated
    1.72 +// locally, in metamod.dll, which won't become unavailable or invalid due
    1.73 +// to a dlclose().  These local copies are passed to the engine instead of
    1.74 +// those allocated in the plugin DLLs.  Thus, when a plugin is later
    1.75 +// unloaded and dlclose'd, the engine can still reference the memory
    1.76 +// locations of registered strings and functions, without segfaulting.
    1.77 +//
    1.78 +// Now, what happens with a registered string or function after a plugin is
    1.79 +// unloaded not quite as nice as I'd like.
    1.80 +//
    1.81 +// Console commands will print a message along the lines of:
    1.82 +//    [metamod: command '%s' unavailable; plugin unloaded]
    1.83 +//
    1.84 +// Cvars though, have their value unaffected.  This can be convenient when
    1.85 +// reloading a plugin, as the cvars keep their old values.  However it can
    1.86 +// also be confusing, as there is no indication when viewing/setting cvars
    1.87 +// from the console that they no longer have an effect.
    1.88 +//
    1.89 +// Also note, the console commands for listing registered cmds and cvars
    1.90 +// will try to show the name of the associated plugin.  However, this only
    1.91 +// works under linux; under windows it will say "(unknown)".  Linux
    1.92 +// provides a routine "dladdr()" which can indicate (during Reg request)
    1.93 +// which plugin it's coming from, and this is stored with the cvar entry.
    1.94 +// Windows didn't seem to have a similar routine, and I couldn't find
    1.95 +// another way to get the information..
    1.96 +
    1.97 +
    1.98 +// Generic command handler, passed to the engine for any AddServerCommand
    1.99 +// calls made by the plugin.  It finds the appropriate plugin function
   1.100 +// pointer to call based on CMD_ARGV(0).
   1.101 +void meta_command_handler(void) {
   1.102 +	MRegCmd *icmd;
   1.103 +	const char *cmd;
   1.104 +
   1.105 +	META_DEBUG(5, ("called: meta_command_handler; arg0=%s args='%s'", CMD_ARGV(0), CMD_ARGS()));
   1.106 +	cmd=CMD_ARGV(0);
   1.107 +	if(!cmd) {
   1.108 +		META_ERROR("Null command name in meta_command_handler() ??");
   1.109 +		return;
   1.110 +	}
   1.111 +
   1.112 +	icmd=RegCmds->find(cmd);
   1.113 +	if(!icmd) {
   1.114 +		META_ERROR("Couldn't find registered plugin command: %s", cmd);
   1.115 +		return;
   1.116 +	}
   1.117 +	if(icmd->call() != mTRUE)
   1.118 +		META_CONS("[metamod: command '%s' unavailable; plugin unloaded]", cmd);
   1.119 +}
   1.120 +
   1.121 +
   1.122 +// Replacement for engine routine AddServerCommand; called by plugins.
   1.123 +// Rather then handing the engine the plugin's command string and function
   1.124 +// pointer (both of which are allocated in the plugin DLL), this hands the
   1.125 +// engine a command string and function pointer allocated locally (in the
   1.126 +// metamod DLL).
   1.127 +//
   1.128 +// The string handed to the engine is just a strdup() of the plugin's
   1.129 +// string.  The function pointer handed to the engine is actually a pointer
   1.130 +// to a generic command-handler function (see above).
   1.131 +void meta_AddServerCommand(char *cmd_name, void (*function) (void)) {
   1.132 +	MPlugin *iplug=NULL; MRegCmd *icmd=NULL;
   1.133 +
   1.134 +	META_DEBUG(4, ("called: meta_AddServerCommand; cmd_name=%s, function=%d", cmd_name, function));
   1.135 +
   1.136 +	// try to find which plugin is registering this command
   1.137 +	if(!(iplug=Plugins->find_memloc((void *)function))) {
   1.138 +		// if this isn't supported on this OS, don't log an error
   1.139 +		if(meta_errno != ME_OSNOTSUP)
   1.140 +			META_ERROR("Failed to find memloc for regcmd '%s'", cmd_name);
   1.141 +	}
   1.142 +
   1.143 +	// See if this command was previously registered, ie a "reloaded" plugin.
   1.144 +	icmd=RegCmds->find(cmd_name);
   1.145 +	if(!icmd) {
   1.146 +		// If not found, add.
   1.147 +		icmd=RegCmds->add(cmd_name);
   1.148 +		if(!icmd) {
   1.149 +			// error details logged in add()
   1.150 +			return;
   1.151 +		}
   1.152 +		// Only register if not previously registered..
   1.153 +		REG_SVR_COMMAND(icmd->name, meta_command_handler);
   1.154 +	}
   1.155 +
   1.156 +	icmd->pfnCmd=function;
   1.157 +	icmd->status=RG_VALID;
   1.158 +	// Store which plugin this is for, if we know.  We can use '0' for
   1.159 +	// unknown plugin, since plugin index starts at 1.
   1.160 +	if(iplug)
   1.161 +		icmd->plugid = iplug->index;
   1.162 +	else
   1.163 +		icmd->plugid = 0;
   1.164 +}
   1.165 +
   1.166 +
   1.167 +// Replacement for engine routine CVarRegister; called by plugins.  Rather
   1.168 +// then handing the engine the plugin's cvar structure (which is allocated
   1.169 +// in the plugin DLL), this hands the engine a cvar structure allocated
   1.170 +// locally (in the metamod DLL).
   1.171 +//
   1.172 +// The cvar handed to the engine is globally allocated in the metamod.dll;
   1.173 +// the "name" and "string" fields are strdup()'s of the plugin's strings.
   1.174 +// Note that, once this is done, the cvar_t allocated in the plugin is no
   1.175 +// longer used for _anything_.  As long as everything sets/gets the cvar
   1.176 +// values via the engine functions, this will work fine.  If the plugin
   1.177 +// code tries to _directly_ read/set the fields of its own cvar structures,
   1.178 +// it will fail to work properly.
   1.179 +void meta_CVarRegister(cvar_t *pCvar) {
   1.180 +	MPlugin *iplug=NULL;
   1.181 +	MRegCvar *icvar=NULL;
   1.182 +
   1.183 +	META_DEBUG(4, ("called: meta_CVarRegister; name=%s", pCvar->name));
   1.184 +
   1.185 +	// try to find which plugin is registering this cvar
   1.186 +	if(!(iplug=Plugins->find_memloc((void *)pCvar))) {
   1.187 +		// if this isn't supported on this OS, don't log an error
   1.188 +		if(meta_errno != ME_OSNOTSUP)
   1.189 +			// Note: if cvar_t was malloc'd by the plugin, we can't
   1.190 +			// determine the calling plugin.  Thus, this becomes a Debug
   1.191 +			// rather than Error message.
   1.192 +			META_DEBUG(1, ("Failed to find memloc for regcvar '%s'", 
   1.193 +						pCvar->name));
   1.194 +	}
   1.195 +
   1.196 +	// See if this cvar was previously registered, ie a "reloaded" plugin.
   1.197 +	icvar=RegCvars->find(pCvar->name);
   1.198 +	if(!icvar) {
   1.199 +		// If not found, add.
   1.200 +		icvar=RegCvars->add(pCvar->name);
   1.201 +		if(!icvar) {
   1.202 +			// error details logged in add()
   1.203 +			return;
   1.204 +		}
   1.205 +		// Reset to given value
   1.206 +		icvar->set(pCvar);
   1.207 +		CVAR_REGISTER(icvar->data);
   1.208 +	}
   1.209 +	// Note: if not a new cvar, then we don't set the values, and just keep
   1.210 +	// the pre-existing value.
   1.211 +
   1.212 +	icvar->status=RG_VALID;
   1.213 +	// Store which plugin this is for, if we know.  Use '0' for unknown
   1.214 +	// plugin, as plugin index starts at 1.
   1.215 +	if(iplug)
   1.216 +		icvar->plugid = iplug->index;
   1.217 +	else
   1.218 +		icvar->plugid = 0;
   1.219 +}
   1.220 +
   1.221 +
   1.222 +// Replacement for engine routine RegUserMsg; called by plugins.  Rather
   1.223 +// than handing the engine the plugin's string (which is allocated in the
   1.224 +// plugin DLL), this hands the engine a string allocated from the stack.
   1.225 +//
   1.226 +// Note that while the above two functions maintain a list of registered
   1.227 +// strings/funcs/cvars from the plugins, this is not done here as I
   1.228 +// couldn't identify a need for it.  Since the engine function merely maps
   1.229 +// a string to an int (msgid) for subsequent use in MessageBegin/etc,
   1.230 +// there's no function that needs to be provided and caught (like the
   1.231 +// commands) nor any useful actions to perform upon plugin unload (like the
   1.232 +// commands and cvars).  This merely provides differently located storage
   1.233 +// for the string.
   1.234 +
   1.235 +int meta_RegUserMsg(const char *pszName, int iSize) {
   1.236 +	char *cp;
   1.237 +
   1.238 +	cp=strdup(pszName);
   1.239 +	return(REG_USER_MSG(cp, iSize));
   1.240 +}