core/sourcemod.cpp
branchrefac-jit
changeset 2168 c24a6fa957db
child 2175 54a8411faaac
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/sourcemod.cpp	Sun Jul 06 04:49:55 2008 +0000
     1.3 @@ -0,0 +1,720 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod
     1.8 + * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id$
    1.33 + */
    1.34 +
    1.35 +#include <stdio.h>
    1.36 +#include "sourcemod.h"
    1.37 +#include "sourcemm_api.h"
    1.38 +#include "systems/LibrarySys.h"
    1.39 +#include "vm/sp_vm_engine.h"
    1.40 +#include <sh_string.h>
    1.41 +#include "PluginSys.h"
    1.42 +#include "ShareSys.h"
    1.43 +#include "CoreConfig.h"
    1.44 +#include "Logger.h"
    1.45 +#include "ExtensionSys.h"
    1.46 +#include "AdminCache.h"
    1.47 +#include "sm_stringutil.h"
    1.48 +#include "PlayerManager.h"
    1.49 +#include "Translator.h"
    1.50 +#include "ForwardSys.h"
    1.51 +#include "TimerSys.h"
    1.52 +#include "GameConfigs.h"
    1.53 +
    1.54 +SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
    1.55 +SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
    1.56 +SH_DECL_HOOK1_void(IServerGameDLL, GameFrame, SH_NOATTRIB, false, bool);
    1.57 +SH_DECL_HOOK1_void(IVEngineServer, ServerCommand, SH_NOATTRIB, false, const char *);
    1.58 +
    1.59 +SourcePawnEngine g_SourcePawn;
    1.60 +SourceModBase g_SourceMod;
    1.61 +
    1.62 +ILibrary *g_pJIT = NULL;
    1.63 +SourceHook::String g_BaseDir;
    1.64 +ISourcePawnEngine *g_pSourcePawn = &g_SourcePawn;
    1.65 +IVirtualMachine *g_pVM;
    1.66 +IdentityToken_t *g_pCoreIdent = NULL;
    1.67 +IForward *g_pOnMapEnd = NULL;
    1.68 +bool g_Loaded = false;
    1.69 +
    1.70 +typedef int (*GIVEENGINEPOINTER)(ISourcePawnEngine *);
    1.71 +typedef int (*GIVEENGINEPOINTER2)(ISourcePawnEngine *, unsigned int api_version);
    1.72 +typedef unsigned int (*GETEXPORTCOUNT)();
    1.73 +typedef IVirtualMachine *(*GETEXPORT)(unsigned int);
    1.74 +typedef void (*NOTIFYSHUTDOWN)();
    1.75 +
    1.76 +void ShutdownJIT()
    1.77 +{
    1.78 +	NOTIFYSHUTDOWN notify = (NOTIFYSHUTDOWN)g_pJIT->GetSymbolAddress("NotifyShutdown");
    1.79 +	if (notify)
    1.80 +	{
    1.81 +		notify();
    1.82 +	}
    1.83 +
    1.84 +	g_pJIT->CloseLibrary();
    1.85 +}
    1.86 +
    1.87 +SourceModBase::SourceModBase()
    1.88 +{
    1.89 +	m_IsMapLoading = false;
    1.90 +	m_ExecPluginReload = false;
    1.91 +	m_GotBasePath = false;
    1.92 +}
    1.93 +
    1.94 +ConfigResult SourceModBase::OnSourceModConfigChanged(const char *key, 
    1.95 +									  const char *value, 
    1.96 +									  ConfigSource source, 
    1.97 +									  char *error, 
    1.98 +									  size_t maxlength)
    1.99 +{
   1.100 +	if (strcasecmp(key, "BasePath") == 0)
   1.101 +	{
   1.102 +		if (source == ConfigSource_Console)
   1.103 +		{
   1.104 +			UTIL_Format(error, maxlength, "Cannot be set at runtime");
   1.105 +			return ConfigResult_Reject;
   1.106 +		}
   1.107 +
   1.108 +		if (!m_GotBasePath)
   1.109 +		{
   1.110 +			g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/%s", g_BaseDir.c_str(), value);
   1.111 +			g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), value);
   1.112 +
   1.113 +			m_GotBasePath = true;
   1.114 +		}
   1.115 +
   1.116 +		return ConfigResult_Accept;
   1.117 +	}
   1.118 +
   1.119 +	return ConfigResult_Ignore;
   1.120 +}
   1.121 +
   1.122 +bool SourceModBase::InitializeSourceMod(char *error, size_t maxlength, bool late)
   1.123 +{
   1.124 +	const char *gamepath = g_SMAPI->GetBaseDir();
   1.125 +
   1.126 +	/* Store full path to game */
   1.127 +	g_BaseDir.assign(gamepath);
   1.128 +
   1.129 +	/* Store name of game directory by itself */
   1.130 +	size_t len = strlen(gamepath);
   1.131 +	for (size_t i = len - 1; i >= 0; i--)
   1.132 +	{
   1.133 +		if (gamepath[i] == PLATFORM_SEP_CHAR)
   1.134 +		{
   1.135 +			strncopy(m_ModDir, &gamepath[++i], sizeof(m_ModDir));
   1.136 +			break;
   1.137 +		}
   1.138 +	}
   1.139 +
   1.140 +	/* Initialize CoreConfig so we can get SourceMod base path properly - this basically parses core.cfg */
   1.141 +	g_CoreConfig.Initialize();
   1.142 +
   1.143 +	/* This shouldn't happen, but can't hurt to be safe */
   1.144 +	if (!g_LibSys.PathExists(m_SMBaseDir) || !m_GotBasePath)
   1.145 +	{
   1.146 +		g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/addons/sourcemod", g_BaseDir.c_str());
   1.147 +		g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), "addons/sourcemod");
   1.148 +		m_GotBasePath = true;
   1.149 +	}
   1.150 +
   1.151 +	/* Attempt to load the JIT! */
   1.152 +	char file[PLATFORM_MAX_PATH];
   1.153 +	char myerror[255];
   1.154 +	g_SMAPI->PathFormat(file, sizeof(file), "%s/bin/sourcepawn.jit.x86.%s",
   1.155 +		GetSourceModPath(),
   1.156 +		PLATFORM_LIB_EXT
   1.157 +		);
   1.158 +
   1.159 +	g_pJIT = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
   1.160 +	if (!g_pJIT)
   1.161 +	{
   1.162 +		if (error && maxlength)
   1.163 +		{
   1.164 +			snprintf(error, maxlength, "%s (failed to load bin/sourcepawn.jit.x86.%s)", 
   1.165 +				myerror,
   1.166 +				PLATFORM_LIB_EXT);
   1.167 +		}
   1.168 +		return false;
   1.169 +	}
   1.170 +
   1.171 +	int err;
   1.172 +	
   1.173 +	GIVEENGINEPOINTER2 jit_init2 = (GIVEENGINEPOINTER2)g_pJIT->GetSymbolAddress("GiveEnginePointer2");
   1.174 +	if (!jit_init2)
   1.175 +	{
   1.176 +		GIVEENGINEPOINTER jit_init = (GIVEENGINEPOINTER)g_pJIT->GetSymbolAddress("GiveEnginePointer");
   1.177 +		if (!jit_init)
   1.178 +		{
   1.179 +			ShutdownJIT();
   1.180 +			if (error && maxlength)
   1.181 +			{
   1.182 +				snprintf(error, maxlength, "Failed to find GiveEnginePointer in JIT!");
   1.183 +			}
   1.184 +			return false;
   1.185 +		}
   1.186 +
   1.187 +		if ((err=jit_init(g_pSourcePawn)) != 0)
   1.188 +		{
   1.189 +			ShutdownJIT();
   1.190 +			if (error && maxlength)
   1.191 +			{
   1.192 +				snprintf(error, maxlength, "GiveEnginePointer returned %d in the JIT", err);
   1.193 +			}
   1.194 +			return false;
   1.195 +		}
   1.196 +	}
   1.197 +	else
   1.198 +	{
   1.199 +		/* On version bumps, we should check for older versions as well, if the new version fails.
   1.200 +		 * We can then check the exports to see if any VM versions will be sufficient.
   1.201 +		 */
   1.202 +		if ((err=jit_init2(g_pSourcePawn, SOURCEPAWN_ENGINE_API_VERSION)) != SP_ERROR_NONE)
   1.203 +		{
   1.204 +			ShutdownJIT();
   1.205 +			if (error && maxlength)
   1.206 +			{
   1.207 +				snprintf(error, maxlength, "JIT incompatible with SourceMod version");
   1.208 +			}
   1.209 +			return false;
   1.210 +		}
   1.211 +	}
   1.212 +
   1.213 +	GETEXPORTCOUNT jit_getnum = (GETEXPORTCOUNT)g_pJIT->GetSymbolAddress("GetExportCount");
   1.214 +	GETEXPORT jit_get = (GETEXPORT)g_pJIT->GetSymbolAddress("GetExport");
   1.215 +	if (!jit_get)
   1.216 +	{
   1.217 +		ShutdownJIT();
   1.218 +		if (error && maxlength)
   1.219 +		{
   1.220 +			snprintf(error, maxlength, "JIT is missing a necessary export!");
   1.221 +		}
   1.222 +		return false;
   1.223 +	}
   1.224 +
   1.225 +	unsigned int num = jit_getnum();
   1.226 +	if (!num)
   1.227 +	{
   1.228 +		ShutdownJIT();
   1.229 +		if (error && maxlength)
   1.230 +		{
   1.231 +			snprintf(error, maxlength, "JIT did not export any virtual machines!");
   1.232 +		}
   1.233 +		return false;
   1.234 +	}
   1.235 +
   1.236 +	unsigned int api_version;
   1.237 +	for (unsigned int i=0; i<num; i++)
   1.238 +	{
   1.239 +		 if ((g_pVM=jit_get(i)) == NULL)
   1.240 +		 {
   1.241 +			 if (error && maxlength)
   1.242 +			 {
   1.243 +				 snprintf(error, maxlength, "JIT did not export any virtual machines!");
   1.244 +			 }
   1.245 +			 continue;
   1.246 +		 }
   1.247 +		/* Refuse any API that we might not be able to deal with.
   1.248 +		 * <s>Also refuse anything < 3 because we need fake natives.</s>
   1.249 +		 * Also refuse anything < 7 because we need the new sp_native definition.
   1.250 +		 */
   1.251 +		 api_version = g_pVM->GetAPIVersion();
   1.252 +		 if (api_version < 7 || api_version > SOURCEPAWN_VM_API_VERSION)
   1.253 +		 {
   1.254 +			 if (error && maxlength)
   1.255 +			 {
   1.256 +				 snprintf(error, maxlength, "JIT is not a compatible version");
   1.257 +			 }
   1.258 +			 g_pVM = NULL;
   1.259 +			 continue;
   1.260 +		 }
   1.261 +		 break;
   1.262 +	}
   1.263 +
   1.264 +	if (!g_pVM)
   1.265 +	{
   1.266 +		ShutdownJIT();
   1.267 +		return false;
   1.268 +	}
   1.269 +
   1.270 +	/* Hook this now so we can detect startup without calling StartSourceMod() */
   1.271 +	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
   1.272 +
   1.273 +	/* Only load if we're not late */
   1.274 +	if (!late)
   1.275 +	{
   1.276 +		StartSourceMod(false);
   1.277 +	}
   1.278 +
   1.279 +	return true;
   1.280 +}
   1.281 +
   1.282 +void SourceModBase::StartSourceMod(bool late)
   1.283 +{
   1.284 +	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
   1.285 +	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, &g_Timers, &TimerSystem::GameFrame, false);
   1.286 +
   1.287 +	enginePatch = SH_GET_CALLCLASS(engine);
   1.288 +	gamedllPatch = SH_GET_CALLCLASS(gamedll);
   1.289 +
   1.290 +	g_ShareSys.Initialize();
   1.291 +
   1.292 +	/* Make the global core identity */
   1.293 +	g_pCoreIdent = g_ShareSys.CreateCoreIdentity();
   1.294 +
   1.295 +	/* Notify! */
   1.296 +	SMGlobalClass *pBase = SMGlobalClass::head;
   1.297 +	while (pBase)
   1.298 +	{
   1.299 +		pBase->OnSourceModStartup(false);
   1.300 +		pBase = pBase->m_pGlobalClassNext;
   1.301 +	}
   1.302 +
   1.303 +	/* Notify! */
   1.304 +	pBase = SMGlobalClass::head;
   1.305 +	while (pBase)
   1.306 +	{
   1.307 +		pBase->OnSourceModAllInitialized();
   1.308 +		pBase = pBase->m_pGlobalClassNext;
   1.309 +	}
   1.310 +
   1.311 +	/* Notify! */
   1.312 +	pBase = SMGlobalClass::head;
   1.313 +	while (pBase)
   1.314 +	{
   1.315 +		pBase->OnSourceModAllInitialized_Post();
   1.316 +		pBase = pBase->m_pGlobalClassNext;
   1.317 +	}
   1.318 +
   1.319 +	/* Add us now... */
   1.320 +	g_ShareSys.AddInterface(NULL, this);
   1.321 +
   1.322 +	/* We're loaded! */
   1.323 +	g_Loaded = true;
   1.324 +
   1.325 +	/* Initialize VSP stuff */
   1.326 +	if (vsp_interface != NULL)
   1.327 +	{
   1.328 +		g_SourceMod_Core.OnVSPListening(vsp_interface);
   1.329 +	}
   1.330 +}
   1.331 +
   1.332 +static bool g_LevelEndBarrier = false;
   1.333 +bool SourceModBase::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
   1.334 +{
   1.335 +	/* If we're not loaded... */
   1.336 +	if (!g_Loaded)
   1.337 +	{
   1.338 +		/* Do all global initialization now */
   1.339 +		StartSourceMod(true);
   1.340 +	}
   1.341 +
   1.342 +	m_IsMapLoading = true;
   1.343 +	m_ExecPluginReload = true;
   1.344 +
   1.345 +	/* Notify! */
   1.346 +	SMGlobalClass *pBase = SMGlobalClass::head;
   1.347 +	while (pBase)
   1.348 +	{
   1.349 +		pBase->OnSourceModLevelChange(pMapName);
   1.350 +		pBase = pBase->m_pGlobalClassNext;
   1.351 +	}
   1.352 +
   1.353 +	DoGlobalPluginLoads();
   1.354 +
   1.355 +	m_IsMapLoading = false;
   1.356 +
   1.357 +	/* Notify! */
   1.358 +	pBase = SMGlobalClass::head;
   1.359 +	while (pBase)
   1.360 +	{
   1.361 +		pBase->OnSourceModPluginsLoaded();
   1.362 +		pBase = pBase->m_pGlobalClassNext;
   1.363 +	}
   1.364 +
   1.365 +	if (!g_pOnMapEnd)
   1.366 +	{
   1.367 +		g_pOnMapEnd = g_Forwards.CreateForward("OnMapEnd", ET_Ignore, 0, NULL);
   1.368 +	}
   1.369 +
   1.370 +	g_LevelEndBarrier = true;
   1.371 +
   1.372 +	RETURN_META_VALUE(MRES_IGNORED, true);
   1.373 +}
   1.374 +
   1.375 +void SourceModBase::LevelShutdown()
   1.376 +{
   1.377 +	if (g_LevelEndBarrier)
   1.378 +	{
   1.379 +		SMGlobalClass *next = SMGlobalClass::head;
   1.380 +		while (next)
   1.381 +		{
   1.382 +			next->OnSourceModLevelEnd();
   1.383 +			next = next->m_pGlobalClassNext;
   1.384 +		}
   1.385 +		
   1.386 +		if (g_pOnMapEnd != NULL)
   1.387 +		{
   1.388 +			g_pOnMapEnd->Execute(NULL);
   1.389 +		}
   1.390 +
   1.391 +		g_Timers.RemoveMapChangeTimers();
   1.392 +
   1.393 +		g_LevelEndBarrier = false;
   1.394 +	}
   1.395 +
   1.396 +	g_OnMapStarted = false;
   1.397 +
   1.398 +	if (m_ExecPluginReload)
   1.399 +	{
   1.400 +		g_PluginSys.ReloadOrUnloadPlugins();
   1.401 +		m_ExecPluginReload = false;
   1.402 +	}
   1.403 +}
   1.404 +
   1.405 +bool SourceModBase::IsMapLoading() const
   1.406 +{
   1.407 +	return m_IsMapLoading;
   1.408 +}
   1.409 +
   1.410 +void SourceModBase::DoGlobalPluginLoads()
   1.411 +{
   1.412 +	char config_path[PLATFORM_MAX_PATH];
   1.413 +	char plugins_path[PLATFORM_MAX_PATH];
   1.414 +
   1.415 +	BuildPath(Path_SM, config_path, 
   1.416 +		sizeof(config_path),
   1.417 +		"configs/plugin_settings.cfg");
   1.418 +	BuildPath(Path_SM, plugins_path,
   1.419 +		sizeof(plugins_path),
   1.420 +		"plugins");
   1.421 +
   1.422 +	/* Load any auto extensions */
   1.423 +	g_Extensions.TryAutoload();
   1.424 +
   1.425 +	/* Fire the extensions ready message */
   1.426 +	g_SMAPI->MetaFactory(SOURCEMOD_NOTICE_EXTENSIONS, NULL, NULL);	
   1.427 +
   1.428 +	/* Load any game extension */
   1.429 +	const char *game_ext;
   1.430 +	if ((game_ext = g_pGameConf->GetKeyValue("GameExtension")) != NULL)
   1.431 +	{
   1.432 +		char path[PLATFORM_MAX_PATH];
   1.433 +		UTIL_Format(path, sizeof(path), "%s.ext." PLATFORM_LIB_EXT, game_ext);
   1.434 +		g_Extensions.LoadAutoExtension(path);
   1.435 +	}
   1.436 +
   1.437 +	/* Run the first pass */
   1.438 +	g_PluginSys.LoadAll_FirstPass(config_path, plugins_path);
   1.439 +
   1.440 +	/* Mark any extensions as loaded */
   1.441 +	g_Extensions.MarkAllLoaded();
   1.442 +
   1.443 +	/* No modules yet, it's safe to call this from here */
   1.444 +	g_PluginSys.LoadAll_SecondPass();
   1.445 +
   1.446 +	/* Re-mark any extensions as loaded */
   1.447 +	g_Extensions.MarkAllLoaded();
   1.448 +
   1.449 +	/* Call OnAllPluginsLoaded */
   1.450 +	g_PluginSys.AllPluginsLoaded();
   1.451 +}
   1.452 +
   1.453 +size_t SourceModBase::BuildPath(PathType type, char *buffer, size_t maxlength, const char *format, ...)
   1.454 +{
   1.455 +	char _buffer[PLATFORM_MAX_PATH];
   1.456 +	va_list ap;
   1.457 +
   1.458 +	va_start(ap, format);
   1.459 +	vsnprintf(_buffer, PLATFORM_MAX_PATH, format, ap);
   1.460 +	va_end(ap);
   1.461 +
   1.462 +	/* If we get a "file://" notation, strip off the file:// part so we're left 
   1.463 +	 * with an absolute path.  Note that the absolute path gets returned, so 
   1.464 +	 * usage with relative paths here is completely invalid.
   1.465 +	 */
   1.466 +	if (type != Path_SM_Rel && strncmp(_buffer, "file://", 7) == 0)
   1.467 +	{
   1.468 +		return g_LibSys.PathFormat(buffer, maxlength, "%s", &_buffer[7]);
   1.469 +	}
   1.470 +
   1.471 +	const char *base = NULL;
   1.472 +	if (type == Path_Game)
   1.473 +	{
   1.474 +		base = GetGamePath();
   1.475 +	}
   1.476 +	else if (type == Path_SM)
   1.477 +	{
   1.478 +		base = GetSourceModPath();
   1.479 +	}
   1.480 +	else if (type == Path_SM_Rel)
   1.481 +	{
   1.482 +		base = m_SMRelDir;
   1.483 +	}
   1.484 +
   1.485 +	if (base)
   1.486 +	{
   1.487 +		return g_LibSys.PathFormat(buffer, maxlength, "%s/%s", base, _buffer);
   1.488 +	}
   1.489 +	else
   1.490 +	{
   1.491 +		return g_LibSys.PathFormat(buffer, maxlength, "%s", _buffer);
   1.492 +	}
   1.493 +}
   1.494 +
   1.495 +void SourceModBase::CloseSourceMod()
   1.496 +{
   1.497 +	/* Force a level end */
   1.498 +	LevelShutdown();
   1.499 +
   1.500 +	/* Unload plugins */
   1.501 +	g_PluginSys.Shutdown();
   1.502 +
   1.503 +	/* Unload extensions */
   1.504 +	g_Extensions.Shutdown();
   1.505 +
   1.506 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
   1.507 +
   1.508 +	if (g_Loaded)
   1.509 +	{
   1.510 +		if (g_pOnMapEnd)
   1.511 +		{
   1.512 +			g_Forwards.ReleaseForward(g_pOnMapEnd);
   1.513 +		}
   1.514 +	
   1.515 +		/* Notify! */
   1.516 +		SMGlobalClass *pBase = SMGlobalClass::head;
   1.517 +		while (pBase)
   1.518 +		{
   1.519 +			pBase->OnSourceModShutdown();
   1.520 +			pBase = pBase->m_pGlobalClassNext;
   1.521 +		}
   1.522 +	
   1.523 +		/* Delete all data packs */
   1.524 +		CStack<CDataPack *>::iterator iter;
   1.525 +		CDataPack *pd;
   1.526 +		for (iter=m_freepacks.begin(); iter!=m_freepacks.end(); iter++)
   1.527 +		{
   1.528 +			pd = (*iter);
   1.529 +			delete pd;
   1.530 +		}
   1.531 +		m_freepacks.popall();
   1.532 +	
   1.533 +		/* Notify! */
   1.534 +		pBase = SMGlobalClass::head;
   1.535 +		while (pBase)
   1.536 +		{
   1.537 +			pBase->OnSourceModAllShutdown();
   1.538 +			pBase = pBase->m_pGlobalClassNext;
   1.539 +		}
   1.540 +	
   1.541 +		if (enginePatch)
   1.542 +		{
   1.543 +			SH_RELEASE_CALLCLASS(enginePatch);
   1.544 +			enginePatch = NULL;
   1.545 +		}
   1.546 +	
   1.547 +		if (gamedllPatch)
   1.548 +		{
   1.549 +			SH_RELEASE_CALLCLASS(gamedllPatch);
   1.550 +			gamedllPatch = NULL;
   1.551 +		}
   1.552 +
   1.553 +		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
   1.554 +		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, &g_Timers, &TimerSystem::GameFrame, false);
   1.555 +	}
   1.556 +
   1.557 +	/* Rest In Peace */
   1.558 +	ShutdownJIT();
   1.559 +}
   1.560 +
   1.561 +void SourceModBase::LogMessage(IExtension *pExt, const char *format, ...)
   1.562 +{
   1.563 +	IExtensionInterface *pAPI = pExt->GetAPI();
   1.564 +	const char *tag = pAPI->GetExtensionTag();
   1.565 +	char buffer[2048];
   1.566 +	va_list ap;
   1.567 +
   1.568 +	va_start(ap, format);
   1.569 +	vsnprintf(buffer, sizeof(buffer), format, ap);
   1.570 +	va_end(ap);
   1.571 +
   1.572 +	if (tag)
   1.573 +	{
   1.574 +		g_Logger.LogMessage("[%s] %s", tag, buffer);
   1.575 +	} else {
   1.576 +		g_Logger.LogMessage("%s", buffer);
   1.577 +	}
   1.578 +}
   1.579 +
   1.580 +void SourceModBase::LogError(IExtension *pExt, const char *format, ...)
   1.581 +{
   1.582 +	IExtensionInterface *pAPI = pExt->GetAPI();
   1.583 +	const char *tag = pAPI->GetExtensionTag();
   1.584 +	char buffer[2048];
   1.585 +	va_list ap;
   1.586 +
   1.587 +	va_start(ap, format);
   1.588 +	vsnprintf(buffer, sizeof(buffer), format, ap);
   1.589 +	va_end(ap);
   1.590 +
   1.591 +	if (tag)
   1.592 +	{
   1.593 +		g_Logger.LogError("[%s] %s", tag, buffer);
   1.594 +	} else {
   1.595 +		g_Logger.LogError("%s", buffer);
   1.596 +	}
   1.597 +}
   1.598 +
   1.599 +size_t SourceModBase::FormatString(char *buffer, size_t maxlength, IPluginContext *pContext, const cell_t *params, unsigned int param)
   1.600 +{
   1.601 +	char *fmt;
   1.602 +
   1.603 +	pContext->LocalToString(params[param], &fmt);
   1.604 +
   1.605 +	int lparam = ++param;
   1.606 +
   1.607 +	return atcprintf(buffer, maxlength, fmt, pContext, params, &lparam);
   1.608 +}
   1.609 +
   1.610 +const char *SourceModBase::GetSourceModPath() const
   1.611 +{
   1.612 +	return m_SMBaseDir;
   1.613 +}
   1.614 +
   1.615 +const char *SourceModBase::GetGamePath() const
   1.616 +{
   1.617 +	return g_BaseDir.c_str();
   1.618 +}
   1.619 +
   1.620 +unsigned int SourceModBase::SetGlobalTarget(unsigned int index)
   1.621 +{
   1.622 +	unsigned int old = m_target;
   1.623 +	m_target = index;
   1.624 +	return old;
   1.625 +}
   1.626 +
   1.627 +unsigned int SourceModBase::GetGlobalTarget() const
   1.628 +{
   1.629 +	return m_target;
   1.630 +}
   1.631 +
   1.632 +IDataPack *SourceModBase::CreateDataPack()
   1.633 +{
   1.634 +	CDataPack *pack;
   1.635 +	if (m_freepacks.empty())
   1.636 +	{
   1.637 +		pack = new CDataPack;
   1.638 +	} else {
   1.639 +		pack = m_freepacks.front();
   1.640 +		m_freepacks.pop();
   1.641 +		pack->Initialize();
   1.642 +	}
   1.643 +	return pack;
   1.644 +}
   1.645 +
   1.646 +void SourceModBase::FreeDataPack(IDataPack *pack)
   1.647 +{
   1.648 +	m_freepacks.push(static_cast<CDataPack *>(pack));
   1.649 +}
   1.650 +
   1.651 +Handle_t SourceModBase::GetDataPackHandleType(bool readonly)
   1.652 +{
   1.653 +	//:TODO:
   1.654 +	return 0;
   1.655 +}
   1.656 +
   1.657 +const char *SourceModBase::GetGameFolderName() const
   1.658 +{
   1.659 +	return m_ModDir;
   1.660 +}
   1.661 +
   1.662 +ISourcePawnEngine *SourceModBase::GetScriptingEngine()
   1.663 +{
   1.664 +	return g_pSourcePawn;
   1.665 +}
   1.666 +
   1.667 +IVirtualMachine *SourceModBase::GetScriptingVM()
   1.668 +{
   1.669 +	return g_pVM;
   1.670 +}
   1.671 +
   1.672 +void SourceModBase::AllPluginsLoaded()
   1.673 +{
   1.674 +	SMGlobalClass *base = SMGlobalClass::head;
   1.675 +	while (base)
   1.676 +	{
   1.677 +		base->OnSourceModGameInitialized();
   1.678 +		base = base->m_pGlobalClassNext;
   1.679 +	}
   1.680 +}
   1.681 +
   1.682 +time_t SourceModBase::GetAdjustedTime()
   1.683 +{
   1.684 +	return ::GetAdjustedTime();
   1.685 +}
   1.686 +
   1.687 +void SourceModBase::AddGameFrameHook(GAME_FRAME_HOOK hook)
   1.688 +{
   1.689 +	m_frame_hooks.push_back(hook);
   1.690 +}
   1.691 +
   1.692 +void SourceModBase::RemoveGameFrameHook(GAME_FRAME_HOOK hook)
   1.693 +{
   1.694 +	for (size_t i = 0; i < m_frame_hooks.size(); i++)
   1.695 +	{
   1.696 +		if (m_frame_hooks[i] == hook)
   1.697 +		{
   1.698 +			m_frame_hooks.erase(m_frame_hooks.iterAt(i));
   1.699 +			return;
   1.700 +		}
   1.701 +	}
   1.702 +}
   1.703 +
   1.704 +void SourceModBase::ProcessGameFrameHooks(bool simulating)
   1.705 +{
   1.706 +	if (m_frame_hooks.size() == 0)
   1.707 +	{
   1.708 +		return;
   1.709 +	}
   1.710 +
   1.711 +	for (size_t i = 0; i < m_frame_hooks.size(); i++)
   1.712 +	{
   1.713 +		m_frame_hooks[i](simulating);
   1.714 +	}
   1.715 +}
   1.716 +
   1.717 +SMGlobalClass *SMGlobalClass::head = NULL;
   1.718 +
   1.719 +SMGlobalClass::SMGlobalClass()
   1.720 +{
   1.721 +	m_pGlobalClassNext = SMGlobalClass::head;
   1.722 +	SMGlobalClass::head = this;
   1.723 +}