1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2005 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): David "BAILOPAN" Anderson
8 * ============================
13 #include "concommands.h"
17 * @brief Implementation of main API interface
21 using namespace SourceMM;
31 void CSmmAPI::LogMsg(ISmmPlugin *pl, const char *msg, ...)
34 static char buffer[2048];
39 vsnprintf(buffer, sizeof(buffer)-1, msg, ap);
42 LogMessage("[%s] %s", pl->GetLogTag(), buffer);
45 CreateInterfaceFn CSmmAPI::engineFactory(bool syn)
49 return g_Engine.engineFactory;
52 CreateInterfaceFn CSmmAPI::physicsFactory(bool syn)
55 return PhysicsFactory;
56 return g_Engine.physicsFactory;
59 CreateInterfaceFn CSmmAPI::fileSystemFactory(bool syn)
62 return FileSystemFactory;
63 return g_Engine.fileSystemFactory;
66 CreateInterfaceFn CSmmAPI::serverFactory(bool syn)
69 return CreateInterface;
70 return g_GameDll.factory;
73 CGlobalVars *CSmmAPI::pGlobals()
75 return g_Engine.pGlobals;
78 void CSmmAPI::SetLastMetaReturn(META_RES res)
83 META_RES CSmmAPI::GetLastMetaReturn()
88 IConCommandBaseAccessor *CSmmAPI::GetCvarBaseAccessor()
90 return static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor);
93 bool CSmmAPI::RegisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
95 if (pCommand->IsCommand())
97 g_PluginMngr.AddPluginCmd(plugin, pCommand);
99 g_PluginMngr.AddPluginCvar(plugin, pCommand);
102 return g_SMConVarAccessor.Register(pCommand);
105 void CSmmAPI::UnregisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
107 if (pCommand->IsCommand())
109 g_PluginMngr.RemovePluginCmd(plugin, pCommand);
111 g_PluginMngr.RemovePluginCvar(plugin, pCommand);
114 g_SMConVarAccessor.Unregister(pCommand);
117 void CSmmAPI::ConPrint(const char *fmt)
119 (m_ConPrintf)("%s", fmt);
122 void CSmmAPI::ConPrintf(const char *fmt, ...)
125 static char buf[4096];
127 vsnprintf(buf, sizeof(buf)-1, fmt, ap);
132 void CSmmAPI::AddListener(ISmmPlugin *plugin, IMetamodListener *pListener)
134 CPluginManager::CPlugin *pl = g_PluginMngr.FindByAPI(plugin);
136 pl->m_Events.push_back(pListener);
139 void *CSmmAPI::MetaFactory(const char *iface, int *_ret)
144 //first check ours... we get first chance!
145 if (strcmp(iface, MMIFACE_SOURCEHOOK)==0)
149 return static_cast<void *>(static_cast<SourceHook::ISourceHook *>(&g_SourceHook));
150 } else if (strcmp(iface, MMIFACE_PLMANAGER)==0) {
153 return static_cast<void *>(static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor));
156 CPluginManager::CPlugin *pl;
157 SourceHook::List<IMetamodListener *>::iterator event;
158 IMetamodListener *api;
162 for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++)
165 for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++)
169 if ( (val=api->OnMetamodQuery(iface, &ret)) != NULL )
179 *_ret = IFACE_FAILED;
184 //////////////////////////////////////////////////////////////////////////
185 //THERE BE HAX HERE!!!! DON'T TELL ALFRED, BUT GABE WANTED IT THAT WAY. //
186 // (note: you can find the offset by looking for the text //
187 // "Echo text to console", you'll find the callback cmd pushed on the //
189 //////////////////////////////////////////////////////////////////////////
192 #define ENGINE486_SIG "\x55\x89\xE5\x53\x83\xEC\x14\xBB"
193 #define ENGINE486_OFFS 40
194 #define ENGINE686_SIG "\x53\x83\xEC\x08\xBB\x01\x00\x00"
195 #define ENGINE686_OFFS 50
196 #define ENGINEAMD_SIG "\x53\x51\xBB\x01\x00\x00\x00\x51"
197 #define ENGINEAMD_OFFS 47
198 #define ENGINEW32_SIG "\xA1\x2A\x2A\x2A\x2A\x56\xBE\x01"
199 #define ENGINEW32_OFFS 38
200 #define IA32_CALL 0xE8
202 bool vcmp(void *_addr1, void *_addr2, size_t len)
204 unsigned char *addr1 = (unsigned char *)_addr1;
205 unsigned char *addr2 = (unsigned char *)_addr2;
207 for (size_t i=0; i<len; i++)
211 if (addr1[i] != addr2[i])
218 //Thanks to fysh for the idea of extracting info from "echo" and for
219 // having the original offsets at hand!
220 bool CSmmAPI::CacheCmds()
222 ICvar *pCvar = g_Engine.icvar;
224 ConCommandBase *pBase = pCvar->GetCommands();
225 unsigned char *ptr = NULL;
226 FnCommandCallback callback = NULL;
231 if ( strcmp(pBase->GetName(), "echo") == 0 )
233 //callback = //*((FnCommandCallback *)((char *)pBase + offsetof(ConCommand, m_fnCommandCallback)));
234 callback = ((ConCommand *)pBase)->GetCallback();
235 ptr = (unsigned char *)callback;
236 if (vcmp(ptr, ENGINE486_SIG, SIGLEN))
238 offs = ENGINE486_OFFS;
239 } else if (vcmp(ptr, ENGINE686_SIG, SIGLEN)) {
240 offs = ENGINE686_OFFS;
241 } else if (vcmp(ptr, ENGINEAMD_SIG, SIGLEN)) {
242 offs = ENGINEAMD_OFFS;
243 } else if (vcmp(ptr, ENGINEW32_SIG, SIGLEN)) {
244 offs = ENGINEW32_OFFS;
246 if (!offs || ptr[offs-1] != IA32_CALL)
248 m_ConPrintf = (CONPRINTF_FUNC)Msg;
251 //get the relative offset
252 m_ConPrintf = *((CONPRINTF_FUNC *)(ptr + offs));
253 //add the base offset, to the ip (which is the address+offset + 4 bytes for next instruction)
254 m_ConPrintf = (CONPRINTF_FUNC)((unsigned long)m_ConPrintf + (unsigned long)(ptr + offs) + 4);
260 pBase = const_cast<ConCommandBase *>(pBase->GetNext());
263 m_ConPrintf = (CONPRINTF_FUNC)Msg;
268 bool CSmmAPI::CacheSuccessful()
273 void CSmmAPI::GetApiVersions(int &major, int &minor, int &plvers, int &plmin)
275 major = SM_VERS_API_MAJOR;
276 minor = SM_VERS_API_MINOR;
277 plvers = PLAPI_VERSION;
278 plmin = PLAPI_MIN_VERSION;
281 void CSmmAPI::GetShVersions(int &shvers, int &shimpl)
283 shvers = SH_IFACE_VERSION;
284 shimpl = SH_IMPL_VERSION;