From 0a5981b2e48aa309233552935854778f96f4e7ee Mon Sep 17 00:00:00 2001 From: Ayuto Date: Mon, 9 Nov 2015 23:42:37 +0100 Subject: [PATCH 01/13] Initial conversion functions update --- src/core/modules/commands/commands_client.cpp | 5 +- src/core/modules/entities/entities.h | 39 ++++-- src/core/modules/entities/entities_datamaps.h | 36 +++-- src/core/modules/entities/entities_entity.h | 56 ++++++-- .../modules/entities/entities_generator.cpp | 5 +- src/core/modules/entities/entities_helpers.h | 15 +- .../entities/entities_helpers_wrap.cpp | 63 ++++----- .../entities/orangebox/entities_helpers.h | 7 +- .../modules/filters/filters_recipients.cpp | 13 +- .../modules/players/players_generator.cpp | 3 +- .../modules/players/players_helpers_wrap.cpp | 53 +++---- src/core/patches/undefined_symbols.cpp | 12 +- src/core/sp_main.cpp | 73 ++++++++-- src/core/utilities/conversions.h | 130 +++++++++-------- .../utilities/conversions/baseentity_from.cpp | 89 +++++------- .../utilities/conversions/basehandle_from.cpp | 93 +++++++------ src/core/utilities/conversions/edict_from.cpp | 128 +++++++---------- src/core/utilities/conversions/index_from.cpp | 131 +++++++++--------- .../utilities/conversions/inthandle_from.cpp | 82 +++++------ .../utilities/conversions/playerinfo_from.cpp | 87 ++++++------ .../utilities/conversions/pointer_from.cpp | 75 +++++----- .../utilities/conversions/userid_from.cpp | 85 +++++------- 22 files changed, 686 insertions(+), 594 deletions(-) diff --git a/src/core/modules/commands/commands_client.cpp b/src/core/modules/commands/commands_client.cpp index b5c1b3622..cfb13bceb 100644 --- a/src/core/modules/commands/commands_client.cpp +++ b/src/core/modules/commands/commands_client.cpp @@ -112,7 +112,10 @@ void UnregisterClientCommandFilter(PyObject* pCallable) //----------------------------------------------------------------------------- PLUGIN_RESULT DispatchClientCommand(edict_t* pEntity, const CCommand &command) { - int iIndex = IndexFromEdict(pEntity); + unsigned int iIndex; + // TODO: When does this ever happen? + if (!IndexFromEdict(pEntity, iIndex)) + iIndex = -1; // Loop through all registered Client Command Filters for(int i = 0; i < s_ClientCommandFilters.m_vecCallables.Count(); i++) diff --git a/src/core/modules/entities/entities.h b/src/core/modules/entities/entities.h index 187bfcd3f..ac0a03800 100644 --- a/src/core/modules/entities/entities.h +++ b/src/core/modules/entities/entities.h @@ -48,34 +48,49 @@ class TakeDamageInfoBaseWrapper: public CTakeDamageInfo { public: - int get_inflictor() + object get_inflictor() { - return IndexFromBaseHandle(m_hInflictor); + unsigned int iEntityIndex; + if (!IndexFromBaseHandle(m_hInflictor, iEntityIndex)) + return object(); + + return object(iEntityIndex); } void set_inflictor(unsigned int uiInflictor) { - m_hInflictor = BaseHandleFromIndex(uiInflictor, true); + if (!BaseHandleFromIndex(uiInflictor, m_hInflictor)) + BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseHandle object from the given index: '%i'", uiInflictor); } - int get_attacker() + object get_attacker() { - return IndexFromBaseHandle(m_hAttacker); + unsigned int iEntityIndex; + if (!IndexFromBaseHandle(m_hAttacker, iEntityIndex)) + return object(); + + return object(iEntityIndex); } void set_attacker(unsigned int uiAttacker) { - m_hAttacker = BaseHandleFromIndex(uiAttacker, true); + if (!BaseHandleFromIndex(uiAttacker, m_hAttacker)) + BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseHandle object from the given index: '%i'", uiAttacker); } - int get_weapon() + object get_weapon() { - return IndexFromBaseHandle(m_hWeapon); + unsigned int iEntityIndex; + if (!IndexFromBaseHandle(m_hWeapon, iEntityIndex)) + return object(); + + return object(iEntityIndex); } void set_weapon(unsigned int uiWeapon) { - m_hWeapon = BaseHandleFromIndex(uiWeapon, true); + if (!BaseHandleFromIndex(uiWeapon, m_hWeapon)) + BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseHandle object from the given index: '%i'", uiWeapon); } void set_base_damage(float flBaseDamage) @@ -110,7 +125,7 @@ class TakeDamageInfoSharedExt return pTakeDamageInfo; } - static int get_inflictor(CTakeDamageInfo *pTakeDamageInfo) + static object get_inflictor(CTakeDamageInfo *pTakeDamageInfo) { return ((TakeDamageInfoBaseWrapper *)pTakeDamageInfo)->get_inflictor(); } @@ -120,7 +135,7 @@ class TakeDamageInfoSharedExt ((TakeDamageInfoBaseWrapper *)pTakeDamageInfo)->set_inflictor(iInflictor); } - static int get_attacker(CTakeDamageInfo *pTakeDamageInfo) + static object get_attacker(CTakeDamageInfo *pTakeDamageInfo) { return ((TakeDamageInfoBaseWrapper *)pTakeDamageInfo)->get_attacker(); } @@ -130,7 +145,7 @@ class TakeDamageInfoSharedExt ((TakeDamageInfoBaseWrapper *)pTakeDamageInfo)->set_attacker(iAttacker); } - static int get_weapon(CTakeDamageInfo *pTakeDamageInfo) + static object get_weapon(CTakeDamageInfo *pTakeDamageInfo) { return ((TakeDamageInfoBaseWrapper *)pTakeDamageInfo)->get_weapon(); } diff --git a/src/core/modules/entities/entities_datamaps.h b/src/core/modules/entities/entities_datamaps.h index 2d9ed91ee..1d7465b4d 100644 --- a/src/core/modules/entities/entities_datamaps.h +++ b/src/core/modules/entities/entities_datamaps.h @@ -134,14 +134,22 @@ class VariantSharedExt return pVector; } - static int get_entity(variant_t *pVariant) + static object get_entity(variant_t *pVariant) { - return IndexFromBaseHandle(pVariant->Entity()); + unsigned int iEntityIndex; + if (!IndexFromBaseHandle(pVariant->Entity(), iEntityIndex)) + return object(); + + return object(iEntityIndex); } static void set_entity(variant_t *pVariant, unsigned int uiEntity) { - pVariant->SetEntity(BaseEntityFromIndex(uiEntity, true)); + CBaseEntity* pBaseEntity; + if (!BaseEntityFromIndex(uiEntity, pBaseEntity)) + BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseEntity object from the given index: '%i'", uiEntity); + + pVariant->SetEntity(pBaseEntity); } }; @@ -161,24 +169,34 @@ class InputDataSharedExt return pInputData; } - static int get_activator(const inputdata_t& pInputData) + static object get_activator(const inputdata_t& pInputData) { - return IndexFromBaseEntity(pInputData.pActivator); + unsigned int iEntityIndex; + if (!IndexFromBaseEntity(pInputData.pActivator, iEntityIndex)) + return object(); + + return object(iEntityIndex); } static void set_activator(inputdata_t *pInputData, unsigned int uiActivator) { - pInputData->pActivator = BaseEntityFromIndex(uiActivator, true); + if (!BaseEntityFromIndex(uiActivator, pInputData->pActivator)) + BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseEntity object from the given index: '%i'", uiActivator); } - static int get_caller(const inputdata_t& pInputData) + static object get_caller(const inputdata_t& pInputData) { - return IndexFromBaseEntity(pInputData.pCaller); + unsigned int iEntityIndex; + if (!IndexFromBaseEntity(pInputData.pCaller, iEntityIndex)) + return object(); + + return object(iEntityIndex); } static void set_caller(inputdata_t *pInputData, unsigned int uiCaller) { - pInputData->pCaller = BaseEntityFromIndex(uiCaller, true); + if (!BaseEntityFromIndex(uiCaller, pInputData->pCaller)) + BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseEntity object from the given index: '%i'", uiCaller); } }; diff --git a/src/core/modules/entities/entities_entity.h b/src/core/modules/entities/entities_entity.h index 28d739b52..1fd94f286 100644 --- a/src/core/modules/entities/entities_entity.h +++ b/src/core/modules/entities/entities_entity.h @@ -72,7 +72,11 @@ class CBaseEntityWrapper: public IServerEntity public: static boost::shared_ptr __init__(unsigned int uiEntityIndex) { - return CBaseEntityWrapper::wrap(BaseEntityFromIndex(uiEntityIndex, true)); + CBaseEntity* pBaseEntity; + if (!BaseEntityFromIndex(uiEntityIndex, pBaseEntity)) + BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to create BaseEntity object from this index: '%i'", uiEntityIndex); + + return CBaseEntityWrapper::wrap(pBaseEntity); } static boost::shared_ptr wrap(CBaseEntity* pEntity) @@ -188,27 +192,57 @@ class CBaseEntityWrapper: public IServerEntity } edict_t* GetEdict() - { return EdictFromBaseEntity(GetThis()); } + { + edict_t* pEdict; + if (!EdictFromBaseEntity(GetThis(), pEdict)) + return NULL; + + return pEdict; + } - int GetIndex() - { return IndexFromBaseEntity(GetThis()); } + object GetIndex() + { + unsigned int iEntityIndex; + if (!IndexFromBaseEntity(GetThis(), iEntityIndex)) + return object(); + + return object(iEntityIndex); + } CPointer* GetPointer() - { return PointerFromBaseEntity(GetThis()); } + { + CPointer pPointer; + if (!PointerFromBaseEntity(GetThis(), pPointer)) + return NULL; + + return new CPointer(pPointer); + } - CBaseHandle GetBaseHandle() - { return BaseHandleFromBaseEntity(GetThis()); } + object GetBaseHandle() + { + CBaseHandle hBaseHandle; + if (!BaseHandleFromBaseEntity(GetThis(), hBaseHandle)) + return object(); + + return object(hBaseHandle); + } - int GetIntHandle() - { return IntHandleFromBaseEntity(GetThis()); } + object GetIntHandle() + { + unsigned int iEntityHandle; + if (!IntHandleFromBaseEntity(GetThis(), iEntityHandle)) + return object(); + + return object(iEntityHandle); + } bool IsPlayer() { if (!IServerUnknownExt::IsNetworked(GetThis())) return false; - int index = GetIndex(); - return index > WORLD_ENTITY_INDEX && index <= gpGlobals->maxClients; + unsigned int iEntityIndex; + return IndexFromBaseEntity(GetThis(), iEntityIndex); } }; diff --git a/src/core/modules/entities/entities_generator.cpp b/src/core/modules/entities/entities_generator.cpp index 7af93406e..7a6a3a5ea 100644 --- a/src/core/modules/entities/entities_generator.cpp +++ b/src/core/modules/entities/entities_generator.cpp @@ -103,7 +103,10 @@ edict_t* CEntityGenerator::getNext() { while (m_pCurrentEntity) { - edict_t *pEdict = EdictFromBaseEntity(m_pCurrentEntity); + edict_t *pEdict; + if (!EdictFromBaseEntity(m_pCurrentEntity, pEdict)) + pEdict = NULL; + m_pCurrentEntity = (CBaseEntity *)servertools->NextEntity(m_pCurrentEntity); if (pEdict) { diff --git a/src/core/modules/entities/entities_helpers.h b/src/core/modules/entities/entities_helpers.h index e71362149..7c030c152 100644 --- a/src/core/modules/entities/entities_helpers.h +++ b/src/core/modules/entities/entities_helpers.h @@ -44,23 +44,32 @@ extern IServerTools *servertools; //----------------------------------------------------------------------------- // Creates an entity of the given name and returns its index... //----------------------------------------------------------------------------- +// TODO: This should return a BaseEntity object unsigned int create_entity(const char *szClassName) { CBaseEntity *pBaseEntity = (CBaseEntity *)servertools->CreateEntityByName(szClassName); - if (!pBaseEntity) BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Unable to create an entity of type \"%s\".", szClassName); - return IndexFromBaseEntity(pBaseEntity); + unsigned int iEntityIndex; + if (!IndexFromBaseEntity(pBaseEntity, iEntityIndex)) + BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an index from the given BaseEntity object: '%p'", pBaseEntity); + + return iEntityIndex; } //----------------------------------------------------------------------------- // Spawns the given entity index... //----------------------------------------------------------------------------- +// TODO: This should accept a BaseEntity object void spawn_entity(unsigned int uiEntityIndex) { - servertools->DispatchSpawn(BaseEntityFromIndex(uiEntityIndex, true)); + CBaseEntity* pBaseEntity; + if (!BaseEntityFromIndex(uiEntityIndex, pBaseEntity)) + BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseEntity object from the given index: '%i'", uiEntityIndex); + + servertools->DispatchSpawn(pBaseEntity); } diff --git a/src/core/modules/entities/entities_helpers_wrap.cpp b/src/core/modules/entities/entities_helpers_wrap.cpp index 59de90e7a..2079df73e 100644 --- a/src/core/modules/entities/entities_helpers_wrap.cpp +++ b/src/core/modules/entities/entities_helpers_wrap.cpp @@ -31,6 +31,7 @@ #include "utilities/wrap_macros.h" #include "utilities/conversions.h" #include "entities_helpers.h" +#include "entities_entity.h" #include ENGINE_INCLUDE_PATH(entities_helpers.h) @@ -84,45 +85,45 @@ void export_entity_helper_functions(scope _helpers) //----------------------------------------------------------------------------- void export_entity_conversion_functions(scope _helpers) { - // To Index conversions... - EXPORT_CONVERSION_FUNCTION(unsigned int, Index, edict_t *, Edict); - EXPORT_CONVERSION_FUNCTION(unsigned int, Index, CBaseHandle, BaseHandle); - EXPORT_CONVERSION_FUNCTION(unsigned int, Index, int, IntHandle); - EXPORT_CONVERSION_FUNCTION(unsigned int, Index, CPointer *, Pointer); - EXPORT_CONVERSION_FUNCTION(unsigned int, Index, CBaseEntity *, BaseEntity); +// To Index conversions... + EXPORT_CONVERSION_FUNCTION(unsigned int, Index, edict_t *, Edict, object(result)); + EXPORT_CONVERSION_FUNCTION(unsigned int, Index, CBaseHandle, BaseHandle, object(result)); + EXPORT_CONVERSION_FUNCTION(unsigned int, Index, unsigned int, IntHandle, object(result)); + EXPORT_CONVERSION_FUNCTION(unsigned int, Index, CPointer *, Pointer, object(result)); + EXPORT_CONVERSION_FUNCTION(unsigned int, Index, CBaseEntity *, BaseEntity, object(result)); // To Edict conversions... - EXPORT_CONVERSION_FUNCTION(edict_t *, Edict, unsigned int, Index, reference_existing_object_policy()); - EXPORT_CONVERSION_FUNCTION(edict_t *, Edict, CBaseHandle, BaseHandle, reference_existing_object_policy()); - EXPORT_CONVERSION_FUNCTION(edict_t *, Edict, int, IntHandle, reference_existing_object_policy()); - EXPORT_CONVERSION_FUNCTION(edict_t *, Edict, CPointer *, Pointer, reference_existing_object_policy()); - EXPORT_CONVERSION_FUNCTION(edict_t *, Edict, CBaseEntity *, BaseEntity, reference_existing_object_policy()); + EXPORT_CONVERSION_FUNCTION(edict_t *, Edict, unsigned int, Index, object(ptr(result))); + EXPORT_CONVERSION_FUNCTION(edict_t *, Edict, CBaseHandle, BaseHandle, object(ptr(result))); + EXPORT_CONVERSION_FUNCTION(edict_t *, Edict, unsigned int, IntHandle, object(ptr(result))); + EXPORT_CONVERSION_FUNCTION(edict_t *, Edict, CPointer *, Pointer, object(ptr(result))); + EXPORT_CONVERSION_FUNCTION(edict_t *, Edict, CBaseEntity *, BaseEntity, object(ptr(result))); // To BaseHandle conversions... - EXPORT_CONVERSION_FUNCTION(CBaseHandle, BaseHandle, unsigned int, Index); - EXPORT_CONVERSION_FUNCTION(CBaseHandle, BaseHandle, edict_t *, Edict); - EXPORT_CONVERSION_FUNCTION(CBaseHandle, BaseHandle, int, IntHandle); - EXPORT_CONVERSION_FUNCTION(CBaseHandle, BaseHandle, CPointer *, Pointer); - EXPORT_CONVERSION_FUNCTION(CBaseHandle, BaseHandle, CBaseEntity *, BaseEntity); + EXPORT_CONVERSION_FUNCTION(CBaseHandle, BaseHandle, unsigned int, Index, object(result)); + EXPORT_CONVERSION_FUNCTION(CBaseHandle, BaseHandle, edict_t *, Edict, object(result)); + EXPORT_CONVERSION_FUNCTION(CBaseHandle, BaseHandle, unsigned int, IntHandle, object(result)); + EXPORT_CONVERSION_FUNCTION(CBaseHandle, BaseHandle, CPointer *, Pointer, object(result)); + EXPORT_CONVERSION_FUNCTION(CBaseHandle, BaseHandle, CBaseEntity *, BaseEntity, object(result)); // To IntHandle conversions... - EXPORT_CONVERSION_FUNCTION(int, IntHandle, unsigned int, Index); - EXPORT_CONVERSION_FUNCTION(int, IntHandle, edict_t *, Edict); - EXPORT_CONVERSION_FUNCTION(int, IntHandle, CBaseHandle, BaseHandle); - EXPORT_CONVERSION_FUNCTION(int, IntHandle, CPointer *, Pointer); - EXPORT_CONVERSION_FUNCTION(int, IntHandle, CBaseEntity *, BaseEntity); + EXPORT_CONVERSION_FUNCTION(unsigned int, IntHandle, unsigned int, Index, object(result)); + EXPORT_CONVERSION_FUNCTION(unsigned int, IntHandle, edict_t *, Edict, object(result)); + EXPORT_CONVERSION_FUNCTION(unsigned int, IntHandle, CBaseHandle, BaseHandle, object(result)); + EXPORT_CONVERSION_FUNCTION(unsigned int, IntHandle, CPointer *, Pointer, object(result)); + EXPORT_CONVERSION_FUNCTION(unsigned int, IntHandle, CBaseEntity *, BaseEntity, object(result)); // To Pointer conversions... - EXPORT_CONVERSION_FUNCTION(CPointer *, Pointer, unsigned int, Index, manage_new_object_policy()); - EXPORT_CONVERSION_FUNCTION(CPointer *, Pointer, edict_t *, Edict, manage_new_object_policy()); - EXPORT_CONVERSION_FUNCTION(CPointer *, Pointer, CBaseHandle, BaseHandle, manage_new_object_policy()); - EXPORT_CONVERSION_FUNCTION(CPointer *, Pointer, int, IntHandle, manage_new_object_policy()); - EXPORT_CONVERSION_FUNCTION(CPointer *, Pointer, CBaseEntity *, BaseEntity, manage_new_object_policy()); + EXPORT_CONVERSION_FUNCTION(CPointer, Pointer, unsigned int, Index, object(result)); + EXPORT_CONVERSION_FUNCTION(CPointer, Pointer, edict_t *, Edict, object(result)); + EXPORT_CONVERSION_FUNCTION(CPointer, Pointer, CBaseHandle, BaseHandle, object(result)); + EXPORT_CONVERSION_FUNCTION(CPointer, Pointer, unsigned int, IntHandle, object(result)); + EXPORT_CONVERSION_FUNCTION(CPointer, Pointer, CBaseEntity *, BaseEntity, object(result)); // To BaseEntity conversions... - EXPORT_CONVERSION_FUNCTION(CBaseEntity *, BaseEntity, unsigned int, Index, return_by_value_policy()); - EXPORT_CONVERSION_FUNCTION(CBaseEntity *, BaseEntity, edict_t *, Edict, return_by_value_policy()); - EXPORT_CONVERSION_FUNCTION(CBaseEntity *, BaseEntity, CBaseHandle, BaseHandle, return_by_value_policy()); - EXPORT_CONVERSION_FUNCTION(CBaseEntity *, BaseEntity, int, IntHandle, return_by_value_policy()); - EXPORT_CONVERSION_FUNCTION(CBaseEntity *, BaseEntity, CPointer *, Pointer, return_by_value_policy()); + EXPORT_CONVERSION_FUNCTION(CBaseEntity *, BaseEntity, unsigned int, Index, object(ptr((CBaseEntityWrapper*) result))); + EXPORT_CONVERSION_FUNCTION(CBaseEntity *, BaseEntity, edict_t *, Edict, object(ptr((CBaseEntityWrapper*) result))); + EXPORT_CONVERSION_FUNCTION(CBaseEntity *, BaseEntity, CBaseHandle, BaseHandle, object(ptr((CBaseEntityWrapper*) result))); + EXPORT_CONVERSION_FUNCTION(CBaseEntity *, BaseEntity, unsigned int, IntHandle, object(ptr((CBaseEntityWrapper*) result))); + EXPORT_CONVERSION_FUNCTION(CBaseEntity *, BaseEntity, CPointer *, Pointer, object(ptr((CBaseEntityWrapper*) result))); } diff --git a/src/core/modules/entities/orangebox/entities_helpers.h b/src/core/modules/entities/orangebox/entities_helpers.h index d240d7e77..c8e057eba 100644 --- a/src/core/modules/entities/orangebox/entities_helpers.h +++ b/src/core/modules/entities/orangebox/entities_helpers.h @@ -45,9 +45,14 @@ extern IServerTools *servertools; //----------------------------------------------------------------------------- // Removes the entity matching the given index. //----------------------------------------------------------------------------- +// TODO: This should accept a BaseEntity object void remove_entity(unsigned int uiEntityIndex) { - servertools->RemoveEntity(BaseEntityFromIndex(uiEntityIndex, true)); + CBaseEntity* pBaseEntity; + if (!BaseEntityFromIndex(uiEntityIndex, pBaseEntity)) + BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseEntity object from the given index: '%i'", uiEntityIndex); + + servertools->RemoveEntity(pBaseEntity); } diff --git a/src/core/modules/filters/filters_recipients.cpp b/src/core/modules/filters/filters_recipients.cpp index eb552bc99..0db2d37cc 100644 --- a/src/core/modules/filters/filters_recipients.cpp +++ b/src/core/modules/filters/filters_recipients.cpp @@ -102,16 +102,15 @@ void MRecipientFilter::AddAllPlayers() for(int i = 1; i <= gpGlobals->maxClients; i++) { - edict_t *pPlayer = EdictFromIndex(i); - - // Skip invalid entities. - if( !pPlayer ) { + // Make sure the player is valid + edict_t* pPlayer; + if(!EdictFromIndex(i, pPlayer)) continue; - } m_Recipients.AddToTail(i); } } + void MRecipientFilter::AddRecipient(int iPlayer) { // Return if the recipient is already in the vector @@ -119,8 +118,8 @@ void MRecipientFilter::AddRecipient(int iPlayer) return; // Make sure the player is valid - edict_t* pPlayer = EdictFromIndex(iPlayer); - if(!pPlayer) + edict_t* pPlayer; + if(!EdictFromIndex(iPlayer, pPlayer)) return; // Skip non-player entities. diff --git a/src/core/modules/players/players_generator.cpp b/src/core/modules/players/players_generator.cpp index c75f6ff22..4d74461ab 100644 --- a/src/core/modules/players/players_generator.cpp +++ b/src/core/modules/players/players_generator.cpp @@ -71,8 +71,7 @@ edict_t *CPlayerGenerator::getNext() while(m_iEntityIndex < gpGlobals->maxClients) { m_iEntityIndex++; - pEdict = EdictFromIndex(m_iEntityIndex); - if (pEdict) + if (!EdictFromIndex(m_iEntityIndex, pEdict)) break; } return pEdict; diff --git a/src/core/modules/players/players_helpers_wrap.cpp b/src/core/modules/players/players_helpers_wrap.cpp index 384037878..6851d7a77 100644 --- a/src/core/modules/players/players_helpers_wrap.cpp +++ b/src/core/modules/players/players_helpers_wrap.cpp @@ -30,6 +30,7 @@ #include "export_main.h" #include "utilities/conversions.h" #include "utilities/wrap_macros.h" +#include "modules/entities/entities_entity.h" BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(CBaseEntity) @@ -56,44 +57,44 @@ DECLARE_SP_SUBMODULE(_players, _helpers) void export_player_conversion_functions(scope _helpers) { // To Index conversions... - EXPORT_CONVERSION_FUNCTION(unsigned int, Index, unsigned int, Userid); - EXPORT_CONVERSION_FUNCTION(unsigned int, Index, IPlayerInfo *, PlayerInfo); + EXPORT_CONVERSION_FUNCTION(unsigned int, Index, unsigned int, Userid, object(result)); + EXPORT_CONVERSION_FUNCTION(unsigned int, Index, IPlayerInfo *, PlayerInfo, object(result)); // To Edict conversions... - EXPORT_CONVERSION_FUNCTION(edict_t *, Edict, unsigned int, Userid, reference_existing_object_policy()); - EXPORT_CONVERSION_FUNCTION(edict_t *, Edict, IPlayerInfo *, PlayerInfo, reference_existing_object_policy()); + EXPORT_CONVERSION_FUNCTION(edict_t *, Edict, unsigned int, Userid, object(ptr(result))); + EXPORT_CONVERSION_FUNCTION(edict_t *, Edict, IPlayerInfo *, PlayerInfo, object(ptr(result))); // To BaseHandle conversions... - EXPORT_CONVERSION_FUNCTION(CBaseHandle, BaseHandle, unsigned int, Userid); - EXPORT_CONVERSION_FUNCTION(CBaseHandle, BaseHandle, IPlayerInfo *, PlayerInfo); + EXPORT_CONVERSION_FUNCTION(CBaseHandle, BaseHandle, unsigned int, Userid, object(result)); + EXPORT_CONVERSION_FUNCTION(CBaseHandle, BaseHandle, IPlayerInfo *, PlayerInfo, object(result)); // To IntHandle conversions... - EXPORT_CONVERSION_FUNCTION(int, IntHandle, unsigned int, Userid); - EXPORT_CONVERSION_FUNCTION(int, IntHandle, IPlayerInfo *, PlayerInfo); + EXPORT_CONVERSION_FUNCTION(unsigned int, IntHandle, unsigned int, Userid, object(result)); + EXPORT_CONVERSION_FUNCTION(unsigned int, IntHandle, IPlayerInfo *, PlayerInfo, object(result)); // To Pointer conversions... - EXPORT_CONVERSION_FUNCTION(CPointer *, Pointer, unsigned int, Userid, manage_new_object_policy()); - EXPORT_CONVERSION_FUNCTION(CPointer *, Pointer, IPlayerInfo *, PlayerInfo, manage_new_object_policy()); + EXPORT_CONVERSION_FUNCTION(CPointer, Pointer, unsigned int, Userid, object(result)); + EXPORT_CONVERSION_FUNCTION(CPointer, Pointer, IPlayerInfo *, PlayerInfo, object(result)); // To UserID conversions... - EXPORT_CONVERSION_FUNCTION(unsigned int, Userid, unsigned int, Index); - EXPORT_CONVERSION_FUNCTION(unsigned int, Userid, edict_t *, Edict); - EXPORT_CONVERSION_FUNCTION(unsigned int, Userid, CBaseHandle, BaseHandle); - EXPORT_CONVERSION_FUNCTION(unsigned int, Userid, int, IntHandle); - EXPORT_CONVERSION_FUNCTION(unsigned int, Userid, CPointer *, Pointer); - EXPORT_CONVERSION_FUNCTION(unsigned int, Userid, IPlayerInfo *, PlayerInfo); - EXPORT_CONVERSION_FUNCTION(unsigned int, Userid, CBaseEntity *, BaseEntity); + EXPORT_CONVERSION_FUNCTION(unsigned int, Userid, unsigned int, Index, object(result)); + EXPORT_CONVERSION_FUNCTION(unsigned int, Userid, edict_t *, Edict, object(result)); + EXPORT_CONVERSION_FUNCTION(unsigned int, Userid, CBaseHandle, BaseHandle, object(result)); + EXPORT_CONVERSION_FUNCTION(unsigned int, Userid, unsigned int, IntHandle, object(result)); + EXPORT_CONVERSION_FUNCTION(unsigned int, Userid, CPointer *, Pointer, object(result)); + EXPORT_CONVERSION_FUNCTION(unsigned int, Userid, IPlayerInfo *, PlayerInfo, object(result)); + EXPORT_CONVERSION_FUNCTION(unsigned int, Userid, CBaseEntity *, BaseEntity, object(result)); // To PlayerInfo conversions... - EXPORT_CONVERSION_FUNCTION(IPlayerInfo *, PlayerInfo, unsigned int, Index, reference_existing_object_policy()); - EXPORT_CONVERSION_FUNCTION(IPlayerInfo *, PlayerInfo, edict_t *, Edict, reference_existing_object_policy()); - EXPORT_CONVERSION_FUNCTION(IPlayerInfo *, PlayerInfo, CBaseHandle, BaseHandle, reference_existing_object_policy()); - EXPORT_CONVERSION_FUNCTION(IPlayerInfo *, PlayerInfo, int, IntHandle, reference_existing_object_policy()); - EXPORT_CONVERSION_FUNCTION(IPlayerInfo *, PlayerInfo, CPointer *, Pointer, reference_existing_object_policy()); - EXPORT_CONVERSION_FUNCTION(IPlayerInfo *, PlayerInfo, unsigned int, Userid, reference_existing_object_policy()); - EXPORT_CONVERSION_FUNCTION(IPlayerInfo *, PlayerInfo, CBaseEntity *, BaseEntity, reference_existing_object_policy()); + EXPORT_CONVERSION_FUNCTION(IPlayerInfo *, PlayerInfo, unsigned int, Index, object(ptr(result))); + EXPORT_CONVERSION_FUNCTION(IPlayerInfo *, PlayerInfo, edict_t *, Edict, object(ptr(result))); + EXPORT_CONVERSION_FUNCTION(IPlayerInfo *, PlayerInfo, CBaseHandle, BaseHandle, object(ptr(result))); + EXPORT_CONVERSION_FUNCTION(IPlayerInfo *, PlayerInfo, unsigned int, IntHandle, object(ptr(result))); + EXPORT_CONVERSION_FUNCTION(IPlayerInfo *, PlayerInfo, CPointer *, Pointer, object(ptr(result))); + EXPORT_CONVERSION_FUNCTION(IPlayerInfo *, PlayerInfo, unsigned int, Userid, object(ptr(result))); + EXPORT_CONVERSION_FUNCTION(IPlayerInfo *, PlayerInfo, CBaseEntity *, BaseEntity, object(ptr(result))); // To BaseEntity conversions... - EXPORT_CONVERSION_FUNCTION(CBaseEntity *, BaseEntity, unsigned int, Userid, return_by_value_policy()); - EXPORT_CONVERSION_FUNCTION(CBaseEntity *, BaseEntity, IPlayerInfo *, PlayerInfo, return_by_value_policy()); + EXPORT_CONVERSION_FUNCTION(CBaseEntity *, BaseEntity, unsigned int, Userid, object(ptr((CBaseEntityWrapper*) result))); + EXPORT_CONVERSION_FUNCTION(CBaseEntity *, BaseEntity, IPlayerInfo *, PlayerInfo, object(ptr((CBaseEntityWrapper*) result))); } diff --git a/src/core/patches/undefined_symbols.cpp b/src/core/patches/undefined_symbols.cpp index d09560917..d1ef0943c 100644 --- a/src/core/patches/undefined_symbols.cpp +++ b/src/core/patches/undefined_symbols.cpp @@ -56,7 +56,11 @@ SendProp::~SendProp() //----------------------------------------------------------------------------- bool CGameTrace::DidHitWorld() const { - return BaseEntityFromIndex(WORLD_ENTITY_INDEX) == m_pEnt; + CBaseEntity* pBaseEntity; + if (!BaseEntityFromIndex(WORLD_ENTITY_INDEX, pBaseEntity)) + return false; + + return pBaseEntity == m_pEnt; } @@ -65,7 +69,11 @@ bool CGameTrace::DidHitWorld() const //----------------------------------------------------------------------------- int CGameTrace::GetEntityIndex() const { - return IndexFromBaseEntity(m_pEnt); + unsigned int iEntityIndex; + if (!IndexFromBaseEntity(m_pEnt, iEntityIndex)) + return INVALID_ENTITY_INDEX; + + return iEntityIndex; } diff --git a/src/core/sp_main.cpp b/src/core/sp_main.cpp index 347fe2627..2491fe2f6 100644 --- a/src/core/sp_main.cpp +++ b/src/core/sp_main.cpp @@ -362,7 +362,11 @@ void CSourcePython::LevelShutdown( void ) // !!!!this can get called multiple ti //----------------------------------------------------------------------------- void CSourcePython::ClientActive( edict_t *pEntity ) { - CALL_LISTENERS(ClientActive, IndexFromEdict(pEntity)); + unsigned int iEntityIndex; + if (!IndexFromEdict(pEntity, iEntityIndex)) + return; + + CALL_LISTENERS(ClientActive, iEntityIndex); } //----------------------------------------------------------------------------- @@ -370,7 +374,11 @@ void CSourcePython::ClientActive( edict_t *pEntity ) //----------------------------------------------------------------------------- void CSourcePython::ClientDisconnect( edict_t *pEntity ) { - CALL_LISTENERS(ClientDisconnect, IndexFromEdict(pEntity)); + unsigned int iEntityIndex; + if (!IndexFromEdict(pEntity, iEntityIndex)) + return; + + CALL_LISTENERS(ClientDisconnect, iEntityIndex); } //----------------------------------------------------------------------------- @@ -378,7 +386,11 @@ void CSourcePython::ClientDisconnect( edict_t *pEntity ) //----------------------------------------------------------------------------- void CSourcePython::ClientPutInServer( edict_t *pEntity, char const *playername ) { - CALL_LISTENERS(ClientPutInServer, IndexFromEdict(pEntity), playername); + unsigned int iEntityIndex; + if (!IndexFromEdict(pEntity, iEntityIndex)) + return; + + CALL_LISTENERS(ClientPutInServer, iEntityIndex, playername); } //----------------------------------------------------------------------------- @@ -394,7 +406,11 @@ void CSourcePython::SetCommandClient( int index ) //----------------------------------------------------------------------------- void CSourcePython::ClientSettingsChanged( edict_t *pEdict ) { - CALL_LISTENERS(ClientSettingsChanged, IndexFromEdict(pEdict)); + unsigned int iEntityIndex; + if (!IndexFromEdict(pEdict, iEntityIndex)) + return; + + CALL_LISTENERS(ClientSettingsChanged, iEntityIndex); } //----------------------------------------------------------------------------- @@ -402,9 +418,13 @@ void CSourcePython::ClientSettingsChanged( edict_t *pEdict ) //----------------------------------------------------------------------------- PLUGIN_RESULT CSourcePython::ClientConnect( bool *bAllowConnect, edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen ) { + unsigned int iEntityIndex; + if (!IndexFromEdict(pEntity, iEntityIndex)) + return PLUGIN_CONTINUE; + CPointer allowConnect = CPointer((unsigned long) bAllowConnect); CPointer rejectMessage = CPointer((unsigned long) reject); - CALL_LISTENERS(ClientConnect, allowConnect, IndexFromEdict(pEntity), pszName, pszAddress, rejectMessage, maxrejectlen); + CALL_LISTENERS(ClientConnect, allowConnect, iEntityIndex, pszName, pszAddress, rejectMessage, maxrejectlen); return PLUGIN_OVERRIDE; } @@ -424,7 +444,11 @@ void CSourcePython::OnQueryCvarValueFinished( QueryCvarCookie_t iCookie, edict_t EQueryCvarValueStatus eStatus, const char *pCvarName, const char *pCvarValue ) { PythonLog(4, "Cvar query (cookie: %d, status: %d) - name: %s, value: %s", iCookie, eStatus, pCvarName, pCvarValue ); - CALL_LISTENERS(OnQueryCvarValueFinished, (int) iCookie, IndexFromEdict(pPlayerEntity), eStatus, pCvarName, pCvarValue); + unsigned int iEntityIndex; + if (!IndexFromEdict(pPlayerEntity, iEntityIndex)) + return; + + CALL_LISTENERS(OnQueryCvarValueFinished, (int) iCookie, iEntityIndex, eStatus, pCvarName, pCvarValue); } //----------------------------------------------------------------------------- @@ -450,14 +474,22 @@ PLUGIN_RESULT CSourcePython::ClientCommand( edict_t *pEntity, const CCommand &ar #ifdef ENGINE_CSGO void CSourcePython::ClientFullyConnect( edict_t *pEntity ) { - CALL_LISTENERS(ClientFullyConnect, IndexFromEdict(pEntity)); + unsigned int iEntityIndex; + if (!IndexFromEdict(pEntity, iEntityIndex)) + return; + + CALL_LISTENERS(ClientFullyConnect, iEntityIndex); } #endif #if defined(ENGINE_CSGO) || defined(ENGINE_BMS) void CSourcePython::OnEdictAllocated( edict_t *edict ) { - CALL_LISTENERS(OnEdictAllocated, IndexFromEdict(edict)); + unsigned int iEntityIndex; + if (!IndexFromEdict(pEntity, iEntityIndex)) + return; + + CALL_LISTENERS(OnEdictAllocated, iEntityIndex); } void CSourcePython::OnEdictFreed( const edict_t *edict ) @@ -475,8 +507,15 @@ void CSourcePython::OnEntityPreSpawned( CBaseEntity *pEntity ) void CSourcePython::OnEntityCreated( CBaseEntity *pEntity ) { - int iIndex = IndexFromBaseEntity(pEntity); - edict_t *pEdict = EdictFromIndex(iIndex); + // TODO: Udpate this. The index should be None instead of -1 + unsigned int iIndex; + if (!IndexFromBaseEntity(pEntity, iIndex)) + iIndex = -1; + + edict_t *pEdict; + if (!EdictFromIndex(iIndex, pEdict)) + pEdict = NULL; + if (pEdict) { IServerUnknown* pServerUnknown = pEdict->GetUnknown(); @@ -488,12 +527,22 @@ void CSourcePython::OnEntityCreated( CBaseEntity *pEntity ) void CSourcePython::OnEntitySpawned( CBaseEntity *pEntity ) { - CALL_LISTENERS(OnEntitySpawned, IndexFromBaseEntity(pEntity), ptr((CBaseEntityWrapper*) pEntity)); + // TODO: Udpate this. The index should be None instead of -1 + unsigned int iEntityIndex; + if (!IndexFromBaseEntity(pEntity, iEntityIndex)) + iEntityIndex = -1; + + CALL_LISTENERS(OnEntitySpawned, iEntityIndex, ptr((CBaseEntityWrapper*) pEntity)); } void CSourcePython::OnEntityDeleted( CBaseEntity *pEntity ) { - CALL_LISTENERS(OnEntityDeleted, IndexFromBaseEntity(pEntity), ptr((CBaseEntityWrapper*) pEntity)); + // TODO: Udpate this. The index should be None instead of -1 + unsigned int iEntityIndex; + if (!IndexFromBaseEntity(pEntity, iEntityIndex)) + iEntityIndex = -1; + + CALL_LISTENERS(OnEntityDeleted, iEntityIndex, ptr((CBaseEntityWrapper*) pEntity)); } void CSourcePython::OnDataLoaded( MDLCacheDataType_t type, MDLHandle_t handle ) diff --git a/src/core/utilities/conversions.h b/src/core/utilities/conversions.h index b57d6b98a..2a5f51503 100644 --- a/src/core/utilities/conversions.h +++ b/src/core/utilities/conversions.h @@ -62,108 +62,118 @@ extern IPlayerInfoManager *playerinfomanager; //----------------------------------------------------------------------------- // EdictFrom* declarations //----------------------------------------------------------------------------- -edict_t* EdictFromIndex( int iEntityIndex, bool bRaiseException = false ); -edict_t* EdictFromUserid( int iUserID, bool bRaiseException = false ); -edict_t* EdictFromPlayerInfo( IPlayerInfo *pPlayerInfo, bool bRaiseException = false ); -edict_t* EdictFromBaseEntity( CBaseEntity *pBaseEntity, bool bRaiseException = false ); -edict_t* EdictFromBaseHandle( CBaseHandle hBaseHandle, bool bRaiseException = false ); -edict_t* EdictFromIntHandle( int iEntityHandle, bool bRaiseException = false ); -edict_t* EdictFromPointer( CPointer *pEntityPointer, bool bRaiseException = false ); +bool EdictFromIndex( unsigned int iEntityIndex, edict_t*& output ); +bool EdictFromUserid( unsigned int iUserID, edict_t*& output ); +bool EdictFromPlayerInfo( IPlayerInfo *pPlayerInfo, edict_t*& output ); +bool EdictFromBaseEntity( CBaseEntity *pBaseEntity, edict_t*& output ); +bool EdictFromBaseHandle( CBaseHandle hBaseHandle, edict_t*& output ); +bool EdictFromIntHandle( unsigned int iEntityHandle, edict_t*& output ); +bool EdictFromPointer( CPointer *pEntityPointer, edict_t*& output ); //----------------------------------------------------------------------------- // IntHandleFrom* declarations //----------------------------------------------------------------------------- -int IntHandleFromBaseHandle( CBaseHandle hBaseHandle, bool bRaiseException = false ); -int IntHandleFromIndex( int iEntityIndex, bool bRaiseException = false ); -int IntHandleFromEdict( edict_t *pEdict, bool bRaiseException = false ); -int IntHandleFromBaseEntity( CBaseEntity *pBaseEntity, bool bRaiseException = false ); -int IntHandleFromPointer( CPointer *pEntityPointer, bool bRaiseException = false ); -int IntHandleFromUserid( int iUserID, bool bRaiseException = false ); -int IntHandleFromPlayerInfo( IPlayerInfo *pPlayerInfo, bool bRaiseException = false ); +bool IntHandleFromBaseHandle( CBaseHandle hBaseHandle, unsigned int& output ); +bool IntHandleFromIndex( unsigned int iEntityIndex, unsigned int& output ); +bool IntHandleFromEdict( edict_t *pEdict, unsigned int& output ); +bool IntHandleFromBaseEntity( CBaseEntity *pBaseEntity, unsigned int& output ); +bool IntHandleFromPointer( CPointer *pEntityPointer, unsigned int& output ); +bool IntHandleFromUserid( unsigned int iUserID, unsigned int& output ); +bool IntHandleFromPlayerInfo( IPlayerInfo *pPlayerInfo, unsigned int& output ); //----------------------------------------------------------------------------- // BaseEntityFrom* declarations //----------------------------------------------------------------------------- -CBaseEntity* BaseEntityFromEdict( edict_t *pEdict, bool bRaiseException = false ); -CBaseEntity* BaseEntityFromPointer( CPointer *pEntityPointer, bool bRaiseException = false ); -CBaseEntity* BaseEntityFromIndex( int iEntityIndex, bool bRaiseException = false ); -CBaseEntity* BaseEntityFromIntHandle( int iEntityHandle, bool bRaiseException = false ); -CBaseEntity* BaseEntityFromBaseHandle( CBaseHandle hBaseHandle, bool bRaiseException = false ); -CBaseEntity* BaseEntityFromUserid( int iUserID, bool bRaiseException = false ); -CBaseEntity* BaseEntityFromPlayerInfo( IPlayerInfo *pPlayerInfo, bool bRaiseException = false ); +bool BaseEntityFromEdict( edict_t *pEdict, CBaseEntity*& output ); +bool BaseEntityFromPointer( CPointer *pEntityPointer, CBaseEntity*& output ); +bool BaseEntityFromIndex( unsigned int iEntityIndex, CBaseEntity*& output ); +bool BaseEntityFromIntHandle( unsigned int iEntityHandle, CBaseEntity*& output ); +bool BaseEntityFromBaseHandle( CBaseHandle hBaseHandle, CBaseEntity*& output ); +bool BaseEntityFromUserid( unsigned int iUserID, CBaseEntity*& output ); +bool BaseEntityFromPlayerInfo( IPlayerInfo *pPlayerInfo, CBaseEntity*& output ); //----------------------------------------------------------------------------- // UseridFrom* declarations //----------------------------------------------------------------------------- -int UseridFromPlayerInfo( IPlayerInfo *pPlayerInfo, bool bRaiseException = false ); -int UseridFromIndex( int iEntityIndex, bool bRaiseException = false ); -int UseridFromEdict( edict_t *pEdict, bool bRaiseException = false ); -int UseridFromBaseHandle( CBaseHandle hBaseHandle, bool bRaiseException = false ); -int UseridFromIntHandle( int iEntityHandle, bool bRaiseException = false ); -int UseridFromBaseEntity( CBaseEntity *pBaseEntity, bool bRaiseException = false ); -int UseridFromPointer( CPointer *pEntityPointer, bool bRaiseException = false ); +bool UseridFromPlayerInfo( IPlayerInfo *pPlayerInfo, unsigned int& output ); +bool UseridFromIndex( unsigned int iEntityIndex, unsigned int& output ); +bool UseridFromEdict( edict_t *pEdict, unsigned int& output ); +bool UseridFromBaseHandle( CBaseHandle hBaseHandle, unsigned int& output ); +bool UseridFromIntHandle( unsigned int iEntityHandle, unsigned int& output ); +bool UseridFromBaseEntity( CBaseEntity *pBaseEntity, unsigned int& output ); +bool UseridFromPointer( CPointer *pEntityPointer, unsigned int& output ); //----------------------------------------------------------------------------- // PlayerInfoFrom* declarations //----------------------------------------------------------------------------- -IPlayerInfo* PlayerInfoFromIndex( int iEntityIndex, bool bRaiseException = false ); -IPlayerInfo* PlayerInfoFromBaseEntity( CBaseEntity *pBaseEntity, bool bRaiseException = false ); -IPlayerInfo* PlayerInfoFromPointer( CPointer *pEntityPointer, bool bRaiseException = false ); -IPlayerInfo* PlayerInfoFromEdict( edict_t *pEdict, bool bRaiseException = false ); -IPlayerInfo* PlayerInfoFromBaseHandle( CBaseHandle hBaseHandle, bool bRaiseException = false ); -IPlayerInfo* PlayerInfoFromIntHandle( int iEntityHandle, bool bRaiseException = false ); -IPlayerInfo* PlayerInfoFromUserid( int iUserID, bool bRaiseException = false ); +bool PlayerInfoFromIndex( unsigned int iEntityIndex, IPlayerInfo*& output ); +bool PlayerInfoFromBaseEntity( CBaseEntity *pBaseEntity, IPlayerInfo*& output ); +bool PlayerInfoFromPointer( CPointer *pEntityPointer, IPlayerInfo*& output ); +bool PlayerInfoFromEdict( edict_t *pEdict, IPlayerInfo*& output ); +bool PlayerInfoFromBaseHandle( CBaseHandle hBaseHandle, IPlayerInfo*& output ); +bool PlayerInfoFromIntHandle( unsigned int iEntityHandle, IPlayerInfo*& output ); +bool PlayerInfoFromUserid( unsigned int iUserID, IPlayerInfo*& output ); //----------------------------------------------------------------------------- // IndexFrom* declarations //----------------------------------------------------------------------------- -int IndexFromEdict( edict_t *pEdict, bool bRaiseException = false ); -int IndexFromBaseEntity( CBaseEntity *pBaseEntity, bool bRaiseException = false ); -int IndexFromPointer( CPointer *pEntityPointer, bool bRaiseException = false ); -int IndexFromBaseHandle( CBaseHandle hBaseHandle, bool bRaiseException = false ); -int IndexFromIntHandle( int iEntityHandle, bool bRaiseException = false ); -int IndexFromUserid( int iUserID, bool bRaiseException = false ); -int IndexFromPlayerInfo( IPlayerInfo *pPlayerInfo, bool bRaiseException = false ); +bool IndexFromEdict( edict_t *pEdict, unsigned int& output ); +bool IndexFromBaseEntity( CBaseEntity *pBaseEntity, unsigned int& output ); +bool IndexFromPointer( CPointer *pEntityPointer, unsigned int& output ); +bool IndexFromBaseHandle( CBaseHandle hBaseHandle, unsigned int& output ); +bool IndexFromIntHandle( unsigned int iEntityHandle, unsigned int& output ); +bool IndexFromUserid( unsigned int iUserID, unsigned int& output ); +bool IndexFromPlayerInfo( IPlayerInfo *pPlayerInfo, unsigned int& output ); //----------------------------------------------------------------------------- // BaseHandleFrom* declarations //----------------------------------------------------------------------------- -CBaseHandle BaseHandleFromIndex( int iEntityIndex, bool bRaiseException = false ); -CBaseHandle BaseHandleFromIntHandle( int iEntityHandle, bool bRaiseException = false ); -CBaseHandle BaseHandleFromBaseEntity( CBaseEntity *pBaseEntity, bool bRaiseException = false); -CBaseHandle BaseHandleFromPointer( CPointer *pEntityPointer, bool bRaiseException = false ); -CBaseHandle BaseHandleFromUserid( int iUserID, bool bRaiseException = false ); -CBaseHandle BaseHandleFromPlayerInfo( IPlayerInfo *pPlayerInfo, bool bRaiseException = false ); -CBaseHandle BaseHandleFromEdict( edict_t *pEdict, bool bRaiseException = false ); +bool BaseHandleFromIndex( unsigned int iEntityIndex, CBaseHandle& output ); +bool BaseHandleFromIntHandle( unsigned int iEntityHandle, CBaseHandle& output ); +bool BaseHandleFromBaseEntity( CBaseEntity *pBaseEntity, CBaseHandle& output ); +bool BaseHandleFromPointer( CPointer *pEntityPointer, CBaseHandle& output ); +bool BaseHandleFromUserid( unsigned int iUserID, CBaseHandle& output ); +bool BaseHandleFromPlayerInfo( IPlayerInfo *pPlayerInfo, CBaseHandle& output ); +bool BaseHandleFromEdict( edict_t *pEdict, CBaseHandle& output ); //----------------------------------------------------------------------------- // PointerFrom* declarations //----------------------------------------------------------------------------- -CPointer* PointerFromIndex( int iEntityIndex, bool bRaiseException = false ); -CPointer* PointerFromBaseEntity( CBaseEntity *pBaseEntity, bool bRaiseException = false ); -CPointer* PointerFromBaseHandle( CBaseHandle hBaseHandle, bool bRaiseException = false ); -CPointer* PointerFromIntHandle( int iEntityHandle, bool bRaiseException = false ); -CPointer* PointerFromUserid( int iUserID, bool bRaiseException = false ); -CPointer* PointerFromPlayerInfo( IPlayerInfo *pPlayerInfo, bool bRaiseException = false ); -CPointer* PointerFromEdict( edict_t *pEdict, bool bRaiseException = false ); +bool PointerFromIndex( unsigned int iEntityIndex, CPointer& output ); +bool PointerFromBaseEntity( CBaseEntity *pBaseEntity, CPointer& output ); +bool PointerFromBaseHandle( CBaseHandle hBaseHandle, CPointer& output ); +bool PointerFromIntHandle( unsigned int iEntityHandle, CPointer& output ); +bool PointerFromUserid( unsigned int iUserID, CPointer& output ); +bool PointerFromPlayerInfo( IPlayerInfo *pPlayerInfo, CPointer& output ); +bool PointerFromEdict( edict_t *pEdict, CPointer& output ); //----------------------------------------------------------------------------- // Helper macro to avoid some redundant typing... //----------------------------------------------------------------------------- -#define EXPORT_CONVERSION_FUNCTION(to_type, to_name, from_type, from_name, ...) \ +#define EXPORT_CONVERSION_FUNCTION(to_type, to_name, from_type, from_name, result_wrapper) \ + class to_name##From##from_name##Wrapper \ + { \ + public: \ + static object wrapper(from_type from) { \ + to_type result; \ + if (!to_name##From##from_name(from, result)) \ + return object();\ + \ + return result_wrapper; \ + } \ + }; \ def(extract(str(XSTRINGIFY(to_name##_from_##from_name)).lower().ptr()), \ - &to_name##From##from_name, \ + &to_name##From##from_name##Wrapper::wrapper, \ XSTRINGIFY(Return the to_name (of type `#to_type`) from the given from_name (of type `#from_type`).), \ - (XSTRINGIFY(from_name), arg("raise_exception")=true), \ - ##__VA_ARGS__ \ + args(XSTRINGIFY(from_name)) \ ) diff --git a/src/core/utilities/conversions/baseentity_from.cpp b/src/core/utilities/conversions/baseentity_from.cpp index a516e84a0..f084b5621 100644 --- a/src/core/utilities/conversions/baseentity_from.cpp +++ b/src/core/utilities/conversions/baseentity_from.cpp @@ -33,106 +33,93 @@ //----------------------------------------------------------------------------- // Returns a BaseEntity instance from the given Edict instance. //----------------------------------------------------------------------------- -CBaseEntity *BaseEntityFromEdict( edict_t *pEdict, bool bRaiseException ) +bool BaseEntityFromEdict( edict_t *pEdict, CBaseEntity*& output ) { - CBaseEntity *pBaseEntity = NULL; + if (!pEdict || pEdict->IsFree()) + return false; - if (pEdict && !pEdict->IsFree()) - { - IServerUnknown *pServerUnknown = pEdict->GetUnknown(); - if (pServerUnknown) - pBaseEntity = pServerUnknown->GetBaseEntity(); - } + IServerUnknown *pServerUnknown = pEdict->GetUnknown(); + if (!pServerUnknown) + return false; - if (!pBaseEntity && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseEntity instance from the given Edict instance (%x).", pEdict); - - return pBaseEntity; + output = pServerUnknown->GetBaseEntity(); + return true; } //----------------------------------------------------------------------------- // Returns a BaseEntity instance from the given Pointer instance. //----------------------------------------------------------------------------- -CBaseEntity *BaseEntityFromPointer( CPointer *pEntityPointer, bool bRaiseException ) +bool BaseEntityFromPointer( CPointer *pEntityPointer, CBaseEntity*& output ) { - CBaseEntity *pBaseEntity = NULL; - - if (IndexFromPointer(pEntityPointer) != INVALID_ENTITY_INDEX) - pBaseEntity = (CBaseEntity *)pEntityPointer->m_ulAddr; + if (!pEntityPointer || !pEntityPointer->IsValid()) + return false; - if (!pBaseEntity && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseEntity instance from the given Pointer instance (%x).", pEntityPointer->m_ulAddr); - - return pBaseEntity; + output = (CBaseEntity *) pEntityPointer->m_ulAddr; + return true; } //----------------------------------------------------------------------------- // Returns a BaseEntity instance from the given index. //----------------------------------------------------------------------------- -CBaseEntity *BaseEntityFromIndex( int iEntityIndex, bool bRaiseException ) +bool BaseEntityFromIndex( unsigned int iEntityIndex, CBaseEntity*& output ) { - CBaseEntity *pBaseEntity = BaseEntityFromEdict(EdictFromIndex(iEntityIndex)); - - if (!pBaseEntity && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseEntity instance from the given index (%i).", iEntityIndex); + edict_t* pEdict; + if (!EdictFromIndex(iEntityIndex, pEdict)) + return false; - return pBaseEntity; + return BaseEntityFromEdict(pEdict, output); } //----------------------------------------------------------------------------- // Returns a BaseEntity instance from the given BaseHandle instance. //----------------------------------------------------------------------------- -CBaseEntity *BaseEntityFromBaseHandle( CBaseHandle hBaseHandle, bool bRaiseException ) +bool BaseEntityFromBaseHandle( CBaseHandle hBaseHandle, CBaseEntity*& output ) { - CBaseEntity *pBaseEntity = BaseEntityFromEdict(EdictFromBaseHandle(hBaseHandle)); + edict_t* pEdict; + if (!EdictFromBaseHandle(hBaseHandle, pEdict)) + return false; - if (!pBaseEntity && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseEntity instance from the given BaseHandle instance (%i).", hBaseHandle.ToInt()); - - return pBaseEntity; + return BaseEntityFromEdict(pEdict, output); } //----------------------------------------------------------------------------- // Returns a BaseEntity instance from the given IntHandle. //----------------------------------------------------------------------------- -CBaseEntity *BaseEntityFromIntHandle( int iEntityHandle, bool bRaiseException ) +bool BaseEntityFromIntHandle( unsigned int iEntityHandle, CBaseEntity*& output ) { - CBaseEntity *pBaseEntity = BaseEntityFromEdict(EdictFromIntHandle(iEntityHandle)); - - if (!pBaseEntity && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseEntity instance from the given IntHandle (%i).", iEntityHandle); + edict_t* pEdict; + if (!EdictFromIntHandle(iEntityHandle, pEdict)) + return false; - return pBaseEntity; + return BaseEntityFromEdict(pEdict, output); } //----------------------------------------------------------------------------- // Returns a BaseEntity instance from the given UserID. //----------------------------------------------------------------------------- -CBaseEntity *BaseEntityFromUserid( int iUserID, bool bRaiseException ) +bool BaseEntityFromUserid( unsigned int iUserID, CBaseEntity*& output ) { - CBaseEntity *pBaseEntity = BaseEntityFromEdict(EdictFromUserid(iUserID)); + edict_t* pEdict; + if (!EdictFromUserid(iUserID, pEdict)) + return false; - if (!pBaseEntity && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseEntity instance from the given UserID (%i).", iUserID); - - return pBaseEntity; + return BaseEntityFromEdict(pEdict, output); } //----------------------------------------------------------------------------- // Returns a BaseEntity instance from the given PlayerInfo instance. //----------------------------------------------------------------------------- -CBaseEntity *BaseEntityFromPlayerInfo( IPlayerInfo *pPlayerInfo, bool bRaiseException ) +bool BaseEntityFromPlayerInfo( IPlayerInfo *pPlayerInfo, CBaseEntity*& output ) { - CBaseEntity *pBaseEntity = BaseEntityFromEdict(EdictFromPlayerInfo(pPlayerInfo)); - - if (!pBaseEntity && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseEntity instance from the given PlayerInfo instance (%x).", pPlayerInfo); + edict_t* pEdict; + if (!EdictFromPlayerInfo(pPlayerInfo, pEdict)) + return false; - return pBaseEntity; + return BaseEntityFromEdict(pEdict, output); } diff --git a/src/core/utilities/conversions/basehandle_from.cpp b/src/core/utilities/conversions/basehandle_from.cpp index 961481845..591beda1f 100644 --- a/src/core/utilities/conversions/basehandle_from.cpp +++ b/src/core/utilities/conversions/basehandle_from.cpp @@ -33,102 +33,107 @@ //----------------------------------------------------------------------------- // Returns a BaseHandle instance from the given Edict instance. //----------------------------------------------------------------------------- -CBaseHandle BaseHandleFromEdict( edict_t *pEdict, bool bRaiseException ) +bool BaseHandleFromEdict( edict_t *pEdict, CBaseHandle& output ) { - CBaseHandle hBaseHandle = NULL; + if (!pEdict || pEdict->IsFree()) + return false; - if (pEdict && !pEdict->IsFree()) - { - IServerNetworkable *pServerNetworkable = pEdict->GetNetworkable(); - if (pServerNetworkable) - hBaseHandle = pServerNetworkable->GetEntityHandle(); - } + IServerNetworkable *pServerNetworkable = pEdict->GetNetworkable(); + if (!pServerNetworkable) + return false; - if (!hBaseHandle.ToInt() && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseHandle instance from the given Edict instance (%x).", pEdict); + IHandleEntity* pHandleEntity = pServerNetworkable->GetEntityHandle(); + if (!pHandleEntity) + return false; - return hBaseHandle; + CBaseHandle hBaseHandle = pHandleEntity->GetRefEHandle(); + if (!hBaseHandle.ToInt()) + return false; + + output = hBaseHandle; + return true; } //----------------------------------------------------------------------------- // Returns a BaseHandle instance from the given index. //----------------------------------------------------------------------------- -CBaseHandle BaseHandleFromIndex( int iEntityIndex, bool bRaiseException ) +bool BaseHandleFromIndex( unsigned int iEntityIndex, CBaseHandle& output ) { - CBaseHandle hBaseHandle = BaseHandleFromEdict(EdictFromIndex(iEntityIndex)); - - if (!hBaseHandle.ToInt() && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseHandle instance from the given index (%i).", iEntityIndex); + edict_t* pEdict; + if (!EdictFromIndex(iEntityIndex, pEdict)) + return false; - return hBaseHandle; + return BaseHandleFromEdict(pEdict, output); } //----------------------------------------------------------------------------- // Returns a BaseHandle instance from the given IntHandle. //----------------------------------------------------------------------------- -CBaseHandle BaseHandleFromIntHandle( int iEntityHandle, bool bRaiseException ) +bool BaseHandleFromIntHandle( unsigned int iEntityHandle, CBaseHandle& output ) { CBaseHandle hBaseHandle = CBaseHandle(iEntityHandle); + unsigned int iIndex; + if (!IndexFromBaseHandle(hBaseHandle, iIndex)) + return false; - if (IndexFromBaseHandle(hBaseHandle) == INVALID_ENTITY_INDEX && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseHandle instance from the given IntHandle (%i).", iEntityHandle); - - return hBaseHandle; + output = hBaseHandle; + return true; } //----------------------------------------------------------------------------- // Returns a BaseHandle instance from the given BaseEntity instance. //----------------------------------------------------------------------------- -CBaseHandle BaseHandleFromBaseEntity( CBaseEntity *pBaseEntity, bool bRaiseException ) +bool BaseHandleFromBaseEntity( CBaseEntity *pBaseEntity, CBaseHandle& output ) { + if (!pBaseEntity) + return false; + CBaseHandle hBaseHandle = pBaseEntity->GetRefEHandle(); - if (!hBaseHandle.ToInt() && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseHandle instance from the given BaseEntity instance (%x).", pBaseEntity); + if (!hBaseHandle.ToInt()) + return false; - return hBaseHandle; + output = hBaseHandle; + return true; } //----------------------------------------------------------------------------- // Returns a BaseHandle instance from the given Pointer instance. //----------------------------------------------------------------------------- -CBaseHandle BaseHandleFromPointer( CPointer *pEntityPointer, bool bRaiseException ) +bool BaseHandleFromPointer( CPointer *pEntityPointer, CBaseHandle& output ) { - CBaseHandle hBaseHandle = BaseHandleFromBaseEntity(BaseEntityFromPointer(pEntityPointer)); + CBaseEntity* pBaseEntity; + if (!BaseEntityFromPointer(pEntityPointer, pBaseEntity)) + return false; - if (!hBaseHandle.ToInt() && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseHandle instance from the given Pointer instance (%x).", pEntityPointer->m_ulAddr); - - return hBaseHandle; + return BaseHandleFromBaseEntity(pBaseEntity, output); } //----------------------------------------------------------------------------- // Returns a BaseHandle instance from the given UserID. //----------------------------------------------------------------------------- -CBaseHandle BaseHandleFromUserid( int iUserID, bool bRaiseException ) +bool BaseHandleFromUserid( unsigned int iUserID, CBaseHandle& output ) { - CBaseHandle hBaseHandle = BaseHandleFromEdict(EdictFromUserid(iUserID)); - - if (!hBaseHandle.ToInt() && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseHandle instance from the given UserID (%i).", iUserID); + edict_t* pEdict; + if (!EdictFromUserid(iUserID, pEdict)) + return false; - return hBaseHandle; + return BaseHandleFromEdict(pEdict, output); } //----------------------------------------------------------------------------- // Returns a BaseHandle instance from the given PlayerInfo instance. //----------------------------------------------------------------------------- -CBaseHandle BaseHandleFromPlayerInfo( IPlayerInfo *pPlayerInfo, bool bRaiseException ) +bool BaseHandleFromPlayerInfo( IPlayerInfo *pPlayerInfo, CBaseHandle& output ) { - CBaseHandle hBaseHandle = BaseHandleFromEdict(EdictFromPlayerInfo(pPlayerInfo)); - - if (!hBaseHandle.ToInt() && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseHandle instance from the given PlayerInfo instance (%x).", pPlayerInfo); + edict_t* pEdict; + if (!EdictFromPlayerInfo(pPlayerInfo, pEdict)) + return false; - return hBaseHandle; + return BaseHandleFromEdict(pEdict, output); } diff --git a/src/core/utilities/conversions/edict_from.cpp b/src/core/utilities/conversions/edict_from.cpp index 11cd230fb..65a1f38e7 100644 --- a/src/core/utilities/conversions/edict_from.cpp +++ b/src/core/utilities/conversions/edict_from.cpp @@ -33,135 +33,111 @@ //----------------------------------------------------------------------------- // Returns an Edict instance from the given index. //----------------------------------------------------------------------------- -edict_t *EdictFromIndex( int iEntityIndex, bool bRaiseException ) +bool EdictFromIndex( unsigned int iEntityIndex, edict_t*& output ) { - edict_t *pEdict = NULL; + if (iEntityIndex >= (unsigned int) gpGlobals->maxEntities) + return false; - if (iEntityIndex > INVALID_ENTITY_INDEX && iEntityIndex < gpGlobals->maxEntities) - { - edict_t *pTempEdict; + edict_t* pEdict; #if defined(ENGINE_ORANGEBOX) || defined(ENGINE_BMS) - pTempEdict = engine->PEntityOfEntIndex(iEntityIndex); + pEdict = engine->PEntityOfEntIndex(iEntityIndex); #else - pTempEdict = (edict_t *)(gpGlobals->pEdicts + iEntityIndex); + pEdict = (edict_t *)(gpGlobals->pEdicts + iEntityIndex); #endif - if (pTempEdict && !pTempEdict->IsFree() && pTempEdict->GetUnknown()) - pEdict = pTempEdict; - } - - if (!pEdict && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an Edict instance from the given index (%i).", iEntityIndex); + if (!pEdict || pEdict->IsFree() || !pEdict->GetUnknown()) + return false; - return pEdict; + output = pEdict; + return true; } //----------------------------------------------------------------------------- // Returns an Edict instance from the given UserID. //----------------------------------------------------------------------------- -edict_t *EdictFromUserid( int iUserID, bool bRaiseException ) +bool EdictFromUserid( unsigned int iUserID, edict_t*& output ) { - edict_t *pEdict = NULL; - - if (iUserID > INVALID_PLAYER_USERID) + for (int iCurrentIndex = 1; iCurrentIndex <= gpGlobals->maxClients; iCurrentIndex++) { - for (int iCurrentIndex = 1; iCurrentIndex <= gpGlobals->maxClients; iCurrentIndex++) - { - edict_t *pCurrentEdict = EdictFromIndex(iCurrentIndex); - if (engine->GetPlayerUserId(pCurrentEdict) == iUserID) - pEdict = pCurrentEdict; + edict_t* pEdict; + if (!EdictFromIndex(iCurrentIndex, pEdict)) + continue; + + if (engine->GetPlayerUserId(pEdict) == iUserID) { + output = pEdict; + return true; } } - - if (!pEdict && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an Edict instance from the given UserID (%i).", iUserID); - - return pEdict; + return false; } //----------------------------------------------------------------------------- // Returns an Edict instance from the given PlayerInfo instance. //----------------------------------------------------------------------------- -edict_t *EdictFromPlayerInfo( IPlayerInfo *pPlayerInfo, bool bRaiseException ) +bool EdictFromPlayerInfo( IPlayerInfo *pPlayerInfo, edict_t*& output ) { - edict_t *pEdict = NULL; - - if (pPlayerInfo) - pEdict = EdictFromUserid(pPlayerInfo->GetUserID()); + if (!pPlayerInfo) + return false; - if (!pEdict && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an Edict instance from the given PlayerInfo instance (%x).", pPlayerInfo); - - return pEdict; + return EdictFromUserid(pPlayerInfo->GetUserID(), output); } //----------------------------------------------------------------------------- // Returns an Edict instance from the given BaseEntity instance. //----------------------------------------------------------------------------- -edict_t *EdictFromBaseEntity( CBaseEntity *pBaseEntity, bool bRaiseException ) +bool EdictFromBaseEntity( CBaseEntity *pBaseEntity, edict_t*& output ) { - edict_t *pEdict = NULL; - - if (pBaseEntity) - { - IServerUnknown *pServerUnknown = (IServerUnknown *)pBaseEntity; - if (pServerUnknown) - { - IServerNetworkable *pServerNetworkable = pServerUnknown->GetNetworkable(); - if (pServerNetworkable) - pEdict = pServerNetworkable->GetEdict(); - } - } - - if (!pEdict && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an Edict instance from the given BaseEntity instance (%x).", pBaseEntity); - - return pEdict; + if (!pBaseEntity) + return false; + + IServerNetworkable *pServerNetworkable = pBaseEntity->GetNetworkable(); + if (!pServerNetworkable) + return false; + + edict_t* pEdict = pServerNetworkable->GetEdict(); + if (!pEdict || pEdict->IsFree()) + return false; + + output = pEdict; + return true; } //----------------------------------------------------------------------------- // Returns an Edict instance from the given BaseHandle instance. //----------------------------------------------------------------------------- -edict_t *EdictFromBaseHandle( CBaseHandle hBaseHandle, bool bRaiseException ) +bool EdictFromBaseHandle( CBaseHandle hBaseHandle, edict_t*& output ) { - edict_t *pEdict = EdictFromIndex(IndexFromBaseHandle(hBaseHandle)); + unsigned int iIndex; + if (!IndexFromBaseHandle(hBaseHandle, iIndex)) + return false; - if (!pEdict && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an Edict instance from the given BaseHandle instance (%i).", hBaseHandle.ToInt()); - - return pEdict; + return EdictFromIndex(iIndex, output); } //----------------------------------------------------------------------------- // Returns an Edict instance from the given IntHandle. //----------------------------------------------------------------------------- -edict_t *EdictFromIntHandle( int iEntityHandle, bool bRaiseException ) +bool EdictFromIntHandle( unsigned int iEntityHandle, edict_t*& output ) { - edict_t *pEdict = EdictFromIndex(IndexFromIntHandle(iEntityHandle)); - - if (!pEdict && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an Edict instance from the given IntHandle (%i).", iEntityHandle); + unsigned int iIndex; + if (!IndexFromIntHandle(iEntityHandle, iIndex)) + return false; - return pEdict; + return EdictFromIndex(iIndex, output); } //----------------------------------------------------------------------------- // Returns an Edict instance from the given Pointer instance. //----------------------------------------------------------------------------- -edict_t *EdictFromPointer( CPointer *pEntityPointer, bool bRaiseException ) +bool EdictFromPointer( CPointer *pEntityPointer, edict_t*& output ) { - edict_t *pEdict = NULL; - - if (pEntityPointer && pEntityPointer->IsValid()) - pEdict = EdictFromBaseEntity((CBaseEntity *)pEntityPointer->m_ulAddr); - - if (!pEdict && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an Edict instance from the given Pointer instance (%x).", pEntityPointer->m_ulAddr); + if (!pEntityPointer || !pEntityPointer->IsValid()) + return false; - return pEdict; + return EdictFromBaseEntity((CBaseEntity *)pEntityPointer->m_ulAddr, output); } diff --git a/src/core/utilities/conversions/index_from.cpp b/src/core/utilities/conversions/index_from.cpp index a428f3f51..7a8b663e7 100644 --- a/src/core/utilities/conversions/index_from.cpp +++ b/src/core/utilities/conversions/index_from.cpp @@ -33,128 +33,125 @@ //----------------------------------------------------------------------------- // Returns an index from the given Edict instance. //----------------------------------------------------------------------------- -int IndexFromEdict( edict_t *pEdict, bool bRaiseException ) +bool IndexFromEdict( edict_t *pEdict, unsigned int& output ) { - int iEntityIndex = INVALID_ENTITY_INDEX; + if (!pEdict || pEdict->IsFree()) + return false; - if (pEdict && !pEdict->IsFree()) + int iEntityIndex; #if defined(ENGINE_ORANGEBOX) || defined(ENGINE_BMS) - iEntityIndex = engine->IndexOfEdict(pEdict); + iEntityIndex = engine->IndexOfEdict(pEdict); #else - iEntityIndex = pEdict - gpGlobals->pEdicts; + iEntityIndex = pEdict - gpGlobals->pEdicts; #endif - if (iEntityIndex == INVALID_ENTITY_INDEX && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an index from the given Edict instance (%x).", pEdict); + if (iEntityIndex == INVALID_ENTITY_INDEX) + return false; - return iEntityIndex; + output = iEntityIndex; + return true; } //----------------------------------------------------------------------------- // Returns an index from the given BaseEntity instance. //----------------------------------------------------------------------------- -int IndexFromBaseEntity( CBaseEntity *pBaseEntity, bool bRaiseException ) +bool IndexFromBaseEntity( CBaseEntity *pBaseEntity, unsigned int& output ) { - int iEntityIndex = INVALID_ENTITY_INDEX; - - if (pBaseEntity) - { - IServerUnknown *pServerUnknown = (IServerUnknown *)pBaseEntity; - if (pServerUnknown) - { - IServerNetworkable *pServerNetworkable = pServerUnknown->GetNetworkable(); - if (pServerNetworkable) - iEntityIndex = IndexFromEdict(pServerNetworkable->GetEdict()); - } - } - - if (iEntityIndex == INVALID_ENTITY_INDEX && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an index from the given BaseEntity instance (%x).", pBaseEntity); - - return iEntityIndex; + if (!pBaseEntity) + return false; + + IServerNetworkable *pServerNetworkable = pBaseEntity->GetNetworkable(); + if (!pServerNetworkable) + return false; + + edict_t* pEdict = pServerNetworkable->GetEdict(); + if (!pEdict || pEdict->IsFree()) + return false; + + return IndexFromEdict(pEdict, output); } //----------------------------------------------------------------------------- // Returns an index from the given Pointer instance. //----------------------------------------------------------------------------- -int IndexFromPointer( CPointer *pEntityPointer, bool bRaiseException ) +bool IndexFromPointer( CPointer *pEntityPointer, unsigned int& output ) { - int iEntityIndex = INVALID_ENTITY_INDEX; - - if (pEntityPointer && pEntityPointer->IsValid()) - iEntityIndex = IndexFromBaseEntity((CBaseEntity *)pEntityPointer->m_ulAddr); - - if (iEntityIndex == INVALID_ENTITY_INDEX && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an index from the given Pointer instance (%x).", pEntityPointer->m_ulAddr); + if (!pEntityPointer || !pEntityPointer->IsValid()) + return false; - return iEntityIndex; + return IndexFromBaseEntity((CBaseEntity *)pEntityPointer->m_ulAddr, output); } //----------------------------------------------------------------------------- // Returns an index from the given BaseHandle instance. //----------------------------------------------------------------------------- -int IndexFromBaseHandle( CBaseHandle hBaseHandle, bool bRaiseException ) +bool IndexFromBaseHandle( CBaseHandle hBaseHandle, unsigned int& output ) { - int iEntityIndex = INVALID_ENTITY_INDEX; + if (!hBaseHandle.IsValid()) + return false; - if (hBaseHandle.IsValid()) - iEntityIndex = hBaseHandle.GetEntryIndex(); + int iEntityIndex = hBaseHandle.GetEntryIndex(); + if (iEntityIndex == INVALID_ENTITY_INDEX) + return false; - if (iEntityIndex == INVALID_ENTITY_INDEX && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an index from the given BaseHandle instance (%i).", hBaseHandle.ToInt()); - - return iEntityIndex; + output = iEntityIndex; + return true; } //----------------------------------------------------------------------------- // Returns an index from the given IntHandle. //----------------------------------------------------------------------------- -int IndexFromIntHandle( int iEntityHandle, bool bRaiseException ) +bool IndexFromIntHandle( unsigned int iEntityHandle, unsigned int& output ) { - int iEntityIndex = (int) INVALID_ENTITY_INDEX; + if (iEntityHandle == (int) INVALID_EHANDLE_INDEX) + return false; + + CBaseHandle hBaseHandle(iEntityHandle); + unsigned int iEntityIndex; + if (!IndexFromBaseHandle(hBaseHandle, iEntityIndex)) + return false; + + edict_t* pEdict; + if (!EdictFromIndex(iEntityIndex, pEdict)) + return false; - if (iEntityHandle != (int) INVALID_EHANDLE_INDEX) - { - CBaseHandle hBaseHandle(iEntityHandle); - const CBaseHandle hTestHandle = BaseHandleFromEdict(EdictFromIndex(hBaseHandle.GetEntryIndex())); - if (hTestHandle.IsValid() || (hBaseHandle.GetSerialNumber() == hTestHandle.GetSerialNumber())) - iEntityIndex = hBaseHandle.GetEntryIndex(); - } + CBaseHandle hTestHandle; + if (!BaseHandleFromEdict(pEdict, hTestHandle)) + return false; - if (iEntityIndex == INVALID_ENTITY_INDEX && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an index from the given IntHandle (%i).", iEntityHandle); + if (!hTestHandle.IsValid() || hBaseHandle.GetSerialNumber() != hTestHandle.GetSerialNumber()) + return false; - return iEntityIndex; + output = iEntityIndex; + return true; } //----------------------------------------------------------------------------- // Returns an index from the given UserID. //----------------------------------------------------------------------------- -int IndexFromUserid( int iUserID, bool bRaiseException ) +bool IndexFromUserid( unsigned int iUserID, unsigned int& output ) { - int iEntityIndex = IndexFromEdict(EdictFromUserid(iUserID)); + edict_t* pEdict; + if (!EdictFromUserid(iUserID, pEdict)) + return false; - if (iEntityIndex == INVALID_ENTITY_INDEX && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an index from the given UserID (%i).", iUserID); - - return iEntityIndex; + return IndexFromEdict(pEdict, output); } //----------------------------------------------------------------------------- // Returns an index instance from the given PlayerInfo instance. //----------------------------------------------------------------------------- -int IndexFromPlayerInfo( IPlayerInfo *pPlayerInfo, bool bRaiseException ) +bool IndexFromPlayerInfo( IPlayerInfo *pPlayerInfo, unsigned int& output ) { - int iEntityIndex = IndexFromEdict(EdictFromPlayerInfo(pPlayerInfo)); - - if (iEntityIndex == INVALID_ENTITY_INDEX && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an index from the given PlayerInfo instance (%x).", pPlayerInfo); + edict_t* pEdict; + if (!EdictFromPlayerInfo(pPlayerInfo, pEdict)) + return false; - return iEntityIndex; + return IndexFromEdict(pEdict, output); } diff --git a/src/core/utilities/conversions/inthandle_from.cpp b/src/core/utilities/conversions/inthandle_from.cpp index e27469f50..5ef72ebb3 100644 --- a/src/core/utilities/conversions/inthandle_from.cpp +++ b/src/core/utilities/conversions/inthandle_from.cpp @@ -33,99 +33,93 @@ //----------------------------------------------------------------------------- // Returns an IntHandle from the given BaseHandle instance. //----------------------------------------------------------------------------- -int IntHandleFromBaseHandle( CBaseHandle hBaseHandle, bool bRaiseException ) +bool IntHandleFromBaseHandle( CBaseHandle hBaseHandle, unsigned int& output ) { - int iEntityHandle = (int) INVALID_EHANDLE_INDEX; + if (!hBaseHandle.IsValid()) + return false; - if (hBaseHandle.IsValid()) - iEntityHandle = hBaseHandle.ToInt(); + unsigned int iEntityHandle = hBaseHandle.ToInt(); + if (iEntityHandle == INVALID_EHANDLE_INDEX) + return false; - if (iEntityHandle == (int) INVALID_EHANDLE_INDEX && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an IntHandle from the given BaseHandle instance (%i).", hBaseHandle.ToInt()); - - return iEntityHandle; + output = iEntityHandle; + return true; } //----------------------------------------------------------------------------- // Returns an IntHandle from the given index. //----------------------------------------------------------------------------- -int IntHandleFromIndex( int iEntityIndex, bool bRaiseException ) +bool IntHandleFromIndex( unsigned int iEntityIndex, unsigned int& output ) { - int iEntityHandle = IntHandleFromBaseHandle(BaseHandleFromIndex(iEntityIndex)); - - if (iEntityHandle == (int) INVALID_EHANDLE_INDEX && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an IntHandle from the given index (%i).", iEntityIndex); + CBaseHandle hBaseHandle; + if (!BaseHandleFromIndex(iEntityIndex, hBaseHandle)) + return false; - return iEntityHandle; + return IntHandleFromBaseHandle(hBaseHandle, output); } //----------------------------------------------------------------------------- // Returns an IntHandle from the given Edict instance. //----------------------------------------------------------------------------- -int IntHandleFromEdict( edict_t *pEdict, bool bRaiseException ) +bool IntHandleFromEdict( edict_t *pEdict, unsigned int& output ) { - int iEntityHandle = IntHandleFromBaseHandle(BaseHandleFromEdict(pEdict)); + CBaseHandle hBaseHandle; + if (!BaseHandleFromEdict(pEdict, hBaseHandle)) + return false; - if (iEntityHandle == (int) INVALID_EHANDLE_INDEX && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an IntHandle from the given Edict instance (%x).", pEdict); - - return iEntityHandle; + return IntHandleFromBaseHandle(hBaseHandle, output); } //----------------------------------------------------------------------------- // Returns an IntHandle from the given BaseEntity instance. //----------------------------------------------------------------------------- -int IntHandleFromBaseEntity( CBaseEntity *pBaseEntity, bool bRaiseException ) +bool IntHandleFromBaseEntity( CBaseEntity *pBaseEntity, unsigned int& output ) { - int iEntityHandle = IntHandleFromBaseHandle(BaseHandleFromBaseEntity(pBaseEntity)); - - if (iEntityHandle == (int) INVALID_EHANDLE_INDEX && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an IntHandle from the given BaseEntity instance (%x).", pBaseEntity); + CBaseHandle hBaseHandle; + if (!BaseHandleFromBaseEntity(pBaseEntity, hBaseHandle)) + return false; - return iEntityHandle; + return IntHandleFromBaseHandle(hBaseHandle, output); } //----------------------------------------------------------------------------- // Returns an IntHandle from the given Pointer instance. //----------------------------------------------------------------------------- -int IntHandleFromPointer( CPointer *pEntityPointer, bool bRaiseException ) +bool IntHandleFromPointer( CPointer *pEntityPointer, unsigned int& output ) { - int iEntityHandle = IntHandleFromBaseEntity(BaseEntityFromPointer(pEntityPointer)); + CBaseEntity* pBaseEntity; + if (!BaseEntityFromPointer(pEntityPointer, pBaseEntity)) + return false; - if (iEntityHandle == (int) INVALID_EHANDLE_INDEX && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an IntHandle from the given Pointer instance (%x).", pEntityPointer->m_ulAddr); - - return iEntityHandle; + return IntHandleFromBaseEntity(pBaseEntity, output); } //----------------------------------------------------------------------------- // Returns an IntHandle from the given UserID. //----------------------------------------------------------------------------- -int IntHandleFromUserid( int iUserID, bool bRaiseException ) +bool IntHandleFromUserid( unsigned int iUserID, unsigned int& output ) { - int iEntityHandle = IntHandleFromEdict(EdictFromUserid(iUserID)); - - if (iEntityHandle == (int) INVALID_EHANDLE_INDEX && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an IntHandle from the given UserID (%i).", iUserID); + edict_t* pEdict; + if (!EdictFromUserid(iUserID, pEdict)) + return false; - return iEntityHandle; + return IntHandleFromEdict(pEdict, output); } //----------------------------------------------------------------------------- // Returns an IntHandle from the given PlayerInfo instance. //----------------------------------------------------------------------------- -int IntHandleFromPlayerInfo( IPlayerInfo *pPlayerInfo, bool bRaiseException ) +bool IntHandleFromPlayerInfo( IPlayerInfo *pPlayerInfo, unsigned int& output ) { - int iEntityHandle = IntHandleFromEdict(EdictFromPlayerInfo(pPlayerInfo)); - - if (iEntityHandle == (int) INVALID_EHANDLE_INDEX && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an IntHandle from the given PlayerInfo instance (%x).", pPlayerInfo); + edict_t* pEdict; + if (!EdictFromPlayerInfo(pPlayerInfo, pEdict)) + return false; - return iEntityHandle; + return IntHandleFromEdict(pEdict, output); } diff --git a/src/core/utilities/conversions/playerinfo_from.cpp b/src/core/utilities/conversions/playerinfo_from.cpp index 9ae352ec4..cc9e548e2 100644 --- a/src/core/utilities/conversions/playerinfo_from.cpp +++ b/src/core/utilities/conversions/playerinfo_from.cpp @@ -33,103 +33,96 @@ //----------------------------------------------------------------------------- // Returns a PlayerInfo instance from the given index. //----------------------------------------------------------------------------- -IPlayerInfo *PlayerInfoFromIndex( int iEntityIndex, bool bRaiseException ) +bool PlayerInfoFromIndex( unsigned int iEntityIndex, IPlayerInfo*& output) { - IPlayerInfo *pPlayerInfo = NULL; + if (iEntityIndex == WORLD_ENTITY_INDEX || iEntityIndex > (unsigned int) gpGlobals->maxClients) + return false; - if (iEntityIndex > WORLD_ENTITY_INDEX && iEntityIndex <= gpGlobals->maxClients) - { - edict_t *pEdict = EdictFromIndex(iEntityIndex); - if (pEdict) - pPlayerInfo = playerinfomanager->GetPlayerInfo(pEdict); - } + edict_t *pEdict; + if (!EdictFromIndex(iEntityIndex, pEdict)) + return false; - if (!pPlayerInfo && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a PlayerInfo instance from the given index (%i).", iEntityIndex); - - return pPlayerInfo; + return PlayerInfoFromEdict(pEdict, output); } //----------------------------------------------------------------------------- // Returns a PlayerInfo instance from the given BaseEntity instance. //----------------------------------------------------------------------------- -IPlayerInfo *PlayerInfoFromBaseEntity( CBaseEntity *pBaseEntity, bool bRaiseException ) +bool PlayerInfoFromBaseEntity( CBaseEntity *pBaseEntity, IPlayerInfo*& output) { - IPlayerInfo *pPlayerInfo = PlayerInfoFromIndex(IndexFromBaseEntity(pBaseEntity)); - - if (!pPlayerInfo && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a PlayerInfo instance from the given BaseEntity instance (%x).", pBaseEntity); + unsigned int iEntityIndex; + if (!IndexFromBaseEntity(pBaseEntity, iEntityIndex)) + return false; - return pPlayerInfo; + return PlayerInfoFromIndex(iEntityIndex, output); } //----------------------------------------------------------------------------- // Returns a PlayerInfo instance from the given Pointer instance. //----------------------------------------------------------------------------- -IPlayerInfo *PlayerInfoFromPointer( CPointer *pEntityPointer, bool bRaiseException ) +bool PlayerInfoFromPointer( CPointer *pEntityPointer, IPlayerInfo*& output) { - IPlayerInfo *pPlayerInfo = PlayerInfoFromBaseEntity(BaseEntityFromPointer(pEntityPointer)); - - if (!pPlayerInfo && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a PlayerInfo instance from the given Pointer instance (%x).", pEntityPointer->m_ulAddr); + CBaseEntity* pBaseEntity; + if (!BaseEntityFromPointer(pEntityPointer, pBaseEntity)) + return false; - return pPlayerInfo; + return PlayerInfoFromBaseEntity(pBaseEntity, output); } //----------------------------------------------------------------------------- // Returns a PlayerInfo instance from the given Edict instance. //----------------------------------------------------------------------------- -IPlayerInfo *PlayerInfoFromEdict( edict_t *pEdict, bool bRaiseException ) +bool PlayerInfoFromEdict( edict_t *pEdict, IPlayerInfo*& output) { - IPlayerInfo *pPlayerInfo = PlayerInfoFromIndex(IndexFromEdict(pEdict)); + if (pEdict || pEdict->IsFree()) + return false; - if (!pPlayerInfo && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a PlayerInfo instance from the given Edict instance (%x).", pEdict); + IPlayerInfo* pPlayerInfo = playerinfomanager->GetPlayerInfo(pEdict); + if (!pPlayerInfo) + return false; - return pPlayerInfo; + output = pPlayerInfo; + return false; } //----------------------------------------------------------------------------- // Returns a PlayerInfo instance from the given BaseHandle instance. //----------------------------------------------------------------------------- -IPlayerInfo *PlayerInfoFromBaseHandle( CBaseHandle hBaseHandle, bool bRaiseException ) +bool PlayerInfoFromBaseHandle( CBaseHandle hBaseHandle, IPlayerInfo*& output) { - IPlayerInfo *pPlayerInfo = PlayerInfoFromIndex(hBaseHandle.GetEntryIndex()); + unsigned int iEntityIndex; + if (!IndexFromBaseHandle(hBaseHandle, iEntityIndex)) + return false; - if (!pPlayerInfo && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a PlayerInfo instance from the given BaseHandle instance (%i).", hBaseHandle.ToInt()); - - return pPlayerInfo; + return PlayerInfoFromIndex(iEntityIndex, output); } //----------------------------------------------------------------------------- // Returns a PlayerInfo instance from the given IntHandle. //----------------------------------------------------------------------------- -IPlayerInfo *PlayerInfoFromIntHandle( int iEntityHandle, bool bRaiseException ) +bool PlayerInfoFromIntHandle( unsigned int iEntityHandle, IPlayerInfo*& output) { - IPlayerInfo *pPlayerInfo = PlayerInfoFromIndex(IndexFromIntHandle(iEntityHandle)); - - if (!pPlayerInfo && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a PlayerInfo instance from the given IntHandle (%i).", iEntityHandle); + unsigned int iEntityIndex; + if (!IndexFromIntHandle(iEntityHandle, iEntityIndex)) + return false; - return pPlayerInfo; + return PlayerInfoFromIndex(iEntityIndex, output); } //----------------------------------------------------------------------------- // Returns a PlayerInfo instance from the given UserID. //----------------------------------------------------------------------------- -IPlayerInfo *PlayerInfoFromUserid( int iUserID, bool bRaiseException ) +bool PlayerInfoFromUserid( unsigned int iUserID, IPlayerInfo*& output) { - IPlayerInfo *pPlayerInfo = PlayerInfoFromEdict(EdictFromUserid(iUserID)); - - if (!pPlayerInfo && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a PlayerInfo instance from the given UserID (%i).", iUserID); + edict_t* pEdict; + if (!EdictFromUserid(iUserID, pEdict)) + return false; - return pPlayerInfo; + return PlayerInfoFromEdict(pEdict, output); } diff --git a/src/core/utilities/conversions/pointer_from.cpp b/src/core/utilities/conversions/pointer_from.cpp index 6bc907c92..26949d46a 100644 --- a/src/core/utilities/conversions/pointer_from.cpp +++ b/src/core/utilities/conversions/pointer_from.cpp @@ -33,94 +33,89 @@ //----------------------------------------------------------------------------- // Returns a Pointer instance from the given Edict instance. //----------------------------------------------------------------------------- -CPointer *PointerFromEdict( edict_t *pEdict, bool bRaiseException ) +bool PointerFromEdict( edict_t *pEdict, CPointer& output) { - CBaseEntity *pBaseEntity = BaseEntityFromEdict(pEdict); + CBaseEntity* pBaseEntity; + if (!BaseEntityFromEdict(pEdict, pBaseEntity)) + return false; - if (!pBaseEntity && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a Pointer instance from the given Edict instance (%x).", pEdict); - - return new CPointer((unsigned long)pBaseEntity); + return PointerFromBaseEntity(pBaseEntity, output); } //----------------------------------------------------------------------------- // Returns a Pointer instance from the given index. //----------------------------------------------------------------------------- -CPointer *PointerFromIndex( int iEntityIndex, bool bRaiseException ) +bool PointerFromIndex( unsigned int iEntityIndex, CPointer& output) { - CBaseEntity *pBaseEntity = BaseEntityFromIndex(iEntityIndex); - - if (!pBaseEntity && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a Pointer instance from the given index (%i).", iEntityIndex); + CBaseEntity *pBaseEntity; + if (!BaseEntityFromIndex(iEntityIndex, pBaseEntity)) + return false; - return new CPointer((unsigned long)pBaseEntity); + return PointerFromBaseEntity(pBaseEntity, output); } //----------------------------------------------------------------------------- // Returns a Pointer instance from the given BaseEntity instance. //----------------------------------------------------------------------------- -CPointer *PointerFromBaseEntity( CBaseEntity *pBaseEntity, bool bRaiseException ) +bool PointerFromBaseEntity( CBaseEntity *pBaseEntity, CPointer& output) { - if (!pBaseEntity && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a Pointer instance from the given BaseEntity instance (%x).", pBaseEntity); + if (!pBaseEntity) + return false; - return new CPointer((unsigned long)pBaseEntity); + output = CPointer((unsigned long) pBaseEntity); + return true; } //----------------------------------------------------------------------------- // Returns a Pointer instance from the given BaseHandle instance. //----------------------------------------------------------------------------- -CPointer *PointerFromBaseHandle( CBaseHandle hBaseHandle, bool bRaiseException ) +bool PointerFromBaseHandle( CBaseHandle hBaseHandle, CPointer& output) { - CBaseEntity *pBaseEntity = BaseEntityFromBaseHandle(hBaseHandle); - - if (!pBaseEntity && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a Pointer instance from the given BaseHandle instance (%i).", hBaseHandle.ToInt()); + CBaseEntity *pBaseEntity; + if (!BaseEntityFromBaseHandle(hBaseHandle, pBaseEntity)) + return false; - return new CPointer((unsigned long)pBaseEntity); + return PointerFromBaseEntity(pBaseEntity, output); } //----------------------------------------------------------------------------- // Returns a Pointer instance from the given IntHandle. //----------------------------------------------------------------------------- -CPointer *PointerFromIntHandle( int iEntityHandle, bool bRaiseException ) +bool PointerFromIntHandle( unsigned int iEntityHandle, CPointer& output) { - CBaseEntity *pBaseEntity = BaseEntityFromIntHandle(iEntityHandle); + CBaseEntity *pBaseEntity; + if (!BaseEntityFromIntHandle(iEntityHandle, pBaseEntity)) + return false; - if (!pBaseEntity && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a Pointer instance from the given IntHandle (%i).", iEntityHandle); - - return new CPointer((unsigned long)pBaseEntity); + return PointerFromBaseEntity(pBaseEntity, output); } //----------------------------------------------------------------------------- // Returns a Pointer instance from the given UserID. //----------------------------------------------------------------------------- -CPointer *PointerFromUserid( int iUserID, bool bRaiseException ) +bool PointerFromUserid( unsigned int iUserID, CPointer& output) { - CBaseEntity *pBaseEntity = BaseEntityFromUserid(iUserID); - - if (!pBaseEntity && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a Pointer instance from the given UserID (%i).", iUserID); + CBaseEntity *pBaseEntity; + if (!BaseEntityFromUserid(iUserID, pBaseEntity)) + return false; - return new CPointer((unsigned long)pBaseEntity); + return PointerFromBaseEntity(pBaseEntity, output); } //----------------------------------------------------------------------------- // Returns a Pointer instance from the given PlayerInfo instance. //----------------------------------------------------------------------------- -CPointer *PointerFromPlayerInfo( IPlayerInfo *pPlayerInfo, bool bRaiseException ) +bool PointerFromPlayerInfo( IPlayerInfo *pPlayerInfo, CPointer& output) { - CBaseEntity *pBaseEntity = BaseEntityFromPlayerInfo(pPlayerInfo); - - if (!pBaseEntity && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a Pointer instance from the given PlayerInfo instance (%x).", pPlayerInfo); + CBaseEntity *pBaseEntity; + if (!BaseEntityFromPlayerInfo(pPlayerInfo, pBaseEntity)) + return false; - return new CPointer((unsigned long)pBaseEntity); + return PointerFromBaseEntity(pBaseEntity, output); } diff --git a/src/core/utilities/conversions/userid_from.cpp b/src/core/utilities/conversions/userid_from.cpp index 203dfee96..e875edeea 100644 --- a/src/core/utilities/conversions/userid_from.cpp +++ b/src/core/utilities/conversions/userid_from.cpp @@ -33,102 +33,93 @@ //----------------------------------------------------------------------------- // Returns a UserID from the given PlayerInfo instance. //----------------------------------------------------------------------------- -int UseridFromPlayerInfo( IPlayerInfo *pPlayerInfo, bool bRaiseException ) +bool UseridFromPlayerInfo( IPlayerInfo *pPlayerInfo, unsigned int& output ) { - int iUserID = INVALID_PLAYER_USERID; + if (!pPlayerInfo) + return false; - if (pPlayerInfo) - iUserID = pPlayerInfo->GetUserID(); + int iUserID = pPlayerInfo->GetUserID(); + if (iUserID == INVALID_PLAYER_USERID) + return false; - if (iUserID == INVALID_PLAYER_USERID && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a UserID from the given PlayerInfo instance (%x).", pPlayerInfo); - - return iUserID; + output = iUserID; + return true; } //----------------------------------------------------------------------------- // Returns a UserID from the given index. //----------------------------------------------------------------------------- -int UseridFromIndex( int iEntityIndex, bool bRaiseException ) +bool UseridFromIndex( unsigned int iEntityIndex, unsigned int& output ) { - int iUserID = UseridFromPlayerInfo(PlayerInfoFromIndex(iEntityIndex)); - - if (iUserID == INVALID_PLAYER_USERID && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a UserID from the given index (%i).", iEntityIndex); + IPlayerInfo* pPlayerInfo; + if (!PlayerInfoFromIndex(iEntityIndex, pPlayerInfo)) + return false; - return iUserID; + return UseridFromPlayerInfo(pPlayerInfo, output); } //----------------------------------------------------------------------------- // Returns a UserID from the given Edict instance. //----------------------------------------------------------------------------- -int UseridFromEdict( edict_t *pEdict, bool bRaiseException ) +bool UseridFromEdict( edict_t *pEdict, unsigned int& output ) { - int iUserID = UseridFromIndex(IndexFromEdict(pEdict)); + unsigned int iEntityIndex; + if (!IndexFromEdict(pEdict, iEntityIndex)) + return false; - if (iUserID == INVALID_PLAYER_USERID && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a UserID from the given Edict instance (%x).", pEdict); - - return iUserID; + return UseridFromIndex(iEntityIndex, output); } //----------------------------------------------------------------------------- // Returns a UserID from the given BaseHandle instance. //----------------------------------------------------------------------------- -int UseridFromBaseHandle( CBaseHandle hBaseHandle, bool bRaiseException ) +bool UseridFromBaseHandle( CBaseHandle hBaseHandle, unsigned int& output ) { - int iUserID = UseridFromIndex(IndexFromBaseHandle(hBaseHandle)); - - if (iUserID == INVALID_PLAYER_USERID && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a UserID from the given BaseHandle instance (%i).", hBaseHandle.ToInt()); + unsigned int iEntityIndex; + if (!IndexFromBaseHandle(hBaseHandle, iEntityIndex)) + return false; - return iUserID; + return UseridFromIndex(iEntityIndex, output); } //----------------------------------------------------------------------------- // Returns a UserID from the given IntHandle. //----------------------------------------------------------------------------- -int UseridFromIntHandle( int iEntityHandle, bool bRaiseException ) +bool UseridFromIntHandle( unsigned int iEntityHandle, unsigned int& output ) { - int iUserID = INVALID_PLAYER_USERID; + CBaseHandle hBaseHandle; + if (!BaseHandleFromIntHandle(iEntityHandle, hBaseHandle)) + return false; - if (iEntityHandle != (int) INVALID_EHANDLE_INDEX) - iUserID = UseridFromBaseHandle(BaseHandleFromIntHandle(iEntityHandle)); - - if (iUserID == INVALID_PLAYER_USERID && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a UserID from the given IntHandle (%i).", iEntityHandle); - - return iUserID; + return UseridFromBaseHandle(hBaseHandle, output); } //----------------------------------------------------------------------------- // Returns a UserID from the given BaseEntity instance. //----------------------------------------------------------------------------- -int UseridFromBaseEntity( CBaseEntity *pBaseEntity, bool bRaiseException ) +bool UseridFromBaseEntity( CBaseEntity *pBaseEntity, unsigned int& output ) { - int iUserID = UseridFromPlayerInfo(PlayerInfoFromBaseEntity(pBaseEntity)); + IPlayerInfo* pPlayerInfo; + if (!PlayerInfoFromBaseEntity(pBaseEntity, pPlayerInfo)) + return false; - if (iUserID == INVALID_PLAYER_USERID && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a UserID from the given BaseEntity instance (%x).", pBaseEntity); - - return iUserID; + return UseridFromPlayerInfo(pPlayerInfo, output); } //----------------------------------------------------------------------------- // Returns a UserID from the given Pointer instance. //----------------------------------------------------------------------------- -int UseridFromPointer( CPointer *pEntityPointer, bool bRaiseException ) +bool UseridFromPointer( CPointer *pEntityPointer, unsigned int& output ) { - int iUserID = UseridFromBaseEntity(BaseEntityFromPointer(pEntityPointer)); - - if (iUserID == INVALID_PLAYER_USERID && bRaiseException) - BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a UserID from the given Pointer instance (%x).", pEntityPointer->m_ulAddr); + CBaseEntity* pBaseEntity; + if (!BaseEntityFromPointer(pEntityPointer, pBaseEntity)) + return false; - return iUserID; + return UseridFromBaseEntity(pBaseEntity, output); } From caad874c694ad80d07315afb4fc321cbbc5347a6 Mon Sep 17 00:00:00 2001 From: Ayuto Date: Fri, 13 Nov 2015 11:59:59 +0100 Subject: [PATCH 02/13] Fixed BaseEntity.is_player --- src/core/modules/entities/entities_entity.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/core/modules/entities/entities_entity.h b/src/core/modules/entities/entities_entity.h index 1fd94f286..7bedfc125 100644 --- a/src/core/modules/entities/entities_entity.h +++ b/src/core/modules/entities/entities_entity.h @@ -242,7 +242,10 @@ class CBaseEntityWrapper: public IServerEntity return false; unsigned int iEntityIndex; - return IndexFromBaseEntity(GetThis(), iEntityIndex); + if (!IndexFromBaseEntity(GetThis(), iEntityIndex)) + return false; + + return iEntityIndex > WORLD_ENTITY_INDEX && iEntityIndex <= (unsigned int) gpGlobals->maxClients; } }; From f5b604b2932ed19503208f401f26d271b4db3e9c Mon Sep 17 00:00:00 2001 From: Ayuto Date: Fri, 13 Nov 2015 12:01:58 +0100 Subject: [PATCH 03/13] Updated OnEntityCreated, OnEntitySpawned and OnEntityDeleted listeners They now receive None instead of an invalid entity index --- src/core/sp_main.cpp | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/core/sp_main.cpp b/src/core/sp_main.cpp index 2491fe2f6..b2fcc974e 100644 --- a/src/core/sp_main.cpp +++ b/src/core/sp_main.cpp @@ -507,13 +507,17 @@ void CSourcePython::OnEntityPreSpawned( CBaseEntity *pEntity ) void CSourcePython::OnEntityCreated( CBaseEntity *pEntity ) { - // TODO: Udpate this. The index should be None instead of -1 - unsigned int iIndex; - if (!IndexFromBaseEntity(pEntity, iIndex)) - iIndex = -1; + unsigned int iEntityIndex; + object index; + if (!IndexFromBaseEntity(pEntity, iEntityIndex)) { + index = object(); + } + else { + index = object(iEntityIndex); + } edict_t *pEdict; - if (!EdictFromIndex(iIndex, pEdict)) + if (!EdictFromIndex(iEntityIndex, pEdict)) pEdict = NULL; if (pEdict) @@ -522,27 +526,35 @@ void CSourcePython::OnEntityCreated( CBaseEntity *pEntity ) if (pServerUnknown) pEdict->m_pNetworkable = pServerUnknown->GetNetworkable(); } - CALL_LISTENERS(OnEntityCreated, iIndex, ptr((CBaseEntityWrapper*) pEntity)); + CALL_LISTENERS(OnEntityCreated, index, ptr((CBaseEntityWrapper*) pEntity)); } void CSourcePython::OnEntitySpawned( CBaseEntity *pEntity ) { - // TODO: Udpate this. The index should be None instead of -1 unsigned int iEntityIndex; - if (!IndexFromBaseEntity(pEntity, iEntityIndex)) - iEntityIndex = -1; + object index; + if (!IndexFromBaseEntity(pEntity, iEntityIndex)) { + index = object(); + } + else { + index = object(iEntityIndex); + } - CALL_LISTENERS(OnEntitySpawned, iEntityIndex, ptr((CBaseEntityWrapper*) pEntity)); + CALL_LISTENERS(OnEntitySpawned, index, ptr((CBaseEntityWrapper*) pEntity)); } void CSourcePython::OnEntityDeleted( CBaseEntity *pEntity ) { - // TODO: Udpate this. The index should be None instead of -1 unsigned int iEntityIndex; - if (!IndexFromBaseEntity(pEntity, iEntityIndex)) - iEntityIndex = -1; + object index; + if (!IndexFromBaseEntity(pEntity, iEntityIndex)) { + index = object(); + } + else { + index = object(iEntityIndex); + } - CALL_LISTENERS(OnEntityDeleted, iEntityIndex, ptr((CBaseEntityWrapper*) pEntity)); + CALL_LISTENERS(OnEntityDeleted, index, ptr((CBaseEntityWrapper*) pEntity)); } void CSourcePython::OnDataLoaded( MDLCacheDataType_t type, MDLHandle_t handle ) From a8d1738fa4fbefdc573151486ebc9f8f54e0fe85 Mon Sep 17 00:00:00 2001 From: Ayuto Date: Fri, 13 Nov 2015 12:03:49 +0100 Subject: [PATCH 04/13] Updated DispatchClientCommand to return in case of an invalid index (Should never happen I guess) --- src/core/modules/commands/commands_client.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/modules/commands/commands_client.cpp b/src/core/modules/commands/commands_client.cpp index cfb13bceb..6e8cec4aa 100644 --- a/src/core/modules/commands/commands_client.cpp +++ b/src/core/modules/commands/commands_client.cpp @@ -113,9 +113,8 @@ void UnregisterClientCommandFilter(PyObject* pCallable) PLUGIN_RESULT DispatchClientCommand(edict_t* pEntity, const CCommand &command) { unsigned int iIndex; - // TODO: When does this ever happen? if (!IndexFromEdict(pEntity, iIndex)) - iIndex = -1; + return PLUGIN_CONTINUE; // Loop through all registered Client Command Filters for(int i = 0; i < s_ClientCommandFilters.m_vecCallables.Count(); i++) From 080dea857d444dff21be4326d4f58f51874466c2 Mon Sep 17 00:00:00 2001 From: Ayuto Date: Fri, 13 Nov 2015 12:05:54 +0100 Subject: [PATCH 05/13] Fixed two comparisons --- addons/source-python/packages/source-python/entities/hooks.py | 2 +- .../packages/source-python/players/weapons/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/addons/source-python/packages/source-python/entities/hooks.py b/addons/source-python/packages/source-python/entities/hooks.py index f8eba04b9..3b67a9f98 100644 --- a/addons/source-python/packages/source-python/entities/hooks.py +++ b/addons/source-python/packages/source-python/entities/hooks.py @@ -42,7 +42,7 @@ class EntityCondition(object): @staticmethod def is_player(entity): """Return True if the entity is a player.""" - return userid_from_index(entity.index, False) != INVALID_PLAYER_USERID + return userid_from_index(entity.index) is not None @classmethod def is_not_player(cls, entity): diff --git a/addons/source-python/packages/source-python/players/weapons/__init__.py b/addons/source-python/packages/source-python/players/weapons/__init__.py index 71c6f1cc9..8af76aeab 100644 --- a/addons/source-python/packages/source-python/players/weapons/__init__.py +++ b/addons/source-python/packages/source-python/players/weapons/__init__.py @@ -332,7 +332,7 @@ def weapon_indexes( index = index_from_inthandle(handle, raise_exception=False) # Is this a valid index? - if index == INVALID_ENTITY_INDEX: + if index is None: # Move onto the next offset continue From d6ecc8471eaf7f8e2daca6ee04086798ed6fe0ad Mon Sep 17 00:00:00 2001 From: Ayuto Date: Fri, 13 Nov 2015 12:08:33 +0100 Subject: [PATCH 06/13] Added uniqueid_from_index Updated some conversion functions to return None instead of an error address_from_playerinfo is now return an empty string for bots --- .../packages/source-python/players/entity.py | 2 +- .../packages/source-python/players/helpers.py | 31 ++++++++++++------- .../packages/source-python/settings/types.py | 7 ++--- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/addons/source-python/packages/source-python/players/entity.py b/addons/source-python/packages/source-python/players/entity.py index ba9b44be6..39285277c 100644 --- a/addons/source-python/packages/source-python/players/entity.py +++ b/addons/source-python/packages/source-python/players/entity.py @@ -142,7 +142,7 @@ def uniqueid(self): def address(self): """Return the player's IP address and port. - If the player is a bot, '0' will be returned. + If the player is a bot, an empty string will be returned. :return: The IP address. E.g. '127.0.0.1:27015' :rtype: str diff --git a/addons/source-python/packages/source-python/players/helpers.py b/addons/source-python/packages/source-python/players/helpers.py index bd9488816..7a58eb845 100644 --- a/addons/source-python/packages/source-python/players/helpers.py +++ b/addons/source-python/packages/source-python/players/helpers.py @@ -73,6 +73,7 @@ 'playerinfo_from_userid', 'pointer_from_playerinfo', 'pointer_from_userid', + 'uniqueid_from_index', 'uniqueid_from_playerinfo', 'userid_from_baseentity', 'userid_from_basehandle', @@ -100,9 +101,8 @@ def index_from_steamid(steamid): # Return the index of the current player return index_from_playerinfo(playerinfo) - - # If no player found with a matching SteamID, raise an error - raise ValueError('Invalid SteamID "{0}"'.format(steamid)) + + return None def index_from_uniqueid(uniqueid): @@ -118,9 +118,8 @@ def index_from_uniqueid(uniqueid): # Return the index of the current player return index_from_playerinfo(playerinfo) - - # If no player found with a matching UniqueID, raise an error - raise ValueError('Invalid UniqueID "{0}"'.format(uniqueid)) + + return None def index_from_name(name): @@ -136,9 +135,8 @@ def index_from_name(name): # Return the index of the current player return index_from_playerinfo(playerinfo) - - # If no player found with a matching name, raise an error - raise ValueError('Invalid name "{0}"'.format(name)) + + return None def uniqueid_from_playerinfo(playerinfo): @@ -163,6 +161,15 @@ def uniqueid_from_playerinfo(playerinfo): # Return the player's SteamID return steamid + + +def uniqueid_from_index(index): + """Return the UniqueID for the given player index.""" + playerinfo = playerinfo_from_index(index) + if playerinfo is None: + return None + + return uniqueid_from_playerinfo(playerinfo) def address_from_playerinfo(playerinfo): @@ -170,9 +177,9 @@ def address_from_playerinfo(playerinfo): # Is the player a bot? if playerinfo.is_fake_client(): - # Return a base value, since using - # .get_address() crashes with bots - return '0' + # Return an empty string, since using .get_address() crashes + # with bots + return '' # Get the player's index index = index_from_playerinfo(playerinfo) diff --git a/addons/source-python/packages/source-python/settings/types.py b/addons/source-python/packages/source-python/settings/types.py index 34e1f66ea..85890c292 100644 --- a/addons/source-python/packages/source-python/settings/types.py +++ b/addons/source-python/packages/source-python/settings/types.py @@ -20,8 +20,7 @@ # Messages from messages import SayText # Players -from players.helpers import playerinfo_from_index -from players.helpers import uniqueid_from_playerinfo +from players.helpers import uniqueid_from_index # Settings from settings import _settings_strings from settings.storage import _player_settings_storage @@ -122,7 +121,7 @@ def get_setting(self, index): return value # Get the client's uniqueid - uniqueid = uniqueid_from_playerinfo(playerinfo_from_index(index)) + uniqueid = uniqueid_from_index(index)) # Is the uniqueid in the setting's storage dictionary? if uniqueid in _player_settings_storage: @@ -155,7 +154,7 @@ def _menu_build(self, menu, index): def _chosen_value(self, menu, index, option): """Store the player's chosen value for the setting.""" # Get the client's uniqueid - uniqueid = uniqueid_from_playerinfo(playerinfo_from_index(index)) + uniqueid = uniqueid_from_index(index)) # Set the player's setting _player_settings_storage[uniqueid][self.convar] = option.value From 307f3028a52e179b54343270e74f3c535e190d82 Mon Sep 17 00:00:00 2001 From: Ayuto Date: Fri, 13 Nov 2015 12:14:35 +0100 Subject: [PATCH 07/13] Fixed another comparison Removed ununsed imports --- addons/source-python/packages/source-python/entities/hooks.py | 4 +--- .../packages/source-python/players/weapons/__init__.py | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/addons/source-python/packages/source-python/entities/hooks.py b/addons/source-python/packages/source-python/entities/hooks.py index 3b67a9f98..9d598198d 100644 --- a/addons/source-python/packages/source-python/entities/hooks.py +++ b/addons/source-python/packages/source-python/entities/hooks.py @@ -9,7 +9,6 @@ # Core from core import AutoUnload # Entities -from entities.constants import INVALID_ENTITY_INDEX # Memory from _memory import HookType # Filters @@ -19,7 +18,6 @@ # Entities from entities.entity import Entity # Players -from players.constants import INVALID_PLAYER_USERID from players.entity import Player from players.helpers import userid_from_index @@ -181,5 +179,5 @@ def initialize(self, index): @OnEntityCreated def on_entity_created(index, ptr): """Called when a new entity has been created.""" - if index != INVALID_ENTITY_INDEX: + if index is not None: _waiting_entity_hooks.initialize(index) diff --git a/addons/source-python/packages/source-python/players/weapons/__init__.py b/addons/source-python/packages/source-python/players/weapons/__init__.py index 8af76aeab..9ea004174 100644 --- a/addons/source-python/packages/source-python/players/weapons/__init__.py +++ b/addons/source-python/packages/source-python/players/weapons/__init__.py @@ -7,7 +7,6 @@ # ============================================================================= # Source.Python Imports # Entities -from entities.constants import INVALID_ENTITY_INDEX from entities.entity import Entity from entities.helpers import edict_from_index from entities.helpers import index_from_inthandle From a501b40f1593ab6bf60fadc5450c5d52557386d8 Mon Sep 17 00:00:00 2001 From: Ayuto Date: Fri, 13 Nov 2015 17:10:23 +0100 Subject: [PATCH 08/13] Updated conversion usage in game specific files --- src/core/modules/entities/bms/entities_helpers.h | 6 +++++- src/core/modules/entities/csgo/entities_helpers.h | 4 +++- src/core/modules/entities/l4d2/entities_helpers.h | 5 ++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/core/modules/entities/bms/entities_helpers.h b/src/core/modules/entities/bms/entities_helpers.h index d33b01e25..58b9febfa 100644 --- a/src/core/modules/entities/bms/entities_helpers.h +++ b/src/core/modules/entities/bms/entities_helpers.h @@ -47,7 +47,11 @@ extern IServerTools *servertools; //----------------------------------------------------------------------------- void remove_entity(unsigned int uiEntityIndex) { - servertools->RemoveEntity(BaseEntityFromIndex(uiEntityIndex, true)); + CBaseEntity* pBaseEntity; + if (!BaseEntityFromIndex(uiEntityIndex, pBaseEntity)) + BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseEntity object from the given index: '%i'", uiEntityIndex); + + servertools->RemoveEntity(pBaseEntity); } diff --git a/src/core/modules/entities/csgo/entities_helpers.h b/src/core/modules/entities/csgo/entities_helpers.h index 8ccd12616..cc1a76a79 100644 --- a/src/core/modules/entities/csgo/entities_helpers.h +++ b/src/core/modules/entities/csgo/entities_helpers.h @@ -48,7 +48,9 @@ extern IServerTools *servertools; //----------------------------------------------------------------------------- void remove_entity(unsigned int uiEntityIndex) { - CBaseEntity *pBaseEntity = BaseEntityFromIndex(uiEntityIndex, true); + CBaseEntity *pBaseEntity; + if (!BaseEntityFromIndex(uiEntityIndex, pBaseEntity)) + BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseEntity object from the given index: '%i'", uiEntityIndex); int iHammerID = INT_MAX; while (servertools->FindEntityByHammerID(iHammerID)) diff --git a/src/core/modules/entities/l4d2/entities_helpers.h b/src/core/modules/entities/l4d2/entities_helpers.h index be6a5f9cc..06854d8f0 100644 --- a/src/core/modules/entities/l4d2/entities_helpers.h +++ b/src/core/modules/entities/l4d2/entities_helpers.h @@ -46,7 +46,10 @@ void remove_entity(unsigned int uiEntityIndex) CEntityFactoryDictionary* pFactoryDict = extract( import("entities.factories").attr("factory_dictionary")); - edict_t* pEdict = EdictFromIndex(uiEntityIndex, true); + edict_t* pEdict; + if (!EdictFromIndex(uiEntityIndex, pEdict)) + BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get an Edict object from the given index: '%i'", uiEntityIndex); + pFactoryDict->Destroy(pEdict->GetClassName(), pEdict->GetNetworkable()); } From 60e68f385c4e912cc72bd3122d947bec10e1d96e Mon Sep 17 00:00:00 2001 From: Ayuto Date: Fri, 13 Nov 2015 17:12:57 +0100 Subject: [PATCH 09/13] Fixed pEntity not being defined --- src/core/sp_main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/sp_main.cpp b/src/core/sp_main.cpp index b2fcc974e..eef95f208 100644 --- a/src/core/sp_main.cpp +++ b/src/core/sp_main.cpp @@ -486,7 +486,7 @@ void CSourcePython::ClientFullyConnect( edict_t *pEntity ) void CSourcePython::OnEdictAllocated( edict_t *edict ) { unsigned int iEntityIndex; - if (!IndexFromEdict(pEntity, iEntityIndex)) + if (!IndexFromEdict(edict, iEntityIndex)) return; CALL_LISTENERS(OnEdictAllocated, iEntityIndex); From b062234ee4ee30ae499e5df51db3f7fa1055b74f Mon Sep 17 00:00:00 2001 From: Ayuto Date: Fri, 13 Nov 2015 21:46:35 +0100 Subject: [PATCH 10/13] Fixed a syntax error --- addons/source-python/packages/source-python/settings/types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/source-python/packages/source-python/settings/types.py b/addons/source-python/packages/source-python/settings/types.py index 85890c292..a78fdcea0 100644 --- a/addons/source-python/packages/source-python/settings/types.py +++ b/addons/source-python/packages/source-python/settings/types.py @@ -121,7 +121,7 @@ def get_setting(self, index): return value # Get the client's uniqueid - uniqueid = uniqueid_from_index(index)) + uniqueid = uniqueid_from_index(index) # Is the uniqueid in the setting's storage dictionary? if uniqueid in _player_settings_storage: From 8cbd9f49e3298917909ed4a8be8f51e1134cd9c7 Mon Sep 17 00:00:00 2001 From: Ayuto Date: Fri, 13 Nov 2015 21:48:27 +0100 Subject: [PATCH 11/13] And another SytaxError... --- addons/source-python/packages/source-python/settings/types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/source-python/packages/source-python/settings/types.py b/addons/source-python/packages/source-python/settings/types.py index a78fdcea0..80c328b82 100644 --- a/addons/source-python/packages/source-python/settings/types.py +++ b/addons/source-python/packages/source-python/settings/types.py @@ -154,7 +154,7 @@ def _menu_build(self, menu, index): def _chosen_value(self, menu, index, option): """Store the player's chosen value for the setting.""" # Get the client's uniqueid - uniqueid = uniqueid_from_index(index)) + uniqueid = uniqueid_from_index(index) # Set the player's setting _player_settings_storage[uniqueid][self.convar] = option.value From 8b42d831630b10e2c8b39570a368c96327a462ab Mon Sep 17 00:00:00 2001 From: Ayuto Date: Sat, 14 Nov 2015 01:03:12 +0100 Subject: [PATCH 12/13] Fixed 3 stupid mistakes --- src/core/modules/players/players_generator.cpp | 2 +- src/core/utilities/conversions/playerinfo_from.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/modules/players/players_generator.cpp b/src/core/modules/players/players_generator.cpp index 4d74461ab..8cf252d43 100644 --- a/src/core/modules/players/players_generator.cpp +++ b/src/core/modules/players/players_generator.cpp @@ -71,7 +71,7 @@ edict_t *CPlayerGenerator::getNext() while(m_iEntityIndex < gpGlobals->maxClients) { m_iEntityIndex++; - if (!EdictFromIndex(m_iEntityIndex, pEdict)) + if (EdictFromIndex(m_iEntityIndex, pEdict)) break; } return pEdict; diff --git a/src/core/utilities/conversions/playerinfo_from.cpp b/src/core/utilities/conversions/playerinfo_from.cpp index cc9e548e2..43e1b6598 100644 --- a/src/core/utilities/conversions/playerinfo_from.cpp +++ b/src/core/utilities/conversions/playerinfo_from.cpp @@ -77,7 +77,7 @@ bool PlayerInfoFromPointer( CPointer *pEntityPointer, IPlayerInfo*& output) //----------------------------------------------------------------------------- bool PlayerInfoFromEdict( edict_t *pEdict, IPlayerInfo*& output) { - if (pEdict || pEdict->IsFree()) + if (!pEdict || pEdict->IsFree()) return false; IPlayerInfo* pPlayerInfo = playerinfomanager->GetPlayerInfo(pEdict); @@ -85,7 +85,7 @@ bool PlayerInfoFromEdict( edict_t *pEdict, IPlayerInfo*& output) return false; output = pPlayerInfo; - return false; + return true; } From 5e37661fb426009b0fd20e37826ae3046c896881 Mon Sep 17 00:00:00 2001 From: Ayuto22 Date: Thu, 19 Nov 2015 15:00:43 +0100 Subject: [PATCH 13/13] Fixed error checking in two FromBaseHandle conversions --- src/core/utilities/conversions/index_from.cpp | 2 +- src/core/utilities/conversions/inthandle_from.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/utilities/conversions/index_from.cpp b/src/core/utilities/conversions/index_from.cpp index 7a8b663e7..1f3b0aed5 100644 --- a/src/core/utilities/conversions/index_from.cpp +++ b/src/core/utilities/conversions/index_from.cpp @@ -90,7 +90,7 @@ bool IndexFromPointer( CPointer *pEntityPointer, unsigned int& output ) //----------------------------------------------------------------------------- bool IndexFromBaseHandle( CBaseHandle hBaseHandle, unsigned int& output ) { - if (!hBaseHandle.IsValid()) + if (!hBaseHandle.IsValid() || !hBaseHandle.ToInt()) return false; int iEntityIndex = hBaseHandle.GetEntryIndex(); diff --git a/src/core/utilities/conversions/inthandle_from.cpp b/src/core/utilities/conversions/inthandle_from.cpp index 5ef72ebb3..810145305 100644 --- a/src/core/utilities/conversions/inthandle_from.cpp +++ b/src/core/utilities/conversions/inthandle_from.cpp @@ -39,7 +39,7 @@ bool IntHandleFromBaseHandle( CBaseHandle hBaseHandle, unsigned int& output ) return false; unsigned int iEntityHandle = hBaseHandle.ToInt(); - if (iEntityHandle == INVALID_EHANDLE_INDEX) + if (!iEntityHandle) return false; output = iEntityHandle;