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