Update linux dep script (no bug, r=NPOTB).
2 * vim: set ts=4 sw=4 tw=99 noet:
3 * =============================================================================
5 * Copyright (C) 2004-2010 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
33 #include "sourcemod.h"
34 #include "sourcemm_api.h"
35 #include "LibrarySys.h"
36 #include <sh_string.h>
37 #include "CoreConfig.h"
39 #include "AdminCache.h"
40 #include "sm_stringutil.h"
41 #include "PlayerManager.h"
43 #include <IGameConfigs.h>
44 #include "frame_hooks.h"
45 #include "logic_bridge.h"
47 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
48 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
49 SH_DECL_HOOK1_void(IServerGameDLL, GameFrame, SH_NOATTRIB, false, bool);
50 SH_DECL_HOOK1_void(IVEngineServer, ServerCommand, SH_NOATTRIB, false, const char *);
52 SourceModBase g_SourceMod;
54 ILibrary *g_pJIT = NULL;
55 SourceHook::String g_BaseDir;
56 ISourcePawnEngine *g_pSourcePawn = NULL;
57 ISourcePawnEngine2 *g_pSourcePawn2 = NULL;
58 IdentityToken_t *g_pCoreIdent = NULL;
59 IForward *g_pOnMapEnd = NULL;
60 IGameConfig *g_pGameConf = NULL;
61 bool g_Loaded = false;
62 bool sm_show_debug_spew = false;
63 bool sm_disable_jit = false;
65 typedef ISourcePawnEngine *(*GET_SP_V1)();
66 typedef ISourcePawnEngine2 *(*GET_SP_V2)();
67 typedef void (*NOTIFYSHUTDOWN)();
69 #ifdef PLATFORM_WINDOWS
70 ConVar sm_basepath("sm_basepath", "addons\\sourcemod", 0, "SourceMod base path (set via command line)");
71 #elif defined PLATFORM_LINUX || defined PLATFORM_APPLE
72 ConVar sm_basepath("sm_basepath", "addons/sourcemod", 0, "SourceMod base path (set via command line)");
77 NOTIFYSHUTDOWN notify = (NOTIFYSHUTDOWN)g_pJIT->GetSymbolAddress("NotifyShutdown");
83 if (g_pSourcePawn2 != NULL)
85 g_pSourcePawn2->Shutdown();
88 g_pJIT->CloseLibrary();
91 SourceModBase::SourceModBase()
93 m_IsMapLoading = false;
94 m_ExecPluginReload = false;
95 m_GotBasePath = false;
98 ConfigResult SourceModBase::OnSourceModConfigChanged(const char *key,
104 if (strcasecmp(key, "BasePath") == 0)
106 if (source == ConfigSource_Console)
108 UTIL_Format(error, maxlength, "Cannot be set at runtime");
109 return ConfigResult_Reject;
114 g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/%s", g_BaseDir.c_str(), value);
115 g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), value);
117 m_GotBasePath = true;
120 return ConfigResult_Accept;
122 else if (strcasecmp(key, "DebugSpew") == 0)
124 sm_show_debug_spew = (strcasecmp(value, "yes") == 0) ? true : false;
126 return ConfigResult_Accept;
128 else if (strcasecmp(key, "DisableJIT") == 0)
130 sm_disable_jit = (strcasecmp(value, "yes") == 0) ? true : false;
132 g_pSourcePawn2->SetJitEnabled(!sm_disable_jit);
134 return ConfigResult_Accept;
137 return ConfigResult_Ignore;
140 static bool sSourceModInitialized = false;
142 bool SourceModBase::InitializeSourceMod(char *error, size_t maxlength, bool late)
144 const char *gamepath = g_SMAPI->GetBaseDir();
146 /* Store full path to game */
147 g_BaseDir.assign(gamepath);
149 /* Store name of game directory by itself */
150 size_t len = strlen(gamepath);
151 for (size_t i = len - 1; i < len; i--)
153 if (gamepath[i] == PLATFORM_SEP_CHAR)
155 strncopy(m_ModDir, &gamepath[++i], sizeof(m_ModDir));
160 const char *basepath = icvar->GetCommandLineValue("sm_basepath");
161 /* Set a custom base path if there is one. */
162 if (basepath != NULL && basepath[0] != '\0')
164 m_GotBasePath = true;
166 /* Otherwise, use a default and keep the m_GotBasePath unlocked. */
169 basepath = sm_basepath.GetDefault();
172 g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/%s", g_BaseDir.c_str(), basepath);
173 g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), "%s", basepath);
175 if (!StartLogicBridge(error, maxlength))
180 /* Initialize CoreConfig to get the SourceMod base path properly - this parses core.cfg */
181 g_CoreConfig.Initialize();
183 /* There will always be a path by this point, since it was force-set above. */
184 m_GotBasePath = true;
186 /* Attempt to load the JIT! */
187 char file[PLATFORM_MAX_PATH];
189 g_SMAPI->PathFormat(file, sizeof(file), "%s/bin/sourcepawn.jit.x86.%s",
194 g_pJIT = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
197 if (error && maxlength)
199 UTIL_Format(error, maxlength, "%s (failed to load bin/sourcepawn.jit.x86.%s)",
206 GET_SP_V1 getv1 = (GET_SP_V1)g_pJIT->GetSymbolAddress("GetSourcePawnEngine1");
207 GET_SP_V2 getv2 = (GET_SP_V2)g_pJIT->GetSymbolAddress("GetSourcePawnEngine2");
211 if (error && maxlength)
213 snprintf(error, maxlength, "JIT is too old; upgrade SourceMod");
218 else if (getv2 == NULL)
220 if (error && maxlength)
222 snprintf(error, maxlength, "JIT is too old; upgrade SourceMod");
228 g_pSourcePawn = getv1();
229 g_pSourcePawn2 = getv2();
231 if (g_pSourcePawn2->GetAPIVersion() < 3)
233 g_pSourcePawn2 = NULL;
234 if (error && maxlength)
236 snprintf(error, maxlength, "JIT version is out of date");
241 if (!g_pSourcePawn2->Initialize())
243 g_pSourcePawn2 = NULL;
244 if (error && maxlength)
246 snprintf(error, maxlength, "JIT could not be initialized");
251 g_pSourcePawn2->SetDebugListener(logicore.debugger);
254 g_pSourcePawn2->SetJitEnabled(!sm_disable_jit);
256 sSourceModInitialized = true;
258 /* Hook this now so we can detect startup without calling StartSourceMod() */
259 SH_ADD_HOOK(IServerGameDLL, LevelInit, gamedll, SH_MEMBER(this, &SourceModBase::LevelInit), false);
261 /* Only load if we're not late */
264 StartSourceMod(false);
270 void SourceModBase::StartSourceMod(bool late)
272 SH_ADD_HOOK(IServerGameDLL, LevelShutdown, gamedll, SH_MEMBER(this, &SourceModBase::LevelShutdown), false);
273 SH_ADD_HOOK(IServerGameDLL, GameFrame, gamedll, SH_MEMBER(&g_Timers, &TimerSystem::GameFrame), false);
275 enginePatch = SH_GET_CALLCLASS(engine);
276 gamedllPatch = SH_GET_CALLCLASS(gamedll);
281 SMGlobalClass *pBase = SMGlobalClass::head;
284 pBase->OnSourceModStartup(false);
285 pBase = pBase->m_pGlobalClassNext;
287 g_pGameConf = logicore.GetCoreGameConfig();
290 pBase = SMGlobalClass::head;
293 pBase->OnSourceModAllInitialized();
294 pBase = pBase->m_pGlobalClassNext;
298 pBase = SMGlobalClass::head;
301 pBase->OnSourceModAllInitialized_Post();
302 pBase = pBase->m_pGlobalClassNext;
306 sharesys->AddInterface(NULL, this);
311 /* Initialize VSP stuff */
312 if (vsp_interface != NULL)
314 g_SourceMod_Core.OnVSPListening(vsp_interface);
319 /* We missed doing anythin gin this if we late-loaded. Sneak it in now. */
323 /* If we want to autoload, do that now */
324 const char *disabled = GetCoreConfigValue("DisableAutoUpdate");
325 if (disabled == NULL || strcasecmp(disabled, "yes") != 0)
327 extsys->LoadAutoExtension("updater.ext." PLATFORM_LIB_EXT);
330 const char *timeout = GetCoreConfigValue("SlowScriptTimeout");
335 if (atoi(timeout) != 0)
337 g_pSourcePawn2->InstallWatchdogTimer(atoi(timeout) * 1000);
341 static bool g_LevelEndBarrier = false;
342 bool SourceModBase::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
344 /* If we're not loaded... */
347 /* Do all global initialization now */
348 StartSourceMod(true);
351 m_IsMapLoading = true;
352 m_ExecPluginReload = true;
355 SMGlobalClass *pBase = SMGlobalClass::head;
358 pBase->OnSourceModLevelChange(pMapName);
359 pBase = pBase->m_pGlobalClassNext;
362 DoGlobalPluginLoads();
364 m_IsMapLoading = false;
367 pBase = SMGlobalClass::head;
370 pBase->OnSourceModPluginsLoaded();
371 pBase = pBase->m_pGlobalClassNext;
376 g_pOnMapEnd = forwardsys->CreateForward("OnMapEnd", ET_Ignore, 0, NULL);
379 g_LevelEndBarrier = true;
381 RETURN_META_VALUE(MRES_IGNORED, true);
384 void SourceModBase::LevelShutdown()
386 if (g_LevelEndBarrier)
388 SMGlobalClass *next = SMGlobalClass::head;
391 next->OnSourceModLevelEnd();
392 next = next->m_pGlobalClassNext;
395 if (g_pOnMapEnd != NULL)
397 g_pOnMapEnd->Execute(NULL);
400 g_Timers.RemoveMapChangeTimers();
402 g_LevelEndBarrier = false;
405 g_OnMapStarted = false;
407 if (m_ExecPluginReload)
409 scripts->RefreshAll();
410 m_ExecPluginReload = false;
414 bool SourceModBase::IsMapLoading() const
416 return m_IsMapLoading;
419 void SourceModBase::DoGlobalPluginLoads()
421 char config_path[PLATFORM_MAX_PATH];
422 char plugins_path[PLATFORM_MAX_PATH];
424 BuildPath(Path_SM, config_path,
426 "configs/plugin_settings.cfg");
427 BuildPath(Path_SM, plugins_path,
428 sizeof(plugins_path),
431 /* Load any auto extensions */
432 extsys->TryAutoload();
434 /* Fire the extensions ready message */
435 g_SMAPI->MetaFactory(SOURCEMOD_NOTICE_EXTENSIONS, NULL, NULL);
437 /* Load any game extension */
438 const char *game_ext;
439 if ((game_ext = g_pGameConf->GetKeyValue("GameExtension")) != NULL)
441 char path[PLATFORM_MAX_PATH];
442 UTIL_Format(path, sizeof(path), "%s.ext." PLATFORM_LIB_EXT, game_ext);
443 extsys->LoadAutoExtension(path);
446 scripts->LoadAll(config_path, plugins_path);
449 size_t SourceModBase::BuildPath(PathType type, char *buffer, size_t maxlength, const char *format, ...)
451 char _buffer[PLATFORM_MAX_PATH];
454 va_start(ap, format);
455 vsnprintf(_buffer, PLATFORM_MAX_PATH, format, ap);
458 /* If we get a "file://" notation, strip off the file:// part so we're left
459 * with an absolute path. Note that the absolute path gets returned, so
460 * usage with relative paths here is completely invalid.
462 if (type != Path_SM_Rel && strncmp(_buffer, "file://", 7) == 0)
464 return g_LibSys.PathFormat(buffer, maxlength, "%s", &_buffer[7]);
467 const char *base = NULL;
468 if (type == Path_Game)
470 base = GetGamePath();
472 else if (type == Path_SM)
474 base = GetSourceModPath();
476 else if (type == Path_SM_Rel)
483 return g_LibSys.PathFormat(buffer, maxlength, "%s/%s", base, _buffer);
487 return g_LibSys.PathFormat(buffer, maxlength, "%s", _buffer);
491 void SourceModBase::CloseSourceMod()
493 if (!sSourceModInitialized)
496 SH_REMOVE_HOOK(IServerGameDLL, LevelInit, gamedll, SH_MEMBER(this, &SourceModBase::LevelInit), false);
500 /* Force a level end */
506 ShutdownLogicBridge();
510 void SourceModBase::ShutdownServices()
515 /* Unload extensions */
519 forwardsys->ReleaseForward(g_pOnMapEnd);
522 SMGlobalClass *pBase = SMGlobalClass::head;
525 pBase->OnSourceModShutdown();
526 pBase = pBase->m_pGlobalClassNext;
529 /* Delete all data packs */
530 CStack<CDataPack *>::iterator iter;
532 for (iter=m_freepacks.begin(); iter!=m_freepacks.end(); iter++)
537 m_freepacks.popall();
540 pBase = SMGlobalClass::head;
543 pBase->OnSourceModAllShutdown();
544 pBase = pBase->m_pGlobalClassNext;
549 SH_RELEASE_CALLCLASS(enginePatch);
555 SH_RELEASE_CALLCLASS(gamedllPatch);
559 SH_REMOVE_HOOK(IServerGameDLL, LevelShutdown, gamedll, SH_MEMBER(this, &SourceModBase::LevelShutdown), false);
560 SH_REMOVE_HOOK(IServerGameDLL, GameFrame, gamedll, SH_MEMBER(&g_Timers, &TimerSystem::GameFrame), false);
563 void SourceModBase::LogMessage(IExtension *pExt, const char *format, ...)
565 IExtensionInterface *pAPI = pExt->GetAPI();
566 const char *tag = pAPI->GetExtensionTag();
570 va_start(ap, format);
571 vsnprintf(buffer, sizeof(buffer), format, ap);
576 g_Logger.LogMessage("[%s] %s", tag, buffer);
578 g_Logger.LogMessage("%s", buffer);
582 void SourceModBase::LogError(IExtension *pExt, const char *format, ...)
584 IExtensionInterface *pAPI = pExt->GetAPI();
585 const char *tag = pAPI->GetExtensionTag();
589 va_start(ap, format);
590 vsnprintf(buffer, sizeof(buffer), format, ap);
595 g_Logger.LogError("[%s] %s", tag, buffer);
597 g_Logger.LogError("%s", buffer);
601 size_t SourceModBase::FormatString(char *buffer, size_t maxlength, IPluginContext *pContext, const cell_t *params, unsigned int param)
605 pContext->LocalToString(params[param], &fmt);
607 int lparam = ++param;
609 return atcprintf(buffer, maxlength, fmt, pContext, params, &lparam);
612 const char *SourceModBase::GetSourceModPath() const
617 const char *SourceModBase::GetGamePath() const
619 return g_BaseDir.c_str();
622 unsigned int SourceModBase::SetGlobalTarget(unsigned int index)
624 unsigned int old = m_target;
629 unsigned int SourceModBase::GetGlobalTarget() const
634 IDataPack *SourceModBase::CreateDataPack()
637 if (m_freepacks.empty())
639 pack = new CDataPack;
641 pack = m_freepacks.front();
648 void SourceModBase::FreeDataPack(IDataPack *pack)
650 m_freepacks.push(static_cast<CDataPack *>(pack));
653 Handle_t SourceModBase::GetDataPackHandleType(bool readonly)
659 const char *SourceModBase::GetGameFolderName() const
664 ISourcePawnEngine *SourceModBase::GetScriptingEngine()
666 return g_pSourcePawn;
669 IVirtualMachine *SourceModBase::GetScriptingVM()
674 void SourceModBase::AllPluginsLoaded()
681 SMGlobalClass *base = SMGlobalClass::head;
684 base->OnSourceModGameInitialized();
685 base = base->m_pGlobalClassNext;
689 time_t SourceModBase::GetAdjustedTime()
691 return ::GetAdjustedTime();
694 void SourceModBase::AddGameFrameHook(GAME_FRAME_HOOK hook)
696 m_frame_hooks.push_back(hook);
699 void SourceModBase::RemoveGameFrameHook(GAME_FRAME_HOOK hook)
701 for (size_t i = 0; i < m_frame_hooks.size(); i++)
703 if (m_frame_hooks[i] == hook)
705 m_frame_hooks.erase(m_frame_hooks.iterAt(i));
711 void SourceModBase::ProcessGameFrameHooks(bool simulating)
713 if (m_frame_hooks.size() == 0)
718 for (size_t i = 0; i < m_frame_hooks.size(); i++)
720 m_frame_hooks[i](simulating);
724 size_t SourceModBase::Format(char *buffer, size_t maxlength, const char *fmt, ...)
730 len = FormatArgs(buffer, maxlength, fmt, ap);
736 size_t SourceModBase::FormatArgs(char *buffer,
741 return UTIL_FormatArgs(buffer, maxlength, fmt, ap);
744 void SourceModBase::AddFrameAction(FRAMEACTION fn, void *data)
746 ::AddFrameAction(FrameAction(fn, data));
749 const char *SourceModBase::GetCoreConfigValue(const char *key)
751 return g_CoreConfig.GetCoreConfigValue(key);
754 int SourceModBase::GetPluginId()
759 int SourceModBase::GetShApiVersion()
762 g_SMAPI->GetShVersions(api, impl);
767 bool SourceModBase::IsMapRunning()
769 return g_OnMapStarted;
772 SMGlobalClass *SMGlobalClass::head = NULL;