extensions/sdktools/vglobals.cpp
author Borja Ferrer <faluco@alliedmods.net>
Thu Dec 06 02:25:35 2007 +0000 (2007-12-06)
changeset 1769 7a3f09643820
parent 1718 2655287f48dd
child 1773 d00a853ec547
permissions -rw-r--r--
added amb1235, added support for the IServer interface
added a native to retrieve the spray file of a client
added a native to get the net traffic of the server
teame06@1245
     1
/**
teame06@1245
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
teame06@1245
     4
 * SourceMod SDKTools Extension
teame06@1245
     5
 * Copyright (C) 2004-2007 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
faluco@1769
    86
#if defined PLATFORM_WINDOWS
faluco@1769
    87
	/* Thanks to DS for the sigs */
faluco@1769
    88
	#define ISERVER_WIN_SIG				"\x8B\x44\x24\x2A\x50\xB9\x2A\x2A\x2A\x2A\xE8"
faluco@1769
    89
	#define ISERVER_WIN_SIG_LEN			11
faluco@1769
    90
void GetIServer()
faluco@1769
    91
{
faluco@1769
    92
	/* First check that the IVEngineServer::CreateFakeClient exists */
faluco@1769
    93
	if (!memutils->FindPattern(engine, ISERVER_WIN_SIG, ISERVER_WIN_SIG_LEN))
faluco@1769
    94
	{
faluco@1769
    95
		return;
faluco@1769
    96
	}
faluco@1769
    97
faluco@1769
    98
	int offset;
faluco@1769
    99
	void *vfunc = NULL;
faluco@1769
   100
faluco@1769
   101
	/* Get the offset into CreateFakeClient */
faluco@1769
   102
	if (!g_pGameConf->GetOffset("sv", &offset))
faluco@1769
   103
	{
faluco@1769
   104
		return;
faluco@1769
   105
	}
faluco@1769
   106
#if defined METAMOD_PLAPI_VERSION
faluco@1769
   107
	/* Get the CreateFakeClient function pointer */
faluco@1769
   108
	if (!(vfunc=SH_GET_ORIG_VFNPTR_ENTRY(engine, &IVEngineServer::CreateFakeClient)))
faluco@1769
   109
	{
faluco@1769
   110
		return;
faluco@1769
   111
	}
faluco@1769
   112
faluco@1769
   113
	/* Finally we have the interface we were looking for */
faluco@1769
   114
	iserver = *reinterpret_cast<IServer **>(reinterpret_cast<unsigned char *>(vfunc) + offset);
faluco@1769
   115
#else
faluco@1769
   116
	/* Get the interface manually */
faluco@1769
   117
	SourceHook::MemFuncInfo info = {true, -1, 0, 0};
faluco@1769
   118
	SourceHook::GetFuncInfo(&IVEngineServer::CreateFakeClient, info);
faluco@1769
   119
faluco@1769
   120
	vfunc = enginePatch->GetOrigFunc(info.vtbloffs, info.vtblindex);
faluco@1769
   121
	if (!vfunc)
faluco@1769
   122
	{
faluco@1769
   123
		void **vtable = *reinterpret_cast<void ***>(enginePatch->GetThisPtr() + info.thisptroffs + info.vtbloffs);
faluco@1769
   124
		vfunc = vtable[info.vtblindex];
faluco@1769
   125
	}
faluco@1769
   126
faluco@1769
   127
	iserver = *reinterpret_cast<IServer **>(reinterpret_cast<unsigned char *>(vfunc) + offset);
faluco@1769
   128
#endif
faluco@1769
   129
}
faluco@1769
   130
#elif defined PLATFORM_POSIX
faluco@1769
   131
void GetIServer()
faluco@1769
   132
{
faluco@1769
   133
	void *addr;
faluco@1769
   134
	if (!g_pGameConf->GetMemSig("sv", &addr) || !addr)
faluco@1769
   135
	{
faluco@1769
   136
		return;
faluco@1769
   137
	}
faluco@1769
   138
faluco@1769
   139
	iserver = *reinterpret_cast<IServer **>(addr);
faluco@1769
   140
}
faluco@1769
   141
#endif