Added client version of "meta" command in order to allow clients to view version information and a list of loaded plugins.
Added ClientConPrintf to API for printing text in a client's console. Same as IVEngineServer::ClientPrintf except that it allows string formatting.
Updated changelog for possible future version.
Version bumpage.
Feel free to kill me for doing a change of this almost massive scale :-\
At least none of this breaks plugins though.
1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2006 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, PluginId *id)
147 //first check ours... we get first chance!
148 if (strcmp(iface, MMIFACE_SOURCEHOOK)==0)
152 return static_cast<void *>(static_cast<SourceHook::ISourceHook *>(&g_SourceHook));
153 } else if (strcmp(iface, MMIFACE_PLMANAGER)==0) {
156 return static_cast<void *>(static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor));
159 CPluginManager::CPlugin *pl;
160 SourceHook::List<IMetamodListener *>::iterator event;
161 IMetamodListener *api;
165 for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++)
168 for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++)
172 if ( (val=api->OnMetamodQuery(iface, &ret)) != NULL )
184 *_ret = IFACE_FAILED;
189 //////////////////////////////////////////////////////////////////////////
190 //THERE BE HAX HERE!!!! DON'T TELL ALFRED, BUT GABE WANTED IT THAT WAY. //
191 // (note: you can find the offset by looking for the text //
192 // "Echo text to console", you'll find the callback cmd pushed on the //
194 //////////////////////////////////////////////////////////////////////////
197 #define ENGINE486_SIG "\x55\x89\xE5\x53\x83\xEC\x14\xBB"
198 #define ENGINE486_OFFS 40
199 #define ENGINE686_SIG "\x53\x83\xEC\x08\xBB\x01\x00\x00"
200 #define ENGINE686_OFFS 50
201 #define ENGINEAMD_SIG "\x53\x51\xBB\x01\x00\x00\x00\x51"
202 #define ENGINEAMD_OFFS 47
203 #define ENGINEW32_SIG "\xA1\x2A\x2A\x2A\x2A\x56\xBE\x01"
204 #define ENGINEW32_OFFS 38
205 #define IA32_CALL 0xE8
207 bool vcmp(void *_addr1, void *_addr2, size_t len)
209 unsigned char *addr1 = (unsigned char *)_addr1;
210 unsigned char *addr2 = (unsigned char *)_addr2;
212 for (size_t i=0; i<len; i++)
216 if (addr1[i] != addr2[i])
223 //Thanks to fysh for the idea of extracting info from "echo" and for
224 // having the original offsets at hand!
225 bool CSmmAPI::CacheCmds()
227 ICvar *pCvar = g_Engine.icvar;
229 ConCommandBase *pBase = pCvar->GetCommands();
230 unsigned char *ptr = NULL;
231 FnCommandCallback callback = NULL;
236 if ( strcmp(pBase->GetName(), "echo") == 0 )
238 //callback = //*((FnCommandCallback *)((char *)pBase + offsetof(ConCommand, m_fnCommandCallback)));
239 callback = ((ConCommand *)pBase)->GetCallback();
240 ptr = (unsigned char *)callback;
241 if (vcmp(ptr, ENGINE486_SIG, SIGLEN))
243 offs = ENGINE486_OFFS;
244 } else if (vcmp(ptr, ENGINE686_SIG, SIGLEN)) {
245 offs = ENGINE686_OFFS;
246 } else if (vcmp(ptr, ENGINEAMD_SIG, SIGLEN)) {
247 offs = ENGINEAMD_OFFS;
248 } else if (vcmp(ptr, ENGINEW32_SIG, SIGLEN)) {
249 offs = ENGINEW32_OFFS;
251 if (!offs || ptr[offs-1] != IA32_CALL)
253 m_ConPrintf = (CONPRINTF_FUNC)Msg;
256 //get the relative offset
257 m_ConPrintf = *((CONPRINTF_FUNC *)(ptr + offs));
258 //add the base offset, to the ip (which is the address+offset + 4 bytes for next instruction)
259 m_ConPrintf = (CONPRINTF_FUNC)((unsigned long)m_ConPrintf + (unsigned long)(ptr + offs) + 4);
265 pBase = const_cast<ConCommandBase *>(pBase->GetNext());
268 m_ConPrintf = (CONPRINTF_FUNC)Msg;
273 bool CSmmAPI::CacheSuccessful()
278 void CSmmAPI::GetApiVersions(int &major, int &minor, int &plvers, int &plmin)
280 major = SM_VERS_API_MAJOR;
281 minor = SM_VERS_API_MINOR;
282 plvers = PLAPI_VERSION;
283 plmin = PLAPI_MIN_VERSION;
286 void CSmmAPI::GetShVersions(int &shvers, int &shimpl)
288 shvers = SH_IFACE_VERSION;
289 shimpl = SH_IMPL_VERSION;
292 int CSmmAPI::FormatIface(char iface[], unsigned int maxlength)
294 unsigned int length = (unsigned int)strlen(iface);
298 for (i=length-1; i>=0; i--)
300 if (!isdigit(iface[i]))
304 if ( (num && (maxlength <= length)) || (!num && (maxlength <= length+3)) )
310 num = atoi(&(iface[++i]));
314 snprintf(&(iface[i]), 4, "%03d", num);
319 void *CSmmAPI::InterfaceSearch(CreateInterfaceFn fn, const char *iface, int max, int *ret)
321 size_t len = strlen(iface);
324 char *_if = new char[len + 2];
333 if ( (pf = (fn)(_if, ret)) != NULL )
337 } while ( num = FormatIface(_if, len) );
344 const char *CSmmAPI::GetBaseDir()
346 return g_ModPath.c_str();
349 void CSmmAPI::PathFormat(char *buffer, size_t len, const char *fmt, ...)
353 size_t mylen = vsnprintf(buffer, len, fmt, ap);
356 for (size_t i=0; i<mylen; i++)
358 if (buffer[i] == ALT_SEP_CHAR)
359 buffer[i] = PATH_SEP_CHAR;
363 void CSmmAPI::ClientConPrintf(edict_t *client, const char *fmt, ...)
366 static char buf[4096];
368 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
369 g_Engine.engine->ClientPrintf(client, buf);