core/sourcemod.cpp
author David Anderson <dvander@alliedmods.net>
Wed May 13 21:55:50 2009 -0400 (2009-05-13)
changeset 2726 c96f3fab8211
parent 2632 f297107ad5da
child 2728 96022dad7e53
permissions -rw-r--r--
Import of core modularization plan (bug 3599).
     1 /**
     2  * vim: set ts=4 sw=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2009 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 "LibrarySys.h"
    36 #include <sh_string.h>
    37 #include "PluginSys.h"
    38 #include "ShareSys.h"
    39 #include "CoreConfig.h"
    40 #include "Logger.h"
    41 #include "ExtensionSys.h"
    42 #include "AdminCache.h"
    43 #include "sm_stringutil.h"
    44 #include "PlayerManager.h"
    45 #include "Translator.h"
    46 #include "ForwardSys.h"
    47 #include "TimerSys.h"
    48 #include "GameConfigs.h"
    49 #include "DebugReporter.h"
    50 #include "Profiler.h"
    51 #include "frame_hooks.h"
    52 #include "logic_bridge.h"
    53 
    54 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
    55 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
    56 SH_DECL_HOOK1_void(IServerGameDLL, GameFrame, SH_NOATTRIB, false, bool);
    57 SH_DECL_HOOK1_void(IVEngineServer, ServerCommand, SH_NOATTRIB, false, const char *);
    58 
    59 SourceModBase g_SourceMod;
    60 
    61 ILibrary *g_pJIT = NULL;
    62 SourceHook::String g_BaseDir;
    63 ISourcePawnEngine *g_pSourcePawn = NULL;
    64 ISourcePawnEngine2 *g_pSourcePawn2 = NULL;
    65 IdentityToken_t *g_pCoreIdent = NULL;
    66 IForward *g_pOnMapEnd = NULL;
    67 bool g_Loaded = false;
    68 bool sm_show_debug_spew = false;
    69 
    70 typedef ISourcePawnEngine *(*GET_SP_V1)();
    71 typedef ISourcePawnEngine2 *(*GET_SP_V2)();
    72 typedef void (*NOTIFYSHUTDOWN)();
    73 
    74 #ifdef PLATFORM_WINDOWS
    75 ConVar sm_basepath("sm_basepath", "addons\\sourcemod", 0, "SourceMod base path (set via command line)");
    76 #elif defined PLATFORM_LINUX || defined PLATFORM_APPLE
    77 ConVar sm_basepath("sm_basepath", "addons/sourcemod", 0, "SourceMod base path (set via command line)");
    78 #endif
    79 
    80 void ShutdownJIT()
    81 {
    82 	NOTIFYSHUTDOWN notify = (NOTIFYSHUTDOWN)g_pJIT->GetSymbolAddress("NotifyShutdown");
    83 	if (notify)
    84 	{
    85 		notify();
    86 	}
    87 
    88 	if (g_pSourcePawn2 != NULL)
    89 	{
    90 		g_pSourcePawn2->Shutdown();
    91 	}
    92 
    93 	g_pJIT->CloseLibrary();
    94 }
    95 
    96 SourceModBase::SourceModBase()
    97 {
    98 	m_IsMapLoading = false;
    99 	m_ExecPluginReload = false;
   100 	m_GotBasePath = false;
   101 }
   102 
   103 ConfigResult SourceModBase::OnSourceModConfigChanged(const char *key, 
   104 									  const char *value, 
   105 									  ConfigSource source, 
   106 									  char *error, 
   107 									  size_t maxlength)
   108 {
   109 	if (strcasecmp(key, "BasePath") == 0)
   110 	{
   111 		if (source == ConfigSource_Console)
   112 		{
   113 			UTIL_Format(error, maxlength, "Cannot be set at runtime");
   114 			return ConfigResult_Reject;
   115 		}
   116 
   117 		if (!m_GotBasePath)
   118 		{
   119 			g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/%s", g_BaseDir.c_str(), value);
   120 			g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), value);
   121 
   122 			m_GotBasePath = true;
   123 		}
   124 
   125 		return ConfigResult_Accept;
   126 	}
   127 	else if (strcasecmp(key, "DebugSpew") == 0)
   128 	{
   129 		sm_show_debug_spew = (strcasecmp(value, "yes") == 0) ? true : false;
   130 
   131 		return ConfigResult_Accept;
   132 	}
   133 
   134 	return ConfigResult_Ignore;
   135 }
   136 
   137 bool SourceModBase::InitializeSourceMod(char *error, size_t maxlength, bool late)
   138 {
   139 	const char *gamepath = g_SMAPI->GetBaseDir();
   140 
   141 	/* Store full path to game */
   142 	g_BaseDir.assign(gamepath);
   143 
   144 	/* Store name of game directory by itself */
   145 	size_t len = strlen(gamepath);
   146 	for (size_t i = len - 1; i >= 0; i--)
   147 	{
   148 		if (gamepath[i] == PLATFORM_SEP_CHAR)
   149 		{
   150 			strncopy(m_ModDir, &gamepath[++i], sizeof(m_ModDir));
   151 			break;
   152 		}
   153 	}
   154 
   155 	const char *basepath = icvar->GetCommandLineValue("sm_basepath");
   156 	/* Set a custom base path if there is one. */
   157 	if (basepath != NULL && basepath[0] != '\0')
   158 	{
   159 		m_GotBasePath = true;
   160 	}
   161 	/* Otherwise, use a default and keep the m_GotBasePath unlocked. */
   162 	else
   163 	{
   164 		basepath = sm_basepath.GetDefault();
   165 	}
   166 
   167 	g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/%s", g_BaseDir.c_str(), basepath);
   168 	g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), "%s", basepath);
   169 
   170 	if (!StartLogicBridge(error, maxlength))
   171 	{
   172 		return false;
   173 	}
   174 
   175 	/* Initialize CoreConfig to get the SourceMod base path properly - this parses core.cfg */
   176 	g_CoreConfig.Initialize();
   177 
   178 	/* There will always be a path by this point, since it was force-set above. */
   179 	m_GotBasePath = true;
   180 
   181 	/* Attempt to load the JIT! */
   182 	char file[PLATFORM_MAX_PATH];
   183 	char myerror[255];
   184 	g_SMAPI->PathFormat(file, sizeof(file), "%s/bin/sourcepawn.jit.x86.%s",
   185 		GetSourceModPath(),
   186 		PLATFORM_LIB_EXT
   187 		);
   188 
   189 	g_pJIT = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
   190 	if (!g_pJIT)
   191 	{
   192 		if (error && maxlength)
   193 		{
   194 			UTIL_Format(error, maxlength, "%s (failed to load bin/sourcepawn.jit.x86.%s)", 
   195 				myerror,
   196 				PLATFORM_LIB_EXT);
   197 		}
   198 		return false;
   199 	}
   200 
   201 	GET_SP_V1 getv1 = (GET_SP_V1)g_pJIT->GetSymbolAddress("GetSourcePawnEngine1");
   202 	GET_SP_V2 getv2 = (GET_SP_V2)g_pJIT->GetSymbolAddress("GetSourcePawnEngine2");
   203 
   204 	if (getv1 == NULL)
   205 	{
   206 		if (error && maxlength)
   207 		{
   208 			snprintf(error, maxlength, "JIT is too old; upgrade SourceMod");
   209 		}
   210 		ShutdownJIT();
   211 		return false;
   212 	}
   213 	else if (getv2 == NULL)
   214 	{
   215 		if (error && maxlength)
   216 		{
   217 			snprintf(error, maxlength, "JIT is too old; upgrade SourceMod");
   218 		}
   219 		ShutdownJIT();
   220 		return false;
   221 	}
   222 
   223 	g_pSourcePawn = getv1();
   224 	g_pSourcePawn2 = getv2();
   225 
   226 	if (g_pSourcePawn2->GetAPIVersion() < 3)
   227 	{
   228 		g_pSourcePawn2 = NULL;
   229 		if (error && maxlength)
   230 		{
   231 			snprintf(error, maxlength, "JIT version is out of date");
   232 		}
   233 		return false;
   234 	}
   235 
   236 	if (!g_pSourcePawn2->Initialize())
   237 	{
   238 		g_pSourcePawn2 = NULL;
   239 		if (error && maxlength)
   240 		{
   241 			snprintf(error, maxlength, "JIT could not be initialized");
   242 		}
   243 		return false;
   244 	}
   245 
   246 	g_pSourcePawn2->SetDebugListener(&g_DbgReporter);
   247 	g_pSourcePawn2->SetProfiler(&g_Profiler);
   248 
   249 	/* Hook this now so we can detect startup without calling StartSourceMod() */
   250 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
   251 
   252 	/* Only load if we're not late */
   253 	if (!late)
   254 	{
   255 		StartSourceMod(false);
   256 	}
   257 
   258 	return true;
   259 }
   260 
   261 void SourceModBase::StartSourceMod(bool late)
   262 {
   263 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
   264 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, &g_Timers, &TimerSystem::GameFrame, false);
   265 
   266 	enginePatch = SH_GET_CALLCLASS(engine);
   267 	gamedllPatch = SH_GET_CALLCLASS(gamedll);
   268 
   269 	g_ShareSys.Initialize();
   270 
   271 	/* Make the global core identity */
   272 	g_pCoreIdent = g_ShareSys.CreateCoreIdentity();
   273 
   274 	InitLogicBridge();
   275 
   276 	/* Notify! */
   277 	SMGlobalClass *pBase = SMGlobalClass::head;
   278 	while (pBase)
   279 	{
   280 		pBase->OnSourceModStartup(false);
   281 		pBase = pBase->m_pGlobalClassNext;
   282 	}
   283 
   284 	/* Notify! */
   285 	pBase = SMGlobalClass::head;
   286 	while (pBase)
   287 	{
   288 		pBase->OnSourceModAllInitialized();
   289 		pBase = pBase->m_pGlobalClassNext;
   290 	}
   291 
   292 	/* Notify! */
   293 	pBase = SMGlobalClass::head;
   294 	while (pBase)
   295 	{
   296 		pBase->OnSourceModAllInitialized_Post();
   297 		pBase = pBase->m_pGlobalClassNext;
   298 	}
   299 
   300 	/* Add us now... */
   301 	g_ShareSys.AddInterface(NULL, this);
   302 
   303 	/* We're loaded! */
   304 	g_Loaded = true;
   305 
   306 	/* Initialize VSP stuff */
   307 	if (vsp_interface != NULL)
   308 	{
   309 		g_SourceMod_Core.OnVSPListening(vsp_interface);
   310 	}
   311 
   312 	/* If we want to autoload, do that now */
   313 	const char *disabled = GetCoreConfigValue("DisableAutoUpdate");
   314 	if (disabled == NULL || strcasecmp(disabled, "yes") != 0)
   315 	{
   316 		g_Extensions.LoadAutoExtension("updater.ext." PLATFORM_LIB_EXT);
   317 	}
   318 }
   319 
   320 static bool g_LevelEndBarrier = false;
   321 bool SourceModBase::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
   322 {
   323 	/* If we're not loaded... */
   324 	if (!g_Loaded)
   325 	{
   326 		/* Do all global initialization now */
   327 		StartSourceMod(true);
   328 	}
   329 
   330 	m_IsMapLoading = true;
   331 	m_ExecPluginReload = true;
   332 
   333 	/* Notify! */
   334 	SMGlobalClass *pBase = SMGlobalClass::head;
   335 	while (pBase)
   336 	{
   337 		pBase->OnSourceModLevelChange(pMapName);
   338 		pBase = pBase->m_pGlobalClassNext;
   339 	}
   340 
   341 	DoGlobalPluginLoads();
   342 
   343 	m_IsMapLoading = false;
   344 
   345 	/* Notify! */
   346 	pBase = SMGlobalClass::head;
   347 	while (pBase)
   348 	{
   349 		pBase->OnSourceModPluginsLoaded();
   350 		pBase = pBase->m_pGlobalClassNext;
   351 	}
   352 
   353 	if (!g_pOnMapEnd)
   354 	{
   355 		g_pOnMapEnd = g_Forwards.CreateForward("OnMapEnd", ET_Ignore, 0, NULL);
   356 	}
   357 
   358 	g_LevelEndBarrier = true;
   359 
   360 	RETURN_META_VALUE(MRES_IGNORED, true);
   361 }
   362 
   363 void SourceModBase::LevelShutdown()
   364 {
   365 	if (g_LevelEndBarrier)
   366 	{
   367 		SMGlobalClass *next = SMGlobalClass::head;
   368 		while (next)
   369 		{
   370 			next->OnSourceModLevelEnd();
   371 			next = next->m_pGlobalClassNext;
   372 		}
   373 		
   374 		if (g_pOnMapEnd != NULL)
   375 		{
   376 			g_pOnMapEnd->Execute(NULL);
   377 		}
   378 
   379 		g_Timers.RemoveMapChangeTimers();
   380 
   381 		g_LevelEndBarrier = false;
   382 	}
   383 
   384 	g_OnMapStarted = false;
   385 
   386 	if (m_ExecPluginReload)
   387 	{
   388 		g_PluginSys.ReloadOrUnloadPlugins();
   389 		m_ExecPluginReload = false;
   390 	}
   391 }
   392 
   393 bool SourceModBase::IsMapLoading() const
   394 {
   395 	return m_IsMapLoading;
   396 }
   397 
   398 void SourceModBase::DoGlobalPluginLoads()
   399 {
   400 	char config_path[PLATFORM_MAX_PATH];
   401 	char plugins_path[PLATFORM_MAX_PATH];
   402 
   403 	BuildPath(Path_SM, config_path, 
   404 		sizeof(config_path),
   405 		"configs/plugin_settings.cfg");
   406 	BuildPath(Path_SM, plugins_path,
   407 		sizeof(plugins_path),
   408 		"plugins");
   409 
   410 	/* Load any auto extensions */
   411 	g_Extensions.TryAutoload();
   412 
   413 	/* Fire the extensions ready message */
   414 	g_SMAPI->MetaFactory(SOURCEMOD_NOTICE_EXTENSIONS, NULL, NULL);
   415 
   416 	/* Load any game extension */
   417 	const char *game_ext;
   418 	if ((game_ext = g_pGameConf->GetKeyValue("GameExtension")) != NULL)
   419 	{
   420 		char path[PLATFORM_MAX_PATH];
   421 		UTIL_Format(path, sizeof(path), "%s.ext." PLATFORM_LIB_EXT, game_ext);
   422 		g_Extensions.LoadAutoExtension(path);
   423 	}
   424 
   425 	/* Run the first pass */
   426 	g_PluginSys.LoadAll_FirstPass(config_path, plugins_path);
   427 
   428 	/* Mark any extensions as loaded */
   429 	g_Extensions.MarkAllLoaded();
   430 
   431 	/* No modules yet, it's safe to call this from here */
   432 	g_PluginSys.LoadAll_SecondPass();
   433 
   434 	/* Re-mark any extensions as loaded */
   435 	g_Extensions.MarkAllLoaded();
   436 
   437 	/* Call OnAllPluginsLoaded */
   438 	g_PluginSys.AllPluginsLoaded();
   439 }
   440 
   441 size_t SourceModBase::BuildPath(PathType type, char *buffer, size_t maxlength, const char *format, ...)
   442 {
   443 	char _buffer[PLATFORM_MAX_PATH];
   444 	va_list ap;
   445 
   446 	va_start(ap, format);
   447 	vsnprintf(_buffer, PLATFORM_MAX_PATH, format, ap);
   448 	va_end(ap);
   449 
   450 	/* If we get a "file://" notation, strip off the file:// part so we're left 
   451 	 * with an absolute path.  Note that the absolute path gets returned, so 
   452 	 * usage with relative paths here is completely invalid.
   453 	 */
   454 	if (type != Path_SM_Rel && strncmp(_buffer, "file://", 7) == 0)
   455 	{
   456 		return g_LibSys.PathFormat(buffer, maxlength, "%s", &_buffer[7]);
   457 	}
   458 
   459 	const char *base = NULL;
   460 	if (type == Path_Game)
   461 	{
   462 		base = GetGamePath();
   463 	}
   464 	else if (type == Path_SM)
   465 	{
   466 		base = GetSourceModPath();
   467 	}
   468 	else if (type == Path_SM_Rel)
   469 	{
   470 		base = m_SMRelDir;
   471 	}
   472 
   473 	if (base)
   474 	{
   475 		return g_LibSys.PathFormat(buffer, maxlength, "%s/%s", base, _buffer);
   476 	}
   477 	else
   478 	{
   479 		return g_LibSys.PathFormat(buffer, maxlength, "%s", _buffer);
   480 	}
   481 }
   482 
   483 void SourceModBase::CloseSourceMod()
   484 {
   485 	/* Force a level end */
   486 	LevelShutdown();
   487 
   488 	/* Unload plugins */
   489 	g_PluginSys.Shutdown();
   490 
   491 	/* Unload extensions */
   492 	g_Extensions.Shutdown();
   493 
   494 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
   495 
   496 	if (g_Loaded)
   497 	{
   498 		if (g_pOnMapEnd)
   499 		{
   500 			g_Forwards.ReleaseForward(g_pOnMapEnd);
   501 		}
   502 	
   503 		/* Notify! */
   504 		SMGlobalClass *pBase = SMGlobalClass::head;
   505 		while (pBase)
   506 		{
   507 			pBase->OnSourceModShutdown();
   508 			pBase = pBase->m_pGlobalClassNext;
   509 		}
   510 	
   511 		/* Delete all data packs */
   512 		CStack<CDataPack *>::iterator iter;
   513 		CDataPack *pd;
   514 		for (iter=m_freepacks.begin(); iter!=m_freepacks.end(); iter++)
   515 		{
   516 			pd = (*iter);
   517 			delete pd;
   518 		}
   519 		m_freepacks.popall();
   520 	
   521 		/* Notify! */
   522 		pBase = SMGlobalClass::head;
   523 		while (pBase)
   524 		{
   525 			pBase->OnSourceModAllShutdown();
   526 			pBase = pBase->m_pGlobalClassNext;
   527 		}
   528 	
   529 		if (enginePatch)
   530 		{
   531 			SH_RELEASE_CALLCLASS(enginePatch);
   532 			enginePatch = NULL;
   533 		}
   534 	
   535 		if (gamedllPatch)
   536 		{
   537 			SH_RELEASE_CALLCLASS(gamedllPatch);
   538 			gamedllPatch = NULL;
   539 		}
   540 
   541 		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
   542 		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, &g_Timers, &TimerSystem::GameFrame, false);
   543 	}
   544 
   545 	/* Rest In Peace */
   546 	ShutdownLogicBridge();
   547 	ShutdownJIT();
   548 }
   549 
   550 void SourceModBase::LogMessage(IExtension *pExt, const char *format, ...)
   551 {
   552 	IExtensionInterface *pAPI = pExt->GetAPI();
   553 	const char *tag = pAPI->GetExtensionTag();
   554 	char buffer[2048];
   555 	va_list ap;
   556 
   557 	va_start(ap, format);
   558 	vsnprintf(buffer, sizeof(buffer), format, ap);
   559 	va_end(ap);
   560 
   561 	if (tag)
   562 	{
   563 		g_Logger.LogMessage("[%s] %s", tag, buffer);
   564 	} else {
   565 		g_Logger.LogMessage("%s", buffer);
   566 	}
   567 }
   568 
   569 void SourceModBase::LogError(IExtension *pExt, const char *format, ...)
   570 {
   571 	IExtensionInterface *pAPI = pExt->GetAPI();
   572 	const char *tag = pAPI->GetExtensionTag();
   573 	char buffer[2048];
   574 	va_list ap;
   575 
   576 	va_start(ap, format);
   577 	vsnprintf(buffer, sizeof(buffer), format, ap);
   578 	va_end(ap);
   579 
   580 	if (tag)
   581 	{
   582 		g_Logger.LogError("[%s] %s", tag, buffer);
   583 	} else {
   584 		g_Logger.LogError("%s", buffer);
   585 	}
   586 }
   587 
   588 size_t SourceModBase::FormatString(char *buffer, size_t maxlength, IPluginContext *pContext, const cell_t *params, unsigned int param)
   589 {
   590 	char *fmt;
   591 
   592 	pContext->LocalToString(params[param], &fmt);
   593 
   594 	int lparam = ++param;
   595 
   596 	return atcprintf(buffer, maxlength, fmt, pContext, params, &lparam);
   597 }
   598 
   599 const char *SourceModBase::GetSourceModPath() const
   600 {
   601 	return m_SMBaseDir;
   602 }
   603 
   604 const char *SourceModBase::GetGamePath() const
   605 {
   606 	return g_BaseDir.c_str();
   607 }
   608 
   609 unsigned int SourceModBase::SetGlobalTarget(unsigned int index)
   610 {
   611 	unsigned int old = m_target;
   612 	m_target = index;
   613 	return old;
   614 }
   615 
   616 unsigned int SourceModBase::GetGlobalTarget() const
   617 {
   618 	return m_target;
   619 }
   620 
   621 IDataPack *SourceModBase::CreateDataPack()
   622 {
   623 	CDataPack *pack;
   624 	if (m_freepacks.empty())
   625 	{
   626 		pack = new CDataPack;
   627 	} else {
   628 		pack = m_freepacks.front();
   629 		m_freepacks.pop();
   630 		pack->Initialize();
   631 	}
   632 	return pack;
   633 }
   634 
   635 void SourceModBase::FreeDataPack(IDataPack *pack)
   636 {
   637 	m_freepacks.push(static_cast<CDataPack *>(pack));
   638 }
   639 
   640 Handle_t SourceModBase::GetDataPackHandleType(bool readonly)
   641 {
   642 	//:TODO:
   643 	return 0;
   644 }
   645 
   646 const char *SourceModBase::GetGameFolderName() const
   647 {
   648 	return m_ModDir;
   649 }
   650 
   651 ISourcePawnEngine *SourceModBase::GetScriptingEngine()
   652 {
   653 	return g_pSourcePawn;
   654 }
   655 
   656 IVirtualMachine *SourceModBase::GetScriptingVM()
   657 {
   658 	return NULL;
   659 }
   660 
   661 void SourceModBase::AllPluginsLoaded()
   662 {
   663 	SMGlobalClass *base = SMGlobalClass::head;
   664 	while (base)
   665 	{
   666 		base->OnSourceModGameInitialized();
   667 		base = base->m_pGlobalClassNext;
   668 	}
   669 }
   670 
   671 time_t SourceModBase::GetAdjustedTime()
   672 {
   673 	return ::GetAdjustedTime();
   674 }
   675 
   676 void SourceModBase::AddGameFrameHook(GAME_FRAME_HOOK hook)
   677 {
   678 	m_frame_hooks.push_back(hook);
   679 }
   680 
   681 void SourceModBase::RemoveGameFrameHook(GAME_FRAME_HOOK hook)
   682 {
   683 	for (size_t i = 0; i < m_frame_hooks.size(); i++)
   684 	{
   685 		if (m_frame_hooks[i] == hook)
   686 		{
   687 			m_frame_hooks.erase(m_frame_hooks.iterAt(i));
   688 			return;
   689 		}
   690 	}
   691 }
   692 
   693 void SourceModBase::ProcessGameFrameHooks(bool simulating)
   694 {
   695 	if (m_frame_hooks.size() == 0)
   696 	{
   697 		return;
   698 	}
   699 
   700 	for (size_t i = 0; i < m_frame_hooks.size(); i++)
   701 	{
   702 		m_frame_hooks[i](simulating);
   703 	}
   704 }
   705 
   706 size_t SourceModBase::Format(char *buffer, size_t maxlength, const char *fmt, ...)
   707 {
   708 	size_t len;
   709 	va_list ap;
   710 
   711 	va_start(ap, fmt);
   712 	len = FormatArgs(buffer, maxlength, fmt, ap);
   713 	va_end(ap);
   714 
   715 	return len;
   716 }
   717 
   718 size_t SourceModBase::FormatArgs(char *buffer,
   719 								 size_t maxlength,
   720 								 const char *fmt,
   721 								 va_list ap)
   722 {
   723 	return UTIL_FormatArgs(buffer, maxlength, fmt, ap);
   724 }
   725 
   726 void SourceModBase::AddFrameAction(FRAMEACTION fn, void *data)
   727 {
   728 	::AddFrameAction(FrameAction(fn, data));
   729 }
   730 
   731 const char *SourceModBase::GetCoreConfigValue(const char *key)
   732 {
   733 	return g_CoreConfig.GetCoreConfigValue(key);
   734 }
   735 
   736 int SourceModBase::GetPluginId()
   737 {
   738 	return g_PLID;
   739 }
   740 
   741 SMGlobalClass *SMGlobalClass::head = NULL;
   742