Renamed convar.h to convar_smm.h in order to avoid potential compiling problems with the order of include directories.
1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2006 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"
18 * @brief Implementation of main API interface
22 using namespace SourceMM;
32 void CSmmAPI::LogMsg(ISmmPlugin *pl, const char *msg, ...)
35 static char buffer[2048];
40 vsnprintf(buffer, sizeof(buffer)-1, msg, ap);
43 LogMessage("[%s] %s", pl->GetLogTag(), buffer);
46 CreateInterfaceFn CSmmAPI::engineFactory(bool syn)
50 return g_Engine.engineFactory;
53 CreateInterfaceFn CSmmAPI::physicsFactory(bool syn)
56 return PhysicsFactory;
57 return g_Engine.physicsFactory;
60 CreateInterfaceFn CSmmAPI::fileSystemFactory(bool syn)
63 return FileSystemFactory;
64 return g_Engine.fileSystemFactory;
67 CreateInterfaceFn CSmmAPI::serverFactory(bool syn)
70 return CreateInterface;
71 return g_GameDll.factory;
74 CGlobalVars *CSmmAPI::pGlobals()
76 return g_Engine.pGlobals;
79 void CSmmAPI::SetLastMetaReturn(META_RES res)
84 META_RES CSmmAPI::GetLastMetaReturn()
89 IConCommandBaseAccessor *CSmmAPI::GetCvarBaseAccessor()
91 return static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor);
94 bool CSmmAPI::RegisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
96 if (pCommand->IsCommand())
98 g_PluginMngr.AddPluginCmd(plugin, pCommand);
100 g_PluginMngr.AddPluginCvar(plugin, pCommand);
103 return g_SMConVarAccessor.Register(pCommand);
106 void CSmmAPI::UnregisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
108 if (pCommand->IsCommand())
110 g_PluginMngr.RemovePluginCmd(plugin, pCommand);
112 g_PluginMngr.RemovePluginCvar(plugin, pCommand);
115 g_SMConVarAccessor.Unregister(pCommand);
118 void CSmmAPI::ConPrint(const char *fmt)
120 (m_ConPrintf)("%s", fmt);
123 void CSmmAPI::ConPrintf(const char *fmt, ...)
126 static char buf[4096];
128 vsnprintf(buf, sizeof(buf)-1, fmt, ap);
133 void CSmmAPI::AddListener(ISmmPlugin *plugin, IMetamodListener *pListener)
135 CPluginManager::CPlugin *pl = g_PluginMngr.FindByAPI(plugin);
137 pl->m_Events.push_back(pListener);
140 void *CSmmAPI::MetaFactory(const char *iface, int *_ret, PluginId *id)
148 //first check ours... we get first chance!
149 if (strcmp(iface, MMIFACE_SOURCEHOOK) == 0)
153 return static_cast<void *>(static_cast<SourceHook::ISourceHook *>(&g_SourceHook));
154 } else if (strcmp(iface, MMIFACE_PLMANAGER) == 0) {
157 return static_cast<void *>(static_cast<ISmmPluginManager *>(&g_PluginMngr));
160 CPluginManager::CPlugin *pl;
161 SourceHook::List<IMetamodListener *>::iterator event;
162 IMetamodListener *api;
166 for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++)
169 for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++)
173 if ( (val=api->OnMetamodQuery(iface, &ret)) != NULL )
185 *_ret = IFACE_FAILED;
190 //////////////////////////////////////////////////////////////////////////
191 //THERE BE HAX HERE!!!! DON'T TELL ALFRED, BUT GABE WANTED IT THAT WAY. //
192 // (note: you can find the offset by looking for the text //
193 // "Echo text to console", you'll find the callback cmd pushed on the //
195 //////////////////////////////////////////////////////////////////////////
198 #define ENGINE486_SIG "\x55\x89\xE5\x53\x83\xEC\x14\xBB"
199 #define ENGINE486_OFFS 40
200 #define ENGINE686_SIG "\x53\x83\xEC\x08\xBB\x01\x00\x00"
201 #define ENGINE686_OFFS 50
202 #define ENGINEAMD_SIG "\x53\x51\xBB\x01\x00\x00\x00\x51"
203 #define ENGINEAMD_OFFS 47
204 #define ENGINEW32_SIG "\xA1\x2A\x2A\x2A\x2A\x56\xBE\x01"
205 #define ENGINEW32_OFFS 38
206 #define IA32_CALL 0xE8
208 bool vcmp(const void *_addr1, const void *_addr2, size_t len)
210 unsigned char *addr1 = (unsigned char *)_addr1;
211 unsigned char *addr2 = (unsigned char *)_addr2;
213 for (size_t i=0; i<len; i++)
217 if (addr1[i] != addr2[i])
224 //Thanks to fysh for the idea of extracting info from "echo" and for
225 // having the original offsets at hand!
226 bool CSmmAPI::CacheCmds()
228 ICvar *pCvar = g_Engine.icvar;
230 ConCommandBase *pBase = pCvar->GetCommands();
231 unsigned char *ptr = NULL;
232 FnCommandCallback callback = NULL;
237 if ( strcmp(pBase->GetName(), "echo") == 0 )
239 //callback = //*((FnCommandCallback *)((char *)pBase + offsetof(ConCommand, m_fnCommandCallback)));
240 callback = ((ConCommand *)pBase)->GetCallback();
241 ptr = (unsigned char *)callback;
243 if (vcmp(ptr, ENGINE486_SIG, SIGLEN))
245 offs = ENGINE486_OFFS;
247 else if (vcmp(ptr, ENGINE686_SIG, SIGLEN))
249 offs = ENGINE686_OFFS;
251 else if (vcmp(ptr, ENGINEAMD_SIG, SIGLEN))
253 offs = ENGINEAMD_OFFS;
255 #elif defined OS_WIN32 // Only one Windows engine binary so far...
256 if (vcmp(ptr, ENGINEW32_SIG, SIGLEN))
258 offs = ENGINEW32_OFFS;
262 if (!offs || ptr[offs-1] != IA32_CALL)
264 m_ConPrintf = (CONPRINTF_FUNC)Msg;
267 //get the relative offset
268 m_ConPrintf = *((CONPRINTF_FUNC *)(ptr + offs));
269 //add the base offset, to the ip (which is the address+offset + 4 bytes for next instruction)
270 m_ConPrintf = (CONPRINTF_FUNC)((unsigned long)m_ConPrintf + (unsigned long)(ptr + offs) + 4);
276 pBase = const_cast<ConCommandBase *>(pBase->GetNext());
279 m_ConPrintf = (CONPRINTF_FUNC)Msg;
284 bool CSmmAPI::CacheSuccessful()
289 void CSmmAPI::GetApiVersions(int &major, int &minor, int &plvers, int &plmin)
291 major = SM_VERS_API_MAJOR;
292 minor = SM_VERS_API_MINOR;
293 plvers = PLAPI_VERSION;
294 plmin = PLAPI_MIN_VERSION;
297 void CSmmAPI::GetShVersions(int &shvers, int &shimpl)
299 shvers = SH_IFACE_VERSION;
300 shimpl = SH_IMPL_VERSION;
303 int CSmmAPI::FormatIface(char iface[], unsigned int maxlength)
305 int length = (int)strlen(iface);
309 for (i=length-1; i>=0; i--)
311 if (!isdigit(iface[i]))
321 if ( (num && ((int)maxlength <= length)) || (!num && ((int)maxlength <= length+3)) )
327 num = atoi(&(iface[++i]));
331 snprintf(&(iface[i]), 4, "%03d", num);
336 void *CSmmAPI::InterfaceSearch(CreateInterfaceFn fn, const char *iface, int max, int *ret)
338 char _if[256]; /* assume no interface goes beyond this */
339 size_t len = strlen(iface);
346 if (len + 4 > sizeof(_if))
359 if ( (pf = (fn)(_if, ret)) != NULL )
363 } while (( num = FormatIface(_if, len+1) ));
368 void *CSmmAPI::VInterfaceMatch(CreateInterfaceFn fn, const char *iface, int min)
370 char buffer[256]; /* assume no interface will go beyond this */
371 int len = static_cast<int>(strlen(iface));
372 int ret; /* just in case something doesn't handle NULL properly */
374 if (len > static_cast<int>(sizeof(buffer) - 4))
379 strcpy(buffer, iface);
383 char *ptr = &buffer[len-1];
385 while (isdigit(*ptr) && digits <=3)
393 /* for now, assume this is an error */
394 strcpy(buffer, iface);
397 min = (min == 0) ? 1 : min;
398 snprintf(num, sizeof(num), "%03d", min);
403 return InterfaceSearch(fn, buffer, IFACE_MAXNUM, &ret);
406 const char *CSmmAPI::GetBaseDir()
408 return g_ModPath.c_str();
411 void CSmmAPI::PathFormat(char *buffer, size_t len, const char *fmt, ...)
415 size_t mylen = vsnprintf(buffer, len, fmt, ap);
418 if (mylen == 0xFFFFFFFF || mylen >= len)
423 for (size_t i=0; i<mylen; i++)
425 if (buffer[i] == ALT_SEP_CHAR)
426 buffer[i] = PATH_SEP_CHAR;
430 void CSmmAPI::ClientConPrintf(edict_t *client, const char *fmt, ...)
433 static char buf[4096];
435 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
436 g_Engine.engine->ClientPrintf(client, buf);