extensions/sdktools/vglobals.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Mon Apr 04 13:44:19 2011 -0400 (2011-04-04)
changeset 3221 ca0e525dcf86
parent 3027 0f90dd9754ca
child 3333 e247490894a1
permissions -rw-r--r--
Fixed some gamedata lookups requiring symbols on linux/mac (bug 4832, r=fyren).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod SDKTools Extension
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "extension.h"
    33 
    34 void **g_pGameRules = NULL;
    35 void *g_EntList = NULL;
    36 
    37 
    38 void InitializeValveGlobals()
    39 {
    40 	g_EntList = gamehelpers->GetGlobalEntityList();
    41 
    42 	/*
    43 	 * g_pGameRules
    44 	 *
    45 	 * First try to lookup pointer directly for platforms with symbols.
    46 	 * If symbols aren't present (Windows or stripped Linux/Mac), 
    47 	 * attempt find via CreateGameRulesObject + offset
    48 	 */
    49 
    50 	char *addr;
    51 	if (g_pGameConf->GetMemSig("g_pGameRules", (void **)&addr) && addr)
    52 	{
    53 		g_pGameRules = reinterpret_cast<void **>(addr);
    54 	}
    55 	else if (g_pGameConf->GetMemSig("CreateGameRulesObject", (void **)&addr) && addr)
    56 	{
    57 		int offset;
    58 		if (!g_pGameConf->GetOffset("g_pGameRules", &offset) || !offset)
    59 		{
    60 			return;
    61 		}
    62 		g_pGameRules = *reinterpret_cast<void ***>(addr + offset);
    63 	}
    64 }
    65 
    66 size_t UTIL_StringToSignature(const char *str, char buffer[], size_t maxlength)
    67 {
    68 	size_t real_bytes = 0;
    69 	size_t length = strlen(str);
    70 
    71 	for (size_t i=0; i<length; i++)
    72 	{
    73 		if (real_bytes >= maxlength)
    74 		{
    75 			break;
    76 		}
    77 		buffer[real_bytes++] = (unsigned char)str[i];
    78 		if (str[i] == '\\'
    79 			&& str[i+1] == 'x')
    80 		{
    81 			if (i + 3 >= length)
    82 			{
    83 				continue;
    84 			}
    85 			/* Get the hex part */
    86 			char s_byte[3];
    87 			int r_byte;
    88 			s_byte[0] = str[i+2];
    89 			s_byte[1] = str[i+3];
    90 			s_byte[2] = '\n';
    91 			/* Read it as an integer */
    92 			sscanf(s_byte, "%x", &r_byte);
    93 			/* Save the value */
    94 			buffer[real_bytes-1] = (unsigned char)r_byte;
    95 			/* Adjust index */
    96 			i += 3;
    97 		}
    98 	}
    99 
   100 	return real_bytes;
   101 }
   102 
   103 bool UTIL_VerifySignature(const void *addr, const char *sig, size_t len)
   104 {
   105 	unsigned char *addr1 = (unsigned char *) addr;
   106 	unsigned char *addr2 = (unsigned char *) sig;
   107 
   108 	for (size_t i = 0; i < len; i++)
   109 	{
   110 		if (addr2[i] == '*')
   111 			continue;
   112 		if (addr1[i] != addr2[i])
   113 			return false;
   114 	}
   115 
   116 	return true;
   117 }
   118 
   119 #if defined PLATFORM_WINDOWS
   120 void GetIServer()
   121 {
   122 	const char *sigstr;
   123 	char sig[32];
   124 	size_t siglen;
   125 	int offset;
   126 	void *vfunc = NULL;
   127 
   128 #if defined METAMOD_PLAPI_VERSION || PLAPI_VERSION >= 11
   129 	/* Get the CreateFakeClient function pointer */
   130 	if (!(vfunc=SH_GET_ORIG_VFNPTR_ENTRY(engine, &IVEngineServer::CreateFakeClient)))
   131 	{
   132 		return;
   133 	}
   134 #else
   135 	/* Get the interface manually */
   136 	SourceHook::MemFuncInfo info = {true, -1, 0, 0};
   137 	SourceHook::GetFuncInfo(&IVEngineServer::CreateFakeClient, info);
   138 
   139 	vfunc = enginePatch->GetOrigFunc(info.vtbloffs, info.vtblindex);
   140 	if (!vfunc)
   141 	{
   142 		void **vtable = *reinterpret_cast<void ***>(enginePatch->GetThisPtr() + info.thisptroffs + info.vtbloffs);
   143 		vfunc = vtable[info.vtblindex];
   144 	}
   145 #endif
   146 
   147 	/* Get signature string for IVEngineServer::CreateFakeClient() */
   148 	sigstr = g_pGameConf->GetKeyValue("CreateFakeClient_Windows");
   149 
   150 	if (!sigstr)
   151 	{
   152 		return;
   153 	}
   154 
   155 	/* Convert signature string to signature bytes */
   156 	siglen = UTIL_StringToSignature(sigstr, sig, sizeof(sig));
   157 
   158 	/* Check if we're on the expected function */
   159 	if (!UTIL_VerifySignature(vfunc, sig, siglen))
   160 	{
   161 		return;
   162 	}
   163 
   164 	/* Get the offset into CreateFakeClient */
   165 	if (!g_pGameConf->GetOffset("sv", &offset))
   166 	{
   167 		return;
   168 	}
   169 
   170 	/* Finally we have the interface we were looking for */
   171 	iserver = *reinterpret_cast<IServer **>(reinterpret_cast<unsigned char *>(vfunc) + offset);
   172 }
   173 #elif defined PLATFORM_POSIX
   174 void GetIServer()
   175 {
   176 	void *addr;
   177 	if (!g_pGameConf->GetMemSig("sv", &addr) || !addr)
   178 	{
   179 		return;
   180 	}
   181 
   182 	iserver = reinterpret_cast<IServer *>(addr);
   183 }
   184 #endif