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"
27 #include "CTranslator.h"
28 #include "ForwardSys.h"
30 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
31 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
32 SH_DECL_HOOK1_void(IServerGameDLL, GameFrame, SH_NOATTRIB, false, bool);
34 SourcePawnEngine g_SourcePawn;
35 SourceModBase g_SourceMod;
37 ILibrary *g_pJIT = NULL;
38 SourceHook::String g_BaseDir;
39 ISourcePawnEngine *g_pSourcePawn = &g_SourcePawn;
40 IVirtualMachine *g_pVM;
41 IdentityToken_t *g_pCoreIdent = NULL;
42 float g_LastTime = 0.0f;
43 float g_LastAuthCheck = 0.0f;
44 IForward *g_pOnGameFrame = NULL;
46 typedef int (*GIVEENGINEPOINTER)(ISourcePawnEngine *);
47 typedef unsigned int (*GETEXPORTCOUNT)();
48 typedef IVirtualMachine *(*GETEXPORT)(unsigned int);
49 typedef void (*NOTIFYSHUTDOWN)();
53 NOTIFYSHUTDOWN notify = (NOTIFYSHUTDOWN)g_pJIT->GetSymbolAddress("NotifyShutdown");
59 g_pJIT->CloseLibrary();
62 SourceModBase::SourceModBase()
64 m_IsMapLoading = false;
65 m_ExecPluginReload = false;
68 bool SourceModBase::InitializeSourceMod(char *error, size_t err_max, bool late)
70 g_BaseDir.assign(g_SMAPI->GetBaseDir());
71 g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/addons/sourcemod", g_BaseDir.c_str());
72 g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), "addons/sourcemod");
74 /* Attempt to load the JIT! */
75 char file[PLATFORM_MAX_PATH];
77 g_SMAPI->PathFormat(file, sizeof(file), "%s/bin/sourcepawn.jit.x86.%s",
82 g_pJIT = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
87 snprintf(error, err_max, "%s (failed to load bin/sourcepawn.jit.x86.%s)",
95 GIVEENGINEPOINTER jit_init = (GIVEENGINEPOINTER)g_pJIT->GetSymbolAddress("GiveEnginePointer");
101 snprintf(error, err_max, "Failed to find GiveEnginePointer in JIT!");
106 if ((err=jit_init(g_pSourcePawn)) != 0)
109 if (error && err_max)
111 snprintf(error, err_max, "GiveEnginePointer returned %d in the JIT", err);
116 GETEXPORTCOUNT jit_getnum = (GETEXPORTCOUNT)g_pJIT->GetSymbolAddress("GetExportCount");
117 GETEXPORT jit_get = (GETEXPORT)g_pJIT->GetSymbolAddress("GetExport");
121 if (error && err_max)
123 snprintf(error, err_max, "JIT is missing a necessary export!");
128 unsigned int num = jit_getnum();
129 if (!num || ((g_pVM=jit_get(0)) == NULL))
132 if (error && err_max)
134 snprintf(error, err_max, "JIT did not export any virtual machines!");
139 unsigned int api = g_pVM->GetAPIVersion();
140 if (api != SOURCEPAWN_VM_API_VERSION)
143 if (error && err_max)
145 snprintf(error, err_max, "JIT is not a compatible version");
150 StartSourceMod(late);
155 void SourceModBase::StartSourceMod(bool late)
157 /* First initialize the global hooks we need */
158 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
159 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
160 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, this, &SourceModBase::GameFrame, false);
162 enginePatch = SH_GET_CALLCLASS(engine);
163 gamedllPatch = SH_GET_CALLCLASS(gamedll);
166 SMGlobalClass *pBase = SMGlobalClass::head;
169 pBase->OnSourceModStartup(false);
170 pBase = pBase->m_pGlobalClassNext;
173 /* Make the global core identity */
174 g_pCoreIdent = g_ShareSys.CreateCoreIdentity();
177 pBase = SMGlobalClass::head;
180 pBase->OnSourceModAllInitialized();
181 pBase = pBase->m_pGlobalClassNext;
185 g_ShareSys.AddInterface(NULL, this);
188 bool SourceModBase::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
190 m_IsMapLoading = true;
191 m_ExecPluginReload = true;
193 g_LastAuthCheck = 0.0f;
195 g_Logger.MapChange(pMapName);
197 /* Refresh language stuff */
198 char path[PLATFORM_MAX_PATH];
199 BuildPath(Path_SM, path, sizeof(path), "configs/languages.cfg");
200 g_Translator.RebuildLanguageDatabase(path);
202 DoGlobalPluginLoads();
204 m_IsMapLoading = false;
206 g_Admins.DumpAdminCache(AdminCache_Overrides, true);
207 g_Admins.DumpAdminCache(AdminCache_Groups, true);
211 g_pOnGameFrame = g_Forwards.CreateForward("OnGameFrame", ET_Ignore, 0, NULL);
214 RETURN_META_VALUE(MRES_IGNORED, true);
217 void SourceModBase::GameFrame(bool simulating)
220 * Note: This is all hardcoded rather than delegated to save
221 * precious CPU cycles.
223 float curtime = gpGlobals->curtime;
224 if (curtime - g_LastTime > 0.1f)
227 && (gpGlobals->curtime - g_LastAuthCheck > 0.7f))
229 g_LastAuthCheck = gpGlobals->curtime;
230 g_Players.RunAuthChecks();
232 g_LastTime = curtime;
235 if (g_pOnGameFrame && g_pOnGameFrame->GetFunctionCount())
237 g_pOnGameFrame->Execute(NULL);
241 void SourceModBase::LevelShutdown()
243 if (m_ExecPluginReload)
245 g_PluginSys.ReloadOrUnloadPlugins();
246 m_ExecPluginReload = false;
250 bool SourceModBase::IsMapLoading() const
252 return m_IsMapLoading;
255 void SourceModBase::DoGlobalPluginLoads()
257 char config_path[PLATFORM_MAX_PATH];
258 char plugins_path[PLATFORM_MAX_PATH];
260 BuildPath(Path_SM, config_path,
262 "configs/plugin_settings.cfg");
263 BuildPath(Path_SM, plugins_path,
264 sizeof(plugins_path),
267 /* Load any auto extensions */
268 g_Extensions.TryAutoload();
270 /* Run the first pass */
271 g_PluginSys.LoadAll_FirstPass(config_path, plugins_path);
273 /* Mark any extensions as loaded */
274 g_Extensions.MarkAllLoaded();
276 /* No modules yet, it's safe to call this from here */
277 g_PluginSys.LoadAll_SecondPass();
279 /* Re-mark any extensions as loaded */
280 g_Extensions.MarkAllLoaded();
283 size_t SourceModBase::BuildPath(PathType type, char *buffer, size_t maxlength, char *format, ...)
285 char _buffer[PLATFORM_MAX_PATH+1];
288 va_start(ap, format);
289 vsnprintf(_buffer, PLATFORM_MAX_PATH, format, ap);
292 const char *base = NULL;
293 if (type == Path_Game)
296 } else if (type == Path_SM) {
297 base = GetSourceModPath();
298 } else if (type == Path_SM_Rel) {
304 return g_LibSys.PathFormat(buffer, maxlength, "%s/%s", base, _buffer);
306 return g_LibSys.PathFormat(buffer, maxlength, "%s", _buffer);
310 void SourceModBase::CloseSourceMod()
314 g_Forwards.ReleaseForward(g_pOnGameFrame);
318 SMGlobalClass *pBase = SMGlobalClass::head;
321 pBase->OnSourceModShutdown();
322 pBase = pBase->m_pGlobalClassNext;
325 /* Delete all data packs */
326 CStack<CDataPack *>::iterator iter;
328 for (iter=m_freepacks.begin(); iter!=m_freepacks.end(); iter++)
333 m_freepacks.popall();
336 pBase = SMGlobalClass::head;
339 pBase->OnSourceModAllShutdown();
340 pBase = pBase->m_pGlobalClassNext;
345 SH_RELEASE_CALLCLASS(enginePatch);
351 SH_RELEASE_CALLCLASS(gamedllPatch);
355 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
356 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
357 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, gamedll, this, &SourceModBase::GameFrame, false);
363 void SourceModBase::LogMessage(IExtension *pExt, const char *format, ...)
365 IExtensionInterface *pAPI = pExt->GetAPI();
366 const char *tag = pAPI->GetExtensionTag();
370 va_start(ap, format);
371 vsnprintf(buffer, sizeof(buffer), format, ap);
376 g_Logger.LogMessage("[%s] %s", tag, buffer);
378 g_Logger.LogMessage("%s", buffer);
382 void SourceModBase::LogError(IExtension *pExt, const char *format, ...)
384 IExtensionInterface *pAPI = pExt->GetAPI();
385 const char *tag = pAPI->GetExtensionTag();
389 va_start(ap, format);
390 vsnprintf(buffer, sizeof(buffer), format, ap);
395 g_Logger.LogError("[%s] %s", tag, buffer);
397 g_Logger.LogError("%s", buffer);
401 size_t SourceModBase::FormatString(char *buffer, size_t maxlength, IPluginContext *pContext, const cell_t *params, unsigned int param)
405 pContext->LocalToString(params[param], &fmt);
407 int lparam = ++param;
409 return atcprintf(buffer, maxlength, fmt, pContext, params, &lparam);
412 const char *SourceModBase::GetSourceModPath() const
417 const char *SourceModBase::GetModPath() const
419 return g_BaseDir.c_str();
422 void SourceModBase::SetGlobalTarget(unsigned int index)
427 void SourceModBase::SetAuthChecking(bool set)
429 m_CheckingAuth = set;
432 unsigned int SourceModBase::GetGlobalTarget() const
437 IDataPack *SourceModBase::CreateDataPack()
440 if (m_freepacks.empty())
442 pack = new CDataPack;
444 pack = m_freepacks.front();
451 void SourceModBase::FreeDataPack(IDataPack *pack)
453 m_freepacks.push(static_cast<CDataPack *>(pack));
456 Handle_t SourceModBase::GetDataPackHandleType(bool readonly)
462 SMGlobalClass *SMGlobalClass::head = NULL;
464 SMGlobalClass::SMGlobalClass()
466 m_pGlobalClassNext = SMGlobalClass::head;
467 SMGlobalClass::head = this;