sourcemm/CSmmAPI.cpp
author David Anderson <dvander@alliedmods.net>
Thu Oct 06 21:59:10 2005 +0000 (2005-10-06)
changeset 124 b04971393ee4
parent 94 e37bb4ce37d7
child 138 dfa3b31cc042
permissions -rw-r--r--
Finalized versioning and PI
Added QueryRunning() output to meta info/list.
     1 /* ======== SourceMM ========
     2 * Copyright (C) 2004-2005 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): David "BAILOPAN" Anderson
     8 * ============================
     9 */
    10 
    11 #include "CSmmAPI.h"
    12 #include "sourcemm.h"
    13 #include "concommands.h"
    14 #include "CPlugin.h"
    15 
    16 /**
    17  * @brief Implementation of main API interface
    18  * @file CSmmAPI.cpp
    19  */
    20 
    21 using namespace SourceMM;
    22 
    23 CSmmAPI g_SmmAPI;
    24 
    25 CSmmAPI::CSmmAPI()
    26 {
    27 	m_ConPrintf = NULL;
    28 	m_Cache = false;
    29 }
    30 
    31 ISmmPluginManager *CSmmAPI::PluginManager()
    32 {
    33 	return static_cast<ISmmPluginManager *>(&g_PluginMngr);
    34 }
    35 
    36 SourceHook::ISourceHook *CSmmAPI::SourceHook()
    37 {
    38 	return static_cast<SourceHook::ISourceHook *>(&g_SourceHook);
    39 }
    40 
    41 void CSmmAPI::LogMsg(ISmmPlugin *pl, const char *msg, ...)
    42 {
    43 	va_list ap;
    44 	static char buffer[2048];
    45 
    46 	buffer[0] = '\0';
    47 
    48 	va_start(ap, msg);
    49 	vsnprintf(buffer, sizeof(buffer)-1, msg, ap);
    50 	va_end(ap);
    51 
    52 	LogMessage("[%s] %s", pl->GetLogTag(), buffer);
    53 }
    54 
    55 CreateInterfaceFn CSmmAPI::engineFactory(bool syn)
    56 {
    57 	if (syn)
    58 		return EngineFactory;
    59 	return g_Engine.engineFactory;
    60 }
    61 
    62 CreateInterfaceFn CSmmAPI::physicsFactory(bool syn)
    63 {
    64 	if (syn)
    65 		return PhysicsFactory;
    66 	return g_Engine.physicsFactory;
    67 }
    68 
    69 CreateInterfaceFn CSmmAPI::fileSystemFactory(bool syn)
    70 {
    71 	if (syn)
    72 		return FileSystemFactory;
    73 	return g_Engine.fileSystemFactory;
    74 }
    75 
    76 CreateInterfaceFn CSmmAPI::serverFactory(bool syn)
    77 {
    78 	if (syn)
    79 		return CreateInterface;
    80 	return g_GameDll.factory;
    81 }
    82 
    83 CGlobalVars *CSmmAPI::pGlobals()
    84 {
    85 	return g_Engine.pGlobals;
    86 }
    87 
    88 void CSmmAPI::SetLastMetaReturn(META_RES res)
    89 {
    90 	m_Res = res;
    91 }
    92 
    93 META_RES CSmmAPI::GetLastMetaReturn()
    94 {
    95 	return m_Res;
    96 }
    97 
    98 IConCommandBaseAccessor *CSmmAPI::GetCvarBaseAccessor()
    99 {
   100 	return static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor);
   101 }
   102 
   103 bool CSmmAPI::RegisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
   104 {
   105 	if (pCommand->IsCommand())
   106 	{
   107 		g_PluginMngr.AddPluginCmd(plugin, pCommand);
   108 	} else {
   109 		g_PluginMngr.AddPluginCvar(plugin, pCommand);
   110 	}
   111 
   112 	return g_SMConVarAccessor.Register(pCommand);
   113 }
   114 
   115 void CSmmAPI::UnregisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
   116 {
   117 	if (pCommand->IsCommand())
   118 	{
   119 		g_PluginMngr.RemovePluginCmd(plugin, pCommand);
   120 	} else {
   121 		g_PluginMngr.RemovePluginCvar(plugin, pCommand);
   122 	}
   123 
   124 	g_SMConVarAccessor.Unregister(pCommand);
   125 }
   126 
   127 void CSmmAPI::ConPrint(const char *fmt)
   128 {
   129 	(m_ConPrintf)("%s", fmt);
   130 }
   131 
   132 void CSmmAPI::ConPrintf(const char *fmt, ...)
   133 {
   134 	va_list ap;
   135 	static char buf[4096];
   136 	va_start(ap, fmt);
   137 	vsnprintf(buf, sizeof(buf)-1, fmt, ap);
   138 	ConPrint(buf);
   139 	va_end(ap);
   140 }
   141 
   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 //
   146 //   stack.)                                                            //
   147 //////////////////////////////////////////////////////////////////////////
   148 
   149 #define SIGLEN			8
   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
   159 
   160 bool vcmp(void *_addr1, void *_addr2, size_t len)
   161 {
   162 	unsigned char *addr1 = (unsigned char *)_addr1;
   163 	unsigned char *addr2 = (unsigned char *)_addr2;
   164 
   165 	for (size_t i=0; i<len; i++)
   166 	{
   167 		if (addr2[i] == '*')
   168 			continue;
   169 		if (addr1[i] != addr2[i])
   170 			return false;
   171 	}
   172 
   173 	return true;
   174 }
   175 
   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()
   179 {
   180 	ICvar *pCvar = g_Engine.icvar;
   181 
   182 	ConCommandBase *pBase = pCvar->GetCommands();
   183 	unsigned char *ptr = NULL;
   184 	FnCommandCallback callback = NULL;
   185 	int offs = 0;
   186 
   187 	while (pBase)
   188 	{
   189 		if ( strcmp(pBase->GetName(), "echo") == 0 )
   190 		{
   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))
   195 			{
   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;
   203 			}
   204 			if (!offs || ptr[offs-1] != IA32_CALL)
   205 			{
   206 				m_ConPrintf = (CONPRINTF_FUNC)Msg;
   207 				return false;
   208 			}
   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);
   213 
   214 			m_Cache = true;
   215 
   216 			return true;
   217 		}
   218 		pBase = const_cast<ConCommandBase *>(pBase->GetNext());
   219 	}
   220 
   221 	m_ConPrintf = (CONPRINTF_FUNC)Msg;
   222 
   223 	return false;
   224 }
   225 
   226 bool CSmmAPI::CacheSuccessful()
   227 {
   228 	return m_Cache;
   229 }
   230 
   231 void CSmmAPI::GetApiVersions(int &major, int &minor, int &plvers, int &plmin)
   232 {
   233 	major = SM_VERS_API_MAJOR;
   234 	minor = SM_VERS_API_MINOR;
   235 	plvers = PLAPI_VERSION;
   236 	plmin = PLAPI_MIN_VERSION;
   237 }
   238 
   239 void CSmmAPI::GetShVersions(int &shvers, int &shimpl)
   240 {
   241 	shvers = SH_IFACE_VERSION;
   242 	shimpl = SH_IMPL_VERSION;
   243 }