darope@0: // vi: set ts=4 sw=4 : darope@0: // vim: set tw=75 : darope@0: darope@0: // plugin.cpp - generic plugin operation darope@0: darope@0: /* darope@0: * Copyright (c) 2001-2003 Will Day darope@0: * darope@0: * This file is part of Metamod. darope@0: * darope@0: * Metamod is free software; you can redistribute it and/or modify it darope@0: * under the terms of the GNU General Public License as published by the darope@0: * Free Software Foundation; either version 2 of the License, or (at darope@0: * your option) any later version. darope@0: * darope@0: * Metamod is distributed in the hope that it will be useful, but darope@0: * WITHOUT ANY WARRANTY; without even the implied warranty of darope@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU darope@0: * General Public License for more details. darope@0: * darope@0: * You should have received a copy of the GNU General Public License darope@0: * along with Metamod; if not, write to the Free Software Foundation, darope@0: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA darope@0: * darope@0: * In addition, as a special exception, the author gives permission to darope@0: * link the code of this program with the Half-Life Game Engine ("HL darope@0: * Engine") and Modified Game Libraries ("MODs") developed by Valve, darope@0: * L.L.C ("Valve"). You must obey the GNU General Public License in all darope@0: * respects for all of the code used other than the HL Engine and MODs darope@0: * from Valve. If you modify this file, you may extend this exception darope@0: * to your version of the file, but you are not obligated to do so. If darope@0: * you do not wish to do so, delete this exception statement from your darope@0: * version. darope@0: * darope@0: */ darope@0: darope@0: #include // always darope@0: darope@0: #include // darope@0: #include // COMPILE_TIME, darope@0: #include // STRNCPY, darope@0: darope@0: #include "wdmisc.h" // me darope@0: #include "plugin.h" // generic routines darope@0: #include "log_plugin.h" // MAX_LOGMSG_LEN darope@0: darope@0: darope@0: // Must provide at least one of these.. darope@0: META_FUNCTIONS gMetaFunctionTable = { darope@0: NULL, // pfnGetEntityAPI HL SDK; called before game DLL darope@0: NULL, // pfnGetEntityAPI_Post META; called after game DLL darope@0: GetEntityAPI2, // pfnGetEntityAPI2 HL SDK2; called before game DLL darope@0: NULL, // pfnGetEntityAPI2_Post META; called after game DLL darope@0: NULL, // pfnGetNewDLLFunctions HL SDK2; called before game DLL darope@0: NULL, // pfnGetNewDLLFunctions_Post META; called after game DLL darope@0: GetEngineFunctions, // pfnGetEngineFunctions META; called before HL engine darope@0: NULL, // pfnGetEngineFunctions_Post META; called after HL engine darope@0: }; darope@0: darope@0: darope@0: cvar_t init_plugin_debug = {"wd_debug", "0", FCVAR_EXTDLL, 0, NULL}; darope@0: cvar_t bounce = {"bounce", "0", FCVAR_EXTDLL, 0, NULL}; darope@0: darope@0: darope@0: // Meta_Query, any desired checks for valid use. darope@0: int plugin_query(void) { darope@0: return(TRUE); darope@0: } darope@0: darope@0: // Meta_Attach. Plugin startup. Register commands and cvars. darope@0: int plugin_attach(void) { darope@0: log_init("wd_debug"); darope@0: darope@0: bounce_init(); darope@0: do_my_getengfuncs(); darope@0: REG_SVR_COMMAND("msglist", &wd_msglist); darope@0: REG_SVR_COMMAND("msgid", &wd_msgid); darope@0: REG_SVR_COMMAND("testit", &wd_testit); darope@0: darope@0: return(TRUE); darope@0: } darope@0: darope@0: // Meta_Detach. Cleaning up. darope@0: int plugin_detach(void) { darope@0: return(TRUE); darope@0: } darope@0: darope@0: darope@0: void bounce_init(void) { darope@0: CVAR_REGISTER(&bounce); darope@0: } darope@0: darope@0: void bounce_check(void) { darope@0: if((int)CVAR_GET_FLOAT("bounce")==1) { darope@0: LOG_MESSAGE(PLID, "Exiting server at end of map (bounce==1)"); darope@0: SERVER_COMMAND("quit\n"); darope@0: } darope@0: } darope@0: darope@0: void do_my_getengfuncs(void) { darope@0: memset(&meta_engfuncs, 0, sizeof(meta_engfuncs)); darope@0: meta_engfuncs.pfnAlertMessage = wd_AlertMessage; darope@0: } darope@0: darope@0: // The below was done basically as a proof-of-concept for catching certain darope@0: // game-events that aren't otherwise triggered by the HL API. It works only darope@0: // by examining every log message that the server generates, and checking darope@0: // to see if it includes the string indicating the particular event in darope@0: // question (in this case "Planted_The_Bomb"). darope@0: // darope@0: // Although this works, it is really a rather poor solution to the problem, darope@0: // as it assumes a lot of string-compares, which can seriously degrade the darope@0: // performance of the server. I did try to take as many short-cuts as darope@0: // reasonable in the code, to decrease the amount of string-comparing going darope@0: // on, which makes the code a bit less readable than doing just a "strstr" darope@0: // on the log line. darope@0: ds@265: void wd_AlertMessage(ALERT_TYPE atype, const char *szFmt, ...) { darope@0: va_list ap; darope@0: static char buf[MAX_LOGMSG_LEN]; darope@0: char *cp, *ep; darope@0: int found=0; darope@0: size_t len=0; darope@0: darope@0: if(atype != at_logged) darope@0: RETURN_META(MRES_IGNORED); darope@0: darope@0: va_start(ap, szFmt); darope@0: vsnprintf(buf, sizeof(buf), szFmt, ap); darope@0: va_end(ap); darope@0: darope@0: // Team "...." triggered ... darope@0: if((cp=buf) && (cp[0]=='T') darope@0: && (cp=strchr(cp+1, '"')) darope@0: && (cp=strchr(cp+1, '"')) darope@0: && !strncmp(cp+2, "triggered", 9)) darope@0: { darope@0: cp+=2; darope@0: found=1; darope@0: } darope@0: // World triggered ... darope@0: else if((cp=buf) && (cp[0]=='W') darope@0: && !strncmp(cp+6, "triggered", 9)) darope@0: { darope@0: cp+=6; darope@0: found=1; darope@0: } darope@0: // "player<444><44444><...>" triggered ... darope@0: else if((cp=buf) && (cp[0]=='"') darope@0: && (cp=strchr(cp+1, '"')) darope@0: && !strncmp(cp+2, "triggered", 9)) darope@0: { darope@0: cp+=2; darope@0: found=1; darope@0: } darope@0: darope@0: if(found) { darope@0: // past 'triggered "' darope@0: cp+=11; darope@0: if((ep=strchr(cp, '"'))) darope@0: len=ep-cp-1; darope@0: } darope@0: darope@0: if(len && !strncmp(cp, "Planted_The_Bomb", len)) { darope@0: char name[128]; darope@0: char *beg, *end; darope@0: beg=buf + 1; darope@0: end=strchr(buf, '<') + 1; darope@0: if(!beg || !end) darope@0: RETURN_META(MRES_IGNORED); darope@0: len=end-beg; darope@0: if(len > sizeof(name)) darope@0: len=sizeof(name); darope@0: STRNCPY(name, beg, len); darope@0: CENTER_SAY(PLID, "%s set up us the bomb!", name); darope@0: } darope@0: RETURN_META(MRES_HANDLED); darope@0: } darope@0: darope@0: void wd_msglist(void) { darope@0: int i, size; darope@0: const char *cp; darope@0: LOG_CONSOLE(PLID, "registered user msgs:"); darope@0: for(i=0; i < MAX_REG_MSGS; i++) { darope@0: cp=GET_USER_MSG_NAME(PLID, i, &size); darope@0: if(cp) darope@0: LOG_CONSOLE(PLID, "%d %s %d", i, cp, size); darope@0: } darope@0: } darope@0: darope@0: void wd_msgid(void) { darope@0: const char *msgname; darope@0: int id; darope@0: msgname=CMD_ARGV(1); darope@0: id=GET_USER_MSG_ID(PLID, msgname, NULL); darope@0: if(id) darope@0: LOG_CONSOLE(PLID, "%s = %d", msgname, id); darope@0: else darope@0: LOG_CONSOLE(PLID, "msg not found: %s", msgname); darope@0: } darope@0: darope@0: void wd_testit(void) { darope@0: cvar_t *cv; darope@0: cv=CVAR_GET_POINTER("csguard_version"); darope@0: if(cv) darope@0: LOG_CONSOLE(PLID, "csguard_version found!: %d", cv); darope@0: else darope@0: LOG_CONSOLE(PLID, "csguard_version not found!"); darope@0: }