core/sourcemod.cpp
author David Anderson <dvander@alliedmods.net>
Wed Aug 01 02:12:47 2007 +0000 (2007-08-01)
changeset 1231 17badaf66ce4
parent 1221 f2dc55c0ab6f
child 1330 ab94316bb52b
permissions -rw-r--r--
updated more header files (mostly internal now)
     1 /**
     2  * vim: set ts=4 :
     3  * ================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * ================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or 
     9  * modify it under the terms of the GNU General Public License, 
    10  * version 3.0, as published by the Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful,
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  * GNU General Public License for more details.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to 
    21  * link the code of this program (as well as its derivative works) to 
    22  * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any 
    23  * Game MODs that run on software by the Valve Corporation.  You must 
    24  * obey the GNU General Public License in all respects for all other 
    25  * code used. Additionally, AlliedModders LLC grants this exception 
    26  * to all derivative works. AlliedModders LLC defines further 
    27  * exceptions, found in LICENSE.txt (as of this writing, version 
    28  * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #include <stdio.h>
    34 #include "sourcemod.h"
    35 #include "sourcemm_api.h"
    36 #include "systems/LibrarySys.h"
    37 #include "vm/sp_vm_engine.h"
    38 #include <sh_string.h>
    39 #include "PluginSys.h"
    40 #include "ShareSys.h"
    41 #include "CoreConfig.h"
    42 #include "Logger.h"
    43 #include "ExtensionSys.h"
    44 #include "AdminCache.h"
    45 #include "sm_stringutil.h"
    46 #include "PlayerManager.h"
    47 #include "Translator.h"
    48 #include "ForwardSys.h"
    49 #include "TimerSys.h"
    50 #include "MenuStyle_Valve.h"
    51 #include "MenuStyle_Radio.h"
    52 #include "Database.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 SourcePawnEngine g_SourcePawn;
    60 SourceModBase g_SourceMod;
    61 
    62 ILibrary *g_pJIT = NULL;
    63 SourceHook::String g_BaseDir;
    64 ISourcePawnEngine *g_pSourcePawn = &g_SourcePawn;
    65 IVirtualMachine *g_pVM;
    66 IdentityToken_t *g_pCoreIdent = NULL;
    67 float g_LastTime = 0.0f;
    68 float g_LastMenuTime = 0.0f;
    69 float g_LastAuthCheck = 0.0f;
    70 IForward *g_pOnGameFrame = NULL;
    71 IForward *g_pOnMapEnd = NULL;
    72 bool g_Loaded = false;
    73 int g_StillFrames = 0;
    74 float g_StillTime = 0.0f;
    75 
    76 typedef int (*GIVEENGINEPOINTER)(ISourcePawnEngine *);
    77 typedef int (*GIVEENGINEPOINTER2)(ISourcePawnEngine *, unsigned int api_version);
    78 typedef unsigned int (*GETEXPORTCOUNT)();
    79 typedef IVirtualMachine *(*GETEXPORT)(unsigned int);
    80 typedef void (*NOTIFYSHUTDOWN)();
    81 
    82 void ShutdownJIT()
    83 {
    84 	NOTIFYSHUTDOWN notify = (NOTIFYSHUTDOWN)g_pJIT->GetSymbolAddress("NotifyShutdown");
    85 	if (notify)
    86 	{
    87 		notify();
    88 	}
    89 
    90 	g_pJIT->CloseLibrary();
    91 }
    92 
    93 SourceModBase::SourceModBase()
    94 {
    95 	m_IsMapLoading = false;
    96 	m_ExecPluginReload = false;
    97 	m_ExecOnMapEnd = 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 
   126 	return ConfigResult_Ignore;
   127 }
   128 
   129 bool SourceModBase::InitializeSourceMod(char *error, size_t maxlength, bool late)
   130 {
   131 	const char *gamepath = g_SMAPI->GetBaseDir();
   132 
   133 	/* Store full path to game */
   134 	g_BaseDir.assign(gamepath);
   135 
   136 	/* Store name of game directory by itself */
   137 	size_t len = strlen(gamepath);
   138 	for (size_t i = len - 1; i >= 0; i--)
   139 	{
   140 		if (gamepath[i] == PLATFORM_SEP_CHAR)
   141 		{
   142 			strncopy(m_ModDir, &gamepath[++i], sizeof(m_ModDir));
   143 			break;
   144 		}
   145 	}
   146 
   147 	/* Initialize CoreConfig so we can get SourceMod base path properly - this basically parses core.cfg */
   148 	g_CoreConfig.Initialize();
   149 
   150 	/* This shouldn't happen, but can't hurt to be safe */
   151 	if (!g_LibSys.PathExists(m_SMBaseDir) || !m_GotBasePath)
   152 	{
   153 		g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/addons/sourcemod", g_BaseDir.c_str());
   154 		g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), "addons/sourcemod");
   155 		m_GotBasePath = true;
   156 	}
   157 
   158 	/* Attempt to load the JIT! */
   159 	char file[PLATFORM_MAX_PATH];
   160 	char myerror[255];
   161 	g_SMAPI->PathFormat(file, sizeof(file), "%s/bin/sourcepawn.jit.x86.%s",
   162 		GetSourceModPath(),
   163 		PLATFORM_LIB_EXT
   164 		);
   165 
   166 	g_pJIT = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
   167 	if (!g_pJIT)
   168 	{
   169 		if (error && maxlength)
   170 		{
   171 			snprintf(error, maxlength, "%s (failed to load bin/sourcepawn.jit.x86.%s)", 
   172 				myerror,
   173 				PLATFORM_LIB_EXT);
   174 		}
   175 		return false;
   176 	}
   177 
   178 	int err;
   179 	
   180 	GIVEENGINEPOINTER2 jit_init2 = (GIVEENGINEPOINTER2)g_pJIT->GetSymbolAddress("GiveEnginePointer2");
   181 	if (!jit_init2)
   182 	{
   183 		GIVEENGINEPOINTER jit_init = (GIVEENGINEPOINTER)g_pJIT->GetSymbolAddress("GiveEnginePointer");
   184 		if (!jit_init)
   185 		{
   186 			ShutdownJIT();
   187 			if (error && maxlength)
   188 			{
   189 				snprintf(error, maxlength, "Failed to find GiveEnginePointer in JIT!");
   190 			}
   191 			return false;
   192 		}
   193 
   194 		if ((err=jit_init(g_pSourcePawn)) != 0)
   195 		{
   196 			ShutdownJIT();
   197 			if (error && maxlength)
   198 			{
   199 				snprintf(error, maxlength, "GiveEnginePointer returned %d in the JIT", err);
   200 			}
   201 			return false;
   202 		}
   203 	} else {
   204 		/* On version bumps, we should check for older versions as well, if the new version fails.
   205 		 * We can then check the exports to see if any VM versions will be sufficient.
   206 		 */
   207 		if ((err=jit_init2(g_pSourcePawn, SOURCEPAWN_ENGINE_API_VERSION)) != SP_ERROR_NONE)
   208 		{
   209 			ShutdownJIT();
   210 			if (error && maxlength)
   211 			{
   212 				snprintf(error, maxlength, "JIT incompatible with SourceMod version");
   213 			}
   214 			return false;
   215 		}
   216 	}
   217 
   218 	GETEXPORTCOUNT jit_getnum = (GETEXPORTCOUNT)g_pJIT->GetSymbolAddress("GetExportCount");
   219 	GETEXPORT jit_get = (GETEXPORT)g_pJIT->GetSymbolAddress("GetExport");
   220 	if (!jit_get)
   221 	{
   222 		ShutdownJIT();
   223 		if (error && maxlength)
   224 		{
   225 			snprintf(error, maxlength, "JIT is missing a necessary export!");
   226 		}
   227 		return false;
   228 	}
   229 
   230 	unsigned int num = jit_getnum();
   231 	if (!num)
   232 	{
   233 		ShutdownJIT();
   234 		if (error && maxlength)
   235 		{
   236 			snprintf(error, maxlength, "JIT did not export any virtual machines!");
   237 		}
   238 		return false;
   239 	}
   240 
   241 	unsigned int api_version;
   242 	for (unsigned int i=0; i<num; i++)
   243 	{
   244 		 if ((g_pVM=jit_get(i)) == NULL)
   245 		 {
   246 			 if (error && maxlength)
   247 			 {
   248 				 snprintf(error, maxlength, "JIT did not export any virtual machines!");
   249 			 }
   250 			 continue;
   251 		 }
   252 		/* Refuse any API that we might not be able to deal with.
   253 		 * Also refuse anything < 3 because we need fake natives.
   254 		 */
   255 		 api_version = g_pVM->GetAPIVersion();
   256 		 if (api_version < 3 || api_version > SOURCEPAWN_VM_API_VERSION)
   257 		 {
   258 			 if (error && maxlength)
   259 			 {
   260 				 snprintf(error, maxlength, "JIT is not a compatible version");
   261 			 }
   262 			 g_pVM = NULL;
   263 			 continue;
   264 		 }
   265 		 break;
   266 	}
   267 
   268 	if (!g_pVM)
   269 	{
   270 		ShutdownJIT();
   271 		return false;
   272 	}
   273 
   274 	/* Hook this now so we can detect startup without calling StartSourceMod() */
   275 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
   276 
   277 	/* Only load if we're not late */
   278 	if (!late)
   279 	{
   280 		StartSourceMod(false);
   281 	}
   282 
   283 	return true;
   284 }
   285 
   286 void SourceModBase::StartSourceMod(bool late)
   287 {
   288 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
   289 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, this, &SourceModBase::GameFrame, false);
   290 
   291 	enginePatch = SH_GET_CALLCLASS(engine);
   292 	gamedllPatch = SH_GET_CALLCLASS(gamedll);
   293 
   294 	/* Notify! */
   295 	SMGlobalClass *pBase = SMGlobalClass::head;
   296 	while (pBase)
   297 	{
   298 		pBase->OnSourceModStartup(false);
   299 		pBase = pBase->m_pGlobalClassNext;
   300 	}
   301 
   302 	/* Make the global core identity */
   303 	g_pCoreIdent = g_ShareSys.CreateCoreIdentity();
   304 
   305 	/* Notify! */
   306 	pBase = SMGlobalClass::head;
   307 	while (pBase)
   308 	{
   309 		pBase->OnSourceModAllInitialized();
   310 		pBase = pBase->m_pGlobalClassNext;
   311 	}
   312 
   313 	/* Add us now... */
   314 	g_ShareSys.AddInterface(NULL, this);
   315 
   316 	/* We're loaded! */
   317 	g_Loaded = true;
   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 	m_ExecOnMapEnd = true;
   333 	g_LastTime = 0.0f;
   334 	g_LastMenuTime = 0.0f;
   335 	g_LastAuthCheck = 0.0f;
   336 	g_SimTicks.ticking = true;
   337 	g_SimTicks.tickcount = 0;
   338 	g_StillTime = 0.0f;
   339 	g_StillFrames = 0;
   340 
   341 	/* Notify! */
   342 	SMGlobalClass *pBase = SMGlobalClass::head;
   343 	while (pBase)
   344 	{
   345 		pBase->OnSourceModLevelChange(pMapName);
   346 		pBase = pBase->m_pGlobalClassNext;
   347 	}
   348 
   349 	DoGlobalPluginLoads();
   350 
   351 	m_IsMapLoading = false;
   352 
   353 	/* Notify! */
   354 	pBase = SMGlobalClass::head;
   355 	while (pBase)
   356 	{
   357 		pBase->OnSourceModPluginsLoaded();
   358 		pBase = pBase->m_pGlobalClassNext;
   359 	}
   360 
   361 	if (!g_pOnGameFrame)
   362 	{
   363 		g_pOnGameFrame = g_Forwards.CreateForward("OnGameFrame", ET_Ignore, 0, NULL);
   364 	}
   365 
   366 	if (!g_pOnMapEnd)
   367 	{
   368 		g_pOnMapEnd = g_Forwards.CreateForward("OnMapEnd", ET_Ignore, 0, NULL);
   369 	}
   370 
   371 	g_LevelEndBarrier = true;
   372 
   373 	RETURN_META_VALUE(MRES_IGNORED, true);
   374 }
   375 
   376 void StartTickSimulation()
   377 {
   378 	g_SimTicks.ticking = false;
   379 	g_SimTicks.tickcount = 0;
   380 	g_SimTicks.ticktime = gpGlobals->curtime;
   381 }
   382 
   383 void StopTickSimulation()
   384 {
   385 	g_SimTicks.ticking = true;
   386 	g_Timers.MapChange(false);
   387 	g_StillFrames = 0;
   388 	g_LastTime = gpGlobals->curtime;
   389 }
   390 
   391 void SimulateTick()
   392 {
   393 	g_SimTicks.tickcount++;
   394 	g_SimTicks.ticktime = g_StillTime + (g_SimTicks.tickcount * gpGlobals->interval_per_tick);
   395 }
   396 
   397 void SourceModBase::GameFrame(bool simulating)
   398 {
   399 	g_DBMan.RunFrame();
   400 
   401 	/**
   402 	 * Note: This is all hardcoded rather than delegated to save
   403 	 * precious CPU cycles.
   404 	 */
   405 	float curtime = gpGlobals->curtime;
   406 	int framecount = gpGlobals->framecount;
   407 
   408 	/* Verify that we're still ticking */
   409 	if (g_SimTicks.ticking)
   410 	{
   411 		if (g_StillFrames == 0)
   412 		{
   413 			g_StillFrames = framecount;
   414 			g_StillTime = curtime;
   415 		} else {
   416 			/* Try to detect when we've stopped ticking.
   417 			 * We do this once 10 frames pass and there have been no ticks.
   418 			 */
   419 			if (g_StillTime == curtime)
   420 			{
   421 				if (framecount - g_StillFrames >= 5)
   422 				{
   423 					StartTickSimulation();
   424 					return;
   425 				}
   426 			} else {
   427 				/* We're definitely ticking we get here, 
   428 				 * but update everything as a precaution */
   429 				g_StillFrames = framecount;
   430 				g_StillTime = curtime;
   431 			}
   432 		}
   433 	} else {
   434 		/* We need to make sure we should still be simulating. */
   435 		if (g_StillTime != curtime)
   436 		{
   437 			/* Wow, we're ticking again! It's time to revert. */
   438 			StopTickSimulation();
   439 			return;
   440 		}
   441 		/* Nope, not ticking.  Simulate! */
   442 		SimulateTick();
   443 		curtime = g_SimTicks.ticktime;
   444 	}
   445 
   446 	if (curtime - g_LastTime >= 0.1f)
   447 	{
   448 		if (m_CheckingAuth
   449 			&& (gpGlobals->curtime - g_LastAuthCheck > 0.7f))
   450 		{
   451 			g_LastAuthCheck = gpGlobals->curtime;
   452 			g_Players.RunAuthChecks();
   453 		}
   454 
   455 		g_Timers.RunFrame();
   456 		g_LastTime = curtime;
   457 	}
   458 
   459 	if (g_SimTicks.ticking && curtime - g_LastMenuTime >= 1.0f)
   460 	{
   461 		g_ValveMenuStyle.ProcessWatchList();
   462 		g_RadioMenuStyle.ProcessWatchList();
   463 		g_LastMenuTime = curtime;
   464 	}
   465 
   466 	if (g_pOnGameFrame && g_pOnGameFrame->GetFunctionCount())
   467 	{
   468 		g_pOnGameFrame->Execute(NULL);
   469 	}
   470 }
   471 
   472 void SourceModBase::LevelShutdown()
   473 {
   474 	if (g_LevelEndBarrier)
   475 	{
   476 		SMGlobalClass *next = SMGlobalClass::head;
   477 		while (next)
   478 		{
   479 			next->OnSourceModLevelEnd();
   480 			next = next->m_pGlobalClassNext;
   481 		}
   482 		g_LevelEndBarrier = false;
   483 	}
   484 
   485 	if (g_pOnMapEnd && m_ExecOnMapEnd)
   486 	{
   487 		g_pOnMapEnd->Execute(NULL);
   488 		m_ExecOnMapEnd = false;
   489 	}
   490 
   491 	g_OnMapStarted = false;
   492 
   493 	if (m_ExecPluginReload)
   494 	{
   495 		g_PluginSys.ReloadOrUnloadPlugins();
   496 		m_ExecPluginReload = false;
   497 	}
   498 }
   499 
   500 bool SourceModBase::IsMapLoading() const
   501 {
   502 	return m_IsMapLoading;
   503 }
   504 
   505 void SourceModBase::DoGlobalPluginLoads()
   506 {
   507 	char config_path[PLATFORM_MAX_PATH];
   508 	char plugins_path[PLATFORM_MAX_PATH];
   509 
   510 	BuildPath(Path_SM, config_path, 
   511 		sizeof(config_path),
   512 		"configs/plugin_settings.cfg");
   513 	BuildPath(Path_SM, plugins_path,
   514 		sizeof(plugins_path),
   515 		"plugins");
   516 
   517 	/* Load any auto extensions */
   518 	g_Extensions.TryAutoload();
   519 
   520 	/* Run the first pass */
   521 	g_PluginSys.LoadAll_FirstPass(config_path, plugins_path);
   522 
   523 	/* Mark any extensions as loaded */
   524 	g_Extensions.MarkAllLoaded();
   525 
   526 	/* No modules yet, it's safe to call this from here */
   527 	g_PluginSys.LoadAll_SecondPass();
   528 
   529 	/* Re-mark any extensions as loaded */
   530 	g_Extensions.MarkAllLoaded();
   531 
   532 	/* Call OnAllPluginsLoaded */
   533 	g_PluginSys.AllPluginsLoaded();
   534 }
   535 
   536 size_t SourceModBase::BuildPath(PathType type, char *buffer, size_t maxlength, char *format, ...)
   537 {
   538 	char _buffer[PLATFORM_MAX_PATH];
   539 	va_list ap;
   540 
   541 	va_start(ap, format);
   542 	vsnprintf(_buffer, PLATFORM_MAX_PATH, format, ap);
   543 	va_end(ap);
   544 
   545 	const char *base = NULL;
   546 	if (type == Path_Game)
   547 	{
   548 		base = GetGamePath();
   549 	} else if (type == Path_SM) {
   550 		base = GetSourceModPath();
   551 	} else if (type == Path_SM_Rel) {
   552 		base = m_SMRelDir;
   553 	}
   554 
   555 	if (base)
   556 	{
   557 		return g_LibSys.PathFormat(buffer, maxlength, "%s/%s", base, _buffer);
   558 	} else {
   559 		return g_LibSys.PathFormat(buffer, maxlength, "%s", _buffer);
   560 	}
   561 }
   562 
   563 void SourceModBase::CloseSourceMod()
   564 {
   565 	/* Force a level end */
   566 	LevelShutdown();
   567 
   568 	/* Unload plugins */
   569 	g_PluginSys.Shutdown();
   570 
   571 	/* Unload extensions */
   572 	g_Extensions.Shutdown();
   573 
   574 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
   575 
   576 	if (g_Loaded)
   577 	{
   578 		if (g_pOnGameFrame)
   579 		{
   580 			g_Forwards.ReleaseForward(g_pOnGameFrame);
   581 		}
   582 
   583 		if (g_pOnMapEnd)
   584 		{
   585 			g_Forwards.ReleaseForward(g_pOnMapEnd);
   586 		}
   587 	
   588 		/* Notify! */
   589 		SMGlobalClass *pBase = SMGlobalClass::head;
   590 		while (pBase)
   591 		{
   592 			pBase->OnSourceModShutdown();
   593 			pBase = pBase->m_pGlobalClassNext;
   594 		}
   595 	
   596 		/* Delete all data packs */
   597 		CStack<CDataPack *>::iterator iter;
   598 		CDataPack *pd;
   599 		for (iter=m_freepacks.begin(); iter!=m_freepacks.end(); iter++)
   600 		{
   601 			pd = (*iter);
   602 			delete pd;
   603 		}
   604 		m_freepacks.popall();
   605 	
   606 		/* Notify! */
   607 		pBase = SMGlobalClass::head;
   608 		while (pBase)
   609 		{
   610 			pBase->OnSourceModAllShutdown();
   611 			pBase = pBase->m_pGlobalClassNext;
   612 		}
   613 	
   614 		if (enginePatch)
   615 		{
   616 			SH_RELEASE_CALLCLASS(enginePatch);
   617 			enginePatch = NULL;
   618 		}
   619 	
   620 		if (gamedllPatch)
   621 		{
   622 			SH_RELEASE_CALLCLASS(gamedllPatch);
   623 			gamedllPatch = NULL;
   624 		}
   625 
   626 		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
   627 		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, this, &SourceModBase::GameFrame, false);
   628 	}
   629 
   630 	/* Rest In Peace */
   631 	ShutdownJIT();
   632 }
   633 
   634 void SourceModBase::LogMessage(IExtension *pExt, const char *format, ...)
   635 {
   636 	IExtensionInterface *pAPI = pExt->GetAPI();
   637 	const char *tag = pAPI->GetExtensionTag();
   638 	char buffer[2048];
   639 	va_list ap;
   640 
   641 	va_start(ap, format);
   642 	vsnprintf(buffer, sizeof(buffer), format, ap);
   643 	va_end(ap);
   644 
   645 	if (tag)
   646 	{
   647 		g_Logger.LogMessage("[%s] %s", tag, buffer);
   648 	} else {
   649 		g_Logger.LogMessage("%s", buffer);
   650 	}
   651 }
   652 
   653 void SourceModBase::LogError(IExtension *pExt, const char *format, ...)
   654 {
   655 	IExtensionInterface *pAPI = pExt->GetAPI();
   656 	const char *tag = pAPI->GetExtensionTag();
   657 	char buffer[2048];
   658 	va_list ap;
   659 
   660 	va_start(ap, format);
   661 	vsnprintf(buffer, sizeof(buffer), format, ap);
   662 	va_end(ap);
   663 
   664 	if (tag)
   665 	{
   666 		g_Logger.LogError("[%s] %s", tag, buffer);
   667 	} else {
   668 		g_Logger.LogError("%s", buffer);
   669 	}
   670 }
   671 
   672 size_t SourceModBase::FormatString(char *buffer, size_t maxlength, IPluginContext *pContext, const cell_t *params, unsigned int param)
   673 {
   674 	char *fmt;
   675 
   676 	pContext->LocalToString(params[param], &fmt);
   677 
   678 	int lparam = ++param;
   679 
   680 	return atcprintf(buffer, maxlength, fmt, pContext, params, &lparam);
   681 }
   682 
   683 const char *SourceModBase::GetSourceModPath() const
   684 {
   685 	return m_SMBaseDir;
   686 }
   687 
   688 const char *SourceModBase::GetGamePath() const
   689 {
   690 	return g_BaseDir.c_str();
   691 }
   692 
   693 void SourceModBase::SetGlobalTarget(unsigned int index)
   694 {
   695 	m_target = index;
   696 }
   697 
   698 void SourceModBase::SetAuthChecking(bool set)
   699 {
   700 	m_CheckingAuth = set;
   701 }
   702 
   703 unsigned int SourceModBase::GetGlobalTarget() const
   704 {
   705 	return m_target;
   706 }
   707 
   708 IDataPack *SourceModBase::CreateDataPack()
   709 {
   710 	CDataPack *pack;
   711 	if (m_freepacks.empty())
   712 	{
   713 		pack = new CDataPack;
   714 	} else {
   715 		pack = m_freepacks.front();
   716 		m_freepacks.pop();
   717 		pack->Initialize();
   718 	}
   719 	return pack;
   720 }
   721 
   722 void SourceModBase::FreeDataPack(IDataPack *pack)
   723 {
   724 	m_freepacks.push(static_cast<CDataPack *>(pack));
   725 }
   726 
   727 Handle_t SourceModBase::GetDataPackHandleType(bool readonly)
   728 {
   729 	//:TODO:
   730 	return 0;
   731 }
   732 
   733 const char *SourceModBase::GetGameFolderName() const
   734 {
   735 	return m_ModDir;
   736 }
   737 
   738 ISourcePawnEngine *SourceModBase::GetScriptingEngine()
   739 {
   740 	return g_pSourcePawn;
   741 }
   742 
   743 IVirtualMachine *SourceModBase::GetScriptingVM()
   744 {
   745 	return g_pVM;
   746 }
   747 
   748 void SourceModBase::AllPluginsLoaded()
   749 {
   750 	SMGlobalClass *base = SMGlobalClass::head;
   751 	while (base)
   752 	{
   753 		base->OnSourceModGameInitialized();
   754 		base = base->m_pGlobalClassNext;
   755 	}
   756 }
   757 
   758 SMGlobalClass *SMGlobalClass::head = NULL;
   759 
   760 SMGlobalClass::SMGlobalClass()
   761 {
   762 	m_pGlobalClassNext = SMGlobalClass::head;
   763 	SMGlobalClass::head = this;
   764 }