Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions addons/source-python/packages/source-python/entities/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# Core
from core import AutoUnload
# Entities
from entities.constants import INVALID_ENTITY_INDEX
# Memory
from _memory import HookType
# Filters
Expand All @@ -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

Expand All @@ -42,7 +40,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):
Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 19 additions & 12 deletions addons/source-python/packages/source-python/players/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -163,16 +161,25 @@ 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):
"""Return the IP address for the given player."""
# Is the player a bot?
if playerinfo.is_fake_client():

# Return a base value, since using
# <netinfo>.get_address() crashes with bots
return '0'
# Return an empty string, since using <netinfo>.get_address() crashes
# with bots
return ''

# Get the player's index
index = index_from_playerinfo(playerinfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -332,7 +331,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
Expand Down
7 changes: 3 additions & 4 deletions addons/source-python/packages/source-python/settings/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/core/modules/commands/commands_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ void UnregisterClientCommandFilter(PyObject* pCallable)
//-----------------------------------------------------------------------------
PLUGIN_RESULT DispatchClientCommand(edict_t* pEntity, const CCommand &command)
{
int iIndex = IndexFromEdict(pEntity);
unsigned int iIndex;
if (!IndexFromEdict(pEntity, iIndex))
return PLUGIN_CONTINUE;

// Loop through all registered Client Command Filters
for(int i = 0; i < s_ClientCommandFilters.m_vecCallables.Count(); i++)
Expand Down
6 changes: 5 additions & 1 deletion src/core/modules/entities/bms/entities_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
4 changes: 3 additions & 1 deletion src/core/modules/entities/csgo/entities_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
39 changes: 27 additions & 12 deletions src/core/modules/entities/entities.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down
36 changes: 27 additions & 9 deletions src/core/modules/entities/entities_datamaps.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand All @@ -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);
}
};

Expand Down
Loading