Skip to content

Added instance_property support to entity managers. #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 21, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,16 @@ srv_check = False
offset_windows = 372


# TODO: Remove when outdated.
[instance_attribute]

[[assists]]
offset_windows = 4024
offset_linux = 4048
type = INT


[based_attribute]

# from memory import alloc
# from players.entity import Player
Expand All @@ -74,8 +82,8 @@ srv_check = False
# break
# print('Offset of Player.assists is:', offset)
[[assists]]
offset_windows = 4024
offset_linux = 4048
base = m_iFrags
offset = 4
type = INT


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ srv_check = False
on_rescue_zone_touch = OnRescueZoneTouch


# TODO: Remove when outdated.
[instance_attribute]

[[mvps]]
Expand All @@ -75,6 +76,24 @@ srv_check = False
type = STRING_ARRAY


[based_attribute]

[[mvps]]
base = m_bIsHoldingLookAtWeapon
offset = 11
type = INT

[[score]]
base = m_bIsHoldingLookAtWeapon
offset = 51
type = INT

[[clan_tag]]
base = m_flGroundAccelLinearFracLastTime
offset = 140
type = STRING_ARRAY


[property]

stamina = cslocaldata.m_flStamina
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
on_rescue_zone_touch = OnRescueZoneTouch


# TODO: Remove when outdated.
[instance_attribute]

[[mvps]]
Expand All @@ -63,6 +64,19 @@
type = STRING_ARRAY


[based_attribute]

[[mvps]]
base = cslocaldata.m_bPlayerDominatingMe.065
offset = 4
type = INT

[[clan_tag]]
base = m_flFlashDuration
offset = -28
type = STRING_ARRAY


[property]

stamina = cslocaldata.m_flStamina
Expand Down
36 changes: 35 additions & 1 deletion addons/source-python/packages/source-python/entities/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from memory import DataType
from memory import get_object_pointer
from memory import make_object
from memory.helpers import Key
from memory.helpers import Type
from memory.manager import CustomType
from memory.manager import TypeManager
Expand Down Expand Up @@ -332,6 +333,38 @@ def _get_server_class(self, class_name, datamap):
instance, name, offset, property_contents,
_supported_descriptor_types[desc.type])

# Loop through all based attributes
for name, data in manager_contents.get('based_attribute', {}).items():

# Resolve the method to register this attribute
method = getattr(self, data.get('method', 'instance_attribute'))

# Resolve the offset of this attribute
offset = Key.as_int(
self,
data.get('offset_' + PLATFORM, data.get('offset', 0))
)

# Resolve the base offset of this attribute
base = data.get('base_' + PLATFORM, data.get('base'))
try:
offset += instance.properties[base].offset
except KeyError:
raise NameError(
f'"{base}" is not a valid property ' +
f'for attribute "{class_name}.{name}".'
)

# Generate the attribute
attribute = method(
Key.as_attribute_type(self, data['type']),
offset,
data.get('doc')
)

# Assign the attribute to the instance
setattr(instance, name, attribute)

# Get a list of all properties for the current server class
properties = list(instance.properties)

Expand Down Expand Up @@ -485,7 +518,8 @@ def _add_property(
value = self.instance_attribute(prop_type, offset)

# Add the property to the properties dictionary
instance.properties[name] = EntityProperty(value, prop_type, networked)
instance.properties[name] = EntityProperty(
value, prop_type, networked, offset)

# Is the property not a named property?
if name not in contents:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@ class TypeDescriptionFlags(IntFlag):
class EntityProperty(object):
"""Class used to store property information for verification."""

def __init__(self, instance, prop_type, networked):
def __init__(self, instance, prop_type, networked, offset):
"""Store the base attributes on instantiation."""
self._instance = instance
self._prop_type = prop_type
self._networked = networked
self._offset = offset

@property
def instance(self):
Expand All @@ -123,3 +124,9 @@ def prop_type(self):
def networked(self):
"""Return whether the property is networked."""
return self._networked

@property
def offset(self):
"""Returns the offset of the property."""
return self._offset