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 ISmmPluginManager *CSmmAPI::PluginManager()
33 return static_cast<ISmmPluginManager *>(&g_PluginMngr);
36 SourceHook::ISourceHook *CSmmAPI::SourceHook()
38 return static_cast<SourceHook::ISourceHook *>(&g_SourceHook);
41 void CSmmAPI::LogMsg(ISmmPlugin *pl, const char *msg, ...)
44 static char buffer[2048];
49 vsnprintf(buffer, sizeof(buffer)-1, msg, ap);
52 LogMessage("[%s] %s", pl->GetLogTag(), buffer);
55 CreateInterfaceFn CSmmAPI::engineFactory(bool syn)
59 return g_Engine.engineFactory;
62 CreateInterfaceFn CSmmAPI::physicsFactory(bool syn)
65 return PhysicsFactory;
66 return g_Engine.physicsFactory;
69 CreateInterfaceFn CSmmAPI::fileSystemFactory(bool syn)
72 return FileSystemFactory;
73 return g_Engine.fileSystemFactory;
76 CreateInterfaceFn CSmmAPI::serverFactory(bool syn)
79 return CreateInterface;
80 return g_GameDll.factory;
83 CGlobalVars *CSmmAPI::pGlobals()
85 return g_Engine.pGlobals;
88 void CSmmAPI::SetLastMetaReturn(META_RES res)
93 META_RES CSmmAPI::GetLastMetaReturn()
98 IConCommandBaseAccessor *CSmmAPI::GetCvarBaseAccessor()
100 return static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor);
103 bool CSmmAPI::RegisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
105 if (pCommand->IsCommand())
107 g_PluginMngr.AddPluginCmd(plugin, pCommand);
109 g_PluginMngr.AddPluginCvar(plugin, pCommand);
112 return g_SMConVarAccessor.Register(pCommand);
115 void CSmmAPI::UnregisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
117 if (pCommand->IsCommand())
119 g_PluginMngr.RemovePluginCmd(plugin, pCommand);
121 g_PluginMngr.RemovePluginCvar(plugin, pCommand);
124 g_SMConVarAccessor.Unregister(pCommand);
127 void CSmmAPI::ConPrint(const char *fmt)
129 (m_ConPrintf)("%s", fmt);
132 void CSmmAPI::ConPrintf(const char *fmt, ...)
135 static char buf[4096];
137 vsnprintf(buf, sizeof(buf)-1, fmt, ap);
142 //////////////////////////////////////////////////////////////////////////
143 //THERE BE HAX HERE!!!! DON'T TELL ALFRED, BUT GABE WANTED IT THAT WAY. //
144 // (note: you can find the offset by looking for the text //
145 // "Echo text to console", you'll find the callback cmd pushed on the //
147 //////////////////////////////////////////////////////////////////////////
150 #define ENGINE486_SIG "\x55\x89\xE5\x53\x83\xEC\x14\xBB"
151 #define ENGINE486_OFFS 40
152 #define ENGINE686_SIG "\x53\x83\xEC\x08\xBB\x01\x00\x00"
153 #define ENGINE686_OFFS 50
154 #define ENGINEAMD_SIG "\x53\x51\xBB\x01\x00\x00\x00\x51"
155 #define ENGINEAMD_OFFS 47
156 #define ENGINEW32_SIG "\xA1\x2A\x2A\x2A\x2A\x56\xBE\x01"
157 #define ENGINEW32_OFFS 38
158 #define IA32_CALL 0xE8
160 bool vcmp(void *_addr1, void *_addr2, size_t len)
162 unsigned char *addr1 = (unsigned char *)_addr1;
163 unsigned char *addr2 = (unsigned char *)_addr2;
165 for (size_t i=0; i<len; i++)
169 if (addr1[i] != addr2[i])
176 //Thanks to fysh for the idea of extracting info from "echo" and for
177 // having the original offsets at hand!
178 bool CSmmAPI::CacheCmds()
180 ICvar *pCvar = g_Engine.icvar;
182 ConCommandBase *pBase = pCvar->GetCommands();
183 unsigned char *ptr = NULL;
184 FnCommandCallback callback = NULL;
189 if ( strcmp(pBase->GetName(), "echo") == 0 )
191 //callback = //*((FnCommandCallback *)((char *)pBase + offsetof(ConCommand, m_fnCommandCallback)));
192 callback = ((ConCommand *)pBase)->GetCallback();
193 ptr = (unsigned char *)callback;
194 if (vcmp(ptr, ENGINE486_SIG, SIGLEN))
196 offs = ENGINE486_OFFS;
197 } else if (vcmp(ptr, ENGINE686_SIG, SIGLEN)) {
198 offs = ENGINE686_OFFS;
199 } else if (vcmp(ptr, ENGINEAMD_SIG, SIGLEN)) {
200 offs = ENGINEAMD_OFFS;
201 } else if (vcmp(ptr, ENGINEW32_SIG, SIGLEN)) {
202 offs = ENGINEW32_OFFS;
204 if (!offs || ptr[offs-1] != IA32_CALL)
206 m_ConPrintf = (CONPRINTF_FUNC)Msg;
209 //get the relative offset
210 m_ConPrintf = *((CONPRINTF_FUNC *)(ptr + offs));
211 //add the base offset, to the ip (which is the address+offset + 4 bytes for next instruction)
212 m_ConPrintf = (CONPRINTF_FUNC)((unsigned long)m_ConPrintf + (unsigned long)(ptr + offs) + 4);
218 pBase = const_cast<ConCommandBase *>(pBase->GetNext());
221 m_ConPrintf = (CONPRINTF_FUNC)Msg;
226 bool CSmmAPI::CacheSuccessful()
231 void CSmmAPI::GetApiVersions(int &major, int &minor, int &plvers, int &plmin)
233 major = SM_VERS_API_MAJOR;
234 minor = SM_VERS_API_MINOR;
235 plvers = PLAPI_VERSION;
236 plmin = PLAPI_MIN_VERSION;
239 void CSmmAPI::GetShVersions(int &shvers, int &shimpl)
241 shvers = SH_IFACE_VERSION;
242 shimpl = SH_IMPL_VERSION;