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"
16 * @brief Implementation of main API interface
20 using namespace SourceMM;
30 ISmmPluginManager *CSmmAPI::PluginManager()
32 return static_cast<ISmmPluginManager *>(&g_PluginMngr);
35 SourceHook::ISourceHook *CSmmAPI::SourceHook()
37 return static_cast<SourceHook::ISourceHook *>(&g_SourceHook);
40 void CSmmAPI::LogMsg(ISmmPlugin *pl, const char *msg, ...)
43 static char buffer[2048];
48 vsnprintf(buffer, sizeof(buffer)-1, msg, ap);
51 LogMessage("[%s] %s", pl->GetLogTag(), buffer);
54 CreateInterfaceFn CSmmAPI::engineFactory(bool syn)
58 return g_Engine.engineFactory;
61 CreateInterfaceFn CSmmAPI::physicsFactory(bool syn)
64 return PhysicsFactory;
65 return g_Engine.physicsFactory;
68 CreateInterfaceFn CSmmAPI::fileSystemFactory(bool syn)
71 return FileSystemFactory;
72 return g_Engine.fileSystemFactory;
75 CreateInterfaceFn CSmmAPI::serverFactory(bool syn)
78 return CreateInterface;
79 return g_GameDll.factory;
82 CGlobalVars *CSmmAPI::pGlobals()
84 return g_Engine.pGlobals;
87 void CSmmAPI::SetLastMetaReturn(META_RES res)
92 META_RES CSmmAPI::GetLastMetaReturn()
97 IConCommandBaseAccessor *CSmmAPI::GetCvarBaseAccessor()
99 return static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor);
102 bool CSmmAPI::RegisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
104 if (pCommand->IsCommand())
106 g_PluginMngr.AddPluginCmd(plugin, pCommand);
108 g_PluginMngr.AddPluginCvar(plugin, pCommand);
111 return g_SMConVarAccessor.Register(pCommand);
114 void CSmmAPI::UnregisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
116 if (pCommand->IsCommand())
118 g_PluginMngr.RemovePluginCmd(plugin, pCommand);
120 g_PluginMngr.RemovePluginCvar(plugin, pCommand);
123 g_SMConVarAccessor.Unregister(pCommand);
126 void CSmmAPI::ConPrint(const char *fmt)
128 (m_ConPrintf)("%s", fmt);
131 void CSmmAPI::ConPrintf(const char *fmt, ...)
134 static char buf[4096];
136 vsnprintf(buf, sizeof(buf)-1, fmt, ap);
141 //////////////////////////////////////////////////////////////////////////
142 //THERE BE HAX HERE!!!! DON'T TELL ALFRED, BUT GABE WANTED IT THAT WAY. //
143 // (note: you can find the offset by looking for the text //
144 // "Echo text to console", you'll find the callback cmd pushed on the //
146 //////////////////////////////////////////////////////////////////////////
149 #define ENGINE486_SIG "\x55\x89\xE5\x53\x83\xEC\x14\xBB"
150 #define ENGINE486_OFFS 40
151 #define ENGINE686_SIG "\x53\x83\xEC\x08\xBB\x01\x00\x00"
152 #define ENGINE686_OFFS 50
153 #define ENGINEAMD_SIG "\x53\x51\xBB\x01\x00\x00\x00\x51"
154 #define ENGINEAMD_OFFS 47
155 #define ENGINEW32_SIG "\xA1\x2A\x2A\x2A\x2A\x56\xBE\x01"
156 #define ENGINEW32_OFFS 38
157 #define IA32_CALL 0xE8
159 bool vcmp(void *_addr1, void *_addr2, size_t len)
161 unsigned char *addr1 = (unsigned char *)_addr1;
162 unsigned char *addr2 = (unsigned char *)_addr2;
164 for (size_t i=0; i<len; i++)
168 if (addr1[i] != addr2[i])
175 //Thanks to fysh for the idea of extracting info from "echo" and for
176 // having the original offsets at hand!
177 bool CSmmAPI::CacheCmds()
179 ICvar *pCvar = g_Engine.icvar;
181 ConCommandBase *pBase = pCvar->GetCommands();
182 unsigned char *ptr = NULL;
183 FnCommandCallback callback = NULL;
188 if ( strcmp(pBase->GetName(), "echo") == 0 )
190 //callback = //*((FnCommandCallback *)((char *)pBase + offsetof(ConCommand, m_fnCommandCallback)));
191 callback = ((ConCommand *)pBase)->GetCallback();
192 ptr = (unsigned char *)callback;
193 if (vcmp(ptr, ENGINE486_SIG, SIGLEN))
195 offs = ENGINE486_OFFS;
196 } else if (vcmp(ptr, ENGINE686_SIG, SIGLEN)) {
197 offs = ENGINE686_OFFS;
198 } else if (vcmp(ptr, ENGINEAMD_SIG, SIGLEN)) {
199 offs = ENGINEAMD_OFFS;
200 } else if (vcmp(ptr, ENGINEW32_SIG, SIGLEN)) {
201 offs = ENGINEW32_OFFS;
203 if (!offs || ptr[offs-1] != IA32_CALL)
205 m_ConPrintf = (CONPRINTF_FUNC)Msg;
208 //get the relative offset
209 m_ConPrintf = *((CONPRINTF_FUNC *)(ptr + offs));
210 //add the base offset, to the ip (which is the address+offset + 4 bytes for next instruction)
211 m_ConPrintf = (CONPRINTF_FUNC)((unsigned long)m_ConPrintf + (unsigned long)(ptr + offs) + 4);
217 pBase = const_cast<ConCommandBase *>(pBase->GetNext());
220 m_ConPrintf = (CONPRINTF_FUNC)Msg;
225 bool CSmmAPI::CacheSuccessful()