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