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