2008-03-30 09:00:22 +02:00
|
|
|
/**
|
|
|
|
* vim: set ts=4 :
|
|
|
|
* =============================================================================
|
|
|
|
* SourceMod SDKTools Extension
|
|
|
|
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
|
|
|
|
* =============================================================================
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License, version 3.0, as published by the
|
|
|
|
* Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* As a special exception, AlliedModders LLC gives you permission to link the
|
|
|
|
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
|
|
|
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
|
|
|
* by the Valve Corporation. You must obey the GNU General Public License in
|
|
|
|
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
|
|
|
* this exception to all derivative works. AlliedModders LLC defines further
|
|
|
|
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
|
|
|
* or <http://www.sourcemod.net/license.php>.
|
|
|
|
*
|
|
|
|
* Version: $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "extension.h"
|
2013-03-16 18:50:36 +01:00
|
|
|
#include "vhelpers.h"
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2014-12-13 22:35:32 +01:00
|
|
|
static void *s_pGameRules = nullptr;
|
2014-12-13 21:48:51 +01:00
|
|
|
static void **g_ppGameRules = nullptr;
|
|
|
|
void *g_EntList = nullptr;
|
2013-03-16 18:50:36 +01:00
|
|
|
CBaseHandle g_ResourceEntity;
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2014-12-13 21:48:51 +01:00
|
|
|
void *GameRules()
|
|
|
|
{
|
2014-12-13 22:35:32 +01:00
|
|
|
return g_ppGameRules ? *g_ppGameRules : s_pGameRules;
|
2014-12-13 21:48:51 +01:00
|
|
|
}
|
2009-07-24 02:34:31 +02:00
|
|
|
|
2008-03-30 09:00:22 +02:00
|
|
|
void InitializeValveGlobals()
|
|
|
|
{
|
2009-07-24 02:34:31 +02:00
|
|
|
g_EntList = gamehelpers->GetGlobalEntityList();
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2011-04-04 19:44:19 +02:00
|
|
|
/*
|
|
|
|
* g_pGameRules
|
|
|
|
*
|
|
|
|
* First try to lookup pointer directly for platforms with symbols.
|
|
|
|
* If symbols aren't present (Windows or stripped Linux/Mac),
|
|
|
|
* attempt find via CreateGameRulesObject + offset
|
|
|
|
*/
|
2009-07-24 02:34:31 +02:00
|
|
|
|
2011-04-04 19:44:19 +02:00
|
|
|
char *addr;
|
|
|
|
if (g_pGameConf->GetMemSig("g_pGameRules", (void **)&addr) && addr)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2014-12-13 21:48:51 +01:00
|
|
|
g_ppGameRules = reinterpret_cast<void **>(addr);
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
2011-04-04 19:44:19 +02:00
|
|
|
else if (g_pGameConf->GetMemSig("CreateGameRulesObject", (void **)&addr) && addr)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2011-04-04 19:44:19 +02:00
|
|
|
int offset;
|
|
|
|
if (!g_pGameConf->GetOffset("g_pGameRules", &offset) || !offset)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-12-13 21:48:51 +01:00
|
|
|
g_ppGameRules = *reinterpret_cast<void ***>(addr + offset);
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
2009-07-24 02:34:31 +02:00
|
|
|
}
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2014-12-13 22:35:32 +01:00
|
|
|
static bool UTIL_FindDataTable(SendTable *pTable,
|
|
|
|
const char *name,
|
|
|
|
sm_sendprop_info_t *info,
|
|
|
|
unsigned int offset = 0)
|
|
|
|
{
|
|
|
|
const char *pname;
|
|
|
|
int props = pTable->GetNumProps();
|
|
|
|
SendProp *prop;
|
|
|
|
SendTable *table;
|
|
|
|
|
|
|
|
for (int i = 0; i<props; i++)
|
|
|
|
{
|
|
|
|
prop = pTable->GetProp(i);
|
|
|
|
|
|
|
|
if ((table = prop->GetDataTable()) != NULL)
|
|
|
|
{
|
|
|
|
pname = prop->GetName();
|
|
|
|
if (pname && strcmp(name, pname) == 0)
|
|
|
|
{
|
|
|
|
info->prop = prop;
|
|
|
|
info->actual_offset = offset + info->prop->GetOffset();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (UTIL_FindDataTable(table,
|
|
|
|
name,
|
|
|
|
info,
|
|
|
|
offset + prop->GetOffset())
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ServerClass *UTIL_FindServerClass(const char *classname)
|
|
|
|
{
|
|
|
|
ServerClass *sc = gamedll->GetAllServerClasses();
|
|
|
|
while (sc)
|
|
|
|
{
|
|
|
|
if (strcmp(classname, sc->GetName()) == 0)
|
|
|
|
{
|
|
|
|
return sc;
|
|
|
|
}
|
|
|
|
sc = sc->m_pNext;
|
|
|
|
}
|
|
|
|
|
2014-12-14 21:21:54 +01:00
|
|
|
return nullptr;
|
2014-12-13 22:35:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateValveGlobals()
|
|
|
|
{
|
|
|
|
s_pGameRules = nullptr;
|
|
|
|
|
|
|
|
const char *pszNetClass = g_pGameConf->GetKeyValue("GameRulesProxy");
|
|
|
|
const char *pszDTName = g_pGameConf->GetKeyValue("GameRulesDataTable");
|
|
|
|
if (pszNetClass && pszDTName)
|
|
|
|
{
|
|
|
|
sm_sendprop_info_t info;
|
2014-12-14 21:21:54 +01:00
|
|
|
ServerClass *sc = UTIL_FindServerClass(pszNetClass);
|
|
|
|
|
|
|
|
if (sc && UTIL_FindDataTable(sc->m_pTable, pszDTName, &info))
|
2014-12-13 22:35:32 +01:00
|
|
|
{
|
|
|
|
auto proxyFn = info.prop->GetDataTableProxyFn();
|
|
|
|
if (proxyFn)
|
|
|
|
{
|
|
|
|
CSendProxyRecipients recp;
|
|
|
|
s_pGameRules = proxyFn(nullptr, nullptr, nullptr, &recp, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-22 22:20:45 +01:00
|
|
|
size_t UTIL_StringToSignature(const char *str, char buffer[], size_t maxlength)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2009-01-22 22:20:45 +01:00
|
|
|
size_t real_bytes = 0;
|
|
|
|
size_t length = strlen(str);
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2009-01-22 22:20:45 +01:00
|
|
|
for (size_t i=0; i<length; i++)
|
|
|
|
{
|
|
|
|
if (real_bytes >= maxlength)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buffer[real_bytes++] = (unsigned char)str[i];
|
|
|
|
if (str[i] == '\\'
|
|
|
|
&& str[i+1] == 'x')
|
|
|
|
{
|
|
|
|
if (i + 3 >= length)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Get the hex part */
|
|
|
|
char s_byte[3];
|
|
|
|
int r_byte;
|
|
|
|
s_byte[0] = str[i+2];
|
|
|
|
s_byte[1] = str[i+3];
|
|
|
|
s_byte[2] = '\n';
|
|
|
|
/* Read it as an integer */
|
|
|
|
sscanf(s_byte, "%x", &r_byte);
|
|
|
|
/* Save the value */
|
|
|
|
buffer[real_bytes-1] = (unsigned char)r_byte;
|
|
|
|
/* Adjust index */
|
|
|
|
i += 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return real_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UTIL_VerifySignature(const void *addr, const char *sig, size_t len)
|
|
|
|
{
|
|
|
|
unsigned char *addr1 = (unsigned char *) addr;
|
|
|
|
unsigned char *addr2 = (unsigned char *) sig;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < len; i++)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
|
|
|
if (addr2[i] == '*')
|
|
|
|
continue;
|
|
|
|
if (addr1[i] != addr2[i])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined PLATFORM_WINDOWS
|
2014-03-03 12:20:40 +01:00
|
|
|
#define FAKECLIENT_KEY "CreateFakeClient_Windows"
|
|
|
|
#elif defined PLATFORM_LINUX
|
|
|
|
#define FAKECLIENT_KEY "CreateFakeClient_Linux"
|
|
|
|
#elif defined PLATFORM_APPLE
|
|
|
|
#define FAKECLIENT_KEY "CreateFakeClient_Mac"
|
|
|
|
#else
|
|
|
|
#error "Unsupported platform"
|
|
|
|
#endif
|
|
|
|
|
2008-03-30 09:00:22 +02:00
|
|
|
void GetIServer()
|
|
|
|
{
|
2015-05-07 03:12:13 +02:00
|
|
|
#if SOURCE_ENGINE == SE_TF2 \
|
|
|
|
|| SOURCE_ENGINE == SE_DODS \
|
|
|
|
|| SOURCE_ENGINE == SE_HL2DM \
|
|
|
|
|| SOURCE_ENGINE == SE_CSS \
|
2015-02-22 21:16:26 +01:00
|
|
|
|| SOURCE_ENGINE == SE_SDK2013 \
|
2015-05-07 03:12:13 +02:00
|
|
|
|| SOURCE_ENGINE == SE_BMS \
|
2015-02-22 21:16:26 +01:00
|
|
|
|| SOURCE_ENGINE == SE_INSURGENCY
|
2014-10-31 00:25:13 +01:00
|
|
|
|
2015-02-22 21:16:26 +01:00
|
|
|
#if SOURCE_ENGINE != SE_INSURGENCY
|
2014-10-31 00:25:13 +01:00
|
|
|
if (g_SMAPI->GetEngineFactory(false)("VEngineServer022", nullptr))
|
2015-02-22 21:16:26 +01:00
|
|
|
#endif // !SE_INSURGENCY
|
2014-10-31 00:25:13 +01:00
|
|
|
{
|
|
|
|
iserver = engine->GetIServer();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-03-03 12:20:40 +01:00
|
|
|
void *addr;
|
2009-01-22 22:20:45 +01:00
|
|
|
const char *sigstr;
|
|
|
|
char sig[32];
|
|
|
|
size_t siglen;
|
2008-03-30 09:00:22 +02:00
|
|
|
int offset;
|
|
|
|
void *vfunc = NULL;
|
|
|
|
|
2014-03-03 12:20:40 +01:00
|
|
|
/* Use the symbol if it exists */
|
|
|
|
if (g_pGameConf->GetMemSig("sv", &addr) && addr)
|
|
|
|
{
|
|
|
|
iserver = reinterpret_cast<IServer *>(addr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-30 09:00:22 +02:00
|
|
|
/* Get the CreateFakeClient function pointer */
|
|
|
|
if (!(vfunc=SH_GET_ORIG_VFNPTR_ENTRY(engine, &IVEngineServer::CreateFakeClient)))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2009-01-22 22:20:45 +01:00
|
|
|
|
|
|
|
/* Get signature string for IVEngineServer::CreateFakeClient() */
|
2014-03-03 12:20:40 +01:00
|
|
|
sigstr = g_pGameConf->GetKeyValue(FAKECLIENT_KEY);
|
2009-01-22 22:20:45 +01:00
|
|
|
|
|
|
|
if (!sigstr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert signature string to signature bytes */
|
|
|
|
siglen = UTIL_StringToSignature(sigstr, sig, sizeof(sig));
|
|
|
|
|
2008-03-30 09:00:22 +02:00
|
|
|
/* Check if we're on the expected function */
|
2009-01-22 22:20:45 +01:00
|
|
|
if (!UTIL_VerifySignature(vfunc, sig, siglen))
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-22 22:20:45 +01:00
|
|
|
/* Get the offset into CreateFakeClient */
|
|
|
|
if (!g_pGameConf->GetOffset("sv", &offset))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally we have the interface we were looking for */
|
2008-03-30 09:00:22 +02:00
|
|
|
iserver = *reinterpret_cast<IServer **>(reinterpret_cast<unsigned char *>(vfunc) + offset);
|
|
|
|
}
|
2011-07-06 23:37:35 +02:00
|
|
|
|
2013-03-16 18:50:36 +01:00
|
|
|
void GetResourceEntity()
|
|
|
|
{
|
|
|
|
g_ResourceEntity.Term();
|
|
|
|
|
2013-03-16 19:11:56 +01:00
|
|
|
#if SOURCE_ENGINE >= SE_ORANGEBOX
|
2013-03-16 18:50:36 +01:00
|
|
|
const char *classname = g_pGameConf->GetKeyValue("ResourceEntityClassname");
|
|
|
|
if (classname != NULL)
|
|
|
|
{
|
2013-03-16 19:11:56 +01:00
|
|
|
for (CBaseEntity *pEntity = (CBaseEntity *)servertools->FirstEntity(); pEntity; pEntity = (CBaseEntity *)servertools->NextEntity(pEntity))
|
2013-03-16 18:50:36 +01:00
|
|
|
{
|
2013-03-16 19:11:56 +01:00
|
|
|
if (!strcmp(gamehelpers->GetEntityClassname(pEntity), classname))
|
2013-03-16 18:50:36 +01:00
|
|
|
{
|
|
|
|
g_ResourceEntity = ((IHandleEntity *)pEntity)->GetRefEHandle();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2013-03-16 19:11:56 +01:00
|
|
|
#endif
|
2013-03-16 18:50:36 +01:00
|
|
|
{
|
|
|
|
int edictCount = gpGlobals->maxEntities;
|
|
|
|
|
|
|
|
for (int i=0; i<edictCount; i++)
|
|
|
|
{
|
|
|
|
edict_t *pEdict = PEntityOfEntIndex(i);
|
|
|
|
if (!pEdict || pEdict->IsFree())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!pEdict->GetNetworkable())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
IHandleEntity *pHandleEnt = pEdict->GetNetworkable()->GetEntityHandle();
|
|
|
|
if (!pHandleEnt)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerClass *pClass = pEdict->GetNetworkable()->GetServerClass();
|
|
|
|
if (FindNestedDataTable(pClass->m_pTable, "DT_PlayerResource"))
|
|
|
|
{
|
|
|
|
g_ResourceEntity = pHandleEnt->GetRefEHandle();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-06 23:37:35 +02:00
|
|
|
const char *GetDTTypeName(int type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case DPT_Int:
|
|
|
|
{
|
|
|
|
return "integer";
|
|
|
|
}
|
|
|
|
case DPT_Float:
|
|
|
|
{
|
|
|
|
return "float";
|
|
|
|
}
|
|
|
|
case DPT_Vector:
|
|
|
|
{
|
|
|
|
return "vector";
|
|
|
|
}
|
|
|
|
case DPT_String:
|
|
|
|
{
|
|
|
|
return "string";
|
|
|
|
}
|
|
|
|
case DPT_Array:
|
|
|
|
{
|
|
|
|
return "array";
|
|
|
|
}
|
|
|
|
case DPT_DataTable:
|
|
|
|
{
|
|
|
|
return "datatable";
|
|
|
|
}
|
2013-03-17 03:57:57 +01:00
|
|
|
#if SOURCE_ENGINE >= SE_ALIENSWARM
|
|
|
|
case DPT_Int64:
|
|
|
|
{
|
|
|
|
return "int64";
|
|
|
|
}
|
|
|
|
#endif
|
2011-07-06 23:37:35 +02:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|