extensions/sdktools/vglobals.cpp
author Scott Ehlert <ds@alliedmods.net>
Sun Sep 14 03:01:05 2008 -0500 (2008-09-14)
changeset 2289 44045ead61cd
parent 1952 b7aa2b626807
parent 1947 e73a1e51833b
child 2535 b3bfd3c1ad9f
permissions -rw-r--r--
Eliminated head: da55a80522cf, a remnant of sourcemod-1.1.0 branch
teame06@1245
     1
/**
teame06@1245
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
teame06@1245
     4
 * SourceMod SDKTools Extension
ds@1947
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1330
     6
 * =============================================================================
teame06@1245
     7
 *
ds@1330
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1330
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330
    10
 * Free Software Foundation.
teame06@1245
    11
 * 
ds@1330
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1330
    15
 * details.
teame06@1245
    16
 *
ds@1330
    17
 * You should have received a copy of the GNU General Public License along with
ds@1330
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
teame06@1245
    19
 *
ds@1330
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1330
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1330
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1330
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330
    27
 * or <http://www.sourcemod.net/license.php>.
faluco@1247
    28
 *
faluco@1247
    29
 * Version: $Id$
faluco@1247
    30
 */
faluco@1247
    31
faluco@1247
    32
#include "extension.h"
faluco@1247
    33
faluco@1247
    34
void **g_pGameRules = NULL;
faluco@1247
    35
void *g_EntList = NULL;
faluco@1247
    36
faluco@1247
    37
#ifdef PLATFORM_WINDOWS
faluco@1247
    38
void InitializeValveGlobals()
faluco@1247
    39
{
faluco@1247
    40
	char *addr = NULL;
faluco@1247
    41
	int offset;
faluco@1247
    42
faluco@1247
    43
	/* gEntList and/or g_pEntityList */
faluco@1247
    44
	if (!g_pGameConf->GetMemSig("LevelShutdown", (void **)&addr) || !addr)
faluco@1247
    45
	{
faluco@1247
    46
		return;
faluco@1247
    47
	}
faluco@1247
    48
	if (!g_pGameConf->GetOffset("gEntList", &offset) || !offset)
faluco@1247
    49
	{
faluco@1247
    50
		return;
faluco@1247
    51
	}
faluco@1247
    52
	g_EntList = *reinterpret_cast<void **>(addr + offset);
faluco@1718
    53
faluco@1718
    54
	/* g_pGameRules */
faluco@1718
    55
	if (!g_pGameConf->GetMemSig("CreateGameRulesObject", (void **)&addr) || !addr)
faluco@1718
    56
	{
faluco@1718
    57
		return;
faluco@1718
    58
	}
faluco@1718
    59
	if (!g_pGameConf->GetOffset("g_pGameRules", &offset) || !offset)
faluco@1718
    60
	{
faluco@1718
    61
		return;
faluco@1718
    62
	}
faluco@1718
    63
	g_pGameRules = *reinterpret_cast<void ***>(addr + offset);
faluco@1247
    64
}
faluco@1247
    65
#elif defined PLATFORM_LINUX
faluco@1247
    66
void InitializeValveGlobals()
faluco@1247
    67
{
faluco@1247
    68
	char *addr = NULL;
faluco@1247
    69
faluco@1718
    70
	/* gEntList and/or g_pEntityList */
faluco@1718
    71
	if (!g_pGameConf->GetMemSig("gEntList", (void **)&addr) || !addr)
faluco@1718
    72
	{
faluco@1718
    73
		return;
faluco@1718
    74
	}
faluco@1718
    75
	g_EntList = reinterpret_cast<void *>(addr);
faluco@1718
    76
faluco@1247
    77
	/* g_pGameRules */
faluco@1247
    78
	if (!g_pGameConf->GetMemSig("g_pGameRules", (void **)&addr) || !addr)
faluco@1247
    79
	{
faluco@1247
    80
		return;
faluco@1247
    81
	}
faluco@1247
    82
	g_pGameRules = reinterpret_cast<void **>(addr);
faluco@1247
    83
}
faluco@1247
    84
#endif
faluco@1769
    85
ds@1952
    86
bool vcmp(const void *_addr1, const void *_addr2, size_t len)
ds@1952
    87
{
ds@1952
    88
	unsigned char *addr1 = (unsigned char *)_addr1;
ds@1952
    89
	unsigned char *addr2 = (unsigned char *)_addr2;
ds@1952
    90
ds@1952
    91
	for (size_t i=0; i<len; i++)
ds@1952
    92
	{
ds@1952
    93
		if (addr2[i] == '*')
ds@1952
    94
			continue;
ds@1952
    95
		if (addr1[i] != addr2[i])
ds@1952
    96
			return false;
ds@1952
    97
	}
ds@1952
    98
ds@1952
    99
	return true;
faluco@1773
   100
}
faluco@1773
   101
faluco@1769
   102
#if defined PLATFORM_WINDOWS
faluco@1769
   103
	/* Thanks to DS for the sigs */
faluco@1769
   104
	#define ISERVER_WIN_SIG				"\x8B\x44\x24\x2A\x50\xB9\x2A\x2A\x2A\x2A\xE8"
faluco@1769
   105
	#define ISERVER_WIN_SIG_LEN			11
faluco@1769
   106
void GetIServer()
faluco@1769
   107
{
faluco@1769
   108
	int offset;
faluco@1769
   109
	void *vfunc = NULL;
faluco@1769
   110
faluco@1769
   111
	/* Get the offset into CreateFakeClient */
faluco@1769
   112
	if (!g_pGameConf->GetOffset("sv", &offset))
faluco@1769
   113
	{
faluco@1769
   114
		return;
faluco@1769
   115
	}
faluco@1769
   116
#if defined METAMOD_PLAPI_VERSION
faluco@1769
   117
	/* Get the CreateFakeClient function pointer */
faluco@1769
   118
	if (!(vfunc=SH_GET_ORIG_VFNPTR_ENTRY(engine, &IVEngineServer::CreateFakeClient)))
faluco@1769
   119
	{
faluco@1769
   120
		return;
faluco@1769
   121
	}
faluco@1769
   122
faluco@1773
   123
	/* Check if we're on the expected function */
faluco@1773
   124
	if (!vcmp(vfunc, ISERVER_WIN_SIG, ISERVER_WIN_SIG_LEN))
faluco@1773
   125
	{
faluco@1773
   126
		return;
faluco@1773
   127
	}
faluco@1773
   128
faluco@1769
   129
	/* Finally we have the interface we were looking for */
faluco@1769
   130
	iserver = *reinterpret_cast<IServer **>(reinterpret_cast<unsigned char *>(vfunc) + offset);
faluco@1769
   131
#else
faluco@1769
   132
	/* Get the interface manually */
faluco@1769
   133
	SourceHook::MemFuncInfo info = {true, -1, 0, 0};
faluco@1769
   134
	SourceHook::GetFuncInfo(&IVEngineServer::CreateFakeClient, info);
faluco@1769
   135
faluco@1769
   136
	vfunc = enginePatch->GetOrigFunc(info.vtbloffs, info.vtblindex);
faluco@1769
   137
	if (!vfunc)
faluco@1769
   138
	{
faluco@1769
   139
		void **vtable = *reinterpret_cast<void ***>(enginePatch->GetThisPtr() + info.thisptroffs + info.vtbloffs);
faluco@1769
   140
		vfunc = vtable[info.vtblindex];
faluco@1769
   141
	}
faluco@1773
   142
	/* Check if we're on the expected function */
faluco@1773
   143
	if (!vcmp(vfunc, ISERVER_WIN_SIG, ISERVER_WIN_SIG_LEN))
faluco@1773
   144
	{
faluco@1773
   145
		return;
faluco@1773
   146
	}
faluco@1769
   147
faluco@1769
   148
	iserver = *reinterpret_cast<IServer **>(reinterpret_cast<unsigned char *>(vfunc) + offset);
faluco@1769
   149
#endif
faluco@1769
   150
}
faluco@1769
   151
#elif defined PLATFORM_POSIX
faluco@1769
   152
void GetIServer()
faluco@1769
   153
{
faluco@1769
   154
	void *addr;
faluco@1769
   155
	if (!g_pGameConf->GetMemSig("sv", &addr) || !addr)
faluco@1769
   156
	{
faluco@1769
   157
		return;
faluco@1769
   158
	}
faluco@1769
   159
faluco@1773
   160
	iserver = reinterpret_cast<IServer *>(addr);
faluco@1769
   161
}
faluco@1769
   162
#endif