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