core/sourcemod.cpp
author Fyren <fyrenmoo@gmail.com>
Sun Mar 01 16:41:44 2009 -0500 (2009-03-01)
changeset 2637 ed7b03e3bf16
parent 2632 f297107ad5da
child 2726 c96f3fab8211
permissions -rw-r--r--
Added case insensitivity search to ReplaceString (bug 3639, r=dvander).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 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 "LibrarySys.h"
    36 #include <sh_string.h>
    37 #include "PluginSys.h"
    38 #include "ShareSys.h"
    39 #include "CoreConfig.h"
    40 #include "Logger.h"
    41 #include "ExtensionSys.h"
    42 #include "AdminCache.h"
    43 #include "sm_stringutil.h"
    44 #include "PlayerManager.h"
    45 #include "Translator.h"
    46 #include "ForwardSys.h"
    47 #include "TimerSys.h"
    48 #include "GameConfigs.h"
    49 #include "DebugReporter.h"
    50 #include "Profiler.h"
    51 #include "frame_hooks.h"
    52 
    53 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
    54 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
    55 SH_DECL_HOOK1_void(IServerGameDLL, GameFrame, SH_NOATTRIB, false, bool);
    56 SH_DECL_HOOK1_void(IVEngineServer, ServerCommand, SH_NOATTRIB, false, const char *);
    57 
    58 SourceModBase g_SourceMod;
    59 
    60 ILibrary *g_pJIT = NULL;
    61 SourceHook::String g_BaseDir;
    62 ISourcePawnEngine *g_pSourcePawn = NULL;
    63 ISourcePawnEngine2 *g_pSourcePawn2 = NULL;
    64 IdentityToken_t *g_pCoreIdent = NULL;
    65 IForward *g_pOnMapEnd = NULL;
    66 bool g_Loaded = false;
    67 bool sm_show_debug_spew = false;
    68 
    69 typedef ISourcePawnEngine *(*GET_SP_V1)();
    70 typedef ISourcePawnEngine2 *(*GET_SP_V2)();
    71 typedef void (*NOTIFYSHUTDOWN)();
    72 
    73 #ifdef PLATFORM_WINDOWS
    74 ConVar sm_basepath("sm_basepath", "addons\\sourcemod", 0, "SourceMod base path (set via command line)");
    75 #elif defined PLATFORM_LINUX || defined PLATFORM_APPLE
    76 ConVar sm_basepath("sm_basepath", "addons/sourcemod", 0, "SourceMod base path (set via command line)");
    77 #endif
    78 
    79 void ShutdownJIT()
    80 {
    81 	NOTIFYSHUTDOWN notify = (NOTIFYSHUTDOWN)g_pJIT->GetSymbolAddress("NotifyShutdown");
    82 	if (notify)
    83 	{
    84 		notify();
    85 	}
    86 
    87 	if (g_pSourcePawn2 != NULL)
    88 	{
    89 		g_pSourcePawn2->Shutdown();
    90 	}
    91 
    92 	g_pJIT->CloseLibrary();
    93 }
    94 
    95 SourceModBase::SourceModBase()
    96 {
    97 	m_IsMapLoading = false;
    98 	m_ExecPluginReload = false;
    99 	m_GotBasePath = false;
   100 }
   101 
   102 ConfigResult SourceModBase::OnSourceModConfigChanged(const char *key, 
   103 									  const char *value, 
   104 									  ConfigSource source, 
   105 									  char *error, 
   106 									  size_t maxlength)
   107 {
   108 	if (strcasecmp(key, "BasePath") == 0)
   109 	{
   110 		if (source == ConfigSource_Console)
   111 		{
   112 			UTIL_Format(error, maxlength, "Cannot be set at runtime");
   113 			return ConfigResult_Reject;
   114 		}
   115 
   116 		if (!m_GotBasePath)
   117 		{
   118 			g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/%s", g_BaseDir.c_str(), value);
   119 			g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), value);
   120 
   121 			m_GotBasePath = true;
   122 		}
   123 
   124 		return ConfigResult_Accept;
   125 	}
   126 	else if (strcasecmp(key, "DebugSpew") == 0)
   127 	{
   128 		sm_show_debug_spew = (strcasecmp(value, "yes") == 0) ? true : false;
   129 
   130 		return ConfigResult_Accept;
   131 	}
   132 
   133 	return ConfigResult_Ignore;
   134 }
   135 
   136 bool SourceModBase::InitializeSourceMod(char *error, size_t maxlength, bool late)
   137 {
   138 	const char *gamepath = g_SMAPI->GetBaseDir();
   139 
   140 	/* Store full path to game */
   141 	g_BaseDir.assign(gamepath);
   142 
   143 	/* Store name of game directory by itself */
   144 	size_t len = strlen(gamepath);
   145 	for (size_t i = len - 1; i >= 0; i--)
   146 	{
   147 		if (gamepath[i] == PLATFORM_SEP_CHAR)
   148 		{
   149 			strncopy(m_ModDir, &gamepath[++i], sizeof(m_ModDir));
   150 			break;
   151 		}
   152 	}
   153 
   154 	const char *basepath = icvar->GetCommandLineValue("sm_basepath");
   155 	/* Set a custom base path if there is one. */
   156 	if (basepath != NULL && basepath[0] != '\0')
   157 	{
   158 		m_GotBasePath = true;
   159 	}
   160 	/* Otherwise, use a default and keep the m_GotBasePath unlocked. */
   161 	else
   162 	{
   163 		basepath = sm_basepath.GetDefault();
   164 	}
   165 
   166 	g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/%s", g_BaseDir.c_str(), basepath);
   167 	g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), "%s", basepath);
   168 
   169 	/* Initialize CoreConfig to get the SourceMod base path properly - this parses core.cfg */
   170 	g_CoreConfig.Initialize();
   171 
   172 	/* There will always be a path by this point, since it was force-set above. */
   173 	m_GotBasePath = true;
   174 
   175 	/* Attempt to load the JIT! */
   176 	char file[PLATFORM_MAX_PATH];
   177 	char myerror[255];
   178 	g_SMAPI->PathFormat(file, sizeof(file), "%s/bin/sourcepawn.jit.x86.%s",
   179 		GetSourceModPath(),
   180 		PLATFORM_LIB_EXT
   181 		);
   182 
   183 	g_pJIT = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
   184 	if (!g_pJIT)
   185 	{
   186 		if (error && maxlength)
   187 		{
   188 			snprintf(error, maxlength, "%s (failed to load bin/sourcepawn.jit.x86.%s)", 
   189 				myerror,
   190 				PLATFORM_LIB_EXT);
   191 		}
   192 		return false;
   193 	}
   194 
   195 	GET_SP_V1 getv1 = (GET_SP_V1)g_pJIT->GetSymbolAddress("GetSourcePawnEngine1");
   196 	GET_SP_V2 getv2 = (GET_SP_V2)g_pJIT->GetSymbolAddress("GetSourcePawnEngine2");
   197 
   198 	if (getv1 == NULL)
   199 	{
   200 		if (error && maxlength)
   201 		{
   202 			snprintf(error, maxlength, "JIT is too old; upgrade SourceMod");
   203 		}
   204 		ShutdownJIT();
   205 		return false;
   206 	}
   207 	else if (getv2 == NULL)
   208 	{
   209 		if (error && maxlength)
   210 		{
   211 			snprintf(error, maxlength, "JIT is too old; upgrade SourceMod");
   212 		}
   213 		ShutdownJIT();
   214 		return false;
   215 	}
   216 
   217 	g_pSourcePawn = getv1();
   218 	g_pSourcePawn2 = getv2();
   219 
   220 	if (g_pSourcePawn2->GetAPIVersion() < 3)
   221 	{
   222 		g_pSourcePawn2 = NULL;
   223 		if (error && maxlength)
   224 		{
   225 			snprintf(error, maxlength, "JIT version is out of date");
   226 		}
   227 		return false;
   228 	}
   229 
   230 	if (!g_pSourcePawn2->Initialize())
   231 	{
   232 		g_pSourcePawn2 = NULL;
   233 		if (error && maxlength)
   234 		{
   235 			snprintf(error, maxlength, "JIT could not be initialized");
   236 		}
   237 		return false;
   238 	}
   239 
   240 	g_pSourcePawn2->SetDebugListener(&g_DbgReporter);
   241 	g_pSourcePawn2->SetProfiler(&g_Profiler);
   242 
   243 	/* Hook this now so we can detect startup without calling StartSourceMod() */
   244 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
   245 
   246 	/* Only load if we're not late */
   247 	if (!late)
   248 	{
   249 		StartSourceMod(false);
   250 	}
   251 
   252 	return true;
   253 }
   254 
   255 void SourceModBase::StartSourceMod(bool late)
   256 {
   257 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
   258 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, &g_Timers, &TimerSystem::GameFrame, false);
   259 
   260 	enginePatch = SH_GET_CALLCLASS(engine);
   261 	gamedllPatch = SH_GET_CALLCLASS(gamedll);
   262 
   263 	g_ShareSys.Initialize();
   264 
   265 	/* Make the global core identity */
   266 	g_pCoreIdent = g_ShareSys.CreateCoreIdentity();
   267 
   268 	/* Notify! */
   269 	SMGlobalClass *pBase = SMGlobalClass::head;
   270 	while (pBase)
   271 	{
   272 		pBase->OnSourceModStartup(false);
   273 		pBase = pBase->m_pGlobalClassNext;
   274 	}
   275 
   276 	/* Notify! */
   277 	pBase = SMGlobalClass::head;
   278 	while (pBase)
   279 	{
   280 		pBase->OnSourceModAllInitialized();
   281 		pBase = pBase->m_pGlobalClassNext;
   282 	}
   283 
   284 	/* Notify! */
   285 	pBase = SMGlobalClass::head;
   286 	while (pBase)
   287 	{
   288 		pBase->OnSourceModAllInitialized_Post();
   289 		pBase = pBase->m_pGlobalClassNext;
   290 	}
   291 
   292 	/* Add us now... */
   293 	g_ShareSys.AddInterface(NULL, this);
   294 
   295 	/* We're loaded! */
   296 	g_Loaded = true;
   297 
   298 	/* Initialize VSP stuff */
   299 	if (vsp_interface != NULL)
   300 	{
   301 		g_SourceMod_Core.OnVSPListening(vsp_interface);
   302 	}
   303 
   304 	/* If we want to autoload, do that now */
   305 	const char *disabled = GetCoreConfigValue("DisableAutoUpdate");
   306 	if (disabled == NULL || strcasecmp(disabled, "yes") != 0)
   307 	{
   308 		g_Extensions.LoadAutoExtension("updater.ext." PLATFORM_LIB_EXT);
   309 	}
   310 }
   311 
   312 static bool g_LevelEndBarrier = false;
   313 bool SourceModBase::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
   314 {
   315 	/* If we're not loaded... */
   316 	if (!g_Loaded)
   317 	{
   318 		/* Do all global initialization now */
   319 		StartSourceMod(true);
   320 	}
   321 
   322 	m_IsMapLoading = true;
   323 	m_ExecPluginReload = true;
   324 
   325 	/* Notify! */
   326 	SMGlobalClass *pBase = SMGlobalClass::head;
   327 	while (pBase)
   328 	{
   329 		pBase->OnSourceModLevelChange(pMapName);
   330 		pBase = pBase->m_pGlobalClassNext;
   331 	}
   332 
   333 	DoGlobalPluginLoads();
   334 
   335 	m_IsMapLoading = false;
   336 
   337 	/* Notify! */
   338 	pBase = SMGlobalClass::head;
   339 	while (pBase)
   340 	{
   341 		pBase->OnSourceModPluginsLoaded();
   342 		pBase = pBase->m_pGlobalClassNext;
   343 	}
   344 
   345 	if (!g_pOnMapEnd)
   346 	{
   347 		g_pOnMapEnd = g_Forwards.CreateForward("OnMapEnd", ET_Ignore, 0, NULL);
   348 	}
   349 
   350 	g_LevelEndBarrier = true;
   351 
   352 	RETURN_META_VALUE(MRES_IGNORED, true);
   353 }
   354 
   355 void SourceModBase::LevelShutdown()
   356 {
   357 	if (g_LevelEndBarrier)
   358 	{
   359 		SMGlobalClass *next = SMGlobalClass::head;
   360 		while (next)
   361 		{
   362 			next->OnSourceModLevelEnd();
   363 			next = next->m_pGlobalClassNext;
   364 		}
   365 		
   366 		if (g_pOnMapEnd != NULL)
   367 		{
   368 			g_pOnMapEnd->Execute(NULL);
   369 		}
   370 
   371 		g_Timers.RemoveMapChangeTimers();
   372 
   373 		g_LevelEndBarrier = false;
   374 	}
   375 
   376 	g_OnMapStarted = false;
   377 
   378 	if (m_ExecPluginReload)
   379 	{
   380 		g_PluginSys.ReloadOrUnloadPlugins();
   381 		m_ExecPluginReload = false;
   382 	}
   383 }
   384 
   385 bool SourceModBase::IsMapLoading() const
   386 {
   387 	return m_IsMapLoading;
   388 }
   389 
   390 void SourceModBase::DoGlobalPluginLoads()
   391 {
   392 	char config_path[PLATFORM_MAX_PATH];
   393 	char plugins_path[PLATFORM_MAX_PATH];
   394 
   395 	BuildPath(Path_SM, config_path, 
   396 		sizeof(config_path),
   397 		"configs/plugin_settings.cfg");
   398 	BuildPath(Path_SM, plugins_path,
   399 		sizeof(plugins_path),
   400 		"plugins");
   401 
   402 	/* Load any auto extensions */
   403 	g_Extensions.TryAutoload();
   404 
   405 	/* Fire the extensions ready message */
   406 	g_SMAPI->MetaFactory(SOURCEMOD_NOTICE_EXTENSIONS, NULL, NULL);
   407 
   408 	/* Load any game extension */
   409 	const char *game_ext;
   410 	if ((game_ext = g_pGameConf->GetKeyValue("GameExtension")) != NULL)
   411 	{
   412 		char path[PLATFORM_MAX_PATH];
   413 		UTIL_Format(path, sizeof(path), "%s.ext." PLATFORM_LIB_EXT, game_ext);
   414 		g_Extensions.LoadAutoExtension(path);
   415 	}
   416 
   417 	/* Run the first pass */
   418 	g_PluginSys.LoadAll_FirstPass(config_path, plugins_path);
   419 
   420 	/* Mark any extensions as loaded */
   421 	g_Extensions.MarkAllLoaded();
   422 
   423 	/* No modules yet, it's safe to call this from here */
   424 	g_PluginSys.LoadAll_SecondPass();
   425 
   426 	/* Re-mark any extensions as loaded */
   427 	g_Extensions.MarkAllLoaded();
   428 
   429 	/* Call OnAllPluginsLoaded */
   430 	g_PluginSys.AllPluginsLoaded();
   431 }
   432 
   433 size_t SourceModBase::BuildPath(PathType type, char *buffer, size_t maxlength, const char *format, ...)
   434 {
   435 	char _buffer[PLATFORM_MAX_PATH];
   436 	va_list ap;
   437 
   438 	va_start(ap, format);
   439 	vsnprintf(_buffer, PLATFORM_MAX_PATH, format, ap);
   440 	va_end(ap);
   441 
   442 	/* If we get a "file://" notation, strip off the file:// part so we're left 
   443 	 * with an absolute path.  Note that the absolute path gets returned, so 
   444 	 * usage with relative paths here is completely invalid.
   445 	 */
   446 	if (type != Path_SM_Rel && strncmp(_buffer, "file://", 7) == 0)
   447 	{
   448 		return g_LibSys.PathFormat(buffer, maxlength, "%s", &_buffer[7]);
   449 	}
   450 
   451 	const char *base = NULL;
   452 	if (type == Path_Game)
   453 	{
   454 		base = GetGamePath();
   455 	}
   456 	else if (type == Path_SM)
   457 	{
   458 		base = GetSourceModPath();
   459 	}
   460 	else if (type == Path_SM_Rel)
   461 	{
   462 		base = m_SMRelDir;
   463 	}
   464 
   465 	if (base)
   466 	{
   467 		return g_LibSys.PathFormat(buffer, maxlength, "%s/%s", base, _buffer);
   468 	}
   469 	else
   470 	{
   471 		return g_LibSys.PathFormat(buffer, maxlength, "%s", _buffer);
   472 	}
   473 }
   474 
   475 void SourceModBase::CloseSourceMod()
   476 {
   477 	/* Force a level end */
   478 	LevelShutdown();
   479 
   480 	/* Unload plugins */
   481 	g_PluginSys.Shutdown();
   482 
   483 	/* Unload extensions */
   484 	g_Extensions.Shutdown();
   485 
   486 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
   487 
   488 	if (g_Loaded)
   489 	{
   490 		if (g_pOnMapEnd)
   491 		{
   492 			g_Forwards.ReleaseForward(g_pOnMapEnd);
   493 		}
   494 	
   495 		/* Notify! */
   496 		SMGlobalClass *pBase = SMGlobalClass::head;
   497 		while (pBase)
   498 		{
   499 			pBase->OnSourceModShutdown();
   500 			pBase = pBase->m_pGlobalClassNext;
   501 		}
   502 	
   503 		/* Delete all data packs */
   504 		CStack<CDataPack *>::iterator iter;
   505 		CDataPack *pd;
   506 		for (iter=m_freepacks.begin(); iter!=m_freepacks.end(); iter++)
   507 		{
   508 			pd = (*iter);
   509 			delete pd;
   510 		}
   511 		m_freepacks.popall();
   512 	
   513 		/* Notify! */
   514 		pBase = SMGlobalClass::head;
   515 		while (pBase)
   516 		{
   517 			pBase->OnSourceModAllShutdown();
   518 			pBase = pBase->m_pGlobalClassNext;
   519 		}
   520 	
   521 		if (enginePatch)
   522 		{
   523 			SH_RELEASE_CALLCLASS(enginePatch);
   524 			enginePatch = NULL;
   525 		}
   526 	
   527 		if (gamedllPatch)
   528 		{
   529 			SH_RELEASE_CALLCLASS(gamedllPatch);
   530 			gamedllPatch = NULL;
   531 		}
   532 
   533 		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
   534 		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, &g_Timers, &TimerSystem::GameFrame, false);
   535 	}
   536 
   537 	/* Rest In Peace */
   538 	ShutdownJIT();
   539 }
   540 
   541 void SourceModBase::LogMessage(IExtension *pExt, const char *format, ...)
   542 {
   543 	IExtensionInterface *pAPI = pExt->GetAPI();
   544 	const char *tag = pAPI->GetExtensionTag();
   545 	char buffer[2048];
   546 	va_list ap;
   547 
   548 	va_start(ap, format);
   549 	vsnprintf(buffer, sizeof(buffer), format, ap);
   550 	va_end(ap);
   551 
   552 	if (tag)
   553 	{
   554 		g_Logger.LogMessage("[%s] %s", tag, buffer);
   555 	} else {
   556 		g_Logger.LogMessage("%s", buffer);
   557 	}
   558 }
   559 
   560 void SourceModBase::LogError(IExtension *pExt, const char *format, ...)
   561 {
   562 	IExtensionInterface *pAPI = pExt->GetAPI();
   563 	const char *tag = pAPI->GetExtensionTag();
   564 	char buffer[2048];
   565 	va_list ap;
   566 
   567 	va_start(ap, format);
   568 	vsnprintf(buffer, sizeof(buffer), format, ap);
   569 	va_end(ap);
   570 
   571 	if (tag)
   572 	{
   573 		g_Logger.LogError("[%s] %s", tag, buffer);
   574 	} else {
   575 		g_Logger.LogError("%s", buffer);
   576 	}
   577 }
   578 
   579 size_t SourceModBase::FormatString(char *buffer, size_t maxlength, IPluginContext *pContext, const cell_t *params, unsigned int param)
   580 {
   581 	char *fmt;
   582 
   583 	pContext->LocalToString(params[param], &fmt);
   584 
   585 	int lparam = ++param;
   586 
   587 	return atcprintf(buffer, maxlength, fmt, pContext, params, &lparam);
   588 }
   589 
   590 const char *SourceModBase::GetSourceModPath() const
   591 {
   592 	return m_SMBaseDir;
   593 }
   594 
   595 const char *SourceModBase::GetGamePath() const
   596 {
   597 	return g_BaseDir.c_str();
   598 }
   599 
   600 unsigned int SourceModBase::SetGlobalTarget(unsigned int index)
   601 {
   602 	unsigned int old = m_target;
   603 	m_target = index;
   604 	return old;
   605 }
   606 
   607 unsigned int SourceModBase::GetGlobalTarget() const
   608 {
   609 	return m_target;
   610 }
   611 
   612 IDataPack *SourceModBase::CreateDataPack()
   613 {
   614 	CDataPack *pack;
   615 	if (m_freepacks.empty())
   616 	{
   617 		pack = new CDataPack;
   618 	} else {
   619 		pack = m_freepacks.front();
   620 		m_freepacks.pop();
   621 		pack->Initialize();
   622 	}
   623 	return pack;
   624 }
   625 
   626 void SourceModBase::FreeDataPack(IDataPack *pack)
   627 {
   628 	m_freepacks.push(static_cast<CDataPack *>(pack));
   629 }
   630 
   631 Handle_t SourceModBase::GetDataPackHandleType(bool readonly)
   632 {
   633 	//:TODO:
   634 	return 0;
   635 }
   636 
   637 const char *SourceModBase::GetGameFolderName() const
   638 {
   639 	return m_ModDir;
   640 }
   641 
   642 ISourcePawnEngine *SourceModBase::GetScriptingEngine()
   643 {
   644 	return g_pSourcePawn;
   645 }
   646 
   647 IVirtualMachine *SourceModBase::GetScriptingVM()
   648 {
   649 	return NULL;
   650 }
   651 
   652 void SourceModBase::AllPluginsLoaded()
   653 {
   654 	SMGlobalClass *base = SMGlobalClass::head;
   655 	while (base)
   656 	{
   657 		base->OnSourceModGameInitialized();
   658 		base = base->m_pGlobalClassNext;
   659 	}
   660 }
   661 
   662 time_t SourceModBase::GetAdjustedTime()
   663 {
   664 	return ::GetAdjustedTime();
   665 }
   666 
   667 void SourceModBase::AddGameFrameHook(GAME_FRAME_HOOK hook)
   668 {
   669 	m_frame_hooks.push_back(hook);
   670 }
   671 
   672 void SourceModBase::RemoveGameFrameHook(GAME_FRAME_HOOK hook)
   673 {
   674 	for (size_t i = 0; i < m_frame_hooks.size(); i++)
   675 	{
   676 		if (m_frame_hooks[i] == hook)
   677 		{
   678 			m_frame_hooks.erase(m_frame_hooks.iterAt(i));
   679 			return;
   680 		}
   681 	}
   682 }
   683 
   684 void SourceModBase::ProcessGameFrameHooks(bool simulating)
   685 {
   686 	if (m_frame_hooks.size() == 0)
   687 	{
   688 		return;
   689 	}
   690 
   691 	for (size_t i = 0; i < m_frame_hooks.size(); i++)
   692 	{
   693 		m_frame_hooks[i](simulating);
   694 	}
   695 }
   696 
   697 size_t SourceModBase::Format(char *buffer, size_t maxlength, const char *fmt, ...)
   698 {
   699 	size_t len;
   700 	va_list ap;
   701 
   702 	va_start(ap, fmt);
   703 	len = FormatArgs(buffer, maxlength, fmt, ap);
   704 	va_end(ap);
   705 
   706 	return len;
   707 }
   708 
   709 size_t SourceModBase::FormatArgs(char *buffer,
   710 								 size_t maxlength,
   711 								 const char *fmt,
   712 								 va_list ap)
   713 {
   714 	return UTIL_FormatArgs(buffer, maxlength, fmt, ap);
   715 }
   716 
   717 void SourceModBase::AddFrameAction(FRAMEACTION fn, void *data)
   718 {
   719 	::AddFrameAction(FrameAction(fn, data));
   720 }
   721 
   722 const char *SourceModBase::GetCoreConfigValue(const char *key)
   723 {
   724 	return g_CoreConfig.GetCoreConfigValue(key);
   725 }
   726 
   727 int SourceModBase::GetPluginId()
   728 {
   729 	return g_PLID;
   730 }
   731 
   732 SMGlobalClass *SMGlobalClass::head = NULL;
   733 
   734 SMGlobalClass::SMGlobalClass()
   735 {
   736 	m_pGlobalClassNext = SMGlobalClass::head;
   737 	SMGlobalClass::head = this;
   738 }