metamod/reg_support.cpp
author darope
Mon Jun 28 19:47:04 2004 +0000 (2004-06-28)
branchB-work
changeset 62 ecbb47c671a4
parent 0 d15304fcb7a7
child 149 610def0c5b51
child 248 f72386298cba
permissions -rw-r--r--
Made some changes to make it compile with gcc 3.3.3
     1 // vi: set ts=4 sw=4 :
     2 // vim: set tw=75 :
     3 
     4 // reg_support.cpp - support for things "registered" by plugins (console
     5 //                   cmds, cvars, msgs, etc)
     6 
     7 /*
     8  * Copyright (c) 2001-2003 Will Day <[email protected]>
     9  *
    10  *    This file is part of Metamod.
    11  *
    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.
    16  *
    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.
    21  *
    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
    25  *
    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
    34  *    version.
    35  *
    36  */
    37 
    38 #ifdef linux
    39 // enable extra routines in system header files, like strsignal
    40 #  ifndef _GNU_SOURCE
    41 #    define _GNU_SOURCE
    42 #  endif
    43 #endif /* linux */
    44 
    45 #include <string.h>			// strsignal, etc
    46 
    47 #include <extdll.h>			// always
    48 #include "sdk_util.h"		// REG_SVR_COMMAND, etc
    49 
    50 #include "reg_support.h"	// me
    51 #include "metamod.h"		// RegCmds, etc
    52 #include "log_meta.h"		// META_ERROR, etc
    53 
    54 // "Register" support.
    55 //
    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.
    59 //
    60 // The problem was that, when a plugin called, say:
    61 //    REG_SVR_COMMAND("stub_version", svr_stub_version);
    62 //
    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.
    69 //
    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.
    76 //
    77 // Now, what happens with a registered string or function after a plugin is
    78 // unloaded not quite as nice as I'd like.
    79 //
    80 // Console commands will print a message along the lines of:
    81 //    [metamod: command '%s' unavailable; plugin unloaded]
    82 //
    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.
    87 //
    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..
    95 
    96 
    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) {
   101 	MRegCmd *icmd;
   102 	const char *cmd;
   103 
   104 	META_DEBUG(5, ("called: meta_command_handler; arg0=%s args='%s'", CMD_ARGV(0), CMD_ARGS()));
   105 	cmd=CMD_ARGV(0);
   106 	if(!cmd) {
   107 		META_ERROR("Null command name in meta_command_handler() ??");
   108 		return;
   109 	}
   110 
   111 	icmd=RegCmds->find(cmd);
   112 	if(!icmd) {
   113 		META_ERROR("Couldn't find registered plugin command: %s", cmd);
   114 		return;
   115 	}
   116 	if(icmd->call() != mTRUE)
   117 		META_CONS("[metamod: command '%s' unavailable; plugin unloaded]", cmd);
   118 }
   119 
   120 
   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
   125 // metamod DLL).
   126 //
   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;
   132 
   133 	META_DEBUG(4, ("called: meta_AddServerCommand; cmd_name=%s, function=%d", cmd_name, function));
   134 
   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);
   140 	}
   141 
   142 	// See if this command was previously registered, ie a "reloaded" plugin.
   143 	icmd=RegCmds->find(cmd_name);
   144 	if(!icmd) {
   145 		// If not found, add.
   146 		icmd=RegCmds->add(cmd_name);
   147 		if(!icmd) {
   148 			// error details logged in add()
   149 			return;
   150 		}
   151 		// Only register if not previously registered..
   152 		REG_SVR_COMMAND(icmd->name, meta_command_handler);
   153 	}
   154 
   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.
   159 	if(iplug)
   160 		icmd->plugid = iplug->index;
   161 	else
   162 		icmd->plugid = 0;
   163 }
   164 
   165 
   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).
   170 //
   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) {
   179 	MPlugin *iplug=NULL;
   180 	MRegCvar *icvar=NULL;
   181 
   182 	META_DEBUG(4, ("called: meta_CVarRegister; name=%s", pCvar->name));
   183 
   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'", 
   192 						pCvar->name));
   193 	}
   194 
   195 	// See if this cvar was previously registered, ie a "reloaded" plugin.
   196 	icvar=RegCvars->find(pCvar->name);
   197 	if(!icvar) {
   198 		// If not found, add.
   199 		icvar=RegCvars->add(pCvar->name);
   200 		if(!icvar) {
   201 			// error details logged in add()
   202 			return;
   203 		}
   204 		// Reset to given value
   205 		icvar->set(pCvar);
   206 		CVAR_REGISTER(icvar->data);
   207 	}
   208 	// Note: if not a new cvar, then we don't set the values, and just keep
   209 	// the pre-existing value.
   210 
   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.
   214 	if(iplug)
   215 		icvar->plugid = iplug->index;
   216 	else
   217 		icvar->plugid = 0;
   218 }
   219 
   220 
   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.
   224 //
   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
   232 // for the string.
   233 
   234 int meta_RegUserMsg(const char *pszName, int iSize) {
   235 	char *cp;
   236 
   237 	cp=strdup(pszName);
   238 	return(REG_USER_MSG(cp, iSize));
   239 }