From 0c7696da65379a3661af0afad3f4047293e5b032 Mon Sep 17 00:00:00 2001 From: Nicholas Hastings Date: Sat, 16 Mar 2013 13:50:36 -0400 Subject: [PATCH] Add GetPlayerResourceEntity to SDKTools; deprecate old, broken TF2-specific impl (bug 5491, r=asherkin). --- extensions/sdktools/extension.cpp | 7 +++ extensions/sdktools/extension.h | 2 - extensions/sdktools/msvc10/sdktools.vcxproj | 1 + .../sdktools/msvc10/sdktools.vcxproj.filters | 3 ++ extensions/sdktools/teamnatives.cpp | 32 ++---------- extensions/sdktools/teamnatives.h | 39 +++++++++++++++ extensions/sdktools/vglobals.cpp | 50 +++++++++++++++++++ extensions/sdktools/vglobals.h | 4 ++ extensions/sdktools/vhelpers.cpp | 25 ++++++++++ extensions/sdktools/vhelpers.h | 2 + extensions/sdktools/vnatives.cpp | 11 ++++ plugins/include/sdktools.inc | 7 +++ plugins/include/tf2.inc | 1 + plugins/include/tf2_stocks.inc | 2 + 14 files changed, 157 insertions(+), 29 deletions(-) create mode 100644 extensions/sdktools/teamnatives.h diff --git a/extensions/sdktools/extension.cpp b/extensions/sdktools/extension.cpp index 3bfe5b35..43b877b0 100644 --- a/extensions/sdktools/extension.cpp +++ b/extensions/sdktools/extension.cpp @@ -43,6 +43,7 @@ #include "gamerulesnatives.h" #include #include "clientnatives.h" +#include "teamnatives.h" /** * @file extension.cpp * @brief Implements SDK Tools extension code. @@ -279,6 +280,12 @@ void SDKTools::SDK_OnAllLoaded() InitializeValveGlobals(); } +void SDKTools::OnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax) +{ + InitTeamNatives(); + GetResourceEntity(); +} + bool SDKTools::QueryRunning(char *error, size_t maxlength) { SM_CHECK_IFACE(BINTOOLS, g_pBinTools); diff --git a/extensions/sdktools/extension.h b/extensions/sdktools/extension.h index fb88331a..b5766bec 100644 --- a/extensions/sdktools/extension.h +++ b/extensions/sdktools/extension.h @@ -135,8 +135,6 @@ extern ICallWrapper *g_pAcceptInput; extern SourceHook::CallClass *enginePatch; extern SourceHook::CallClass *enginesoundPatch; -extern const char *tools_GetTeamName(int team); - #include #define ENGINE_CALL(func) SH_CALL(enginePatch, &IVEngineServer::func) diff --git a/extensions/sdktools/msvc10/sdktools.vcxproj b/extensions/sdktools/msvc10/sdktools.vcxproj index e17e60e3..db504c5b 100644 --- a/extensions/sdktools/msvc10/sdktools.vcxproj +++ b/extensions/sdktools/msvc10/sdktools.vcxproj @@ -1325,6 +1325,7 @@ + diff --git a/extensions/sdktools/msvc10/sdktools.vcxproj.filters b/extensions/sdktools/msvc10/sdktools.vcxproj.filters index b2eb0c7f..b2224b9f 100644 --- a/extensions/sdktools/msvc10/sdktools.vcxproj.filters +++ b/extensions/sdktools/msvc10/sdktools.vcxproj.filters @@ -137,6 +137,9 @@ Header Files + + Header Files + diff --git a/extensions/sdktools/teamnatives.cpp b/extensions/sdktools/teamnatives.cpp index 3c3e21e6..f8910cee 100644 --- a/extensions/sdktools/teamnatives.cpp +++ b/extensions/sdktools/teamnatives.cpp @@ -30,6 +30,7 @@ */ #include "extension.h" +#include "vhelpers.h" #include struct TeamInfo @@ -42,36 +43,13 @@ const char *m_iScore; SourceHook::CVector g_Teams; -bool FindTeamEntities(SendTable *pTable, const char *name) -{ - if (strcmp(pTable->GetName(), name) == 0) - { - return true; - } - - int props = pTable->GetNumProps(); - SendProp *prop; - - for (int i=0; iGetProp(i); - if (prop->GetDataTable()) - { - if (FindTeamEntities(prop->GetDataTable(), name)) - { - return true; - } - } - } - - return false; -} - -void SDKTools::OnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax) +void InitTeamNatives() { g_Teams.clear(); g_Teams.resize(1); + int edictCount = gpGlobals->maxEntities; + for (int i=0; iGetNetworkable()->GetServerClass(); - if (FindTeamEntities(pClass->m_pTable, "DT_Team")) + if (FindNestedDataTable(pClass->m_pTable, "DT_Team")) { SendProp *pTeamNumProp = g_pGameHelpers->FindInSendTable(pClass->GetName(), "m_iTeamNum"); diff --git a/extensions/sdktools/teamnatives.h b/extensions/sdktools/teamnatives.h new file mode 100644 index 00000000..f4a2c900 --- /dev/null +++ b/extensions/sdktools/teamnatives.h @@ -0,0 +1,39 @@ +/** + * 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 . + * + * 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 . + * + * Version: $Id$ + */ + +#ifndef _INCLUDE_SOURCEMOD_TEAMNATIVES_H_ +#define _INCLUDE_SOURCEMOD_TEAMNATIVES_H_ + +void InitTeamNatives(); + +extern const char *tools_GetTeamName(int team); + +#endif //_INCLUDE_SOURCEMOD_TEAMNATIVES_H_ diff --git a/extensions/sdktools/vglobals.cpp b/extensions/sdktools/vglobals.cpp index 5bdae558..cbaa49d3 100644 --- a/extensions/sdktools/vglobals.cpp +++ b/extensions/sdktools/vglobals.cpp @@ -30,9 +30,11 @@ */ #include "extension.h" +#include "vhelpers.h" void **g_pGameRules = NULL; void *g_EntList = NULL; +CBaseHandle g_ResourceEntity; void InitializeValveGlobals() @@ -183,6 +185,54 @@ void GetIServer() } #endif +void GetResourceEntity() +{ + g_ResourceEntity.Term(); + + const char *classname = g_pGameConf->GetKeyValue("ResourceEntityClassname"); + if (classname != NULL) + { + for (void *pEntity = servertools->FirstEntity(); pEntity; pEntity = servertools->NextEntity(pEntity)) + { + if (!strcmp(gamehelpers->GetEntityClassname((CBaseEntity *)pEntity), classname)) + { + g_ResourceEntity = ((IHandleEntity *)pEntity)->GetRefEHandle(); + break; + } + } + } + else + { + int edictCount = gpGlobals->maxEntities; + + for (int i=0; iIsFree()) + { + 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; + } + } + } +} + const char *GetDTTypeName(int type) { switch (type) diff --git a/extensions/sdktools/vglobals.h b/extensions/sdktools/vglobals.h index 1fc817ee..5cc2081e 100644 --- a/extensions/sdktools/vglobals.h +++ b/extensions/sdktools/vglobals.h @@ -35,9 +35,13 @@ extern void **g_pGameRules; extern void *g_EntList; +extern CBaseHandle g_ResourceEntity; + void InitializeValveGlobals(); void GetIServer(); +void GetResourceEntity(); + const char *GetDTTypeName(int type); #endif // _INCLUDE_SDKTOOLS_VGLOBALS_H_ diff --git a/extensions/sdktools/vhelpers.cpp b/extensions/sdktools/vhelpers.cpp index e2f95c06..c18b26e4 100644 --- a/extensions/sdktools/vhelpers.cpp +++ b/extensions/sdktools/vhelpers.cpp @@ -279,6 +279,31 @@ void ShutdownHelpers() s_EyeAngles.Shutdown(); } +bool FindNestedDataTable(SendTable *pTable, const char *name) +{ + if (strcmp(pTable->GetName(), name) == 0) + { + return true; + } + + int props = pTable->GetNumProps(); + SendProp *prop; + + for (int i=0; iGetProp(i); + if (prop->GetDataTable()) + { + if (FindNestedDataTable(prop->GetDataTable(), name)) + { + return true; + } + } + } + + return false; +} + void UTIL_DrawSendTable_XML(FILE *fp, SendTable *pTable, int space_count) { char spaces[255]; diff --git a/extensions/sdktools/vhelpers.h b/extensions/sdktools/vhelpers.h index 46cbdb81..ad092ea0 100644 --- a/extensions/sdktools/vhelpers.h +++ b/extensions/sdktools/vhelpers.h @@ -72,4 +72,6 @@ bool GetPlayerInfo(int client, player_info_t *info); void ShutdownHelpers(); +bool FindNestedDataTable(SendTable *pTable, const char *name); + #endif //_INCLUDE_SDKTOOLS_VHELPERS_H_ diff --git a/extensions/sdktools/vnatives.cpp b/extensions/sdktools/vnatives.cpp index af212e7f..1e66715b 100644 --- a/extensions/sdktools/vnatives.cpp +++ b/extensions/sdktools/vnatives.cpp @@ -1208,6 +1208,16 @@ static cell_t SetClientInfo(IPluginContext *pContext, const cell_t *params) return 1; } +static cell_t GetPlayerResourceEntity(IPluginContext *pContext, const cell_t *params) +{ + if (gamehelpers->GetHandleEntity(g_ResourceEntity) != NULL) + { + return g_ResourceEntity.GetEntryIndex(); + } + + return -1; +} + sp_nativeinfo_t g_Natives[] = { {"ExtinguishEntity", ExtinguishEntity}, @@ -1235,5 +1245,6 @@ sp_nativeinfo_t g_Natives[] = {"EquipPlayerWeapon", WeaponEquip}, {"ActivateEntity", ActivateEntity}, {"SetClientInfo", SetClientInfo}, + {"GetPlayerResourceEntity", GetPlayerResourceEntity}, {NULL, NULL}, }; diff --git a/plugins/include/sdktools.inc b/plugins/include/sdktools.inc index ad01d1ea..5621266f 100644 --- a/plugins/include/sdktools.inc +++ b/plugins/include/sdktools.inc @@ -194,6 +194,13 @@ native Handle:EndPrepSDKCall(); */ native any:SDKCall(Handle:call, any:...); +/* + * Returns the entity index of the player resource/manager entity. + * + * @return Index of resource entity or -1 if not found. + */ +native GetPlayerResourceEntity(); + #include /** diff --git a/plugins/include/tf2.inc b/plugins/include/tf2.inc index 7b5961e6..7700bb5d 100644 --- a/plugins/include/tf2.inc +++ b/plugins/include/tf2.inc @@ -278,6 +278,7 @@ native TF2_MakeBleed(client, attacker, Float:duration); * * @return The current resource entity index. */ +#pragma deprecated Use GetPlayerResourceEntity instead native TF2_GetResourceEntity(); /** diff --git a/plugins/include/tf2_stocks.inc b/plugins/include/tf2_stocks.inc index 455258b2..eef22a73 100644 --- a/plugins/include/tf2_stocks.inc +++ b/plugins/include/tf2_stocks.inc @@ -350,6 +350,7 @@ stock TF2_SetPlayerClass(client, TFClassType:class, bool:weapons=true, bool:pers * @return Value or -1 on failure. * @error Invalid client index, client not in game or failed to find resource entity. */ +#pragma deprecated Use GetPlayerResourceEntity and GetEntProp instead stock TF2_GetPlayerResourceData(client, TFResourceType:type) { if (!IsClientConnected(client)) @@ -385,6 +386,7 @@ stock TF2_GetPlayerResourceData(client, TFResourceType:type) * @return Value or -1 on failure. * @error Invalid client index, client not in game or failed to find resource entity. */ +#pragma deprecated Use GetPlayerResourceEntity and SetEntProp instead stock bool:TF2_SetPlayerResourceData(client, TFResourceType:type, any:value) { if (!IsClientConnected(client))