wdmisc_plugin/wdmisc.cpp
author darope
Tue Apr 06 07:36:14 2004 +0000 (2004-04-06)
changeset 0 d15304fcb7a7
child 265 9e61d0d70e9a
permissions -rw-r--r--
Initial revision
     1 // vi: set ts=4 sw=4 :
     2 // vim: set tw=75 :
     3 
     4 // plugin.cpp - generic plugin operation
     5 
     6 /*
     7  * Copyright (c) 2001-2003 Will Day <[email protected]>
     8  *
     9  *    This file is part of Metamod.
    10  *
    11  *    Metamod is free software; you can redistribute it and/or modify it
    12  *    under the terms of the GNU General Public License as published by the
    13  *    Free Software Foundation; either version 2 of the License, or (at
    14  *    your option) any later version.
    15  *
    16  *    Metamod is distributed in the hope that it will be useful, but
    17  *    WITHOUT ANY WARRANTY; without even the implied warranty of
    18  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    19  *    General Public License for more details.
    20  *
    21  *    You should have received a copy of the GNU General Public License
    22  *    along with Metamod; if not, write to the Free Software Foundation,
    23  *    Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    24  *
    25  *    In addition, as a special exception, the author gives permission to
    26  *    link the code of this program with the Half-Life Game Engine ("HL
    27  *    Engine") and Modified Game Libraries ("MODs") developed by Valve,
    28  *    L.L.C ("Valve").  You must obey the GNU General Public License in all
    29  *    respects for all of the code used other than the HL Engine and MODs
    30  *    from Valve.  If you modify this file, you may extend this exception
    31  *    to your version of the file, but you are not obligated to do so.  If
    32  *    you do not wish to do so, delete this exception statement from your
    33  *    version.
    34  *
    35  */
    36 
    37 #include <extdll.h>			// always
    38 
    39 #include <meta_api.h>		// 
    40 #include <vdate.h>			// COMPILE_TIME,
    41 #include <support_meta.h>	// STRNCPY,
    42 
    43 #include "wdmisc.h"			// me
    44 #include "plugin.h"			// generic routines
    45 #include "log_plugin.h"		// MAX_LOGMSG_LEN
    46 
    47 
    48 // Must provide at least one of these..
    49 META_FUNCTIONS gMetaFunctionTable = {
    50 	NULL,			// pfnGetEntityAPI				HL SDK; called before game DLL
    51 	NULL,			// pfnGetEntityAPI_Post			META; called after game DLL
    52 	GetEntityAPI2,	// pfnGetEntityAPI2				HL SDK2; called before game DLL
    53 	NULL,			// pfnGetEntityAPI2_Post		META; called after game DLL
    54 	NULL,			// pfnGetNewDLLFunctions		HL SDK2; called before game DLL
    55 	NULL,			// pfnGetNewDLLFunctions_Post	META; called after game DLL
    56 	GetEngineFunctions,	// pfnGetEngineFunctions	META; called before HL engine
    57 	NULL,			// pfnGetEngineFunctions_Post	META; called after HL engine
    58 };
    59 
    60 
    61 cvar_t init_plugin_debug = {"wd_debug", "0", FCVAR_EXTDLL, 0, NULL};
    62 cvar_t bounce = {"bounce", "0", FCVAR_EXTDLL, 0, NULL};
    63 
    64 
    65 // Meta_Query, any desired checks for valid use.
    66 int plugin_query(void) {
    67 	return(TRUE);
    68 }
    69 
    70 // Meta_Attach.  Plugin startup.  Register commands and cvars.
    71 int plugin_attach(void) {
    72 	log_init("wd_debug");
    73 
    74 	bounce_init();
    75 	do_my_getengfuncs();
    76 	REG_SVR_COMMAND("msglist", &wd_msglist);
    77 	REG_SVR_COMMAND("msgid", &wd_msgid);
    78 	REG_SVR_COMMAND("testit", &wd_testit);
    79 
    80 	return(TRUE);
    81 }
    82 
    83 // Meta_Detach.  Cleaning up.
    84 int plugin_detach(void) {
    85 	return(TRUE);
    86 }
    87 
    88 
    89 void bounce_init(void) {
    90 	CVAR_REGISTER(&bounce);
    91 }
    92 
    93 void bounce_check(void) {
    94 	if((int)CVAR_GET_FLOAT("bounce")==1) {
    95 		LOG_MESSAGE(PLID, "Exiting server at end of map (bounce==1)");
    96 		SERVER_COMMAND("quit\n");
    97 	}
    98 }
    99 
   100 void do_my_getengfuncs(void) {
   101 	memset(&meta_engfuncs, 0, sizeof(meta_engfuncs));
   102 	meta_engfuncs.pfnAlertMessage = wd_AlertMessage;
   103 }
   104 
   105 // The below was done basically as a proof-of-concept for catching certain
   106 // game-events that aren't otherwise triggered by the HL API. It works only
   107 // by examining every log message that the server generates, and checking
   108 // to see if it includes the string indicating the particular event in
   109 // question (in this case "Planted_The_Bomb"). 
   110 // 
   111 // Although this works, it is really a rather poor solution to the problem,
   112 // as it assumes a lot of string-compares, which can seriously degrade the
   113 // performance of the server. I did try to take as many short-cuts as
   114 // reasonable in the code, to decrease the amount of string-comparing going
   115 // on, which makes the code a bit less readable than doing just a "strstr"
   116 // on the log line. 
   117 
   118 void wd_AlertMessage(ALERT_TYPE atype, char *szFmt, ...) {
   119 	va_list ap;
   120 	static char buf[MAX_LOGMSG_LEN];
   121 	char *cp, *ep;
   122 	int found=0;
   123 	size_t len=0;
   124 
   125 	if(atype != at_logged)
   126 		RETURN_META(MRES_IGNORED);
   127 
   128 	va_start(ap, szFmt);
   129 	vsnprintf(buf, sizeof(buf), szFmt, ap);
   130 	va_end(ap);
   131 
   132 	// Team "...." triggered ...
   133 	if((cp=buf) && (cp[0]=='T') 
   134 			&& (cp=strchr(cp+1, '"')) 
   135 			&& (cp=strchr(cp+1, '"'))
   136 			&& !strncmp(cp+2, "triggered", 9))
   137 	{
   138 		cp+=2;
   139 		found=1;
   140 	}
   141 	// World triggered ...
   142 	else if((cp=buf) && (cp[0]=='W') 
   143 			&& !strncmp(cp+6, "triggered", 9))
   144 	{
   145 		cp+=6;
   146 		found=1;
   147 	}
   148 	// "player<444><44444><...>" triggered ...
   149 	else if((cp=buf) && (cp[0]=='"') 
   150 			&& (cp=strchr(cp+1, '"')) 
   151 			&& !strncmp(cp+2, "triggered", 9))
   152 	{
   153 		cp+=2;
   154 		found=1;
   155 	}
   156 
   157 	if(found) {
   158 		// past 'triggered "'
   159 		cp+=11;
   160 		if((ep=strchr(cp, '"')))
   161 			len=ep-cp-1;
   162 	}
   163 
   164 	if(len && !strncmp(cp, "Planted_The_Bomb", len)) {
   165 		char name[128];
   166 		char *beg, *end;
   167 		beg=buf + 1;
   168 		end=strchr(buf, '<') + 1;
   169 		if(!beg || !end)
   170 			RETURN_META(MRES_IGNORED);
   171 		len=end-beg;
   172 		if(len > sizeof(name))
   173 			len=sizeof(name);
   174 		STRNCPY(name, beg, len);
   175 		CENTER_SAY(PLID, "%s set up us the bomb!", name);
   176 	}
   177 	RETURN_META(MRES_HANDLED);
   178 }
   179 
   180 void wd_msglist(void) {
   181 	int i, size;
   182 	const char *cp;
   183 	LOG_CONSOLE(PLID, "registered user msgs:");
   184 	for(i=0; i < MAX_REG_MSGS; i++) {
   185 		cp=GET_USER_MSG_NAME(PLID, i, &size);
   186 		if(cp)
   187 			LOG_CONSOLE(PLID, "%d %s %d", i, cp, size);
   188 	}
   189 }
   190 
   191 void wd_msgid(void) {
   192 	const char *msgname;
   193 	int id;
   194 	msgname=CMD_ARGV(1);
   195 	id=GET_USER_MSG_ID(PLID, msgname, NULL);
   196 	if(id)
   197 		LOG_CONSOLE(PLID, "%s = %d", msgname, id);
   198 	else
   199 		LOG_CONSOLE(PLID, "msg not found: %s", msgname);
   200 }
   201 
   202 void wd_testit(void) {
   203 	cvar_t *cv;
   204 	cv=CVAR_GET_POINTER("csguard_version");
   205 	if(cv)
   206 		LOG_CONSOLE(PLID, "csguard_version found!: %d", cv);
   207 	else
   208 		LOG_CONSOLE(PLID, "csguard_version not found!");
   209 }