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