trace_plugin/meta_api.cpp
changeset 0 d15304fcb7a7
child 265 9e61d0d70e9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/trace_plugin/meta_api.cpp	Tue Apr 06 07:36:14 2004 +0000
     1.3 @@ -0,0 +1,169 @@
     1.4 +// vi: set ts=4 sw=4 :
     1.5 +// vim: set tw=75 :
     1.6 +
     1.7 +// meta_api.cpp - common implementation of Metamod's plugin interface
     1.8 +
     1.9 +// This file is intended to provide a decent common codebase for the
    1.10 +// Metamod plugins that I maintain, so everything is set up to be very
    1.11 +// generalized, with the things specific for each plugin handled in
    1.12 +// plugin.{h,cpp} and elsewhere.
    1.13 +
    1.14 +/*
    1.15 + * Copyright (c) 2001-2003 Will Day <[email protected]>
    1.16 + *
    1.17 + *    This file is part of Metamod.
    1.18 + *
    1.19 + *    Metamod is free software; you can redistribute it and/or modify it
    1.20 + *    under the terms of the GNU General Public License as published by the
    1.21 + *    Free Software Foundation; either version 2 of the License, or (at
    1.22 + *    your option) any later version.
    1.23 + *
    1.24 + *    Metamod is distributed in the hope that it will be useful, but
    1.25 + *    WITHOUT ANY WARRANTY; without even the implied warranty of
    1.26 + *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.27 + *    General Public License for more details.
    1.28 + *
    1.29 + *    You should have received a copy of the GNU General Public License
    1.30 + *    along with Metamod; if not, write to the Free Software Foundation,
    1.31 + *    Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.32 + *
    1.33 + *    In addition, as a special exception, the author gives permission to
    1.34 + *    link the code of this program with the Half-Life Game Engine ("HL
    1.35 + *    Engine") and Modified Game Libraries ("MODs") developed by Valve,
    1.36 + *    L.L.C ("Valve").  You must obey the GNU General Public License in all
    1.37 + *    respects for all of the code used other than the HL Engine and MODs
    1.38 + *    from Valve.  If you modify this file, you may extend this exception
    1.39 + *    to your version of the file, but you are not obligated to do so.  If
    1.40 + *    you do not wish to do so, delete this exception statement from your
    1.41 + *    version.
    1.42 + *
    1.43 + */
    1.44 +
    1.45 +#include <extdll.h>
    1.46 +
    1.47 +#include <dllapi.h>
    1.48 +#include <meta_api.h>
    1.49 +#include <vdate.h>
    1.50 +
    1.51 +#include "plugin.h"
    1.52 +#include "info_name.h"
    1.53 +#include "log_plugin.h"
    1.54 +
    1.55 +extern META_FUNCTIONS gMetaFunctionTable;
    1.56 +
    1.57 +// Description of plugin.
    1.58 +// (V* info from info_name.h)
    1.59 +plugin_info_t Plugin_info = {
    1.60 +	META_INTERFACE_VERSION, // ifvers
    1.61 +	VNAME,			// name
    1.62 +	VVERSION,		// version
    1.63 +	VDATE,			// date
    1.64 +	VAUTHOR,		// author
    1.65 +	VURL,			// url
    1.66 +	VLOGTAG,		// logtag
    1.67 +	PT_ANYTIME,		// loadable
    1.68 +	PT_ANYPAUSE,	// unloadable
    1.69 +};
    1.70 +
    1.71 +// Global variables from metamod.  These variable names are referenced by
    1.72 +// various macros.
    1.73 +meta_globals_t *gpMetaGlobals;		// metamod globals
    1.74 +gamedll_funcs_t *gpGamedllFuncs;	// gameDLL function tables
    1.75 +mutil_funcs_t *gpMetaUtilFuncs;		// metamod utility functions
    1.76 +
    1.77 +// Metamod requesting info about this plugin
    1.78 +//  ifvers			(given) interface_version metamod is using
    1.79 +//  pPlugInfo		(requested) struct with info about plugin
    1.80 +//  pMetaUtilFuncs	(given) table of utility functions provided by metamod
    1.81 +C_DLLEXPORT int Meta_Query(char *ifvers, plugin_info_t **pPlugInfo,
    1.82 +		mutil_funcs_t *pMetaUtilFuncs) 
    1.83 +{
    1.84 +	if ((int) CVAR_GET_FLOAT("developer") != 0)
    1.85 +		UTIL_LogPrintf("[%s] dev: called: Meta_Query; version=%s, ours=%s\n", 
    1.86 +				Plugin_info.logtag, ifvers, Plugin_info.ifvers);
    1.87 +
    1.88 +	// Check for valid pMetaUtilFuncs before we continue.
    1.89 +	if(!pMetaUtilFuncs) {
    1.90 +		UTIL_LogPrintf("[%s] ERROR: Meta_Query called with null pMetaUtilFuncs\n", Plugin_info.logtag);
    1.91 +		return(FALSE);
    1.92 +	}
    1.93 +	gpMetaUtilFuncs=pMetaUtilFuncs;
    1.94 +
    1.95 +	// Give metamod our plugin_info struct.
    1.96 +	*pPlugInfo=&Plugin_info;
    1.97 +
    1.98 +	// Check for interface version compatibility.
    1.99 +	if(!FStrEq(ifvers, Plugin_info.ifvers)) {
   1.100 +		int mmajor=0, mminor=0, pmajor=0, pminor=0;
   1.101 +		LOG_MESSAGE(PLID, "WARNING: meta-interface version mismatch; requested=%s ours=%s",
   1.102 +				Plugin_info.logtag, ifvers);
   1.103 +		// If plugin has later interface version, it's incompatible (update
   1.104 +		// metamod).
   1.105 +		sscanf(ifvers, "%d:%d", &mmajor, &mminor);
   1.106 +		sscanf(META_INTERFACE_VERSION, "%d:%d", &pmajor, &pminor);
   1.107 +		if(pmajor > mmajor || (pmajor==mmajor && pminor > mminor)) {
   1.108 +			LOG_ERROR(PLID, "metamod version is too old for this plugin; update metamod");
   1.109 +			return(FALSE);
   1.110 +		}
   1.111 +		// If plugin has older major interface version, it's incompatible
   1.112 +		// (update plugin).
   1.113 +		else if(pmajor < mmajor) {
   1.114 +			LOG_ERROR(PLID, "metamod version is incompatible with this plugin; please find a newer version of this plugin");
   1.115 +			return(FALSE);
   1.116 +		}
   1.117 +		// Minor interface is older, but this is guaranteed to be backwards
   1.118 +		// compatible, so we warn, but we still accept it.
   1.119 +		else if(pmajor==mmajor && pminor < mminor)
   1.120 +			LOG_MESSAGE(PLID, "WARNING: metamod version is newer than expected; consider finding a newer version of this plugin");
   1.121 +		else
   1.122 +			LOG_ERROR(PLID, "unexpected version comparison; metavers=%s, mmajor=%d, mminor=%d; plugvers=%s, pmajor=%d, pminor=%d", ifvers, mmajor, mminor, META_INTERFACE_VERSION, pmajor, pminor);
   1.123 +	}
   1.124 +
   1.125 +	return (plugin_query());
   1.126 +}
   1.127 +
   1.128 +// Metamod attaching plugin to the server.
   1.129 +//  now				(given) current phase, ie during map, during changelevel, or at startup
   1.130 +//  pFunctionTable	(requested) table of function tables this plugin catches
   1.131 +//  pMGlobals		(given) global vars from metamod
   1.132 +//  pGamedllFuncs	(given) copy of function tables from game dll
   1.133 +C_DLLEXPORT int Meta_Attach(PLUG_LOADTIME now, META_FUNCTIONS *pFunctionTable, 
   1.134 +		meta_globals_t *pMGlobals, gamedll_funcs_t *pGamedllFuncs)
   1.135 +{
   1.136 +	if(now > Plugin_info.loadable) {
   1.137 +		LOG_ERROR(PLID, "Can't load plugin right now");
   1.138 +		return(FALSE);
   1.139 +	}
   1.140 +	if(!pMGlobals) {
   1.141 +		LOG_ERROR(PLID, "Meta_Attach called with null pMGlobals");
   1.142 +		return(FALSE);
   1.143 +	}
   1.144 +	gpMetaGlobals=pMGlobals;
   1.145 +	if(!pFunctionTable) {
   1.146 +		LOG_ERROR(PLID, "Meta_Attach called with null pFunctionTable");
   1.147 +		return(FALSE);
   1.148 +	}
   1.149 +	memcpy(pFunctionTable, &gMetaFunctionTable, sizeof(META_FUNCTIONS));
   1.150 +	gpGamedllFuncs=pGamedllFuncs;
   1.151 +
   1.152 +	LOG_MESSAGE(PLID, "%s v%s  %s", Plugin_info.name, Plugin_info.version, 
   1.153 +			Plugin_info.date);
   1.154 +	LOG_MESSAGE(PLID, "by %s", Plugin_info.author);
   1.155 +	LOG_MESSAGE(PLID, "   %s", Plugin_info.url);
   1.156 +	LOG_MESSAGE(PLID, "compiled: %s Eastern (%s)", COMPILE_TIME, OPT_TYPE);
   1.157 +
   1.158 +	// Let's go.
   1.159 +	return(plugin_attach());
   1.160 +}
   1.161 +
   1.162 +// Metamod detaching plugin from the server.
   1.163 +// now		(given) current phase, ie during map, etc
   1.164 +// reason	(given) why detaching (refresh, console unload, forced unload, etc)
   1.165 +C_DLLEXPORT int Meta_Detach(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) {
   1.166 +	if(now > Plugin_info.unloadable && reason != PNL_CMD_FORCED) {
   1.167 +		LOG_ERROR(PLID, "Can't unload plugin right now");
   1.168 +		return(FALSE);
   1.169 +	}
   1.170 +	// Done!
   1.171 +	return(plugin_detach());
   1.172 +}