|
darope@0
|
1 |
// vi: set ts=4 sw=4 : |
|
darope@0
|
2 |
// vim: set tw=75 : |
|
darope@0
|
3 |
|
|
darope@0
|
4 |
// metamod.cpp - (main) implementation of metamod operations |
|
darope@0
|
5 |
|
|
darope@0
|
6 |
/* |
|
darope@236
|
7 |
* Copyright (c) 2001-2004 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 <malloc.h> // malloc, etc |
|
darope@0
|
38 |
#include <errno.h> // errno, etc |
|
darope@0
|
39 |
|
|
darope@0
|
40 |
#include <extdll.h> // always |
|
darope@0
|
41 |
#include <enginecallback.h> // GET_GAME_DIR, etc |
|
darope@0
|
42 |
|
|
darope@0
|
43 |
#include "metamod.h" // me |
|
darope@0
|
44 |
#include "h_export.h" // GIVE_ENGINE_FUNCTIONS_FN, etc |
|
darope@0
|
45 |
#include "mreg.h" // class MRegCmdList, etc |
|
darope@0
|
46 |
#include "meta_api.h" // meta_globals_t, etc |
|
darope@0
|
47 |
#include "mutil.h" // mutil_funcs_t, etc |
|
darope@0
|
48 |
#include "osdep.h" // DLOPEN, getcwd, is_absolute_path, |
|
darope@0
|
49 |
#include "reg_support.h" // meta_AddServerCommand, etc |
|
darope@0
|
50 |
#include "game_support.h" // lookup_game, etc |
|
darope@0
|
51 |
#include "commands_meta.h" // meta_register_cmdcvar, etc |
|
darope@0
|
52 |
#include "thread_logparse.h" // logparse_handle, etc |
|
darope@0
|
53 |
#include "support_meta.h" // valid_gamedir_file, etc |
|
darope@0
|
54 |
#include "log_meta.h" // META_LOG, etc |
|
darope@0
|
55 |
#include "types_meta.h" // mBOOL |
|
darope@236
|
56 |
#include "info_name.h" // VNAME, COPYRIGHT_YEAR, etc |
|
darope@236
|
57 |
#include "vdate.h" // COMPILE_TIME, COMPILE_TZONE, etc |
|
darope@0
|
58 |
|
|
darope@0
|
59 |
cvar_t meta_version = {"metamod_version", VVERSION, FCVAR_SERVER, 0, NULL}; |
|
darope@0
|
60 |
|
|
darope@0
|
61 |
MConfig static_config; |
|
darope@0
|
62 |
MConfig *Config=&static_config; |
|
darope@0
|
63 |
option_t global_options[] = { |
|
darope@0
|
64 |
{ "debuglevel", CF_INT, &Config->debuglevel, "0" }, |
|
darope@0
|
65 |
{ "gamedll", CF_PATH, &Config->gamedll, NULL }, |
|
darope@0
|
66 |
{ "plugins_file", CF_PATH, &Config->plugins_file, PLUGINS_INI }, |
|
darope@0
|
67 |
{ "exec_cfg", CF_STR, &Config->exec_cfg, EXEC_CFG }, |
|
darope@0
|
68 |
// list terminator |
|
darope@0
|
69 |
{ NULL, CF_NONE, NULL, NULL } |
|
darope@0
|
70 |
}; |
|
darope@0
|
71 |
|
|
darope@0
|
72 |
gamedll_t GameDLL; |
|
darope@0
|
73 |
|
|
darope@0
|
74 |
meta_globals_t PublicMetaGlobals; |
|
darope@0
|
75 |
meta_globals_t PrivateMetaGlobals; |
|
darope@0
|
76 |
|
|
darope@0
|
77 |
enginefuncs_t g_plugin_engfuncs; |
|
darope@0
|
78 |
|
|
darope@0
|
79 |
MPluginList *Plugins; |
|
darope@0
|
80 |
MRegCmdList *RegCmds; |
|
darope@0
|
81 |
MRegCvarList *RegCvars; |
|
darope@0
|
82 |
MRegMsgList *RegMsgs; |
|
darope@0
|
83 |
|
|
darope@248
|
84 |
MPlayerList g_Players; |
|
darope@248
|
85 |
|
|
darope@248
|
86 |
unsigned int CALL_API_count = 0; |
|
darope@248
|
87 |
|
|
darope@0
|
88 |
#ifdef UNFINISHED |
|
darope@0
|
89 |
MHookList *Hooks; |
|
darope@0
|
90 |
#endif /* UNFINISHED */ |
|
darope@0
|
91 |
|
|
darope@0
|
92 |
// Very first metamod function that's run. |
|
darope@0
|
93 |
// Do startup operations... |
|
darope@0
|
94 |
void metamod_startup(void) { |
|
darope@0
|
95 |
char *cp, *mmfile=NULL, *cfile=NULL; |
|
darope@0
|
96 |
|
|
darope@0
|
97 |
META_CONS(" "); |
|
darope@236
|
98 |
META_CONS(" %s version %s Copyright (c) 2001-%s %s", VNAME, VVERSION, COPYRIGHT_YEAR, VAUTHOR); |
|
darope@0
|
99 |
META_CONS(" %s comes with ABSOLUTELY NO WARRANTY; for details type `meta gpl'.", VNAME); |
|
darope@0
|
100 |
META_CONS(" This is free software, and you are welcome to redistribute it"); |
|
darope@0
|
101 |
META_CONS(" under certain conditions; type `meta gpl' for details."); |
|
darope@0
|
102 |
META_CONS(" "); |
|
darope@0
|
103 |
|
|
darope@0
|
104 |
META_LOG("%s v%s %s", VNAME, VVERSION, VDATE); |
|
darope@0
|
105 |
META_LOG("by %s", VAUTHOR); |
|
darope@0
|
106 |
META_LOG(" %s", VURL); |
|
darope@236
|
107 |
META_LOG("compiled: %s %s (%s)", COMPILE_TIME, COMPILE_TZONE, OPT_TYPE); |
|
darope@0
|
108 |
|
|
darope@0
|
109 |
// If running with "+developer", allow an opportunity to break in with |
|
darope@0
|
110 |
// a debugger. |
|
darope@236
|
111 |
if((int) CVAR_GET_FLOAT("developer") != 0) { |
|
darope@236
|
112 |
sleep(10); |
|
darope@236
|
113 |
} |
|
darope@0
|
114 |
|
|
darope@0
|
115 |
// specify our new() handler |
|
darope@242
|
116 |
mm_set_new_handler(); |
|
darope@0
|
117 |
|
|
darope@0
|
118 |
// Get gamedir, very early on, because it seems we need it all over the |
|
darope@0
|
119 |
// place here at the start. |
|
darope@0
|
120 |
if(!meta_init_gamedll()) { |
|
darope@0
|
121 |
META_ERROR("Failure to init game DLL; exiting..."); |
|
darope@0
|
122 |
do_exit(1); |
|
darope@0
|
123 |
} |
|
darope@0
|
124 |
|
|
darope@0
|
125 |
// Register various console commands and cvars. |
|
darope@0
|
126 |
// Can I do these here, rather than waiting for GameDLLInit() ? |
|
darope@0
|
127 |
// Looks like it works okay.. |
|
darope@0
|
128 |
meta_register_cmdcvar(); |
|
darope@0
|
129 |
|
|
darope@0
|
130 |
// Set a slight debug level for developer mode, if debug level not |
|
darope@0
|
131 |
// already set. |
|
darope@0
|
132 |
if((int) CVAR_GET_FLOAT("developer") != 0 && meta_debug.value == 0) |
|
darope@0
|
133 |
CVAR_SET_FLOAT("meta_debug", 3.0); |
|
darope@0
|
134 |
|
|
darope@0
|
135 |
// Init default values |
|
darope@0
|
136 |
Config->init(global_options); |
|
darope@0
|
137 |
// Find config file |
|
darope@0
|
138 |
cfile=CONFIG_INI; |
|
darope@0
|
139 |
if((cp=LOCALINFO("mm_configfile")) && *cp != '\0') { |
|
darope@0
|
140 |
META_LOG("Configfile specified via localinfo: %s", cp); |
|
darope@0
|
141 |
if(valid_gamedir_file(cp)) |
|
darope@0
|
142 |
cfile=cp; |
|
darope@0
|
143 |
else |
|
darope@0
|
144 |
META_ERROR("Empty/missing config.ini file: %s; falling back to %s", |
|
darope@0
|
145 |
cp, cfile); |
|
darope@0
|
146 |
} |
|
darope@0
|
147 |
// Load config file |
|
darope@0
|
148 |
if(valid_gamedir_file(cfile)) |
|
darope@0
|
149 |
Config->load(cfile); |
|
darope@0
|
150 |
else |
|
darope@0
|
151 |
META_DEBUG(2, ("No config.ini file found: %s", CONFIG_INI)); |
|
darope@0
|
152 |
|
|
darope@0
|
153 |
// Now, override config options with localinfo commandline options. |
|
darope@0
|
154 |
if((cp=LOCALINFO("mm_debug")) && *cp != '\0') { |
|
darope@0
|
155 |
META_LOG("Debuglevel specified via localinfo: %s", cp); |
|
darope@0
|
156 |
Config->set("debuglevel", cp); |
|
darope@0
|
157 |
} |
|
darope@0
|
158 |
if((cp=LOCALINFO("mm_gamedll")) && *cp != '\0') { |
|
darope@0
|
159 |
META_LOG("Gamedll specified via localinfo: %s", cp); |
|
darope@0
|
160 |
Config->set("gamedll", cp); |
|
darope@0
|
161 |
} |
|
darope@0
|
162 |
if((cp=LOCALINFO("mm_pluginsfile")) && *cp != '\0') { |
|
darope@0
|
163 |
META_LOG("Pluginsfile specified via localinfo: %s", cp); |
|
darope@0
|
164 |
Config->set("plugins_file", cp); |
|
darope@0
|
165 |
} |
|
darope@0
|
166 |
if((cp=LOCALINFO("mm_execcfg")) && *cp != '\0') { |
|
darope@0
|
167 |
META_LOG("Execcfg specified via localinfo: %s", cp); |
|
darope@0
|
168 |
Config->set("exec_cfg", cp); |
|
darope@0
|
169 |
} |
|
darope@0
|
170 |
|
|
darope@0
|
171 |
|
|
darope@0
|
172 |
// Check for an initial debug level, since cfg files don't get exec'd |
|
darope@0
|
173 |
// until later. |
|
darope@0
|
174 |
if(Config->debuglevel != 0) |
|
darope@0
|
175 |
CVAR_SET_FLOAT("meta_debug", Config->debuglevel); |
|
darope@0
|
176 |
|
|
darope@0
|
177 |
// Prepare for registered commands from plugins. |
|
darope@0
|
178 |
RegCmds = new MRegCmdList(); |
|
darope@0
|
179 |
RegCvars = new MRegCvarList(); |
|
darope@0
|
180 |
|
|
darope@0
|
181 |
// Prepare for registered user messages from gamedll. |
|
darope@0
|
182 |
RegMsgs = new MRegMsgList(); |
|
darope@0
|
183 |
|
|
darope@0
|
184 |
// Copy, and store pointer in Engine struct. Yes, we could just store |
|
darope@0
|
185 |
// the actual engine_t struct in Engine, but then it wouldn't be a |
|
darope@0
|
186 |
// pointer to match the other g_engfuncs. |
|
darope@0
|
187 |
memcpy(&g_plugin_engfuncs, Engine.funcs, sizeof(g_plugin_engfuncs)); |
|
darope@0
|
188 |
Engine.pl_funcs=&g_plugin_engfuncs; |
|
darope@0
|
189 |
// substitute our special versions of various commands |
|
darope@0
|
190 |
Engine.pl_funcs->pfnAddServerCommand = meta_AddServerCommand; |
|
darope@0
|
191 |
Engine.pl_funcs->pfnCVarRegister = meta_CVarRegister; |
|
darope@0
|
192 |
Engine.pl_funcs->pfnCvar_RegisterVariable = meta_CVarRegister; |
|
darope@0
|
193 |
Engine.pl_funcs->pfnRegUserMsg = meta_RegUserMsg; |
|
darope@248
|
194 |
Engine.pl_funcs->pfnQueryClientCvarValue = meta_QueryClientCvarValue; |
|
darope@0
|
195 |
|
|
darope@0
|
196 |
#ifdef UNFINISHED |
|
darope@0
|
197 |
// Init the list of event/logline hooks. |
|
darope@0
|
198 |
Hooks = new MHookList(); |
|
darope@0
|
199 |
#endif /* UNFINISHED */ |
|
darope@0
|
200 |
|
|
darope@0
|
201 |
// Before, we loaded plugins before loading the game DLL, so that if no |
|
darope@0
|
202 |
// plugins caught engine functions, we could pass engine funcs straight |
|
darope@0
|
203 |
// to game dll, rather than acting as intermediary. (Should perform |
|
darope@0
|
204 |
// better, right?) |
|
darope@0
|
205 |
// |
|
darope@0
|
206 |
// But since a plugin can be loaded at any time, we have to go ahead |
|
darope@0
|
207 |
// and catch the engine funcs regardless. Also, we want to give each |
|
darope@0
|
208 |
// plugin a copy of the gameDLL's api tables, in case they need to call |
|
darope@0
|
209 |
// API functions directly. |
|
darope@0
|
210 |
// |
|
darope@0
|
211 |
// Thus, load gameDLL first, then plugins. |
|
darope@0
|
212 |
// |
|
darope@0
|
213 |
// However, we have to init the Plugins object first, because if the |
|
darope@0
|
214 |
// gamedll calls engine functions during GiveFnptrsToDll (like hpb_bot |
|
darope@0
|
215 |
// does) then it needs to be non-null so META_ENGINE_HANDLE won't crash. |
|
darope@0
|
216 |
// |
|
darope@0
|
217 |
// However, having replaced valid_file with valid_gamedir_file, we need |
|
darope@0
|
218 |
// to at least initialize the gameDLL to include the gamedir, before |
|
darope@0
|
219 |
// looking for plugins.ini. |
|
darope@0
|
220 |
// |
|
darope@0
|
221 |
// In fact, we need gamedir even earlier, so moved up above. |
|
darope@0
|
222 |
|
|
darope@0
|
223 |
// Fall back to old plugins filename, if configured one isn't found. |
|
darope@0
|
224 |
mmfile=PLUGINS_INI; |
|
darope@0
|
225 |
if(!valid_gamedir_file(PLUGINS_INI) && valid_gamedir_file(OLD_PLUGINS_INI)) |
|
darope@0
|
226 |
mmfile=OLD_PLUGINS_INI; |
|
darope@0
|
227 |
if(valid_gamedir_file(Config->plugins_file)) |
|
darope@0
|
228 |
mmfile=Config->plugins_file; |
|
darope@0
|
229 |
else |
|
darope@0
|
230 |
META_ERROR("Plugins file is empty/missing: %s; falling back to %s", |
|
darope@0
|
231 |
Config->plugins_file, mmfile); |
|
darope@0
|
232 |
|
|
darope@0
|
233 |
Plugins = new MPluginList(mmfile); |
|
darope@0
|
234 |
|
|
darope@0
|
235 |
if(!meta_load_gamedll()) { |
|
darope@0
|
236 |
META_ERROR("Failure to load game DLL; exiting..."); |
|
darope@0
|
237 |
do_exit(1); |
|
darope@0
|
238 |
} |
|
darope@0
|
239 |
if(!Plugins->load()) { |
|
darope@0
|
240 |
META_ERROR("Failure to load plugins..."); |
|
darope@0
|
241 |
// Exit on failure here? Dunno... |
|
darope@0
|
242 |
} |
|
darope@0
|
243 |
|
|
darope@0
|
244 |
#ifdef UNFINISHED |
|
darope@0
|
245 |
// Start up the log parsing thread. |
|
darope@0
|
246 |
startup_logparse_thread(); |
|
darope@0
|
247 |
#endif /* UNFINISHED */ |
|
darope@0
|
248 |
|
|
darope@0
|
249 |
// Allow for commands to metamod plugins at startup. Autoexec.cfg is |
|
darope@0
|
250 |
// read too early, and server.cfg is read too late. |
|
darope@0
|
251 |
// |
|
darope@0
|
252 |
// Only attempt load if the file appears to exist and be non-empty, to |
|
darope@0
|
253 |
// avoid confusing users with "couldn't exec exec.cfg" console |
|
darope@0
|
254 |
// messages. |
|
darope@0
|
255 |
if(valid_gamedir_file(Config->exec_cfg)) |
|
darope@0
|
256 |
mmfile=Config->exec_cfg; |
|
darope@0
|
257 |
else if(valid_gamedir_file(OLD_EXEC_CFG)) |
|
darope@0
|
258 |
mmfile=OLD_EXEC_CFG; |
|
darope@0
|
259 |
else |
|
darope@0
|
260 |
mmfile=NULL; |
|
darope@0
|
261 |
|
|
darope@0
|
262 |
if(mmfile) { |
|
darope@0
|
263 |
if(mmfile[0]=='/') |
|
darope@0
|
264 |
META_ERROR("Cannot exec absolute pathnames: %s", mmfile); |
|
darope@0
|
265 |
else { |
|
darope@0
|
266 |
char cmd[NAME_MAX]; |
|
darope@0
|
267 |
META_LOG("Exec'ing metamod exec.cfg: %s...", mmfile); |
|
darope@0
|
268 |
snprintf(cmd, sizeof(cmd), "exec %s\n", mmfile); |
|
darope@0
|
269 |
SERVER_COMMAND(cmd); |
|
darope@0
|
270 |
} |
|
darope@0
|
271 |
} |
|
darope@0
|
272 |
} |
|
darope@0
|
273 |
|
|
darope@0
|
274 |
// Set initial GameDLL fields (name, gamedir). |
|
darope@0
|
275 |
// meta_errno values: |
|
darope@0
|
276 |
// - ME_NULLRESULT getcwd failed |
|
darope@0
|
277 |
mBOOL meta_init_gamedll(void) { |
|
darope@0
|
278 |
char gamedir[PATH_MAX]; |
|
darope@0
|
279 |
char *cp; |
|
darope@0
|
280 |
|
|
darope@0
|
281 |
memset(&GameDLL, 0, sizeof(GameDLL)); |
|
darope@0
|
282 |
|
|
darope@0
|
283 |
GET_GAME_DIR(gamedir); |
|
darope@0
|
284 |
normalize_pathname(gamedir); |
|
darope@0
|
285 |
// |
|
darope@0
|
286 |
// As of 1.1.1.1, the engine routine GET_GAME_DIR no longer returns a |
|
darope@0
|
287 |
// full-pathname, but rather just the path specified as the argument to |
|
darope@0
|
288 |
// "-game". |
|
darope@0
|
289 |
// |
|
darope@0
|
290 |
// However, since we have to work properly under both the new version |
|
darope@0
|
291 |
// as well as previous versions, we have to support both possibilities. |
|
darope@0
|
292 |
// |
|
darope@0
|
293 |
// Note: the code has always assumed the server op wouldn't do: |
|
darope@0
|
294 |
// hlds -game other/firearms |
|
darope@0
|
295 |
// |
|
darope@0
|
296 |
if(is_absolute_path(gamedir)) { |
|
darope@0
|
297 |
// Old style; GET_GAME_DIR returned full pathname. Copy this into |
|
darope@0
|
298 |
// our gamedir, and truncate to get the game name. |
|
darope@0
|
299 |
// (note check for both linux and win32 full pathname.) |
|
darope@0
|
300 |
STRNCPY(GameDLL.gamedir, gamedir, sizeof(GameDLL.gamedir)); |
|
darope@0
|
301 |
cp=strrchr(gamedir, '/') + 1; |
|
darope@0
|
302 |
STRNCPY(GameDLL.name, cp, sizeof(GameDLL.name)); |
|
darope@0
|
303 |
} |
|
darope@0
|
304 |
else { |
|
darope@0
|
305 |
// New style; GET_GAME_DIR returned game name. Copy this into our |
|
darope@0
|
306 |
// game name, and prepend the current working directory. |
|
darope@0
|
307 |
char buf[PATH_MAX]; |
|
darope@0
|
308 |
if(!getcwd(buf, sizeof(buf))) { |
|
darope@0
|
309 |
META_ERROR("dll: Couldn't get cwd; %s", strerror(errno)); |
|
darope@0
|
310 |
RETURN_ERRNO(mFALSE, ME_NULLRESULT); |
|
darope@0
|
311 |
} |
|
darope@0
|
312 |
snprintf(GameDLL.gamedir, sizeof(GameDLL.gamedir), |
|
darope@0
|
313 |
"%s/%s", buf, gamedir); |
|
darope@0
|
314 |
STRNCPY(GameDLL.name, gamedir, sizeof(GameDLL.name)); |
|
darope@0
|
315 |
} |
|
darope@0
|
316 |
|
|
darope@0
|
317 |
META_DEBUG(3, ("Game: %s", GameDLL.name)); |
|
darope@0
|
318 |
|
|
darope@0
|
319 |
return(mTRUE); |
|
darope@0
|
320 |
} |
|
darope@0
|
321 |
|
|
darope@0
|
322 |
// Load game DLL. |
|
darope@0
|
323 |
// meta_errno values: |
|
darope@0
|
324 |
// - ME_DLOPEN couldn't dlopen game dll file |
|
darope@0
|
325 |
// - ME_DLMISSING couldn't find required routine in game dll |
|
darope@0
|
326 |
// (GiveFnptrsToDll, GetEntityAPI, GetEntityAPI2) |
|
darope@0
|
327 |
mBOOL meta_load_gamedll(void) { |
|
darope@0
|
328 |
int iface_vers; |
|
darope@0
|
329 |
int found=0; |
|
darope@0
|
330 |
|
|
darope@0
|
331 |
GIVE_ENGINE_FUNCTIONS_FN pfn_give_engfuncs; |
|
darope@0
|
332 |
GETNEWDLLFUNCTIONS_FN pfn_getapinew; |
|
darope@0
|
333 |
GETENTITYAPI2_FN pfn_getapi2; |
|
darope@0
|
334 |
GETENTITYAPI_FN pfn_getapi; |
|
darope@0
|
335 |
|
|
darope@0
|
336 |
if(!setup_gamedll(&GameDLL)) { |
|
darope@0
|
337 |
META_ERROR("dll: Unrecognized game: %s", GameDLL.name); |
|
darope@0
|
338 |
// meta_errno should be already set in lookup_game() |
|
darope@0
|
339 |
return(mFALSE); |
|
darope@0
|
340 |
} |
|
darope@0
|
341 |
|
|
darope@0
|
342 |
// open the game DLL |
|
darope@0
|
343 |
if(!(GameDLL.handle=DLOPEN(GameDLL.pathname))) { |
|
darope@0
|
344 |
META_ERROR("dll: Couldn't load game DLL %s: %s", GameDLL.pathname, |
|
darope@0
|
345 |
DLERROR()); |
|
darope@0
|
346 |
RETURN_ERRNO(mFALSE, ME_DLOPEN); |
|
darope@0
|
347 |
} |
|
darope@0
|
348 |
|
|
darope@0
|
349 |
// Used to only pass our table of engine funcs if a loaded plugin |
|
darope@0
|
350 |
// wanted to catch one of the functions, but now that plugins are |
|
darope@0
|
351 |
// dynamically loadable at any time, we have to always pass our table, |
|
darope@0
|
352 |
// so that any plugin loaded later can catch what they need to. |
|
darope@0
|
353 |
if((pfn_give_engfuncs = (GIVE_ENGINE_FUNCTIONS_FN) DLSYM(GameDLL.handle, |
|
darope@0
|
354 |
"GiveFnptrsToDll"))) |
|
darope@0
|
355 |
{ |
|
darope@0
|
356 |
pfn_give_engfuncs(&meta_engfuncs, gpGlobals); |
|
darope@0
|
357 |
META_DEBUG(3, ("dll: Game '%s': Called GiveFnptrsToDll", |
|
darope@0
|
358 |
GameDLL.name)); |
|
darope@0
|
359 |
} |
|
darope@0
|
360 |
else { |
|
darope@0
|
361 |
META_ERROR("dll: Couldn't find GiveFnptrsToDll() in game DLL '%s': %s", |
|
darope@0
|
362 |
GameDLL.name, DLERROR()); |
|
darope@0
|
363 |
RETURN_ERRNO(mFALSE, ME_DLMISSING); |
|
darope@0
|
364 |
} |
|
darope@0
|
365 |
|
|
darope@0
|
366 |
// Yes...another macro. |
|
darope@0
|
367 |
#define GET_FUNC_TABLE_FROM_GAME(gamedll, pfnGetFuncs, STR_GetFuncs, struct_field, API_TYPE, TABLE_TYPE, vers_pass, vers_int, vers_want, gotit) \ |
|
darope@0
|
368 |
if((pfnGetFuncs = (API_TYPE) DLSYM(gamedll.handle, STR_GetFuncs))) { \ |
|
darope@248
|
369 |
gamedll.funcs.struct_field = (TABLE_TYPE*) calloc(1, sizeof(TABLE_TYPE)); \ |
|
darope@0
|
370 |
if(!gamedll.funcs.struct_field) {\ |
|
darope@0
|
371 |
META_ERROR("malloc failed for gamedll struct_field: %s", STR_GetFuncs); \ |
|
darope@0
|
372 |
} \ |
|
darope@0
|
373 |
else if(pfnGetFuncs(gamedll.funcs.struct_field, vers_pass)) { \ |
|
darope@0
|
374 |
META_DEBUG(3, ("dll: Game '%s': Found %s", gamedll.name, STR_GetFuncs)); \ |
|
darope@0
|
375 |
gotit=1; \ |
|
darope@0
|
376 |
} \ |
|
darope@0
|
377 |
else { \ |
|
darope@0
|
378 |
META_ERROR("dll: Failure calling %s in game '%s'", STR_GetFuncs, gamedll.name); \ |
|
darope@0
|
379 |
free(gamedll.funcs.struct_field); \ |
|
darope@0
|
380 |
gamedll.funcs.struct_field=NULL; \ |
|
darope@0
|
381 |
if(vers_int != vers_want) { \ |
|
darope@0
|
382 |
META_ERROR("dll: Interface version didn't match; we wanted %d, they had %d", vers_want, vers_int); \ |
|
darope@0
|
383 |
/* reproduce error from engine */ \ |
|
darope@0
|
384 |
META_CONS("=================="); \ |
|
darope@0
|
385 |
META_CONS("Game DLL version mismatch"); \ |
|
darope@0
|
386 |
META_CONS("DLL version is %d, engine version is %d", vers_int, vers_want); \ |
|
darope@0
|
387 |
if(vers_int > vers_want) \ |
|
darope@0
|
388 |
META_CONS("Engine appears to be outdated, check for updates"); \ |
|
darope@0
|
389 |
else \ |
|
darope@0
|
390 |
META_CONS("The game DLL for %s appears to be outdated, check for updates", GameDLL.name); \ |
|
darope@0
|
391 |
META_CONS("=================="); \ |
|
darope@0
|
392 |
ALERT(at_error, "Exiting...\n"); \ |
|
darope@0
|
393 |
} \ |
|
darope@0
|
394 |
} \ |
|
darope@0
|
395 |
} \ |
|
darope@0
|
396 |
else { \ |
|
darope@0
|
397 |
META_DEBUG(5, ("dll: Game '%s': No %s", gamedll.name, STR_GetFuncs)); \ |
|
darope@0
|
398 |
gamedll.funcs.struct_field=NULL; \ |
|
darope@0
|
399 |
} |
|
darope@0
|
400 |
|
|
darope@0
|
401 |
// Look for API-NEW interface in Game dll. We do this before API2/API, because |
|
darope@0
|
402 |
// that's what the engine appears to do.. |
|
darope@0
|
403 |
iface_vers=NEW_DLL_FUNCTIONS_VERSION; |
|
darope@0
|
404 |
GET_FUNC_TABLE_FROM_GAME(GameDLL, pfn_getapinew, "GetNewDLLFunctions", newapi_table, |
|
darope@0
|
405 |
GETNEWDLLFUNCTIONS_FN, NEW_DLL_FUNCTIONS, |
|
darope@0
|
406 |
&iface_vers, iface_vers, NEW_DLL_FUNCTIONS_VERSION, found); |
|
darope@0
|
407 |
|
|
darope@0
|
408 |
// Look for API2 interface in plugin; preferred over API-1. |
|
darope@0
|
409 |
found=0; |
|
darope@0
|
410 |
iface_vers=INTERFACE_VERSION; |
|
darope@0
|
411 |
GET_FUNC_TABLE_FROM_GAME(GameDLL, pfn_getapi2, "GetEntityAPI2", dllapi_table, |
|
darope@0
|
412 |
GETENTITYAPI2_FN, DLL_FUNCTIONS, |
|
darope@0
|
413 |
&iface_vers, iface_vers, INTERFACE_VERSION, found); |
|
darope@0
|
414 |
|
|
darope@0
|
415 |
// Look for API-1 in plugin, if API2 interface wasn't found. |
|
darope@0
|
416 |
if(!found) { |
|
darope@0
|
417 |
found=0; |
|
darope@0
|
418 |
GET_FUNC_TABLE_FROM_GAME(GameDLL, pfn_getapi, "GetEntityAPI", dllapi_table, |
|
darope@0
|
419 |
GETENTITYAPI_FN, DLL_FUNCTIONS, |
|
darope@0
|
420 |
INTERFACE_VERSION, INTERFACE_VERSION, INTERFACE_VERSION, found); |
|
darope@0
|
421 |
} |
|
darope@0
|
422 |
|
|
darope@0
|
423 |
// If didn't find either, return failure. |
|
darope@0
|
424 |
if(!found) { |
|
darope@0
|
425 |
META_ERROR("dll: Couldn't find either GetEntityAPI nor GetEntityAPI2 in game DLL '%s'", GameDLL.name); |
|
darope@0
|
426 |
RETURN_ERRNO(mFALSE, ME_DLMISSING); |
|
darope@0
|
427 |
} |
|
darope@0
|
428 |
|
|
darope@0
|
429 |
META_LOG("Game DLL for '%s' loaded successfully", GameDLL.desc); |
|
darope@0
|
430 |
return(mTRUE); |
|
darope@0
|
431 |
} |
|
darope@248
|
432 |
|