core/sourcemod.cpp
author Scott Ehlert <ds@alliedmods.net>
Sun Sep 14 02:59:26 2008 -0500 (2008-09-14)
changeset 2288 15c77b21589a
parent 2226 d10b38524be3
child 2289 44045ead61cd
permissions -rw-r--r--
Eliminated head for dead branch: refac-jit
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
dvander@2226
    79
	if (g_pSourcePawn2 != NULL)
dvander@2226
    80
	{
dvander@2226
    81
		g_pSourcePawn2->Shutdown();
dvander@2226
    82
	}
dvander@2226
    83
ds@1952
    84
	g_pJIT->CloseLibrary();
ds@1952
    85
}
ds@1952
    86
ds@1952
    87
SourceModBase::SourceModBase()
ds@1952
    88
{
ds@1952
    89
	m_IsMapLoading = false;
ds@1952
    90
	m_ExecPluginReload = false;
ds@1952
    91
	m_GotBasePath = false;
ds@1952
    92
}
ds@1952
    93
ds@1952
    94
ConfigResult SourceModBase::OnSourceModConfigChanged(const char *key, 
ds@1952
    95
									  const char *value, 
ds@1952
    96
									  ConfigSource source, 
ds@1952
    97
									  char *error, 
ds@1952
    98
									  size_t maxlength)
ds@1952
    99
{
ds@1952
   100
	if (strcasecmp(key, "BasePath") == 0)
ds@1952
   101
	{
ds@1952
   102
		if (source == ConfigSource_Console)
ds@1952
   103
		{
ds@1952
   104
			UTIL_Format(error, maxlength, "Cannot be set at runtime");
ds@1952
   105
			return ConfigResult_Reject;
ds@1952
   106
		}
ds@1952
   107
ds@1952
   108
		if (!m_GotBasePath)
ds@1952
   109
		{
ds@1952
   110
			g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/%s", g_BaseDir.c_str(), value);
ds@1952
   111
			g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), value);
ds@1952
   112
ds@1952
   113
			m_GotBasePath = true;
ds@1952
   114
		}
ds@1952
   115
ds@1952
   116
		return ConfigResult_Accept;
ds@1952
   117
	}
ds@1952
   118
ds@1952
   119
	return ConfigResult_Ignore;
ds@1952
   120
}
ds@1952
   121
ds@1952
   122
bool SourceModBase::InitializeSourceMod(char *error, size_t maxlength, bool late)
ds@1952
   123
{
ds@1952
   124
	const char *gamepath = g_SMAPI->GetBaseDir();
ds@1952
   125
ds@1952
   126
	/* Store full path to game */
ds@1952
   127
	g_BaseDir.assign(gamepath);
ds@1952
   128
ds@1952
   129
	/* Store name of game directory by itself */
ds@1952
   130
	size_t len = strlen(gamepath);
ds@1952
   131
	for (size_t i = len - 1; i >= 0; i--)
ds@1952
   132
	{
ds@1952
   133
		if (gamepath[i] == PLATFORM_SEP_CHAR)
ds@1952
   134
		{
ds@1952
   135
			strncopy(m_ModDir, &gamepath[++i], sizeof(m_ModDir));
ds@1952
   136
			break;
ds@1952
   137
		}
ds@1952
   138
	}
ds@1952
   139
ds@1952
   140
	/* Initialize CoreConfig so we can get SourceMod base path properly - this basically parses core.cfg */
ds@1952
   141
	g_CoreConfig.Initialize();
ds@1952
   142
ds@1952
   143
	/* This shouldn't happen, but can't hurt to be safe */
ds@1952
   144
	if (!g_LibSys.PathExists(m_SMBaseDir) || !m_GotBasePath)
ds@1952
   145
	{
ds@1952
   146
		g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/addons/sourcemod", g_BaseDir.c_str());
ds@1952
   147
		g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), "addons/sourcemod");
ds@1952
   148
		m_GotBasePath = true;
ds@1952
   149
	}
ds@1952
   150
ds@1952
   151
	/* Attempt to load the JIT! */
ds@1952
   152
	char file[PLATFORM_MAX_PATH];
ds@1952
   153
	char myerror[255];
ds@1952
   154
	g_SMAPI->PathFormat(file, sizeof(file), "%s/bin/sourcepawn.jit.x86.%s",
ds@1952
   155
		GetSourceModPath(),
ds@1952
   156
		PLATFORM_LIB_EXT
ds@1952
   157
		);
ds@1952
   158
ds@1952
   159
	g_pJIT = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
ds@1952
   160
	if (!g_pJIT)
ds@1952
   161
	{
ds@1952
   162
		if (error && maxlength)
ds@1952
   163
		{
ds@1952
   164
			snprintf(error, maxlength, "%s (failed to load bin/sourcepawn.jit.x86.%s)", 
ds@1952
   165
				myerror,
ds@1952
   166
				PLATFORM_LIB_EXT);
ds@1952
   167
		}
ds@1952
   168
		return false;
ds@1952
   169
	}
ds@1952
   170
dvander@2193
   171
	GET_SP_V1 getv1 = (GET_SP_V1)g_pJIT->GetSymbolAddress("GetSourcePawnEngine1");
dvander@2193
   172
	GET_SP_V2 getv2 = (GET_SP_V2)g_pJIT->GetSymbolAddress("GetSourcePawnEngine2");
ds@1952
   173
dvander@2193
   174
	if (getv1 == NULL)
dvander@2086
   175
	{
ds@1952
   176
		if (error && maxlength)
ds@1952
   177
		{
dvander@2193
   178
			snprintf(error, maxlength, "JIT is too old; upgrade SourceMod");
ds@1952
   179
		}
dvander@2193
   180
		ShutdownJIT();
dvander@2193
   181
		return false;
dvander@2193
   182
	}
dvander@2193
   183
	else if (getv2 == NULL)
dvander@2193
   184
	{
dvander@2193
   185
		if (error && maxlength)
dvander@2193
   186
		{
dvander@2193
   187
			snprintf(error, maxlength, "JIT is too old; upgrade SourceMod");
dvander@2193
   188
		}
dvander@2193
   189
		ShutdownJIT();
ds@1952
   190
		return false;
ds@1952
   191
	}
ds@1952
   192
dvander@2193
   193
	g_pSourcePawn = getv1();
dvander@2193
   194
	g_pSourcePawn2 = getv2();
ds@1952
   195
dvander@2226
   196
	if (g_pSourcePawn2->GetAPIVersion() < 2)
dvander@2226
   197
	{
dvander@2226
   198
		g_pSourcePawn2 = NULL;
dvander@2226
   199
		if (error && maxlength)
dvander@2226
   200
		{
dvander@2226
   201
			snprintf(error, maxlength, "JIT version is out of date");
dvander@2226
   202
		}
dvander@2226
   203
		return false;
dvander@2226
   204
	}
dvander@2226
   205
dvander@2226
   206
	if (!g_pSourcePawn2->Initialize())
dvander@2226
   207
	{
dvander@2226
   208
		g_pSourcePawn2 = NULL;
dvander@2226
   209
		if (error && maxlength)
dvander@2226
   210
		{
dvander@2226
   211
			snprintf(error, maxlength, "JIT could not be initialized");
dvander@2226
   212
		}
dvander@2226
   213
		return false;
dvander@2226
   214
	}
dvander@2226
   215
dvander@2193
   216
	g_pSourcePawn2->SetDebugListener(&g_DbgReporter);
dvander@2193
   217
	g_pSourcePawn2->SetProfiler(&g_Profiler);
ds@1952
   218
ds@1952
   219
	/* Hook this now so we can detect startup without calling StartSourceMod() */
ds@1952
   220
	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
ds@1952
   221
ds@1952
   222
	/* Only load if we're not late */
ds@1952
   223
	if (!late)
ds@1952
   224
	{
ds@1952
   225
		StartSourceMod(false);
ds@1952
   226
	}
ds@1952
   227
ds@1952
   228
	return true;
ds@1952
   229
}
ds@1952
   230
ds@1952
   231
void SourceModBase::StartSourceMod(bool late)
ds@1952
   232
{
ds@1952
   233
	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
ds@1952
   234
	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, &g_Timers, &TimerSystem::GameFrame, false);
ds@1952
   235
ds@1952
   236
	enginePatch = SH_GET_CALLCLASS(engine);
ds@1952
   237
	gamedllPatch = SH_GET_CALLCLASS(gamedll);
ds@1952
   238
ds@1952
   239
	g_ShareSys.Initialize();
ds@1952
   240
ds@1952
   241
	/* Make the global core identity */
ds@1952
   242
	g_pCoreIdent = g_ShareSys.CreateCoreIdentity();
ds@1952
   243
ds@1952
   244
	/* Notify! */
ds@1952
   245
	SMGlobalClass *pBase = SMGlobalClass::head;
ds@1952
   246
	while (pBase)
ds@1952
   247
	{
ds@1952
   248
		pBase->OnSourceModStartup(false);
ds@1952
   249
		pBase = pBase->m_pGlobalClassNext;
ds@1952
   250
	}
ds@1952
   251
ds@1952
   252
	/* Notify! */
ds@1952
   253
	pBase = SMGlobalClass::head;
ds@1952
   254
	while (pBase)
ds@1952
   255
	{
ds@1952
   256
		pBase->OnSourceModAllInitialized();
ds@1952
   257
		pBase = pBase->m_pGlobalClassNext;
ds@1952
   258
	}
ds@1952
   259
ds@1952
   260
	/* Notify! */
ds@1952
   261
	pBase = SMGlobalClass::head;
ds@1952
   262
	while (pBase)
ds@1952
   263
	{
ds@1952
   264
		pBase->OnSourceModAllInitialized_Post();
ds@1952
   265
		pBase = pBase->m_pGlobalClassNext;
ds@1952
   266
	}
ds@1952
   267
ds@1952
   268
	/* Add us now... */
ds@1952
   269
	g_ShareSys.AddInterface(NULL, this);
ds@1952
   270
ds@1952
   271
	/* We're loaded! */
ds@1952
   272
	g_Loaded = true;
ds@1952
   273
ds@1952
   274
	/* Initialize VSP stuff */
ds@1952
   275
	if (vsp_interface != NULL)
ds@1952
   276
	{
ds@1952
   277
		g_SourceMod_Core.OnVSPListening(vsp_interface);
ds@1952
   278
	}
ds@1952
   279
}
ds@1952
   280
ds@1952
   281
static bool g_LevelEndBarrier = false;
ds@1952
   282
bool SourceModBase::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
ds@1952
   283
{
ds@1952
   284
	/* If we're not loaded... */
ds@1952
   285
	if (!g_Loaded)
ds@1952
   286
	{
ds@1952
   287
		/* Do all global initialization now */
ds@1952
   288
		StartSourceMod(true);
ds@1952
   289
	}
ds@1952
   290
ds@1952
   291
	m_IsMapLoading = true;
ds@1952
   292
	m_ExecPluginReload = true;
ds@1952
   293
ds@1952
   294
	/* Notify! */
ds@1952
   295
	SMGlobalClass *pBase = SMGlobalClass::head;
ds@1952
   296
	while (pBase)
ds@1952
   297
	{
ds@1952
   298
		pBase->OnSourceModLevelChange(pMapName);
ds@1952
   299
		pBase = pBase->m_pGlobalClassNext;
ds@1952
   300
	}
ds@1952
   301
ds@1952
   302
	DoGlobalPluginLoads();
ds@1952
   303
ds@1952
   304
	m_IsMapLoading = false;
ds@1952
   305
ds@1952
   306
	/* Notify! */
ds@1952
   307
	pBase = SMGlobalClass::head;
ds@1952
   308
	while (pBase)
ds@1952
   309
	{
ds@1952
   310
		pBase->OnSourceModPluginsLoaded();
ds@1952
   311
		pBase = pBase->m_pGlobalClassNext;
ds@1952
   312
	}
ds@1952
   313
ds@1952
   314
	if (!g_pOnMapEnd)
ds@1952
   315
	{
ds@1952
   316
		g_pOnMapEnd = g_Forwards.CreateForward("OnMapEnd", ET_Ignore, 0, NULL);
ds@1952
   317
	}
ds@1952
   318
ds@1952
   319
	g_LevelEndBarrier = true;
ds@1952
   320
ds@1952
   321
	RETURN_META_VALUE(MRES_IGNORED, true);
ds@1952
   322
}
ds@1952
   323
ds@1952
   324
void SourceModBase::LevelShutdown()
ds@1952
   325
{
ds@1952
   326
	if (g_LevelEndBarrier)
ds@1952
   327
	{
ds@1952
   328
		SMGlobalClass *next = SMGlobalClass::head;
ds@1952
   329
		while (next)
ds@1952
   330
		{
ds@1952
   331
			next->OnSourceModLevelEnd();
ds@1952
   332
			next = next->m_pGlobalClassNext;
ds@1952
   333
		}
ds@1952
   334
		
ds@1952
   335
		if (g_pOnMapEnd != NULL)
ds@1952
   336
		{
ds@1952
   337
			g_pOnMapEnd->Execute(NULL);
ds@1952
   338
		}
ds@1952
   339
ds@1952
   340
		g_Timers.RemoveMapChangeTimers();
ds@1952
   341
ds@1952
   342
		g_LevelEndBarrier = false;
ds@1952
   343
	}
ds@1952
   344
ds@1952
   345
	g_OnMapStarted = false;
ds@1952
   346
ds@1952
   347
	if (m_ExecPluginReload)
ds@1952
   348
	{
ds@1952
   349
		g_PluginSys.ReloadOrUnloadPlugins();
ds@1952
   350
		m_ExecPluginReload = false;
ds@1952
   351
	}
ds@1952
   352
}
ds@1952
   353
ds@1952
   354
bool SourceModBase::IsMapLoading() const
ds@1952
   355
{
ds@1952
   356
	return m_IsMapLoading;
ds@1952
   357
}
ds@1952
   358
ds@1952
   359
void SourceModBase::DoGlobalPluginLoads()
ds@1952
   360
{
ds@1952
   361
	char config_path[PLATFORM_MAX_PATH];
ds@1952
   362
	char plugins_path[PLATFORM_MAX_PATH];
ds@1952
   363
ds@1952
   364
	BuildPath(Path_SM, config_path, 
ds@1952
   365
		sizeof(config_path),
ds@1952
   366
		"configs/plugin_settings.cfg");
ds@1952
   367
	BuildPath(Path_SM, plugins_path,
ds@1952
   368
		sizeof(plugins_path),
ds@1952
   369
		"plugins");
ds@1952
   370
ds@1952
   371
	/* Load any auto extensions */
ds@1952
   372
	g_Extensions.TryAutoload();
ds@1952
   373
ds@1952
   374
	/* Fire the extensions ready message */
ds@1952
   375
	g_SMAPI->MetaFactory(SOURCEMOD_NOTICE_EXTENSIONS, NULL, NULL);	
ds@1952
   376
ds@1952
   377
	/* Load any game extension */
ds@1952
   378
	const char *game_ext;
ds@1952
   379
	if ((game_ext = g_pGameConf->GetKeyValue("GameExtension")) != NULL)
ds@1952
   380
	{
ds@1952
   381
		char path[PLATFORM_MAX_PATH];
ds@1952
   382
		UTIL_Format(path, sizeof(path), "%s.ext." PLATFORM_LIB_EXT, game_ext);
ds@1952
   383
		g_Extensions.LoadAutoExtension(path);
ds@1952
   384
	}
ds@1952
   385
ds@1952
   386
	/* Run the first pass */
ds@1952
   387
	g_PluginSys.LoadAll_FirstPass(config_path, plugins_path);
ds@1952
   388
ds@1952
   389
	/* Mark any extensions as loaded */
ds@1952
   390
	g_Extensions.MarkAllLoaded();
ds@1952
   391
ds@1952
   392
	/* No modules yet, it's safe to call this from here */
ds@1952
   393
	g_PluginSys.LoadAll_SecondPass();
ds@1952
   394
ds@1952
   395
	/* Re-mark any extensions as loaded */
ds@1952
   396
	g_Extensions.MarkAllLoaded();
ds@1952
   397
ds@1952
   398
	/* Call OnAllPluginsLoaded */
ds@1952
   399
	g_PluginSys.AllPluginsLoaded();
ds@1952
   400
}
ds@1952
   401
ds@1952
   402
size_t SourceModBase::BuildPath(PathType type, char *buffer, size_t maxlength, const char *format, ...)
ds@1952
   403
{
ds@1952
   404
	char _buffer[PLATFORM_MAX_PATH];
ds@1952
   405
	va_list ap;
ds@1952
   406
ds@1952
   407
	va_start(ap, format);
ds@1952
   408
	vsnprintf(_buffer, PLATFORM_MAX_PATH, format, ap);
ds@1952
   409
	va_end(ap);
ds@1952
   410
ds@1952
   411
	/* If we get a "file://" notation, strip off the file:// part so we're left 
ds@1952
   412
	 * with an absolute path.  Note that the absolute path gets returned, so 
ds@1952
   413
	 * usage with relative paths here is completely invalid.
ds@1952
   414
	 */
ds@1952
   415
	if (type != Path_SM_Rel && strncmp(_buffer, "file://", 7) == 0)
ds@1952
   416
	{
ds@1952
   417
		return g_LibSys.PathFormat(buffer, maxlength, "%s", &_buffer[7]);
ds@1952
   418
	}
ds@1952
   419
ds@1952
   420
	const char *base = NULL;
ds@1952
   421
	if (type == Path_Game)
ds@1952
   422
	{
ds@1952
   423
		base = GetGamePath();
ds@1952
   424
	}
ds@1952
   425
	else if (type == Path_SM)
ds@1952
   426
	{
ds@1952
   427
		base = GetSourceModPath();
ds@1952
   428
	}
ds@1952
   429
	else if (type == Path_SM_Rel)
ds@1952
   430
	{
ds@1952
   431
		base = m_SMRelDir;
ds@1952
   432
	}
ds@1952
   433
ds@1952
   434
	if (base)
ds@1952
   435
	{
ds@1952
   436
		return g_LibSys.PathFormat(buffer, maxlength, "%s/%s", base, _buffer);
ds@1952
   437
	}
ds@1952
   438
	else
ds@1952
   439
	{
ds@1952
   440
		return g_LibSys.PathFormat(buffer, maxlength, "%s", _buffer);
ds@1952
   441
	}
ds@1952
   442
}
ds@1952
   443
ds@1952
   444
void SourceModBase::CloseSourceMod()
ds@1952
   445
{
ds@1952
   446
	/* Force a level end */
ds@1952
   447
	LevelShutdown();
ds@1952
   448
ds@1952
   449
	/* Unload plugins */
ds@1952
   450
	g_PluginSys.Shutdown();
ds@1952
   451
ds@1952
   452
	/* Unload extensions */
ds@1952
   453
	g_Extensions.Shutdown();
ds@1952
   454
ds@1952
   455
	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
ds@1952
   456
ds@1952
   457
	if (g_Loaded)
ds@1952
   458
	{
ds@1952
   459
		if (g_pOnMapEnd)
ds@1952
   460
		{
ds@1952
   461
			g_Forwards.ReleaseForward(g_pOnMapEnd);
ds@1952
   462
		}
ds@1952
   463
	
ds@1952
   464
		/* Notify! */
ds@1952
   465
		SMGlobalClass *pBase = SMGlobalClass::head;
ds@1952
   466
		while (pBase)
ds@1952
   467
		{
ds@1952
   468
			pBase->OnSourceModShutdown();
ds@1952
   469
			pBase = pBase->m_pGlobalClassNext;
ds@1952
   470
		}
ds@1952
   471
	
ds@1952
   472
		/* Delete all data packs */
ds@1952
   473
		CStack<CDataPack *>::iterator iter;
ds@1952
   474
		CDataPack *pd;
ds@1952
   475
		for (iter=m_freepacks.begin(); iter!=m_freepacks.end(); iter++)
ds@1952
   476
		{
ds@1952
   477
			pd = (*iter);
ds@1952
   478
			delete pd;
ds@1952
   479
		}
ds@1952
   480
		m_freepacks.popall();
ds@1952
   481
	
ds@1952
   482
		/* Notify! */
ds@1952
   483
		pBase = SMGlobalClass::head;
ds@1952
   484
		while (pBase)
ds@1952
   485
		{
ds@1952
   486
			pBase->OnSourceModAllShutdown();
ds@1952
   487
			pBase = pBase->m_pGlobalClassNext;
ds@1952
   488
		}
ds@1952
   489
	
ds@1952
   490
		if (enginePatch)
ds@1952
   491
		{
ds@1952
   492
			SH_RELEASE_CALLCLASS(enginePatch);
ds@1952
   493
			enginePatch = NULL;
ds@1952
   494
		}
ds@1952
   495
	
ds@1952
   496
		if (gamedllPatch)
ds@1952
   497
		{
ds@1952
   498
			SH_RELEASE_CALLCLASS(gamedllPatch);
ds@1952
   499
			gamedllPatch = NULL;
ds@1952
   500
		}
ds@1952
   501
ds@1952
   502
		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
ds@1952
   503
		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, &g_Timers, &TimerSystem::GameFrame, false);
ds@1952
   504
	}
ds@1952
   505
ds@1952
   506
	/* Rest In Peace */
ds@1952
   507
	ShutdownJIT();
ds@1952
   508
}
ds@1952
   509
ds@1952
   510
void SourceModBase::LogMessage(IExtension *pExt, const char *format, ...)
ds@1952
   511
{
ds@1952
   512
	IExtensionInterface *pAPI = pExt->GetAPI();
ds@1952
   513
	const char *tag = pAPI->GetExtensionTag();
ds@1952
   514
	char buffer[2048];
ds@1952
   515
	va_list ap;
ds@1952
   516
ds@1952
   517
	va_start(ap, format);
ds@1952
   518
	vsnprintf(buffer, sizeof(buffer), format, ap);
ds@1952
   519
	va_end(ap);
ds@1952
   520
ds@1952
   521
	if (tag)
ds@1952
   522
	{
ds@1952
   523
		g_Logger.LogMessage("[%s] %s", tag, buffer);
ds@1952
   524
	} else {
ds@1952
   525
		g_Logger.LogMessage("%s", buffer);
ds@1952
   526
	}
ds@1952
   527
}
ds@1952
   528
ds@1952
   529
void SourceModBase::LogError(IExtension *pExt, const char *format, ...)
ds@1952
   530
{
ds@1952
   531
	IExtensionInterface *pAPI = pExt->GetAPI();
ds@1952
   532
	const char *tag = pAPI->GetExtensionTag();
ds@1952
   533
	char buffer[2048];
ds@1952
   534
	va_list ap;
ds@1952
   535
ds@1952
   536
	va_start(ap, format);
ds@1952
   537
	vsnprintf(buffer, sizeof(buffer), format, ap);
ds@1952
   538
	va_end(ap);
ds@1952
   539
ds@1952
   540
	if (tag)
ds@1952
   541
	{
ds@1952
   542
		g_Logger.LogError("[%s] %s", tag, buffer);
ds@1952
   543
	} else {
ds@1952
   544
		g_Logger.LogError("%s", buffer);
ds@1952
   545
	}
ds@1952
   546
}
ds@1952
   547
ds@1952
   548
size_t SourceModBase::FormatString(char *buffer, size_t maxlength, IPluginContext *pContext, const cell_t *params, unsigned int param)
ds@1952
   549
{
ds@1952
   550
	char *fmt;
ds@1952
   551
ds@1952
   552
	pContext->LocalToString(params[param], &fmt);
ds@1952
   553
ds@1952
   554
	int lparam = ++param;
ds@1952
   555
ds@1952
   556
	return atcprintf(buffer, maxlength, fmt, pContext, params, &lparam);
ds@1952
   557
}
ds@1952
   558
ds@1952
   559
const char *SourceModBase::GetSourceModPath() const
ds@1952
   560
{
ds@1952
   561
	return m_SMBaseDir;
ds@1952
   562
}
ds@1952
   563
ds@1952
   564
const char *SourceModBase::GetGamePath() const
ds@1952
   565
{
ds@1952
   566
	return g_BaseDir.c_str();
ds@1952
   567
}
ds@1952
   568
ds@1952
   569
unsigned int SourceModBase::SetGlobalTarget(unsigned int index)
ds@1952
   570
{
ds@1952
   571
	unsigned int old = m_target;
ds@1952
   572
	m_target = index;
ds@1952
   573
	return old;
ds@1952
   574
}
ds@1952
   575
ds@1952
   576
unsigned int SourceModBase::GetGlobalTarget() const
ds@1952
   577
{
ds@1952
   578
	return m_target;
ds@1952
   579
}
ds@1952
   580
ds@1952
   581
IDataPack *SourceModBase::CreateDataPack()
ds@1952
   582
{
ds@1952
   583
	CDataPack *pack;
ds@1952
   584
	if (m_freepacks.empty())
ds@1952
   585
	{
ds@1952
   586
		pack = new CDataPack;
ds@1952
   587
	} else {
ds@1952
   588
		pack = m_freepacks.front();
ds@1952
   589
		m_freepacks.pop();
ds@1952
   590
		pack->Initialize();
ds@1952
   591
	}
ds@1952
   592
	return pack;
ds@1952
   593
}
ds@1952
   594
ds@1952
   595
void SourceModBase::FreeDataPack(IDataPack *pack)
ds@1952
   596
{
ds@1952
   597
	m_freepacks.push(static_cast<CDataPack *>(pack));
ds@1952
   598
}
ds@1952
   599
ds@1952
   600
Handle_t SourceModBase::GetDataPackHandleType(bool readonly)
ds@1952
   601
{
ds@1952
   602
	//:TODO:
ds@1952
   603
	return 0;
ds@1952
   604
}
ds@1952
   605
ds@1952
   606
const char *SourceModBase::GetGameFolderName() const
ds@1952
   607
{
ds@1952
   608
	return m_ModDir;
ds@1952
   609
}
ds@1952
   610
ds@1952
   611
ISourcePawnEngine *SourceModBase::GetScriptingEngine()
ds@1952
   612
{
ds@1952
   613
	return g_pSourcePawn;
ds@1952
   614
}
ds@1952
   615
ds@1952
   616
IVirtualMachine *SourceModBase::GetScriptingVM()
ds@1952
   617
{
dvander@2193
   618
	return NULL;
ds@1952
   619
}
ds@1952
   620
ds@1952
   621
void SourceModBase::AllPluginsLoaded()
ds@1952
   622
{
ds@1952
   623
	SMGlobalClass *base = SMGlobalClass::head;
ds@1952
   624
	while (base)
ds@1952
   625
	{
ds@1952
   626
		base->OnSourceModGameInitialized();
ds@1952
   627
		base = base->m_pGlobalClassNext;
ds@1952
   628
	}
ds@1952
   629
}
ds@1952
   630
ds@1952
   631
time_t SourceModBase::GetAdjustedTime()
ds@1952
   632
{
ds@1952
   633
	return ::GetAdjustedTime();
ds@1952
   634
}
ds@1952
   635
ds@1952
   636
void SourceModBase::AddGameFrameHook(GAME_FRAME_HOOK hook)
ds@1952
   637
{
ds@1952
   638
	m_frame_hooks.push_back(hook);
ds@1952
   639
}
ds@1952
   640
ds@1952
   641
void SourceModBase::RemoveGameFrameHook(GAME_FRAME_HOOK hook)
ds@1952
   642
{
ds@1952
   643
	for (size_t i = 0; i < m_frame_hooks.size(); i++)
ds@1952
   644
	{
ds@1952
   645
		if (m_frame_hooks[i] == hook)
ds@1952
   646
		{
ds@1952
   647
			m_frame_hooks.erase(m_frame_hooks.iterAt(i));
ds@1952
   648
			return;
ds@1952
   649
		}
ds@1952
   650
	}
ds@1952
   651
}
ds@1952
   652
ds@1952
   653
void SourceModBase::ProcessGameFrameHooks(bool simulating)
ds@1952
   654
{
ds@1952
   655
	if (m_frame_hooks.size() == 0)
ds@1952
   656
	{
ds@1952
   657
		return;
ds@1952
   658
	}
ds@1952
   659
ds@1952
   660
	for (size_t i = 0; i < m_frame_hooks.size(); i++)
ds@1952
   661
	{
ds@1952
   662
		m_frame_hooks[i](simulating);
ds@1952
   663
	}
ds@1952
   664
}
ds@1952
   665
ds@1952
   666
SMGlobalClass *SMGlobalClass::head = NULL;
ds@1952
   667
ds@1952
   668
SMGlobalClass::SMGlobalClass()
ds@1952
   669
{
ds@1952
   670
	m_pGlobalClassNext = SMGlobalClass::head;
ds@1952
   671
	SMGlobalClass::head = this;
ds@1952
   672
}