Skip to content

Commit e3b040a

Browse files
committed
Merge pull request #83 from Source-Python-Dev-Team/conversions_update2
Merge conversions_update2 into master
2 parents fbfb4ae + 5e37661 commit e3b040a

30 files changed

+739
-621
lines changed

addons/source-python/packages/source-python/entities/hooks.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
# Core
1010
from core import AutoUnload
1111
# Entities
12-
from entities.constants import INVALID_ENTITY_INDEX
1312
# Memory
1413
from _memory import HookType
1514
# Filters
@@ -19,7 +18,6 @@
1918
# Entities
2019
from entities.entity import Entity
2120
# Players
22-
from players.constants import INVALID_PLAYER_USERID
2321
from players.entity import Player
2422
from players.helpers import userid_from_index
2523

@@ -42,7 +40,7 @@ class EntityCondition(object):
4240
@staticmethod
4341
def is_player(entity):
4442
"""Return True if the entity is a player."""
45-
return userid_from_index(entity.index, False) != INVALID_PLAYER_USERID
43+
return userid_from_index(entity.index) is not None
4644

4745
@classmethod
4846
def is_not_player(cls, entity):
@@ -181,5 +179,5 @@ def initialize(self, index):
181179
@OnEntityCreated
182180
def on_entity_created(index, ptr):
183181
"""Called when a new entity has been created."""
184-
if index != INVALID_ENTITY_INDEX:
182+
if index is not None:
185183
_waiting_entity_hooks.initialize(index)

addons/source-python/packages/source-python/players/entity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def uniqueid(self):
142142
def address(self):
143143
"""Return the player's IP address and port.
144144
145-
If the player is a bot, '0' will be returned.
145+
If the player is a bot, an empty string will be returned.
146146
147147
:return: The IP address. E.g. '127.0.0.1:27015'
148148
:rtype: str

addons/source-python/packages/source-python/players/helpers.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
'playerinfo_from_userid',
7474
'pointer_from_playerinfo',
7575
'pointer_from_userid',
76+
'uniqueid_from_index',
7677
'uniqueid_from_playerinfo',
7778
'userid_from_baseentity',
7879
'userid_from_basehandle',
@@ -100,9 +101,8 @@ def index_from_steamid(steamid):
100101

101102
# Return the index of the current player
102103
return index_from_playerinfo(playerinfo)
103-
104-
# If no player found with a matching SteamID, raise an error
105-
raise ValueError('Invalid SteamID "{0}"'.format(steamid))
104+
105+
return None
106106

107107

108108
def index_from_uniqueid(uniqueid):
@@ -118,9 +118,8 @@ def index_from_uniqueid(uniqueid):
118118

119119
# Return the index of the current player
120120
return index_from_playerinfo(playerinfo)
121-
122-
# If no player found with a matching UniqueID, raise an error
123-
raise ValueError('Invalid UniqueID "{0}"'.format(uniqueid))
121+
122+
return None
124123

125124

126125
def index_from_name(name):
@@ -136,9 +135,8 @@ def index_from_name(name):
136135

137136
# Return the index of the current player
138137
return index_from_playerinfo(playerinfo)
139-
140-
# If no player found with a matching name, raise an error
141-
raise ValueError('Invalid name "{0}"'.format(name))
138+
139+
return None
142140

143141

144142
def uniqueid_from_playerinfo(playerinfo):
@@ -163,16 +161,25 @@ def uniqueid_from_playerinfo(playerinfo):
163161

164162
# Return the player's SteamID
165163
return steamid
164+
165+
166+
def uniqueid_from_index(index):
167+
"""Return the UniqueID for the given player index."""
168+
playerinfo = playerinfo_from_index(index)
169+
if playerinfo is None:
170+
return None
171+
172+
return uniqueid_from_playerinfo(playerinfo)
166173

167174

168175
def address_from_playerinfo(playerinfo):
169176
"""Return the IP address for the given player."""
170177
# Is the player a bot?
171178
if playerinfo.is_fake_client():
172179

173-
# Return a base value, since using
174-
# <netinfo>.get_address() crashes with bots
175-
return '0'
180+
# Return an empty string, since using <netinfo>.get_address() crashes
181+
# with bots
182+
return ''
176183

177184
# Get the player's index
178185
index = index_from_playerinfo(playerinfo)

addons/source-python/packages/source-python/players/weapons/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
# =============================================================================
88
# Source.Python Imports
99
# Entities
10-
from entities.constants import INVALID_ENTITY_INDEX
1110
from entities.entity import Entity
1211
from entities.helpers import edict_from_index
1312
from entities.helpers import index_from_inthandle
@@ -332,7 +331,7 @@ def weapon_indexes(
332331
index = index_from_inthandle(handle, raise_exception=False)
333332

334333
# Is this a valid index?
335-
if index == INVALID_ENTITY_INDEX:
334+
if index is None:
336335

337336
# Move onto the next offset
338337
continue

addons/source-python/packages/source-python/settings/types.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
# Messages
2121
from messages import SayText
2222
# Players
23-
from players.helpers import playerinfo_from_index
24-
from players.helpers import uniqueid_from_playerinfo
23+
from players.helpers import uniqueid_from_index
2524
# Settings
2625
from settings import _settings_strings
2726
from settings.storage import _player_settings_storage
@@ -122,7 +121,7 @@ def get_setting(self, index):
122121
return value
123122

124123
# Get the client's uniqueid
125-
uniqueid = uniqueid_from_playerinfo(playerinfo_from_index(index))
124+
uniqueid = uniqueid_from_index(index)
126125

127126
# Is the uniqueid in the setting's storage dictionary?
128127
if uniqueid in _player_settings_storage:
@@ -155,7 +154,7 @@ def _menu_build(self, menu, index):
155154
def _chosen_value(self, menu, index, option):
156155
"""Store the player's chosen value for the setting."""
157156
# Get the client's uniqueid
158-
uniqueid = uniqueid_from_playerinfo(playerinfo_from_index(index))
157+
uniqueid = uniqueid_from_index(index)
159158

160159
# Set the player's setting
161160
_player_settings_storage[uniqueid][self.convar] = option.value

src/core/modules/commands/commands_client.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ void UnregisterClientCommandFilter(PyObject* pCallable)
112112
//-----------------------------------------------------------------------------
113113
PLUGIN_RESULT DispatchClientCommand(edict_t* pEntity, const CCommand &command)
114114
{
115-
int iIndex = IndexFromEdict(pEntity);
115+
unsigned int iIndex;
116+
if (!IndexFromEdict(pEntity, iIndex))
117+
return PLUGIN_CONTINUE;
116118

117119
// Loop through all registered Client Command Filters
118120
for(int i = 0; i < s_ClientCommandFilters.m_vecCallables.Count(); i++)

src/core/modules/entities/bms/entities_helpers.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ extern IServerTools *servertools;
4747
//-----------------------------------------------------------------------------
4848
void remove_entity(unsigned int uiEntityIndex)
4949
{
50-
servertools->RemoveEntity(BaseEntityFromIndex(uiEntityIndex, true));
50+
CBaseEntity* pBaseEntity;
51+
if (!BaseEntityFromIndex(uiEntityIndex, pBaseEntity))
52+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseEntity object from the given index: '%i'", uiEntityIndex);
53+
54+
servertools->RemoveEntity(pBaseEntity);
5155
}
5256

5357

src/core/modules/entities/csgo/entities_helpers.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ extern IServerTools *servertools;
4848
//-----------------------------------------------------------------------------
4949
void remove_entity(unsigned int uiEntityIndex)
5050
{
51-
CBaseEntity *pBaseEntity = BaseEntityFromIndex(uiEntityIndex, true);
51+
CBaseEntity *pBaseEntity;
52+
if (!BaseEntityFromIndex(uiEntityIndex, pBaseEntity))
53+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseEntity object from the given index: '%i'", uiEntityIndex);
5254

5355
int iHammerID = INT_MAX;
5456
while (servertools->FindEntityByHammerID(iHammerID))

src/core/modules/entities/entities.h

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,34 +48,49 @@
4848
class TakeDamageInfoBaseWrapper: public CTakeDamageInfo
4949
{
5050
public:
51-
int get_inflictor()
51+
object get_inflictor()
5252
{
53-
return IndexFromBaseHandle(m_hInflictor);
53+
unsigned int iEntityIndex;
54+
if (!IndexFromBaseHandle(m_hInflictor, iEntityIndex))
55+
return object();
56+
57+
return object(iEntityIndex);
5458
}
5559

5660
void set_inflictor(unsigned int uiInflictor)
5761
{
58-
m_hInflictor = BaseHandleFromIndex(uiInflictor, true);
62+
if (!BaseHandleFromIndex(uiInflictor, m_hInflictor))
63+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseHandle object from the given index: '%i'", uiInflictor);
5964
}
6065

61-
int get_attacker()
66+
object get_attacker()
6267
{
63-
return IndexFromBaseHandle(m_hAttacker);
68+
unsigned int iEntityIndex;
69+
if (!IndexFromBaseHandle(m_hAttacker, iEntityIndex))
70+
return object();
71+
72+
return object(iEntityIndex);
6473
}
6574

6675
void set_attacker(unsigned int uiAttacker)
6776
{
68-
m_hAttacker = BaseHandleFromIndex(uiAttacker, true);
77+
if (!BaseHandleFromIndex(uiAttacker, m_hAttacker))
78+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseHandle object from the given index: '%i'", uiAttacker);
6979
}
7080

71-
int get_weapon()
81+
object get_weapon()
7282
{
73-
return IndexFromBaseHandle(m_hWeapon);
83+
unsigned int iEntityIndex;
84+
if (!IndexFromBaseHandle(m_hWeapon, iEntityIndex))
85+
return object();
86+
87+
return object(iEntityIndex);
7488
}
7589

7690
void set_weapon(unsigned int uiWeapon)
7791
{
78-
m_hWeapon = BaseHandleFromIndex(uiWeapon, true);
92+
if (!BaseHandleFromIndex(uiWeapon, m_hWeapon))
93+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseHandle object from the given index: '%i'", uiWeapon);
7994
}
8095

8196
void set_base_damage(float flBaseDamage)
@@ -110,7 +125,7 @@ class TakeDamageInfoSharedExt
110125
return pTakeDamageInfo;
111126
}
112127

113-
static int get_inflictor(CTakeDamageInfo *pTakeDamageInfo)
128+
static object get_inflictor(CTakeDamageInfo *pTakeDamageInfo)
114129
{
115130
return ((TakeDamageInfoBaseWrapper *)pTakeDamageInfo)->get_inflictor();
116131
}
@@ -120,7 +135,7 @@ class TakeDamageInfoSharedExt
120135
((TakeDamageInfoBaseWrapper *)pTakeDamageInfo)->set_inflictor(iInflictor);
121136
}
122137

123-
static int get_attacker(CTakeDamageInfo *pTakeDamageInfo)
138+
static object get_attacker(CTakeDamageInfo *pTakeDamageInfo)
124139
{
125140
return ((TakeDamageInfoBaseWrapper *)pTakeDamageInfo)->get_attacker();
126141
}
@@ -130,7 +145,7 @@ class TakeDamageInfoSharedExt
130145
((TakeDamageInfoBaseWrapper *)pTakeDamageInfo)->set_attacker(iAttacker);
131146
}
132147

133-
static int get_weapon(CTakeDamageInfo *pTakeDamageInfo)
148+
static object get_weapon(CTakeDamageInfo *pTakeDamageInfo)
134149
{
135150
return ((TakeDamageInfoBaseWrapper *)pTakeDamageInfo)->get_weapon();
136151
}

src/core/modules/entities/entities_datamaps.h

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,22 @@ class VariantSharedExt
134134
return pVector;
135135
}
136136

137-
static int get_entity(variant_t *pVariant)
137+
static object get_entity(variant_t *pVariant)
138138
{
139-
return IndexFromBaseHandle(pVariant->Entity());
139+
unsigned int iEntityIndex;
140+
if (!IndexFromBaseHandle(pVariant->Entity(), iEntityIndex))
141+
return object();
142+
143+
return object(iEntityIndex);
140144
}
141145

142146
static void set_entity(variant_t *pVariant, unsigned int uiEntity)
143147
{
144-
pVariant->SetEntity(BaseEntityFromIndex(uiEntity, true));
148+
CBaseEntity* pBaseEntity;
149+
if (!BaseEntityFromIndex(uiEntity, pBaseEntity))
150+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseEntity object from the given index: '%i'", uiEntity);
151+
152+
pVariant->SetEntity(pBaseEntity);
145153
}
146154
};
147155

@@ -161,24 +169,34 @@ class InputDataSharedExt
161169
return pInputData;
162170
}
163171

164-
static int get_activator(const inputdata_t& pInputData)
172+
static object get_activator(const inputdata_t& pInputData)
165173
{
166-
return IndexFromBaseEntity(pInputData.pActivator);
174+
unsigned int iEntityIndex;
175+
if (!IndexFromBaseEntity(pInputData.pActivator, iEntityIndex))
176+
return object();
177+
178+
return object(iEntityIndex);
167179
}
168180

169181
static void set_activator(inputdata_t *pInputData, unsigned int uiActivator)
170182
{
171-
pInputData->pActivator = BaseEntityFromIndex(uiActivator, true);
183+
if (!BaseEntityFromIndex(uiActivator, pInputData->pActivator))
184+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseEntity object from the given index: '%i'", uiActivator);
172185
}
173186

174-
static int get_caller(const inputdata_t& pInputData)
187+
static object get_caller(const inputdata_t& pInputData)
175188
{
176-
return IndexFromBaseEntity(pInputData.pCaller);
189+
unsigned int iEntityIndex;
190+
if (!IndexFromBaseEntity(pInputData.pCaller, iEntityIndex))
191+
return object();
192+
193+
return object(iEntityIndex);
177194
}
178195

179196
static void set_caller(inputdata_t *pInputData, unsigned int uiCaller)
180197
{
181-
pInputData->pCaller = BaseEntityFromIndex(uiCaller, true);
198+
if (!BaseEntityFromIndex(uiCaller, pInputData->pCaller))
199+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to get a BaseEntity object from the given index: '%i'", uiCaller);
182200
}
183201
};
184202

0 commit comments

Comments
 (0)