sourcemm/CSmmAPI.cpp
author David Anderson <dvander@alliedmods.net>
Fri Jun 24 22:34:17 2005 +0000 (2005-06-24)
changeset 77 10eebca6ba6c
parent 76 39266faeec97
child 94 e37bb4ce37d7
permissions -rw-r--r--
Minor cleanup
     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 
    15 /**
    16  * @brief Implementation of main API interface
    17  * @file CSmmAPI.cpp
    18  */
    19 
    20 using namespace SourceMM;
    21 
    22 CSmmAPI g_SmmAPI;
    23 
    24 CSmmAPI::CSmmAPI()
    25 {
    26 	m_ConPrintf = NULL;
    27 	m_Cache = false;
    28 }
    29 
    30 ISmmPluginManager *CSmmAPI::PluginManager()
    31 {
    32 	return static_cast<ISmmPluginManager *>(&g_PluginMngr);
    33 }
    34 
    35 SourceHook::ISourceHook *CSmmAPI::SourceHook()
    36 {
    37 	return static_cast<SourceHook::ISourceHook *>(&g_SourceHook);
    38 }
    39 
    40 void CSmmAPI::LogMsg(ISmmPlugin *pl, const char *msg, ...)
    41 {
    42 	va_list ap;
    43 	static char buffer[2048];
    44 
    45 	buffer[0] = '\0';
    46 
    47 	va_start(ap, msg);
    48 	vsnprintf(buffer, sizeof(buffer)-1, msg, ap);
    49 	va_end(ap);
    50 
    51 	LogMessage("[%s] %s", pl->GetLogTag(), buffer);
    52 }
    53 
    54 CreateInterfaceFn CSmmAPI::engineFactory(bool syn)
    55 {
    56 	if (syn)
    57 		return EngineFactory;
    58 	return g_Engine.engineFactory;
    59 }
    60 
    61 CreateInterfaceFn CSmmAPI::physicsFactory(bool syn)
    62 {
    63 	if (syn)
    64 		return PhysicsFactory;
    65 	return g_Engine.physicsFactory;
    66 }
    67 
    68 CreateInterfaceFn CSmmAPI::fileSystemFactory(bool syn)
    69 {
    70 	if (syn)
    71 		return FileSystemFactory;
    72 	return g_Engine.fileSystemFactory;
    73 }
    74 
    75 CreateInterfaceFn CSmmAPI::serverFactory(bool syn)
    76 {
    77 	if (syn)
    78 		return CreateInterface;
    79 	return g_GameDll.factory;
    80 }
    81 
    82 CGlobalVars *CSmmAPI::pGlobals()
    83 {
    84 	return g_Engine.pGlobals;
    85 }
    86 
    87 void CSmmAPI::SetLastMetaReturn(META_RES res)
    88 {
    89 	m_Res = res;
    90 }
    91 
    92 META_RES CSmmAPI::GetLastMetaReturn()
    93 {
    94 	return m_Res;
    95 }
    96 
    97 IConCommandBaseAccessor *CSmmAPI::GetCvarBaseAccessor()
    98 {
    99 	return static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor);
   100 }
   101 
   102 bool CSmmAPI::RegisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
   103 {
   104 	if (pCommand->IsCommand())
   105 	{
   106 		g_PluginMngr.AddPluginCmd(plugin, pCommand);
   107 	} else {
   108 		g_PluginMngr.AddPluginCvar(plugin, pCommand);
   109 	}
   110 
   111 	return g_SMConVarAccessor.Register(pCommand);
   112 }
   113 
   114 void CSmmAPI::UnregisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
   115 {
   116 	if (pCommand->IsCommand())
   117 	{
   118 		g_PluginMngr.RemovePluginCmd(plugin, pCommand);
   119 	} else {
   120 		g_PluginMngr.RemovePluginCvar(plugin, pCommand);
   121 	}
   122 
   123 	g_SMConVarAccessor.Unregister(pCommand);
   124 }
   125 
   126 void CSmmAPI::ConPrint(const char *fmt)
   127 {
   128 	(m_ConPrintf)("%s", fmt);
   129 }
   130 
   131 void CSmmAPI::ConPrintf(const char *fmt, ...)
   132 {
   133 	va_list ap;
   134 	static char buf[4096];
   135 	va_start(ap, fmt);
   136 	vsnprintf(buf, sizeof(buf)-1, fmt, ap);
   137 	ConPrint(buf);
   138 	va_end(ap);
   139 }
   140 
   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 //
   145 //   stack.)                                                            //
   146 //////////////////////////////////////////////////////////////////////////
   147 
   148 #define SIGLEN			8
   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\x31\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
   158 
   159 bool vcmp(void *_addr1, void *_addr2, size_t len)
   160 {
   161 	unsigned char *addr1 = (unsigned char *)_addr1;
   162 	unsigned char *addr2 = (unsigned char *)_addr2;
   163 
   164 	for (size_t i=0; i<len; i++)
   165 	{
   166 		if (addr2[i] == '*')
   167 			continue;
   168 		if (addr1[i] != addr2[i])
   169 			return false;
   170 	}
   171 
   172 	return true;
   173 }
   174 
   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()
   178 {
   179 	ICvar *pCvar = g_Engine.icvar;
   180 
   181 	ConCommandBase *pBase = pCvar->GetCommands();
   182 	unsigned char *ptr = NULL;
   183 	FnCommandCallback callback = NULL;
   184 	int offs = 0;
   185 
   186 	while (pBase)
   187 	{
   188 		if ( strcmp(pBase->GetName(), "echo") == 0 )
   189 		{
   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))
   194 			{
   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;
   202 			}
   203 			if (!offs || ptr[offs-1] != IA32_CALL)
   204 			{
   205 				m_ConPrintf = (CONPRINTF_FUNC)Msg;
   206 				return false;
   207 			}
   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);
   212 
   213 			m_Cache = true;
   214 
   215 			return true;
   216 		}
   217 		pBase = const_cast<ConCommandBase *>(pBase->GetNext());
   218 	}
   219 
   220 	m_ConPrintf = (CONPRINTF_FUNC)Msg;
   221 
   222 	return false;
   223 }
   224 
   225 bool CSmmAPI::CacheSuccessful()
   226 {
   227 	return m_Cache;
   228 }