3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
16 #include "sourcemod.h"
17 #include "sourcemm_api.h"
18 #include "systems/LibrarySys.h"
19 #include "vm/sp_vm_engine.h"
20 #include <sh_string.h>
21 #include "PluginSys.h"
24 #include "ExtensionSys.h"
25 #include "AdminCache.h"
26 #include "sm_stringutil.h"
27 #include "PlayerManager.h"
28 #include "Translator.h"
29 #include "ForwardSys.h"
32 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
33 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
34 SH_DECL_HOOK1_void(IServerGameDLL, GameFrame, SH_NOATTRIB, false, bool);
36 SourcePawnEngine g_SourcePawn;
37 SourceModBase g_SourceMod;
39 ILibrary *g_pJIT = NULL;
40 SourceHook::String g_BaseDir;
41 ISourcePawnEngine *g_pSourcePawn = &g_SourcePawn;
42 IVirtualMachine *g_pVM;
43 IdentityToken_t *g_pCoreIdent = NULL;
44 float g_LastTime = 0.0f;
45 float g_LastAuthCheck = 0.0f;
46 IForward *g_pOnGameFrame = NULL;
48 typedef int (*GIVEENGINEPOINTER)(ISourcePawnEngine *);
49 typedef unsigned int (*GETEXPORTCOUNT)();
50 typedef IVirtualMachine *(*GETEXPORT)(unsigned int);
51 typedef void (*NOTIFYSHUTDOWN)();
55 NOTIFYSHUTDOWN notify = (NOTIFYSHUTDOWN)g_pJIT->GetSymbolAddress("NotifyShutdown");
61 g_pJIT->CloseLibrary();
64 SourceModBase::SourceModBase()
66 m_IsMapLoading = false;
67 m_ExecPluginReload = false;
70 bool SourceModBase::InitializeSourceMod(char *error, size_t err_max, bool late)
72 g_BaseDir.assign(g_SMAPI->GetBaseDir());
73 g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/addons/sourcemod", g_BaseDir.c_str());
74 g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), "addons/sourcemod");
76 /* Attempt to load the JIT! */
77 char file[PLATFORM_MAX_PATH];
79 g_SMAPI->PathFormat(file, sizeof(file), "%s/bin/sourcepawn.jit.x86.%s",
84 g_pJIT = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
89 snprintf(error, err_max, "%s (failed to load bin/sourcepawn.jit.x86.%s)",
97 GIVEENGINEPOINTER jit_init = (GIVEENGINEPOINTER)g_pJIT->GetSymbolAddress("GiveEnginePointer");
101 if (error && err_max)
103 snprintf(error, err_max, "Failed to find GiveEnginePointer in JIT!");
108 if ((err=jit_init(g_pSourcePawn)) != 0)
111 if (error && err_max)
113 snprintf(error, err_max, "GiveEnginePointer returned %d in the JIT", err);
118 GETEXPORTCOUNT jit_getnum = (GETEXPORTCOUNT)g_pJIT->GetSymbolAddress("GetExportCount");
119 GETEXPORT jit_get = (GETEXPORT)g_pJIT->GetSymbolAddress("GetExport");
123 if (error && err_max)
125 snprintf(error, err_max, "JIT is missing a necessary export!");
130 unsigned int num = jit_getnum();
131 if (!num || ((g_pVM=jit_get(0)) == NULL))
134 if (error && err_max)
136 snprintf(error, err_max, "JIT did not export any virtual machines!");
141 unsigned int api = g_pVM->GetAPIVersion();
142 if (api != SOURCEPAWN_VM_API_VERSION)
145 if (error && err_max)
147 snprintf(error, err_max, "JIT is not a compatible version");
152 StartSourceMod(late);
157 void SourceModBase::StartSourceMod(bool late)
159 /* First initialize the global hooks we need */
160 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
161 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
162 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, this, &SourceModBase::GameFrame, false);
164 enginePatch = SH_GET_CALLCLASS(engine);
165 gamedllPatch = SH_GET_CALLCLASS(gamedll);
168 SMGlobalClass *pBase = SMGlobalClass::head;
171 pBase->OnSourceModStartup(false);
172 pBase = pBase->m_pGlobalClassNext;
175 /* Make the global core identity */
176 g_pCoreIdent = g_ShareSys.CreateCoreIdentity();
179 pBase = SMGlobalClass::head;
182 pBase->OnSourceModAllInitialized();
183 pBase = pBase->m_pGlobalClassNext;
187 g_ShareSys.AddInterface(NULL, this);
190 bool SourceModBase::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
192 m_IsMapLoading = true;
193 m_ExecPluginReload = true;
195 g_LastAuthCheck = 0.0f;
197 g_Logger.MapChange(pMapName);
198 g_Timers.MapChange();
200 /* Refresh language stuff */
201 char path[PLATFORM_MAX_PATH];
202 BuildPath(Path_SM, path, sizeof(path), "configs/languages.cfg");
203 g_Translator.RebuildLanguageDatabase(path);
205 DoGlobalPluginLoads();
207 m_IsMapLoading = false;
209 g_Admins.DumpAdminCache(AdminCache_Overrides, true);
210 g_Admins.DumpAdminCache(AdminCache_Groups, true);
214 g_pOnGameFrame = g_Forwards.CreateForward("OnGameFrame", ET_Ignore, 0, NULL);
217 RETURN_META_VALUE(MRES_IGNORED, true);
220 void SourceModBase::GameFrame(bool simulating)
223 * Note: This is all hardcoded rather than delegated to save
224 * precious CPU cycles.
226 float curtime = gpGlobals->curtime;
227 if (curtime - g_LastTime >= 0.1f)
230 && (gpGlobals->curtime - g_LastAuthCheck > 0.7f))
232 g_LastAuthCheck = gpGlobals->curtime;
233 g_Players.RunAuthChecks();
237 g_LastTime = curtime;
240 if (g_pOnGameFrame && g_pOnGameFrame->GetFunctionCount())
242 g_pOnGameFrame->Execute(NULL);
246 void SourceModBase::LevelShutdown()
248 if (m_ExecPluginReload)
250 g_PluginSys.ReloadOrUnloadPlugins();
251 m_ExecPluginReload = false;
255 bool SourceModBase::IsMapLoading() const
257 return m_IsMapLoading;
260 void SourceModBase::DoGlobalPluginLoads()
262 char config_path[PLATFORM_MAX_PATH];
263 char plugins_path[PLATFORM_MAX_PATH];
265 BuildPath(Path_SM, config_path,
267 "configs/plugin_settings.cfg");
268 BuildPath(Path_SM, plugins_path,
269 sizeof(plugins_path),
272 /* Load any auto extensions */
273 g_Extensions.TryAutoload();
275 /* Run the first pass */
276 g_PluginSys.LoadAll_FirstPass(config_path, plugins_path);
278 /* Mark any extensions as loaded */
279 g_Extensions.MarkAllLoaded();
281 /* No modules yet, it's safe to call this from here */
282 g_PluginSys.LoadAll_SecondPass();
284 /* Re-mark any extensions as loaded */
285 g_Extensions.MarkAllLoaded();
288 size_t SourceModBase::BuildPath(PathType type, char *buffer, size_t maxlength, char *format, ...)
290 char _buffer[PLATFORM_MAX_PATH];
293 va_start(ap, format);
294 vsnprintf(_buffer, PLATFORM_MAX_PATH, format, ap);
297 const char *base = NULL;
298 if (type == Path_Game)
301 } else if (type == Path_SM) {
302 base = GetSourceModPath();
303 } else if (type == Path_SM_Rel) {
309 return g_LibSys.PathFormat(buffer, maxlength, "%s/%s", base, _buffer);
311 return g_LibSys.PathFormat(buffer, maxlength, "%s", _buffer);
315 void SourceModBase::CloseSourceMod()
319 g_Forwards.ReleaseForward(g_pOnGameFrame);
323 SMGlobalClass *pBase = SMGlobalClass::head;
326 pBase->OnSourceModShutdown();
327 pBase = pBase->m_pGlobalClassNext;
330 /* Delete all data packs */
331 CStack<CDataPack *>::iterator iter;
333 for (iter=m_freepacks.begin(); iter!=m_freepacks.end(); iter++)
338 m_freepacks.popall();
341 pBase = SMGlobalClass::head;
344 pBase->OnSourceModAllShutdown();
345 pBase = pBase->m_pGlobalClassNext;
350 SH_RELEASE_CALLCLASS(enginePatch);
356 SH_RELEASE_CALLCLASS(gamedllPatch);
360 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
361 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
362 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, this, &SourceModBase::GameFrame, false);
368 void SourceModBase::LogMessage(IExtension *pExt, const char *format, ...)
370 IExtensionInterface *pAPI = pExt->GetAPI();
371 const char *tag = pAPI->GetExtensionTag();
375 va_start(ap, format);
376 vsnprintf(buffer, sizeof(buffer), format, ap);
381 g_Logger.LogMessage("[%s] %s", tag, buffer);
383 g_Logger.LogMessage("%s", buffer);
387 void SourceModBase::LogError(IExtension *pExt, const char *format, ...)
389 IExtensionInterface *pAPI = pExt->GetAPI();
390 const char *tag = pAPI->GetExtensionTag();
394 va_start(ap, format);
395 vsnprintf(buffer, sizeof(buffer), format, ap);
400 g_Logger.LogError("[%s] %s", tag, buffer);
402 g_Logger.LogError("%s", buffer);
406 size_t SourceModBase::FormatString(char *buffer, size_t maxlength, IPluginContext *pContext, const cell_t *params, unsigned int param)
410 pContext->LocalToString(params[param], &fmt);
412 int lparam = ++param;
414 return atcprintf(buffer, maxlength, fmt, pContext, params, &lparam);
417 const char *SourceModBase::GetSourceModPath() const
422 const char *SourceModBase::GetModPath() const
424 return g_BaseDir.c_str();
427 void SourceModBase::SetGlobalTarget(unsigned int index)
432 void SourceModBase::SetAuthChecking(bool set)
434 m_CheckingAuth = set;
437 unsigned int SourceModBase::GetGlobalTarget() const
442 IDataPack *SourceModBase::CreateDataPack()
445 if (m_freepacks.empty())
447 pack = new CDataPack;
449 pack = m_freepacks.front();
456 void SourceModBase::FreeDataPack(IDataPack *pack)
458 m_freepacks.push(static_cast<CDataPack *>(pack));
461 Handle_t SourceModBase::GetDataPackHandleType(bool readonly)
467 SMGlobalClass *SMGlobalClass::head = NULL;
469 SMGlobalClass::SMGlobalClass()
471 m_pGlobalClassNext = SMGlobalClass::head;
472 SMGlobalClass::head = this;