trace_plugin/meta_api.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Sep 26 22:57:05 2013 -0500 (2013-09-26)
changeset 286 202d9c3207a2
parent 265 9e61d0d70e9a
permissions -rw-r--r--
Fixed warning when building stub_mm plugin.
     1 // vi: set ts=4 sw=4 :
     2 // vim: set tw=75 :
     3 
     4 // meta_api.cpp - common implementation of Metamod's plugin interface
     5 
     6 // This file is intended to provide a decent common codebase for the
     7 // Metamod plugins that I maintain, so everything is set up to be very
     8 // generalized, with the things specific for each plugin handled in
     9 // plugin.{h,cpp} and elsewhere.
    10 
    11 /*
    12  * Copyright (c) 2001-2003 Will Day <[email protected]>
    13  *
    14  *    This file is part of Metamod.
    15  *
    16  *    Metamod is free software; you can redistribute it and/or modify it
    17  *    under the terms of the GNU General Public License as published by the
    18  *    Free Software Foundation; either version 2 of the License, or (at
    19  *    your option) any later version.
    20  *
    21  *    Metamod is distributed in the hope that it will be useful, but
    22  *    WITHOUT ANY WARRANTY; without even the implied warranty of
    23  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    24  *    General Public License for more details.
    25  *
    26  *    You should have received a copy of the GNU General Public License
    27  *    along with Metamod; if not, write to the Free Software Foundation,
    28  *    Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    29  *
    30  *    In addition, as a special exception, the author gives permission to
    31  *    link the code of this program with the Half-Life Game Engine ("HL
    32  *    Engine") and Modified Game Libraries ("MODs") developed by Valve,
    33  *    L.L.C ("Valve").  You must obey the GNU General Public License in all
    34  *    respects for all of the code used other than the HL Engine and MODs
    35  *    from Valve.  If you modify this file, you may extend this exception
    36  *    to your version of the file, but you are not obligated to do so.  If
    37  *    you do not wish to do so, delete this exception statement from your
    38  *    version.
    39  *
    40  */
    41 
    42 #include <extdll.h>
    43 
    44 #include <dllapi.h>
    45 #include <meta_api.h>
    46 #include <vdate.h>
    47 
    48 #include "plugin.h"
    49 #include "info_name.h"
    50 #include "log_plugin.h"
    51 
    52 extern META_FUNCTIONS gMetaFunctionTable;
    53 
    54 // Description of plugin.
    55 // (V* info from info_name.h)
    56 plugin_info_t Plugin_info = {
    57 	META_INTERFACE_VERSION, // ifvers
    58 	VNAME,			// name
    59 	VVERSION,		// version
    60 	VDATE,			// date
    61 	VAUTHOR,		// author
    62 	VURL,			// url
    63 	VLOGTAG,		// logtag
    64 	PT_ANYTIME,		// loadable
    65 	PT_ANYPAUSE,	// unloadable
    66 };
    67 
    68 // Global variables from metamod.  These variable names are referenced by
    69 // various macros.
    70 meta_globals_t *gpMetaGlobals;		// metamod globals
    71 gamedll_funcs_t *gpGamedllFuncs;	// gameDLL function tables
    72 mutil_funcs_t *gpMetaUtilFuncs;		// metamod utility functions
    73 
    74 // Metamod requesting info about this plugin
    75 //  ifvers			(given) interface_version metamod is using
    76 //  pPlugInfo		(requested) struct with info about plugin
    77 //  pMetaUtilFuncs	(given) table of utility functions provided by metamod
    78 C_DLLEXPORT int Meta_Query(const char *ifvers, plugin_info_t **pPlugInfo,
    79 		mutil_funcs_t *pMetaUtilFuncs) 
    80 {
    81 	if ((int) CVAR_GET_FLOAT("developer") != 0)
    82 		UTIL_LogPrintf("[%s] dev: called: Meta_Query; version=%s, ours=%s\n", 
    83 				Plugin_info.logtag, ifvers, Plugin_info.ifvers);
    84 
    85 	// Check for valid pMetaUtilFuncs before we continue.
    86 	if(!pMetaUtilFuncs) {
    87 		UTIL_LogPrintf("[%s] ERROR: Meta_Query called with null pMetaUtilFuncs\n", Plugin_info.logtag);
    88 		return(FALSE);
    89 	}
    90 	gpMetaUtilFuncs=pMetaUtilFuncs;
    91 
    92 	// Give metamod our plugin_info struct.
    93 	*pPlugInfo=&Plugin_info;
    94 
    95 	// Check for interface version compatibility.
    96 	if(!FStrEq(ifvers, Plugin_info.ifvers)) {
    97 		int mmajor=0, mminor=0, pmajor=0, pminor=0;
    98 		LOG_MESSAGE(PLID, "WARNING: meta-interface version mismatch; requested=%s ours=%s",
    99 				Plugin_info.logtag, ifvers);
   100 		// If plugin has later interface version, it's incompatible (update
   101 		// metamod).
   102 		sscanf(ifvers, "%d:%d", &mmajor, &mminor);
   103 		sscanf(META_INTERFACE_VERSION, "%d:%d", &pmajor, &pminor);
   104 		if(pmajor > mmajor || (pmajor==mmajor && pminor > mminor)) {
   105 			LOG_ERROR(PLID, "metamod version is too old for this plugin; update metamod");
   106 			return(FALSE);
   107 		}
   108 		// If plugin has older major interface version, it's incompatible
   109 		// (update plugin).
   110 		else if(pmajor < mmajor) {
   111 			LOG_ERROR(PLID, "metamod version is incompatible with this plugin; please find a newer version of this plugin");
   112 			return(FALSE);
   113 		}
   114 		// Minor interface is older, but this is guaranteed to be backwards
   115 		// compatible, so we warn, but we still accept it.
   116 		else if(pmajor==mmajor && pminor < mminor)
   117 			LOG_MESSAGE(PLID, "WARNING: metamod version is newer than expected; consider finding a newer version of this plugin");
   118 		else
   119 			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);
   120 	}
   121 
   122 	return (plugin_query());
   123 }
   124 
   125 // Metamod attaching plugin to the server.
   126 //  now				(given) current phase, ie during map, during changelevel, or at startup
   127 //  pFunctionTable	(requested) table of function tables this plugin catches
   128 //  pMGlobals		(given) global vars from metamod
   129 //  pGamedllFuncs	(given) copy of function tables from game dll
   130 C_DLLEXPORT int Meta_Attach(PLUG_LOADTIME now, META_FUNCTIONS *pFunctionTable, 
   131 		meta_globals_t *pMGlobals, gamedll_funcs_t *pGamedllFuncs)
   132 {
   133 	if(now > Plugin_info.loadable) {
   134 		LOG_ERROR(PLID, "Can't load plugin right now");
   135 		return(FALSE);
   136 	}
   137 	if(!pMGlobals) {
   138 		LOG_ERROR(PLID, "Meta_Attach called with null pMGlobals");
   139 		return(FALSE);
   140 	}
   141 	gpMetaGlobals=pMGlobals;
   142 	if(!pFunctionTable) {
   143 		LOG_ERROR(PLID, "Meta_Attach called with null pFunctionTable");
   144 		return(FALSE);
   145 	}
   146 	memcpy(pFunctionTable, &gMetaFunctionTable, sizeof(META_FUNCTIONS));
   147 	gpGamedllFuncs=pGamedllFuncs;
   148 
   149 	LOG_MESSAGE(PLID, "%s v%s  %s", Plugin_info.name, Plugin_info.version, 
   150 			Plugin_info.date);
   151 	LOG_MESSAGE(PLID, "by %s", Plugin_info.author);
   152 	LOG_MESSAGE(PLID, "   %s", Plugin_info.url);
   153 	LOG_MESSAGE(PLID, "compiled: %s Eastern (%s)", COMPILE_TIME, OPT_TYPE);
   154 
   155 	// Let's go.
   156 	return(plugin_attach());
   157 }
   158 
   159 // Metamod detaching plugin from the server.
   160 // now		(given) current phase, ie during map, etc
   161 // reason	(given) why detaching (refresh, console unload, forced unload, etc)
   162 C_DLLEXPORT int Meta_Detach(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) {
   163 	if(now > Plugin_info.unloadable && reason != PNL_CMD_FORCED) {
   164 		LOG_ERROR(PLID, "Can't unload plugin right now");
   165 		return(FALSE);
   166 	}
   167 	// Done!
   168 	return(plugin_detach());
   169 }