sourcemm/CSmmAPI.cpp
branchsourcemm-1.6.0
changeset 347 e82968c21606
child 348 716af2e7be78
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sourcemm/CSmmAPI.cpp	Tue Sep 18 20:03:47 2007 +0000
     1.3 @@ -0,0 +1,668 @@
     1.4 +/* ======== SourceMM ========
     1.5 + * Copyright (C) 2004-2007 Metamod:Source Development Team
     1.6 + * No warranties of any kind
     1.7 + *
     1.8 + * License: zlib/libpng
     1.9 + *
    1.10 + * Author(s): David "BAILOPAN" Anderson
    1.11 + * ============================
    1.12 + */
    1.13 +
    1.14 +#include "convar_smm.h"
    1.15 +#include "CSmmAPI.h"
    1.16 +#include "sourcemm.h"
    1.17 +#include "concommands.h"
    1.18 +#include "CPlugin.h"
    1.19 +#include "vsp_listener.h"
    1.20 +
    1.21 +/**
    1.22 + * @brief Implementation of main API interface
    1.23 + * @file CSmmAPI.cpp
    1.24 + */
    1.25 +
    1.26 +using namespace SourceMM;
    1.27 +
    1.28 +CSmmAPI g_SmmAPI;
    1.29 +
    1.30 +CSmmAPI::CSmmAPI()
    1.31 +{
    1.32 +	m_ConPrintf = NULL;
    1.33 +	m_CmdCache = false;
    1.34 +	m_MsgCount = -1;
    1.35 +	m_VSP = false;
    1.36 +}
    1.37 +
    1.38 +CSmmAPI::~CSmmAPI()
    1.39 +{
    1.40 +	m_UserMessages.RemoveAll();
    1.41 +}
    1.42 +
    1.43 +void CSmmAPI::LogMsg(ISmmPlugin *pl, const char *msg, ...)
    1.44 +{
    1.45 +	va_list ap;
    1.46 +	static char buffer[2048];
    1.47 +
    1.48 +	va_start(ap, msg);
    1.49 +	UTIL_FormatArgs(buffer, sizeof(buffer), msg, ap);
    1.50 +	va_end(ap);
    1.51 +
    1.52 +	LogMessage("[%s] %s", pl->GetLogTag(), buffer);
    1.53 +}
    1.54 +
    1.55 +CreateInterfaceFn CSmmAPI::engineFactory(bool syn)
    1.56 +{
    1.57 +	if (syn)
    1.58 +		return EngineFactory;
    1.59 +	return g_Engine.engineFactory;
    1.60 +}
    1.61 +
    1.62 +CreateInterfaceFn CSmmAPI::physicsFactory(bool syn)
    1.63 +{
    1.64 +	if (syn)
    1.65 +		return PhysicsFactory;
    1.66 +	return g_Engine.physicsFactory;
    1.67 +}
    1.68 +
    1.69 +CreateInterfaceFn CSmmAPI::fileSystemFactory(bool syn)
    1.70 +{
    1.71 +	if (syn)
    1.72 +		return FileSystemFactory;
    1.73 +	return g_Engine.fileSystemFactory;
    1.74 +}
    1.75 +
    1.76 +CreateInterfaceFn CSmmAPI::serverFactory(bool syn)
    1.77 +{
    1.78 +	if (syn)
    1.79 +		return CreateInterface;
    1.80 +	return g_GameDll.factory;
    1.81 +}
    1.82 +
    1.83 +CGlobalVars *CSmmAPI::pGlobals()
    1.84 +{
    1.85 +	return g_Engine.pGlobals;
    1.86 +}
    1.87 +
    1.88 +void CSmmAPI::SetLastMetaReturn(META_RES res)
    1.89 +{
    1.90 +	m_Res = res;
    1.91 +}
    1.92 +
    1.93 +META_RES CSmmAPI::GetLastMetaReturn()
    1.94 +{
    1.95 +	return m_Res;
    1.96 +}
    1.97 +
    1.98 +IConCommandBaseAccessor *CSmmAPI::GetCvarBaseAccessor()
    1.99 +{
   1.100 +	return static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor);
   1.101 +}
   1.102 +
   1.103 +bool CSmmAPI::RegisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
   1.104 +{
   1.105 +	if (pCommand->IsCommand())
   1.106 +	{
   1.107 +		g_PluginMngr.AddPluginCmd(plugin, pCommand);
   1.108 +	} else {
   1.109 +		g_PluginMngr.AddPluginCvar(plugin, pCommand);
   1.110 +	}
   1.111 +
   1.112 +	return g_SMConVarAccessor.Register(pCommand);
   1.113 +}
   1.114 +
   1.115 +void CSmmAPI::UnregisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
   1.116 +{
   1.117 +	if (pCommand->IsCommand())
   1.118 +	{
   1.119 +		g_PluginMngr.RemovePluginCmd(plugin, pCommand);
   1.120 +	} else {
   1.121 +		g_PluginMngr.RemovePluginCvar(plugin, pCommand);
   1.122 +	}
   1.123 +
   1.124 +	g_SMConVarAccessor.Unregister(pCommand);
   1.125 +}
   1.126 +
   1.127 +void CSmmAPI::ConPrint(const char *fmt)
   1.128 +{
   1.129 +	(m_ConPrintf)("%s", fmt);
   1.130 +}
   1.131 +
   1.132 +void CSmmAPI::ConPrintf(const char *fmt, ...)
   1.133 +{
   1.134 +	va_list ap;
   1.135 +	static char buffer[4096];
   1.136 +
   1.137 +	va_start(ap, fmt);
   1.138 +	UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
   1.139 +	va_end(ap);
   1.140 +
   1.141 +	(m_ConPrintf)("%s", buffer);
   1.142 +}
   1.143 +
   1.144 +void CSmmAPI::AddListener(ISmmPlugin *plugin, IMetamodListener *pListener)
   1.145 +{
   1.146 +	CPluginManager::CPlugin *pl = g_PluginMngr.FindByAPI(plugin);
   1.147 +
   1.148 +	pl->m_Events.push_back(pListener);
   1.149 +}
   1.150 +
   1.151 +void *CSmmAPI::MetaFactory(const char *iface, int *_ret, PluginId *id)
   1.152 +{
   1.153 +	if (id)
   1.154 +	{
   1.155 +		*id = 0;
   1.156 +	}
   1.157 +
   1.158 +	if (!iface)
   1.159 +	{
   1.160 +		return NULL;
   1.161 +	}
   1.162 +
   1.163 +	//first check ours... we get first chance!
   1.164 +	if (strcmp(iface, MMIFACE_SOURCEHOOK) == 0)
   1.165 +	{
   1.166 +		if (_ret)
   1.167 +		{
   1.168 +			*_ret = IFACE_OK;
   1.169 +		}
   1.170 +		return static_cast<void *>(static_cast<SourceHook::ISourceHook *>(&g_SourceHook));
   1.171 +	} else if (strcmp(iface, MMIFACE_PLMANAGER) == 0) {
   1.172 +		if (_ret)
   1.173 +		{
   1.174 +			*_ret = IFACE_OK;
   1.175 +		}
   1.176 +		return static_cast<void *>(static_cast<ISmmPluginManager *>(&g_PluginMngr));
   1.177 +	}
   1.178 +
   1.179 +	CPluginManager::CPlugin *pl;
   1.180 +	SourceHook::List<IMetamodListener *>::iterator event;
   1.181 +	IMetamodListener *api;
   1.182 +	int ret = 0;
   1.183 +	void *val = NULL;
   1.184 +
   1.185 +	for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++)
   1.186 +	{
   1.187 +		pl = (*iter);
   1.188 +		for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++)
   1.189 +		{
   1.190 +			api = (*event);
   1.191 +			ret = IFACE_FAILED;
   1.192 +			if ( (val=api->OnMetamodQuery(iface, &ret)) != NULL )
   1.193 +			{
   1.194 +				if (_ret)
   1.195 +					*_ret = ret;
   1.196 +				if (id)
   1.197 +					*id = pl->m_Id;
   1.198 +				return val;
   1.199 +			}
   1.200 +		}
   1.201 +	}
   1.202 +
   1.203 +	if (_ret)
   1.204 +		*_ret = IFACE_FAILED;
   1.205 +
   1.206 +	return NULL;
   1.207 +}
   1.208 +
   1.209 +//////////////////////////////////////////////////////////////////////////
   1.210 +//THERE BE HAX HERE!!!! DON'T TELL ALFRED, BUT GABE WANTED IT THAT WAY. //
   1.211 +// (note: you can find the offset by looking for the text               //
   1.212 +//   "Echo text to console", you'll find the callback cmd pushed on the //
   1.213 +//   stack.)                                                            //
   1.214 +//////////////////////////////////////////////////////////////////////////
   1.215 +
   1.216 +#define SIGLEN			8
   1.217 +#define ENGINE486_SIG	"\x55\x89\xE5\x53\x83\xEC\x14\xBB"
   1.218 +#define ENGINE486_OFFS	40
   1.219 +#define ENGINE686_SIG	"\x53\x83\xEC\x08\xBB\x01\x00\x00"
   1.220 +#define ENGINE686_OFFS	50
   1.221 +#define	ENGINEAMD_SIG	"\x53\x51\xBB\x01\x00\x00\x00\x51"
   1.222 +#define	ENGINEAMD_OFFS	47
   1.223 +#define ENGINEW32_SIG	"\xA1\x2A\x2A\x2A\x2A\x56\xBE\x01"
   1.224 +#define ENGINEW32_OFFS	38
   1.225 +#define IA32_CALL		0xE8
   1.226 +
   1.227 +bool vcmp(const void *_addr1, const void *_addr2, size_t len)
   1.228 +{
   1.229 +	unsigned char *addr1 = (unsigned char *)_addr1;
   1.230 +	unsigned char *addr2 = (unsigned char *)_addr2;
   1.231 +
   1.232 +	for (size_t i=0; i<len; i++)
   1.233 +	{
   1.234 +		if (addr2[i] == '*')
   1.235 +			continue;
   1.236 +		if (addr1[i] != addr2[i])
   1.237 +			return false;
   1.238 +	}
   1.239 +
   1.240 +	return true;
   1.241 +}
   1.242 +
   1.243 +//Thanks to fysh for the idea of extracting info from "echo" and for
   1.244 +// having the original offsets at hand!
   1.245 +bool CSmmAPI::CacheCmds()
   1.246 +{
   1.247 +	ICvar *pCvar = g_Engine.icvar;
   1.248 +
   1.249 +	ConCommandBase *pBase = pCvar->GetCommands();
   1.250 +	unsigned char *ptr = NULL;
   1.251 +	FnCommandCallback callback = NULL;
   1.252 +	int offs = 0;
   1.253 +
   1.254 +	while (pBase)
   1.255 +	{
   1.256 +		if ( strcmp(pBase->GetName(), "echo") == 0 )
   1.257 +		{
   1.258 +			//callback = //*((FnCommandCallback *)((char *)pBase + offsetof(ConCommand, m_fnCommandCallback)));
   1.259 +			callback = ((ConCommand *)pBase)->GetCallback();
   1.260 +			ptr = (unsigned char *)callback;
   1.261 +		#ifdef OS_LINUX
   1.262 +			if (vcmp(ptr, ENGINE486_SIG, SIGLEN))
   1.263 +			{
   1.264 +				offs = ENGINE486_OFFS;
   1.265 +			}
   1.266 +			else if (vcmp(ptr, ENGINE686_SIG, SIGLEN))
   1.267 +			{
   1.268 +				offs = ENGINE686_OFFS;
   1.269 +			}
   1.270 +			else if (vcmp(ptr, ENGINEAMD_SIG, SIGLEN))
   1.271 +			{
   1.272 +				offs = ENGINEAMD_OFFS;
   1.273 +			}
   1.274 +		#elif defined OS_WIN32 // Only one Windows engine binary so far...
   1.275 +			if (vcmp(ptr, ENGINEW32_SIG, SIGLEN))
   1.276 +			{
   1.277 +				offs = ENGINEW32_OFFS;
   1.278 +			}
   1.279 +		#endif
   1.280 +
   1.281 +			if (!offs || ptr[offs - 1] != IA32_CALL)
   1.282 +			{
   1.283 +				m_ConPrintf = (CONPRINTF_FUNC)Msg;
   1.284 +				return false;
   1.285 +			}
   1.286 +			//get the relative offset
   1.287 +			m_ConPrintf = *((CONPRINTF_FUNC *)(ptr + offs));
   1.288 +			//add the base offset, to the ip (which is the address+offset + 4 bytes for next instruction)
   1.289 +			m_ConPrintf = (CONPRINTF_FUNC)((unsigned long)m_ConPrintf + (unsigned long)(ptr + offs) + 4);
   1.290 +
   1.291 +			m_CmdCache = true;
   1.292 +
   1.293 +			return true;
   1.294 +		}
   1.295 +		pBase = const_cast<ConCommandBase *>(pBase->GetNext());
   1.296 +	}
   1.297 +
   1.298 +	m_ConPrintf = (CONPRINTF_FUNC)Msg;
   1.299 +
   1.300 +	return false;
   1.301 +}
   1.302 +
   1.303 +bool CSmmAPI::CmdCacheSuccessful()
   1.304 +{
   1.305 +	return m_CmdCache;
   1.306 +}
   1.307 +
   1.308 +void CSmmAPI::GetApiVersions(int &major, int &minor, int &plvers, int &plmin)
   1.309 +{
   1.310 +	major = SM_VERS_API_MAJOR;
   1.311 +	minor = SM_VERS_API_MINOR;
   1.312 +	plvers = PLAPI_VERSION;
   1.313 +	plmin = PLAPI_MIN_VERSION;
   1.314 +}
   1.315 +
   1.316 +void CSmmAPI::GetShVersions(int &shvers, int &shimpl)
   1.317 +{
   1.318 +	shvers = SH_IFACE_VERSION;
   1.319 +	shimpl = SH_IMPL_VERSION;
   1.320 +}
   1.321 +
   1.322 +int CSmmAPI::FormatIface(char iface[], unsigned int maxlength)
   1.323 +{
   1.324 +	int length = (int)strlen(iface);
   1.325 +	int i;
   1.326 +	int num = 0;
   1.327 +
   1.328 +	for (i = length - 1; i >= 0; i--)
   1.329 +	{
   1.330 +		if (!isdigit(iface[i]))
   1.331 +		{
   1.332 +			if (i != length - 1)
   1.333 +			{
   1.334 +				num = 1;
   1.335 +			}
   1.336 +			break;
   1.337 +		}
   1.338 +	}
   1.339 +
   1.340 +	if ( (num && ((int)maxlength <= length)) || (!num && ((int)maxlength <= length + 3)) )
   1.341 +	{
   1.342 +		return -1;
   1.343 +	}
   1.344 +
   1.345 +	if (i != length - 1)
   1.346 +		num = atoi(&(iface[++i]));
   1.347 +
   1.348 +	num++;
   1.349 +
   1.350 +	snprintf(&(iface[i]), 4, "%03d", num);
   1.351 +
   1.352 +	return num;
   1.353 +}
   1.354 +
   1.355 +void *CSmmAPI::InterfaceSearch(CreateInterfaceFn fn, const char *iface, int max, int *ret)
   1.356 +{
   1.357 +	char _if[256];	/* assume no interface goes beyond this */
   1.358 +	size_t len = strlen(iface);
   1.359 +	int num = 0;
   1.360 +	void *pf = NULL;
   1.361 +
   1.362 +	if (max > 999)
   1.363 +		max = 999;
   1.364 +
   1.365 +	if (len + 4 > sizeof(_if))
   1.366 +	{
   1.367 +		if (ret)
   1.368 +		{
   1.369 +			*ret = IFACE_FAILED;
   1.370 +		}
   1.371 +		return NULL;
   1.372 +	}
   1.373 +
   1.374 +	strcpy(_if, iface);
   1.375 +
   1.376 +	do
   1.377 +	{
   1.378 +		if ( (pf = (fn)(_if, ret)) != NULL )
   1.379 +			break;
   1.380 +		if (num > max)
   1.381 +			break;
   1.382 +	} while (( num = FormatIface(_if, len+1) ));
   1.383 +
   1.384 +	return pf;
   1.385 +}
   1.386 +
   1.387 +void *CSmmAPI::VInterfaceMatch(CreateInterfaceFn fn, const char *iface, int min)
   1.388 +{
   1.389 +	char buffer[256];	/* assume no interface will go beyond this */
   1.390 +	size_t len = strlen(iface);
   1.391 +	int ret;			/* just in case something doesn't handle NULL properly */
   1.392 +
   1.393 +	if (len > sizeof(buffer) - 4)
   1.394 +	{
   1.395 +		return NULL;
   1.396 +	}
   1.397 +
   1.398 +	strcpy(buffer, iface);
   1.399 +
   1.400 +	if (min != -1)
   1.401 +	{
   1.402 +		char *ptr = &buffer[len - 1];
   1.403 +		int digits = 0;
   1.404 +		while (isdigit(*ptr) && digits <=3)
   1.405 +		{
   1.406 +			*ptr = '\0';
   1.407 +			digits++;
   1.408 +			ptr--;
   1.409 +		}
   1.410 +		if (digits != 3)
   1.411 +		{
   1.412 +			/* for now, assume this is an error */
   1.413 +			strcpy(buffer, iface);
   1.414 +		} else {
   1.415 +			char num[4];
   1.416 +			min = (min == 0) ? 1 : min;
   1.417 +			snprintf(num, sizeof(num), "%03d", min);
   1.418 +			strcat(buffer, num);
   1.419 +		}
   1.420 +	}
   1.421 +
   1.422 +	return InterfaceSearch(fn, buffer, IFACE_MAXNUM, &ret);
   1.423 +}
   1.424 +
   1.425 +const char *CSmmAPI::GetBaseDir()
   1.426 +{
   1.427 +	return g_ModPath.c_str();
   1.428 +}
   1.429 +
   1.430 +void CSmmAPI::PathFormat(char *buffer, size_t len, const char *fmt, ...)
   1.431 +{
   1.432 +	va_list ap;
   1.433 +	va_start(ap, fmt);
   1.434 +	size_t mylen = UTIL_FormatArgs(buffer, len, fmt, ap);
   1.435 +	va_end(ap);
   1.436 +
   1.437 +	for (size_t i = 0; i < mylen; i++)
   1.438 +	{
   1.439 +		if (buffer[i] == ALT_SEP_CHAR)
   1.440 +		{
   1.441 +			buffer[i] = PATH_SEP_CHAR;
   1.442 +		}
   1.443 +	}
   1.444 +}
   1.445 +
   1.446 +void CSmmAPI::ClientConPrintf(edict_t *client, const char *fmt, ...)
   1.447 +{
   1.448 +	va_list ap;
   1.449 +	static char buffer[4096];
   1.450 +
   1.451 +	va_start(ap, fmt);
   1.452 +	UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
   1.453 +	va_end(ap);
   1.454 +
   1.455 +	g_Engine.engine->ClientPrintf(client, buffer);
   1.456 +}
   1.457 +
   1.458 +void CSmmAPI::LoadAsVSP()
   1.459 +{
   1.460 +	size_t len;
   1.461 +	char engine_file[PATH_SIZE];
   1.462 +	char rel_path[PATH_SIZE * 2];
   1.463 +
   1.464 +	GetFileOfAddress(g_Engine.engine, engine_file, sizeof(engine_file));
   1.465 +
   1.466 +	/* Chop off the "engine" file part */
   1.467 +	len = strlen(engine_file);
   1.468 +	for (size_t i = len - 1; i >= 0 && i < len; i--)
   1.469 +	{
   1.470 +		if (engine_file[i] == '/'
   1.471 +			|| engine_file[i] == '\\')
   1.472 +		{
   1.473 +			engine_file[i] = '\0';
   1.474 +			break;
   1.475 +		}
   1.476 +	}
   1.477 +
   1.478 +	const char *usepath = g_SmmPath.c_str();
   1.479 +	if (UTIL_Relatize(rel_path, sizeof(rel_path), engine_file, g_SmmPath.c_str()))
   1.480 +	{
   1.481 +		usepath = rel_path;
   1.482 +	}
   1.483 +	
   1.484 +	char command[PATH_SIZE * 2];
   1.485 +	g_VspListener.SetLoadable(true);
   1.486 +	UTIL_Format(command, sizeof(command), "plugin_load \"%s\"\n", usepath);
   1.487 +	g_Engine.engine->ServerCommand(command);
   1.488 +}
   1.489 +
   1.490 +void CSmmAPI::EnableVSPListener()
   1.491 +{
   1.492 +	/* If GameInit already passed and we're not already enabled or loaded, go ahead and LoadAsVSP load */
   1.493 +	if (bGameInit && !m_VSP && !g_VspListener.IsLoaded())
   1.494 +	{
   1.495 +		LoadAsVSP();
   1.496 +	}
   1.497 +	m_VSP = true;
   1.498 +}
   1.499 +
   1.500 +int CSmmAPI::GetGameDLLVersion()
   1.501 +{
   1.502 +	return g_GameDllVersion;
   1.503 +}
   1.504 +
   1.505 +//////////////////////////////////////////////////////////////////////
   1.506 +// EVEN MORE HACKS HERE! YOU HAVE BEEN WARNED!                      //
   1.507 +// Signatures necessary in finding the pointer to the CUtlDict that //
   1.508 +//   stores user message information.                               //
   1.509 +// IServerGameDLL::GetUserMessageInfo() normally crashes with bad   //
   1.510 +//   message indices. This is our answer to it. Yuck! <:-(          //
   1.511 +//////////////////////////////////////////////////////////////////////
   1.512 +#ifdef OS_WIN32
   1.513 +	/* General Windows sig */
   1.514 +	#define MSGCLASS_SIGLEN		7
   1.515 +	#define MSGCLASS_SIG		"\x8B\x0D\x2A\x2A\x2A\x2A\x56"
   1.516 +	#define MSGCLASS_OFFS		2
   1.517 +
   1.518 +	/* Dystopia Wimdows hack */
   1.519 +	#define MSGCLASS2_SIGLEN	16
   1.520 +	#define MSGCLASS2_SIG		"\x56\x8B\x74\x24\x2A\x85\xF6\x7C\x2A\x3B\x35\x2A\x2A\x2A\x2A\x7D"
   1.521 +	#define MSGCLASS2_OFFS		11
   1.522 +
   1.523 +	/* Windows frame pointer sig */
   1.524 +	#define MSGCLASS3_SIGLEN	18
   1.525 +	#define MSGCLASS3_SIG		"\x55\x8B\xEC\x51\x89\x2A\x2A\x8B\x2A\x2A\x50\x8B\x0D\x2A\x2A\x2A\x2A\xE8"
   1.526 +	#define MSGCLASS3_OFFS		13
   1.527 +#elif defined OS_LINUX
   1.528 +	/* No frame pointer sig */
   1.529 +	#define MSGCLASS_SIGLEN		14
   1.530 +	#define MSGCLASS_SIG		"\x53\x83\xEC\x2A\x8B\x2A\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
   1.531 +	#define MSGCLASS_OFFS		9
   1.532 +
   1.533 +	/* Frame pointer sig */
   1.534 +	#define MSGCLASS2_SIGLEN	16
   1.535 +	#define MSGCLASS2_SIG		"\x55\x89\xE5\x53\x83\xEC\x2A\x8B\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
   1.536 +	#define MSGCLASS2_OFFS		11
   1.537 +#endif
   1.538 +
   1.539 +/* This is the ugliest function in all of SourceMM */
   1.540 +/* :TODO: Make this prettier */
   1.541 +bool CSmmAPI::CacheUserMessages()
   1.542 +{
   1.543 +	SourceHook::MemFuncInfo info = {true, -1, 0, 0};
   1.544 +	SourceHook::GetFuncInfo(&IServerGameDLL::GetUserMessageInfo, info);
   1.545 +
   1.546 +	/* Get address of original GetUserMessageInfo() */
   1.547 +	char *vfunc = reinterpret_cast<char *>(g_GameDllPatch->GetOrigFunc(info.vtbloffs, info.vtblindex));
   1.548 +
   1.549 +	/* If we can't get original function, that means there's no hook */
   1.550 +	if (vfunc == NULL)
   1.551 +	{
   1.552 +		/* Get virtual function address 'manually' then */
   1.553 +		char *adjustedptr = reinterpret_cast<char *>(g_GameDll.pGameDLL) + info.thisptroffs + info.vtbloffs;
   1.554 +		char **vtable = *reinterpret_cast<char ***>(adjustedptr);
   1.555 +
   1.556 +		vfunc = vtable[info.vtblindex];
   1.557 +	}
   1.558 +
   1.559 +	/* Oh dear, we have a relative jump on our hands
   1.560 +	 * PVK II on Windows made me do this, but I suppose it doesn't hurt to check this on Linux too...
   1.561 +	 */
   1.562 +	if (*vfunc == '\xE9')
   1.563 +	{
   1.564 +		/* Get address from displacement...
   1.565 +		 *
   1.566 +		 * Add 5 because it's relative to next instruction:
   1.567 +		 * Opcode <1 byte> + 32-bit displacement <4 bytes> 
   1.568 +		 */
   1.569 +		vfunc = vfunc + *reinterpret_cast<int *>(vfunc + 1) + 5;
   1.570 +	}
   1.571 +
   1.572 +	UserMsgDict *dict = NULL;
   1.573 +
   1.574 +	if (vcmp(vfunc, MSGCLASS_SIG, MSGCLASS_SIGLEN))
   1.575 +	{
   1.576 +		/* Get address of CUserMessages instance */
   1.577 +		char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS_OFFS);
   1.578 +
   1.579 +		/* Get address of CUserMessages::m_UserMessages */
   1.580 +		dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
   1.581 +	} else if (vcmp(vfunc, MSGCLASS2_SIG, MSGCLASS2_SIGLEN)) {
   1.582 +	#ifdef OS_WIN32
   1.583 +		/* If we get here, the code is possibly inlined like in Dystopia */
   1.584 +
   1.585 +		/* Get the address of the CUtlRBTree */
   1.586 +		char *rbtree = *reinterpret_cast<char **>(vfunc + MSGCLASS2_OFFS);
   1.587 +
   1.588 +		/* CUtlDict should be 8 bytes before the CUtlRBTree (hacktacular!) */
   1.589 +		dict = reinterpret_cast<UserMsgDict *>(rbtree - 8);
   1.590 +	#elif defined OS_LINUX
   1.591 +		/* Get address of CUserMessages instance */
   1.592 +		char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS2_OFFS);
   1.593 +
   1.594 +		/* Get address of CUserMessages::m_UserMessages */
   1.595 +		dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
   1.596 +	#endif
   1.597 +	#ifdef OS_WIN32
   1.598 +	} else if (vcmp(vfunc, MSGCLASS3_SIG, MSGCLASS3_SIGLEN)) {
   1.599 +		/* Get address of CUserMessages instance */
   1.600 +		char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS3_OFFS);
   1.601 +
   1.602 +		/* Get address of CUserMessages::m_UserMessages */
   1.603 +		dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
   1.604 +	#endif
   1.605 +	}
   1.606 +
   1.607 +	if (dict)
   1.608 +	{
   1.609 +		m_MsgCount = dict->Count();
   1.610 +
   1.611 +		/* Ensure that count is within bounds of an unsigned byte, because that's what engine supports */
   1.612 +		if (m_MsgCount < 0 || m_MsgCount > 255)
   1.613 +		{
   1.614 +			m_MsgCount = -1;
   1.615 +			return false;
   1.616 +		}
   1.617 +
   1.618 +		UserMessage *msg;
   1.619 +
   1.620 +		/* Cache messages in our CUtlDict */
   1.621 +		for (int i = 0; i < m_MsgCount; i++)
   1.622 +		{
   1.623 +			msg = dict->Element(i);
   1.624 +			m_UserMessages.Insert(msg->name, msg);
   1.625 +		}
   1.626 +
   1.627 +		return true;
   1.628 +	}
   1.629 +
   1.630 +	return false;
   1.631 +}
   1.632 +
   1.633 +bool CSmmAPI::MsgCacheSuccessful()
   1.634 +{
   1.635 +	return m_MsgCount > -1;
   1.636 +}
   1.637 +
   1.638 +int CSmmAPI::GetUserMessageCount()
   1.639 +{
   1.640 +	return m_MsgCount;
   1.641 +}
   1.642 +
   1.643 +int CSmmAPI::FindUserMessage(const char *name, int *size)
   1.644 +{
   1.645 +	int index = m_UserMessages.Find(name);
   1.646 +
   1.647 +	if (size && index > -1)
   1.648 +	{
   1.649 +		UserMessage *msg = m_UserMessages.Element(index);
   1.650 +		*size = msg->size;
   1.651 +	}
   1.652 +
   1.653 +	return index;
   1.654 +}
   1.655 +
   1.656 +const char *CSmmAPI::GetUserMessage(int index, int *size)
   1.657 +{
   1.658 +	if (m_MsgCount <= 0 || index < 0 || index >= m_MsgCount)
   1.659 +	{
   1.660 +		return NULL;
   1.661 +	}
   1.662 +
   1.663 +	UserMessage *msg = m_UserMessages.Element(index);
   1.664 +
   1.665 +	if (size)
   1.666 +	{
   1.667 +		*size = msg->size;
   1.668 +	}
   1.669 +
   1.670 +	return msg->name;
   1.671 +}