1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2007 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): David "BAILOPAN" Anderson
8 * ============================
11 #include "convar_smm.h"
14 #include "concommands.h"
16 #include "vsp_listener.h"
19 * @brief Implementation of main API interface
23 using namespace SourceMM;
34 void CSmmAPI::LogMsg(ISmmPlugin *pl, const char *msg, ...)
37 static char buffer[2048];
42 vsnprintf(buffer, sizeof(buffer)-1, msg, ap);
45 LogMessage("[%s] %s", pl->GetLogTag(), buffer);
48 CreateInterfaceFn CSmmAPI::engineFactory(bool syn)
52 return g_Engine.engineFactory;
55 CreateInterfaceFn CSmmAPI::physicsFactory(bool syn)
58 return PhysicsFactory;
59 return g_Engine.physicsFactory;
62 CreateInterfaceFn CSmmAPI::fileSystemFactory(bool syn)
65 return FileSystemFactory;
66 return g_Engine.fileSystemFactory;
69 CreateInterfaceFn CSmmAPI::serverFactory(bool syn)
72 return CreateInterface;
73 return g_GameDll.factory;
76 CGlobalVars *CSmmAPI::pGlobals()
78 return g_Engine.pGlobals;
81 void CSmmAPI::SetLastMetaReturn(META_RES res)
86 META_RES CSmmAPI::GetLastMetaReturn()
91 IConCommandBaseAccessor *CSmmAPI::GetCvarBaseAccessor()
93 return static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor);
96 bool CSmmAPI::RegisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
98 if (pCommand->IsCommand())
100 g_PluginMngr.AddPluginCmd(plugin, pCommand);
102 g_PluginMngr.AddPluginCvar(plugin, pCommand);
105 return g_SMConVarAccessor.Register(pCommand);
108 void CSmmAPI::UnregisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
110 if (pCommand->IsCommand())
112 g_PluginMngr.RemovePluginCmd(plugin, pCommand);
114 g_PluginMngr.RemovePluginCvar(plugin, pCommand);
117 g_SMConVarAccessor.Unregister(pCommand);
120 void CSmmAPI::ConPrint(const char *fmt)
122 (m_ConPrintf)("%s", fmt);
125 void CSmmAPI::ConPrintf(const char *fmt, ...)
128 static char buf[4096];
130 vsnprintf(buf, sizeof(buf)-1, fmt, ap);
135 void CSmmAPI::AddListener(ISmmPlugin *plugin, IMetamodListener *pListener)
137 CPluginManager::CPlugin *pl = g_PluginMngr.FindByAPI(plugin);
139 pl->m_Events.push_back(pListener);
142 void *CSmmAPI::MetaFactory(const char *iface, int *_ret, PluginId *id)
154 //first check ours... we get first chance!
155 if (strcmp(iface, MMIFACE_SOURCEHOOK) == 0)
161 return static_cast<void *>(static_cast<SourceHook::ISourceHook *>(&g_SourceHook));
162 } else if (strcmp(iface, MMIFACE_PLMANAGER) == 0) {
167 return static_cast<void *>(static_cast<ISmmPluginManager *>(&g_PluginMngr));
170 CPluginManager::CPlugin *pl;
171 SourceHook::List<IMetamodListener *>::iterator event;
172 IMetamodListener *api;
176 for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++)
179 for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++)
183 if ( (val=api->OnMetamodQuery(iface, &ret)) != NULL )
195 *_ret = IFACE_FAILED;
200 //////////////////////////////////////////////////////////////////////////
201 //THERE BE HAX HERE!!!! DON'T TELL ALFRED, BUT GABE WANTED IT THAT WAY. //
202 // (note: you can find the offset by looking for the text //
203 // "Echo text to console", you'll find the callback cmd pushed on the //
205 //////////////////////////////////////////////////////////////////////////
208 #define ENGINE486_SIG "\x55\x89\xE5\x53\x83\xEC\x14\xBB"
209 #define ENGINE486_OFFS 40
210 #define ENGINE686_SIG "\x53\x83\xEC\x08\xBB\x01\x00\x00"
211 #define ENGINE686_OFFS 50
212 #define ENGINEAMD_SIG "\x53\x51\xBB\x01\x00\x00\x00\x51"
213 #define ENGINEAMD_OFFS 47
214 #define ENGINEW32_SIG "\xA1\x2A\x2A\x2A\x2A\x56\xBE\x01"
215 #define ENGINEW32_OFFS 38
216 #define IA32_CALL 0xE8
218 bool vcmp(const void *_addr1, const void *_addr2, size_t len)
220 unsigned char *addr1 = (unsigned char *)_addr1;
221 unsigned char *addr2 = (unsigned char *)_addr2;
223 for (size_t i=0; i<len; i++)
227 if (addr1[i] != addr2[i])
234 //Thanks to fysh for the idea of extracting info from "echo" and for
235 // having the original offsets at hand!
236 bool CSmmAPI::CacheCmds()
238 ICvar *pCvar = g_Engine.icvar;
240 ConCommandBase *pBase = pCvar->GetCommands();
241 unsigned char *ptr = NULL;
242 FnCommandCallback callback = NULL;
247 if ( strcmp(pBase->GetName(), "echo") == 0 )
249 //callback = //*((FnCommandCallback *)((char *)pBase + offsetof(ConCommand, m_fnCommandCallback)));
250 callback = ((ConCommand *)pBase)->GetCallback();
251 ptr = (unsigned char *)callback;
253 if (vcmp(ptr, ENGINE486_SIG, SIGLEN))
255 offs = ENGINE486_OFFS;
257 else if (vcmp(ptr, ENGINE686_SIG, SIGLEN))
259 offs = ENGINE686_OFFS;
261 else if (vcmp(ptr, ENGINEAMD_SIG, SIGLEN))
263 offs = ENGINEAMD_OFFS;
265 #elif defined OS_WIN32 // Only one Windows engine binary so far...
266 if (vcmp(ptr, ENGINEW32_SIG, SIGLEN))
268 offs = ENGINEW32_OFFS;
272 if (!offs || ptr[offs-1] != IA32_CALL)
274 m_ConPrintf = (CONPRINTF_FUNC)Msg;
277 //get the relative offset
278 m_ConPrintf = *((CONPRINTF_FUNC *)(ptr + offs));
279 //add the base offset, to the ip (which is the address+offset + 4 bytes for next instruction)
280 m_ConPrintf = (CONPRINTF_FUNC)((unsigned long)m_ConPrintf + (unsigned long)(ptr + offs) + 4);
286 pBase = const_cast<ConCommandBase *>(pBase->GetNext());
289 m_ConPrintf = (CONPRINTF_FUNC)Msg;
294 bool CSmmAPI::CacheSuccessful()
299 void CSmmAPI::GetApiVersions(int &major, int &minor, int &plvers, int &plmin)
301 major = SM_VERS_API_MAJOR;
302 minor = SM_VERS_API_MINOR;
303 plvers = PLAPI_VERSION;
304 plmin = PLAPI_MIN_VERSION;
307 void CSmmAPI::GetShVersions(int &shvers, int &shimpl)
309 shvers = SH_IFACE_VERSION;
310 shimpl = SH_IMPL_VERSION;
313 int CSmmAPI::FormatIface(char iface[], unsigned int maxlength)
315 int length = (int)strlen(iface);
319 for (i=length-1; i>=0; i--)
321 if (!isdigit(iface[i]))
331 if ( (num && ((int)maxlength <= length)) || (!num && ((int)maxlength <= length+3)) )
337 num = atoi(&(iface[++i]));
341 snprintf(&(iface[i]), 4, "%03d", num);
346 void *CSmmAPI::InterfaceSearch(CreateInterfaceFn fn, const char *iface, int max, int *ret)
348 char _if[256]; /* assume no interface goes beyond this */
349 size_t len = strlen(iface);
356 if (len + 4 > sizeof(_if))
369 if ( (pf = (fn)(_if, ret)) != NULL )
373 } while (( num = FormatIface(_if, len+1) ));
378 void *CSmmAPI::VInterfaceMatch(CreateInterfaceFn fn, const char *iface, int min)
380 char buffer[256]; /* assume no interface will go beyond this */
381 int len = static_cast<int>(strlen(iface));
382 int ret; /* just in case something doesn't handle NULL properly */
384 if (len > static_cast<int>(sizeof(buffer) - 4))
389 strcpy(buffer, iface);
393 char *ptr = &buffer[len-1];
395 while (isdigit(*ptr) && digits <=3)
403 /* for now, assume this is an error */
404 strcpy(buffer, iface);
407 min = (min == 0) ? 1 : min;
408 snprintf(num, sizeof(num), "%03d", min);
413 return InterfaceSearch(fn, buffer, IFACE_MAXNUM, &ret);
416 const char *CSmmAPI::GetBaseDir()
418 return g_ModPath.c_str();
421 void CSmmAPI::PathFormat(char *buffer, size_t len, const char *fmt, ...)
425 size_t mylen = vsnprintf(buffer, len, fmt, ap);
428 if (mylen == 0xFFFFFFFF || mylen >= len)
433 for (size_t i=0; i<mylen; i++)
435 if (buffer[i] == ALT_SEP_CHAR)
436 buffer[i] = PATH_SEP_CHAR;
440 void CSmmAPI::ClientConPrintf(edict_t *client, const char *fmt, ...)
443 static char buf[4096];
445 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
446 g_Engine.engine->ClientPrintf(client, buf);
450 void CSmmAPI::LoadAsVSP()
452 g_Engine.engine->ServerCommand("plugin_load \n");
453 g_Engine.engine->ServerExecute();
455 IServerPluginCallbacks *iface = NULL;
456 if (g_VspListener.IsLoaded())
458 iface = &g_VspListener;
462 CPluginManager::CPlugin *pPlugin;
463 SourceHook::List<IMetamodListener *>::iterator event;
464 IMetamodListener *pML;
465 for (iter=g_PluginMngr._begin(); iter!=g_PluginMngr._end(); iter++)
468 if (pPlugin->m_Status < Pl_Paused)
472 /* Only valid for plugins >= 10 (v1:5, SourceMM 1.4) */
473 if (pPlugin->m_API->GetApiVersion() < 10)
477 for (event=pPlugin->m_Events.begin();
478 event!=pPlugin->m_Events.end();
482 pML->OnVSPListening(iface);
487 void CSmmAPI::EnableVSPListener()
489 /* If GameInit already passed and we're not already enabled or loaded, go ahead and LoadAsVSP load */
490 if (bGameInit && !m_VSP && !g_VspListener.IsLoaded())
497 int CSmmAPI::GetGameDLLVersion()
499 return g_GameDllVersion;