1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcemm/sourcemm.cpp Tue Sep 18 20:03:47 2007 +0000
1.3 @@ -0,0 +1,717 @@
1.4 +/* ======== SourceMM ========
1.5 + * Copyright (C) 2004-2007 Metamod:Source Development Team
1.6 + * No warranties of any kind
1.7 + *
1.8 + * License: zlib/libpng
1.9 + *
1.10 + * Author(s): David "BAILOPAN" Anderson
1.11 + * Contributor(s): Scott "Damaged Soul" Ehlert
1.12 + * : Pavol "PM OnoTo" Marko
1.13 + * ============================
1.14 + */
1.15 +
1.16 +#include <interface.h>
1.17 +#include <eiface.h>
1.18 +#include <tier0/icommandline.h>
1.19 +#include "sourcemm.h"
1.20 +#include "concommands.h"
1.21 +#include "CSmmAPI.h"
1.22 +#include "CPlugin.h"
1.23 +#include "util.h"
1.24 +#include "vsp_listener.h"
1.25 +
1.26 +using namespace SourceMM;
1.27 +
1.28 +/**
1.29 + * @brief Implementation of main SourceMM GameDLL functionality
1.30 + * @file sourcemm.cpp
1.31 + */
1.32 +
1.33 +#undef CommandLine
1.34 +DLL_IMPORT ICommandLine *CommandLine();
1.35 +
1.36 +SH_DECL_HOOK4(IServerGameDLL, DLLInit, SH_NOATTRIB, false, bool, CreateInterfaceFn, CreateInterfaceFn, CreateInterfaceFn, CGlobalVars *);
1.37 +SH_DECL_HOOK0_void(IServerGameDLL, DLLShutdown, SH_NOATTRIB, false);
1.38 +SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
1.39 +SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
1.40 +SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
1.41 +SH_DECL_HOOK0(IServerGameDLL, GameInit, SH_NOATTRIB, false, bool);
1.42 +
1.43 +bool DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals);
1.44 +bool DLLInit_Post(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals);
1.45 +void DLLShutdown_handler();
1.46 +void LevelShutdown_handler();
1.47 +bool LevelInit_handler(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background);
1.48 +bool GameInit_handler();
1.49 +
1.50 +GameDllInfo g_GameDll = {false, NULL, NULL, NULL, NULL};
1.51 +EngineInfo g_Engine;
1.52 +SourceHook::CSourceHookImpl g_SourceHook;
1.53 +SourceHook::ISourceHook *g_SHPtr = &g_SourceHook;
1.54 +SourceHook::String g_ModPath;
1.55 +SourceHook::String g_BinPath;
1.56 +SourceHook::String g_SmmPath;
1.57 +PluginId g_PLID = Pl_Console; /* Technically, SourceMM is the "Console" plugin... :p */
1.58 +bool bInFirstLevel = true;
1.59 +bool gParsedGameInfo = false;
1.60 +bool bGameInit = false;
1.61 +SourceHook::List<GameDllInfo *> gamedll_list;
1.62 +SourceHook::CallClass<IServerGameDLL> *g_GameDllPatch;
1.63 +int g_GameDllVersion = 0;
1.64 +const char VSPIFACE_001[] = "ISERVERPLUGINCALLBACKS001";
1.65 +const char VSPIFACE_002[] = "ISERVERPLUGINCALLBACKS002";
1.66 +const char GAMEINFO_PATH[] = "|gameinfo_path|";
1.67 +
1.68 +void ClearGamedllList();
1.69 +
1.70 +/* Helper Macro */
1.71 +#define IFACE_MACRO(orig,nam) \
1.72 + CPluginManager::CPlugin *pl; \
1.73 + SourceHook::List<IMetamodListener *>::iterator event; \
1.74 + IMetamodListener *api; \
1.75 + int mret = 0; \
1.76 + void *val = NULL; \
1.77 + for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
1.78 + pl = (*iter); \
1.79 + for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++) { \
1.80 + api = (*event); \
1.81 + mret = IFACE_FAILED; \
1.82 + if ( (val=api->On##nam##Query(iface, &mret)) != NULL ) { \
1.83 + if (ret) *ret = mret; \
1.84 + return val; \
1.85 + } \
1.86 + } \
1.87 + } \
1.88 + return (orig)(iface, ret);
1.89 +
1.90 +#define ITER_EVENT(evn, args) \
1.91 + CPluginManager::CPlugin *pl; \
1.92 + SourceHook::List<IMetamodListener *>::iterator event; \
1.93 + IMetamodListener *api; \
1.94 + for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
1.95 + pl = (*iter); \
1.96 + for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++) { \
1.97 + api = (*event); \
1.98 + api->evn args; \
1.99 + } \
1.100 + }
1.101 +
1.102 +///////////////////////////////////
1.103 +// Main code for HL2 Interaction //
1.104 +///////////////////////////////////
1.105 +
1.106 +/* Initialize everything here */
1.107 +void InitMainStates()
1.108 +{
1.109 + char full_path[PATH_SIZE] = {0};
1.110 + GetFileOfAddress((void *)g_GameDll.factory, full_path, sizeof(full_path));
1.111 + g_BinPath.assign(full_path);
1.112 +
1.113 + /* Like Metamod, reload plugins at the end of the map.
1.114 + * This is so plugins can hook everything on load, BUT, new plugins will be reloaded
1.115 + * if the server is shut down (silly, but rare case).
1.116 + */
1.117 + bInFirstLevel = true;
1.118 +
1.119 + SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLInit, g_GameDll.pGameDLL, DLLInit, false);
1.120 + SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLInit, g_GameDll.pGameDLL, DLLInit_Post, true);
1.121 + SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLShutdown, g_GameDll.pGameDLL, DLLShutdown_handler, false);
1.122 + SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelShutdown, g_GameDll.pGameDLL, LevelShutdown_handler, true);
1.123 + SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelInit, g_GameDll.pGameDLL, LevelInit_handler, true);
1.124 + SH_ADD_HOOK_STATICFUNC(IServerGameDLL, GameInit, g_GameDll.pGameDLL, GameInit_handler, false);
1.125 +}
1.126 +
1.127 +bool DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
1.128 +{
1.129 + g_Engine.engineFactory = engineFactory;
1.130 + g_Engine.fileSystemFactory = filesystemFactory;
1.131 + g_Engine.physicsFactory = physicsFactory;
1.132 + g_Engine.pGlobals = pGlobals;
1.133 +
1.134 + g_Engine.engine = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
1.135 + if (!g_Engine.engine)
1.136 + {
1.137 + Error("Could not find IVEngineServer! Metamod cannot load.");
1.138 + return false;
1.139 + }
1.140 + g_Engine.icvar = (ICvar *)((engineFactory)(VENGINE_CVAR_INTERFACE_VERSION , NULL));
1.141 + if (!g_Engine.icvar)
1.142 + {
1.143 + Error("Could not find ICvar! Metamod cannot load.");
1.144 + return false;
1.145 + }
1.146 +
1.147 + g_Engine.loaded = true;
1.148 +
1.149 + /* Initialize our console hooks */
1.150 + ConCommandBaseMgr::OneTimeInit(static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor));
1.151 +
1.152 + g_GameDllPatch = SH_GET_CALLCLASS(g_GameDll.pGameDLL);
1.153 +
1.154 + if (g_GameDll.pGameClients)
1.155 + {
1.156 + SH_ADD_HOOK_STATICFUNC(IServerGameClients, ClientCommand, g_GameDll.pGameClients, ClientCommand_handler, false);
1.157 + } else {
1.158 + /* If IServerGameClients isn't found, this really isn't a fatal error so... */
1.159 + LogMessage("[META] Warning: Could not find IServerGameClients!");
1.160 + LogMessage("[META] Warning: The 'meta' command will not be available to clients.");
1.161 + }
1.162 +
1.163 + if (!g_SmmAPI.CacheCmds())
1.164 + {
1.165 + LogMessage("[META] Warning: Failed to initialize Con_Printf. Defaulting to Msg().");
1.166 + LogMessage("[META] Warning: Console messages will not be redirected to rcon console.");
1.167 + }
1.168 +
1.169 + if (!g_SmmAPI.CacheUserMessages())
1.170 + {
1.171 + /* Don't know of a mod that has stripped out user messages completely,
1.172 + * but perhaps should do something different here?
1.173 + */
1.174 + LogMessage("[META] Warning: Failed to get list of user messages.");
1.175 + LogMessage("[META] Warning: The 'meta game' command will not display user messages.");
1.176 + }
1.177 +
1.178 + const char *pluginFile = g_Engine.icvar->GetCommandLineValue("mm_pluginsfile");
1.179 + if (!pluginFile)
1.180 + {
1.181 + pluginFile = GetPluginsFile();
1.182 + }
1.183 +
1.184 + char full_path[260];
1.185 + g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s", g_ModPath.c_str(), pluginFile);
1.186 +
1.187 + LoadPluginsFromFile(full_path);
1.188 +
1.189 + bInFirstLevel = true;
1.190 +
1.191 + RETURN_META_VALUE(MRES_IGNORED, true);
1.192 +}
1.193 +
1.194 +bool GameInit_handler()
1.195 +{
1.196 + if (bGameInit)
1.197 + {
1.198 + return true;
1.199 + }
1.200 +
1.201 + if (g_SmmAPI.VSPEnabled())
1.202 + {
1.203 + g_SmmAPI.LoadAsVSP();
1.204 + }
1.205 +
1.206 + bGameInit = true;
1.207 +
1.208 + RETURN_META_VALUE(MRES_IGNORED, true);
1.209 +}
1.210 +
1.211 +bool DLLInit_Post(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
1.212 +{
1.213 + g_PluginMngr.SetAllLoaded();
1.214 + RETURN_META_VALUE(MRES_IGNORED, true);
1.215 +}
1.216 +
1.217 +/* This is where the magic happens */
1.218 +SMM_API void *CreateInterface(const char *iface, int *ret)
1.219 +{
1.220 + /* Prevent loading of self as a SourceMM plugin or Valve server plugin :x */
1.221 + if (strcmp(iface, PLAPI_NAME) == 0)
1.222 + {
1.223 + Warning("Do not try loading Metamod:Source as a SourceMM or Valve server plugin.\n");
1.224 +
1.225 + if (ret)
1.226 + {
1.227 + *ret = IFACE_FAILED;
1.228 + }
1.229 +
1.230 + return NULL;
1.231 + }
1.232 +
1.233 + /* We check these separately because we can't reply
1.234 + * unless our interface version really matches.
1.235 + */
1.236 + if ((strcmp(iface, VSPIFACE_002) == 0)
1.237 + || strcmp(iface, VSPIFACE_001) == 0)
1.238 + {
1.239 + if (ret)
1.240 + {
1.241 + *ret = IFACE_OK;
1.242 + }
1.243 + return &g_VspListener;
1.244 + }
1.245 +
1.246 + if (!gParsedGameInfo)
1.247 + {
1.248 + gParsedGameInfo = true;
1.249 + const char *gameDir = NULL;
1.250 + char gamePath[PATH_SIZE];
1.251 + char smmPath[PATH_SIZE];
1.252 +
1.253 + /* Get path to SourceMM DLL */
1.254 + if (!GetFileOfAddress((void *)CreateInterface, smmPath, sizeof(smmPath)))
1.255 + {
1.256 + Error("GetFileOfAddress() failed! Metamod cannot load.\n");
1.257 + return NULL;
1.258 + }
1.259 +
1.260 + g_SmmPath.assign(smmPath);
1.261 +
1.262 + /* Get value of -game from command line, defaulting to hl2 as engine seems to do */
1.263 + gameDir = CommandLine()->ParmValue("-game", "hl2");
1.264 +
1.265 + /* Get absolute path */
1.266 + abspath(gamePath, gameDir);
1.267 + g_ModPath.assign(gamePath);
1.268 +
1.269 + char tempPath[PATH_SIZE];
1.270 +
1.271 + /* Path to gameinfo.txt */
1.272 + g_SmmAPI.PathFormat(tempPath, PATH_SIZE, "%s/%s", g_ModPath.c_str(), "gameinfo.txt");
1.273 +
1.274 + FILE *fp = fopen(tempPath, "rt");
1.275 +
1.276 + if (!fp)
1.277 + {
1.278 + Error("Unable to open gameinfo.txt! Metamod cannot load.\n");
1.279 + return NULL;
1.280 + }
1.281 +
1.282 + char buffer[255];
1.283 + char key[128], val[128];
1.284 + size_t len = 0;
1.285 + bool search = false;
1.286 + bool gamebin = false;
1.287 + char *ptr;
1.288 + const char *lptr;
1.289 + char curPath[PATH_SIZE];
1.290 +
1.291 + getcwd(curPath, PATH_SIZE);
1.292 +
1.293 + while (!feof(fp))
1.294 + {
1.295 + buffer[0] = '\0';
1.296 + fgets(buffer, sizeof(buffer), fp);
1.297 + len = strlen(buffer);
1.298 + if (buffer[len-1] == '\n')
1.299 + buffer[--len] = '\0';
1.300 + UTIL_TrimComments(buffer);
1.301 + UTIL_TrimLeft(buffer);
1.302 + UTIL_TrimRight(buffer);
1.303 + if (stricmp(buffer, "SearchPaths") == 0)
1.304 + search = true;
1.305 + if (!search)
1.306 + continue;
1.307 + UTIL_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
1.308 + if (stricmp(key, "Game") == 0 || stricmp(key, "GameBin") == 0)
1.309 + {
1.310 + if (stricmp(key, "Game") == 0)
1.311 + gamebin = false;
1.312 + else
1.313 + gamebin = true;
1.314 +
1.315 + if (strncmp(val, GAMEINFO_PATH, sizeof(GAMEINFO_PATH) - 1) == 0)
1.316 + {
1.317 + ptr = &(val[sizeof(GAMEINFO_PATH) - 1]);
1.318 + if (ptr[0] == '.')
1.319 + ptr++;
1.320 + lptr = g_ModPath.c_str();
1.321 + } else {
1.322 + ptr = val;
1.323 + lptr = curPath;
1.324 + }
1.325 +
1.326 + size_t ptr_len = strlen(ptr);
1.327 + if (ptr[ptr_len] == '/' || ptr[ptr_len] == '\\')
1.328 + ptr[--ptr_len] = '\0';
1.329 +
1.330 + /* No need to append "bin" if key is GameBin */
1.331 + if (gamebin)
1.332 + {
1.333 + g_SmmAPI.PathFormat(tempPath, PATH_SIZE, "%s/%s/%s", lptr, ptr, SERVER_DLL);
1.334 + } else if (!ptr[0]) {
1.335 + g_SmmAPI.PathFormat(tempPath, PATH_SIZE, "%s/%s/%s", lptr, "bin", SERVER_DLL);
1.336 + } else {
1.337 + g_SmmAPI.PathFormat(tempPath, PATH_SIZE, "%s/%s/%s/%s", lptr, ptr, "bin", SERVER_DLL);
1.338 + }
1.339 +
1.340 + /* If not path to SourceMM... */
1.341 + if (!UTIL_PathCmp(smmPath, tempPath))
1.342 + {
1.343 + FILE *fp = fopen(tempPath, "rb");
1.344 + if (!fp)
1.345 + continue;
1.346 + //:TODO: Optimize this a bit!
1.347 + SourceHook::List<GameDllInfo *>::iterator iter;
1.348 + GameDllInfo *pCheck;
1.349 + bool found = false;
1.350 + for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
1.351 + {
1.352 + pCheck = (*iter);
1.353 + if (GetFileOfAddress((void *)pCheck->factory, buffer, sizeof(buffer)))
1.354 + {
1.355 + if (UTIL_PathCmp(tempPath, buffer))
1.356 + {
1.357 + found = true;
1.358 + break;
1.359 + }
1.360 + }
1.361 + }
1.362 + if (found)
1.363 + continue;
1.364 + fclose(fp);
1.365 + HINSTANCE gamedll = dlmount(tempPath);
1.366 + if (gamedll == NULL)
1.367 + continue;
1.368 + CreateInterfaceFn fn = (CreateInterfaceFn)dlsym(gamedll, "CreateInterface");
1.369 + if (fn == NULL)
1.370 + {
1.371 + dlclose(gamedll);
1.372 + continue;
1.373 + }
1.374 + GameDllInfo *pInfo = new GameDllInfo;
1.375 + pInfo->factory = fn;
1.376 + pInfo->lib = gamedll;
1.377 + pInfo->loaded = true;
1.378 + pInfo->pGameDLL = NULL;
1.379 + gamedll_list.push_back(pInfo);
1.380 + break;
1.381 + }
1.382 + }
1.383 + }
1.384 + fclose(fp);
1.385 + }
1.386 +
1.387 + if (!g_GameDll.loaded)
1.388 + {
1.389 + const char *str = "ServerGameDLL";
1.390 + size_t len = strlen(str);
1.391 +
1.392 + if (strncmp(iface, str, len) == 0)
1.393 + {
1.394 + /* This is the interface we want! Right now we support versions 3 through 8 */
1.395 + g_GameDllVersion = atoi(&(iface[len]));
1.396 + int sizeTooBig = 0; //rename this to sizeWrong in the future!
1.397 + if (g_GameDllVersion < MIN_GAMEDLL_VERSION || g_GameDllVersion > MAX_GAMEDLL_VERSION)
1.398 + {
1.399 + /* Maybe this will get used in the future */
1.400 + sizeTooBig = g_GameDllVersion;
1.401 + if (ret)
1.402 + {
1.403 + *ret = IFACE_FAILED;
1.404 + }
1.405 + }
1.406 + SourceHook::List<GameDllInfo *>::iterator iter;
1.407 + GameDllInfo *pInfo = NULL;
1.408 + void *ptr;
1.409 + for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
1.410 + {
1.411 + pInfo = (*iter);
1.412 + ptr = (pInfo->factory)(iface, ret);
1.413 + if (ptr)
1.414 + {
1.415 + /* This is our GameDLL. Unload the others. */
1.416 + gamedll_list.erase(iter);
1.417 + ClearGamedllList();
1.418 + pInfo->pGameDLL = static_cast<IServerGameDLL *>(ptr);
1.419 + g_GameDll = *pInfo;
1.420 + delete pInfo;
1.421 + break;
1.422 + }
1.423 + }
1.424 + if (g_GameDll.loaded)
1.425 + {
1.426 + if (sizeTooBig)
1.427 + {
1.428 + Error("This mod version requires a SourceMM update (ServerGameDLL%03d)!\n", sizeTooBig);
1.429 + if (ret)
1.430 + {
1.431 + *ret = IFACE_FAILED;
1.432 + }
1.433 + return NULL;
1.434 + } else {
1.435 + InitMainStates();
1.436 + }
1.437 + } else {
1.438 + sizeTooBig = 0;
1.439 + if (ret)
1.440 + *ret = IFACE_FAILED;
1.441 + return NULL;
1.442 + }
1.443 + } else {
1.444 + /* wtf do we do... */
1.445 + /* :TODO: .. something a bit more intelligent? */
1.446 + Error("Engine requested unknown interface before GameDLL was known!\n");
1.447 + return NULL;
1.448 + }
1.449 + }
1.450 +
1.451 + /* We use this interface for responding to the meta client command */
1.452 + if (strncmp(iface, "ServerGameClients", 17) == 0)
1.453 + {
1.454 + void *ptr = (g_GameDll.factory)(iface, ret);
1.455 + g_GameDll.pGameClients = static_cast<IServerGameClients *>(ptr);
1.456 +
1.457 + return ptr;
1.458 + }
1.459 +
1.460 + /* If we got here, there's definitely a GameDLL */
1.461 + IFACE_MACRO(g_GameDll.factory, GameDLL);
1.462 +}
1.463 +
1.464 +void ClearGamedllList()
1.465 +{
1.466 + SourceHook::List<GameDllInfo *>::iterator iter;
1.467 +
1.468 + GameDllInfo *pInfo;
1.469 + for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
1.470 + {
1.471 + pInfo = (*iter);
1.472 + dlclose(pInfo->lib);
1.473 + delete pInfo;
1.474 + }
1.475 +
1.476 + gamedll_list.clear();
1.477 +}
1.478 +
1.479 +void DLLShutdown_handler()
1.480 +{
1.481 + /* Unload plugins */
1.482 + g_PluginMngr.UnloadAll();
1.483 +
1.484 + /* Add the FCVAR_GAMEDLL flag to our cvars so the engine removes them properly */
1.485 + g_SMConVarAccessor.MarkCommandsAsGameDLL();
1.486 + g_SMConVarAccessor.UnregisterGameDLLCommands();
1.487 +
1.488 + SH_CALL(g_GameDllPatch, &IServerGameDLL::DLLShutdown)();
1.489 +
1.490 + SH_RELEASE_CALLCLASS(g_GameDllPatch);
1.491 + g_GameDllPatch = NULL;
1.492 +
1.493 + g_SourceHook.CompleteShutdown();
1.494 +
1.495 + if (g_GameDll.lib && g_GameDll.loaded)
1.496 + dlclose(g_GameDll.lib);
1.497 + memset(&g_GameDll, 0, sizeof(GameDllInfo));
1.498 +
1.499 + RETURN_META(MRES_SUPERCEDE);
1.500 +}
1.501 +
1.502 +int LoadPluginsFromFile(const char *_file)
1.503 +{
1.504 + FILE *fp;
1.505 + int total = 0, skipped=0;
1.506 + PluginId id;
1.507 + bool already;
1.508 +
1.509 + fp = fopen(_file, "rt");
1.510 + if (!fp)
1.511 + {
1.512 + LogMessage("[META] Could not open plugins file %s\n", _file);
1.513 + return -1;
1.514 + }
1.515 +
1.516 + char buffer[255], error[255], full_path[255];
1.517 + const char *ptr, *ext, *file;
1.518 + size_t length;
1.519 + while (!feof(fp))
1.520 + {
1.521 + buffer[0] = '\0';
1.522 + fgets(buffer, sizeof(buffer), fp);
1.523 + length = strlen(buffer);
1.524 + if (!length)
1.525 + continue;
1.526 + if (buffer[length-1] == '\n')
1.527 + buffer[--length] = '\0';
1.528 +
1.529 + UTIL_TrimLeft(buffer);
1.530 + UTIL_TrimRight(buffer);
1.531 +
1.532 + if (buffer[0] == '\0' || buffer[0] == ';' || strncmp(buffer, "//", 2) == 0)
1.533 + continue;
1.534 + file = buffer;
1.535 + if (buffer[0] == '"')
1.536 + {
1.537 + char *cptr = buffer;
1.538 + file = ++cptr;
1.539 +
1.540 + while (*cptr)
1.541 + {
1.542 + if (*cptr == '"')
1.543 + {
1.544 + *cptr = '\0';
1.545 + break;
1.546 + }
1.547 + cptr++;
1.548 + }
1.549 + } else {
1.550 + char *cptr = buffer;
1.551 + while (*cptr)
1.552 + {
1.553 + if (isspace(*cptr))
1.554 + {
1.555 + char *optr = cptr;
1.556 + while (*cptr && isspace(*cptr))
1.557 + cptr++;
1.558 + *optr = '\0';
1.559 + UTIL_TrimRight(cptr);
1.560 + if (*cptr && isalpha(*cptr))
1.561 + {
1.562 + g_PluginMngr.SetAlias(buffer, cptr);
1.563 + file = cptr;
1.564 + }
1.565 + break;
1.566 + }
1.567 + cptr++;
1.568 + }
1.569 + }
1.570 + if (!file[0])
1.571 + {
1.572 + continue;
1.573 + }
1.574 + /* First find if it's an absolute path or not... */
1.575 + if (file[0] == '/' || strncmp(&(file[1]), ":\\", 2) == 0)
1.576 + {
1.577 + /* If we're in an absolute path, ignore our normal heuristics */
1.578 + id = g_PluginMngr.Load(file, Pl_File, already, error, sizeof(error));
1.579 + if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
1.580 + {
1.581 + LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
1.582 + } else {
1.583 + if (already)
1.584 + skipped++;
1.585 + else
1.586 + total++;
1.587 + }
1.588 + } else {
1.589 + /* Attempt to find a file extension */
1.590 + ptr = UTIL_GetExtension(file);
1.591 + /* Add an extension if there's none there */
1.592 + if (!ptr)
1.593 + {
1.594 +#if defined WIN32 || defined _WIN32
1.595 + ext = ".dll";
1.596 +#else
1.597 + ext = "_i486.so";
1.598 +#endif
1.599 + } else {
1.600 + ext = "";
1.601 + }
1.602 + /* Format the new path */
1.603 + g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s%s", g_ModPath.c_str(), file, ext);
1.604 + id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error));
1.605 + if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
1.606 + {
1.607 + LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
1.608 + } else {
1.609 + if (already)
1.610 + skipped++;
1.611 + else
1.612 + total++;
1.613 + }
1.614 + }
1.615 + }
1.616 + fclose(fp);
1.617 +
1.618 + if (skipped)
1.619 + {
1.620 + LogMessage("[META] Loaded %d plugins from file (%d already loaded)", total, skipped);
1.621 + } else {
1.622 + LogMessage("[META] Loaded %d plugins from file.", total);
1.623 + }
1.624 +
1.625 + return total;
1.626 +}
1.627 +
1.628 +/* Wrapper function. This is called when the GameDLL thinks it's using
1.629 + * the engine's real engineFactory.
1.630 + */
1.631 +void *EngineFactory(const char *iface, int *ret)
1.632 +{
1.633 + IFACE_MACRO(g_Engine.engineFactory, Engine);
1.634 +}
1.635 +
1.636 +/* Wrapper function. This is called when the GameDLL thinks it's using
1.637 + * the engine's real physicsFactory.
1.638 + */
1.639 +void *PhysicsFactory(const char *iface, int *ret)
1.640 +{
1.641 + IFACE_MACRO(g_Engine.physicsFactory, Physics);
1.642 +}
1.643 +
1.644 +/* Wrapper function. This is called when the GameDLL thinks it's using
1.645 + * the engine's real fileSystemFactory.
1.646 + */
1.647 +void *FileSystemFactory(const char *iface, int *ret)
1.648 +{
1.649 + IFACE_MACRO(g_Engine.fileSystemFactory, FileSystem);
1.650 +}
1.651 +
1.652 +void LogMessage(const char *msg, ...)
1.653 +{
1.654 + va_list ap;
1.655 + static char buffer[2048];
1.656 +
1.657 + va_start(ap, msg);
1.658 + size_t len = vsnprintf(buffer, sizeof(buffer) - 2, msg, ap);
1.659 + va_end(ap);
1.660 +
1.661 + buffer[len++] = '\n';
1.662 + buffer[len] = '\0';
1.663 +
1.664 + if (!g_Engine.engine)
1.665 + {
1.666 + fprintf(stdout, "%s", buffer);
1.667 + } else {
1.668 + g_Engine.engine->LogPrint(buffer);
1.669 + }
1.670 +}
1.671 +
1.672 +void LevelShutdown_handler(void)
1.673 +{
1.674 + if (!bInFirstLevel)
1.675 + {
1.676 + char full_path[255];
1.677 + g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s", g_ModPath.c_str(), GetPluginsFile());
1.678 +
1.679 + LoadPluginsFromFile(full_path);
1.680 + } else {
1.681 + bInFirstLevel = false;
1.682 + }
1.683 +
1.684 + ITER_EVENT(OnLevelShutdown, ());
1.685 +
1.686 + RETURN_META(MRES_IGNORED);
1.687 +}
1.688 +
1.689 +bool LevelInit_handler(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
1.690 +{
1.691 + if (!g_SmmAPI.CmdCacheSuccessful())
1.692 + {
1.693 + LogMessage("[META] Warning: Failed to initialize Con_Printf. Defaulting to Msg().");
1.694 + LogMessage("[META] Warning: Console messages will not be redirected to rcon console.");
1.695 + }
1.696 +
1.697 + ITER_EVENT(OnLevelInit, (pMapName, pMapEntities, pOldLevel, pLandmarkName, loadGame, background));
1.698 +
1.699 + RETURN_META_VALUE(MRES_IGNORED, false);
1.700 +}
1.701 +
1.702 +#if defined __GNUC__ && (__GNUC__ == 3 || __GNUC__ == 4)
1.703 +void * operator new(size_t size) {
1.704 + return(calloc(1, size));
1.705 +}
1.706 +
1.707 +void * operator new[](size_t size) {
1.708 + return(calloc(1, size));
1.709 +}
1.710 +
1.711 +void operator delete(void * ptr) {
1.712 + if(ptr)
1.713 + free(ptr);
1.714 +}
1.715 +
1.716 +void operator delete[](void * ptr) {
1.717 + if(ptr)
1.718 + free(ptr);
1.719 +}
1.720 +#endif