metamod/reg_support.cpp
author Scott Ehlert <ds@alliedmods.net>
Tue Feb 12 21:06:32 2013 -0600 (2013-02-12)
changeset 265 9e61d0d70e9a
parent 248 f72386298cba
permissions -rw-r--r--
Const-qualified more char pointers for GCC4 and clang support.
darope@0
     1
// vi: set ts=4 sw=4 :
darope@0
     2
// vim: set tw=75 :
darope@0
     3
darope@0
     4
// reg_support.cpp - support for things "registered" by plugins (console
darope@0
     5
//                   cmds, cvars, msgs, etc)
darope@0
     6
darope@0
     7
/*
darope@0
     8
 * Copyright (c) 2001-2003 Will Day <[email protected]>
darope@0
     9
 *
darope@0
    10
 *    This file is part of Metamod.
darope@0
    11
 *
darope@0
    12
 *    Metamod is free software; you can redistribute it and/or modify it
darope@0
    13
 *    under the terms of the GNU General Public License as published by the
darope@0
    14
 *    Free Software Foundation; either version 2 of the License, or (at
darope@0
    15
 *    your option) any later version.
darope@0
    16
 *
darope@0
    17
 *    Metamod is distributed in the hope that it will be useful, but
darope@0
    18
 *    WITHOUT ANY WARRANTY; without even the implied warranty of
darope@0
    19
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
darope@0
    20
 *    General Public License for more details.
darope@0
    21
 *
darope@0
    22
 *    You should have received a copy of the GNU General Public License
darope@0
    23
 *    along with Metamod; if not, write to the Free Software Foundation,
darope@0
    24
 *    Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
darope@0
    25
 *
darope@0
    26
 *    In addition, as a special exception, the author gives permission to
darope@0
    27
 *    link the code of this program with the Half-Life Game Engine ("HL
darope@0
    28
 *    Engine") and Modified Game Libraries ("MODs") developed by Valve,
darope@0
    29
 *    L.L.C ("Valve").  You must obey the GNU General Public License in all
darope@0
    30
 *    respects for all of the code used other than the HL Engine and MODs
darope@0
    31
 *    from Valve.  If you modify this file, you may extend this exception
darope@0
    32
 *    to your version of the file, but you are not obligated to do so.  If
darope@0
    33
 *    you do not wish to do so, delete this exception statement from your
darope@0
    34
 *    version.
darope@0
    35
 *
darope@0
    36
 */
darope@0
    37
darope@0
    38
#ifdef linux
darope@0
    39
// enable extra routines in system header files, like strsignal
darope@242
    40
#  ifndef _GNU_SOURCE
darope@242
    41
#    define _GNU_SOURCE
darope@242
    42
#  endif
darope@0
    43
#endif /* linux */
darope@0
    44
darope@0
    45
#include <string.h>			// strsignal, etc
darope@0
    46
darope@0
    47
#include <extdll.h>			// always
darope@0
    48
#include "sdk_util.h"		// REG_SVR_COMMAND, etc
darope@0
    49
darope@0
    50
#include "reg_support.h"	// me
darope@248
    51
#include "metamod.h"		// RegCmds, g_Players, etc
darope@0
    52
#include "log_meta.h"		// META_ERROR, etc
darope@0
    53
darope@0
    54
// "Register" support.
darope@0
    55
//
darope@0
    56
// This code is necessary to support the different "register" engine
darope@0
    57
// functions that can be called by plugins, so that the plugins can be
darope@0
    58
// safely unloaded without incurring segmentation faults.
darope@0
    59
//
darope@0
    60
// The problem was that, when a plugin called, say:
darope@0
    61
//    REG_SVR_COMMAND("stub_version", svr_stub_version);
darope@0
    62
//
darope@0
    63
// the engine stored the address to the "stub_version" string, as well as,
darope@0
    64
// of course, the svr_stub_version() function pointer.  If the plugin was
darope@0
    65
// unloaded, and dlclose()'d, the memory locations for both the string and
darope@0
    66
// function became invalid and unavailable.  If the engine tried to
darope@0
    67
// strcmp() against the string, or tried to call the function pointer, it
darope@0
    68
// generated a segfault and killed the process.
darope@0
    69
//
darope@0
    70
// The solution used here is to create strings and functions allocated
darope@0
    71
// locally, in metamod.dll, which won't become unavailable or invalid due
darope@0
    72
// to a dlclose().  These local copies are passed to the engine instead of
darope@0
    73
// those allocated in the plugin DLLs.  Thus, when a plugin is later
darope@0
    74
// unloaded and dlclose'd, the engine can still reference the memory
darope@0
    75
// locations of registered strings and functions, without segfaulting.
darope@0
    76
//
darope@0
    77
// Now, what happens with a registered string or function after a plugin is
darope@0
    78
// unloaded not quite as nice as I'd like.
darope@0
    79
//
darope@0
    80
// Console commands will print a message along the lines of:
darope@0
    81
//    [metamod: command '%s' unavailable; plugin unloaded]
darope@0
    82
//
darope@0
    83
// Cvars though, have their value unaffected.  This can be convenient when
darope@0
    84
// reloading a plugin, as the cvars keep their old values.  However it can
darope@0
    85
// also be confusing, as there is no indication when viewing/setting cvars
darope@0
    86
// from the console that they no longer have an effect.
darope@0
    87
//
darope@0
    88
// Also note, the console commands for listing registered cmds and cvars
darope@0
    89
// will try to show the name of the associated plugin.  However, this only
darope@0
    90
// works under linux; under windows it will say "(unknown)".  Linux
darope@0
    91
// provides a routine "dladdr()" which can indicate (during Reg request)
darope@0
    92
// which plugin it's coming from, and this is stored with the cvar entry.
darope@0
    93
// Windows didn't seem to have a similar routine, and I couldn't find
darope@0
    94
// another way to get the information..
darope@0
    95
darope@0
    96
darope@0
    97
// Generic command handler, passed to the engine for any AddServerCommand
darope@0
    98
// calls made by the plugin.  It finds the appropriate plugin function
darope@0
    99
// pointer to call based on CMD_ARGV(0).
darope@0
   100
void meta_command_handler(void) {
darope@0
   101
	MRegCmd *icmd;
darope@0
   102
	const char *cmd;
darope@0
   103
darope@0
   104
	META_DEBUG(5, ("called: meta_command_handler; arg0=%s args='%s'", CMD_ARGV(0), CMD_ARGS()));
darope@0
   105
	cmd=CMD_ARGV(0);
darope@0
   106
	if(!cmd) {
darope@0
   107
		META_ERROR("Null command name in meta_command_handler() ??");
darope@0
   108
		return;
darope@0
   109
	}
darope@0
   110
darope@0
   111
	icmd=RegCmds->find(cmd);
darope@0
   112
	if(!icmd) {
darope@0
   113
		META_ERROR("Couldn't find registered plugin command: %s", cmd);
darope@0
   114
		return;
darope@0
   115
	}
darope@0
   116
	if(icmd->call() != mTRUE)
darope@0
   117
		META_CONS("[metamod: command '%s' unavailable; plugin unloaded]", cmd);
darope@0
   118
}
darope@0
   119
darope@0
   120
darope@0
   121
// Replacement for engine routine AddServerCommand; called by plugins.
darope@0
   122
// Rather then handing the engine the plugin's command string and function
darope@0
   123
// pointer (both of which are allocated in the plugin DLL), this hands the
darope@0
   124
// engine a command string and function pointer allocated locally (in the
darope@0
   125
// metamod DLL).
darope@0
   126
//
darope@0
   127
// The string handed to the engine is just a strdup() of the plugin's
darope@0
   128
// string.  The function pointer handed to the engine is actually a pointer
darope@0
   129
// to a generic command-handler function (see above).
ds@265
   130
void meta_AddServerCommand(const char *cmd_name, void (*function) (void)) {
darope@0
   131
	MPlugin *iplug=NULL; MRegCmd *icmd=NULL;
darope@0
   132
darope@0
   133
	META_DEBUG(4, ("called: meta_AddServerCommand; cmd_name=%s, function=%d", cmd_name, function));
darope@0
   134
darope@0
   135
	// try to find which plugin is registering this command
darope@0
   136
	if(!(iplug=Plugins->find_memloc((void *)function))) {
darope@0
   137
		// if this isn't supported on this OS, don't log an error
darope@0
   138
		if(meta_errno != ME_OSNOTSUP)
darope@0
   139
			META_ERROR("Failed to find memloc for regcmd '%s'", cmd_name);
darope@0
   140
	}
darope@0
   141
darope@0
   142
	// See if this command was previously registered, ie a "reloaded" plugin.
darope@0
   143
	icmd=RegCmds->find(cmd_name);
darope@0
   144
	if(!icmd) {
darope@0
   145
		// If not found, add.
darope@0
   146
		icmd=RegCmds->add(cmd_name);
darope@0
   147
		if(!icmd) {
darope@0
   148
			// error details logged in add()
darope@0
   149
			return;
darope@0
   150
		}
darope@0
   151
		// Only register if not previously registered..
darope@0
   152
		REG_SVR_COMMAND(icmd->name, meta_command_handler);
darope@0
   153
	}
darope@0
   154
darope@0
   155
	icmd->pfnCmd=function;
darope@0
   156
	icmd->status=RG_VALID;
darope@0
   157
	// Store which plugin this is for, if we know.  We can use '0' for
darope@0
   158
	// unknown plugin, since plugin index starts at 1.
darope@0
   159
	if(iplug)
darope@0
   160
		icmd->plugid = iplug->index;
darope@0
   161
	else
darope@0
   162
		icmd->plugid = 0;
darope@0
   163
}
darope@0
   164
darope@0
   165
darope@0
   166
// Replacement for engine routine CVarRegister; called by plugins.  Rather
darope@0
   167
// then handing the engine the plugin's cvar structure (which is allocated
darope@0
   168
// in the plugin DLL), this hands the engine a cvar structure allocated
darope@0
   169
// locally (in the metamod DLL).
darope@0
   170
//
darope@0
   171
// The cvar handed to the engine is globally allocated in the metamod.dll;
darope@0
   172
// the "name" and "string" fields are strdup()'s of the plugin's strings.
darope@0
   173
// Note that, once this is done, the cvar_t allocated in the plugin is no
darope@0
   174
// longer used for _anything_.  As long as everything sets/gets the cvar
darope@0
   175
// values via the engine functions, this will work fine.  If the plugin
darope@0
   176
// code tries to _directly_ read/set the fields of its own cvar structures,
darope@0
   177
// it will fail to work properly.
darope@0
   178
void meta_CVarRegister(cvar_t *pCvar) {
darope@0
   179
	MPlugin *iplug=NULL;
darope@0
   180
	MRegCvar *icvar=NULL;
darope@0
   181
darope@0
   182
	META_DEBUG(4, ("called: meta_CVarRegister; name=%s", pCvar->name));
darope@0
   183
darope@0
   184
	// try to find which plugin is registering this cvar
darope@0
   185
	if(!(iplug=Plugins->find_memloc((void *)pCvar))) {
darope@0
   186
		// if this isn't supported on this OS, don't log an error
darope@0
   187
		if(meta_errno != ME_OSNOTSUP)
darope@0
   188
			// Note: if cvar_t was malloc'd by the plugin, we can't
darope@0
   189
			// determine the calling plugin.  Thus, this becomes a Debug
darope@0
   190
			// rather than Error message.
darope@0
   191
			META_DEBUG(1, ("Failed to find memloc for regcvar '%s'", 
darope@0
   192
						pCvar->name));
darope@0
   193
	}
darope@0
   194
darope@0
   195
	// See if this cvar was previously registered, ie a "reloaded" plugin.
darope@0
   196
	icvar=RegCvars->find(pCvar->name);
darope@0
   197
	if(!icvar) {
darope@0
   198
		// If not found, add.
darope@0
   199
		icvar=RegCvars->add(pCvar->name);
darope@0
   200
		if(!icvar) {
darope@0
   201
			// error details logged in add()
darope@0
   202
			return;
darope@0
   203
		}
darope@0
   204
		// Reset to given value
darope@0
   205
		icvar->set(pCvar);
darope@0
   206
		CVAR_REGISTER(icvar->data);
darope@0
   207
	}
darope@0
   208
	// Note: if not a new cvar, then we don't set the values, and just keep
darope@0
   209
	// the pre-existing value.
darope@0
   210
darope@0
   211
	icvar->status=RG_VALID;
darope@0
   212
	// Store which plugin this is for, if we know.  Use '0' for unknown
darope@0
   213
	// plugin, as plugin index starts at 1.
darope@0
   214
	if(iplug)
darope@0
   215
		icvar->plugid = iplug->index;
darope@0
   216
	else
darope@0
   217
		icvar->plugid = 0;
darope@0
   218
}
darope@0
   219
darope@0
   220
darope@0
   221
// Replacement for engine routine RegUserMsg; called by plugins.  Rather
darope@0
   222
// than handing the engine the plugin's string (which is allocated in the
darope@0
   223
// plugin DLL), this hands the engine a string allocated from the stack.
darope@0
   224
//
darope@0
   225
// Note that while the above two functions maintain a list of registered
darope@0
   226
// strings/funcs/cvars from the plugins, this is not done here as I
darope@0
   227
// couldn't identify a need for it.  Since the engine function merely maps
darope@0
   228
// a string to an int (msgid) for subsequent use in MessageBegin/etc,
darope@0
   229
// there's no function that needs to be provided and caught (like the
darope@0
   230
// commands) nor any useful actions to perform upon plugin unload (like the
darope@0
   231
// commands and cvars).  This merely provides differently located storage
darope@0
   232
// for the string.
darope@0
   233
darope@0
   234
int meta_RegUserMsg(const char *pszName, int iSize) {
darope@0
   235
	char *cp;
darope@0
   236
darope@0
   237
	cp=strdup(pszName);
darope@0
   238
	return(REG_USER_MSG(cp, iSize));
darope@0
   239
}
darope@248
   240
darope@248
   241
// Intercept and record queries
darope@248
   242
void meta_QueryClientCvarValue(const edict_t *player, const char *cvarName)
darope@248
   243
{
darope@248
   244
	g_Players.set_player_cvar_query(player, cvarName);
darope@248
   245
darope@248
   246
	(*g_engfuncs.pfnQueryClientCvarValue)(player, cvarName);
darope@248
   247
}
darope@248
   248
darope@248
   249