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