sourcemm/CSmmAPI.cpp
changeset 287 0492209c065f
parent 282 7a237e9ea5a1
child 288 ec7c7bfbcbae
     1.1 --- a/sourcemm/CSmmAPI.cpp	Mon Feb 19 13:17:07 2007 +0000
     1.2 +++ b/sourcemm/CSmmAPI.cpp	Wed Feb 21 01:51:54 2007 +0000
     1.3 @@ -27,10 +27,16 @@
     1.4  CSmmAPI::CSmmAPI()
     1.5  {
     1.6  	m_ConPrintf = NULL;
     1.7 -	m_Cache = false;
     1.8 +	m_CmdCache = false;
     1.9 +	m_MsgCount = -1;
    1.10  	m_VSP = false;
    1.11  }
    1.12  
    1.13 +CSmmAPI::~CSmmAPI()
    1.14 +{
    1.15 +	m_UserMessages.RemoveAll();
    1.16 +}
    1.17 +
    1.18  void CSmmAPI::LogMsg(ISmmPlugin *pl, const char *msg, ...)
    1.19  {
    1.20  	va_list ap;
    1.21 @@ -279,7 +285,7 @@
    1.22  			//add the base offset, to the ip (which is the address+offset + 4 bytes for next instruction)
    1.23  			m_ConPrintf = (CONPRINTF_FUNC)((unsigned long)m_ConPrintf + (unsigned long)(ptr + offs) + 4);
    1.24  
    1.25 -			m_Cache = true;
    1.26 +			m_CmdCache = true;
    1.27  
    1.28  			return true;
    1.29  		}
    1.30 @@ -291,9 +297,9 @@
    1.31  	return false;
    1.32  }
    1.33  
    1.34 -bool CSmmAPI::CacheSuccessful()
    1.35 +bool CSmmAPI::CmdCacheSuccessful()
    1.36  {
    1.37 -	return m_Cache;
    1.38 +	return m_CmdCache;
    1.39  }
    1.40  
    1.41  void CSmmAPI::GetApiVersions(int &major, int &minor, int &plvers, int &plmin)
    1.42 @@ -498,3 +504,104 @@
    1.43  {
    1.44  	return g_GameDllVersion;
    1.45  }
    1.46 +
    1.47 +//////////////////////////////////////////////////////////////////////
    1.48 +// EVEN MORE HACKS HERE! YOU HAVE BEEN WARNED!                      //
    1.49 +// Signatures necessary in finding the pointer to the CUtlDict that //
    1.50 +//   stores user message information.                               //
    1.51 +// IServerGameDLL::GetUserMessageInfo() normally crashes with bad   //
    1.52 +//   message indices. This is our answer to it. Yuck! <:-(          //
    1.53 +//////////////////////////////////////////////////////////////////////
    1.54 +#ifdef OS_WIN32
    1.55 +	#define MSGCLASS_SIGLEN	7
    1.56 +	#define MSGCLASS_SIG	"\x8B\x0D\x2A\x2A\x2A\x2A\x56"
    1.57 +	#define MSGCLASS_OFFS	2
    1.58 +#elif defined OS_LINUX
    1.59 +	#define MSGCLASS_SIGLEN	14
    1.60 +	#define MSGCLASS_SIG	"\x53\x83\xEC\x2A\x8B\x5C\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
    1.61 +	#define MSGCLASS_OFFS	9
    1.62 +#endif
    1.63 +
    1.64 +/* This is the ugliest function in all of SourceMM */
    1.65 +bool CSmmAPI::CacheUserMessages()
    1.66 +{
    1.67 +	SourceHook::MemFuncInfo info = {true, -1, 0, 0};
    1.68 +	SourceHook::GetFuncInfo(&IServerGameDLL::GetUserMessageInfo, info);
    1.69 +
    1.70 +	// Get address of original GetUserMessageInfo()
    1.71 +	char *vfunc = reinterpret_cast<char *>(g_GameDllPatch->GetOrigFunc(info.vtbloffs, info.vtblindex));
    1.72 +
    1.73 +	// If we can't get original function (GetOrigFunc bug?)
    1.74 +	if (vfunc == NULL)
    1.75 +	{
    1.76 +		// This means there's no hook, so we must get it manually - Lovely code <:-(
    1.77 +		char *adjustedptr = reinterpret_cast<char *>(g_GameDll.pGameDLL) + info.thisptroffs + info.vtbloffs;
    1.78 +		char **vtable = *reinterpret_cast<char ***>(adjustedptr);
    1.79 +
    1.80 +		vfunc = vtable[info.vtblindex];
    1.81 +	}
    1.82 +
    1.83 +	if (vcmp(vfunc, MSGCLASS_SIG, MSGCLASS_SIGLEN))
    1.84 +	{
    1.85 +		// Get address of CUserMessages
    1.86 +		char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS_OFFS);
    1.87 +
    1.88 +		// Get address of CUserMessages::m_UserMessages
    1.89 +		UserMsgDict *dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
    1.90 +
    1.91 +		m_MsgCount = dict->Count();
    1.92 +
    1.93 +		// Cache messages in our CUtlDict
    1.94 +		UserMessage *msg;
    1.95 +
    1.96 +		for (int i = 0; i < m_MsgCount; i++)
    1.97 +		{
    1.98 +			msg = dict->Element(i);
    1.99 +			m_UserMessages.Insert(msg->name, msg);
   1.100 +		}
   1.101 +
   1.102 +		return true;
   1.103 +	}
   1.104 +
   1.105 +	return false;
   1.106 +}
   1.107 +
   1.108 +bool CSmmAPI::MsgCacheSuccessful()
   1.109 +{
   1.110 +	return m_MsgCount > -1;
   1.111 +}
   1.112 +
   1.113 +int CSmmAPI::GetUserMessageCount()
   1.114 +{
   1.115 +	return m_MsgCount;
   1.116 +}
   1.117 +
   1.118 +int CSmmAPI::FindUserMessage(const char *name, int *size)
   1.119 +{
   1.120 +	int index = m_UserMessages.Find(name);
   1.121 +
   1.122 +	if (size && index > -1)
   1.123 +	{
   1.124 +		UserMessage *msg = m_UserMessages.Element(index);
   1.125 +		*size = msg->size;
   1.126 +	}
   1.127 +
   1.128 +	return index;
   1.129 +}
   1.130 +
   1.131 +const char *CSmmAPI::GetUserMessage(int index, int *size)
   1.132 +{
   1.133 +	if (m_MsgCount <= 0 || index < 0 || index >= m_MsgCount)
   1.134 +	{
   1.135 +		return NULL;
   1.136 +	}
   1.137 +
   1.138 +	UserMessage *msg = m_UserMessages.Element(index);
   1.139 +
   1.140 +	if (size)
   1.141 +	{
   1.142 +		*size = msg->size;
   1.143 +	}
   1.144 +
   1.145 +	return msg->name;
   1.146 +}