core/sourcemod.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Mar 21 04:26:53 2008 +0000 (2008-03-21)
changeset 1944 17984feecafd
parent 1921 f993e100e5a3
child 1947 e73a1e51833b
permissions -rw-r--r--
Merged crash fix (amb1539) from 1.0.1 branch
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include <stdio.h>
    33 #include "sourcemod.h"
    34 #include "sourcemm_api.h"
    35 #include "systems/LibrarySys.h"
    36 #include "vm/sp_vm_engine.h"
    37 #include <sh_string.h>
    38 #include "PluginSys.h"
    39 #include "ShareSys.h"
    40 #include "CoreConfig.h"
    41 #include "Logger.h"
    42 #include "ExtensionSys.h"
    43 #include "AdminCache.h"
    44 #include "sm_stringutil.h"
    45 #include "PlayerManager.h"
    46 #include "Translator.h"
    47 #include "ForwardSys.h"
    48 #include "TimerSys.h"
    49 #include "GameConfigs.h"
    50 
    51 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
    52 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
    53 SH_DECL_HOOK1_void(IServerGameDLL, GameFrame, SH_NOATTRIB, false, bool);
    54 SH_DECL_HOOK1_void(IVEngineServer, ServerCommand, SH_NOATTRIB, false, const char *);
    55 
    56 SourcePawnEngine g_SourcePawn;
    57 SourceModBase g_SourceMod;
    58 
    59 ILibrary *g_pJIT = NULL;
    60 SourceHook::String g_BaseDir;
    61 ISourcePawnEngine *g_pSourcePawn = &g_SourcePawn;
    62 IVirtualMachine *g_pVM;
    63 IdentityToken_t *g_pCoreIdent = NULL;
    64 IForward *g_pOnMapEnd = NULL;
    65 bool g_Loaded = false;
    66 
    67 typedef int (*GIVEENGINEPOINTER)(ISourcePawnEngine *);
    68 typedef int (*GIVEENGINEPOINTER2)(ISourcePawnEngine *, unsigned int api_version);
    69 typedef unsigned int (*GETEXPORTCOUNT)();
    70 typedef IVirtualMachine *(*GETEXPORT)(unsigned int);
    71 typedef void (*NOTIFYSHUTDOWN)();
    72 
    73 void ShutdownJIT()
    74 {
    75 	NOTIFYSHUTDOWN notify = (NOTIFYSHUTDOWN)g_pJIT->GetSymbolAddress("NotifyShutdown");
    76 	if (notify)
    77 	{
    78 		notify();
    79 	}
    80 
    81 	g_pJIT->CloseLibrary();
    82 }
    83 
    84 SourceModBase::SourceModBase()
    85 {
    86 	m_IsMapLoading = false;
    87 	m_ExecPluginReload = false;
    88 	m_GotBasePath = false;
    89 }
    90 
    91 ConfigResult SourceModBase::OnSourceModConfigChanged(const char *key, 
    92 									  const char *value, 
    93 									  ConfigSource source, 
    94 									  char *error, 
    95 									  size_t maxlength)
    96 {
    97 	if (strcasecmp(key, "BasePath") == 0)
    98 	{
    99 		if (source == ConfigSource_Console)
   100 		{
   101 			UTIL_Format(error, maxlength, "Cannot be set at runtime");
   102 			return ConfigResult_Reject;
   103 		}
   104 
   105 		if (!m_GotBasePath)
   106 		{
   107 			g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/%s", g_BaseDir.c_str(), value);
   108 			g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), value);
   109 
   110 			m_GotBasePath = true;
   111 		}
   112 
   113 		return ConfigResult_Accept;
   114 	}
   115 
   116 	return ConfigResult_Ignore;
   117 }
   118 
   119 bool SourceModBase::InitializeSourceMod(char *error, size_t maxlength, bool late)
   120 {
   121 	const char *gamepath = g_SMAPI->GetBaseDir();
   122 
   123 	/* Store full path to game */
   124 	g_BaseDir.assign(gamepath);
   125 
   126 	/* Store name of game directory by itself */
   127 	size_t len = strlen(gamepath);
   128 	for (size_t i = len - 1; i >= 0; i--)
   129 	{
   130 		if (gamepath[i] == PLATFORM_SEP_CHAR)
   131 		{
   132 			strncopy(m_ModDir, &gamepath[++i], sizeof(m_ModDir));
   133 			break;
   134 		}
   135 	}
   136 
   137 	/* Initialize CoreConfig so we can get SourceMod base path properly - this basically parses core.cfg */
   138 	g_CoreConfig.Initialize();
   139 
   140 	/* This shouldn't happen, but can't hurt to be safe */
   141 	if (!g_LibSys.PathExists(m_SMBaseDir) || !m_GotBasePath)
   142 	{
   143 		g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/addons/sourcemod", g_BaseDir.c_str());
   144 		g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), "addons/sourcemod");
   145 		m_GotBasePath = true;
   146 	}
   147 
   148 	/* Attempt to load the JIT! */
   149 	char file[PLATFORM_MAX_PATH];
   150 	char myerror[255];
   151 	g_SMAPI->PathFormat(file, sizeof(file), "%s/bin/sourcepawn.jit.x86.%s",
   152 		GetSourceModPath(),
   153 		PLATFORM_LIB_EXT
   154 		);
   155 
   156 	g_pJIT = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
   157 	if (!g_pJIT)
   158 	{
   159 		if (error && maxlength)
   160 		{
   161 			snprintf(error, maxlength, "%s (failed to load bin/sourcepawn.jit.x86.%s)", 
   162 				myerror,
   163 				PLATFORM_LIB_EXT);
   164 		}
   165 		return false;
   166 	}
   167 
   168 	int err;
   169 	
   170 	GIVEENGINEPOINTER2 jit_init2 = (GIVEENGINEPOINTER2)g_pJIT->GetSymbolAddress("GiveEnginePointer2");
   171 	if (!jit_init2)
   172 	{
   173 		GIVEENGINEPOINTER jit_init = (GIVEENGINEPOINTER)g_pJIT->GetSymbolAddress("GiveEnginePointer");
   174 		if (!jit_init)
   175 		{
   176 			ShutdownJIT();
   177 			if (error && maxlength)
   178 			{
   179 				snprintf(error, maxlength, "Failed to find GiveEnginePointer in JIT!");
   180 			}
   181 			return false;
   182 		}
   183 
   184 		if ((err=jit_init(g_pSourcePawn)) != 0)
   185 		{
   186 			ShutdownJIT();
   187 			if (error && maxlength)
   188 			{
   189 				snprintf(error, maxlength, "GiveEnginePointer returned %d in the JIT", err);
   190 			}
   191 			return false;
   192 		}
   193 	} else {
   194 		/* On version bumps, we should check for older versions as well, if the new version fails.
   195 		 * We can then check the exports to see if any VM versions will be sufficient.
   196 		 */
   197 		if ((err=jit_init2(g_pSourcePawn, SOURCEPAWN_ENGINE_API_VERSION)) != SP_ERROR_NONE)
   198 		{
   199 			ShutdownJIT();
   200 			if (error && maxlength)
   201 			{
   202 				snprintf(error, maxlength, "JIT incompatible with SourceMod version");
   203 			}
   204 			return false;
   205 		}
   206 	}
   207 
   208 	GETEXPORTCOUNT jit_getnum = (GETEXPORTCOUNT)g_pJIT->GetSymbolAddress("GetExportCount");
   209 	GETEXPORT jit_get = (GETEXPORT)g_pJIT->GetSymbolAddress("GetExport");
   210 	if (!jit_get)
   211 	{
   212 		ShutdownJIT();
   213 		if (error && maxlength)
   214 		{
   215 			snprintf(error, maxlength, "JIT is missing a necessary export!");
   216 		}
   217 		return false;
   218 	}
   219 
   220 	unsigned int num = jit_getnum();
   221 	if (!num)
   222 	{
   223 		ShutdownJIT();
   224 		if (error && maxlength)
   225 		{
   226 			snprintf(error, maxlength, "JIT did not export any virtual machines!");
   227 		}
   228 		return false;
   229 	}
   230 
   231 	unsigned int api_version;
   232 	for (unsigned int i=0; i<num; i++)
   233 	{
   234 		 if ((g_pVM=jit_get(i)) == NULL)
   235 		 {
   236 			 if (error && maxlength)
   237 			 {
   238 				 snprintf(error, maxlength, "JIT did not export any virtual machines!");
   239 			 }
   240 			 continue;
   241 		 }
   242 		/* Refuse any API that we might not be able to deal with.
   243 		 * Also refuse anything < 3 because we need fake natives.
   244 		 */
   245 		 api_version = g_pVM->GetAPIVersion();
   246 		 if (api_version < 3 || api_version > SOURCEPAWN_VM_API_VERSION)
   247 		 {
   248 			 if (error && maxlength)
   249 			 {
   250 				 snprintf(error, maxlength, "JIT is not a compatible version");
   251 			 }
   252 			 g_pVM = NULL;
   253 			 continue;
   254 		 }
   255 		 break;
   256 	}
   257 
   258 	if (!g_pVM)
   259 	{
   260 		ShutdownJIT();
   261 		return false;
   262 	}
   263 
   264 	/* Hook this now so we can detect startup without calling StartSourceMod() */
   265 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
   266 
   267 	/* Only load if we're not late */
   268 	if (!late)
   269 	{
   270 		StartSourceMod(false);
   271 	}
   272 
   273 	return true;
   274 }
   275 
   276 void SourceModBase::StartSourceMod(bool late)
   277 {
   278 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
   279 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, &g_Timers, &TimerSystem::GameFrame, false);
   280 
   281 	enginePatch = SH_GET_CALLCLASS(engine);
   282 	gamedllPatch = SH_GET_CALLCLASS(gamedll);
   283 
   284 	g_ShareSys.Initialize();
   285 
   286 	/* Make the global core identity */
   287 	g_pCoreIdent = g_ShareSys.CreateCoreIdentity();
   288 
   289 	/* Notify! */
   290 	SMGlobalClass *pBase = SMGlobalClass::head;
   291 	while (pBase)
   292 	{
   293 		pBase->OnSourceModStartup(false);
   294 		pBase = pBase->m_pGlobalClassNext;
   295 	}
   296 
   297 	/* Notify! */
   298 	pBase = SMGlobalClass::head;
   299 	while (pBase)
   300 	{
   301 		pBase->OnSourceModAllInitialized();
   302 		pBase = pBase->m_pGlobalClassNext;
   303 	}
   304 
   305 	/* Notify! */
   306 	pBase = SMGlobalClass::head;
   307 	while (pBase)
   308 	{
   309 		pBase->OnSourceModAllInitialized_Post();
   310 		pBase = pBase->m_pGlobalClassNext;
   311 	}
   312 
   313 	/* Add us now... */
   314 	g_ShareSys.AddInterface(NULL, this);
   315 
   316 	/* We're loaded! */
   317 	g_Loaded = true;
   318 
   319 	/* Initialize VSP stuff */
   320 	if (vsp_interface != NULL)
   321 	{
   322 		g_SourceMod_Core.OnVSPListening(vsp_interface);
   323 	}
   324 }
   325 
   326 static bool g_LevelEndBarrier = false;
   327 bool SourceModBase::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
   328 {
   329 	/* If we're not loaded... */
   330 	if (!g_Loaded)
   331 	{
   332 		/* Do all global initialization now */
   333 		StartSourceMod(true);
   334 	}
   335 
   336 	m_IsMapLoading = true;
   337 	m_ExecPluginReload = true;
   338 
   339 	/* Notify! */
   340 	SMGlobalClass *pBase = SMGlobalClass::head;
   341 	while (pBase)
   342 	{
   343 		pBase->OnSourceModLevelChange(pMapName);
   344 		pBase = pBase->m_pGlobalClassNext;
   345 	}
   346 
   347 	DoGlobalPluginLoads();
   348 
   349 	m_IsMapLoading = false;
   350 
   351 	/* Notify! */
   352 	pBase = SMGlobalClass::head;
   353 	while (pBase)
   354 	{
   355 		pBase->OnSourceModPluginsLoaded();
   356 		pBase = pBase->m_pGlobalClassNext;
   357 	}
   358 
   359 	if (!g_pOnMapEnd)
   360 	{
   361 		g_pOnMapEnd = g_Forwards.CreateForward("OnMapEnd", ET_Ignore, 0, NULL);
   362 	}
   363 
   364 	g_LevelEndBarrier = true;
   365 
   366 	RETURN_META_VALUE(MRES_IGNORED, true);
   367 }
   368 
   369 void SourceModBase::LevelShutdown()
   370 {
   371 	if (g_LevelEndBarrier)
   372 	{
   373 		SMGlobalClass *next = SMGlobalClass::head;
   374 		while (next)
   375 		{
   376 			next->OnSourceModLevelEnd();
   377 			next = next->m_pGlobalClassNext;
   378 		}
   379 		
   380 		if (g_pOnMapEnd != NULL)
   381 		{
   382 			g_pOnMapEnd->Execute(NULL);
   383 		}
   384 
   385 		g_Timers.RemoveMapChangeTimers();
   386 
   387 		g_LevelEndBarrier = false;
   388 	}
   389 
   390 	g_OnMapStarted = false;
   391 
   392 	if (m_ExecPluginReload)
   393 	{
   394 		g_PluginSys.ReloadOrUnloadPlugins();
   395 		m_ExecPluginReload = false;
   396 	}
   397 }
   398 
   399 bool SourceModBase::IsMapLoading() const
   400 {
   401 	return m_IsMapLoading;
   402 }
   403 
   404 void SourceModBase::DoGlobalPluginLoads()
   405 {
   406 	char config_path[PLATFORM_MAX_PATH];
   407 	char plugins_path[PLATFORM_MAX_PATH];
   408 
   409 	BuildPath(Path_SM, config_path, 
   410 		sizeof(config_path),
   411 		"configs/plugin_settings.cfg");
   412 	BuildPath(Path_SM, plugins_path,
   413 		sizeof(plugins_path),
   414 		"plugins");
   415 
   416 	/* Load any auto extensions */
   417 	g_Extensions.TryAutoload();
   418 
   419 	/* Fire the extensions ready message */
   420 	g_SMAPI->MetaFactory(SOURCEMOD_NOTICE_EXTENSIONS, NULL, NULL);	
   421 
   422 	/* Load any game extension */
   423 	const char *game_ext;
   424 	if ((game_ext = g_pGameConf->GetKeyValue("GameExtension")) != NULL)
   425 	{
   426 		char path[PLATFORM_MAX_PATH];
   427 		UTIL_Format(path, sizeof(path), "%s.ext." PLATFORM_LIB_EXT, game_ext);
   428 		g_Extensions.LoadAutoExtension(path);
   429 	}
   430 
   431 	/* Run the first pass */
   432 	g_PluginSys.LoadAll_FirstPass(config_path, plugins_path);
   433 
   434 	/* Mark any extensions as loaded */
   435 	g_Extensions.MarkAllLoaded();
   436 
   437 	/* No modules yet, it's safe to call this from here */
   438 	g_PluginSys.LoadAll_SecondPass();
   439 
   440 	/* Re-mark any extensions as loaded */
   441 	g_Extensions.MarkAllLoaded();
   442 
   443 	/* Call OnAllPluginsLoaded */
   444 	g_PluginSys.AllPluginsLoaded();
   445 }
   446 
   447 size_t SourceModBase::BuildPath(PathType type, char *buffer, size_t maxlength, const char *format, ...)
   448 {
   449 	char _buffer[PLATFORM_MAX_PATH];
   450 	va_list ap;
   451 
   452 	va_start(ap, format);
   453 	vsnprintf(_buffer, PLATFORM_MAX_PATH, format, ap);
   454 	va_end(ap);
   455 
   456 	/* If we get a "file://" notation, strip off the file:// part so we're left 
   457 	 * with an absolute path.  Note that the absolute path gets returned, so 
   458 	 * usage with relative paths here is completely invalid.
   459 	 */
   460 	if (type != Path_SM_Rel && strncmp(_buffer, "file://", 7) == 0)
   461 	{
   462 		return g_LibSys.PathFormat(buffer, maxlength, "%s", &_buffer[7]);
   463 	}
   464 
   465 	const char *base = NULL;
   466 	if (type == Path_Game)
   467 	{
   468 		base = GetGamePath();
   469 	}
   470 	else if (type == Path_SM)
   471 	{
   472 		base = GetSourceModPath();
   473 	}
   474 	else if (type == Path_SM_Rel)
   475 	{
   476 		base = m_SMRelDir;
   477 	}
   478 
   479 	if (base)
   480 	{
   481 		return g_LibSys.PathFormat(buffer, maxlength, "%s/%s", base, _buffer);
   482 	}
   483 	else
   484 	{
   485 		return g_LibSys.PathFormat(buffer, maxlength, "%s", _buffer);
   486 	}
   487 }
   488 
   489 void SourceModBase::CloseSourceMod()
   490 {
   491 	/* Force a level end */
   492 	LevelShutdown();
   493 
   494 	/* Unload plugins */
   495 	g_PluginSys.Shutdown();
   496 
   497 	/* Unload extensions */
   498 	g_Extensions.Shutdown();
   499 
   500 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
   501 
   502 	if (g_Loaded)
   503 	{
   504 		if (g_pOnMapEnd)
   505 		{
   506 			g_Forwards.ReleaseForward(g_pOnMapEnd);
   507 		}
   508 	
   509 		/* Notify! */
   510 		SMGlobalClass *pBase = SMGlobalClass::head;
   511 		while (pBase)
   512 		{
   513 			pBase->OnSourceModShutdown();
   514 			pBase = pBase->m_pGlobalClassNext;
   515 		}
   516 	
   517 		/* Delete all data packs */
   518 		CStack<CDataPack *>::iterator iter;
   519 		CDataPack *pd;
   520 		for (iter=m_freepacks.begin(); iter!=m_freepacks.end(); iter++)
   521 		{
   522 			pd = (*iter);
   523 			delete pd;
   524 		}
   525 		m_freepacks.popall();
   526 	
   527 		/* Notify! */
   528 		pBase = SMGlobalClass::head;
   529 		while (pBase)
   530 		{
   531 			pBase->OnSourceModAllShutdown();
   532 			pBase = pBase->m_pGlobalClassNext;
   533 		}
   534 	
   535 		if (enginePatch)
   536 		{
   537 			SH_RELEASE_CALLCLASS(enginePatch);
   538 			enginePatch = NULL;
   539 		}
   540 	
   541 		if (gamedllPatch)
   542 		{
   543 			SH_RELEASE_CALLCLASS(gamedllPatch);
   544 			gamedllPatch = NULL;
   545 		}
   546 
   547 		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
   548 		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, &g_Timers, &TimerSystem::GameFrame, false);
   549 	}
   550 
   551 	/* Rest In Peace */
   552 	ShutdownJIT();
   553 }
   554 
   555 void SourceModBase::LogMessage(IExtension *pExt, const char *format, ...)
   556 {
   557 	IExtensionInterface *pAPI = pExt->GetAPI();
   558 	const char *tag = pAPI->GetExtensionTag();
   559 	char buffer[2048];
   560 	va_list ap;
   561 
   562 	va_start(ap, format);
   563 	vsnprintf(buffer, sizeof(buffer), format, ap);
   564 	va_end(ap);
   565 
   566 	if (tag)
   567 	{
   568 		g_Logger.LogMessage("[%s] %s", tag, buffer);
   569 	} else {
   570 		g_Logger.LogMessage("%s", buffer);
   571 	}
   572 }
   573 
   574 void SourceModBase::LogError(IExtension *pExt, const char *format, ...)
   575 {
   576 	IExtensionInterface *pAPI = pExt->GetAPI();
   577 	const char *tag = pAPI->GetExtensionTag();
   578 	char buffer[2048];
   579 	va_list ap;
   580 
   581 	va_start(ap, format);
   582 	vsnprintf(buffer, sizeof(buffer), format, ap);
   583 	va_end(ap);
   584 
   585 	if (tag)
   586 	{
   587 		g_Logger.LogError("[%s] %s", tag, buffer);
   588 	} else {
   589 		g_Logger.LogError("%s", buffer);
   590 	}
   591 }
   592 
   593 size_t SourceModBase::FormatString(char *buffer, size_t maxlength, IPluginContext *pContext, const cell_t *params, unsigned int param)
   594 {
   595 	char *fmt;
   596 
   597 	pContext->LocalToString(params[param], &fmt);
   598 
   599 	int lparam = ++param;
   600 
   601 	return atcprintf(buffer, maxlength, fmt, pContext, params, &lparam);
   602 }
   603 
   604 const char *SourceModBase::GetSourceModPath() const
   605 {
   606 	return m_SMBaseDir;
   607 }
   608 
   609 const char *SourceModBase::GetGamePath() const
   610 {
   611 	return g_BaseDir.c_str();
   612 }
   613 
   614 unsigned int SourceModBase::SetGlobalTarget(unsigned int index)
   615 {
   616 	unsigned int old = m_target;
   617 	m_target = index;
   618 	return old;
   619 }
   620 
   621 unsigned int SourceModBase::GetGlobalTarget() const
   622 {
   623 	return m_target;
   624 }
   625 
   626 IDataPack *SourceModBase::CreateDataPack()
   627 {
   628 	CDataPack *pack;
   629 	if (m_freepacks.empty())
   630 	{
   631 		pack = new CDataPack;
   632 	} else {
   633 		pack = m_freepacks.front();
   634 		m_freepacks.pop();
   635 		pack->Initialize();
   636 	}
   637 	return pack;
   638 }
   639 
   640 void SourceModBase::FreeDataPack(IDataPack *pack)
   641 {
   642 	m_freepacks.push(static_cast<CDataPack *>(pack));
   643 }
   644 
   645 Handle_t SourceModBase::GetDataPackHandleType(bool readonly)
   646 {
   647 	//:TODO:
   648 	return 0;
   649 }
   650 
   651 const char *SourceModBase::GetGameFolderName() const
   652 {
   653 	return m_ModDir;
   654 }
   655 
   656 ISourcePawnEngine *SourceModBase::GetScriptingEngine()
   657 {
   658 	return g_pSourcePawn;
   659 }
   660 
   661 IVirtualMachine *SourceModBase::GetScriptingVM()
   662 {
   663 	return g_pVM;
   664 }
   665 
   666 void SourceModBase::AllPluginsLoaded()
   667 {
   668 	SMGlobalClass *base = SMGlobalClass::head;
   669 	while (base)
   670 	{
   671 		base->OnSourceModGameInitialized();
   672 		base = base->m_pGlobalClassNext;
   673 	}
   674 }
   675 
   676 time_t SourceModBase::GetAdjustedTime()
   677 {
   678 	return ::GetAdjustedTime();
   679 }
   680 
   681 void SourceModBase::AddGameFrameHook(GAME_FRAME_HOOK hook)
   682 {
   683 	m_frame_hooks.push_back(hook);
   684 }
   685 
   686 void SourceModBase::RemoveGameFrameHook(GAME_FRAME_HOOK hook)
   687 {
   688 	for (size_t i = 0; i < m_frame_hooks.size(); i++)
   689 	{
   690 		if (m_frame_hooks[i] == hook)
   691 		{
   692 			m_frame_hooks.erase(m_frame_hooks.iterAt(i));
   693 			return;
   694 		}
   695 	}
   696 }
   697 
   698 void SourceModBase::ProcessGameFrameHooks(bool simulating)
   699 {
   700 	if (m_frame_hooks.size() == 0)
   701 	{
   702 		return;
   703 	}
   704 
   705 	for (size_t i = 0; i < m_frame_hooks.size(); i++)
   706 	{
   707 		m_frame_hooks[i](simulating);
   708 	}
   709 }
   710 
   711 SMGlobalClass *SMGlobalClass::head = NULL;
   712 
   713 SMGlobalClass::SMGlobalClass()
   714 {
   715 	m_pGlobalClassNext = SMGlobalClass::head;
   716 	SMGlobalClass::head = this;
   717 }