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