1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/metamod/mreg.cpp Tue Apr 06 07:36:14 2004 +0000
1.3 @@ -0,0 +1,511 @@
1.4 +// vi: set ts=4 sw=4 :
1.5 +// vim: set tw=75 :
1.6 +
1.7 +// mreg.cpp - functions for registered items (classes MRegCmd, MRegCmdList)
1.8 +
1.9 +/*
1.10 + * Copyright (c) 2001-2003 Will Day <[email protected]>
1.11 + *
1.12 + * This file is part of Metamod.
1.13 + *
1.14 + * Metamod is free software; you can redistribute it and/or modify it
1.15 + * under the terms of the GNU General Public License as published by the
1.16 + * Free Software Foundation; either version 2 of the License, or (at
1.17 + * your option) any later version.
1.18 + *
1.19 + * Metamod is distributed in the hope that it will be useful, but
1.20 + * WITHOUT ANY WARRANTY; without even the implied warranty of
1.21 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.22 + * General Public License for more details.
1.23 + *
1.24 + * You should have received a copy of the GNU General Public License
1.25 + * along with Metamod; if not, write to the Free Software Foundation,
1.26 + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.27 + *
1.28 + * In addition, as a special exception, the author gives permission to
1.29 + * link the code of this program with the Half-Life Game Engine ("HL
1.30 + * Engine") and Modified Game Libraries ("MODs") developed by Valve,
1.31 + * L.L.C ("Valve"). You must obey the GNU General Public License in all
1.32 + * respects for all of the code used other than the HL Engine and MODs
1.33 + * from Valve. If you modify this file, you may extend this exception
1.34 + * to your version of the file, but you are not obligated to do so. If
1.35 + * you do not wish to do so, delete this exception statement from your
1.36 + * version.
1.37 + *
1.38 + */
1.39 +
1.40 +#ifdef linux
1.41 +// enable extra routines in system header files, like strsignal
1.42 +#define _GNU_SOURCE
1.43 +#endif /* linux */
1.44 +
1.45 +#include <string.h> // strsignal, strdup, etc
1.46 +#include <errno.h> // strerror, etc
1.47 +
1.48 +#include <extdll.h> // always
1.49 +
1.50 +#include "mreg.h" // me
1.51 +#include "metamod.h" // Plugins, etc
1.52 +#include "mlist.h" // class MPluginList
1.53 +#include "mplugin.h" // class MPlugin
1.54 +#include "types_meta.h" // mBOOL
1.55 +#include "log_meta.h" // META_LOG, etc
1.56 +#include "osdep.h" // os_safe_call, etc
1.57 +
1.58 +
1.59 +///// class MRegCmd:
1.60 +
1.61 +// Init values. It would probably be more "proper" to use containers and
1.62 +// constructors, rather than arrays and init-functions.
1.63 +void MRegCmd::init(int idx)
1.64 +{
1.65 + index = idx;
1.66 + name = NULL;
1.67 + pfnCmd = NULL;
1.68 + plugid = 0;
1.69 + status = RG_INVALID;
1.70 +}
1.71 +
1.72 +// Try to call the function. Relies on OS-specific routine to attempt
1.73 +// calling the function without generating a segfault from an unloaded
1.74 +// plugin DLL.
1.75 +// meta_errno values:
1.76 +// - ME_BADREQ function disabled/invalid
1.77 +// - ME_ARGUMENT function pointer is null
1.78 +mBOOL MRegCmd::call(void) {
1.79 + mBOOL ret;
1.80 +
1.81 + // can we expect to call this function?
1.82 + if(status != RG_VALID)
1.83 + RETURN_ERRNO(mFALSE, ME_BADREQ);
1.84 + if(!pfnCmd)
1.85 + RETURN_ERRNO(mFALSE, ME_ARGUMENT);
1.86 +
1.87 + // try to call this function
1.88 + ret=os_safe_call(pfnCmd);
1.89 + if(!ret) {
1.90 + META_DEBUG(4, ("Plugin reg_cmd '%s' called after unloaded; removed from list", name));
1.91 + status=RG_INVALID;
1.92 + pfnCmd=NULL;
1.93 + // NOTE: we can't free the malloc'd space for the name, as that
1.94 + // would just re-introduce the segfault problem..
1.95 + }
1.96 + // meta_errno (if failed) is set already in os_safe_call()
1.97 + return(ret);
1.98 +}
1.99 +
1.100 +
1.101 +///// class MRegCmdList:
1.102 +
1.103 +// Constructor
1.104 +MRegCmdList::MRegCmdList(void)
1.105 + : mlist(0), size(REG_CMD_GROWSIZE), endlist(0)
1.106 +{
1.107 + int i;
1.108 + mlist = (MRegCmd *) malloc(size * sizeof(MRegCmd));
1.109 + // initialize array
1.110 + for(i=0; i< size; i++)
1.111 + mlist[i].init(i+1); // 1-based index
1.112 + endlist=0;
1.113 +}
1.114 +
1.115 +// Try to find a registered function with the given name.
1.116 +// meta_errno values:
1.117 +// - ME_NOTFOUND couldn't find a matching function
1.118 +MRegCmd *MRegCmdList::find(const char *findname) {
1.119 + int i;
1.120 + for(i=0; i < endlist; i++) {
1.121 + if(!strcasecmp(mlist[i].name, findname))
1.122 + return(&mlist[i]);
1.123 + }
1.124 + RETURN_ERRNO(NULL, ME_NOTFOUND);
1.125 +}
1.126 +
1.127 +// Add the given name to the list and return the instance. This only
1.128 +// writes the "name" to the new cmd; other fields are writtin by caller
1.129 +// (meta_AddServerCommand).
1.130 +// meta_errno values:
1.131 +// - ME_NOMEM couldn't realloc or malloc for various parts
1.132 +MRegCmd *MRegCmdList::add(const char *addname) {
1.133 + MRegCmd *icmd;
1.134 +
1.135 + if(endlist==size) {
1.136 + // grow array
1.137 + MRegCmd *temp;
1.138 + int i, newsize;
1.139 + newsize=size+REG_CMD_GROWSIZE;
1.140 + META_DEBUG(6, ("Growing reg cmd list from %d to %d", size, newsize));
1.141 + temp = (MRegCmd *) realloc(mlist, newsize*sizeof(MRegCmd));
1.142 + if(!temp) {
1.143 + META_ERROR("Couldn't grow registered command list to %d for '%s': %s", newsize, addname, strerror(errno));
1.144 + RETURN_ERRNO(NULL, ME_NOMEM);
1.145 + }
1.146 + mlist=temp;
1.147 + size=newsize;
1.148 + // initialize new (unused) entries
1.149 + for(i=endlist; i<size; i++)
1.150 + mlist[i].init(i+1); // 1-based
1.151 + }
1.152 + icmd = &mlist[endlist];
1.153 +
1.154 + // Malloc space separately for the command name, because:
1.155 + // - Can't point to memory loc in plugin (another segv waiting to
1.156 + // happen).
1.157 + // - Can't point to memory in mlist which might get moved later by
1.158 + // realloc (again, segv).
1.159 + icmd->name=strdup(addname);
1.160 + if(!icmd->name) {
1.161 + META_ERROR("Couldn't strdup for adding reg cmd name '%s': %s",
1.162 + addname, strerror(errno));
1.163 + RETURN_ERRNO(NULL, ME_NOMEM);
1.164 + }
1.165 + endlist++;
1.166 +
1.167 + return(icmd);
1.168 +}
1.169 +
1.170 +// Disable any functions belonging to the given plugin (by index id).
1.171 +void MRegCmdList::disable(int plugin_id) {
1.172 + int i;
1.173 + for(i=0; i< size; i++) {
1.174 + if(mlist[i].plugid == plugin_id)
1.175 + mlist[i].status = RG_INVALID;
1.176 + }
1.177 +}
1.178 +
1.179 +// List all the registered commands.
1.180 +void MRegCmdList::show(void) {
1.181 + int i, n=0, a=0;
1.182 + MRegCmd *icmd;
1.183 + MPlugin *iplug;
1.184 + char bplug[18+1]; // +1 for term null
1.185 +
1.186 + META_CONS("Registered plugin commands:");
1.187 + META_CONS(" %*s %-*s %-s",
1.188 + WIDTH_MAX_REG, "",
1.189 + sizeof(bplug)-1, "plugin", "command");
1.190 + for(i=0; i < endlist; i++) {
1.191 + icmd = &mlist[i];
1.192 + if(icmd->status==RG_VALID) {
1.193 + iplug=Plugins->find(icmd->plugid);
1.194 + if(iplug)
1.195 + STRNCPY(bplug, iplug->desc, sizeof(bplug));
1.196 + else
1.197 + STRNCPY(bplug, "(unknown)", sizeof(bplug));
1.198 + }
1.199 + else
1.200 + STRNCPY(bplug, "(unloaded)", sizeof(bplug));
1.201 + META_CONS(" [%*d] %-*s %-s",
1.202 + WIDTH_MAX_REG, icmd->index,
1.203 + sizeof(bplug)-1, bplug,
1.204 + icmd->name);
1.205 + if(icmd->status==RG_VALID) a++;
1.206 + n++;
1.207 + }
1.208 + META_CONS("%d commands, %d available (%d allocated)", n, a, size);
1.209 +}
1.210 +
1.211 +// List all the registered commands for the given plugin id.
1.212 +void MRegCmdList::show(int plugin_id) {
1.213 + int i, n=0;
1.214 + MRegCmd *icmd;
1.215 +
1.216 + // If OS doesn't support DLFNAME, then we can't know what the plugin's
1.217 + // registered cvars are.
1.218 + DLFNAME(NULL);
1.219 + if(meta_errno==ME_OSNOTSUP) {
1.220 + META_CONS("Registered commands: unknown (can't get info under this OS)");
1.221 + return;
1.222 + }
1.223 + META_CONS("Registered commands:");
1.224 + for(i=0; i < endlist; i++) {
1.225 + icmd = &mlist[i];
1.226 + if(icmd->plugid != plugin_id)
1.227 + continue;
1.228 + META_CONS(" %s", icmd->name);
1.229 + n++;
1.230 + }
1.231 + META_CONS("%d commands", n);
1.232 +}
1.233 +
1.234 +
1.235 +///// class MRegCvar:
1.236 +
1.237 +// Init values. It would probably be more "proper" to use containers and
1.238 +// constructors, rather than arrays and init-functions.
1.239 +void MRegCvar::init(int idx)
1.240 +{
1.241 + index = idx;
1.242 + data = NULL;
1.243 + plugid = 0;
1.244 + status = RG_INVALID;
1.245 +}
1.246 +
1.247 +// Set the cvar, copying values from given cvar.
1.248 +// meta_errno values:
1.249 +// - ME_ARGUMENT given cvar doesn't match this cvar
1.250 +mBOOL MRegCvar::set(cvar_t *src) {
1.251 + if(strcasecmp(src->name, data->name)) {
1.252 + META_ERROR("Tried to set cvar with mismatched name; src=%s dst=%s",
1.253 + src->name, data->name);
1.254 + RETURN_ERRNO(mFALSE, ME_ARGUMENT);
1.255 + }
1.256 + // Would like to free() existing string, but can't tell where it was
1.257 + // allocated...
1.258 + data->string = strdup(src->string);
1.259 + data->flags = src->flags;
1.260 + data->value = src->value;
1.261 + data->next = src->next;
1.262 + return(mTRUE);
1.263 +}
1.264 +
1.265 +
1.266 +///// class MRegCvarList:
1.267 +
1.268 +// Constructor
1.269 +MRegCvarList::MRegCvarList(void)
1.270 + : vlist(0), size(REG_CVAR_GROWSIZE), endlist(0)
1.271 +{
1.272 + int i;
1.273 + vlist = (MRegCvar *) malloc(size * sizeof(MRegCvar));
1.274 + // initialize array
1.275 + for(i=0; i< size; i++)
1.276 + vlist[i].init(i+1); // 1-based
1.277 + endlist=0;
1.278 +}
1.279 +
1.280 +// Add the given cvar name to the list and return the instance. This only
1.281 +// writes the "name" to the new cvar; other fields are written with
1.282 +// cvar::set().
1.283 +// meta_errno values:
1.284 +// - ME_NOMEM couldn't alloc or realloc for various parts
1.285 +MRegCvar *MRegCvarList::add(const char *addname) {
1.286 + MRegCvar *icvar;
1.287 +
1.288 + if(endlist==size) {
1.289 + // grow array
1.290 + MRegCvar *temp;
1.291 + int i, newsize;
1.292 + newsize=size+REG_CVAR_GROWSIZE;
1.293 + META_DEBUG(6, ("Growing reg cvar list from %d to %d", size, newsize));
1.294 + temp = (MRegCvar *) realloc(vlist, newsize*sizeof(MRegCvar));
1.295 + if(!temp) {
1.296 + META_ERROR("Couldn't grow registered cvar list to %d for '%s'; %s", newsize, addname, strerror(errno));
1.297 + RETURN_ERRNO(NULL, ME_NOMEM);
1.298 + }
1.299 + vlist=temp;
1.300 + size=newsize;
1.301 + // initialize new (unused) entries
1.302 + for(i=endlist; i<size; i++)
1.303 + vlist[i].init(i+1); // 1-based
1.304 + }
1.305 +
1.306 + icvar = &vlist[endlist];
1.307 +
1.308 + // Malloc space for the cvar and cvar name, for two reasons:
1.309 + // - Can't point to memory loc in plugin (another segv waiting to
1.310 + // happen).
1.311 + // - Can't point to memory in vlist which might get moved later by
1.312 + // realloc (again, segv).
1.313 + icvar->data = (cvar_t *) malloc(sizeof(cvar_t));
1.314 + if(!icvar->data) {
1.315 + META_ERROR("Couldn't malloc cvar for adding reg cvar name '%s': %s",
1.316 + addname, strerror(errno));
1.317 + RETURN_ERRNO(NULL, ME_NOMEM);
1.318 + }
1.319 + icvar->data->name=strdup(addname);
1.320 + if(!icvar->data->name) {
1.321 + META_ERROR("Couldn't strdup for adding reg cvar name '%s': %s",
1.322 + addname, strerror(errno));
1.323 + RETURN_ERRNO(NULL, ME_NOMEM);
1.324 + }
1.325 + endlist++;
1.326 +
1.327 + return(icvar);
1.328 +}
1.329 +
1.330 +// Try to find a registered cvar with the given name.
1.331 +// meta_errno values:
1.332 +// - ME_NOTFOUND couldn't find a matching cvar
1.333 +MRegCvar *MRegCvarList::find(const char *findname) {
1.334 + int i;
1.335 + for(i=0; i < endlist; i++) {
1.336 + if(!strcasecmp(vlist[i].data->name, findname))
1.337 + return(&vlist[i]);
1.338 + }
1.339 + RETURN_ERRNO(NULL, ME_NOTFOUND);
1.340 +}
1.341 +
1.342 +// Disable any cvars belonging to the given plugin (by index id).
1.343 +void MRegCvarList::disable(int plugin_id) {
1.344 + int i;
1.345 + MRegCvar *icvar;
1.346 + for(i=0; i< size; i++) {
1.347 + icvar=&vlist[i];
1.348 + if(icvar->plugid == plugin_id) {
1.349 + icvar->status = RG_INVALID;
1.350 + icvar->plugid = 0;
1.351 + // Decided not to do this, in order to keep pre-existing values
1.352 + // after a plugin reload.
1.353 + // CVAR_SET_STRING(icvar->data->name, "[metamod: cvar invalid; plugin unloaded]");
1.354 + }
1.355 + }
1.356 +}
1.357 +
1.358 +// List all the registered cvars.
1.359 +void MRegCvarList::show(void) {
1.360 + int i, n=0, a=0;
1.361 + MRegCvar *icvar;
1.362 + MPlugin *iplug;
1.363 + char bplug[13+1], bname[20+1], bval[15+1]; // +1 for term null
1.364 +
1.365 + META_CONS("Registered plugin cvars:");
1.366 + META_CONS(" %*s %-*s %-*s %*s %s",
1.367 + WIDTH_MAX_REG, "",
1.368 + sizeof(bplug)-1, "plugin",
1.369 + sizeof(bname)-1, "cvar",
1.370 + sizeof(bval)-1, "float value",
1.371 + "string value");
1.372 + for(i=0; i < endlist; i++) {
1.373 + icvar = &vlist[i];
1.374 + if(icvar->status==RG_VALID) {
1.375 + iplug=Plugins->find(icvar->plugid);
1.376 + if(iplug)
1.377 + STRNCPY(bplug, iplug->desc, sizeof(bplug));
1.378 + else
1.379 + STRNCPY(bplug, "(unknown)", sizeof(bplug));
1.380 + }
1.381 + else
1.382 + STRNCPY(bplug, "(unloaded)", sizeof(bplug));
1.383 + STRNCPY(bname, icvar->data->name, sizeof(bname));
1.384 + snprintf(bval, sizeof(bval), "%f", icvar->data->value);
1.385 + META_CONS(" [%*d] %-*s %-*s %*s %s",
1.386 + WIDTH_MAX_REG, icvar->index,
1.387 + sizeof(bplug)-1, bplug,
1.388 + sizeof(bname)-1, bname,
1.389 + sizeof(bval)-1, bval,
1.390 + icvar->data->string);
1.391 + if(icvar->status==RG_VALID) a++;
1.392 + n++;
1.393 + }
1.394 + META_CONS("%d cvars, %d available (%d allocated)", n, a, size);
1.395 +}
1.396 +
1.397 +// List the registered cvars for the given plugin id.
1.398 +void MRegCvarList::show(int plugin_id) {
1.399 + int i, n=0;
1.400 + MRegCvar *icvar;
1.401 + char bname[30+1], bval[15+1]; // +1 for term null
1.402 +
1.403 + // If OS doesn't support DLFNAME, then we can't know what the plugin's
1.404 + // registered cvars are.
1.405 + DLFNAME(NULL);
1.406 + if(meta_errno==ME_OSNOTSUP) {
1.407 + META_CONS("Registered cvars: unknown (can't get info under this OS)");
1.408 + return;
1.409 + }
1.410 + META_CONS("%-*s %*s %s",
1.411 + sizeof(bname)-1, "Registered cvars:",
1.412 + sizeof(bval)-1, "float value",
1.413 + "string value");
1.414 + for(i=0; i < endlist; i++) {
1.415 + icvar = &vlist[i];
1.416 + if(icvar->plugid != plugin_id)
1.417 + continue;
1.418 + STRNCPY(bname, icvar->data->name, sizeof(bname));
1.419 + snprintf(bval, sizeof(bval), "%f", icvar->data->value);
1.420 + META_CONS(" %-*s %*s %s",
1.421 + sizeof(bname)-1, bname,
1.422 + sizeof(bval)-1, bval,
1.423 + icvar->data->string);
1.424 + n++;
1.425 + }
1.426 + META_CONS("%d cvars", n);
1.427 +}
1.428 +
1.429 +
1.430 +///// class MRegMsgList:
1.431 +
1.432 +// Constructor
1.433 +MRegMsgList::MRegMsgList(void)
1.434 + : size(MAX_REG_MSGS), endlist(0)
1.435 +{
1.436 + int i;
1.437 + // initialize array
1.438 + memset(mlist, 0, sizeof(mlist));
1.439 + for(i=0; i< size; i++) {
1.440 + mlist[i].index=i+1; // 1-based
1.441 + }
1.442 + endlist=0;
1.443 +}
1.444 +
1.445 +// Add the given user msg the list and return the instance.
1.446 +// meta_errno values:
1.447 +// - ME_MAXREACHED reached max number of msgs allowed
1.448 +MRegMsg *MRegMsgList::add(const char *addname, int addmsgid, int addsize) {
1.449 + MRegMsg *imsg;
1.450 +
1.451 + if(endlist==size) {
1.452 + // all slots used
1.453 + META_ERROR("Couldn't add registered msg '%s' to list; reached max msgs (%d)",
1.454 + addname, size);
1.455 + RETURN_ERRNO(NULL, ME_MAXREACHED);
1.456 + }
1.457 +
1.458 + imsg = &mlist[endlist];
1.459 + endlist++;
1.460 +
1.461 + // Copy msg data into empty slot.
1.462 + // Note: 'addname' assumed to be a constant string allocated in the
1.463 + // gamedll.
1.464 + imsg->name=addname;
1.465 + imsg->msgid=addmsgid;
1.466 + imsg->size=addsize;
1.467 +
1.468 + return(imsg);
1.469 +}
1.470 +
1.471 +// Try to find a registered msg with the given name.
1.472 +// meta_errno values:
1.473 +// - ME_NOTFOUND couldn't find a matching cvar
1.474 +MRegMsg *MRegMsgList::find(const char *findname) {
1.475 + int i;
1.476 + for(i=0; i < endlist; i++) {
1.477 + if(!strcmp(mlist[i].name, findname))
1.478 + return(&mlist[i]);
1.479 + }
1.480 + RETURN_ERRNO(NULL, ME_NOTFOUND);
1.481 +}
1.482 +
1.483 +// Try to find a registered msg with the given msgid.
1.484 +// meta_errno values:
1.485 +// - ME_NOTFOUND couldn't find a matching cvar
1.486 +MRegMsg *MRegMsgList::find(int findmsgid) {
1.487 + int i;
1.488 + for(i=0; i < endlist; i++) {
1.489 + if(mlist[i].msgid == findmsgid)
1.490 + return(&mlist[i]);
1.491 + }
1.492 + RETURN_ERRNO(NULL, ME_NOTFOUND);
1.493 +}
1.494 +
1.495 +// List the registered usermsgs for the gamedll.
1.496 +void MRegMsgList::show(void) {
1.497 + int i, n=0;
1.498 + MRegMsg *imsg;
1.499 + char bname[25+1]; // +1 for term null
1.500 +
1.501 + META_CONS("%-*s %5s %5s",
1.502 + sizeof(bname)-1, "Game registered user msgs:", "msgid", "size");
1.503 + for(i=0; i < endlist; i++) {
1.504 + imsg = &mlist[i];
1.505 + STRNCPY(bname, imsg->name, sizeof(bname));
1.506 + META_CONS(" %-*s %3d %3d",
1.507 + sizeof(bname)-1, bname,
1.508 + imsg->msgid,
1.509 + imsg->size);
1.510 + n++;
1.511 + }
1.512 + META_CONS("%d game user msgs", n);
1.513 +}
1.514 +