1111# Effects
1212from effects .base import TempEntity
1313from effects .templates import temp_entity_templates
14+ # Engines
15+ from engines .server import engine_server
1416# Filters
1517from filters .recipients import RecipientFilter
1618# Memory
1719from memory import get_virtual_function
1820from memory import make_object
1921from memory .hooks import HookType
22+ from memory .hooks import PreHook
23+ from memory .hooks import PostHook
2024
2125
2226# =============================================================================
@@ -42,42 +46,22 @@ def __init__(self, temp_entity_name):
4246 # Store the given temp entity name...
4347 self .name = temp_entity_name
4448
45- # Set the callback to None...
46- self ._callback = None
47-
48- try :
49- # Store the function to hook...
50- self .function = get_virtual_function (
51- temp_entity_templates [temp_entity_name ], 'Create' )
52- except NameError :
53- # Given name was invalid, set the function to None...
54- self .function = None
49+ # Get and store the temp entity template...
50+ self .template = temp_entity_templates [temp_entity_name ]
5551
56- # Re-raise the error ...
57- raise
52+ # Set the callback to None ...
53+ self . callback = None
5854
5955 def __call__ (self , callback ):
6056 """Store the callback and try initialize the hook."""
61- def _callback (stack_data , * args ):
62- """Called when the hooked method is called."""
63- # Get the temp entity instance...
64- temp_entity = make_object (TempEntity , stack_data [0 ])
65-
66- # Are we looking for that temp entity?
67- if temp_entity .name == self .name :
68-
69- # Call the registered callback...
70- return callback (temp_entity , make_object (
71- RecipientFilter , stack_data [1 ]))
72-
7357 # Store the callback...
74- self ._callback = _callback
58+ self .callback = callback
7559
7660 # Initialize the hook...
77- self .function .add_hook (self .hook_type , self . _callback )
61+ self .template .add_hook (self .hook_type , callback )
7862
7963 # Return the callback...
80- return _callback
64+ return callback
8165
8266 @property
8367 def hook_type (self ):
@@ -86,12 +70,7 @@ def hook_type(self):
8670
8771 def _unload_instance (self ):
8872 """Unload the hook."""
89- # Was no hook registered?
90- if self .function is None or self ._callback is None :
91- return
92-
93- # Unregister the hook...
94- self .function .remove_hook (self .hook_type , self ._callback )
73+ self .template .remove_hook (self .hook_type , self .callback )
9574
9675
9776class TempEntityPreHook (_TempEntityHook ):
@@ -104,3 +83,21 @@ class TempEntityPostHook(_TempEntityHook):
10483 """Decorator used to create temp entity post hooks that auto unload."""
10584
10685 hook_type = HookType .POST
86+
87+
88+ # =============================================================================
89+ # >> HOOKS
90+ # =============================================================================
91+ @PreHook (get_virtual_function (engine_server , 'PlaybackTempEntity' ))
92+ def pre_playback_temp_entity (stack_data ):
93+ """Handle pre hooks."""
94+ temp_entity = TempEntity (stack_data [3 ])
95+ return temp_entity .template .handle_hook (HookType .PRE , temp_entity ,
96+ make_object (RecipientFilter , stack_data [1 ]))
97+
98+ @PostHook (get_virtual_function (engine_server , 'PlaybackTempEntity' ))
99+ def post_playback_temp_entity (stack_data , return_value ):
100+ """Handle post hooks."""
101+ temp_entity = TempEntity (stack_data [3 ])
102+ return temp_entity .template .handle_hook (HookType .POST , temp_entity ,
103+ make_object (RecipientFilter , stack_data [1 ]))
0 commit comments