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 "CPlayerManager.h"
28 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
29 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
30 SH_DECL_HOOK1_void(IServerGameDLL, GameFrame, SH_NOATTRIB, false, bool);
32 SourcePawnEngine g_SourcePawn;
33 SourceModBase g_SourceMod;
35 ILibrary *g_pJIT = NULL;
36 SourceHook::String g_BaseDir;
37 ISourcePawnEngine *g_pSourcePawn = &g_SourcePawn;
38 IVirtualMachine *g_pVM;
39 IdentityToken_t *g_pCoreIdent = NULL;
40 float g_LastTime = 0.0f;
41 float g_LastAuthCheck = 0.0f;
43 typedef int (*GIVEENGINEPOINTER)(ISourcePawnEngine *);
44 typedef unsigned int (*GETEXPORTCOUNT)();
45 typedef IVirtualMachine *(*GETEXPORT)(unsigned int);
46 typedef void (*NOTIFYSHUTDOWN)();
50 NOTIFYSHUTDOWN notify = (NOTIFYSHUTDOWN)g_pJIT->GetSymbolAddress("NotifyShutdown");
56 g_pJIT->CloseLibrary();
59 SourceModBase::SourceModBase()
61 m_IsMapLoading = false;
62 m_ExecPluginReload = false;
65 bool SourceModBase::InitializeSourceMod(char *error, size_t err_max, bool late)
67 g_BaseDir.assign(g_SMAPI->GetBaseDir());
68 g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/addons/sourcemod", g_BaseDir.c_str());
69 g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), "addons/sourcemod");
71 /* Attempt to load the JIT! */
72 char file[PLATFORM_MAX_PATH];
74 g_SMAPI->PathFormat(file, sizeof(file), "%s/bin/sourcepawn.jit.x86.%s",
79 g_pJIT = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
84 snprintf(error, err_max, "%s (failed to load bin/sourcepawn.jit.x86.%s)",
92 GIVEENGINEPOINTER jit_init = (GIVEENGINEPOINTER)g_pJIT->GetSymbolAddress("GiveEnginePointer");
98 snprintf(error, err_max, "Failed to find GiveEnginePointer in JIT!");
103 if ((err=jit_init(g_pSourcePawn)) != 0)
106 if (error && err_max)
108 snprintf(error, err_max, "GiveEnginePointer returned %d in the JIT", err);
113 GETEXPORTCOUNT jit_getnum = (GETEXPORTCOUNT)g_pJIT->GetSymbolAddress("GetExportCount");
114 GETEXPORT jit_get = (GETEXPORT)g_pJIT->GetSymbolAddress("GetExport");
118 if (error && err_max)
120 snprintf(error, err_max, "JIT is missing a necessary export!");
125 unsigned int num = jit_getnum();
126 if (!num || ((g_pVM=jit_get(0)) == NULL))
129 if (error && err_max)
131 snprintf(error, err_max, "JIT did not export any virtual machines!");
136 unsigned int api = g_pVM->GetAPIVersion();
137 if (api != SOURCEPAWN_VM_API_VERSION)
140 if (error && err_max)
142 snprintf(error, err_max, "JIT is not a compatible version");
147 StartSourceMod(late);
152 void SourceModBase::StartSourceMod(bool late)
154 /* First initialize the global hooks we need */
155 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
156 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
157 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, this, &SourceModBase::GameFrame, false);
159 enginePatch = SH_GET_CALLCLASS(engine);
162 SMGlobalClass *pBase = SMGlobalClass::head;
165 pBase->OnSourceModStartup(false);
166 pBase = pBase->m_pGlobalClassNext;
169 /* Make the global core identity */
170 g_pCoreIdent = g_ShareSys.CreateCoreIdentity();
173 pBase = SMGlobalClass::head;
176 pBase->OnSourceModAllInitialized();
177 pBase = pBase->m_pGlobalClassNext;
181 g_ShareSys.AddInterface(NULL, this);
184 bool SourceModBase::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
186 m_IsMapLoading = true;
187 m_ExecPluginReload = true;
189 g_LastAuthCheck = 0.0f;
191 g_Logger.MapChange(pMapName);
193 DoGlobalPluginLoads();
195 m_IsMapLoading = false;
197 g_Admins.DumpAdminCache(AdminCache_Overrides, true);
198 g_Admins.DumpAdminCache(AdminCache_Groups, true);
200 RETURN_META_VALUE(MRES_IGNORED, true);
203 void SourceModBase::GameFrame(bool simulating)
206 * Note: This is all hardcoded rather than delegated to save
207 * precious CPU cycles.
209 float curtime = gpGlobals->curtime;
210 if (curtime - g_LastTime > 0.1f)
213 && (gpGlobals->curtime - g_LastAuthCheck > 0.7f))
215 g_LastAuthCheck = gpGlobals->curtime;
216 g_Players.RunAuthChecks();
218 g_LastTime = curtime;
222 void SourceModBase::LevelShutdown()
224 if (m_ExecPluginReload)
226 g_PluginSys.ReloadOrUnloadPlugins();
227 m_ExecPluginReload = false;
231 bool SourceModBase::IsMapLoading() const
233 return m_IsMapLoading;
236 void SourceModBase::DoGlobalPluginLoads()
238 char config_path[PLATFORM_MAX_PATH];
239 char plugins_path[PLATFORM_MAX_PATH];
241 BuildPath(Path_SM, config_path,
243 "configs/plugin_settings.cfg");
244 BuildPath(Path_SM, plugins_path,
245 sizeof(plugins_path),
248 /* Load any auto extensions */
249 g_Extensions.TryAutoload();
251 /* Run the first pass */
252 g_PluginSys.LoadAll_FirstPass(config_path, plugins_path);
254 /* Mark any extensions as loaded */
255 g_Extensions.MarkAllLoaded();
257 /* No modules yet, it's safe to call this from here */
258 g_PluginSys.LoadAll_SecondPass();
260 /* Re-mark any extensions as loaded */
261 g_Extensions.MarkAllLoaded();
264 size_t SourceModBase::BuildPath(PathType type, char *buffer, size_t maxlength, char *format, ...)
266 char _buffer[PLATFORM_MAX_PATH+1];
269 va_start(ap, format);
270 vsnprintf(_buffer, PLATFORM_MAX_PATH, format, ap);
273 const char *base = NULL;
274 if (type == Path_Game)
277 } else if (type == Path_SM) {
278 base = GetSourceModPath();
279 } else if (type == Path_SM_Rel) {
285 return g_LibSys.PathFormat(buffer, maxlength, "%s/%s", base, _buffer);
287 return g_LibSys.PathFormat(buffer, maxlength, "%s", _buffer);
291 void SourceModBase::CloseSourceMod()
294 SMGlobalClass *pBase = SMGlobalClass::head;
297 pBase->OnSourceModShutdown();
298 pBase = pBase->m_pGlobalClassNext;
301 /* Delete all data packs */
302 CStack<CDataPack *>::iterator iter;
304 for (iter=m_freepacks.begin(); iter!=m_freepacks.end(); iter++)
309 m_freepacks.popall();
312 pBase = SMGlobalClass::head;
315 pBase->OnSourceModAllShutdown();
316 pBase = pBase->m_pGlobalClassNext;
321 SH_RELEASE_CALLCLASS(enginePatch);
325 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
326 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
327 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, this, &SourceModBase::GameFrame, false);
333 void SourceModBase::LogMessage(IExtension *pExt, const char *format, ...)
335 IExtensionInterface *pAPI = pExt->GetAPI();
336 const char *tag = pAPI->GetExtensionTag();
340 va_start(ap, format);
341 vsnprintf(buffer, sizeof(buffer), format, ap);
346 g_Logger.LogMessage("[%s] %s", tag, buffer);
348 g_Logger.LogMessage("%s", buffer);
352 void SourceModBase::LogError(IExtension *pExt, const char *format, ...)
354 IExtensionInterface *pAPI = pExt->GetAPI();
355 const char *tag = pAPI->GetExtensionTag();
359 va_start(ap, format);
360 vsnprintf(buffer, sizeof(buffer), format, ap);
365 g_Logger.LogError("[%s] %s", tag, buffer);
367 g_Logger.LogError("%s", buffer);
371 size_t SourceModBase::FormatString(char *buffer, size_t maxlength, IPluginContext *pContext, const cell_t *params, unsigned int param)
375 pContext->LocalToString(params[param], &fmt);
377 int lparam = ++param;
379 return atcprintf(buffer, maxlength, fmt, pContext, params, &lparam);
382 const char *SourceModBase::GetSourceModPath() const
387 const char *SourceModBase::GetModPath() const
389 return g_BaseDir.c_str();
392 void SourceModBase::SetGlobalTarget(unsigned int index)
397 void SourceModBase::SetAuthChecking(bool set)
399 m_CheckingAuth = set;
402 unsigned int SourceModBase::GetGlobalTarget() const
407 IDataPack *SourceModBase::CreateDataPack()
410 if (m_freepacks.empty())
412 pack = new CDataPack;
414 pack = m_freepacks.front();
421 void SourceModBase::FreeDataPack(IDataPack *pack)
423 m_freepacks.push(static_cast<CDataPack *>(pack));
426 Handle_t SourceModBase::GetDataPackHandleType(bool readonly)
432 SMGlobalClass *SMGlobalClass::head = NULL;
434 SMGlobalClass::SMGlobalClass()
436 m_pGlobalClassNext = SMGlobalClass::head;
437 SMGlobalClass::head = this;