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