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"
13 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
14 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
16 SourcePawnEngine g_SourcePawn;
17 SourceModBase g_SourceMod;
19 ILibrary *g_pJIT = NULL;
20 SourceHook::String g_BaseDir;
21 ISourcePawnEngine *g_pSourcePawn = &g_SourcePawn;
22 IVirtualMachine *g_pVM;
23 IdentityToken_t *g_pCoreIdent = NULL;
25 typedef int (*GIVEENGINEPOINTER)(ISourcePawnEngine *);
26 typedef unsigned int (*GETEXPORTCOUNT)();
27 typedef IVirtualMachine *(*GETEXPORT)(unsigned int);
28 typedef void (*NOTIFYSHUTDOWN)();
32 NOTIFYSHUTDOWN notify = (NOTIFYSHUTDOWN)g_pJIT->GetSymbolAddress("NotifyShutdown");
38 g_pJIT->CloseLibrary();
41 SourceModBase::SourceModBase()
43 m_IsMapLoading = false;
44 m_ExecPluginReload = false;
47 bool SourceModBase::InitializeSourceMod(char *error, size_t err_max, bool late)
49 g_BaseDir.assign(g_SMAPI->GetBaseDir());
50 g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/addons/sourcemod", g_BaseDir.c_str());
51 g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), "addons/sourcemod");
53 /* Attempt to load the JIT! */
54 char file[PLATFORM_MAX_PATH];
56 g_SMAPI->PathFormat(file, sizeof(file), "%s/bin/sourcepawn.jit.x86.%s",
61 g_pJIT = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
66 snprintf(error, err_max, "%s (failed to load bin/sourcepawn.jit.x86.%s)",
74 GIVEENGINEPOINTER jit_init = (GIVEENGINEPOINTER)g_pJIT->GetSymbolAddress("GiveEnginePointer");
80 snprintf(error, err_max, "Failed to find GiveEnginePointer in JIT!");
85 if ((err=jit_init(g_pSourcePawn)) != 0)
90 snprintf(error, err_max, "GiveEnginePointer returned %d in the JIT", err);
95 GETEXPORTCOUNT jit_getnum = (GETEXPORTCOUNT)g_pJIT->GetSymbolAddress("GetExportCount");
96 GETEXPORT jit_get = (GETEXPORT)g_pJIT->GetSymbolAddress("GetExport");
100 if (error && err_max)
102 snprintf(error, err_max, "JIT is missing a necessary export!");
107 unsigned int num = jit_getnum();
108 if (!num || ((g_pVM=jit_get(0)) == NULL))
111 if (error && err_max)
113 snprintf(error, err_max, "JIT did not export any virtual machines!");
118 unsigned int api = g_pVM->GetAPIVersion();
119 if (api != SOURCEPAWN_VM_API_VERSION)
122 if (error && err_max)
124 snprintf(error, err_max, "JIT is not a compatible version");
129 StartSourceMod(late);
134 void SourceModBase::StartSourceMod(bool late)
136 /* First initialize the global hooks we need */
137 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
138 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
141 SMGlobalClass *pBase = SMGlobalClass::head;
144 pBase->OnSourceModStartup(false);
145 pBase = pBase->m_pGlobalClassNext;
148 /* Make the global core identity */
149 g_pCoreIdent = g_ShareSys.CreateCoreIdentity();
152 pBase = SMGlobalClass::head;
155 pBase->OnSourceModAllInitialized();
156 pBase = pBase->m_pGlobalClassNext;
160 g_ShareSys.AddInterface(NULL, this);
163 bool SourceModBase::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
165 m_IsMapLoading = true;
166 m_ExecPluginReload = true;
168 g_Logger.MapChange(pMapName);
170 DoGlobalPluginLoads();
172 m_IsMapLoading = false;
174 g_Admins.DumpAdminCache(ADMIN_CACHE_GROUPS|ADMIN_CACHE_ADMINS|ADMIN_CACHE_OVERRIDES, true);
176 RETURN_META_VALUE(MRES_IGNORED, true);
179 void SourceModBase::LevelShutdown()
181 if (m_ExecPluginReload)
183 g_PluginSys.ReloadOrUnloadPlugins();
184 m_ExecPluginReload = false;
188 bool SourceModBase::IsMapLoading()
190 return m_IsMapLoading;
193 void SourceModBase::DoGlobalPluginLoads()
195 char config_path[PLATFORM_MAX_PATH];
196 char plugins_path[PLATFORM_MAX_PATH];
198 BuildPath(Path_SM, config_path,
200 "configs/plugin_settings.cfg");
201 BuildPath(Path_SM, plugins_path,
202 sizeof(plugins_path),
205 /* Run the first pass */
206 g_PluginSys.LoadAll_FirstPass(config_path, plugins_path);
208 /* Mark any extensions as loaded */
209 g_Extensions.MarkAllLoaded();
211 /* No modules yet, it's safe to call this from here */
212 g_PluginSys.LoadAll_SecondPass();
214 /* Re-mark any extensions as loaded */
215 g_Extensions.MarkAllLoaded();
218 size_t SourceModBase::BuildPath(PathType type, char *buffer, size_t maxlength, char *format, ...)
220 char _buffer[PLATFORM_MAX_PATH+1];
223 va_start(ap, format);
224 vsnprintf(_buffer, PLATFORM_MAX_PATH, format, ap);
227 const char *base = NULL;
228 if (type == Path_Game)
231 } else if (type == Path_SM) {
232 base = GetSourceModPath();
233 } else if (type == Path_SM_Rel) {
239 return g_LibSys.PathFormat(buffer, maxlength, "%s/%s", base, _buffer);
241 return g_LibSys.PathFormat(buffer, maxlength, "%s", _buffer);
245 void SourceModBase::CloseSourceMod()
248 SMGlobalClass *pBase = SMGlobalClass::head;
251 pBase->OnSourceModShutdown();
252 pBase = pBase->m_pGlobalClassNext;
256 pBase = SMGlobalClass::head;
259 pBase->OnSourceModAllShutdown();
260 pBase = pBase->m_pGlobalClassNext;
263 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
264 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
270 void SourceModBase::LogMessage(IExtension *pExt, const char *format, ...)
272 IExtensionInterface *pAPI = pExt->GetAPI();
273 const char *tag = pAPI->GetExtensionTag();
277 va_start(ap, format);
278 vsnprintf(buffer, sizeof(buffer), format, ap);
283 g_Logger.LogMessage("[%s] %s", tag, buffer);
285 g_Logger.LogMessage("%s", buffer);
289 void SourceModBase::LogError(IExtension *pExt, const char *format, ...)
291 IExtensionInterface *pAPI = pExt->GetAPI();
292 const char *tag = pAPI->GetExtensionTag();
296 va_start(ap, format);
297 vsnprintf(buffer, sizeof(buffer), format, ap);
302 g_Logger.LogError("[%s] %s", tag, buffer);
304 g_Logger.LogError("%s", buffer);
308 const char *SourceModBase::GetSourceModPath()
313 const char *SourceModBase::GetModPath()
315 return g_BaseDir.c_str();
318 SMGlobalClass *SMGlobalClass::head = NULL;
320 SMGlobalClass::SMGlobalClass()
322 m_pGlobalClassNext = SMGlobalClass::head;
323 SMGlobalClass::head = this;