Optimized forwards, now paused functions are stored in a temp list so we dont have to check if they're runnable on each function execution.
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"
27 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
28 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
30 SourcePawnEngine g_SourcePawn;
31 SourceModBase g_SourceMod;
33 ILibrary *g_pJIT = NULL;
34 SourceHook::String g_BaseDir;
35 ISourcePawnEngine *g_pSourcePawn = &g_SourcePawn;
36 IVirtualMachine *g_pVM;
37 IdentityToken_t *g_pCoreIdent = NULL;
39 typedef int (*GIVEENGINEPOINTER)(ISourcePawnEngine *);
40 typedef unsigned int (*GETEXPORTCOUNT)();
41 typedef IVirtualMachine *(*GETEXPORT)(unsigned int);
42 typedef void (*NOTIFYSHUTDOWN)();
46 NOTIFYSHUTDOWN notify = (NOTIFYSHUTDOWN)g_pJIT->GetSymbolAddress("NotifyShutdown");
52 g_pJIT->CloseLibrary();
55 SourceModBase::SourceModBase()
57 m_IsMapLoading = false;
58 m_ExecPluginReload = false;
61 bool SourceModBase::InitializeSourceMod(char *error, size_t err_max, bool late)
63 g_BaseDir.assign(g_SMAPI->GetBaseDir());
64 g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/addons/sourcemod", g_BaseDir.c_str());
65 g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), "addons/sourcemod");
67 /* Attempt to load the JIT! */
68 char file[PLATFORM_MAX_PATH];
70 g_SMAPI->PathFormat(file, sizeof(file), "%s/bin/sourcepawn.jit.x86.%s",
75 g_pJIT = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
80 snprintf(error, err_max, "%s (failed to load bin/sourcepawn.jit.x86.%s)",
88 GIVEENGINEPOINTER jit_init = (GIVEENGINEPOINTER)g_pJIT->GetSymbolAddress("GiveEnginePointer");
94 snprintf(error, err_max, "Failed to find GiveEnginePointer in JIT!");
99 if ((err=jit_init(g_pSourcePawn)) != 0)
102 if (error && err_max)
104 snprintf(error, err_max, "GiveEnginePointer returned %d in the JIT", err);
109 GETEXPORTCOUNT jit_getnum = (GETEXPORTCOUNT)g_pJIT->GetSymbolAddress("GetExportCount");
110 GETEXPORT jit_get = (GETEXPORT)g_pJIT->GetSymbolAddress("GetExport");
114 if (error && err_max)
116 snprintf(error, err_max, "JIT is missing a necessary export!");
121 unsigned int num = jit_getnum();
122 if (!num || ((g_pVM=jit_get(0)) == NULL))
125 if (error && err_max)
127 snprintf(error, err_max, "JIT did not export any virtual machines!");
132 unsigned int api = g_pVM->GetAPIVersion();
133 if (api != SOURCEPAWN_VM_API_VERSION)
136 if (error && err_max)
138 snprintf(error, err_max, "JIT is not a compatible version");
143 StartSourceMod(late);
148 void SourceModBase::StartSourceMod(bool late)
150 /* First initialize the global hooks we need */
151 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
152 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
155 SMGlobalClass *pBase = SMGlobalClass::head;
158 pBase->OnSourceModStartup(false);
159 pBase = pBase->m_pGlobalClassNext;
162 /* Make the global core identity */
163 g_pCoreIdent = g_ShareSys.CreateCoreIdentity();
166 pBase = SMGlobalClass::head;
169 pBase->OnSourceModAllInitialized();
170 pBase = pBase->m_pGlobalClassNext;
174 g_ShareSys.AddInterface(NULL, this);
177 bool SourceModBase::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
179 m_IsMapLoading = true;
180 m_ExecPluginReload = true;
182 g_Logger.MapChange(pMapName);
184 DoGlobalPluginLoads();
186 m_IsMapLoading = false;
188 g_Admins.DumpAdminCache(ADMIN_CACHE_GROUPS|ADMIN_CACHE_ADMINS|ADMIN_CACHE_OVERRIDES, true);
190 RETURN_META_VALUE(MRES_IGNORED, true);
193 void SourceModBase::LevelShutdown()
195 if (m_ExecPluginReload)
197 g_PluginSys.ReloadOrUnloadPlugins();
198 m_ExecPluginReload = false;
202 bool SourceModBase::IsMapLoading()
204 return m_IsMapLoading;
207 void SourceModBase::DoGlobalPluginLoads()
209 char config_path[PLATFORM_MAX_PATH];
210 char plugins_path[PLATFORM_MAX_PATH];
212 BuildPath(Path_SM, config_path,
214 "configs/plugin_settings.cfg");
215 BuildPath(Path_SM, plugins_path,
216 sizeof(plugins_path),
219 /* Load any auto extensions */
220 g_Extensions.TryAutoload();
222 /* Run the first pass */
223 g_PluginSys.LoadAll_FirstPass(config_path, plugins_path);
225 /* Mark any extensions as loaded */
226 g_Extensions.MarkAllLoaded();
228 /* No modules yet, it's safe to call this from here */
229 g_PluginSys.LoadAll_SecondPass();
231 /* Re-mark any extensions as loaded */
232 g_Extensions.MarkAllLoaded();
235 size_t SourceModBase::BuildPath(PathType type, char *buffer, size_t maxlength, char *format, ...)
237 char _buffer[PLATFORM_MAX_PATH+1];
240 va_start(ap, format);
241 vsnprintf(_buffer, PLATFORM_MAX_PATH, format, ap);
244 const char *base = NULL;
245 if (type == Path_Game)
248 } else if (type == Path_SM) {
249 base = GetSourceModPath();
250 } else if (type == Path_SM_Rel) {
256 return g_LibSys.PathFormat(buffer, maxlength, "%s/%s", base, _buffer);
258 return g_LibSys.PathFormat(buffer, maxlength, "%s", _buffer);
262 void SourceModBase::CloseSourceMod()
265 SMGlobalClass *pBase = SMGlobalClass::head;
268 pBase->OnSourceModShutdown();
269 pBase = pBase->m_pGlobalClassNext;
273 pBase = SMGlobalClass::head;
276 pBase->OnSourceModAllShutdown();
277 pBase = pBase->m_pGlobalClassNext;
280 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
281 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
287 void SourceModBase::LogMessage(IExtension *pExt, const char *format, ...)
289 IExtensionInterface *pAPI = pExt->GetAPI();
290 const char *tag = pAPI->GetExtensionTag();
294 va_start(ap, format);
295 vsnprintf(buffer, sizeof(buffer), format, ap);
300 g_Logger.LogMessage("[%s] %s", tag, buffer);
302 g_Logger.LogMessage("%s", buffer);
306 void SourceModBase::LogError(IExtension *pExt, const char *format, ...)
308 IExtensionInterface *pAPI = pExt->GetAPI();
309 const char *tag = pAPI->GetExtensionTag();
313 va_start(ap, format);
314 vsnprintf(buffer, sizeof(buffer), format, ap);
319 g_Logger.LogError("[%s] %s", tag, buffer);
321 g_Logger.LogError("%s", buffer);
325 size_t SourceModBase::FormatString(char *buffer, size_t maxlength, IPluginContext *pContext, const cell_t *params, unsigned int param)
329 pContext->LocalToString(params[param], &fmt);
331 int lparam = ++param;
333 return atcprintf(buffer, maxlength, fmt, pContext, params, &lparam);
336 const char *SourceModBase::GetSourceModPath()
341 const char *SourceModBase::GetModPath()
343 return g_BaseDir.c_str();
346 void SourceModBase::SetGlobalTarget(unsigned int index)
351 unsigned int SourceModBase::GetGlobalTarget() const
356 SMGlobalClass *SMGlobalClass::head = NULL;
358 SMGlobalClass::SMGlobalClass()
360 m_pGlobalClassNext = SMGlobalClass::head;
361 SMGlobalClass::head = this;