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