sourcemm/CSmmAPI.cpp
changeset 76 39266faeec97
parent 67 1753f7a9b2f2
child 77 10eebca6ba6c
     1.1 --- a/sourcemm/CSmmAPI.cpp	Fri May 27 21:39:30 2005 +0000
     1.2 +++ b/sourcemm/CSmmAPI.cpp	Fri Jun 24 22:26:19 2005 +0000
     1.3 @@ -21,6 +21,12 @@
     1.4  
     1.5  CSmmAPI g_SmmAPI;
     1.6  
     1.7 +CSmmAPI::CSmmAPI()
     1.8 +{
     1.9 +	m_ConPrintf = NULL;
    1.10 +	m_Cache = false;
    1.11 +}
    1.12 +
    1.13  ISmmPluginManager *CSmmAPI::PluginManager()
    1.14  {
    1.15  	return static_cast<ISmmPluginManager *>(&g_PluginMngr);
    1.16 @@ -117,3 +123,107 @@
    1.17  	g_SMConVarAccessor.Unregister(pCommand);
    1.18  }
    1.19  
    1.20 +void CSmmAPI::ConPrint(const char *fmt)
    1.21 +{
    1.22 +	(m_ConPrintf)("%s", fmt);
    1.23 +}
    1.24 +
    1.25 +void CSmmAPI::ConPrintf(const char *fmt, ...)
    1.26 +{
    1.27 +	va_list ap;
    1.28 +	static char buf[4096];
    1.29 +	va_start(ap, fmt);
    1.30 +	vsnprintf(buf, sizeof(buf)-1, fmt, ap);
    1.31 +	ConPrint(buf);
    1.32 +	va_end(ap);
    1.33 +}
    1.34 +
    1.35 +//////////////////////////////////////////////////////////////////////////
    1.36 +//THERE BE HAX HERE!!!! DON'T TELL ALFRED, BUT GABE WANTED IT THAT WAY. //
    1.37 +// (note: you can find the windows offset by looking for the text       //
    1.38 +//   "Echo text to console", you'll find the callback cmd pushed on the //
    1.39 +//   stack.)                                                            //
    1.40 +//////////////////////////////////////////////////////////////////////////
    1.41 +
    1.42 +#define SIGLEN			8
    1.43 +#define ENGINE486_SIG	"\x55\x89\xE5\x53\x83\xEC\x14\xBB"
    1.44 +#define ENGINE486_OFFS	40
    1.45 +#define ENGINE686_SIG	"\x53\x83\xEC\x08\xBB\x01\x00\x00"
    1.46 +#define ENGINE686_OFFS	50
    1.47 +#define	ENGINEAMD_SIG	"\x53\x31\xBB\x01\x00\x00\x00\x51"
    1.48 +#define	ENGINEAMD_OFFS	47
    1.49 +#define ENGINEW32_SIG	"\xA1\x2A\x2A\x2A\x2A\x56\xBE\x01"
    1.50 +#define ENGINEW32_OFFS	38
    1.51 +#define IA32_CALL		0xE8
    1.52 +
    1.53 +bool vcmp(void *_addr1, void *_addr2, size_t len)
    1.54 +{
    1.55 +	unsigned char *addr1 = (unsigned char *)_addr1;
    1.56 +	unsigned char *addr2 = (unsigned char *)_addr2;
    1.57 +
    1.58 +	for (size_t i=0; i<len; i++)
    1.59 +	{
    1.60 +		if (addr2[i] == '*')
    1.61 +			continue;
    1.62 +		if (addr1[i] != addr2[i])
    1.63 +			return false;
    1.64 +	}
    1.65 +
    1.66 +	return true;
    1.67 +}
    1.68 +
    1.69 +//Thanks to fysh for the idea of extracting info from "echo" and for
    1.70 +// having the original offsets at hand!
    1.71 +bool CSmmAPI::CacheCmds()
    1.72 +{
    1.73 +	ICvar *pCvar = g_Engine.icvar;
    1.74 +
    1.75 +	ConCommandBase *pBase = pCvar->GetCommands();
    1.76 +	unsigned char *ptr = NULL;
    1.77 +	char *addr = NULL;
    1.78 +	FnCommandCallback callback = NULL;
    1.79 +	int offs = 0;
    1.80 +
    1.81 +	while (pBase)
    1.82 +	{
    1.83 +		if ( strcmp(pBase->GetName(), "echo") == 0 )
    1.84 +		{
    1.85 +			//callback = //*((FnCommandCallback *)((char *)pBase + offsetof(ConCommand, m_fnCommandCallback)));
    1.86 +			callback = ((ConCommand *)pBase)->GetCallback();
    1.87 +			ptr = (unsigned char *)callback;
    1.88 +			if (vcmp(ptr, ENGINE486_SIG, SIGLEN))
    1.89 +			{
    1.90 +				offs = ENGINE486_OFFS;
    1.91 +			} else if (vcmp(ptr, ENGINE686_SIG, SIGLEN)) {
    1.92 +				offs = ENGINE686_OFFS;
    1.93 +			} else if (vcmp(ptr, ENGINEAMD_SIG, SIGLEN)) {
    1.94 +				offs = ENGINEAMD_OFFS;
    1.95 +			} else if (vcmp(ptr, ENGINEW32_SIG, SIGLEN)) {
    1.96 +				offs = ENGINEW32_OFFS;
    1.97 +			}
    1.98 +			if (!offs || ptr[offs-1] != IA32_CALL)
    1.99 +			{
   1.100 +				m_ConPrintf = (CONPRINTF_FUNC)Msg;
   1.101 +				return false;
   1.102 +			}
   1.103 +			//get the relative offset
   1.104 +			m_ConPrintf = *((CONPRINTF_FUNC *)(ptr + offs));
   1.105 +			//add the base offset, to the ip (which is the address+offset + 4 bytes for next instruction)
   1.106 +			m_ConPrintf = (CONPRINTF_FUNC)((unsigned long)m_ConPrintf + (unsigned long)(ptr + offs) + 4);
   1.107 +
   1.108 +			m_Cache = true;
   1.109 +
   1.110 +			return true;
   1.111 +		}
   1.112 +		pBase = const_cast<ConCommandBase *>(pBase->GetNext());
   1.113 +	}
   1.114 +
   1.115 +	m_ConPrintf = (CONPRINTF_FUNC)Msg;
   1.116 +
   1.117 +	return false;
   1.118 +}
   1.119 +
   1.120 +bool CSmmAPI::CacheSuccessful()
   1.121 +{
   1.122 +	return m_Cache;
   1.123 +}