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