sourcemm/sourcemm.cpp
author David Anderson <dvander@alliedmods.net>
Fri Dec 23 23:38:38 2005 +0000 (2005-12-23)
changeset 166 e85ebcf21a7e
parent 147 5293725a71fe
child 175 b497b7777f6b
permissions -rw-r--r--
initial import of 1.2 api changes
     1 /* ======== SourceMM ========
     2 * Copyright (C) 2004-2005 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 	g_SmmAPI.PathFormat(full_path, sizeof(full_path)-1, "%s/%s", g_ModPath.c_str(), GetPluginsFile());
    96 
    97 	//Like metamod, reload plugins at the end of the map.
    98 	//This is so plugins can hook everything on load, BUT, new plugins will be reloaded
    99 	// if the server is shut down (silly, but rare case).
   100 	bInFirstLevel = true;
   101 
   102 	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLInit, g_GameDll.pGameDLL, DLLInit, false);
   103 	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLInit, g_GameDll.pGameDLL, DLLInit_Post, true);
   104 	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLShutdown, g_GameDll.pGameDLL, DLLShutdown_handler, false);
   105 	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelShutdown, g_GameDll.pGameDLL, LevelShutdown_handler, true);
   106 	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelInit, g_GameDll.pGameDLL, LevelInit_handler, true);
   107 }
   108 
   109 bool DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
   110 {
   111 	g_Engine.engineFactory = engineFactory;
   112 	g_Engine.fileSystemFactory = filesystemFactory;
   113 	g_Engine.physicsFactory = physicsFactory;
   114 	g_Engine.pGlobals = pGlobals;
   115 
   116 	g_Engine.engine = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
   117 	if (!g_Engine.engine)
   118 	{
   119 		Error("Could not find IVEngineServer! Metamod cannot load.");
   120 		return false;
   121 	}
   122 	g_Engine.icvar = (ICvar *)((engineFactory)(VENGINE_CVAR_INTERFACE_VERSION , NULL));
   123 	if (!g_Engine.icvar)
   124 	{
   125 		Error("Could not find ICvar! Metamod cannot load.");
   126 		return false;
   127 	}
   128 
   129 	g_Engine.loaded = true;
   130 
   131 	//Initialize our console hooks
   132 	ConCommandBaseMgr::OneTimeInit(static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor));
   133 
   134 	if (!g_SmmAPI.CacheCmds())
   135 	{
   136 		LogMessage("[META] Warning: Failed to initialize Con_Printf.  Defaulting to Msg().");
   137 		LogMessage("[META] Warning: Console messages will not be redirected to rcon console.");
   138 	}
   139 
   140 	char full_path[260];
   141 	g_SmmAPI.PathFormat(full_path, sizeof(full_path)-1, "%s/%s", g_ModPath.c_str(), GetPluginsFile());
   142 
   143 	LoadPluginsFromFile(full_path);
   144 
   145 	bInFirstLevel = true;
   146 
   147 	dllExec = SH_GET_CALLCLASS(g_GameDll.pGameDLL);
   148 
   149 	RETURN_META_VALUE(MRES_IGNORED, true);
   150 }
   151 
   152 bool DLLInit_Post(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
   153 {
   154 	g_PluginMngr.SetAllLoaded();
   155 	RETURN_META_VALUE(MRES_IGNORED, true);
   156 }
   157 
   158 //This is where the magic happens
   159 SMM_API void *CreateInterface(const char *iface, int *ret)
   160 {
   161 	if (!gParsedGameInfo)
   162 	{
   163 		gParsedGameInfo = true;
   164 		char curpath[260] = {0};
   165 		char dllpath[260] = {0};
   166 		getcwd(curpath, sizeof(curpath)-1);
   167 		if (!GetFileOfAddress((void *)CreateInterface, dllpath, sizeof(dllpath)-1))
   168 		{
   169 			Error("GetFileOfAddress() failed! Metamod cannot load.");
   170 			return NULL;
   171 		}
   172 		SourceHook::String s_dllpath(dllpath);
   173 		//begin the heuristics for searching for the mod path (these are quite ugly).
   174 		//for OS compatibility purposes, we're going to do case insensitivity on windows.
   175 		size_t path_len = strlen(curpath);
   176 		size_t dll_len = strlen(dllpath);
   177 
   178 		//strip the dll path off
   179 		//:TODO: with path stuff - in Linux, \ can exist in a file path as a non-seperator!
   180 		for (size_t i=dll_len-1; i>0; i--)
   181 		{
   182 			if (dllpath[i] == '\\' || dllpath[i] == '/')
   183 			{
   184 				if (i == dll_len-1)
   185 					break;
   186 				//save path by stripping off file name and ending terminator
   187 				dllpath[i] = '\0';
   188 				dll_len = i;
   189 				break;
   190 			}
   191 		}
   192 
   193 		//strip absolute path terminators if any!
   194 		if (curpath[path_len-1] == '/' || curpath[path_len-1] == '\\')
   195 			curpath[--path_len] = '\0';
   196 
   197 		//if the base path doesn't fit into the module path, something is wrong!
   198 		// ex: c:\games\srcds
   199 		//     c:\gaben.dll
   200 		if (path_len > dll_len)
   201 		{
   202 			Error("Could not detect GameDLL path! Metamod cannot load[1].");
   203 			return NULL;
   204 		}
   205 
   206 		//we are now in such a position that the two dir names SHOULD MATCH!
   207 		typedef int (*STRNCMP)(const char *str1, const char *str2, size_t n);
   208 		STRNCMP cmp = 
   209 #ifdef WIN32
   210 			strnicmp;
   211 #else
   212 			strncmp;
   213 #endif
   214 		//are they equal?
   215 		if ( ((cmp)(curpath, dllpath, path_len)) != 0 )
   216 		{
   217 			//:TODO: In this case, we should read /proc/self/maps and find srcds!
   218 			Error("Could not detect GameDLL path! Metamod cannot load[2].");
   219 			return NULL;
   220 		}
   221 		//this will skip past the dir and its separator char
   222 		char *ptr = &(dllpath[path_len+1]);
   223 		path_len = strlen(ptr);
   224 		for (size_t i=0; i<path_len; i++)
   225 		{
   226 			if (ptr[i] == '/' || ptr[i] == '\\')
   227 			{
   228 				if (i == 0)
   229 				{
   230 					Error("Could not detect GameDLL path! Metamod cannot load[3].");
   231 					return NULL;
   232 				} else {
   233 					ptr[i] = '\0';
   234 					path_len = i+1;
   235 					break;
   236 				}
   237 			}
   238 		}
   239 		//WE NOW HAVE A GUESS AT THE MOD DIR.  OH MY GOD.
   240 		char temp_path[260];
   241 		snprintf(temp_path, sizeof(temp_path)-1, 
   242 			"%s%s%s", 
   243 			curpath, 
   244 			PATH_SEP_STR, 
   245 			ptr);
   246 
   247 		g_ModPath.assign(temp_path);
   248 
   249 		snprintf(temp_path, sizeof(temp_path)-1, 
   250 			"%s%s%s", 
   251 			g_ModPath.c_str(),
   252 			PATH_SEP_STR,
   253 			"gameinfo.txt");
   254 
   255 		FILE *fp = fopen(temp_path, "rt");
   256 		if (!fp)
   257 		{
   258 			Error("Unable to open gameinfo.txt!  Metamod cannot load.");
   259 			return NULL;
   260 		}
   261 		char buffer[255];
   262 		char key[128], val[128];
   263 		size_t len = 0;
   264 		const char *gameinfo = "|gameinfo_path|";
   265 		size_t gameinfo_len = strlen(gameinfo);
   266 		bool search = false;
   267 		bool gamebin = false;
   268 		const char *lptr;
   269 		while (!feof(fp))
   270 		{
   271 			buffer[0] = '\0';
   272 			fgets(buffer, sizeof(buffer)-1, fp);
   273 			len = strlen(buffer);
   274 			if (buffer[len-1] == '\n')
   275 				buffer[--len] = '\0';
   276 			UTIL_TrimComments(buffer);
   277 			UTIL_TrimLeft(buffer);
   278 			UTIL_TrimRight(buffer);
   279 			if (stricmp(buffer, "SearchPaths") == 0)
   280 				search = true;
   281 			if (!search)
   282 				continue;
   283 			UTIL_KeySplit(buffer, key, sizeof(key)-1, val, sizeof(val)-1);
   284 			if (stricmp(key, "Game") == 0 || stricmp(key, "GameBin") == 0)
   285 			{
   286 				if (stricmp(key, "Game") == 0)
   287 					gamebin = false;
   288 				else
   289 					gamebin = true;
   290 
   291 				if (strncmp(val, gameinfo, gameinfo_len) == 0)
   292 				{
   293 					ptr = &(val[gameinfo_len]);
   294 					if (ptr[0] == '.')
   295 						ptr++;
   296 					lptr = g_ModPath.c_str();
   297 				} else {
   298 					lptr = curpath;
   299 					ptr = val;
   300 				}
   301 				size_t ptr_len = strlen(ptr);
   302 				if (ptr[ptr_len] == '/' || ptr[ptr_len] == '\\')
   303 					ptr[--ptr_len] = '\0';
   304 				//no need to append "bin"
   305 				if (gamebin)
   306 				{
   307 					g_SmmAPI.PathFormat(temp_path, sizeof(temp_path)-1, "%s/%s/%s", lptr, ptr, SERVER_DLL);
   308 				} else {
   309 					g_SmmAPI.PathFormat(temp_path, sizeof(temp_path)-1, "%s/%s/%s/%s", lptr, ptr, "bin", SERVER_DLL);
   310 				}
   311 				if (!UTIL_PathCmp(s_dllpath.c_str(), temp_path))
   312 				{
   313 					FILE *fp = fopen(temp_path, "rb");
   314 					if (!fp)
   315 						continue;
   316 					//:TODO: Optimize this a bit!
   317 					SourceHook::List<GameDllInfo *>::iterator iter;
   318 					GameDllInfo *pCheck;
   319 					bool found = false;
   320 					for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
   321 					{
   322 						pCheck = (*iter);
   323 						if (GetFileOfAddress(pCheck->factory, buffer, sizeof(buffer)-1))
   324 						{
   325 							if (UTIL_PathCmp(temp_path, buffer))
   326 							{
   327 								found = true;
   328 								break;
   329 							}
   330 						}
   331 					}
   332 					if (found)
   333 						continue;
   334 					fclose(fp);
   335 					HINSTANCE gamedll = dlmount(temp_path);
   336 					if (gamedll == NULL)
   337 						continue;
   338 					CreateInterfaceFn fn = (CreateInterfaceFn)dlsym(gamedll, "CreateInterface");
   339 					if (fn == NULL)
   340 					{
   341 						dlclose(gamedll);
   342 						continue;
   343 					}
   344 					GameDllInfo *pInfo = new GameDllInfo;
   345 					pInfo->factory = fn;
   346 					pInfo->lib = gamedll;
   347 					pInfo->loaded = true;
   348 					pInfo->pGameDLL = NULL;
   349 					gamedll_list.push_back(pInfo);
   350 				}
   351 			}
   352 		}
   353 		fclose(fp);
   354 	}
   355 
   356 	if (!g_GameDll.loaded)
   357 	{
   358 		const char *str = "ServerGameDLL";
   359 		size_t len = strlen(str);
   360 
   361 		if (strncmp(iface, str, len) == 0)
   362 		{
   363 			//This is the interface we want!  Right now we support versions 3 and 4.
   364 			int version = atoi(&(iface[len]));
   365 			if (version < MIN_GAMEDLL_VERSION || version > MAX_GAMEDLL_VERSION)
   366 			{
   367 				Error("GameDLL version %d is not supported by Metamod!", version);
   368 				return NULL;
   369 			}
   370 			SourceHook::List<GameDllInfo *>::iterator iter;
   371 			GameDllInfo *pInfo = NULL;
   372 			void *ptr;
   373 			for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
   374 			{
   375 				pInfo = (*iter);
   376 				ptr = (pInfo->factory)(iface, ret);
   377 				if (ptr)
   378 				{
   379 					//this is our gamedll.  unload the others.
   380 					gamedll_list.erase(iter);
   381 					ClearGamedllList();
   382 					pInfo->pGameDLL = static_cast<IServerGameDLL *>(ptr);
   383 					g_GameDll = *pInfo;
   384 					delete pInfo;
   385 					break;
   386 				}
   387 			}
   388 			if (g_GameDll.loaded)
   389 			{
   390 				InitMainStates();
   391 			} else {
   392 				if (ret)
   393 					*ret = IFACE_FAILED;
   394 				return NULL;
   395 			}
   396 		} else {
   397 			//wtf do we do...
   398 			//:TODO: .. something a bit more intelligent?
   399 			Error("Engine requested unknown interface before GameDLL was known!");
   400 			return NULL;
   401 		}
   402 	}
   403 
   404 	//if we got here, there's definitely a gamedll.
   405 	IFACE_MACRO(g_GameDll.factory, GameDLL);
   406 }
   407 
   408 void ClearGamedllList()
   409 {
   410 	SourceHook::List<GameDllInfo *>::iterator iter;
   411 
   412 	GameDllInfo *pInfo;
   413 	for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
   414 	{
   415 		pInfo = (*iter);
   416 		dlclose(pInfo->lib);
   417 		delete pInfo;
   418 	}
   419 
   420 	gamedll_list.clear();
   421 }
   422 
   423 void DLLShutdown_handler()
   424 {
   425 	//Unload plugins
   426 	g_PluginMngr.UnloadAll();
   427 
   428 	// Add the FCVAR_GAMEDLL flag to our cvars so the engine removes them properly
   429 	g_SMConVarAccessor.MarkCommandsAsGameDLL();
   430 	g_SMConVarAccessor.UnregisterGameDLLCommands();
   431 
   432 	SH_CALL(dllExec, &IServerGameDLL::DLLShutdown)();
   433 
   434 	SH_RELEASE_CALLCLASS(dllExec);
   435 	dllExec = NULL;
   436 
   437 	g_SourceHook.CompleteShutdown();
   438 
   439 	if (g_GameDll.lib && g_GameDll.loaded)
   440 		dlclose(g_GameDll.lib);
   441 	memset(&g_GameDll, 0, sizeof(GameDllInfo));
   442 
   443 	RETURN_META(MRES_SUPERCEDE);
   444 }
   445 
   446 int LoadPluginsFromFile(const char *file)
   447 {
   448 	FILE *fp;
   449 	int total = 0, skipped=0;
   450 	PluginId id;
   451 	bool already;
   452 
   453 	fp = fopen(file, "rt");
   454 	if (!fp)
   455 	{
   456 		LogMessage("[META] Could not open plugins file %s\n", file);
   457 		return -1;
   458 	}
   459 
   460 	char buffer[255], error[255], full_path[128];
   461 	const char *ptr, *ext;
   462 	size_t length;
   463 	while (!feof(fp))
   464 	{
   465 		buffer[0] = '\0';
   466 		fgets(buffer, sizeof(buffer)-1, fp);
   467 		length = strlen(buffer);
   468 		if (!length)
   469 			continue;
   470 		if (buffer[length-1] == '\n')
   471 			buffer[length-1] = '\0';
   472 
   473 		UTIL_TrimLeft(buffer);
   474 		UTIL_TrimRight(buffer);
   475 
   476 		if (buffer[0] == '\0' || buffer[0] == ';' || strncmp(buffer, "//", 2) == 0)
   477 			continue;
   478 		//First find if it's an absolute path or not...
   479 		if (buffer[0] == '/' || strncmp(&(buffer[1]), ":\\", 2) == 0)
   480 		{
   481 			//If we're in an absolute path, ignore our normal heuristics
   482 			id = g_PluginMngr.Load(buffer, Pl_File, already, error, sizeof(error)-1);
   483 			if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
   484 			{
   485 				LogMessage("[META] Failed to load plugin %s.  %s", buffer, error);
   486 			} else {
   487 				if (already)
   488 					skipped++;
   489 				else
   490 					total++;
   491 			}
   492 		} else {
   493 			//Attempt to find a file extension
   494 			ptr = UTIL_GetExtension(buffer);
   495 			//Add an extension if there's none there
   496 			if (!ptr)
   497 			{
   498 #if defined WIN32 || defined _WIN32
   499 				ext = ".dll";
   500 #else
   501 				ext = "_i486.so";
   502 #endif
   503 			} else {
   504 				ext = "";
   505 			}
   506 			//Format the new path
   507 			g_SmmAPI.PathFormat(full_path, sizeof(full_path)-1, "%s/%s%s", g_ModPath.c_str(), buffer, ext);
   508 			id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error)-1);
   509 			if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
   510 			{
   511 				LogMessage("[META] Failed to load plugin %s.  %s", buffer, error);
   512 			} else {
   513 				if (already)
   514 					skipped++;
   515 				else
   516 					total++;
   517 			}
   518 		}
   519 	}
   520 	fclose(fp);
   521 
   522 	if (skipped)
   523 	{
   524 		LogMessage("[META] Loaded %d plugins from file (%d already loaded)", total, skipped);
   525 	} else {
   526 		LogMessage("[META] Loaded %d plugins from file.", total);
   527 	}
   528 	
   529 	return total;
   530 }
   531 
   532 //Wrapper function.  This is called when the GameDLL thinks it's using
   533 // the engine's real engineFactory.
   534 void *EngineFactory(const char *iface, int *ret)
   535 {
   536 	IFACE_MACRO(g_Engine.engineFactory, Engine);
   537 }
   538 
   539 //Wrapper function.  This is called when the GameDLL thinks it's using
   540 // the engine's real physicsFactory.
   541 void *PhysicsFactory(const char *iface, int *ret)
   542 {
   543 	IFACE_MACRO(g_Engine.physicsFactory, Physics);
   544 }
   545 
   546 //Wrapper function.  This is called when the GameDLL thinks it's using
   547 // the engine's real fileSystemFactory.
   548 void *FileSystemFactory(const char *iface, int *ret)
   549 {
   550 	IFACE_MACRO(g_Engine.fileSystemFactory, FileSystem);
   551 }
   552 
   553 void LogMessage(const char *msg, ...)
   554 {
   555 	va_list ap;
   556 	static char buffer[2048];
   557 
   558 	buffer[0] = '\0';
   559 
   560 	va_start(ap, msg);
   561 	vsnprintf(buffer, sizeof(buffer)-5, msg, ap);
   562 	strcat(buffer, "\n");
   563 	va_end(ap);
   564 
   565 	if (!g_Engine.engine)
   566 	{
   567 		fprintf(stdout, "%s", buffer);
   568 	} else {
   569 		g_Engine.engine->LogPrint(buffer);
   570 	}
   571 }
   572 
   573 void LevelShutdown_handler(void)
   574 {
   575 	if (!bInFirstLevel)
   576 	{
   577 		char full_path[255];
   578 #if defined WIN32 || defined _WIN32
   579 		snprintf(full_path, sizeof(full_path)-1, "%s\\addons\\metamod\\%s", g_ModPath.c_str(), "metaplugins.ini");
   580 #else
   581 		snprintf(full_path, sizeof(full_path)-1, "%s/addons/metamod/%s", g_ModPath.c_str(), "metaplugins.ini");
   582 #endif
   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 WIN32 || defined _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