Skip to content

Commit 7747961

Browse files
committed
Fixed Entity.__setattr__ not properly iterating over the entity's server classes.
Fixed dynamic function wrappers from no longer being able to access the instance. Some various improvements to some redundant logics.
1 parent be36626 commit 7747961

3 files changed

Lines changed: 27 additions & 47 deletions

File tree

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

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,13 @@ def __setattr__(self, attr, value):
208208
return
209209

210210
# Loop through all of the entity's server classes
211-
for server_class in self.server_classes:
211+
for server_class, instance in self.server_classes.items():
212212

213213
# Does the current server class contain the given attribute?
214214
if hasattr(server_class, attr):
215215

216216
# Set the attribute's value
217-
setattr(server_class(self.pointer, wrap=True), attr, value)
217+
setattr(instance, attr, value)
218218

219219
# No need to go further
220220
return
@@ -1219,30 +1219,23 @@ def _on_entity_deleted(base_entity):
12191219
:param BaseEntity base_entity:
12201220
The removed entity.
12211221
"""
1222-
# Make sure the entity is networkable...
1223-
if not base_entity.is_networked():
1222+
try:
1223+
# Get the index of the entity...
1224+
index = base_entity.index
1225+
except ValueError:
12241226
return
12251227

1226-
# Get the index of the entity...
1227-
index = base_entity.index
1228-
12291228
# Cleanup the cache
12301229
for cls in _entity_classes:
12311230
cls.cache.pop(index, None)
12321231

1233-
# Was no delay registered for this entity?
1234-
if index not in _entity_delays:
1235-
return
1236-
1237-
# Loop through all delays...
1238-
for delay in _entity_delays[index]:
1239-
1240-
# Make sure the delay is still running...
1241-
if not delay.running:
1242-
continue
1232+
with suppress(KeyError):
1233+
# Loop through all delays...
1234+
for delay in _entity_delays[index]:
12431235

1244-
# Cancel the delay...
1245-
delay.cancel()
1236+
# Cancel the delay...
1237+
with suppress(ValueError):
1238+
delay.cancel()
12461239

1247-
# Remove the entity from the dictionary...
1248-
del _entity_delays[index]
1240+
# Remove the entity from the dictionary...
1241+
del _entity_delays[index]

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

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,9 @@ def __missing__(self, index):
6464

6565
def __delitem__(self, index):
6666
"""Remove the given index from the dictionary."""
67-
# Is the given index not in the dictionary?
68-
if index not in self:
69-
70-
# If so, no need to go further...
71-
return
72-
7367
# Remove the given index from the dictionary...
74-
super().__delitem__(index)
68+
with suppress(KeyError):
69+
super().__delitem__(index)
7570

7671
def from_inthandle(self, inthandle):
7772
"""Get an entity instance from an inthandle.
@@ -87,26 +82,18 @@ def on_automatically_removed(self, index):
8782

8883
def _on_entity_deleted(self, base_entity):
8984
"""OnEntityDeleted listener callback."""
90-
# Is the entity networkable?
91-
if not base_entity.is_networked():
92-
93-
# No, so skip it...
94-
return
95-
96-
# Get the index of the entity...
97-
index = base_entity.index
98-
99-
# Is the index not in the dictionary?
100-
if index not in self:
101-
102-
# No need to go further...
85+
try:
86+
# Get the index of the entity...
87+
index = base_entity.index
88+
except ValueError:
10389
return
10490

105-
# Call the deletion callback for the index...
106-
self.on_automatically_removed(index)
91+
with suppress(KeyError):
92+
# Call the deletion callback for the index...
93+
self.on_automatically_removed(index)
10794

108-
# Remove the index from the dictionary...
109-
super().__delitem__(index)
95+
# Remove the index from the dictionary...
96+
super().__delitem__(index)
11097

11198
def _unload_instance(self):
11299
"""Unregister our OnEntityDeleted listener."""

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# =============================================================================
88
# Python Imports
99
# WeakRef
10-
from weakref import ref
10+
from weakref import proxy
1111

1212
# Source.Python Imports
1313
# Core
@@ -105,7 +105,7 @@ def __init__(self, wrapped_self, wrapper):
105105
# Don't store a strong reference to the wrapped instance.
106106
# If we do, we will ends with a circular reference preventing itself,
107107
# along with everything it refers, to ever be garbage collected.
108-
self.wrapped_self = ref(wrapped_self)
108+
self.wrapped_self = proxy(wrapped_self)
109109

110110
def __call__(self, *args, **kwargs):
111111
return super().__call__(

0 commit comments

Comments
 (0)