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