Skip to content

Commit abb6362

Browse files
authored
Update templates.py
Fixed TempEntity[Pre/Post]Hook decorators for engines that doesn't relies on CBaseTempEntity::Create. Added add_hook, remove_hook, handle_hook and hooks to the TempEntityTemplate class in order to offer a non-decorator way to manage temp entity creation hooks.
1 parent c3a3d36 commit abb6362

File tree

1 file changed

+86
-0
lines changed
  • addons/source-python/packages/source-python/effects

1 file changed

+86
-0
lines changed

addons/source-python/packages/source-python/effects/templates.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from memory import TYPE_SIZES
1919
from memory import get_size
2020
from memory.helpers import Type
21+
from memory.hooks import HookType
2122
from memory.manager import manager
2223
# Paths
2324
from paths import SP_DATA_PATH
@@ -73,6 +74,9 @@ def __init__(self, temp_entity):
7374
# Add the current table to the properties...
7475
self._add_properties(prop.data_table)
7576

77+
# Get a dictionary to store our hooks...
78+
self._hooks = {HookType.PRE: set(), HookType.POST: set()}
79+
7680
# Initialize the base class...
7781
super()._copy_base(temp_entity, self.size)
7882

@@ -159,6 +163,80 @@ def _get_type_size(type_name):
159163
# Raise an exception...
160164
raise ValueError('"{}" is not a supported type.'.format(type_name))
161165

166+
def add_hook(self, hook_type, callback):
167+
"""Register a hook for this temp entity.
168+
169+
:param HookType hook_type:
170+
The type of the hook to register.
171+
:param function callback:
172+
The callback function to register.
173+
"""
174+
# Get the set associated with the given hook type...
175+
hooks = self.hooks.get(hook_type, None)
176+
177+
# Was the given hook type invalid?
178+
if hooks is None:
179+
raise TypeError('The given hook type is invalid.')
180+
181+
# Is the given callback not callable?
182+
if not callable(callback):
183+
raise TypeError('The given callback is not callable.')
184+
185+
# Register the hook...
186+
self.hooks[hook_type].add(callback)
187+
188+
def remove_hook(self, hook_type, callback):
189+
"""Unregister a hook for this temp entity.
190+
191+
:param HookType hook_type:
192+
The type of the hook to unregister.
193+
:param function callback:
194+
The callback function to unregister.
195+
"""
196+
# Get the set associated with the given hook type...
197+
hooks = self.hooks.get(hook_type, None)
198+
199+
# Was the given hook type invalid?
200+
if hooks is None:
201+
raise TypeError('The given hook type is invalid.')
202+
203+
# Unregister the hook...
204+
self.hooks[hook_type].discard(callback)
205+
206+
def handle_hook(self, hook_type, temp_entity, recipient_filter):
207+
"""Call the registered callbacks.
208+
209+
:param HookType hook_type:
210+
The type of the hook to handle.
211+
:param TempEntity temp_entity:
212+
The TempEntity instance.
213+
:param RecipientFilter recipient_filter:
214+
The RecipientFilter instance.
215+
216+
:rtype: bool
217+
"""
218+
# Flag variable to determine whether or not any callback wants to
219+
# block the original call...
220+
block = False
221+
222+
# Loop through all registered hooks for this temp entity...
223+
for callback in self.hooks[hook_type]:
224+
225+
# Call the callback and store the value it returned...
226+
ret = callback(temp_entity, recipient_filter)
227+
228+
# Is the returned value not None and evaluate to False?
229+
if ret is not None and not ret:
230+
231+
# This callback wants to block the original call...
232+
block = True
233+
234+
# Does any callback wanted to block the original call?
235+
if block:
236+
237+
# Yes, so block it...
238+
return False
239+
162240
@property
163241
def aliases(self):
164242
"""Return the aliases of the temp entity.
@@ -167,6 +245,14 @@ def aliases(self):
167245
"""
168246
return self._aliases
169247

248+
@property
249+
def hooks(self):
250+
"""Return the registered hooks for this temp entity.
251+
252+
:rtype: dict
253+
"""
254+
return self._hooks
255+
170256
@property
171257
def properties(self):
172258
"""Return the properties data of the temp entity.

0 commit comments

Comments
 (0)