1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcemm/metamod.cpp Thu Oct 04 20:15:25 2007 +0000
1.3 @@ -0,0 +1,1244 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * ======================================================
1.7 + * Metamod:Source
1.8 + * Copyright (C) 2004-2007 AlliedModders LLC and authors.
1.9 + * All rights reserved.
1.10 + * ======================================================
1.11 + *
1.12 + * This software is provided 'as-is', without any express or implied warranty.
1.13 + * In no event will the authors be held liable for any damages arising from
1.14 + * the use of this software.
1.15 + *
1.16 + * Permission is granted to anyone to use this software for any purpose,
1.17 + * including commercial applications, and to alter it and redistribute it
1.18 + * freely, subject to the following restrictions:
1.19 + *
1.20 + * 1. The origin of this software must not be misrepresented; you must not
1.21 + * claim that you wrote the original software. If you use this software in a
1.22 + * product, an acknowledgment in the product documentation would be
1.23 + * appreciated but is not required.
1.24 + * 2. Altered source versions must be plainly marked as such, and must not be
1.25 + * misrepresented as being the original software.
1.26 + * 3. This notice may not be removed or altered from any source distribution.
1.27 + *
1.28 + * Version: $Id$
1.29 + */
1.30 +
1.31 +#if defined _DEBUG
1.32 +#define DEBUG2
1.33 +#undef _DEBUG
1.34 +#endif
1.35 +#include <interface.h>
1.36 +#include <eiface.h>
1.37 +#include "metamod.h"
1.38 +#include "metamod_provider.h"
1.39 +#include "metamod_plugins.h"
1.40 +#include "metamod_util.h"
1.41 +#include "metamod_console.h"
1.42 +#include "metamod_oslink.h"
1.43 +#if defined DEBUG2
1.44 +#undef DEBUG2
1.45 +#define _DEBUG
1.46 +#endif
1.47 +
1.48 +using namespace SourceMM;
1.49 +using namespace SourceHook;
1.50 +
1.51 +/**
1.52 + * @brief Implementation of main SourceMM GameDLL functionality
1.53 + * @file sourcemm.cpp
1.54 + */
1.55 +
1.56 +SH_DECL_MANUALHOOK4(SGD_DLLInit, 0, 0, 0, bool, CreateInterfaceFn, CreateInterfaceFn, CreateInterfaceFn, CGlobalVars *);
1.57 +SH_DECL_MANUALHOOK0(SGD_GameInit, 0, 0, 0, bool);
1.58 +SH_DECL_MANUALHOOK6(SGD_LevelInit, 0, 0, 0, bool, const char *, const char *, const char *, const char *, bool, bool);
1.59 +SH_DECL_MANUALHOOK0_void(SGD_LevelShutdown, 0, 0, 0);
1.60 +SH_DECL_MANUALHOOK0_void(SGD_DLLShutdown, 0, 0, 0);
1.61 +
1.62 +bool Handler_DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals);
1.63 +bool Handler_DLLInit_Post(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals);
1.64 +void Handler_DLLShutdown();
1.65 +void Handler_LevelShutdown();
1.66 +bool Handler_LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background);
1.67 +bool Handler_GameInit();
1.68 +void InitializeVSP();
1.69 +
1.70 +struct game_dll_t
1.71 +{
1.72 + HINSTANCE lib;
1.73 + CreateInterfaceFn factory;
1.74 +};
1.75 +
1.76 +String mod_path;
1.77 +String metamod_path;
1.78 +String full_bin_path;
1.79 +bool parsed_game_info = false;
1.80 +int vsp_version = 0;
1.81 +int gamedll_version = 0;
1.82 +int engine_build = SOURCE_ENGINE_UNKNOWN;
1.83 +List<game_dll_t *> gamedll_list;
1.84 +bool is_gamedll_loaded = false;
1.85 +bool in_first_level = true;
1.86 +bool is_game_init = false;
1.87 +bool vsp_load_attempted = false;
1.88 +bool vsp_load_requested = false;
1.89 +bool vsp_loaded = false;
1.90 +game_dll_t gamedll_info;
1.91 +ConVar *metamod_version = NULL;
1.92 +ConVar *mm_pluginsfile = NULL;
1.93 +IServerGameDLL *server = NULL;
1.94 +CreateInterfaceFn engine_factory = NULL;
1.95 +CreateInterfaceFn physics_factory = NULL;
1.96 +CreateInterfaceFn filesystem_factory = NULL;
1.97 +CGlobalVars *gpGlobals = NULL;
1.98 +SourceHook::CSourceHookImpl g_SourceHook;
1.99 +SourceHook::ISourceHook *g_SHPtr = &g_SourceHook;
1.100 +PluginId g_PLID = Pl_Console;
1.101 +META_RES last_meta_res;
1.102 +IServerPluginCallbacks *vsp_callbacks = NULL;
1.103 +
1.104 +MetamodSource g_Metamod;
1.105 +
1.106 +void ClearGamedllList();
1.107 +
1.108 +/* Helper Macro */
1.109 +#define IFACE_MACRO(orig,nam) \
1.110 + CPluginManager::CPlugin *pl; \
1.111 + SourceHook::List<IMetamodListener *>::iterator event; \
1.112 + IMetamodListener *api; \
1.113 + int mret = 0; \
1.114 + void *val = NULL; \
1.115 + for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
1.116 + pl = (*iter); \
1.117 + for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++) { \
1.118 + api = (*event); \
1.119 + mret = IFACE_FAILED; \
1.120 + if ( (val=api->On##nam##Query(iface, &mret)) != NULL ) { \
1.121 + if (ret) *ret = mret; \
1.122 + return val; \
1.123 + } \
1.124 + } \
1.125 + } \
1.126 + return (orig)(iface, ret);
1.127 +
1.128 +#define ITER_EVENT(evn, args) \
1.129 + CPluginManager::CPlugin *pl; \
1.130 + SourceHook::List<IMetamodListener *>::iterator event; \
1.131 + IMetamodListener *api; \
1.132 + for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
1.133 + pl = (*iter); \
1.134 + for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++) { \
1.135 + api = (*event); \
1.136 + api->evn args; \
1.137 + } \
1.138 + }
1.139 +
1.140 +/* Initialize everything here */
1.141 +void InitMainStates()
1.142 +{
1.143 + char full_path[PATH_SIZE] = {0};
1.144 + GetFileOfAddress((void *)gamedll_info.factory, full_path, sizeof(full_path));
1.145 + full_bin_path.assign(full_path);
1.146 +
1.147 + /* Like Metamod, reload plugins at the end of the map.
1.148 + * This is so plugins can hook everything on load, BUT, new plugins will be reloaded
1.149 + * if the server is shut down (silly, but rare case).
1.150 + */
1.151 + in_first_level = true;
1.152 +
1.153 + SourceHook::MemFuncInfo info;
1.154 +
1.155 + if (!provider->GetHookInfo(ProvidedHook_DLLInit, &info))
1.156 + {
1.157 + provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::DLLInit");
1.158 + }
1.159 + SH_MANUALHOOK_RECONFIGURE(SGD_DLLInit, info.vtblindex, info.vtbloffs, info.thisptroffs);
1.160 + SH_ADD_MANUALHOOK_STATICFUNC(SGD_DLLInit, server, Handler_DLLInit, false);
1.161 + SH_ADD_MANUALHOOK_STATICFUNC(SGD_DLLInit, server, Handler_DLLInit_Post, true);
1.162 +
1.163 + if (!provider->GetHookInfo(ProvidedHook_GameInit, &info))
1.164 + {
1.165 + provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::GameInit");
1.166 + }
1.167 + SH_MANUALHOOK_RECONFIGURE(SGD_GameInit, info.vtblindex, info.vtbloffs, info.thisptroffs);
1.168 + SH_ADD_MANUALHOOK_STATICFUNC(SGD_GameInit, server, Handler_GameInit, false);
1.169 +
1.170 + if (!provider->GetHookInfo(ProvidedHook_LevelInit, &info))
1.171 + {
1.172 + provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::LevelInit");
1.173 + }
1.174 + SH_MANUALHOOK_RECONFIGURE(SGD_LevelInit, info.vtblindex, info.vtbloffs, info.thisptroffs);
1.175 + SH_ADD_MANUALHOOK_STATICFUNC(SGD_LevelInit, server, Handler_LevelInit, true);
1.176 +
1.177 + if (!provider->GetHookInfo(ProvidedHook_LevelShutdown, &info))
1.178 + {
1.179 + provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::LevelShutdown");
1.180 + }
1.181 + SH_MANUALHOOK_RECONFIGURE(SGD_LevelShutdown, info.vtblindex, info.vtbloffs, info.thisptroffs);
1.182 + SH_ADD_MANUALHOOK_STATICFUNC(SGD_LevelShutdown, server, Handler_LevelShutdown, true);
1.183 +
1.184 + if (!provider->GetHookInfo(ProvidedHook_DLLShutdown, &info))
1.185 + {
1.186 + provider->DisplayError("Metamod:Source could not find a valid hook for IServerGameDLL::DLLShutdown");
1.187 + }
1.188 + SH_MANUALHOOK_RECONFIGURE(SGD_DLLShutdown, info.vtblindex, info.vtbloffs, info.thisptroffs);
1.189 + SH_ADD_MANUALHOOK_STATICFUNC(SGD_DLLShutdown, server, Handler_DLLShutdown, false);
1.190 +}
1.191 +
1.192 +/* This is where the magic happens */
1.193 +SMM_API void *CreateInterface(const char *iface, int *ret)
1.194 +{
1.195 + /* Prevent loading of self as a SourceMM plugin or Valve server plugin :x */
1.196 + if (strcmp(iface, PLAPI_NAME) == 0)
1.197 + {
1.198 + provider->DisplayWarning("Do not try loading Metamod:Source as a plugin.\n");
1.199 +
1.200 + if (ret)
1.201 + {
1.202 + *ret = IFACE_FAILED;
1.203 + }
1.204 +
1.205 + return NULL;
1.206 + }
1.207 +
1.208 + if (strncmp(iface, "ISERVERPLUGINCALLBACKS", 22) == 0)
1.209 + {
1.210 + vsp_callbacks = provider->GetVSPCallbacks(iface);
1.211 +
1.212 + if (vsp_callbacks != NULL && vsp_version == 0)
1.213 + {
1.214 + vsp_version = atoi(&iface[22]);
1.215 + }
1.216 +
1.217 + if (ret)
1.218 + {
1.219 + *ret = (vsp_callbacks != NULL) ? IFACE_OK : IFACE_FAILED;
1.220 + }
1.221 +
1.222 + return vsp_callbacks;
1.223 + }
1.224 +
1.225 + if (!parsed_game_info)
1.226 + {
1.227 + parsed_game_info = true;
1.228 + const char *game_dir = NULL;
1.229 + char game_path[PATH_SIZE];
1.230 + char mm_path[PATH_SIZE];
1.231 +
1.232 + /* Get path to SourceMM DLL */
1.233 + if (!GetFileOfAddress((void *)CreateInterface, mm_path, sizeof(mm_path)))
1.234 + {
1.235 + provider->DisplayError("GetFileOfAddress() failed! Metamod cannot load.\n");
1.236 + return NULL;
1.237 + }
1.238 +
1.239 + metamod_path.assign(mm_path);
1.240 +
1.241 + /* Get value of -game from command line, defaulting to hl2 as engine seems to do */
1.242 + game_dir = provider->GetCommandLineValue("-game", "hl2");
1.243 +
1.244 + engine_build = SOURCE_ENGINE_ORANGEBOX;
1.245 +
1.246 + /* Get absolute path */
1.247 + abspath(game_path, game_dir);
1.248 + mod_path.assign(game_path);
1.249 +
1.250 + char temp_path[PATH_SIZE];
1.251 +
1.252 + /* Path to gameinfo.txt */
1.253 + g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s", mod_path.c_str(), "gameinfo.txt");
1.254 +
1.255 + FILE *fp = fopen(temp_path, "rt");
1.256 +
1.257 + if (!fp)
1.258 + {
1.259 + provider->DisplayError("Unable to open gameinfo.txt! Metamod cannot load.\n");
1.260 + return NULL;
1.261 + }
1.262 +
1.263 + char buffer[255];
1.264 + char key[128], val[128];
1.265 + bool search = false;
1.266 + bool gamebin = false;
1.267 + char *ptr;
1.268 + const char *lptr;
1.269 + char cur_path[PATH_SIZE];
1.270 +
1.271 + getcwd(cur_path, PATH_SIZE);
1.272 +
1.273 + while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
1.274 + {
1.275 + UTIL_TrimComments(buffer);
1.276 + UTIL_TrimLeft(buffer);
1.277 + UTIL_TrimRight(buffer);
1.278 +
1.279 + if (stricmp(buffer, "SearchPaths") == 0)
1.280 + {
1.281 + search = true;
1.282 + }
1.283 +
1.284 + if (!search)
1.285 + {
1.286 + continue;
1.287 + }
1.288 +
1.289 + UTIL_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
1.290 + if (stricmp(key, "Game") == 0 || stricmp(key, "GameBin") == 0)
1.291 + {
1.292 + if (stricmp(key, "Game") == 0)
1.293 + {
1.294 + gamebin = false;
1.295 + }
1.296 + else
1.297 + {
1.298 + gamebin = true;
1.299 + }
1.300 +
1.301 + if (strncmp(val, "|gameinfo_path|", sizeof("|gameinfo_path|") - 1) == 0)
1.302 + {
1.303 + ptr = &(val[sizeof("|gameinfo_path|") - 1]);
1.304 + if (ptr[0] == '.')
1.305 + {
1.306 + ptr++;
1.307 + }
1.308 + lptr = mod_path.c_str();
1.309 + } else {
1.310 + ptr = val;
1.311 + lptr = cur_path;
1.312 + }
1.313 +
1.314 + size_t ptr_len = strlen(ptr);
1.315 + if (ptr[ptr_len] == '/' || ptr[ptr_len] == '\\')
1.316 + {
1.317 + ptr[--ptr_len] = '\0';
1.318 + }
1.319 +
1.320 + /* No need to append "bin" if key is GameBin */
1.321 + if (gamebin)
1.322 + {
1.323 + g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s", lptr, ptr, SERVER_DLL);
1.324 + }
1.325 + else if (!ptr[0])
1.326 + {
1.327 + g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s", lptr, "bin", SERVER_DLL);
1.328 + }
1.329 + else
1.330 + {
1.331 + g_Metamod.PathFormat(temp_path, PATH_SIZE, "%s/%s/%s/%s", lptr, ptr, "bin", SERVER_DLL);
1.332 + }
1.333 +
1.334 + /* If not path to SourceMM... */
1.335 + if (!UTIL_PathCmp(mm_path, temp_path))
1.336 + {
1.337 + FILE *temp_fp = fopen(temp_path, "rb");
1.338 + if (!temp_fp)
1.339 + {
1.340 + continue;
1.341 + }
1.342 + fclose(temp_fp);
1.343 +
1.344 + /* Search for a matching game dll */
1.345 + List<game_dll_t *>::iterator iter;
1.346 + game_dll_t *pCheck;
1.347 + bool found = false;
1.348 + for (iter = gamedll_list.begin(); iter != gamedll_list.end(); iter++)
1.349 + {
1.350 + pCheck = (*iter);
1.351 + if (GetFileOfAddress((void *)pCheck->factory, buffer, sizeof(buffer)))
1.352 + {
1.353 + if (UTIL_PathCmp(temp_path, buffer))
1.354 + {
1.355 + found = true;
1.356 + break;
1.357 + }
1.358 + }
1.359 + }
1.360 +
1.361 + if (found)
1.362 + {
1.363 + continue;
1.364 + }
1.365 +
1.366 + HINSTANCE gamedll = dlmount(temp_path);
1.367 + if (gamedll == NULL)
1.368 + {
1.369 + continue;
1.370 + }
1.371 +
1.372 + CreateInterfaceFn fn = (CreateInterfaceFn)dlsym(gamedll, "CreateInterface");
1.373 + if (fn == NULL)
1.374 + {
1.375 + dlclose(gamedll);
1.376 + continue;
1.377 + }
1.378 +
1.379 + game_dll_t *pInfo = new game_dll_t;
1.380 + pInfo->factory = fn;
1.381 + pInfo->lib = gamedll;
1.382 + gamedll_list.push_back(pInfo);
1.383 + break;
1.384 + }
1.385 + }
1.386 + }
1.387 + fclose(fp);
1.388 + }
1.389 +
1.390 + if (!is_gamedll_loaded)
1.391 + {
1.392 + if (strncmp(iface, "ServerGameDLL", 13) == 0)
1.393 + {
1.394 + List<game_dll_t *>::iterator iter;
1.395 + game_dll_t *pInfo;
1.396 + for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
1.397 + {
1.398 + pInfo = (*iter);
1.399 + if ((server = (IServerGameDLL *)((pInfo->factory)(iface, ret))) != NULL)
1.400 + {
1.401 + if ((gamedll_version = provider->TryServerGameDLL(iface)) != 0)
1.402 + {
1.403 + /* We have a match. Erase us from the list and save our info. */
1.404 + gamedll_list.erase(iter);
1.405 + gamedll_info = *pInfo;
1.406 + delete pInfo;
1.407 + is_gamedll_loaded = true;
1.408 + break;
1.409 + }
1.410 + /* :TODO: error otherwise? */
1.411 + }
1.412 + }
1.413 +
1.414 + if (is_gamedll_loaded)
1.415 + {
1.416 + ClearGamedllList();
1.417 + InitMainStates();
1.418 + }
1.419 + else
1.420 + {
1.421 + if (ret)
1.422 + {
1.423 + *ret = IFACE_FAILED;
1.424 + }
1.425 + return NULL;
1.426 + }
1.427 + }
1.428 + else
1.429 + {
1.430 + /* wtf do we do... */
1.431 + /* :TODO: .. something a bit more intelligent? */
1.432 + provider->DisplayError("Engine requested unknown interface before GameDLL was known!\n");
1.433 + return NULL;
1.434 + }
1.435 + }
1.436 +
1.437 + /* If we got here, there's definitely a GameDLL */
1.438 + IFACE_MACRO(gamedll_info.factory, GameDLL);
1.439 +}
1.440 +
1.441 +void ClearGamedllList()
1.442 +{
1.443 + List<game_dll_t *>::iterator iter;
1.444 +
1.445 + game_dll_t *pInfo;
1.446 + for (iter = gamedll_list.begin(); iter != gamedll_list.end(); iter++)
1.447 + {
1.448 + pInfo = (*iter);
1.449 + dlclose(pInfo->lib);
1.450 + delete pInfo;
1.451 + }
1.452 +
1.453 + gamedll_list.clear();
1.454 +}
1.455 +
1.456 +int LoadPluginsFromFile(const char *_file)
1.457 +{
1.458 + FILE *fp;
1.459 + int total = 0, skipped=0;
1.460 + PluginId id;
1.461 + bool already;
1.462 +
1.463 + fp = fopen(_file, "rt");
1.464 + if (!fp)
1.465 + {
1.466 + LogMessage("[META] Could not open plugins file %s\n", _file);
1.467 + return -1;
1.468 + }
1.469 +
1.470 + char buffer[255], error[255], full_path[255];
1.471 + const char *ptr, *ext, *file;
1.472 + size_t length;
1.473 + while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
1.474 + {
1.475 + UTIL_TrimLeft(buffer);
1.476 + UTIL_TrimRight(buffer);
1.477 +
1.478 + length = strlen(buffer);
1.479 + if (!length)
1.480 + {
1.481 + continue;
1.482 + }
1.483 +
1.484 + if (buffer[0] == '\0' || buffer[0] == ';' || strncmp(buffer, "//", 2) == 0)
1.485 + {
1.486 + continue;
1.487 + }
1.488 +
1.489 + file = buffer;
1.490 + if (buffer[0] == '"')
1.491 + {
1.492 + char *cptr = buffer;
1.493 + file = ++cptr;
1.494 +
1.495 + while (*cptr)
1.496 + {
1.497 + if (*cptr == '"')
1.498 + {
1.499 + *cptr = '\0';
1.500 + break;
1.501 + }
1.502 + cptr++;
1.503 + }
1.504 + }
1.505 + else
1.506 + {
1.507 + char *cptr = buffer;
1.508 + while (*cptr)
1.509 + {
1.510 + if (isspace(*cptr))
1.511 + {
1.512 + char *optr = cptr;
1.513 + while (*cptr && isspace(*cptr))
1.514 + {
1.515 + cptr++;
1.516 + }
1.517 + *optr = '\0';
1.518 + UTIL_TrimRight(cptr);
1.519 + if (*cptr && isalpha(*cptr))
1.520 + {
1.521 + g_PluginMngr.SetAlias(buffer, cptr);
1.522 + file = cptr;
1.523 + }
1.524 + break;
1.525 + }
1.526 + cptr++;
1.527 + }
1.528 + }
1.529 + if (!file[0])
1.530 + {
1.531 + continue;
1.532 + }
1.533 + /* First find if it's an absolute path or not... */
1.534 + if (file[0] == '/' || strncmp(&(file[1]), ":\\", 2) == 0)
1.535 + {
1.536 + /* If we're in an absolute path, ignore our normal heuristics */
1.537 + id = g_PluginMngr.Load(file, Pl_File, already, error, sizeof(error));
1.538 + if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
1.539 + {
1.540 + LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
1.541 + }
1.542 + else
1.543 + {
1.544 + if (already)
1.545 + {
1.546 + skipped++;
1.547 + }
1.548 + else
1.549 + {
1.550 + total++;
1.551 + }
1.552 + }
1.553 + }
1.554 + else
1.555 + {
1.556 + /* Attempt to find a file extension */
1.557 + ptr = UTIL_GetExtension(file);
1.558 + /* Add an extension if there's none there */
1.559 + if (!ptr)
1.560 + {
1.561 +#if defined WIN32 || defined _WIN32
1.562 + ext = ".dll";
1.563 +#else
1.564 + ext = "_i486.so";
1.565 +#endif
1.566 + }
1.567 + else
1.568 + {
1.569 + ext = "";
1.570 + }
1.571 + /* Format the new path */
1.572 + g_Metamod.PathFormat(full_path, sizeof(full_path), "%s/%s%s", mod_path.c_str(), file, ext);
1.573 + id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error));
1.574 + if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
1.575 + {
1.576 + LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
1.577 + }
1.578 + else
1.579 + {
1.580 + if (already)
1.581 + {
1.582 + skipped++;
1.583 + }
1.584 + else
1.585 + {
1.586 + total++;
1.587 + }
1.588 + }
1.589 + }
1.590 + }
1.591 + fclose(fp);
1.592 +
1.593 + if (skipped)
1.594 + {
1.595 + LogMessage("[META] Loaded %d plugins from file (%d already loaded)", total, skipped);
1.596 + }
1.597 + else
1.598 + {
1.599 + LogMessage("[META] Loaded %d plugins from file.", total);
1.600 + }
1.601 +
1.602 + return total;
1.603 +}
1.604 +
1.605 +void InitializeVSP()
1.606 +{
1.607 + size_t len;
1.608 + char engine_file[PATH_SIZE];
1.609 + char engine_path[PATH_SIZE];
1.610 + char rel_path[PATH_SIZE * 2];
1.611 +
1.612 + GetFileOfAddress((void *)engine_factory, engine_file, sizeof(engine_file));
1.613 +
1.614 + /* Chop off the "engine" file part */
1.615 + len = strlen(engine_file);
1.616 + for (size_t i = len - 1; i >= 0 && i < len; i--)
1.617 + {
1.618 + if (engine_file[i] == '/' || engine_file[i] == '\\')
1.619 + {
1.620 + engine_file[i] = '\0';
1.621 + break;
1.622 + }
1.623 + }
1.624 + abspath(engine_path, engine_file);
1.625 +
1.626 + const char *usepath = metamod_path.c_str();
1.627 + if (UTIL_Relatize(rel_path, sizeof(rel_path), engine_path, metamod_path.c_str()))
1.628 + {
1.629 + usepath = rel_path;
1.630 + }
1.631 +
1.632 + char command[PATH_SIZE * 2];
1.633 + UTIL_Format(command, sizeof(command), "plugin_load \"%s\"\n", usepath);
1.634 + provider->ServerCommand(command);
1.635 +}
1.636 +
1.637 +/* Wrapper function. This is called when the GameDLL thinks it's using
1.638 + * the engine's real engineFactory.
1.639 + */
1.640 +void *EngineFactory(const char *iface, int *ret)
1.641 +{
1.642 + IFACE_MACRO(engine_factory, Engine);
1.643 +}
1.644 +
1.645 +/* Wrapper function. This is called when the GameDLL thinks it's using
1.646 + * the engine's real physicsFactory.
1.647 + */
1.648 +void *PhysicsFactory(const char *iface, int *ret)
1.649 +{
1.650 + IFACE_MACRO(physics_factory, Physics);
1.651 +}
1.652 +
1.653 +/* Wrapper function. This is called when the GameDLL thinks it's using
1.654 + * the engine's real fileSystemFactory.
1.655 + */
1.656 +void *FileSystemFactory(const char *iface, int *ret)
1.657 +{
1.658 + IFACE_MACRO(filesystem_factory, FileSystem);
1.659 +}
1.660 +
1.661 +void LogMessage(const char *msg, ...)
1.662 +{
1.663 + va_list ap;
1.664 + static char buffer[2048];
1.665 +
1.666 + va_start(ap, msg);
1.667 + size_t len = vsnprintf(buffer, sizeof(buffer) - 2, msg, ap);
1.668 + va_end(ap);
1.669 +
1.670 + buffer[len++] = '\n';
1.671 + buffer[len] = '\0';
1.672 +
1.673 + if (!provider->LogMessage(buffer))
1.674 + {
1.675 + fprintf(stdout, "%s", buffer);
1.676 + }
1.677 +}
1.678 +
1.679 +bool Handler_DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
1.680 +{
1.681 + engine_factory = engineFactory;
1.682 + filesystem_factory = filesystemFactory;
1.683 + physics_factory = physicsFactory;
1.684 + gpGlobals = pGlobals;
1.685 +
1.686 + provider->Notify_DLLInit_Pre(server, engineFactory, gamedll_info.factory);
1.687 +
1.688 + metamod_version = provider->CreateConVar("metamod_version",
1.689 + SOURCEMM_VERSION,
1.690 + "Metamod:Source Version",
1.691 + ConVarFlag_Notify|ConVarFlag_Replicated|ConVarFlag_SpOnly);
1.692 +
1.693 + mm_pluginsfile = provider->CreateConVar("mm_pluginsfile",
1.694 +#if defined WIN32 || defined _WIN32
1.695 + "addons\\metamod\\metaplugins.ini",
1.696 +#else
1.697 + "addons/metamod/metaplugins.ini",
1.698 +#endif
1.699 + "Metamod:Source Plugins File",
1.700 + ConVarFlag_SpOnly);
1.701 +
1.702 + const char *pluginFile = provider->GetCommandLineValue("mm_pluginsfile", NULL);
1.703 + if (!pluginFile)
1.704 + {
1.705 + pluginFile = provider->GetConVarString(mm_pluginsfile);
1.706 + if (pluginFile == NULL)
1.707 + {
1.708 + pluginFile = "addons/metamod/metaplugins.ini";
1.709 + }
1.710 + }
1.711 +
1.712 + char full_path[260];
1.713 + g_Metamod.PathFormat(full_path, sizeof(full_path), "%s/%s", mod_path.c_str(), pluginFile);
1.714 +
1.715 + LoadPluginsFromFile(full_path);
1.716 +
1.717 + in_first_level = true;
1.718 +
1.719 + RETURN_META_VALUE(MRES_IGNORED, true);
1.720 +}
1.721 +
1.722 +bool Handler_GameInit()
1.723 +{
1.724 + if (is_game_init)
1.725 + {
1.726 + return true;
1.727 + }
1.728 +
1.729 + if (vsp_load_requested)
1.730 + {
1.731 + InitializeVSP();
1.732 + }
1.733 +
1.734 + is_game_init = true;
1.735 +
1.736 + RETURN_META_VALUE(MRES_IGNORED, true);
1.737 +}
1.738 +
1.739 +bool Handler_DLLInit_Post(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
1.740 +{
1.741 + g_PluginMngr.SetAllLoaded();
1.742 + RETURN_META_VALUE(MRES_IGNORED, true);
1.743 +}
1.744 +
1.745 +void Handler_DLLShutdown()
1.746 +{
1.747 + /* Unload plugins */
1.748 + g_PluginMngr.UnloadAll();
1.749 +
1.750 + provider->Notify_DLLShutdown_Pre();
1.751 +
1.752 + SH_CALL(server, &IServerGameDLL::DLLShutdown)();
1.753 +
1.754 + g_SourceHook.CompleteShutdown();
1.755 +
1.756 + if (is_gamedll_loaded && gamedll_info.lib)
1.757 + {
1.758 + dlclose(gamedll_info.lib);
1.759 + is_gamedll_loaded = false;
1.760 + }
1.761 +
1.762 + RETURN_META(MRES_SUPERCEDE);
1.763 +}
1.764 +
1.765 +void Handler_LevelShutdown(void)
1.766 +{
1.767 + if (!in_first_level)
1.768 + {
1.769 + char full_path[255];
1.770 + g_Metamod.PathFormat(full_path,
1.771 + sizeof(full_path),
1.772 + "%s/%s",
1.773 + mod_path.c_str(),
1.774 + provider->GetConVarString(mm_pluginsfile));
1.775 +
1.776 + LoadPluginsFromFile(full_path);
1.777 + }
1.778 + else
1.779 + {
1.780 + in_first_level = false;
1.781 + }
1.782 +
1.783 + ITER_EVENT(OnLevelShutdown, ());
1.784 +
1.785 + RETURN_META(MRES_IGNORED);
1.786 +}
1.787 +
1.788 +bool Handler_LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
1.789 +{
1.790 + ITER_EVENT(OnLevelInit, (pMapName, pMapEntities, pOldLevel, pLandmarkName, loadGame, background));
1.791 +
1.792 + RETURN_META_VALUE(MRES_IGNORED, false);
1.793 +}
1.794 +
1.795 +void MetamodSource::LogMsg(ISmmPlugin *pl, const char *msg, ...)
1.796 +{
1.797 + va_list ap;
1.798 + char buffer[2048];
1.799 +
1.800 + va_start(ap, msg);
1.801 + UTIL_FormatArgs(buffer, sizeof(buffer), msg, ap);
1.802 + va_end(ap);
1.803 +
1.804 + LogMessage("[%s] %s", pl->GetLogTag(), buffer);
1.805 +}
1.806 +
1.807 +CreateInterfaceFn MetamodSource::engineFactory(bool syn/* =true */)
1.808 +{
1.809 + if (syn)
1.810 + {
1.811 + return EngineFactory;
1.812 + }
1.813 +
1.814 + return engine_factory;
1.815 +}
1.816 +
1.817 +CreateInterfaceFn MetamodSource::physicsFactory(bool syn/* =true */)
1.818 +{
1.819 + if (syn)
1.820 + {
1.821 + return PhysicsFactory;
1.822 + }
1.823 +
1.824 + return physics_factory;
1.825 +}
1.826 +
1.827 +CreateInterfaceFn MetamodSource::fileSystemFactory(bool syn/* =true */)
1.828 +{
1.829 + if (syn)
1.830 + {
1.831 + return FileSystemFactory;
1.832 + }
1.833 +
1.834 + return filesystem_factory;
1.835 +}
1.836 +
1.837 +CreateInterfaceFn MetamodSource::serverFactory(bool syn/* =true */)
1.838 +{
1.839 + if (syn)
1.840 + {
1.841 + return CreateInterface;
1.842 + }
1.843 +
1.844 + return gamedll_info.factory;
1.845 +}
1.846 +
1.847 +CGlobalVars *MetamodSource::pGlobals()
1.848 +{
1.849 + return gpGlobals;
1.850 +}
1.851 +
1.852 +void MetamodSource::SetLastMetaReturn(META_RES res)
1.853 +{
1.854 + last_meta_res = res;
1.855 +}
1.856 +
1.857 +META_RES MetamodSource::GetLastMetaReturn()
1.858 +{
1.859 + return last_meta_res;
1.860 +}
1.861 +
1.862 +void MetamodSource::ConPrint(const char *str)
1.863 +{
1.864 + provider->ConsolePrint(str);
1.865 +}
1.866 +
1.867 +void MetamodSource::ConPrintf(const char *fmt, ...)
1.868 +{
1.869 + va_list ap;
1.870 + char buffer[2048];
1.871 +
1.872 + va_start(ap, fmt);
1.873 + UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
1.874 + va_end(ap);
1.875 +
1.876 + provider->ConsolePrint(buffer);
1.877 +}
1.878 +
1.879 +void MetamodSource::GetApiVersions(int &major, int &minor, int &plvers, int &plmin)
1.880 +{
1.881 + major = SM_VERS_API_MAJOR;
1.882 + minor = SM_VERS_API_MINOR;
1.883 + plvers = PLAPI_VERSION;
1.884 + plmin = PLAPI_MIN_VERSION;
1.885 +}
1.886 +
1.887 +void MetamodSource::GetShVersions(int &shvers, int &shimpl)
1.888 +{
1.889 + shvers = SH_IFACE_VERSION;
1.890 + shimpl = SH_IMPL_VERSION;
1.891 +}
1.892 +
1.893 +int MetamodSource::FormatIface(char iface[], unsigned int maxlength)
1.894 +{
1.895 + int length = (int)strlen(iface);
1.896 + int i;
1.897 + int num = 0;
1.898 +
1.899 + for (i = length - 1; i >= 0; i--)
1.900 + {
1.901 + if (!isdigit(iface[i]))
1.902 + {
1.903 + if (i != length - 1)
1.904 + {
1.905 + num = 1;
1.906 + }
1.907 + break;
1.908 + }
1.909 + }
1.910 +
1.911 + if ( (num && ((int)maxlength <= length)) || (!num && ((int)maxlength <= length + 3)) )
1.912 + {
1.913 + return -1;
1.914 + }
1.915 +
1.916 + if (i != length - 1)
1.917 + {
1.918 + num = atoi(&(iface[++i]));
1.919 + }
1.920 +
1.921 + num++;
1.922 +
1.923 + snprintf(&(iface[i]), 4, "%03d", num);
1.924 +
1.925 + return num;
1.926 +}
1.927 +
1.928 +void *MetamodSource::InterfaceSearch(CreateInterfaceFn fn, const char *iface, int max, int *ret)
1.929 +{
1.930 + char _if[256]; /* assume no interface goes beyond this */
1.931 + size_t len = strlen(iface);
1.932 + int num = 0;
1.933 + void *pf = NULL;
1.934 +
1.935 + if (max > 999)
1.936 + {
1.937 + max = 999;
1.938 + }
1.939 +
1.940 + if (len + 4 > sizeof(_if))
1.941 + {
1.942 + if (ret)
1.943 + {
1.944 + *ret = IFACE_FAILED;
1.945 + }
1.946 + return NULL;
1.947 + }
1.948 +
1.949 + strcpy(_if, iface);
1.950 +
1.951 + do
1.952 + {
1.953 + if ((pf = (fn)(_if, ret)) != NULL)
1.954 + {
1.955 + break;
1.956 + }
1.957 + if (num > max)
1.958 + {
1.959 + break;
1.960 + }
1.961 + } while ((num = FormatIface(_if, len+1)));
1.962 +
1.963 + return pf;
1.964 +}
1.965 +
1.966 +void *MetamodSource::VInterfaceMatch(CreateInterfaceFn fn, const char *iface, int min)
1.967 +{
1.968 + char buffer[256]; /* assume no interface will go beyond this */
1.969 + size_t len = strlen(iface);
1.970 + int ret; /* just in case something doesn't handle NULL properly */
1.971 +
1.972 + if (len > sizeof(buffer) - 4)
1.973 + {
1.974 + return NULL;
1.975 + }
1.976 +
1.977 + strcpy(buffer, iface);
1.978 +
1.979 + if (min != -1)
1.980 + {
1.981 + char *ptr = &buffer[len - 1];
1.982 + int digits = 0;
1.983 + while (isdigit(*ptr) && digits <=3)
1.984 + {
1.985 + *ptr = '\0';
1.986 + digits++;
1.987 + ptr--;
1.988 + }
1.989 + if (digits != 3)
1.990 + {
1.991 + /* for now, assume this is an error */
1.992 + strcpy(buffer, iface);
1.993 + }
1.994 + else
1.995 + {
1.996 + char num[4];
1.997 + min = (min == 0) ? 1 : min;
1.998 + snprintf(num, sizeof(num), "%03d", min);
1.999 + strcat(buffer, num);
1.1000 + }
1.1001 + }
1.1002 +
1.1003 + return InterfaceSearch(fn, buffer, IFACE_MAXNUM, &ret);
1.1004 +}
1.1005 +
1.1006 +const char *MetamodSource::GetBaseDir()
1.1007 +{
1.1008 + return mod_path.c_str();
1.1009 +}
1.1010 +
1.1011 +void MetamodSource::PathFormat(char *buffer, size_t len, const char *fmt, ...)
1.1012 +{
1.1013 + va_list ap;
1.1014 + va_start(ap, fmt);
1.1015 + size_t mylen = UTIL_FormatArgs(buffer, len, fmt, ap);
1.1016 + va_end(ap);
1.1017 +
1.1018 + for (size_t i = 0; i < mylen; i++)
1.1019 + {
1.1020 + if (buffer[i] == ALT_SEP_CHAR)
1.1021 + {
1.1022 + buffer[i] = PATH_SEP_CHAR;
1.1023 + }
1.1024 + }
1.1025 +}
1.1026 +
1.1027 +void MetamodSource::ClientConPrintf(edict_t *client, const char *fmt, ...)
1.1028 +{
1.1029 + va_list ap;
1.1030 + char buffer[2048];
1.1031 +
1.1032 + va_start(ap, fmt);
1.1033 + UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
1.1034 + va_end(ap);
1.1035 +
1.1036 + provider->ClientConsolePrint(client, buffer);
1.1037 +}
1.1038 +
1.1039 +void MetamodSource::EnableVSPListener()
1.1040 +{
1.1041 + if (is_game_init && !vsp_load_requested && !vsp_loaded)
1.1042 + {
1.1043 + InitializeVSP();
1.1044 + }
1.1045 +
1.1046 + vsp_load_requested = true;
1.1047 +}
1.1048 +
1.1049 +int MetamodSource::GetVSPVersion()
1.1050 +{
1.1051 + return vsp_version;
1.1052 +}
1.1053 +
1.1054 +int MetamodSource::GetGameDLLVersion()
1.1055 +{
1.1056 + return gamedll_version;
1.1057 +}
1.1058 +
1.1059 +bool MetamodSource::RemotePrintingAvailable()
1.1060 +{
1.1061 + return provider->IsRemotePrintingAvailable();
1.1062 +}
1.1063 +
1.1064 +void *MetamodSource::MetaFactory(const char *iface, int *ret, PluginId *id)
1.1065 +{
1.1066 + if (id)
1.1067 + {
1.1068 + *id = 0;
1.1069 + }
1.1070 +
1.1071 + if (!iface)
1.1072 + {
1.1073 + return NULL;
1.1074 + }
1.1075 +
1.1076 + /* First check ours... we get first chance! */
1.1077 + if (strcmp(iface, MMIFACE_SOURCEHOOK) == 0)
1.1078 + {
1.1079 + if (ret)
1.1080 + {
1.1081 + *ret = IFACE_OK;
1.1082 + }
1.1083 + return static_cast<void *>(static_cast<SourceHook::ISourceHook *>(&g_SourceHook));
1.1084 + }
1.1085 + else if (strcmp(iface, MMIFACE_PLMANAGER) == 0)
1.1086 + {
1.1087 + if (ret)
1.1088 + {
1.1089 + *ret = IFACE_OK;
1.1090 + }
1.1091 + return static_cast<void *>(static_cast<ISmmPluginManager *>(&g_PluginMngr));
1.1092 + }
1.1093 +
1.1094 + CPluginManager::CPlugin *pl;
1.1095 + List<IMetamodListener *>::iterator event;
1.1096 + IMetamodListener *api;
1.1097 + void *value;
1.1098 +
1.1099 + int subret = 0;
1.1100 + for (PluginIter iter = g_PluginMngr._begin();
1.1101 + iter != g_PluginMngr._end();
1.1102 + iter++)
1.1103 + {
1.1104 + pl = (*iter);
1.1105 + for (event = pl->m_Events.begin(); event != pl->m_Events.end(); event++)
1.1106 + {
1.1107 + api = (*event);
1.1108 + subret = IFACE_FAILED;
1.1109 + if ((value = api->OnMetamodQuery(iface, &subret)) != NULL)
1.1110 + {
1.1111 + if (ret)
1.1112 + {
1.1113 + *ret = subret;
1.1114 + }
1.1115 + if (id)
1.1116 + {
1.1117 + *id = pl->m_Id;
1.1118 + }
1.1119 + return value;
1.1120 + }
1.1121 + }
1.1122 + }
1.1123 +
1.1124 + if (ret)
1.1125 + {
1.1126 + *ret = IFACE_FAILED;
1.1127 + }
1.1128 +
1.1129 + return NULL;
1.1130 +}
1.1131 +
1.1132 +void MetamodSource::AddListener(ISmmPlugin *plugin, IMetamodListener *pListener)
1.1133 +{
1.1134 + CPluginManager::CPlugin *pl = g_PluginMngr.FindByAPI(plugin);
1.1135 +
1.1136 + pl->m_Events.push_back(pListener);
1.1137 +}
1.1138 +
1.1139 +const char *MetamodSource::GetGameBinaryPath()
1.1140 +{
1.1141 + return full_bin_path.c_str();
1.1142 +}
1.1143 +
1.1144 +const char *MetamodSource::GetPluginsFile()
1.1145 +{
1.1146 + return provider->GetConVarString(mm_pluginsfile);
1.1147 +}
1.1148 +
1.1149 +IConCommandBaseAccessor *MetamodSource::GetCvarBaseAccessor()
1.1150 +{
1.1151 + return provider->GetConCommandBaseAccessor();
1.1152 +}
1.1153 +
1.1154 +bool MetamodSource::RegisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
1.1155 +{
1.1156 + if (provider->IsConCommandBaseACommand(pCommand))
1.1157 + {
1.1158 + g_PluginMngr.AddPluginCmd(plugin, pCommand);
1.1159 + }
1.1160 + else
1.1161 + {
1.1162 + g_PluginMngr.AddPluginCvar(plugin, pCommand);
1.1163 + }
1.1164 +
1.1165 + return provider->RegisterConCommandBase(pCommand);
1.1166 +}
1.1167 +
1.1168 +void MetamodSource::UnregisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
1.1169 +{
1.1170 + if (provider->IsConCommandBaseACommand(pCommand))
1.1171 + {
1.1172 + g_PluginMngr.RemovePluginCmd(plugin, pCommand);
1.1173 + }
1.1174 + else
1.1175 + {
1.1176 + g_PluginMngr.RemovePluginCvar(plugin, pCommand);
1.1177 + }
1.1178 +
1.1179 + CPluginManager::CPlugin *pOrig = g_PluginMngr.FindByAPI(plugin);
1.1180 + UnregisterConCommandBase(pOrig ? pOrig->m_Id : 0, pCommand);
1.1181 +}
1.1182 +
1.1183 +void MetamodSource::UnregisterConCommandBase(PluginId id, ConCommandBase *pCommand)
1.1184 +{
1.1185 + PluginIter iter;
1.1186 + CPluginManager::CPlugin *pPlugin;
1.1187 + List<IMetamodListener *>::iterator event;
1.1188 + IMetamodListener *pML;
1.1189 + for (iter=g_PluginMngr._begin(); iter!=g_PluginMngr._end(); iter++)
1.1190 + {
1.1191 + pPlugin = (*iter);
1.1192 + if (pPlugin->m_Status < Pl_Paused)
1.1193 + {
1.1194 + continue;
1.1195 + }
1.1196 + /* Only valid for plugins >= 12 (v1:6, SourceMM 1.5) */
1.1197 + if (pPlugin->m_API->GetApiVersion() < 12)
1.1198 + {
1.1199 + continue;
1.1200 + }
1.1201 + for (event=pPlugin->m_Events.begin();
1.1202 + event!=pPlugin->m_Events.end();
1.1203 + event++)
1.1204 + {
1.1205 + pML = (*event);
1.1206 + pML->OnUnlinkConCommandBase(id, pCommand);
1.1207 + }
1.1208 + }
1.1209 +
1.1210 + return provider->UnregisterConCommandBase(pCommand);
1.1211 +}
1.1212 +
1.1213 +int MetamodSource::GetUserMessageCount()
1.1214 +{
1.1215 + return provider->GetUserMessageCount();
1.1216 +}
1.1217 +
1.1218 +int MetamodSource::FindUserMessage(const char *name, int *size/* =NULL */)
1.1219 +{
1.1220 + return provider->FindUserMessage(name, size);
1.1221 +}
1.1222 +
1.1223 +const char *MetamodSource::GetUserMessage(int index, int *size/* =NULL */)
1.1224 +{
1.1225 + return provider->GetUserMessage(index, size);
1.1226 +}
1.1227 +
1.1228 +int MetamodSource::GetSourceEngineBuild()
1.1229 +{
1.1230 + return engine_build;
1.1231 +}
1.1232 +
1.1233 +void MetamodSource::NotifyVSPListening(IServerPluginCallbacks *callbacks)
1.1234 +{
1.1235 + ITER_EVENT(OnVSPListening, (callbacks));
1.1236 +}
1.1237 +
1.1238 +IServerPluginCallbacks *MetamodSource::GetVSPInfo(int *pVersion)
1.1239 +{
1.1240 + if (pVersion)
1.1241 + {
1.1242 + *pVersion = vsp_version;
1.1243 + }
1.1244 +
1.1245 + return vsp_callbacks;
1.1246 +}
1.1247 +