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