core/sourcemod.cpp
author David Anderson <dvander@alliedmods.net>
Sun Aug 26 17:41:02 2007 +0000 (2007-08-26)
changeset 1369 bd9ccced1b1a
parent 1338 1ba17552b7b1
child 1371 d207f0e17532
permissions -rw-r--r--
fixed amb834 - command callbacks could potentially occur on invalid clients in very rare cases
     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 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 "systems/LibrarySys.h"
    36 #include "vm/sp_vm_engine.h"
    37 #include <sh_string.h>
    38 #include "PluginSys.h"
    39 #include "ShareSys.h"
    40 #include "CoreConfig.h"
    41 #include "Logger.h"
    42 #include "ExtensionSys.h"
    43 #include "AdminCache.h"
    44 #include "sm_stringutil.h"
    45 #include "PlayerManager.h"
    46 #include "Translator.h"
    47 #include "ForwardSys.h"
    48 #include "TimerSys.h"
    49 #include "MenuStyle_Valve.h"
    50 #include "MenuStyle_Radio.h"
    51 #include "Database.h"
    52 #include "HalfLife2.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 	g_HL2.ProcessFakeCliCmdQueue();
   401 
   402 	/**
   403 	 * Note: This is all hardcoded rather than delegated to save
   404 	 * precious CPU cycles.
   405 	 */
   406 	float curtime = gpGlobals->curtime;
   407 	int framecount = gpGlobals->framecount;
   408 
   409 	/* Verify that we're still ticking */
   410 	if (g_SimTicks.ticking)
   411 	{
   412 		if (g_StillFrames == 0)
   413 		{
   414 			g_StillFrames = framecount;
   415 			g_StillTime = curtime;
   416 		} else {
   417 			/* Try to detect when we've stopped ticking.
   418 			 * We do this once 10 frames pass and there have been no ticks.
   419 			 */
   420 			if (g_StillTime == curtime)
   421 			{
   422 				if (framecount - g_StillFrames >= 5)
   423 				{
   424 					StartTickSimulation();
   425 					return;
   426 				}
   427 			} else {
   428 				/* We're definitely ticking we get here, 
   429 				 * but update everything as a precaution */
   430 				g_StillFrames = framecount;
   431 				g_StillTime = curtime;
   432 			}
   433 		}
   434 	} else {
   435 		/* We need to make sure we should still be simulating. */
   436 		if (g_StillTime != curtime)
   437 		{
   438 			/* Wow, we're ticking again! It's time to revert. */
   439 			StopTickSimulation();
   440 			return;
   441 		}
   442 		/* Nope, not ticking.  Simulate! */
   443 		SimulateTick();
   444 		curtime = g_SimTicks.ticktime;
   445 	}
   446 
   447 	if (curtime - g_LastTime >= 0.1f)
   448 	{
   449 		if (m_CheckingAuth
   450 			&& (gpGlobals->curtime - g_LastAuthCheck > 0.7f))
   451 		{
   452 			g_LastAuthCheck = gpGlobals->curtime;
   453 			g_Players.RunAuthChecks();
   454 		}
   455 
   456 		g_Timers.RunFrame();
   457 		g_LastTime = curtime;
   458 	}
   459 
   460 	if (g_SimTicks.ticking && curtime - g_LastMenuTime >= 1.0f)
   461 	{
   462 		g_ValveMenuStyle.ProcessWatchList();
   463 		g_RadioMenuStyle.ProcessWatchList();
   464 		g_LastMenuTime = curtime;
   465 	}
   466 
   467 	if (g_pOnGameFrame && g_pOnGameFrame->GetFunctionCount())
   468 	{
   469 		g_pOnGameFrame->Execute(NULL);
   470 	}
   471 }
   472 
   473 void SourceModBase::LevelShutdown()
   474 {
   475 	if (g_LevelEndBarrier)
   476 	{
   477 		SMGlobalClass *next = SMGlobalClass::head;
   478 		while (next)
   479 		{
   480 			next->OnSourceModLevelEnd();
   481 			next = next->m_pGlobalClassNext;
   482 		}
   483 		g_LevelEndBarrier = false;
   484 	}
   485 
   486 	if (g_pOnMapEnd && m_ExecOnMapEnd)
   487 	{
   488 		g_pOnMapEnd->Execute(NULL);
   489 		m_ExecOnMapEnd = false;
   490 	}
   491 
   492 	g_OnMapStarted = false;
   493 
   494 	if (m_ExecPluginReload)
   495 	{
   496 		g_PluginSys.ReloadOrUnloadPlugins();
   497 		m_ExecPluginReload = false;
   498 	}
   499 }
   500 
   501 bool SourceModBase::IsMapLoading() const
   502 {
   503 	return m_IsMapLoading;
   504 }
   505 
   506 void SourceModBase::DoGlobalPluginLoads()
   507 {
   508 	char config_path[PLATFORM_MAX_PATH];
   509 	char plugins_path[PLATFORM_MAX_PATH];
   510 
   511 	BuildPath(Path_SM, config_path, 
   512 		sizeof(config_path),
   513 		"configs/plugin_settings.cfg");
   514 	BuildPath(Path_SM, plugins_path,
   515 		sizeof(plugins_path),
   516 		"plugins");
   517 
   518 	/* Load any auto extensions */
   519 	g_Extensions.TryAutoload();
   520 
   521 	/* Run the first pass */
   522 	g_PluginSys.LoadAll_FirstPass(config_path, plugins_path);
   523 
   524 	/* Mark any extensions as loaded */
   525 	g_Extensions.MarkAllLoaded();
   526 
   527 	/* No modules yet, it's safe to call this from here */
   528 	g_PluginSys.LoadAll_SecondPass();
   529 
   530 	/* Re-mark any extensions as loaded */
   531 	g_Extensions.MarkAllLoaded();
   532 
   533 	/* Call OnAllPluginsLoaded */
   534 	g_PluginSys.AllPluginsLoaded();
   535 }
   536 
   537 size_t SourceModBase::BuildPath(PathType type, char *buffer, size_t maxlength, char *format, ...)
   538 {
   539 	char _buffer[PLATFORM_MAX_PATH];
   540 	va_list ap;
   541 
   542 	va_start(ap, format);
   543 	vsnprintf(_buffer, PLATFORM_MAX_PATH, format, ap);
   544 	va_end(ap);
   545 
   546 	const char *base = NULL;
   547 	if (type == Path_Game)
   548 	{
   549 		base = GetGamePath();
   550 	} else if (type == Path_SM) {
   551 		base = GetSourceModPath();
   552 	} else if (type == Path_SM_Rel) {
   553 		base = m_SMRelDir;
   554 	}
   555 
   556 	if (base)
   557 	{
   558 		return g_LibSys.PathFormat(buffer, maxlength, "%s/%s", base, _buffer);
   559 	} else {
   560 		return g_LibSys.PathFormat(buffer, maxlength, "%s", _buffer);
   561 	}
   562 }
   563 
   564 void SourceModBase::CloseSourceMod()
   565 {
   566 	/* Force a level end */
   567 	LevelShutdown();
   568 
   569 	/* Unload plugins */
   570 	g_PluginSys.Shutdown();
   571 
   572 	/* Unload extensions */
   573 	g_Extensions.Shutdown();
   574 
   575 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
   576 
   577 	if (g_Loaded)
   578 	{
   579 		if (g_pOnGameFrame)
   580 		{
   581 			g_Forwards.ReleaseForward(g_pOnGameFrame);
   582 		}
   583 
   584 		if (g_pOnMapEnd)
   585 		{
   586 			g_Forwards.ReleaseForward(g_pOnMapEnd);
   587 		}
   588 	
   589 		/* Notify! */
   590 		SMGlobalClass *pBase = SMGlobalClass::head;
   591 		while (pBase)
   592 		{
   593 			pBase->OnSourceModShutdown();
   594 			pBase = pBase->m_pGlobalClassNext;
   595 		}
   596 	
   597 		/* Delete all data packs */
   598 		CStack<CDataPack *>::iterator iter;
   599 		CDataPack *pd;
   600 		for (iter=m_freepacks.begin(); iter!=m_freepacks.end(); iter++)
   601 		{
   602 			pd = (*iter);
   603 			delete pd;
   604 		}
   605 		m_freepacks.popall();
   606 	
   607 		/* Notify! */
   608 		pBase = SMGlobalClass::head;
   609 		while (pBase)
   610 		{
   611 			pBase->OnSourceModAllShutdown();
   612 			pBase = pBase->m_pGlobalClassNext;
   613 		}
   614 	
   615 		if (enginePatch)
   616 		{
   617 			SH_RELEASE_CALLCLASS(enginePatch);
   618 			enginePatch = NULL;
   619 		}
   620 	
   621 		if (gamedllPatch)
   622 		{
   623 			SH_RELEASE_CALLCLASS(gamedllPatch);
   624 			gamedllPatch = NULL;
   625 		}
   626 
   627 		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
   628 		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, this, &SourceModBase::GameFrame, false);
   629 	}
   630 
   631 	/* Rest In Peace */
   632 	ShutdownJIT();
   633 }
   634 
   635 void SourceModBase::LogMessage(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.LogMessage("[%s] %s", tag, buffer);
   649 	} else {
   650 		g_Logger.LogMessage("%s", buffer);
   651 	}
   652 }
   653 
   654 void SourceModBase::LogError(IExtension *pExt, const char *format, ...)
   655 {
   656 	IExtensionInterface *pAPI = pExt->GetAPI();
   657 	const char *tag = pAPI->GetExtensionTag();
   658 	char buffer[2048];
   659 	va_list ap;
   660 
   661 	va_start(ap, format);
   662 	vsnprintf(buffer, sizeof(buffer), format, ap);
   663 	va_end(ap);
   664 
   665 	if (tag)
   666 	{
   667 		g_Logger.LogError("[%s] %s", tag, buffer);
   668 	} else {
   669 		g_Logger.LogError("%s", buffer);
   670 	}
   671 }
   672 
   673 size_t SourceModBase::FormatString(char *buffer, size_t maxlength, IPluginContext *pContext, const cell_t *params, unsigned int param)
   674 {
   675 	char *fmt;
   676 
   677 	pContext->LocalToString(params[param], &fmt);
   678 
   679 	int lparam = ++param;
   680 
   681 	return atcprintf(buffer, maxlength, fmt, pContext, params, &lparam);
   682 }
   683 
   684 const char *SourceModBase::GetSourceModPath() const
   685 {
   686 	return m_SMBaseDir;
   687 }
   688 
   689 const char *SourceModBase::GetGamePath() const
   690 {
   691 	return g_BaseDir.c_str();
   692 }
   693 
   694 void SourceModBase::SetGlobalTarget(unsigned int index)
   695 {
   696 	m_target = index;
   697 }
   698 
   699 void SourceModBase::SetAuthChecking(bool set)
   700 {
   701 	m_CheckingAuth = set;
   702 }
   703 
   704 unsigned int SourceModBase::GetGlobalTarget() const
   705 {
   706 	return m_target;
   707 }
   708 
   709 IDataPack *SourceModBase::CreateDataPack()
   710 {
   711 	CDataPack *pack;
   712 	if (m_freepacks.empty())
   713 	{
   714 		pack = new CDataPack;
   715 	} else {
   716 		pack = m_freepacks.front();
   717 		m_freepacks.pop();
   718 		pack->Initialize();
   719 	}
   720 	return pack;
   721 }
   722 
   723 void SourceModBase::FreeDataPack(IDataPack *pack)
   724 {
   725 	m_freepacks.push(static_cast<CDataPack *>(pack));
   726 }
   727 
   728 Handle_t SourceModBase::GetDataPackHandleType(bool readonly)
   729 {
   730 	//:TODO:
   731 	return 0;
   732 }
   733 
   734 const char *SourceModBase::GetGameFolderName() const
   735 {
   736 	return m_ModDir;
   737 }
   738 
   739 ISourcePawnEngine *SourceModBase::GetScriptingEngine()
   740 {
   741 	return g_pSourcePawn;
   742 }
   743 
   744 IVirtualMachine *SourceModBase::GetScriptingVM()
   745 {
   746 	return g_pVM;
   747 }
   748 
   749 void SourceModBase::AllPluginsLoaded()
   750 {
   751 	SMGlobalClass *base = SMGlobalClass::head;
   752 	while (base)
   753 	{
   754 		base->OnSourceModGameInitialized();
   755 		base = base->m_pGlobalClassNext;
   756 	}
   757 }
   758 
   759 SMGlobalClass *SMGlobalClass::head = NULL;
   760 
   761 SMGlobalClass::SMGlobalClass()
   762 {
   763 	m_pGlobalClassNext = SMGlobalClass::head;
   764 	SMGlobalClass::head = this;
   765 }