core/sourcemod.cpp
author Scott Ehlert <ds@alliedmods.net>
Sat Jul 12 09:05:19 2008 +0000 (2008-07-12)
changeset 2201 d2b8bc01ccd8
parent 2193 db7c6362f3f2
child 2226 d10b38524be3
permissions -rw-r--r--
Builds against latest OB SDK now
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#include <stdio.h>
ds@1952
    33
#include "sourcemod.h"
ds@1952
    34
#include "sourcemm_api.h"
ds@1952
    35
#include "systems/LibrarySys.h"
ds@1952
    36
#include <sh_string.h>
ds@1952
    37
#include "PluginSys.h"
ds@1952
    38
#include "ShareSys.h"
ds@1952
    39
#include "CoreConfig.h"
ds@1952
    40
#include "Logger.h"
ds@1952
    41
#include "ExtensionSys.h"
ds@1952
    42
#include "AdminCache.h"
ds@1952
    43
#include "sm_stringutil.h"
ds@1952
    44
#include "PlayerManager.h"
ds@1952
    45
#include "Translator.h"
ds@1952
    46
#include "ForwardSys.h"
ds@1952
    47
#include "TimerSys.h"
ds@1952
    48
#include "GameConfigs.h"
dvander@2193
    49
#include "DebugReporter.h"
dvander@2193
    50
#include "Profiler.h"
ds@1952
    51
ds@1952
    52
SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
ds@1952
    53
SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
ds@1952
    54
SH_DECL_HOOK1_void(IServerGameDLL, GameFrame, SH_NOATTRIB, false, bool);
ds@1952
    55
SH_DECL_HOOK1_void(IVEngineServer, ServerCommand, SH_NOATTRIB, false, const char *);
ds@1952
    56
ds@1952
    57
SourceModBase g_SourceMod;
ds@1952
    58
ds@1952
    59
ILibrary *g_pJIT = NULL;
ds@1952
    60
SourceHook::String g_BaseDir;
dvander@2193
    61
ISourcePawnEngine *g_pSourcePawn = NULL;
dvander@2193
    62
ISourcePawnEngine2 *g_pSourcePawn2 = NULL;
ds@1952
    63
IdentityToken_t *g_pCoreIdent = NULL;
ds@1952
    64
IForward *g_pOnMapEnd = NULL;
ds@1952
    65
bool g_Loaded = false;
ds@1952
    66
dvander@2193
    67
typedef ISourcePawnEngine *(*GET_SP_V1)();
dvander@2193
    68
typedef ISourcePawnEngine2 *(*GET_SP_V2)();
ds@1952
    69
typedef void (*NOTIFYSHUTDOWN)();
ds@1952
    70
ds@1952
    71
void ShutdownJIT()
ds@1952
    72
{
ds@1952
    73
	NOTIFYSHUTDOWN notify = (NOTIFYSHUTDOWN)g_pJIT->GetSymbolAddress("NotifyShutdown");
ds@1952
    74
	if (notify)
ds@1952
    75
	{
ds@1952
    76
		notify();
ds@1952
    77
	}
ds@1952
    78
ds@1952
    79
	g_pJIT->CloseLibrary();
ds@1952
    80
}
ds@1952
    81
ds@1952
    82
SourceModBase::SourceModBase()
ds@1952
    83
{
ds@1952
    84
	m_IsMapLoading = false;
ds@1952
    85
	m_ExecPluginReload = false;
ds@1952
    86
	m_GotBasePath = false;
ds@1952
    87
}
ds@1952
    88
ds@1952
    89
ConfigResult SourceModBase::OnSourceModConfigChanged(const char *key, 
ds@1952
    90
									  const char *value, 
ds@1952
    91
									  ConfigSource source, 
ds@1952
    92
									  char *error, 
ds@1952
    93
									  size_t maxlength)
ds@1952
    94
{
ds@1952
    95
	if (strcasecmp(key, "BasePath") == 0)
ds@1952
    96
	{
ds@1952
    97
		if (source == ConfigSource_Console)
ds@1952
    98
		{
ds@1952
    99
			UTIL_Format(error, maxlength, "Cannot be set at runtime");
ds@1952
   100
			return ConfigResult_Reject;
ds@1952
   101
		}
ds@1952
   102
ds@1952
   103
		if (!m_GotBasePath)
ds@1952
   104
		{
ds@1952
   105
			g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/%s", g_BaseDir.c_str(), value);
ds@1952
   106
			g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), value);
ds@1952
   107
ds@1952
   108
			m_GotBasePath = true;
ds@1952
   109
		}
ds@1952
   110
ds@1952
   111
		return ConfigResult_Accept;
ds@1952
   112
	}
ds@1952
   113
ds@1952
   114
	return ConfigResult_Ignore;
ds@1952
   115
}
ds@1952
   116
ds@1952
   117
bool SourceModBase::InitializeSourceMod(char *error, size_t maxlength, bool late)
ds@1952
   118
{
ds@1952
   119
	const char *gamepath = g_SMAPI->GetBaseDir();
ds@1952
   120
ds@1952
   121
	/* Store full path to game */
ds@1952
   122
	g_BaseDir.assign(gamepath);
ds@1952
   123
ds@1952
   124
	/* Store name of game directory by itself */
ds@1952
   125
	size_t len = strlen(gamepath);
ds@1952
   126
	for (size_t i = len - 1; i >= 0; i--)
ds@1952
   127
	{
ds@1952
   128
		if (gamepath[i] == PLATFORM_SEP_CHAR)
ds@1952
   129
		{
ds@1952
   130
			strncopy(m_ModDir, &gamepath[++i], sizeof(m_ModDir));
ds@1952
   131
			break;
ds@1952
   132
		}
ds@1952
   133
	}
ds@1952
   134
ds@1952
   135
	/* Initialize CoreConfig so we can get SourceMod base path properly - this basically parses core.cfg */
ds@1952
   136
	g_CoreConfig.Initialize();
ds@1952
   137
ds@1952
   138
	/* This shouldn't happen, but can't hurt to be safe */
ds@1952
   139
	if (!g_LibSys.PathExists(m_SMBaseDir) || !m_GotBasePath)
ds@1952
   140
	{
ds@1952
   141
		g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/addons/sourcemod", g_BaseDir.c_str());
ds@1952
   142
		g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), "addons/sourcemod");
ds@1952
   143
		m_GotBasePath = true;
ds@1952
   144
	}
ds@1952
   145
ds@1952
   146
	/* Attempt to load the JIT! */
ds@1952
   147
	char file[PLATFORM_MAX_PATH];
ds@1952
   148
	char myerror[255];
ds@1952
   149
	g_SMAPI->PathFormat(file, sizeof(file), "%s/bin/sourcepawn.jit.x86.%s",
ds@1952
   150
		GetSourceModPath(),
ds@1952
   151
		PLATFORM_LIB_EXT
ds@1952
   152
		);
ds@1952
   153
ds@1952
   154
	g_pJIT = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
ds@1952
   155
	if (!g_pJIT)
ds@1952
   156
	{
ds@1952
   157
		if (error && maxlength)
ds@1952
   158
		{
ds@1952
   159
			snprintf(error, maxlength, "%s (failed to load bin/sourcepawn.jit.x86.%s)", 
ds@1952
   160
				myerror,
ds@1952
   161
				PLATFORM_LIB_EXT);
ds@1952
   162
		}
ds@1952
   163
		return false;
ds@1952
   164
	}
ds@1952
   165
dvander@2193
   166
	GET_SP_V1 getv1 = (GET_SP_V1)g_pJIT->GetSymbolAddress("GetSourcePawnEngine1");
dvander@2193
   167
	GET_SP_V2 getv2 = (GET_SP_V2)g_pJIT->GetSymbolAddress("GetSourcePawnEngine2");
ds@1952
   168
dvander@2193
   169
	if (getv1 == NULL)
dvander@2086
   170
	{
ds@1952
   171
		if (error && maxlength)
ds@1952
   172
		{
dvander@2193
   173
			snprintf(error, maxlength, "JIT is too old; upgrade SourceMod");
ds@1952
   174
		}
dvander@2193
   175
		ShutdownJIT();
dvander@2193
   176
		return false;
dvander@2193
   177
	}
dvander@2193
   178
	else if (getv2 == NULL)
dvander@2193
   179
	{
dvander@2193
   180
		if (error && maxlength)
dvander@2193
   181
		{
dvander@2193
   182
			snprintf(error, maxlength, "JIT is too old; upgrade SourceMod");
dvander@2193
   183
		}
dvander@2193
   184
		ShutdownJIT();
ds@1952
   185
		return false;
ds@1952
   186
	}
ds@1952
   187
dvander@2193
   188
	g_pSourcePawn = getv1();
dvander@2193
   189
	g_pSourcePawn2 = getv2();
ds@1952
   190
dvander@2193
   191
	g_pSourcePawn2->SetDebugListener(&g_DbgReporter);
dvander@2193
   192
	g_pSourcePawn2->SetProfiler(&g_Profiler);
ds@1952
   193
ds@1952
   194
	/* Hook this now so we can detect startup without calling StartSourceMod() */
ds@1952
   195
	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
ds@1952
   196
ds@1952
   197
	/* Only load if we're not late */
ds@1952
   198
	if (!late)
ds@1952
   199
	{
ds@1952
   200
		StartSourceMod(false);
ds@1952
   201
	}
ds@1952
   202
ds@1952
   203
	return true;
ds@1952
   204
}
ds@1952
   205
ds@1952
   206
void SourceModBase::StartSourceMod(bool late)
ds@1952
   207
{
ds@1952
   208
	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
ds@1952
   209
	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, &g_Timers, &TimerSystem::GameFrame, false);
ds@1952
   210
ds@1952
   211
	enginePatch = SH_GET_CALLCLASS(engine);
ds@1952
   212
	gamedllPatch = SH_GET_CALLCLASS(gamedll);
ds@1952
   213
ds@1952
   214
	g_ShareSys.Initialize();
ds@1952
   215
ds@1952
   216
	/* Make the global core identity */
ds@1952
   217
	g_pCoreIdent = g_ShareSys.CreateCoreIdentity();
ds@1952
   218
ds@1952
   219
	/* Notify! */
ds@1952
   220
	SMGlobalClass *pBase = SMGlobalClass::head;
ds@1952
   221
	while (pBase)
ds@1952
   222
	{
ds@1952
   223
		pBase->OnSourceModStartup(false);
ds@1952
   224
		pBase = pBase->m_pGlobalClassNext;
ds@1952
   225
	}
ds@1952
   226
ds@1952
   227
	/* Notify! */
ds@1952
   228
	pBase = SMGlobalClass::head;
ds@1952
   229
	while (pBase)
ds@1952
   230
	{
ds@1952
   231
		pBase->OnSourceModAllInitialized();
ds@1952
   232
		pBase = pBase->m_pGlobalClassNext;
ds@1952
   233
	}
ds@1952
   234
ds@1952
   235
	/* Notify! */
ds@1952
   236
	pBase = SMGlobalClass::head;
ds@1952
   237
	while (pBase)
ds@1952
   238
	{
ds@1952
   239
		pBase->OnSourceModAllInitialized_Post();
ds@1952
   240
		pBase = pBase->m_pGlobalClassNext;
ds@1952
   241
	}
ds@1952
   242
ds@1952
   243
	/* Add us now... */
ds@1952
   244
	g_ShareSys.AddInterface(NULL, this);
ds@1952
   245
ds@1952
   246
	/* We're loaded! */
ds@1952
   247
	g_Loaded = true;
ds@1952
   248
ds@1952
   249
	/* Initialize VSP stuff */
ds@1952
   250
	if (vsp_interface != NULL)
ds@1952
   251
	{
ds@1952
   252
		g_SourceMod_Core.OnVSPListening(vsp_interface);
ds@1952
   253
	}
ds@1952
   254
}
ds@1952
   255
ds@1952
   256
static bool g_LevelEndBarrier = false;
ds@1952
   257
bool SourceModBase::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
ds@1952
   258
{
ds@1952
   259
	/* If we're not loaded... */
ds@1952
   260
	if (!g_Loaded)
ds@1952
   261
	{
ds@1952
   262
		/* Do all global initialization now */
ds@1952
   263
		StartSourceMod(true);
ds@1952
   264
	}
ds@1952
   265
ds@1952
   266
	m_IsMapLoading = true;
ds@1952
   267
	m_ExecPluginReload = true;
ds@1952
   268
ds@1952
   269
	/* Notify! */
ds@1952
   270
	SMGlobalClass *pBase = SMGlobalClass::head;
ds@1952
   271
	while (pBase)
ds@1952
   272
	{
ds@1952
   273
		pBase->OnSourceModLevelChange(pMapName);
ds@1952
   274
		pBase = pBase->m_pGlobalClassNext;
ds@1952
   275
	}
ds@1952
   276
ds@1952
   277
	DoGlobalPluginLoads();
ds@1952
   278
ds@1952
   279
	m_IsMapLoading = false;
ds@1952
   280
ds@1952
   281
	/* Notify! */
ds@1952
   282
	pBase = SMGlobalClass::head;
ds@1952
   283
	while (pBase)
ds@1952
   284
	{
ds@1952
   285
		pBase->OnSourceModPluginsLoaded();
ds@1952
   286
		pBase = pBase->m_pGlobalClassNext;
ds@1952
   287
	}
ds@1952
   288
ds@1952
   289
	if (!g_pOnMapEnd)
ds@1952
   290
	{
ds@1952
   291
		g_pOnMapEnd = g_Forwards.CreateForward("OnMapEnd", ET_Ignore, 0, NULL);
ds@1952
   292
	}
ds@1952
   293
ds@1952
   294
	g_LevelEndBarrier = true;
ds@1952
   295
ds@1952
   296
	RETURN_META_VALUE(MRES_IGNORED, true);
ds@1952
   297
}
ds@1952
   298
ds@1952
   299
void SourceModBase::LevelShutdown()
ds@1952
   300
{
ds@1952
   301
	if (g_LevelEndBarrier)
ds@1952
   302
	{
ds@1952
   303
		SMGlobalClass *next = SMGlobalClass::head;
ds@1952
   304
		while (next)
ds@1952
   305
		{
ds@1952
   306
			next->OnSourceModLevelEnd();
ds@1952
   307
			next = next->m_pGlobalClassNext;
ds@1952
   308
		}
ds@1952
   309
		
ds@1952
   310
		if (g_pOnMapEnd != NULL)
ds@1952
   311
		{
ds@1952
   312
			g_pOnMapEnd->Execute(NULL);
ds@1952
   313
		}
ds@1952
   314
ds@1952
   315
		g_Timers.RemoveMapChangeTimers();
ds@1952
   316
ds@1952
   317
		g_LevelEndBarrier = false;
ds@1952
   318
	}
ds@1952
   319
ds@1952
   320
	g_OnMapStarted = false;
ds@1952
   321
ds@1952
   322
	if (m_ExecPluginReload)
ds@1952
   323
	{
ds@1952
   324
		g_PluginSys.ReloadOrUnloadPlugins();
ds@1952
   325
		m_ExecPluginReload = false;
ds@1952
   326
	}
ds@1952
   327
}
ds@1952
   328
ds@1952
   329
bool SourceModBase::IsMapLoading() const
ds@1952
   330
{
ds@1952
   331
	return m_IsMapLoading;
ds@1952
   332
}
ds@1952
   333
ds@1952
   334
void SourceModBase::DoGlobalPluginLoads()
ds@1952
   335
{
ds@1952
   336
	char config_path[PLATFORM_MAX_PATH];
ds@1952
   337
	char plugins_path[PLATFORM_MAX_PATH];
ds@1952
   338
ds@1952
   339
	BuildPath(Path_SM, config_path, 
ds@1952
   340
		sizeof(config_path),
ds@1952
   341
		"configs/plugin_settings.cfg");
ds@1952
   342
	BuildPath(Path_SM, plugins_path,
ds@1952
   343
		sizeof(plugins_path),
ds@1952
   344
		"plugins");
ds@1952
   345
ds@1952
   346
	/* Load any auto extensions */
ds@1952
   347
	g_Extensions.TryAutoload();
ds@1952
   348
ds@1952
   349
	/* Fire the extensions ready message */
ds@1952
   350
	g_SMAPI->MetaFactory(SOURCEMOD_NOTICE_EXTENSIONS, NULL, NULL);	
ds@1952
   351
ds@1952
   352
	/* Load any game extension */
ds@1952
   353
	const char *game_ext;
ds@1952
   354
	if ((game_ext = g_pGameConf->GetKeyValue("GameExtension")) != NULL)
ds@1952
   355
	{
ds@1952
   356
		char path[PLATFORM_MAX_PATH];
ds@1952
   357
		UTIL_Format(path, sizeof(path), "%s.ext." PLATFORM_LIB_EXT, game_ext);
ds@1952
   358
		g_Extensions.LoadAutoExtension(path);
ds@1952
   359
	}
ds@1952
   360
ds@1952
   361
	/* Run the first pass */
ds@1952
   362
	g_PluginSys.LoadAll_FirstPass(config_path, plugins_path);
ds@1952
   363
ds@1952
   364
	/* Mark any extensions as loaded */
ds@1952
   365
	g_Extensions.MarkAllLoaded();
ds@1952
   366
ds@1952
   367
	/* No modules yet, it's safe to call this from here */
ds@1952
   368
	g_PluginSys.LoadAll_SecondPass();
ds@1952
   369
ds@1952
   370
	/* Re-mark any extensions as loaded */
ds@1952
   371
	g_Extensions.MarkAllLoaded();
ds@1952
   372
ds@1952
   373
	/* Call OnAllPluginsLoaded */
ds@1952
   374
	g_PluginSys.AllPluginsLoaded();
ds@1952
   375
}
ds@1952
   376
ds@1952
   377
size_t SourceModBase::BuildPath(PathType type, char *buffer, size_t maxlength, const char *format, ...)
ds@1952
   378
{
ds@1952
   379
	char _buffer[PLATFORM_MAX_PATH];
ds@1952
   380
	va_list ap;
ds@1952
   381
ds@1952
   382
	va_start(ap, format);
ds@1952
   383
	vsnprintf(_buffer, PLATFORM_MAX_PATH, format, ap);
ds@1952
   384
	va_end(ap);
ds@1952
   385
ds@1952
   386
	/* If we get a "file://" notation, strip off the file:// part so we're left 
ds@1952
   387
	 * with an absolute path.  Note that the absolute path gets returned, so 
ds@1952
   388
	 * usage with relative paths here is completely invalid.
ds@1952
   389
	 */
ds@1952
   390
	if (type != Path_SM_Rel && strncmp(_buffer, "file://", 7) == 0)
ds@1952
   391
	{
ds@1952
   392
		return g_LibSys.PathFormat(buffer, maxlength, "%s", &_buffer[7]);
ds@1952
   393
	}
ds@1952
   394
ds@1952
   395
	const char *base = NULL;
ds@1952
   396
	if (type == Path_Game)
ds@1952
   397
	{
ds@1952
   398
		base = GetGamePath();
ds@1952
   399
	}
ds@1952
   400
	else if (type == Path_SM)
ds@1952
   401
	{
ds@1952
   402
		base = GetSourceModPath();
ds@1952
   403
	}
ds@1952
   404
	else if (type == Path_SM_Rel)
ds@1952
   405
	{
ds@1952
   406
		base = m_SMRelDir;
ds@1952
   407
	}
ds@1952
   408
ds@1952
   409
	if (base)
ds@1952
   410
	{
ds@1952
   411
		return g_LibSys.PathFormat(buffer, maxlength, "%s/%s", base, _buffer);
ds@1952
   412
	}
ds@1952
   413
	else
ds@1952
   414
	{
ds@1952
   415
		return g_LibSys.PathFormat(buffer, maxlength, "%s", _buffer);
ds@1952
   416
	}
ds@1952
   417
}
ds@1952
   418
ds@1952
   419
void SourceModBase::CloseSourceMod()
ds@1952
   420
{
ds@1952
   421
	/* Force a level end */
ds@1952
   422
	LevelShutdown();
ds@1952
   423
ds@1952
   424
	/* Unload plugins */
ds@1952
   425
	g_PluginSys.Shutdown();
ds@1952
   426
ds@1952
   427
	/* Unload extensions */
ds@1952
   428
	g_Extensions.Shutdown();
ds@1952
   429
ds@1952
   430
	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
ds@1952
   431
ds@1952
   432
	if (g_Loaded)
ds@1952
   433
	{
ds@1952
   434
		if (g_pOnMapEnd)
ds@1952
   435
		{
ds@1952
   436
			g_Forwards.ReleaseForward(g_pOnMapEnd);
ds@1952
   437
		}
ds@1952
   438
	
ds@1952
   439
		/* Notify! */
ds@1952
   440
		SMGlobalClass *pBase = SMGlobalClass::head;
ds@1952
   441
		while (pBase)
ds@1952
   442
		{
ds@1952
   443
			pBase->OnSourceModShutdown();
ds@1952
   444
			pBase = pBase->m_pGlobalClassNext;
ds@1952
   445
		}
ds@1952
   446
	
ds@1952
   447
		/* Delete all data packs */
ds@1952
   448
		CStack<CDataPack *>::iterator iter;
ds@1952
   449
		CDataPack *pd;
ds@1952
   450
		for (iter=m_freepacks.begin(); iter!=m_freepacks.end(); iter++)
ds@1952
   451
		{
ds@1952
   452
			pd = (*iter);
ds@1952
   453
			delete pd;
ds@1952
   454
		}
ds@1952
   455
		m_freepacks.popall();
ds@1952
   456
	
ds@1952
   457
		/* Notify! */
ds@1952
   458
		pBase = SMGlobalClass::head;
ds@1952
   459
		while (pBase)
ds@1952
   460
		{
ds@1952
   461
			pBase->OnSourceModAllShutdown();
ds@1952
   462
			pBase = pBase->m_pGlobalClassNext;
ds@1952
   463
		}
ds@1952
   464
	
ds@1952
   465
		if (enginePatch)
ds@1952
   466
		{
ds@1952
   467
			SH_RELEASE_CALLCLASS(enginePatch);
ds@1952
   468
			enginePatch = NULL;
ds@1952
   469
		}
ds@1952
   470
	
ds@1952
   471
		if (gamedllPatch)
ds@1952
   472
		{
ds@1952
   473
			SH_RELEASE_CALLCLASS(gamedllPatch);
ds@1952
   474
			gamedllPatch = NULL;
ds@1952
   475
		}
ds@1952
   476
ds@1952
   477
		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
ds@1952
   478
		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, &g_Timers, &TimerSystem::GameFrame, false);
ds@1952
   479
	}
ds@1952
   480
ds@1952
   481
	/* Rest In Peace */
ds@1952
   482
	ShutdownJIT();
ds@1952
   483
}
ds@1952
   484
ds@1952
   485
void SourceModBase::LogMessage(IExtension *pExt, const char *format, ...)
ds@1952
   486
{
ds@1952
   487
	IExtensionInterface *pAPI = pExt->GetAPI();
ds@1952
   488
	const char *tag = pAPI->GetExtensionTag();
ds@1952
   489
	char buffer[2048];
ds@1952
   490
	va_list ap;
ds@1952
   491
ds@1952
   492
	va_start(ap, format);
ds@1952
   493
	vsnprintf(buffer, sizeof(buffer), format, ap);
ds@1952
   494
	va_end(ap);
ds@1952
   495
ds@1952
   496
	if (tag)
ds@1952
   497
	{
ds@1952
   498
		g_Logger.LogMessage("[%s] %s", tag, buffer);
ds@1952
   499
	} else {
ds@1952
   500
		g_Logger.LogMessage("%s", buffer);
ds@1952
   501
	}
ds@1952
   502
}
ds@1952
   503
ds@1952
   504
void SourceModBase::LogError(IExtension *pExt, const char *format, ...)
ds@1952
   505
{
ds@1952
   506
	IExtensionInterface *pAPI = pExt->GetAPI();
ds@1952
   507
	const char *tag = pAPI->GetExtensionTag();
ds@1952
   508
	char buffer[2048];
ds@1952
   509
	va_list ap;
ds@1952
   510
ds@1952
   511
	va_start(ap, format);
ds@1952
   512
	vsnprintf(buffer, sizeof(buffer), format, ap);
ds@1952
   513
	va_end(ap);
ds@1952
   514
ds@1952
   515
	if (tag)
ds@1952
   516
	{
ds@1952
   517
		g_Logger.LogError("[%s] %s", tag, buffer);
ds@1952
   518
	} else {
ds@1952
   519
		g_Logger.LogError("%s", buffer);
ds@1952
   520
	}
ds@1952
   521
}
ds@1952
   522
ds@1952
   523
size_t SourceModBase::FormatString(char *buffer, size_t maxlength, IPluginContext *pContext, const cell_t *params, unsigned int param)
ds@1952
   524
{
ds@1952
   525
	char *fmt;
ds@1952
   526
ds@1952
   527
	pContext->LocalToString(params[param], &fmt);
ds@1952
   528
ds@1952
   529
	int lparam = ++param;
ds@1952
   530
ds@1952
   531
	return atcprintf(buffer, maxlength, fmt, pContext, params, &lparam);
ds@1952
   532
}
ds@1952
   533
ds@1952
   534
const char *SourceModBase::GetSourceModPath() const
ds@1952
   535
{
ds@1952
   536
	return m_SMBaseDir;
ds@1952
   537
}
ds@1952
   538
ds@1952
   539
const char *SourceModBase::GetGamePath() const
ds@1952
   540
{
ds@1952
   541
	return g_BaseDir.c_str();
ds@1952
   542
}
ds@1952
   543
ds@1952
   544
unsigned int SourceModBase::SetGlobalTarget(unsigned int index)
ds@1952
   545
{
ds@1952
   546
	unsigned int old = m_target;
ds@1952
   547
	m_target = index;
ds@1952
   548
	return old;
ds@1952
   549
}
ds@1952
   550
ds@1952
   551
unsigned int SourceModBase::GetGlobalTarget() const
ds@1952
   552
{
ds@1952
   553
	return m_target;
ds@1952
   554
}
ds@1952
   555
ds@1952
   556
IDataPack *SourceModBase::CreateDataPack()
ds@1952
   557
{
ds@1952
   558
	CDataPack *pack;
ds@1952
   559
	if (m_freepacks.empty())
ds@1952
   560
	{
ds@1952
   561
		pack = new CDataPack;
ds@1952
   562
	} else {
ds@1952
   563
		pack = m_freepacks.front();
ds@1952
   564
		m_freepacks.pop();
ds@1952
   565
		pack->Initialize();
ds@1952
   566
	}
ds@1952
   567
	return pack;
ds@1952
   568
}
ds@1952
   569
ds@1952
   570
void SourceModBase::FreeDataPack(IDataPack *pack)
ds@1952
   571
{
ds@1952
   572
	m_freepacks.push(static_cast<CDataPack *>(pack));
ds@1952
   573
}
ds@1952
   574
ds@1952
   575
Handle_t SourceModBase::GetDataPackHandleType(bool readonly)
ds@1952
   576
{
ds@1952
   577
	//:TODO:
ds@1952
   578
	return 0;
ds@1952
   579
}
ds@1952
   580
ds@1952
   581
const char *SourceModBase::GetGameFolderName() const
ds@1952
   582
{
ds@1952
   583
	return m_ModDir;
ds@1952
   584
}
ds@1952
   585
ds@1952
   586
ISourcePawnEngine *SourceModBase::GetScriptingEngine()
ds@1952
   587
{
ds@1952
   588
	return g_pSourcePawn;
ds@1952
   589
}
ds@1952
   590
ds@1952
   591
IVirtualMachine *SourceModBase::GetScriptingVM()
ds@1952
   592
{
dvander@2193
   593
	return NULL;
ds@1952
   594
}
ds@1952
   595
ds@1952
   596
void SourceModBase::AllPluginsLoaded()
ds@1952
   597
{
ds@1952
   598
	SMGlobalClass *base = SMGlobalClass::head;
ds@1952
   599
	while (base)
ds@1952
   600
	{
ds@1952
   601
		base->OnSourceModGameInitialized();
ds@1952
   602
		base = base->m_pGlobalClassNext;
ds@1952
   603
	}
ds@1952
   604
}
ds@1952
   605
ds@1952
   606
time_t SourceModBase::GetAdjustedTime()
ds@1952
   607
{
ds@1952
   608
	return ::GetAdjustedTime();
ds@1952
   609
}
ds@1952
   610
ds@1952
   611
void SourceModBase::AddGameFrameHook(GAME_FRAME_HOOK hook)
ds@1952
   612
{
ds@1952
   613
	m_frame_hooks.push_back(hook);
ds@1952
   614
}
ds@1952
   615
ds@1952
   616
void SourceModBase::RemoveGameFrameHook(GAME_FRAME_HOOK hook)
ds@1952
   617
{
ds@1952
   618
	for (size_t i = 0; i < m_frame_hooks.size(); i++)
ds@1952
   619
	{
ds@1952
   620
		if (m_frame_hooks[i] == hook)
ds@1952
   621
		{
ds@1952
   622
			m_frame_hooks.erase(m_frame_hooks.iterAt(i));
ds@1952
   623
			return;
ds@1952
   624
		}
ds@1952
   625
	}
ds@1952
   626
}
ds@1952
   627
ds@1952
   628
void SourceModBase::ProcessGameFrameHooks(bool simulating)
ds@1952
   629
{
ds@1952
   630
	if (m_frame_hooks.size() == 0)
ds@1952
   631
	{
ds@1952
   632
		return;
ds@1952
   633
	}
ds@1952
   634
ds@1952
   635
	for (size_t i = 0; i < m_frame_hooks.size(); i++)
ds@1952
   636
	{
ds@1952
   637
		m_frame_hooks[i](simulating);
ds@1952
   638
	}
ds@1952
   639
}
ds@1952
   640
ds@1952
   641
SMGlobalClass *SMGlobalClass::head = NULL;
ds@1952
   642
ds@1952
   643
SMGlobalClass::SMGlobalClass()
ds@1952
   644
{
ds@1952
   645
	m_pGlobalClassNext = SMGlobalClass::head;
ds@1952
   646
	SMGlobalClass::head = this;
ds@1952
   647
}