3 #include "sourcemm_api.h"
4 #include "systems/LibrarySys.h"
5 #include "vm/sp_vm_engine.h"
10 #include "ExtensionSys.h"
11 #include "AdminCache.h"
12 #include "sm_stringutil.h"
14 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
15 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
17 SourcePawnEngine g_SourcePawn;
18 SourceModBase g_SourceMod;
20 ILibrary *g_pJIT = NULL;
21 SourceHook::String g_BaseDir;
22 ISourcePawnEngine *g_pSourcePawn = &g_SourcePawn;
23 IVirtualMachine *g_pVM;
24 IdentityToken_t *g_pCoreIdent = NULL;
26 typedef int (*GIVEENGINEPOINTER)(ISourcePawnEngine *);
27 typedef unsigned int (*GETEXPORTCOUNT)();
28 typedef IVirtualMachine *(*GETEXPORT)(unsigned int);
29 typedef void (*NOTIFYSHUTDOWN)();
33 NOTIFYSHUTDOWN notify = (NOTIFYSHUTDOWN)g_pJIT->GetSymbolAddress("NotifyShutdown");
39 g_pJIT->CloseLibrary();
42 SourceModBase::SourceModBase()
44 m_IsMapLoading = false;
45 m_ExecPluginReload = false;
48 bool SourceModBase::InitializeSourceMod(char *error, size_t err_max, bool late)
50 g_BaseDir.assign(g_SMAPI->GetBaseDir());
51 g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/addons/sourcemod", g_BaseDir.c_str());
52 g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), "addons/sourcemod");
54 /* Attempt to load the JIT! */
55 char file[PLATFORM_MAX_PATH];
57 g_SMAPI->PathFormat(file, sizeof(file), "%s/bin/sourcepawn.jit.x86.%s",
62 g_pJIT = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
67 snprintf(error, err_max, "%s (failed to load bin/sourcepawn.jit.x86.%s)",
75 GIVEENGINEPOINTER jit_init = (GIVEENGINEPOINTER)g_pJIT->GetSymbolAddress("GiveEnginePointer");
81 snprintf(error, err_max, "Failed to find GiveEnginePointer in JIT!");
86 if ((err=jit_init(g_pSourcePawn)) != 0)
91 snprintf(error, err_max, "GiveEnginePointer returned %d in the JIT", err);
96 GETEXPORTCOUNT jit_getnum = (GETEXPORTCOUNT)g_pJIT->GetSymbolAddress("GetExportCount");
97 GETEXPORT jit_get = (GETEXPORT)g_pJIT->GetSymbolAddress("GetExport");
101 if (error && err_max)
103 snprintf(error, err_max, "JIT is missing a necessary export!");
108 unsigned int num = jit_getnum();
109 if (!num || ((g_pVM=jit_get(0)) == NULL))
112 if (error && err_max)
114 snprintf(error, err_max, "JIT did not export any virtual machines!");
119 unsigned int api = g_pVM->GetAPIVersion();
120 if (api != SOURCEPAWN_VM_API_VERSION)
123 if (error && err_max)
125 snprintf(error, err_max, "JIT is not a compatible version");
130 StartSourceMod(late);
135 void SourceModBase::StartSourceMod(bool late)
137 /* First initialize the global hooks we need */
138 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
139 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
142 SMGlobalClass *pBase = SMGlobalClass::head;
145 pBase->OnSourceModStartup(false);
146 pBase = pBase->m_pGlobalClassNext;
149 /* Make the global core identity */
150 g_pCoreIdent = g_ShareSys.CreateCoreIdentity();
153 pBase = SMGlobalClass::head;
156 pBase->OnSourceModAllInitialized();
157 pBase = pBase->m_pGlobalClassNext;
161 g_ShareSys.AddInterface(NULL, this);
164 bool SourceModBase::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
166 m_IsMapLoading = true;
167 m_ExecPluginReload = true;
169 g_Logger.MapChange(pMapName);
171 DoGlobalPluginLoads();
173 m_IsMapLoading = false;
175 g_Admins.DumpAdminCache(ADMIN_CACHE_GROUPS|ADMIN_CACHE_ADMINS|ADMIN_CACHE_OVERRIDES, true);
177 RETURN_META_VALUE(MRES_IGNORED, true);
180 void SourceModBase::LevelShutdown()
182 if (m_ExecPluginReload)
184 g_PluginSys.ReloadOrUnloadPlugins();
185 m_ExecPluginReload = false;
189 bool SourceModBase::IsMapLoading()
191 return m_IsMapLoading;
194 void SourceModBase::DoGlobalPluginLoads()
196 char config_path[PLATFORM_MAX_PATH];
197 char plugins_path[PLATFORM_MAX_PATH];
199 BuildPath(Path_SM, config_path,
201 "configs/plugin_settings.cfg");
202 BuildPath(Path_SM, plugins_path,
203 sizeof(plugins_path),
206 /* Run the first pass */
207 g_PluginSys.LoadAll_FirstPass(config_path, plugins_path);
209 /* Mark any extensions as loaded */
210 g_Extensions.MarkAllLoaded();
212 /* No modules yet, it's safe to call this from here */
213 g_PluginSys.LoadAll_SecondPass();
215 /* Re-mark any extensions as loaded */
216 g_Extensions.MarkAllLoaded();
219 size_t SourceModBase::BuildPath(PathType type, char *buffer, size_t maxlength, char *format, ...)
221 char _buffer[PLATFORM_MAX_PATH+1];
224 va_start(ap, format);
225 vsnprintf(_buffer, PLATFORM_MAX_PATH, format, ap);
228 const char *base = NULL;
229 if (type == Path_Game)
232 } else if (type == Path_SM) {
233 base = GetSourceModPath();
234 } else if (type == Path_SM_Rel) {
240 return g_LibSys.PathFormat(buffer, maxlength, "%s/%s", base, _buffer);
242 return g_LibSys.PathFormat(buffer, maxlength, "%s", _buffer);
246 void SourceModBase::CloseSourceMod()
249 SMGlobalClass *pBase = SMGlobalClass::head;
252 pBase->OnSourceModShutdown();
253 pBase = pBase->m_pGlobalClassNext;
257 pBase = SMGlobalClass::head;
260 pBase->OnSourceModAllShutdown();
261 pBase = pBase->m_pGlobalClassNext;
264 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
265 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
271 void SourceModBase::LogMessage(IExtension *pExt, const char *format, ...)
273 IExtensionInterface *pAPI = pExt->GetAPI();
274 const char *tag = pAPI->GetExtensionTag();
278 va_start(ap, format);
279 vsnprintf(buffer, sizeof(buffer), format, ap);
284 g_Logger.LogMessage("[%s] %s", tag, buffer);
286 g_Logger.LogMessage("%s", buffer);
290 void SourceModBase::LogError(IExtension *pExt, const char *format, ...)
292 IExtensionInterface *pAPI = pExt->GetAPI();
293 const char *tag = pAPI->GetExtensionTag();
297 va_start(ap, format);
298 vsnprintf(buffer, sizeof(buffer), format, ap);
303 g_Logger.LogError("[%s] %s", tag, buffer);
305 g_Logger.LogError("%s", buffer);
309 size_t SourceModBase::FormatString(char *buffer, size_t maxlength, IPluginContext *pContext, const cell_t *params, unsigned int param)
313 pContext->LocalToString(params[param], &fmt);
315 int lparam = ++param;
317 return atcprintf(buffer, maxlength, fmt, pContext, params, &lparam);
320 const char *SourceModBase::GetSourceModPath()
325 const char *SourceModBase::GetModPath()
327 return g_BaseDir.c_str();
330 SMGlobalClass *SMGlobalClass::head = NULL;
332 SMGlobalClass::SMGlobalClass()
334 m_pGlobalClassNext = SMGlobalClass::head;
335 SMGlobalClass::head = this;