Skip to content

Commit 3f1e8a1

Browse files
committed
Added new conversions_c module to implement all entity/player type conversions directly to the build. Made changes in entities.helpers and players.helpers to accomodate this change.
Updated entities.entity.BaseEntity and players.entity.PlayerEntity to use conversion in place of instantiation. Updated getting/setting of properties, keyvalues, and offsets for BaseEntity classes. Updated players.entity.PlayerEntity to use __new__ in place of __init__ to more easily allow inheritance by other classes. Added ServerUnknown.get_base_entity() to return the BaseEntity pointer of the entity.
1 parent c9af37d commit 3f1e8a1

File tree

13 files changed

+649
-367
lines changed

13 files changed

+649
-367
lines changed

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

Lines changed: 27 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# =============================================================================
66
# Source.Python Imports
77
#from Source import Binutils
8-
from entity_c import Edict
8+
from conversions_c import edict_from_index
99
from public import public
1010
# Entities
1111
#from entities.functions import Functions
@@ -28,10 +28,10 @@ def __new__(cls, index, *entities):
2828
is of the correct entity type and add the index attribute'''
2929

3030
# Get the given indexes edict
31-
edict = Edict(index)
31+
edict = edict_from_index(index)
3232

3333
# Is the edict valid?
34-
if edict.is_free() or not edict.is_valid():
34+
if not edict or edict.is_free():
3535

3636
# If not raise an error
3737
raise ValueError(
@@ -78,32 +78,28 @@ def __getattr__(self, attr):
7878
# Return the offset's value
7979
return self._get_offset(attr)
8080

81+
'''
82+
8183
# Is the attribute a function of this entity?
8284
if attr in self.functions:
8385
8486
# Return the function
8587
return self._get_function(attr)
8688
89+
'''
90+
8791
# If the attribute is not found, raise an error
8892
raise AttributeError('Attribute "{0}" not found'.format(attr))
8993

9094
def _get_property(self, item):
9195
'''Gets the value of the given property'''
9296

93-
# Get the property's instance
94-
prop = self.edict.get_prop(self.properties[item].prop)
95-
9697
# Get the property's type
9798
prop_type = self.properties[item].type
9899

99-
# Is the property's type a known type?
100-
if not hasattr(prop, 'get_{0}'.format(prop_type)):
101-
102-
# If not a proper type, raise an error
103-
raise TypeError('Invalid property type "{0}"'.format(prop_type))
104-
105100
# Get the property's value
106-
value = getattr(prop, 'get_{0}'.format(prop_type))()
101+
value = getattr(self.edict, 'get_prop_{0}'.format(
102+
prop_type))(self.properties[item].prop)
107103

108104
# Is the property a True/False property?
109105
if 'True' in self.properties[item]:
@@ -117,24 +113,24 @@ def _get_property(self, item):
117113
def _get_keyvalue(self, item):
118114
'''Gets the value of the given keyvalue'''
119115

116+
# Get the keyvalue's type
117+
kv_type = self.keyvalues[item].type
118+
120119
# Return the value of the given keyvalue
121-
return self.edict.GetKeyValue(item)
120+
return getattr(self.edict, 'get_keyvalue_{0}'.format(kv_type))(item)
122121

123122
def _get_offset(self, item):
124-
'''Gets teh value of the given offset'''
123+
'''Gets the value of the given offset'''
125124

126125
# Get the offset's type
127126
offset_type = self.offsets[item].type
128127

129-
# Is the offset's type a known type?
130-
if not hasattr(Binutils, 'GetLoc{0}'.format(offset_type)):
131-
132-
# If not a proper type, raise an error
133-
raise TypeError('Invalid offset type "{0}"'.format(offset_type))
128+
# Get the CPointer instance for the entity
129+
pointer = CPointer(self.pointer)
134130

135131
# Return the value of the offset
136-
return getattr(Binutils, 'GetLoc{0}'.format(offset_type))(
137-
self.pointer + self.offsets[item].offset)
132+
return getattr(pointer, 'get_{0}'.format(
133+
offset_type))(self.offsets[item].offset)
138134

139135
def _get_function(self, item):
140136
'''Calls a dynamic function'''
@@ -171,19 +167,6 @@ def __setattr__(self, attr, value):
171167
raise ValueError(
172168
'Invalid private attribute "{0}" given.'.format(attr))
173169

174-
# Loop through all instances
175-
# (used to set using edict/IPlayerInfo attributes)
176-
for instance in self.instances:
177-
178-
# Does the current instance contain the given attribute?
179-
if hasattr(instance, attr):
180-
181-
# Get the attribute's instance and use it to set the value
182-
setattr(instance, attr, value)
183-
184-
# No need to go further
185-
return
186-
187170
# Is the attribute a property of this entity?
188171
if attr in self.properties:
189172

@@ -205,63 +188,46 @@ def __setattr__(self, attr, value):
205188
# Was the attribute not found?
206189
else:
207190

208-
# If the attribute is not found, raise an error
209-
raise LookupError('Attribute "{0}" not found'.format(attr))
191+
# If the attribute is not found, just set the attribute
192+
super(BaseEntity, self).__setattr__(attr, value)
210193

211194
def _set_property(self, item, value):
212195
'''Sets the value of the given propery'''
213196

214-
# Get the property's instance
215-
prop = self.edict.get_prop(self.properties[item].prop)
216-
217197
# Get the property's type
218198
prop_type = self.properties[item].type
219199

220-
# Is the property's type a known type?
221-
if not hasattr(prop, 'set_{0}'.format(prop_type)):
222-
223-
# Raise an error
224-
raise TypeError('Invalid property type "{0}"'.format(prop_type))
225-
226200
# Is the property a True/False property?
227201
if 'True' in self.properties[item]:
228202

229203
# Get the exact value to set the property to
230204
value = self.properties[item][str(value)]
231205

232206
# Set the property's value
233-
getattr(prop, 'set_{0}'.format(prop_type))(value)
207+
getattr(prop, 'set_prop_{0}'.format(
208+
prop_type))(self.properties[item].prop, value)
234209

235210
def _set_keyvalue(self, item, value):
236211
'''Sets the value of the given keyvalue'''
237212

238213
# Get the keyvalue's type
239214
kv_type = self.keyvalues[item]
240215

241-
# Is the keyvalue's type a known type?
242-
if not hasattr(self.edict, 'SetKeyValue{0}'.format(kv_type)):
243-
244-
# Raise an error
245-
raise TypeError('Invalid keyvalue type "{0}"'.format(kv_type))
246-
247216
# Set the keyvalue's value
248-
getattr(self.edict, 'SetKeyValue{0}'.format(kv_type))(item, value)
217+
getattr(self.edict, 'set_keyvalue_{0}'.format(kv_type))(item, value)
249218

250219
def _set_offset(self, item, value):
251220
'''Sets the value of the given offset'''
252221

253222
# Get the offset's type
254223
offset_type = self.offsets[item].type
255224

256-
# Is the offset's type a known type?
257-
if not hasattr(Binutils, 'SetLoc{0}'.format(offset_type)):
258-
259-
# If not a proper type, raise an error
260-
raise TypeError('Invalid offset type "{0}"'.format(offset_type))
225+
# Get the CPointer instance for the entity
226+
pointer = CPointer(self.pointer)
261227

262228
# Set the offset's value
263-
getattr(Binutils, 'SetLoc{0}'.format(offset_type))(
264-
self.pointer + self.offsets[item].offset, value)
229+
getattr(pointer, 'set_{0}'.format(
230+
offset_type))(self.offsets[item].offset, value)
265231

266232
def get_color(self):
267233
'''Returns a 4 part tuple (RGBA) for the entity's color'''

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

Lines changed: 4 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -4,130 +4,11 @@
44
# >> IMPORTS
55
# =============================================================================
66
# Source.Python Imports
7-
from entity_c import BaseEntityHandle
8-
from entity_c import Edict
9-
from entity_c import index_of_pointer
10-
from public import public
7+
from conversions_c import *
118

129

1310
# =============================================================================
14-
# >> FUNCTIONS
11+
# >> ALL DECLARATION
1512
# =============================================================================
16-
@public
17-
def index_from_edict(edict):
18-
'''Returns an index from the given edict'''
19-
return edict.get_index()
20-
21-
22-
@public
23-
def index_from_basehandle(bhandle):
24-
'''Returns an index from the given BaseHandle instance'''
25-
return bhandle.get_entry_index()
26-
27-
28-
@public
29-
def index_from_inthandle(ihandle):
30-
'''Returns an index from the given handle in int form'''
31-
return index_from_basehandle(basehandle_from_inthandle(ihandle))
32-
33-
34-
@public
35-
def index_from_pointer(pointer):
36-
'''Returns an index from the given BaseEntity pointer'''
37-
return index_of_pointer(pointer)
38-
39-
40-
@public
41-
def edict_from_index(index):
42-
'''Returns an edict from the given index'''
43-
return Edict(index)
44-
45-
46-
@public
47-
def edict_from_basehandle(bhandle):
48-
'''Returns an edict from the given BaseHandle instance'''
49-
return edict_from_index(index_from_basehandle(bhandle))
50-
51-
52-
@public
53-
def edict_from_inthandle(ihandle):
54-
'''Returns an edict from the given handle in int form'''
55-
return edict_from_index(index_from_inthandle(ihandle))
56-
57-
58-
@public
59-
def edict_from_pointer(pointer):
60-
'''Returns an edict from the given BaseEntity pointer'''
61-
return edict_from_index(index_of_pointer(pointer))
62-
63-
64-
@public
65-
def basehandle_from_index(index):
66-
'''Returns a BaseHandle instance from the given index'''
67-
return basehandle_from_edict(edict_from_index(index))
68-
69-
70-
@public
71-
def basehandle_from_edict(edict):
72-
'''Returns a BaseHandle instance from the given edict'''
73-
return edict.get_networkable().get_entity_handle().get_ref_ehandle()
74-
75-
76-
@public
77-
def basehandle_from_inthandle(ihandle):
78-
'''Returns a BaseHandle instance from the given handle in int form'''
79-
return BaseEntityHandle(ihandle)
80-
81-
82-
@public
83-
def basehandle_from_pointer(pointer):
84-
'''Returns a BaseHandle instance from the given BaseEntity pointer'''
85-
return basehandle_from_index(index_of_pointer(pointer))
86-
87-
88-
@public
89-
def inthandle_from_index(index):
90-
'''Returns a handle in int form from the given index'''
91-
return inthandle_from_basehandle(basehandle_from_index(index))
92-
93-
94-
@public
95-
def inthandle_from_edict(edict):
96-
'''Returns a handle in int form from the given edict'''
97-
return inthandle_from_basehandle(basehandle_from_edict(edict))
98-
99-
100-
@public
101-
def inthandle_from_basehandle(bhandle):
102-
'''Returns a handle in int form from the given BaseHandle instance'''
103-
return bhandle.to_int()
104-
105-
106-
@public
107-
def inthandle_from_pointer(pointer):
108-
'''Returns a handle in int form from the given BaseEntity pointer'''
109-
return inthandle_from_index(index_of_pointer(pointer))
110-
111-
112-
@public
113-
def pointer_from_index(index):
114-
'''Returns a BaseEntity pointer from the given index'''
115-
return pointer_from_edict(edict_from_index(index))
116-
117-
118-
@public
119-
def pointer_from_edict(edict):
120-
'''Returns a BaseEntity pointer from the given edict'''
121-
return edict.get_unknown().get_base_entity()
122-
123-
124-
@public
125-
def pointer_from_basehandle(bhandle):
126-
'''Returns a BaseEntity pointer from the given BaseHandle instance'''
127-
return pointer_from_edict(edict_from_basehandle(bhandle))
128-
129-
130-
@public
131-
def pointer_from_inthandle(ihandle):
132-
'''Returns a BaseEntity pointer from the given handle in int form'''
133-
return pointer_from_edict(edict_from_inthandle(ihandle))
13+
__all__ = [x for x in globals() if not 'userid' in x and
14+
not 'playerinfo' in x and not x.startswith('__')]

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# >> IMPORTS
55
# =============================================================================
66
# Source.Python Imports
7-
from player_c import PlayerInfo
7+
from conversions_c import playerinfo_from_index
88
from core import GameEngine
99
from public import public
1010
# Entities
@@ -22,12 +22,15 @@
2222
class PlayerEntity(BaseEntity, _PlayerWeapons):
2323
'''Class used to interact directly with players'''
2424

25-
def __init__(self, index):
26-
'''Override the __init__ method to set the
25+
def __new__(cls, index):
26+
'''Override the __new__ method to set the
2727
"entities" attribute and set the PlayerInfo'''
2828

29+
# Get the "self" object using the super class' __new__
30+
self = super(PlayerEntity, cls).__new__(cls, index)
31+
2932
# Set the player's info attribute
30-
self._info = PlayerInfo(self.edict)
33+
self._info = playerinfo_from_index(self.index)
3134

3235
# Is the IPlayerInfo instance valid?
3336
if self.info is None:
@@ -38,6 +41,9 @@ def __init__(self, index):
3841
# Set the entities attribute
3942
self._entities = frozenset(['entity', 'player'])
4043

44+
# Return the instance
45+
return self
46+
4147
@property
4248
def info(self):
4349
'''Returns the player's IPlayerInfo instance'''

0 commit comments

Comments
 (0)