Skip to content

Added entity instances caching. #291

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 8 commits into from
Nov 26, 2019
Merged
Prev Previous commit
Next Next commit
Updated docstrings, etc.
  • Loading branch information
jordanbriere committed Nov 25, 2019
commit d7cc213280b6520c40ed6ff2bbbf1752fe5ce7c8
17 changes: 11 additions & 6 deletions addons/source-python/packages/source-python/entities/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,24 @@
# =============================================================================
# >> CLASSES
# =============================================================================
class EntityCaching(BaseEntity.__class__):
class _EntityCaching(BaseEntity.__class__):
"""Metaclass used to cache entity instances."""

def __init__(cls, classname, bases, attributes):
"""Initializes the class."""
# New instances of this class will be cached in that dictionary
cls._cache = {}

# Listen for entity deletions for cleanup purposes
on_entity_deleted_listener_manager.register_listener(
cls.on_entity_deleted
cls._on_entity_deleted
)

# Unregister the listener when the class is being garbage collected
finalize(
cls,
on_entity_deleted_listener_manager.unregister_listener,
cls.on_entity_deleted
cls._on_entity_deleted
)

def __call__(cls, index, caching=True):
Expand Down Expand Up @@ -118,7 +121,7 @@ def __call__(cls, index, caching=True):
def cache(self):
return self._cache

def on_entity_deleted(cls, base_entity):
def _on_entity_deleted(cls, base_entity):
"""Called when an entity is deleted."""
if not base_entity.is_networked():
return
Expand All @@ -127,7 +130,7 @@ def on_entity_deleted(cls, base_entity):
cls.cache.pop(base_entity.index, None)


class Entity(BaseEntity, metaclass=EntityCaching):
class Entity(BaseEntity, metaclass=_EntityCaching):
"""Class used to interact directly with entities.

Beside the standard way of doing stuff via methods and properties this
Expand All @@ -141,11 +144,13 @@ class also provides dynamic attributes that depend on the entity that is
4. :attr:`keyvalues`
"""

def __init__(self, index):
def __init__(self, index, caching=True):
"""Initialize the Entity instance.

:param int index:
The entity index to wrap.
:param bool caching:
Whether to lookup the cache for an existing instance or not.
"""
# Initialize the object
super().__init__(index)
Expand Down
4 changes: 3 additions & 1 deletion addons/source-python/packages/source-python/players/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@
class Player(PlayerMixin, Entity):
"""Class used to interact directly with players."""

def __init__(self, index):
def __init__(self, index, caching=True):
"""Initialize the object.

:param int index:
A valid player index.
:param bool caching:
Whether to lookup the cache for an existing instance or not.
:raise ValueError:
Raised if the index is invalid.
"""
Expand Down
4 changes: 3 additions & 1 deletion addons/source-python/packages/source-python/weapons/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
class Weapon(WeaponMixin, Entity):
"""Allows easy usage of the weapon's attributes."""

def __init__(self, index):
def __init__(self, index, caching=True):
"""Initialize the object.

:param int index:
A valid weapon index.
:param bool caching:
Whether to lookup the cache for an existing instance or not.
:raise ValueError:
Raised if the index is invalid.
"""
Expand Down