darope@0: // vi: set ts=4 sw=4 : darope@0: // vim: set tw=75 : darope@0: darope@0: // meta_api.cpp - common implementation of Metamod's plugin interface darope@0: darope@0: // This file is intended to provide a decent common codebase for the darope@0: // Metamod plugins that I maintain, so everything is set up to be very darope@0: // generalized, with the things specific for each plugin handled in darope@0: // plugin.{h,cpp} and elsewhere. darope@0: darope@0: /* darope@0: * Copyright (c) 2001-2003 Will Day darope@0: * darope@0: * This file is part of Metamod. darope@0: * darope@0: * Metamod is free software; you can redistribute it and/or modify it darope@0: * under the terms of the GNU General Public License as published by the darope@0: * Free Software Foundation; either version 2 of the License, or (at darope@0: * your option) any later version. darope@0: * darope@0: * Metamod is distributed in the hope that it will be useful, but darope@0: * WITHOUT ANY WARRANTY; without even the implied warranty of darope@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU darope@0: * General Public License for more details. darope@0: * darope@0: * You should have received a copy of the GNU General Public License darope@0: * along with Metamod; if not, write to the Free Software Foundation, darope@0: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA darope@0: * darope@0: * In addition, as a special exception, the author gives permission to darope@0: * link the code of this program with the Half-Life Game Engine ("HL darope@0: * Engine") and Modified Game Libraries ("MODs") developed by Valve, darope@0: * L.L.C ("Valve"). You must obey the GNU General Public License in all darope@0: * respects for all of the code used other than the HL Engine and MODs darope@0: * from Valve. If you modify this file, you may extend this exception darope@0: * to your version of the file, but you are not obligated to do so. If darope@0: * you do not wish to do so, delete this exception statement from your darope@0: * version. darope@0: * darope@0: */ darope@0: darope@0: #include darope@0: darope@0: #include darope@0: #include darope@0: #include darope@0: darope@0: #include "plugin.h" darope@0: #include "info_name.h" darope@0: #include "log_plugin.h" darope@0: darope@0: extern META_FUNCTIONS gMetaFunctionTable; darope@0: darope@0: // Description of plugin. darope@0: // (V* info from info_name.h) darope@0: plugin_info_t Plugin_info = { darope@0: META_INTERFACE_VERSION, // ifvers darope@0: VNAME, // name darope@0: VVERSION, // version darope@0: VDATE, // date darope@0: VAUTHOR, // author darope@0: VURL, // url darope@0: VLOGTAG, // logtag darope@0: PT_ANYTIME, // loadable darope@0: PT_ANYPAUSE, // unloadable darope@0: }; darope@0: darope@0: // Global variables from metamod. These variable names are referenced by darope@0: // various macros. darope@0: meta_globals_t *gpMetaGlobals; // metamod globals darope@0: gamedll_funcs_t *gpGamedllFuncs; // gameDLL function tables darope@0: mutil_funcs_t *gpMetaUtilFuncs; // metamod utility functions darope@0: darope@0: // Metamod requesting info about this plugin darope@0: // ifvers (given) interface_version metamod is using darope@0: // pPlugInfo (requested) struct with info about plugin darope@0: // pMetaUtilFuncs (given) table of utility functions provided by metamod darope@0: C_DLLEXPORT int Meta_Query(char *ifvers, plugin_info_t **pPlugInfo, darope@0: mutil_funcs_t *pMetaUtilFuncs) darope@0: { darope@0: if ((int) CVAR_GET_FLOAT("developer") != 0) darope@0: UTIL_LogPrintf("[%s] dev: called: Meta_Query; version=%s, ours=%s\n", darope@0: Plugin_info.logtag, ifvers, Plugin_info.ifvers); darope@0: darope@0: // Check for valid pMetaUtilFuncs before we continue. darope@0: if(!pMetaUtilFuncs) { darope@0: UTIL_LogPrintf("[%s] ERROR: Meta_Query called with null pMetaUtilFuncs\n", Plugin_info.logtag); darope@0: return(FALSE); darope@0: } darope@0: gpMetaUtilFuncs=pMetaUtilFuncs; darope@0: darope@0: // Give metamod our plugin_info struct. darope@0: *pPlugInfo=&Plugin_info; darope@0: darope@0: // Check for interface version compatibility. darope@0: if(!FStrEq(ifvers, Plugin_info.ifvers)) { darope@0: int mmajor=0, mminor=0, pmajor=0, pminor=0; darope@0: LOG_MESSAGE(PLID, "WARNING: meta-interface version mismatch; requested=%s ours=%s", darope@0: Plugin_info.logtag, ifvers); darope@0: // If plugin has later interface version, it's incompatible (update darope@0: // metamod). darope@0: sscanf(ifvers, "%d:%d", &mmajor, &mminor); darope@0: sscanf(META_INTERFACE_VERSION, "%d:%d", &pmajor, &pminor); darope@0: if(pmajor > mmajor || (pmajor==mmajor && pminor > mminor)) { darope@0: LOG_ERROR(PLID, "metamod version is too old for this plugin; update metamod"); darope@0: return(FALSE); darope@0: } darope@0: // If plugin has older major interface version, it's incompatible darope@0: // (update plugin). darope@0: else if(pmajor < mmajor) { darope@0: LOG_ERROR(PLID, "metamod version is incompatible with this plugin; please find a newer version of this plugin"); darope@0: return(FALSE); darope@0: } darope@0: // Minor interface is older, but this is guaranteed to be backwards darope@0: // compatible, so we warn, but we still accept it. darope@0: else if(pmajor==mmajor && pminor < mminor) darope@0: LOG_MESSAGE(PLID, "WARNING: metamod version is newer than expected; consider finding a newer version of this plugin"); darope@0: else darope@0: 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); darope@0: } darope@0: darope@0: return (plugin_query()); darope@0: } darope@0: darope@0: // Metamod attaching plugin to the server. darope@0: // now (given) current phase, ie during map, during changelevel, or at startup darope@0: // pFunctionTable (requested) table of function tables this plugin catches darope@0: // pMGlobals (given) global vars from metamod darope@0: // pGamedllFuncs (given) copy of function tables from game dll darope@0: C_DLLEXPORT int Meta_Attach(PLUG_LOADTIME now, META_FUNCTIONS *pFunctionTable, darope@0: meta_globals_t *pMGlobals, gamedll_funcs_t *pGamedllFuncs) darope@0: { darope@0: if(now > Plugin_info.loadable) { darope@0: LOG_ERROR(PLID, "Can't load plugin right now"); darope@0: return(FALSE); darope@0: } darope@0: if(!pMGlobals) { darope@0: LOG_ERROR(PLID, "Meta_Attach called with null pMGlobals"); darope@0: return(FALSE); darope@0: } darope@0: gpMetaGlobals=pMGlobals; darope@0: if(!pFunctionTable) { darope@0: LOG_ERROR(PLID, "Meta_Attach called with null pFunctionTable"); darope@0: return(FALSE); darope@0: } darope@0: memcpy(pFunctionTable, &gMetaFunctionTable, sizeof(META_FUNCTIONS)); darope@0: gpGamedllFuncs=pGamedllFuncs; darope@0: darope@0: LOG_MESSAGE(PLID, "%s v%s %s", Plugin_info.name, Plugin_info.version, darope@0: Plugin_info.date); darope@0: LOG_MESSAGE(PLID, "by %s", Plugin_info.author); darope@0: LOG_MESSAGE(PLID, " %s", Plugin_info.url); darope@0: LOG_MESSAGE(PLID, "compiled: %s Eastern (%s)", COMPILE_TIME, OPT_TYPE); darope@0: darope@0: // Let's go. darope@0: return(plugin_attach()); darope@0: } darope@0: darope@0: // Metamod detaching plugin from the server. darope@0: // now (given) current phase, ie during map, etc darope@0: // reason (given) why detaching (refresh, console unload, forced unload, etc) darope@0: C_DLLEXPORT int Meta_Detach(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) { darope@0: if(now > Plugin_info.unloadable && reason != PNL_CMD_FORCED) { darope@0: LOG_ERROR(PLID, "Can't unload plugin right now"); darope@0: return(FALSE); darope@0: } darope@0: // Done! darope@0: return(plugin_detach()); darope@0: }