2 * ===============================================================
3 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
4 * ===============================================================
6 * This file is not open source and may not be copied without explicit
7 * written permission of AlliedModders LLC. This file may not be redistributed
8 * in whole or significant part.
9 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
15 #include "sourcemod.h"
16 #include "sourcemm_api.h"
17 #include "systems/LibrarySys.h"
18 #include "vm/sp_vm_engine.h"
19 #include <sh_string.h>
20 #include "PluginSys.h"
23 #include "ExtensionSys.h"
24 #include "AdminCache.h"
25 #include "sm_stringutil.h"
26 #include "PlayerManager.h"
27 #include "Translator.h"
28 #include "ForwardSys.h"
31 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
32 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
33 SH_DECL_HOOK1_void(IServerGameDLL, GameFrame, SH_NOATTRIB, false, bool);
35 SourcePawnEngine g_SourcePawn;
36 SourceModBase g_SourceMod;
38 ILibrary *g_pJIT = NULL;
39 SourceHook::String g_BaseDir;
40 ISourcePawnEngine *g_pSourcePawn = &g_SourcePawn;
41 IVirtualMachine *g_pVM;
42 IdentityToken_t *g_pCoreIdent = NULL;
43 float g_LastTime = 0.0f;
44 float g_LastAuthCheck = 0.0f;
45 IForward *g_pOnGameFrame = NULL;
47 typedef int (*GIVEENGINEPOINTER)(ISourcePawnEngine *);
48 typedef unsigned int (*GETEXPORTCOUNT)();
49 typedef IVirtualMachine *(*GETEXPORT)(unsigned int);
50 typedef void (*NOTIFYSHUTDOWN)();
54 NOTIFYSHUTDOWN notify = (NOTIFYSHUTDOWN)g_pJIT->GetSymbolAddress("NotifyShutdown");
60 g_pJIT->CloseLibrary();
63 SourceModBase::SourceModBase()
65 m_IsMapLoading = false;
66 m_ExecPluginReload = false;
69 bool SourceModBase::InitializeSourceMod(char *error, size_t err_max, bool late)
71 g_BaseDir.assign(g_SMAPI->GetBaseDir());
72 g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/addons/sourcemod", g_BaseDir.c_str());
73 g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), "addons/sourcemod");
75 /* Attempt to load the JIT! */
76 char file[PLATFORM_MAX_PATH];
78 g_SMAPI->PathFormat(file, sizeof(file), "%s/bin/sourcepawn.jit.x86.%s",
83 g_pJIT = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
88 snprintf(error, err_max, "%s (failed to load bin/sourcepawn.jit.x86.%s)",
96 GIVEENGINEPOINTER jit_init = (GIVEENGINEPOINTER)g_pJIT->GetSymbolAddress("GiveEnginePointer");
100 if (error && err_max)
102 snprintf(error, err_max, "Failed to find GiveEnginePointer in JIT!");
107 if ((err=jit_init(g_pSourcePawn)) != 0)
110 if (error && err_max)
112 snprintf(error, err_max, "GiveEnginePointer returned %d in the JIT", err);
117 GETEXPORTCOUNT jit_getnum = (GETEXPORTCOUNT)g_pJIT->GetSymbolAddress("GetExportCount");
118 GETEXPORT jit_get = (GETEXPORT)g_pJIT->GetSymbolAddress("GetExport");
122 if (error && err_max)
124 snprintf(error, err_max, "JIT is missing a necessary export!");
129 unsigned int num = jit_getnum();
130 if (!num || ((g_pVM=jit_get(0)) == NULL))
133 if (error && err_max)
135 snprintf(error, err_max, "JIT did not export any virtual machines!");
140 unsigned int api = g_pVM->GetAPIVersion();
141 if (api != SOURCEPAWN_VM_API_VERSION)
144 if (error && err_max)
146 snprintf(error, err_max, "JIT is not a compatible version");
151 StartSourceMod(late);
156 void SourceModBase::StartSourceMod(bool late)
158 /* First initialize the global hooks we need */
159 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
160 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
161 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, this, &SourceModBase::GameFrame, false);
163 enginePatch = SH_GET_CALLCLASS(engine);
164 gamedllPatch = SH_GET_CALLCLASS(gamedll);
167 SMGlobalClass *pBase = SMGlobalClass::head;
170 pBase->OnSourceModStartup(false);
171 pBase = pBase->m_pGlobalClassNext;
174 /* Make the global core identity */
175 g_pCoreIdent = g_ShareSys.CreateCoreIdentity();
178 pBase = SMGlobalClass::head;
181 pBase->OnSourceModAllInitialized();
182 pBase = pBase->m_pGlobalClassNext;
186 g_ShareSys.AddInterface(NULL, this);
189 bool SourceModBase::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
191 m_IsMapLoading = true;
192 m_ExecPluginReload = true;
194 g_LastAuthCheck = 0.0f;
196 g_Logger.MapChange(pMapName);
197 g_Timers.MapChange();
199 /* Refresh language stuff */
200 char path[PLATFORM_MAX_PATH];
201 BuildPath(Path_SM, path, sizeof(path), "configs/languages.cfg");
202 g_Translator.RebuildLanguageDatabase(path);
204 DoGlobalPluginLoads();
206 m_IsMapLoading = false;
208 g_Admins.DumpAdminCache(AdminCache_Overrides, true);
209 g_Admins.DumpAdminCache(AdminCache_Groups, true);
213 g_pOnGameFrame = g_Forwards.CreateForward("OnGameFrame", ET_Ignore, 0, NULL);
216 RETURN_META_VALUE(MRES_IGNORED, true);
219 void SourceModBase::GameFrame(bool simulating)
222 * Note: This is all hardcoded rather than delegated to save
223 * precious CPU cycles.
225 float curtime = gpGlobals->curtime;
226 if (curtime - g_LastTime >= 0.1f)
229 && (gpGlobals->curtime - g_LastAuthCheck > 0.7f))
231 g_LastAuthCheck = gpGlobals->curtime;
232 g_Players.RunAuthChecks();
236 g_LastTime = curtime;
239 if (g_pOnGameFrame && g_pOnGameFrame->GetFunctionCount())
241 g_pOnGameFrame->Execute(NULL);
245 void SourceModBase::LevelShutdown()
247 if (m_ExecPluginReload)
249 g_PluginSys.ReloadOrUnloadPlugins();
250 m_ExecPluginReload = false;
254 bool SourceModBase::IsMapLoading() const
256 return m_IsMapLoading;
259 void SourceModBase::DoGlobalPluginLoads()
261 char config_path[PLATFORM_MAX_PATH];
262 char plugins_path[PLATFORM_MAX_PATH];
264 BuildPath(Path_SM, config_path,
266 "configs/plugin_settings.cfg");
267 BuildPath(Path_SM, plugins_path,
268 sizeof(plugins_path),
271 /* Load any auto extensions */
272 g_Extensions.TryAutoload();
274 /* Run the first pass */
275 g_PluginSys.LoadAll_FirstPass(config_path, plugins_path);
277 /* Mark any extensions as loaded */
278 g_Extensions.MarkAllLoaded();
280 /* No modules yet, it's safe to call this from here */
281 g_PluginSys.LoadAll_SecondPass();
283 /* Re-mark any extensions as loaded */
284 g_Extensions.MarkAllLoaded();
287 size_t SourceModBase::BuildPath(PathType type, char *buffer, size_t maxlength, char *format, ...)
289 char _buffer[PLATFORM_MAX_PATH+1];
292 va_start(ap, format);
293 vsnprintf(_buffer, PLATFORM_MAX_PATH, format, ap);
296 const char *base = NULL;
297 if (type == Path_Game)
300 } else if (type == Path_SM) {
301 base = GetSourceModPath();
302 } else if (type == Path_SM_Rel) {
308 return g_LibSys.PathFormat(buffer, maxlength, "%s/%s", base, _buffer);
310 return g_LibSys.PathFormat(buffer, maxlength, "%s", _buffer);
314 void SourceModBase::CloseSourceMod()
318 g_Forwards.ReleaseForward(g_pOnGameFrame);
322 SMGlobalClass *pBase = SMGlobalClass::head;
325 pBase->OnSourceModShutdown();
326 pBase = pBase->m_pGlobalClassNext;
329 /* Delete all data packs */
330 CStack<CDataPack *>::iterator iter;
332 for (iter=m_freepacks.begin(); iter!=m_freepacks.end(); iter++)
337 m_freepacks.popall();
340 pBase = SMGlobalClass::head;
343 pBase->OnSourceModAllShutdown();
344 pBase = pBase->m_pGlobalClassNext;
349 SH_RELEASE_CALLCLASS(enginePatch);
355 SH_RELEASE_CALLCLASS(gamedllPatch);
359 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
360 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
361 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, this, &SourceModBase::GameFrame, false);
367 void SourceModBase::LogMessage(IExtension *pExt, const char *format, ...)
369 IExtensionInterface *pAPI = pExt->GetAPI();
370 const char *tag = pAPI->GetExtensionTag();
374 va_start(ap, format);
375 vsnprintf(buffer, sizeof(buffer), format, ap);
380 g_Logger.LogMessage("[%s] %s", tag, buffer);
382 g_Logger.LogMessage("%s", buffer);
386 void SourceModBase::LogError(IExtension *pExt, const char *format, ...)
388 IExtensionInterface *pAPI = pExt->GetAPI();
389 const char *tag = pAPI->GetExtensionTag();
393 va_start(ap, format);
394 vsnprintf(buffer, sizeof(buffer), format, ap);
399 g_Logger.LogError("[%s] %s", tag, buffer);
401 g_Logger.LogError("%s", buffer);
405 size_t SourceModBase::FormatString(char *buffer, size_t maxlength, IPluginContext *pContext, const cell_t *params, unsigned int param)
409 pContext->LocalToString(params[param], &fmt);
411 int lparam = ++param;
413 return atcprintf(buffer, maxlength, fmt, pContext, params, &lparam);
416 const char *SourceModBase::GetSourceModPath() const
421 const char *SourceModBase::GetModPath() const
423 return g_BaseDir.c_str();
426 void SourceModBase::SetGlobalTarget(unsigned int index)
431 void SourceModBase::SetAuthChecking(bool set)
433 m_CheckingAuth = set;
436 unsigned int SourceModBase::GetGlobalTarget() const
441 IDataPack *SourceModBase::CreateDataPack()
444 if (m_freepacks.empty())
446 pack = new CDataPack;
448 pack = m_freepacks.front();
455 void SourceModBase::FreeDataPack(IDataPack *pack)
457 m_freepacks.push(static_cast<CDataPack *>(pack));
460 Handle_t SourceModBase::GetDataPackHandleType(bool readonly)
466 SMGlobalClass *SMGlobalClass::head = NULL;
468 SMGlobalClass::SMGlobalClass()
470 m_pGlobalClassNext = SMGlobalClass::head;
471 SMGlobalClass::head = this;