sourcemm/sourcemm.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Apr 21 02:30:02 2006 +0000 (2006-04-21)
changeset 191 2d0dbd03c503
parent 190 718f99cad77d
child 192 47649a1d49e3
permissions -rw-r--r--
Mm, oopsy. ICvar::GetCommandLineValue could return NULL for the plugins file. That would be bad. :(
(Now fixed with a call to GetPluginsFile in case it is NULL)
     1 /* ======== SourceMM ========
     2 * Copyright (C) 2004-2006 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): David "BAILOPAN" Anderson
     8 * Contributor(s): Scott "Damaged Soul" Ehlert
     9 *				: Pavol "PM OnoTo" Marko
    10 * ============================
    11 */
    12 
    13 #include <interface.h>
    14 #include <eiface.h>
    15 #include "sourcemm.h"
    16 #include "concommands.h"
    17 #include "CSmmAPI.h"
    18 #include "CPlugin.h"
    19 #include "util.h"
    20 
    21 using namespace SourceMM;
    22 
    23 /**
    24  * @brief Implementation of main SourceMM GameDLL functionality
    25  * @file sourcemm.cpp
    26  */
    27 
    28 SH_DECL_HOOK4(IServerGameDLL, DLLInit, SH_NOATTRIB, false, bool, CreateInterfaceFn, CreateInterfaceFn, CreateInterfaceFn, CGlobalVars *);
    29 SH_DECL_HOOK0_void(IServerGameDLL, DLLShutdown, SH_NOATTRIB, false);
    30 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
    31 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
    32 bool DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals);
    33 bool DLLInit_Post(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals);
    34 void DLLShutdown_handler();
    35 void LevelShutdown_handler();
    36 bool LevelInit_handler(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background);
    37 
    38 GameDllInfo g_GameDll = {false, NULL, NULL, NULL};
    39 EngineInfo g_Engine;
    40 SourceHook::CSourceHookImpl g_SourceHook;
    41 SourceHook::ISourceHook *g_SHPtr = &g_SourceHook;
    42 SourceHook::String g_ModPath;
    43 SourceHook::String g_BinPath;
    44 PluginId g_PLID = Pl_Console;		//Technically, SourceMM is the "Console" plugin... :p
    45 bool bInFirstLevel = true;
    46 bool gParsedGameInfo = false;
    47 SourceHook::List<GameDllInfo *> gamedll_list;
    48 SourceHook::CallClass<IServerGameDLL> *dllExec;
    49 
    50 void ClearGamedllList();
    51 
    52 //helper macro
    53 #define	IFACE_MACRO(orig,nam) \
    54 	CPluginManager::CPlugin *pl; \
    55 	SourceHook::List<IMetamodListener *>::iterator event; \
    56 	IMetamodListener *api; \
    57 	int mret = 0; \
    58 	void *val = NULL; \
    59 	for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
    60 		pl = (*iter); \
    61 		for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++) { \
    62 			api = (*event); \
    63 			mret = IFACE_FAILED; \
    64 			if ( (val=api->On##nam##Query(iface, &mret)) != NULL ) { \
    65 				if (ret) *ret = mret; \
    66 				return val; \
    67 			} \
    68 		} \
    69 	} \
    70 	return (orig)(iface, ret);
    71 
    72 #define ITER_EVENT(evn, args) \
    73 	CPluginManager::CPlugin *pl; \
    74 	SourceHook::List<IMetamodListener *>::iterator event; \
    75 	IMetamodListener *api; \
    76 	for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
    77 		pl = (*iter); \
    78 		for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++) { \
    79 			api = (*event); \
    80 			api->evn args; \
    81 		} \
    82 	}
    83 
    84 ///////////////////////////////////
    85 // Main code for HL2 Interaction //
    86 ///////////////////////////////////
    87 
    88 //Initialize everything here
    89 void InitMainStates()
    90 {
    91 	char full_path[260] = {0};
    92 	GetFileOfAddress(g_GameDll.factory, full_path, sizeof(full_path)-1);
    93 	g_BinPath.assign(full_path);
    94 
    95 	//Like metamod, reload plugins at the end of the map.
    96 	//This is so plugins can hook everything on load, BUT, new plugins will be reloaded
    97 	// if the server is shut down (silly, but rare case).
    98 	bInFirstLevel = true;
    99 
   100 	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLInit, g_GameDll.pGameDLL, DLLInit, false);
   101 	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLInit, g_GameDll.pGameDLL, DLLInit_Post, true);
   102 	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLShutdown, g_GameDll.pGameDLL, DLLShutdown_handler, false);
   103 	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelShutdown, g_GameDll.pGameDLL, LevelShutdown_handler, true);
   104 	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelInit, g_GameDll.pGameDLL, LevelInit_handler, true);
   105 }
   106 
   107 bool DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
   108 {
   109 	g_Engine.engineFactory = engineFactory;
   110 	g_Engine.fileSystemFactory = filesystemFactory;
   111 	g_Engine.physicsFactory = physicsFactory;
   112 	g_Engine.pGlobals = pGlobals;
   113 
   114 	g_Engine.engine = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
   115 	if (!g_Engine.engine)
   116 	{
   117 		Error("Could not find IVEngineServer! Metamod cannot load.");
   118 		return false;
   119 	}
   120 	g_Engine.icvar = (ICvar *)((engineFactory)(VENGINE_CVAR_INTERFACE_VERSION , NULL));
   121 	if (!g_Engine.icvar)
   122 	{
   123 		Error("Could not find ICvar! Metamod cannot load.");
   124 		return false;
   125 	}
   126 
   127 	g_Engine.loaded = true;
   128 
   129 	//Initialize our console hooks
   130 	ConCommandBaseMgr::OneTimeInit(static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor));
   131 
   132 	if (!g_SmmAPI.CacheCmds())
   133 	{
   134 		LogMessage("[META] Warning: Failed to initialize Con_Printf.  Defaulting to Msg().");
   135 		LogMessage("[META] Warning: Console messages will not be redirected to rcon console.");
   136 	}
   137 
   138 
   139 	const char *pluginFile = g_Engine.icvar->GetCommandLineValue("mm_pluginsfile");
   140 	if (!pluginFile) 
   141 		pluginFile = GetPluginsFile();
   142 
   143 	char full_path[260];
   144 	g_SmmAPI.PathFormat(full_path, sizeof(full_path)-1, "%s/%s", g_ModPath.c_str(), pluginFile);
   145 
   146 	LoadPluginsFromFile(full_path);
   147 
   148 	bInFirstLevel = true;
   149 
   150 	dllExec = SH_GET_CALLCLASS(g_GameDll.pGameDLL);
   151 
   152 	RETURN_META_VALUE(MRES_IGNORED, true);
   153 }
   154 
   155 bool DLLInit_Post(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
   156 {
   157 	g_PluginMngr.SetAllLoaded();
   158 	RETURN_META_VALUE(MRES_IGNORED, true);
   159 }
   160 
   161 //This is where the magic happens
   162 SMM_API void *CreateInterface(const char *iface, int *ret)
   163 {
   164 	if (!gParsedGameInfo)
   165 	{
   166 		gParsedGameInfo = true;
   167 		char curpath[260] = {0};
   168 		char dllpath[260] = {0};
   169 		getcwd(curpath, sizeof(curpath)-1);
   170 		if (!GetFileOfAddress((void *)CreateInterface, dllpath, sizeof(dllpath)-1))
   171 		{
   172 			Error("GetFileOfAddress() failed! Metamod cannot load.");
   173 			return NULL;
   174 		}
   175 		SourceHook::String s_dllpath(dllpath);
   176 		//begin the heuristics for searching for the mod path (these are quite ugly).
   177 		//for OS compatibility purposes, we're going to do case insensitivity on windows.
   178 		size_t path_len = strlen(curpath);
   179 		size_t dll_len = strlen(dllpath);
   180 
   181 		//strip the dll path off
   182 		//:TODO: with path stuff - in Linux, \ can exist in a file path as a non-seperator!
   183 		for (size_t i=dll_len-1; i>0; i--)
   184 		{
   185 			if (dllpath[i] == '\\' || dllpath[i] == '/')
   186 			{
   187 				if (i == dll_len-1)
   188 					break;
   189 				//save path by stripping off file name and ending terminator
   190 				dllpath[i] = '\0';
   191 				dll_len = i;
   192 				break;
   193 			}
   194 		}
   195 
   196 		//strip absolute path terminators if any!
   197 		if (curpath[path_len-1] == '/' || curpath[path_len-1] == '\\')
   198 			curpath[--path_len] = '\0';
   199 
   200 		//if the base path doesn't fit into the module path, something is wrong!
   201 		// ex: c:\games\srcds
   202 		//     c:\gaben.dll
   203 		if (path_len > dll_len)
   204 		{
   205 			Error("Could not detect GameDLL path! Metamod cannot load[1].");
   206 			return NULL;
   207 		}
   208 
   209 		//we are now in such a position that the two dir names SHOULD MATCH!
   210 		typedef int (*STRNCMP)(const char *str1, const char *str2, size_t n);
   211 		STRNCMP cmp = 
   212 #ifdef WIN32
   213 			strnicmp;
   214 #else
   215 			strncmp;
   216 #endif
   217 		//are they equal?
   218 		if ( ((cmp)(curpath, dllpath, path_len)) != 0 )
   219 		{
   220 			//:TODO: In this case, we should read /proc/self/maps and find srcds!
   221 			Error("Could not detect GameDLL path! Metamod cannot load[2].");
   222 			return NULL;
   223 		}
   224 		//this will skip past the dir and its separator char
   225 		char *ptr = &(dllpath[path_len+1]);
   226 		path_len = strlen(ptr);
   227 		for (size_t i=0; i<path_len; i++)
   228 		{
   229 			if (ptr[i] == '/' || ptr[i] == '\\')
   230 			{
   231 				if (i == 0)
   232 				{
   233 					Error("Could not detect GameDLL path! Metamod cannot load[3].");
   234 					return NULL;
   235 				} else {
   236 					ptr[i] = '\0';
   237 					path_len = i+1;
   238 					break;
   239 				}
   240 			}
   241 		}
   242 		//WE NOW HAVE A GUESS AT THE MOD DIR.  OH MY GOD.
   243 		char temp_path[260];
   244 		snprintf(temp_path, sizeof(temp_path)-1, 
   245 			"%s%s%s", 
   246 			curpath, 
   247 			PATH_SEP_STR, 
   248 			ptr);
   249 
   250 		g_ModPath.assign(temp_path);
   251 
   252 		snprintf(temp_path, sizeof(temp_path)-1, 
   253 			"%s%s%s", 
   254 			g_ModPath.c_str(),
   255 			PATH_SEP_STR,
   256 			"gameinfo.txt");
   257 
   258 		FILE *fp = fopen(temp_path, "rt");
   259 		if (!fp)
   260 		{
   261 			Error("Unable to open gameinfo.txt!  Metamod cannot load.");
   262 			return NULL;
   263 		}
   264 		char buffer[255];
   265 		char key[128], val[128];
   266 		size_t len = 0;
   267 		const char *gameinfo = "|gameinfo_path|";
   268 		size_t gameinfo_len = strlen(gameinfo);
   269 		bool search = false;
   270 		bool gamebin = false;
   271 		const char *lptr;
   272 		while (!feof(fp))
   273 		{
   274 			buffer[0] = '\0';
   275 			fgets(buffer, sizeof(buffer)-1, fp);
   276 			len = strlen(buffer);
   277 			if (buffer[len-1] == '\n')
   278 				buffer[--len] = '\0';
   279 			UTIL_TrimComments(buffer);
   280 			UTIL_TrimLeft(buffer);
   281 			UTIL_TrimRight(buffer);
   282 			if (stricmp(buffer, "SearchPaths") == 0)
   283 				search = true;
   284 			if (!search)
   285 				continue;
   286 			UTIL_KeySplit(buffer, key, sizeof(key)-1, val, sizeof(val)-1);
   287 			if (stricmp(key, "Game") == 0 || stricmp(key, "GameBin") == 0)
   288 			{
   289 				if (stricmp(key, "Game") == 0)
   290 					gamebin = false;
   291 				else
   292 					gamebin = true;
   293 
   294 				if (strncmp(val, gameinfo, gameinfo_len) == 0)
   295 				{
   296 					ptr = &(val[gameinfo_len]);
   297 					if (ptr[0] == '.')
   298 						ptr++;
   299 					lptr = g_ModPath.c_str();
   300 				} else {
   301 					lptr = curpath;
   302 					ptr = val;
   303 				}
   304 				size_t ptr_len = strlen(ptr);
   305 				if (ptr[ptr_len] == '/' || ptr[ptr_len] == '\\')
   306 					ptr[--ptr_len] = '\0';
   307 				//no need to append "bin"
   308 				if (gamebin)
   309 				{
   310 					g_SmmAPI.PathFormat(temp_path, sizeof(temp_path)-1, "%s/%s/%s", lptr, ptr, SERVER_DLL);
   311 				} else {
   312 					g_SmmAPI.PathFormat(temp_path, sizeof(temp_path)-1, "%s/%s/%s/%s", lptr, ptr, "bin", SERVER_DLL);
   313 				}
   314 				if (!UTIL_PathCmp(s_dllpath.c_str(), temp_path))
   315 				{
   316 					FILE *fp = fopen(temp_path, "rb");
   317 					if (!fp)
   318 						continue;
   319 					//:TODO: Optimize this a bit!
   320 					SourceHook::List<GameDllInfo *>::iterator iter;
   321 					GameDllInfo *pCheck;
   322 					bool found = false;
   323 					for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
   324 					{
   325 						pCheck = (*iter);
   326 						if (GetFileOfAddress(pCheck->factory, buffer, sizeof(buffer)-1))
   327 						{
   328 							if (UTIL_PathCmp(temp_path, buffer))
   329 							{
   330 								found = true;
   331 								break;
   332 							}
   333 						}
   334 					}
   335 					if (found)
   336 						continue;
   337 					fclose(fp);
   338 					HINSTANCE gamedll = dlmount(temp_path);
   339 					if (gamedll == NULL)
   340 						continue;
   341 					CreateInterfaceFn fn = (CreateInterfaceFn)dlsym(gamedll, "CreateInterface");
   342 					if (fn == NULL)
   343 					{
   344 						dlclose(gamedll);
   345 						continue;
   346 					}
   347 					GameDllInfo *pInfo = new GameDllInfo;
   348 					pInfo->factory = fn;
   349 					pInfo->lib = gamedll;
   350 					pInfo->loaded = true;
   351 					pInfo->pGameDLL = NULL;
   352 					gamedll_list.push_back(pInfo);
   353 				}
   354 			}
   355 		}
   356 		fclose(fp);
   357 	}
   358 
   359 	if (!g_GameDll.loaded)
   360 	{
   361 		const char *str = "ServerGameDLL";
   362 		size_t len = strlen(str);
   363 
   364 		if (strncmp(iface, str, len) == 0)
   365 		{
   366 			//This is the interface we want!  Right now we support versions 3 and 4.
   367 			int version = atoi(&(iface[len]));
   368 			if (version < MIN_GAMEDLL_VERSION || version > MAX_GAMEDLL_VERSION)
   369 			{
   370 				Error("GameDLL version %d is not supported by Metamod!", version);
   371 				return NULL;
   372 			}
   373 			SourceHook::List<GameDllInfo *>::iterator iter;
   374 			GameDllInfo *pInfo = NULL;
   375 			void *ptr;
   376 			for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
   377 			{
   378 				pInfo = (*iter);
   379 				ptr = (pInfo->factory)(iface, ret);
   380 				if (ptr)
   381 				{
   382 					//this is our gamedll.  unload the others.
   383 					gamedll_list.erase(iter);
   384 					ClearGamedllList();
   385 					pInfo->pGameDLL = static_cast<IServerGameDLL *>(ptr);
   386 					g_GameDll = *pInfo;
   387 					delete pInfo;
   388 					break;
   389 				}
   390 			}
   391 			if (g_GameDll.loaded)
   392 			{
   393 				InitMainStates();
   394 			} else {
   395 				if (ret)
   396 					*ret = IFACE_FAILED;
   397 				return NULL;
   398 			}
   399 		} else {
   400 			//wtf do we do...
   401 			//:TODO: .. something a bit more intelligent?
   402 			Error("Engine requested unknown interface before GameDLL was known!");
   403 			return NULL;
   404 		}
   405 	}
   406 
   407 	//if we got here, there's definitely a gamedll.
   408 	IFACE_MACRO(g_GameDll.factory, GameDLL);
   409 }
   410 
   411 void ClearGamedllList()
   412 {
   413 	SourceHook::List<GameDllInfo *>::iterator iter;
   414 
   415 	GameDllInfo *pInfo;
   416 	for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
   417 	{
   418 		pInfo = (*iter);
   419 		dlclose(pInfo->lib);
   420 		delete pInfo;
   421 	}
   422 
   423 	gamedll_list.clear();
   424 }
   425 
   426 void DLLShutdown_handler()
   427 {
   428 	//Unload plugins
   429 	g_PluginMngr.UnloadAll();
   430 
   431 	// Add the FCVAR_GAMEDLL flag to our cvars so the engine removes them properly
   432 	g_SMConVarAccessor.MarkCommandsAsGameDLL();
   433 	g_SMConVarAccessor.UnregisterGameDLLCommands();
   434 
   435 	SH_CALL(dllExec, &IServerGameDLL::DLLShutdown)();
   436 
   437 	SH_RELEASE_CALLCLASS(dllExec);
   438 	dllExec = NULL;
   439 
   440 	g_SourceHook.CompleteShutdown();
   441 
   442 	if (g_GameDll.lib && g_GameDll.loaded)
   443 		dlclose(g_GameDll.lib);
   444 	memset(&g_GameDll, 0, sizeof(GameDllInfo));
   445 
   446 	RETURN_META(MRES_SUPERCEDE);
   447 }
   448 
   449 int LoadPluginsFromFile(const char *file)
   450 {
   451 	FILE *fp;
   452 	int total = 0, skipped=0;
   453 	PluginId id;
   454 	bool already;
   455 
   456 	fp = fopen(file, "rt");
   457 	if (!fp)
   458 	{
   459 		LogMessage("[META] Could not open plugins file %s\n", file);
   460 		return -1;
   461 	}
   462 
   463 	char buffer[255], error[255], full_path[128];
   464 	const char *ptr, *ext;
   465 	size_t length;
   466 	while (!feof(fp))
   467 	{
   468 		buffer[0] = '\0';
   469 		fgets(buffer, sizeof(buffer)-1, fp);
   470 		length = strlen(buffer);
   471 		if (!length)
   472 			continue;
   473 		if (buffer[length-1] == '\n')
   474 			buffer[length-1] = '\0';
   475 
   476 		UTIL_TrimLeft(buffer);
   477 		UTIL_TrimRight(buffer);
   478 
   479 		if (buffer[0] == '\0' || buffer[0] == ';' || strncmp(buffer, "//", 2) == 0)
   480 			continue;
   481 		//First find if it's an absolute path or not...
   482 		if (buffer[0] == '/' || strncmp(&(buffer[1]), ":\\", 2) == 0)
   483 		{
   484 			//If we're in an absolute path, ignore our normal heuristics
   485 			id = g_PluginMngr.Load(buffer, Pl_File, already, error, sizeof(error)-1);
   486 			if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
   487 			{
   488 				LogMessage("[META] Failed to load plugin %s.  %s", buffer, error);
   489 			} else {
   490 				if (already)
   491 					skipped++;
   492 				else
   493 					total++;
   494 			}
   495 		} else {
   496 			//Attempt to find a file extension
   497 			ptr = UTIL_GetExtension(buffer);
   498 			//Add an extension if there's none there
   499 			if (!ptr)
   500 			{
   501 #if defined WIN32 || defined _WIN32
   502 				ext = ".dll";
   503 #else
   504 				ext = "_i486.so";
   505 #endif
   506 			} else {
   507 				ext = "";
   508 			}
   509 			//Format the new path
   510 			g_SmmAPI.PathFormat(full_path, sizeof(full_path)-1, "%s/%s%s", g_ModPath.c_str(), buffer, ext);
   511 			id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error)-1);
   512 			if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
   513 			{
   514 				LogMessage("[META] Failed to load plugin %s.  %s", buffer, error);
   515 			} else {
   516 				if (already)
   517 					skipped++;
   518 				else
   519 					total++;
   520 			}
   521 		}
   522 	}
   523 	fclose(fp);
   524 
   525 	if (skipped)
   526 	{
   527 		LogMessage("[META] Loaded %d plugins from file (%d already loaded)", total, skipped);
   528 	} else {
   529 		LogMessage("[META] Loaded %d plugins from file.", total);
   530 	}
   531 	
   532 	return total;
   533 }
   534 
   535 //Wrapper function.  This is called when the GameDLL thinks it's using
   536 // the engine's real engineFactory.
   537 void *EngineFactory(const char *iface, int *ret)
   538 {
   539 	IFACE_MACRO(g_Engine.engineFactory, Engine);
   540 }
   541 
   542 //Wrapper function.  This is called when the GameDLL thinks it's using
   543 // the engine's real physicsFactory.
   544 void *PhysicsFactory(const char *iface, int *ret)
   545 {
   546 	IFACE_MACRO(g_Engine.physicsFactory, Physics);
   547 }
   548 
   549 //Wrapper function.  This is called when the GameDLL thinks it's using
   550 // the engine's real fileSystemFactory.
   551 void *FileSystemFactory(const char *iface, int *ret)
   552 {
   553 	IFACE_MACRO(g_Engine.fileSystemFactory, FileSystem);
   554 }
   555 
   556 void LogMessage(const char *msg, ...)
   557 {
   558 	va_list ap;
   559 	static char buffer[2048];
   560 
   561 	buffer[0] = '\0';
   562 
   563 	va_start(ap, msg);
   564 	vsnprintf(buffer, sizeof(buffer)-5, msg, ap);
   565 	strcat(buffer, "\n");
   566 	va_end(ap);
   567 
   568 	if (!g_Engine.engine)
   569 	{
   570 		fprintf(stdout, "%s", buffer);
   571 	} else {
   572 		g_Engine.engine->LogPrint(buffer);
   573 	}
   574 }
   575 
   576 void LevelShutdown_handler(void)
   577 {
   578 	if (!bInFirstLevel)
   579 	{
   580 		char full_path[255];
   581 		g_SmmAPI.PathFormat(full_path, sizeof(full_path) - 1, "%s/%s", g_ModPath.c_str(), GetPluginsFile());
   582 
   583 		LoadPluginsFromFile(full_path);
   584 	} else {
   585 		bInFirstLevel = false;
   586 	}
   587 
   588 	ITER_EVENT(OnLevelShutdown, ());
   589 
   590 	RETURN_META(MRES_IGNORED);
   591 }
   592 
   593 bool LevelInit_handler(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
   594 {
   595 	if (!g_SmmAPI.CacheSuccessful())
   596 	{
   597 		LogMessage("[META] Warning: Failed to initialize Con_Printf.  Defaulting to Msg().");
   598 		LogMessage("[META] Warning: Console messages will not be redirected to rcon console.");
   599 	}
   600 
   601 #if (defined _DEBUG || defined DEBUG) && (defined OS_WIN32)
   602     SetUnhandledExceptionFilter(NULL);
   603 #endif
   604 
   605 	ITER_EVENT(OnLevelInit, (pMapName, pMapEntities, pOldLevel, pLandmarkName, loadGame, background));
   606 
   607 	RETURN_META_VALUE(MRES_IGNORED, false);
   608 }
   609 
   610 #if defined __GNUC__ && (__GNUC__ == 3)
   611 void * ::operator new(size_t size) {
   612 	return(calloc(1, size)); 
   613 }
   614 
   615 void * ::operator new[](size_t size) {
   616 	return(calloc(1, size)); 
   617 }
   618 
   619 void ::operator delete(void * ptr) {
   620 	if(ptr)
   621 		free(ptr);
   622 }
   623 
   624 void ::operator delete[](void * ptr) {
   625 	if(ptr)
   626 		free(ptr);
   627 }
   628 #endif