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);
160 SMGlobalClass *pBase = SMGlobalClass::head;
163 pBase->OnSourceModStartup(false);
164 pBase = pBase->m_pGlobalClassNext;
167 /* Make the global core identity */
168 g_pCoreIdent = g_ShareSys.CreateCoreIdentity();
171 pBase = SMGlobalClass::head;
174 pBase->OnSourceModAllInitialized();
175 pBase = pBase->m_pGlobalClassNext;
179 g_ShareSys.AddInterface(NULL, this);
182 bool SourceModBase::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
184 m_IsMapLoading = true;
185 m_ExecPluginReload = true;
187 g_LastAuthCheck = 0.0f;
189 g_Logger.MapChange(pMapName);
191 DoGlobalPluginLoads();
193 m_IsMapLoading = false;
195 g_Admins.DumpAdminCache(AdminCache_Overrides, true);
196 g_Admins.DumpAdminCache(AdminCache_Groups, true);
198 RETURN_META_VALUE(MRES_IGNORED, true);
201 void SourceModBase::GameFrame(bool simulating)
204 * Note: This is all hardcoded rather than delegated to save
205 * precious CPU cycles.
207 float curtime = gpGlobals->curtime;
208 if (curtime - g_LastTime > 0.1f)
211 && (gpGlobals->curtime - g_LastAuthCheck > 0.7f))
213 g_LastAuthCheck = gpGlobals->curtime;
214 g_Players.RunAuthChecks();
216 g_LastTime = curtime;
220 void SourceModBase::LevelShutdown()
222 if (m_ExecPluginReload)
224 g_PluginSys.ReloadOrUnloadPlugins();
225 m_ExecPluginReload = false;
229 bool SourceModBase::IsMapLoading()
231 return m_IsMapLoading;
234 void SourceModBase::DoGlobalPluginLoads()
236 char config_path[PLATFORM_MAX_PATH];
237 char plugins_path[PLATFORM_MAX_PATH];
239 BuildPath(Path_SM, config_path,
241 "configs/plugin_settings.cfg");
242 BuildPath(Path_SM, plugins_path,
243 sizeof(plugins_path),
246 /* Load any auto extensions */
247 g_Extensions.TryAutoload();
249 /* Run the first pass */
250 g_PluginSys.LoadAll_FirstPass(config_path, plugins_path);
252 /* Mark any extensions as loaded */
253 g_Extensions.MarkAllLoaded();
255 /* No modules yet, it's safe to call this from here */
256 g_PluginSys.LoadAll_SecondPass();
258 /* Re-mark any extensions as loaded */
259 g_Extensions.MarkAllLoaded();
262 size_t SourceModBase::BuildPath(PathType type, char *buffer, size_t maxlength, char *format, ...)
264 char _buffer[PLATFORM_MAX_PATH+1];
267 va_start(ap, format);
268 vsnprintf(_buffer, PLATFORM_MAX_PATH, format, ap);
271 const char *base = NULL;
272 if (type == Path_Game)
275 } else if (type == Path_SM) {
276 base = GetSourceModPath();
277 } else if (type == Path_SM_Rel) {
283 return g_LibSys.PathFormat(buffer, maxlength, "%s/%s", base, _buffer);
285 return g_LibSys.PathFormat(buffer, maxlength, "%s", _buffer);
289 void SourceModBase::CloseSourceMod()
292 SMGlobalClass *pBase = SMGlobalClass::head;
295 pBase->OnSourceModShutdown();
296 pBase = pBase->m_pGlobalClassNext;
300 pBase = SMGlobalClass::head;
303 pBase->OnSourceModAllShutdown();
304 pBase = pBase->m_pGlobalClassNext;
307 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
308 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
309 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, this, &SourceModBase::GameFrame, false);
315 void SourceModBase::LogMessage(IExtension *pExt, const char *format, ...)
317 IExtensionInterface *pAPI = pExt->GetAPI();
318 const char *tag = pAPI->GetExtensionTag();
322 va_start(ap, format);
323 vsnprintf(buffer, sizeof(buffer), format, ap);
328 g_Logger.LogMessage("[%s] %s", tag, buffer);
330 g_Logger.LogMessage("%s", buffer);
334 void SourceModBase::LogError(IExtension *pExt, const char *format, ...)
336 IExtensionInterface *pAPI = pExt->GetAPI();
337 const char *tag = pAPI->GetExtensionTag();
341 va_start(ap, format);
342 vsnprintf(buffer, sizeof(buffer), format, ap);
347 g_Logger.LogError("[%s] %s", tag, buffer);
349 g_Logger.LogError("%s", buffer);
353 size_t SourceModBase::FormatString(char *buffer, size_t maxlength, IPluginContext *pContext, const cell_t *params, unsigned int param)
357 pContext->LocalToString(params[param], &fmt);
359 int lparam = ++param;
361 return atcprintf(buffer, maxlength, fmt, pContext, params, &lparam);
364 const char *SourceModBase::GetSourceModPath()
369 const char *SourceModBase::GetModPath()
371 return g_BaseDir.c_str();
374 void SourceModBase::SetGlobalTarget(unsigned int index)
379 void SourceModBase::SetAuthChecking(bool set)
381 m_CheckingAuth = set;
384 unsigned int SourceModBase::GetGlobalTarget() const
389 SMGlobalClass *SMGlobalClass::head = NULL;
391 SMGlobalClass::SMGlobalClass()
393 m_pGlobalClassNext = SMGlobalClass::head;
394 SMGlobalClass::head = this;