1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/wdmisc_plugin/wdmisc.cpp Tue Apr 06 07:36:14 2004 +0000
1.3 @@ -0,0 +1,209 @@
1.4 +// vi: set ts=4 sw=4 :
1.5 +// vim: set tw=75 :
1.6 +
1.7 +// plugin.cpp - generic plugin operation
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 +#include <extdll.h> // always
1.41 +
1.42 +#include <meta_api.h> //
1.43 +#include <vdate.h> // COMPILE_TIME,
1.44 +#include <support_meta.h> // STRNCPY,
1.45 +
1.46 +#include "wdmisc.h" // me
1.47 +#include "plugin.h" // generic routines
1.48 +#include "log_plugin.h" // MAX_LOGMSG_LEN
1.49 +
1.50 +
1.51 +// Must provide at least one of these..
1.52 +META_FUNCTIONS gMetaFunctionTable = {
1.53 + NULL, // pfnGetEntityAPI HL SDK; called before game DLL
1.54 + NULL, // pfnGetEntityAPI_Post META; called after game DLL
1.55 + GetEntityAPI2, // pfnGetEntityAPI2 HL SDK2; called before game DLL
1.56 + NULL, // pfnGetEntityAPI2_Post META; called after game DLL
1.57 + NULL, // pfnGetNewDLLFunctions HL SDK2; called before game DLL
1.58 + NULL, // pfnGetNewDLLFunctions_Post META; called after game DLL
1.59 + GetEngineFunctions, // pfnGetEngineFunctions META; called before HL engine
1.60 + NULL, // pfnGetEngineFunctions_Post META; called after HL engine
1.61 +};
1.62 +
1.63 +
1.64 +cvar_t init_plugin_debug = {"wd_debug", "0", FCVAR_EXTDLL, 0, NULL};
1.65 +cvar_t bounce = {"bounce", "0", FCVAR_EXTDLL, 0, NULL};
1.66 +
1.67 +
1.68 +// Meta_Query, any desired checks for valid use.
1.69 +int plugin_query(void) {
1.70 + return(TRUE);
1.71 +}
1.72 +
1.73 +// Meta_Attach. Plugin startup. Register commands and cvars.
1.74 +int plugin_attach(void) {
1.75 + log_init("wd_debug");
1.76 +
1.77 + bounce_init();
1.78 + do_my_getengfuncs();
1.79 + REG_SVR_COMMAND("msglist", &wd_msglist);
1.80 + REG_SVR_COMMAND("msgid", &wd_msgid);
1.81 + REG_SVR_COMMAND("testit", &wd_testit);
1.82 +
1.83 + return(TRUE);
1.84 +}
1.85 +
1.86 +// Meta_Detach. Cleaning up.
1.87 +int plugin_detach(void) {
1.88 + return(TRUE);
1.89 +}
1.90 +
1.91 +
1.92 +void bounce_init(void) {
1.93 + CVAR_REGISTER(&bounce);
1.94 +}
1.95 +
1.96 +void bounce_check(void) {
1.97 + if((int)CVAR_GET_FLOAT("bounce")==1) {
1.98 + LOG_MESSAGE(PLID, "Exiting server at end of map (bounce==1)");
1.99 + SERVER_COMMAND("quit\n");
1.100 + }
1.101 +}
1.102 +
1.103 +void do_my_getengfuncs(void) {
1.104 + memset(&meta_engfuncs, 0, sizeof(meta_engfuncs));
1.105 + meta_engfuncs.pfnAlertMessage = wd_AlertMessage;
1.106 +}
1.107 +
1.108 +// The below was done basically as a proof-of-concept for catching certain
1.109 +// game-events that aren't otherwise triggered by the HL API. It works only
1.110 +// by examining every log message that the server generates, and checking
1.111 +// to see if it includes the string indicating the particular event in
1.112 +// question (in this case "Planted_The_Bomb").
1.113 +//
1.114 +// Although this works, it is really a rather poor solution to the problem,
1.115 +// as it assumes a lot of string-compares, which can seriously degrade the
1.116 +// performance of the server. I did try to take as many short-cuts as
1.117 +// reasonable in the code, to decrease the amount of string-comparing going
1.118 +// on, which makes the code a bit less readable than doing just a "strstr"
1.119 +// on the log line.
1.120 +
1.121 +void wd_AlertMessage(ALERT_TYPE atype, char *szFmt, ...) {
1.122 + va_list ap;
1.123 + static char buf[MAX_LOGMSG_LEN];
1.124 + char *cp, *ep;
1.125 + int found=0;
1.126 + size_t len=0;
1.127 +
1.128 + if(atype != at_logged)
1.129 + RETURN_META(MRES_IGNORED);
1.130 +
1.131 + va_start(ap, szFmt);
1.132 + vsnprintf(buf, sizeof(buf), szFmt, ap);
1.133 + va_end(ap);
1.134 +
1.135 + // Team "...." triggered ...
1.136 + if((cp=buf) && (cp[0]=='T')
1.137 + && (cp=strchr(cp+1, '"'))
1.138 + && (cp=strchr(cp+1, '"'))
1.139 + && !strncmp(cp+2, "triggered", 9))
1.140 + {
1.141 + cp+=2;
1.142 + found=1;
1.143 + }
1.144 + // World triggered ...
1.145 + else if((cp=buf) && (cp[0]=='W')
1.146 + && !strncmp(cp+6, "triggered", 9))
1.147 + {
1.148 + cp+=6;
1.149 + found=1;
1.150 + }
1.151 + // "player<444><44444><...>" triggered ...
1.152 + else if((cp=buf) && (cp[0]=='"')
1.153 + && (cp=strchr(cp+1, '"'))
1.154 + && !strncmp(cp+2, "triggered", 9))
1.155 + {
1.156 + cp+=2;
1.157 + found=1;
1.158 + }
1.159 +
1.160 + if(found) {
1.161 + // past 'triggered "'
1.162 + cp+=11;
1.163 + if((ep=strchr(cp, '"')))
1.164 + len=ep-cp-1;
1.165 + }
1.166 +
1.167 + if(len && !strncmp(cp, "Planted_The_Bomb", len)) {
1.168 + char name[128];
1.169 + char *beg, *end;
1.170 + beg=buf + 1;
1.171 + end=strchr(buf, '<') + 1;
1.172 + if(!beg || !end)
1.173 + RETURN_META(MRES_IGNORED);
1.174 + len=end-beg;
1.175 + if(len > sizeof(name))
1.176 + len=sizeof(name);
1.177 + STRNCPY(name, beg, len);
1.178 + CENTER_SAY(PLID, "%s set up us the bomb!", name);
1.179 + }
1.180 + RETURN_META(MRES_HANDLED);
1.181 +}
1.182 +
1.183 +void wd_msglist(void) {
1.184 + int i, size;
1.185 + const char *cp;
1.186 + LOG_CONSOLE(PLID, "registered user msgs:");
1.187 + for(i=0; i < MAX_REG_MSGS; i++) {
1.188 + cp=GET_USER_MSG_NAME(PLID, i, &size);
1.189 + if(cp)
1.190 + LOG_CONSOLE(PLID, "%d %s %d", i, cp, size);
1.191 + }
1.192 +}
1.193 +
1.194 +void wd_msgid(void) {
1.195 + const char *msgname;
1.196 + int id;
1.197 + msgname=CMD_ARGV(1);
1.198 + id=GET_USER_MSG_ID(PLID, msgname, NULL);
1.199 + if(id)
1.200 + LOG_CONSOLE(PLID, "%s = %d", msgname, id);
1.201 + else
1.202 + LOG_CONSOLE(PLID, "msg not found: %s", msgname);
1.203 +}
1.204 +
1.205 +void wd_testit(void) {
1.206 + cvar_t *cv;
1.207 + cv=CVAR_GET_POINTER("csguard_version");
1.208 + if(cv)
1.209 + LOG_CONSOLE(PLID, "csguard_version found!: %d", cv);
1.210 + else
1.211 + LOG_CONSOLE(PLID, "csguard_version not found!");
1.212 +}