Added MM:S 1.4 core to the central tree.
1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2008 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): David "BAILOPAN" Anderson
8 * Contributor(s): Scott "Damaged Soul" Ehlert
9 * : Pavol "PM OnoTo" Marko
10 * ============================
13 #include <interface.h>
15 #include <tier0/icommandline.h>
17 #include "concommands.h"
21 #include "vsp_listener.h"
22 #include "iplayerinfo.h"
23 #include <filesystem.h>
25 using namespace SourceMM;
28 * @brief Implementation of main SourceMM GameDLL functionality
33 DLL_IMPORT ICommandLine *CommandLine();
35 SH_DECL_HOOK4(IServerGameDLL, DLLInit, SH_NOATTRIB, false, bool, CreateInterfaceFn, CreateInterfaceFn, CreateInterfaceFn, CGlobalVars *);
36 SH_DECL_HOOK0_void(IServerGameDLL, DLLShutdown, SH_NOATTRIB, false);
37 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
38 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
39 SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
40 SH_DECL_HOOK0(IServerGameDLL, GameInit, SH_NOATTRIB, false, bool);
42 bool DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals);
43 bool DLLInit_Post(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals);
44 void DLLShutdown_handler();
45 void LevelShutdown_handler();
46 bool LevelInit_handler(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background);
47 bool GameInit_handler();
48 void LookForVDFs(const char *dir);
49 bool KVLoadFromFile(KeyValues *kv, IBaseFileSystem *filesystem, const char *resourceName, const char *pathID = NULL);
51 GameDllInfo g_GameDll = {false, NULL, NULL, NULL, NULL};
53 SourceHook::CSourceHookImpl g_SourceHook;
54 SourceHook::ISourceHook *g_SHPtr = &g_SourceHook;
55 SourceHook::String g_ModPath;
56 SourceHook::String g_BinPath;
57 SourceHook::String g_SmmPath;
58 PluginId g_PLID = Pl_Console; /* Technically, SourceMM is the "Console" plugin... :p */
59 bool bInFirstLevel = true;
60 bool gParsedGameInfo = false;
61 bool bGameInit = false;
62 SourceHook::List<GameDllInfo *> gamedll_list;
63 SourceHook::CallClass<IServerGameDLL> *g_GameDllPatch;
64 SourceHook::CallClass<ICvar> *g_CvarPatch;
65 int g_GameDllVersion = 0;
66 const char VSPIFACE_001[] = "ISERVERPLUGINCALLBACKS001";
67 const char VSPIFACE_002[] = "ISERVERPLUGINCALLBACKS002";
68 const char GAMEINFO_PATH[] = "|gameinfo_path|";
69 IFileSystem *baseFs = NULL;
70 bool g_bLevelChanged = false;
73 void ClearGamedllList();
76 #define IFACE_MACRO(orig,nam) \
77 CPluginManager::CPlugin *pl; \
78 SourceHook::List<CPluginEventHandler>::iterator event; \
79 IMetamodListener *api; \
82 for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
84 for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++) { \
85 api = (*event).event; \
86 mret = IFACE_FAILED; \
87 if ( (val=api->On##nam##Query(iface, &mret)) != NULL ) { \
88 if (ret) *ret = mret; \
93 return (orig)(iface, ret);
95 #define ITER_EVENT(evn, args) \
96 CPluginManager::CPlugin *pl; \
97 SourceHook::List<CPluginEventHandler>::iterator event; \
98 IMetamodListener *api; \
99 for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
101 for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++) { \
102 api = (*event).event; \
107 ///////////////////////////////////
108 // Main code for HL2 Interaction //
109 ///////////////////////////////////
111 /* Initialize everything here */
112 void InitMainStates()
114 char full_path[PATH_SIZE] = {0};
115 GetFileOfAddress((void *)g_GameDll.factory, full_path, sizeof(full_path));
116 g_BinPath.assign(full_path);
118 /* Like Metamod, reload plugins at the end of the map.
119 * This is so plugins can hook everything on load, BUT, new plugins will be reloaded
120 * if the server is shut down (silly, but rare case).
122 bInFirstLevel = true;
124 SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLInit, g_GameDll.pGameDLL, DLLInit, false);
125 SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLInit, g_GameDll.pGameDLL, DLLInit_Post, true);
126 SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLShutdown, g_GameDll.pGameDLL, DLLShutdown_handler, false);
127 SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelShutdown, g_GameDll.pGameDLL, LevelShutdown_handler, true);
128 SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelInit, g_GameDll.pGameDLL, LevelInit_handler, true);
129 SH_ADD_HOOK_STATICFUNC(IServerGameDLL, GameInit, g_GameDll.pGameDLL, GameInit_handler, false);
132 void DoInitialPluginLoads()
134 const char *pluginFile = g_Engine.icvar->GetCommandLineValue("mm_pluginsfile");
135 const char *mmBaseDir = g_Engine.icvar->GetCommandLineValue("mm_basedir");
139 pluginFile = GetPluginsFile();
143 mmBaseDir = GetMetamodBaseDir();
148 g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s", g_ModPath.c_str(), pluginFile);
149 LoadPluginsFromFile(full_path);
150 g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s", g_ModPath.c_str(), mmBaseDir);
151 LookForVDFs(full_path);
154 bool StartupMetamod(CreateInterfaceFn engineFactory, bool bWaitForGameInit)
156 g_Engine.engine = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
158 if (!g_Engine.engine)
160 Error("Could not find IVEngineServer! Metamod cannot load.");
163 g_Engine.icvar = (ICvar *)((engineFactory)(VENGINE_CVAR_INTERFACE_VERSION , NULL));
166 Error("Could not find ICvar! Metamod cannot load.");
170 g_Engine.loaded = true;
172 /* The Ship is the only game known at this time that uses the pre-Episode One engine */
173 g_Engine.original = strcmp(CommandLine()->ParmValue("-game", "hl2"), "ship") == 0;
175 ConCommandBaseMgr::OneTimeInit(static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor));
177 g_GameDllPatch = SH_GET_CALLCLASS(g_GameDll.pGameDLL);
178 g_CvarPatch = SH_GET_CALLCLASS(g_Engine.icvar);
180 if (g_GameDll.pGameClients)
182 SH_ADD_HOOK_STATICFUNC(IServerGameClients, ClientCommand, g_GameDll.pGameClients, ClientCommand_handler, false);
186 /* If IServerGameClients isn't found, this really isn't a fatal error so... */
187 LogMessage("[META] Warning: Could not find IServerGameClients!");
188 LogMessage("[META] Warning: The 'meta' command will not be available to clients.");
191 if (!g_SmmAPI.CacheCmds())
193 LogMessage("[META] Warning: Failed to initialize Con_Printf. Defaulting to Msg().");
194 LogMessage("[META] Warning: Console messages will not be redirected to rcon console.");
197 if (!g_SmmAPI.CacheUserMessages())
199 /* Don't know of a mod that has stripped out user messages completely,
200 * but perhaps should do something different here?
202 LogMessage("[META] Warning: Failed to get list of user messages.");
203 LogMessage("[META] Warning: The 'meta game' command will not display user messages.");
206 baseFs = (IFileSystem *)((engineFactory)(FILESYSTEM_INTERFACE_VERSION, NULL));
209 LogMessage("[META] Failed to find filesystem interface, .vdf files will not be parsed.");
212 if (!g_SMConVarAccessor.InitConCommandBaseList())
214 /* This is very unlikely considering it's old engine */
215 LogMessage("[META] Warning: Failed to find ConCommandBase list!");
216 LogMessage("[META] Warning: ConVars and ConCommands cannot be unregistered properly! Please file a bug report.");
219 if (!bWaitForGameInit)
221 DoInitialPluginLoads();
222 bInFirstLevel = true;
228 bool DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
230 g_Engine.engineFactory = engineFactory;
231 g_Engine.fileSystemFactory = filesystemFactory;
232 g_Engine.physicsFactory = physicsFactory;
233 g_Engine.pGlobals = pGlobals;
235 StartupMetamod(engineFactory, false);
237 RETURN_META_VALUE(MRES_IGNORED, true);
240 bool AlternatelyLoadMetamod(CreateInterfaceFn ifaceFactory, CreateInterfaceFn serverFactory)
242 g_Engine.engineFactory = ifaceFactory;
243 g_Engine.fileSystemFactory = ifaceFactory;
244 g_Engine.physicsFactory = ifaceFactory;
246 IPlayerInfoManager *playerInfoManager = (IPlayerInfoManager *)serverFactory("PlayerInfoManager002", NULL);
247 if (playerInfoManager == NULL)
249 Error("Metamod:Source requires gameinfo.txt modification to load on this game.");
253 g_Engine.pGlobals = playerInfoManager->GetGlobalVars();
255 /* Now find the server */
256 g_GameDll.factory = serverFactory;
257 g_GameDll.lib = NULL;
259 char gamedll_iface[] = "ServerGameDLL000";
260 for (unsigned int i = 3; i <= 50; i++)
262 gamedll_iface[15] = '0' + i;
263 g_GameDll.pGameDLL = (IServerGameDLL *)serverFactory(gamedll_iface, NULL);
264 if (g_GameDll.pGameDLL != NULL)
266 g_GameDllVersion = i;
271 if (g_GameDll.pGameDLL == NULL)
273 Error("Metamod:Source requires gameinfo.txt modification to load on this game.");
277 char gameclients_iface[] = "ServerGameClients000";
278 for (unsigned int i = 3; i <= 4; i++)
280 gameclients_iface[19] = '0' + i;
281 g_GameDll.pGameClients = (IServerGameClients *)serverFactory(gameclients_iface, NULL);
282 if (g_GameDll.pGameClients != NULL)
288 char smm_path[PATH_SIZE];
289 const char *game_dir;
290 GetFileOfAddress((void *)AlternatelyLoadMetamod, smm_path, sizeof(smm_path));
291 g_SmmPath.assign(smm_path);
293 game_dir = CommandLine()->ParmValue("-game", "hl2");
294 abspath(smm_path, game_dir);
295 g_ModPath.assign(smm_path);
299 if (!StartupMetamod(ifaceFactory, true))
304 g_PluginMngr.SetAllLoaded();
309 bool GameInit_handler()
313 RETURN_META_VALUE(MRES_IGNORED, true);
316 if (g_SmmAPI.VSPEnabled() && !g_VspListener.IsRootLoadMethod())
318 g_SmmAPI.LoadAsVSP();
321 if (g_VspListener.IsRootLoadMethod())
323 DoInitialPluginLoads();
329 RETURN_META_VALUE(MRES_IGNORED, true);
332 bool DLLInit_Post(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
334 g_PluginMngr.SetAllLoaded();
335 RETURN_META_VALUE(MRES_IGNORED, true);
338 /* This is where the magic happens */
339 SMM_API void *CreateInterface(const char *iface, int *ret)
341 /* Prevent loading of self as a SourceMM plugin or Valve server plugin :x */
342 if (strcmp(iface, PLAPI_NAME) == 0)
344 Warning("Do not try loading Metamod:Source as a Metamod:Source plugin");
354 /* We check these separately because we can't reply
355 * unless our interface version really matches.
357 if ((strcmp(iface, VSPIFACE_002) == 0)
358 || strcmp(iface, VSPIFACE_001) == 0)
364 return &g_VspListener;
367 /* If we're a VSP, bypass this by default */
368 if (g_VspListener.IsRootLoadMethod())
370 IFACE_MACRO(g_GameDll.factory, GameDLL);
373 if (!gParsedGameInfo)
375 gParsedGameInfo = true;
376 const char *gameDir = NULL;
377 char gamePath[PATH_SIZE];
378 char smmPath[PATH_SIZE];
380 /* Get path to SourceMM DLL */
381 if (!GetFileOfAddress((void *)CreateInterface, smmPath, sizeof(smmPath)))
383 Error("GetFileOfAddress() failed! Metamod cannot load.\n");
387 g_SmmPath.assign(smmPath);
389 /* Get value of -game from command line, defaulting to hl2 as engine seems to do */
390 gameDir = CommandLine()->ParmValue("-game", "hl2");
392 /* Get absolute path */
393 abspath(gamePath, gameDir);
394 g_ModPath.assign(gamePath);
396 char tempPath[PATH_SIZE];
398 /* Path to gameinfo.txt */
399 g_SmmAPI.PathFormat(tempPath, PATH_SIZE, "%s/%s", g_ModPath.c_str(), "gameinfo.txt");
401 FILE *fp = fopen(tempPath, "rt");
405 Error("Unable to open gameinfo.txt! Metamod cannot load.\n");
410 char key[128], val[128];
413 bool gamebin = false;
416 char curPath[PATH_SIZE];
418 getcwd(curPath, PATH_SIZE);
423 fgets(buffer, sizeof(buffer), fp);
424 len = strlen(buffer);
425 if (buffer[len-1] == '\n')
426 buffer[--len] = '\0';
427 UTIL_TrimComments(buffer);
428 UTIL_TrimLeft(buffer);
429 UTIL_TrimRight(buffer);
430 if (stricmp(buffer, "SearchPaths") == 0)
434 UTIL_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
435 if (stricmp(key, "Game") == 0 || stricmp(key, "GameBin") == 0)
437 if (stricmp(key, "Game") == 0)
442 if (strncmp(val, GAMEINFO_PATH, sizeof(GAMEINFO_PATH) - 1) == 0)
444 ptr = &(val[sizeof(GAMEINFO_PATH) - 1]);
447 lptr = g_ModPath.c_str();
453 size_t ptr_len = strlen(ptr);
454 if (ptr[ptr_len] == '/' || ptr[ptr_len] == '\\')
455 ptr[--ptr_len] = '\0';
457 /* No need to append "bin" if key is GameBin */
460 g_SmmAPI.PathFormat(tempPath, PATH_SIZE, "%s/%s/%s", lptr, ptr, SERVER_DLL);
461 } else if (!ptr[0]) {
462 g_SmmAPI.PathFormat(tempPath, PATH_SIZE, "%s/%s/%s", lptr, "bin", SERVER_DLL);
464 g_SmmAPI.PathFormat(tempPath, PATH_SIZE, "%s/%s/%s/%s", lptr, ptr, "bin", SERVER_DLL);
467 /* If not path to SourceMM... */
468 if (!UTIL_PathCmp(smmPath, tempPath))
470 FILE *fp = fopen(tempPath, "rb");
473 //:TODO: Optimize this a bit!
474 SourceHook::List<GameDllInfo *>::iterator iter;
477 for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
480 if (GetFileOfAddress((void *)pCheck->factory, buffer, sizeof(buffer)))
482 if (UTIL_PathCmp(tempPath, buffer))
492 HINSTANCE gamedll = dlmount(tempPath);
495 CreateInterfaceFn fn = (CreateInterfaceFn)dlsym(gamedll, "CreateInterface");
501 GameDllInfo *pInfo = new GameDllInfo;
503 pInfo->lib = gamedll;
504 pInfo->loaded = true;
505 pInfo->pGameDLL = NULL;
506 gamedll_list.push_back(pInfo);
514 if (!g_GameDll.loaded)
516 const char *str = "ServerGameDLL";
517 size_t len = strlen(str);
519 if (strncmp(iface, str, len) == 0)
521 /* This is the interface we want! Right now we support versions 3 through 8 */
522 g_GameDllVersion = atoi(&(iface[len]));
523 int sizeTooBig = 0; //rename this to sizeWrong in the future!
524 if (g_GameDllVersion < MIN_GAMEDLL_VERSION || g_GameDllVersion > MAX_GAMEDLL_VERSION)
526 /* Maybe this will get used in the future */
527 sizeTooBig = g_GameDllVersion;
533 SourceHook::List<GameDllInfo *>::iterator iter;
534 GameDllInfo *pInfo = NULL;
536 for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
539 ptr = (pInfo->factory)(iface, ret);
542 /* This is our GameDLL. Unload the others. */
543 gamedll_list.erase(iter);
545 pInfo->pGameDLL = static_cast<IServerGameDLL *>(ptr);
551 if (g_GameDll.loaded)
555 Error("This mod version requires a SourceMM update (ServerGameDLL%03d)!\n", sizeTooBig);
571 /* wtf do we do... */
572 /* :TODO: .. something a bit more intelligent? */
573 Error("Engine requested unknown interface before GameDLL was known!\n");
578 /* We use this interface for responding to the meta client command */
579 if (strncmp(iface, "ServerGameClients", 17) == 0)
581 void *ptr = (g_GameDll.factory)(iface, ret);
582 g_GameDll.pGameClients = static_cast<IServerGameClients *>(ptr);
587 /* If we got here, there's definitely a GameDLL */
588 IFACE_MACRO(g_GameDll.factory, GameDLL);
591 void ClearGamedllList()
593 SourceHook::List<GameDllInfo *>::iterator iter;
596 for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
603 gamedll_list.clear();
606 void UnloadMetamod(bool shutting_down)
609 g_PluginMngr.UnloadAll();
613 /* Add the FCVAR_GAMEDLL flag to our cvars so the engine removes them properly */
614 g_SMConVarAccessor.MarkCommandsAsGameDLL();
615 g_Engine.icvar->UnlinkVariables(FCVAR_GAMEDLL);
617 SH_CALL(g_GameDllPatch, &IServerGameDLL::DLLShutdown)();
620 SH_RELEASE_CALLCLASS(g_GameDllPatch);
621 SH_RELEASE_CALLCLASS(g_CvarPatch);
622 g_GameDllPatch = NULL;
625 g_SourceHook.CompleteShutdown();
627 if (g_GameDll.lib && g_GameDll.loaded)
629 dlclose(g_GameDll.lib);
631 memset(&g_GameDll, 0, sizeof(GameDllInfo));
634 void DLLShutdown_handler()
637 RETURN_META(MRES_SUPERCEDE);
640 void LoadFromVDF(const char *file)
643 bool already, kvfileLoaded;
645 const char *plugin_file, *alias;
646 char full_path[256], error[256];
648 pValues = new KeyValues("Metamod Plugin");
650 if (g_Engine.original)
652 /* The Ship must use a special version of this function */
653 kvfileLoaded = KVLoadFromFile(pValues, baseFs, file);
657 kvfileLoaded = pValues->LoadFromFile(baseFs, file);
662 pValues->deleteThis();
666 if ((plugin_file = pValues->GetString("file", NULL)) == NULL)
668 pValues->deleteThis();
672 if ((alias = pValues->GetString("alias", NULL)) != NULL)
674 g_PluginMngr.SetAlias(alias, plugin_file);
677 /* Attempt to find a file extension */
678 if (UTIL_GetExtension(plugin_file) == NULL)
680 g_SmmAPI.PathFormat(full_path,
685 #if defined WIN32 || defined _WIN32
694 g_SmmAPI.PathFormat(full_path,
701 id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error));
702 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
704 LogMessage("[META] Failed to load plugin %s: %s", plugin_file, error);
707 pValues->deleteThis();
710 void LookForVDFs(const char *dir)
720 g_SmmAPI.PathFormat(path, sizeof(path), "%s\\*.*", dir);
721 if ((hFind = FindFirstFile(path, &fd)) == INVALID_HANDLE_VALUE)
723 DWORD dw = GetLastError();
724 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
727 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
731 LogMessage("[META] Could not open folder \"%s\" (%s)", dir, error);
737 if (strcmp(fd.cFileName, ".") == 0
738 || strcmp(fd.cFileName, "..") == 0)
742 extidx = strlen(fd.cFileName) - 4;
743 if (extidx < 0 || stricmp(&fd.cFileName[extidx], ".vdf"))
747 g_SmmAPI.PathFormat(path, sizeof(path), "%s\\%s", dir, fd.cFileName);
749 } while (FindNextFile(hFind, &fd));
756 if ((pDir = opendir(dir)) == NULL)
758 LogMessage("[META] Could not open folder \"%s\" (%s)", dir, strerror(errno));
762 while ((pEnt = readdir(pDir)) != NULL)
764 if (strcmp(pEnt->d_name, ".") == 0
765 || strcmp(pEnt->d_name, "..") == 0)
769 extidx = strlen(pEnt->d_name) - 4;
770 if (extidx < 0 || stricmp(&pEnt->d_name[extidx], ".vdf"))
774 g_SmmAPI.PathFormat(path, sizeof(path), "%s/%s", dir, pEnt->d_name);
782 bool KVLoadFromFile(KeyValues *kv, IBaseFileSystem *filesystem, const char *resourceName, const char *pathID)
786 Assert(_heapchk() == _HEAPOK);
789 FileHandle_t f = filesystem->Open(resourceName, "rb", pathID);
793 // load file into a null-terminated buffer
794 int fileSize = filesystem->Size(f);
795 char *buffer = (char *)MemAllocScratch(fileSize + 1);
799 filesystem->Read(buffer, fileSize, f); // read into local buffer
801 buffer[fileSize] = 0; // null terminate file as EOF
803 filesystem->Close( f ); // close file after reading
805 bool retOK = kv->LoadFromBuffer( resourceName, buffer, filesystem );
812 int LoadPluginsFromFile(const char *_file)
815 int total = 0, skipped=0;
819 fp = fopen(_file, "rt");
822 LogMessage("[META] Could not open plugins file %s\n", _file);
826 char buffer[255], error[255], full_path[255];
827 const char *ptr, *ext, *file;
829 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
831 UTIL_TrimLeft(buffer);
832 UTIL_TrimRight(buffer);
834 length = strlen(buffer);
840 if (buffer[0] == '\0' || buffer[0] == ';' || strncmp(buffer, "//", 2) == 0)
846 if (buffer[0] == '"')
869 while (*cptr && isspace(*cptr))
874 UTIL_TrimRight(cptr);
875 if (*cptr && isalpha(*cptr))
877 g_PluginMngr.SetAlias(buffer, cptr);
889 /* First find if it's an absolute path or not... */
890 if (file[0] == '/' || strncmp(&(file[1]), ":\\", 2) == 0)
892 /* If we're in an absolute path, ignore our normal heuristics */
893 id = g_PluginMngr.Load(file, Pl_File, already, error, sizeof(error));
894 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
896 LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
912 /* Attempt to find a file extension */
913 ptr = UTIL_GetExtension(file);
914 /* Add an extension if there's none there */
917 #if defined WIN32 || defined _WIN32
927 /* Format the new path */
928 g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s%s", g_ModPath.c_str(), file, ext);
929 id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error));
930 if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
932 LogMessage("[META] Failed to load plugin %s. %s", buffer, error);
951 LogMessage("[META] Loaded %d plugins from file (%d already loaded)", total, skipped);
955 LogMessage("[META] Loaded %d plugins from file.", total);
962 /* Wrapper function. This is called when the GameDLL thinks it's using
963 * the engine's real engineFactory.
965 void *EngineFactory(const char *iface, int *ret)
967 IFACE_MACRO(g_Engine.engineFactory, Engine);
970 /* Wrapper function. This is called when the GameDLL thinks it's using
971 * the engine's real physicsFactory.
973 void *PhysicsFactory(const char *iface, int *ret)
975 IFACE_MACRO(g_Engine.physicsFactory, Physics);
978 /* Wrapper function. This is called when the GameDLL thinks it's using
979 * the engine's real fileSystemFactory.
981 void *FileSystemFactory(const char *iface, int *ret)
983 IFACE_MACRO(g_Engine.fileSystemFactory, FileSystem);
986 void LogMessage(const char *msg, ...)
989 static char buffer[2048];
992 size_t len = vsnprintf(buffer, sizeof(buffer) - 2, msg, ap);
995 buffer[len++] = '\n';
998 if (!g_Engine.engine)
1000 fprintf(stdout, "%s", buffer);
1002 g_Engine.engine->LogPrint(buffer);
1006 void LevelShutdown_handler(void)
1010 char full_path[255];
1012 g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s", g_ModPath.c_str(), GetPluginsFile());
1013 LoadPluginsFromFile(full_path);
1015 g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s", g_ModPath.c_str(), GetMetamodBaseDir());
1016 LookForVDFs(full_path);
1020 bInFirstLevel = false;
1023 g_bLevelChanged = true;
1025 ITER_EVENT(OnLevelShutdown, ());
1027 RETURN_META(MRES_IGNORED);
1030 bool LevelInit_handler(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
1032 if (!g_SmmAPI.CmdCacheSuccessful())
1034 LogMessage("[META] Warning: Failed to initialize Con_Printf. Defaulting to Msg().");
1035 LogMessage("[META] Warning: Console messages will not be redirected to rcon console.");
1038 ITER_EVENT(OnLevelInit, (pMapName, pMapEntities, pOldLevel, pLandmarkName, loadGame, background));
1040 RETURN_META_VALUE(MRES_IGNORED, false);
1043 #if defined __GNUC__ && (__GNUC__ == 3 || __GNUC__ == 4)
1044 void * operator new(size_t size) {
1045 return(calloc(1, size));
1048 void * operator new[](size_t size) {
1049 return(calloc(1, size));
1052 void operator delete(void * ptr) {
1057 void operator delete[](void * ptr) {