3
3
import unrealsdk
4
4
import functools
5
5
import weakref
6
- from typing import Any , Callable , Optional , Union
7
- from inspect import signature , Parameter
6
+ from inspect import Parameter , signature
7
+ from typing import Any , Callable , Tuple , Union
8
8
9
+ __all__ : Tuple [str , ...] = (
10
+ "AnyHook" ,
11
+ "Hook" ,
12
+ "HookFunction" ,
13
+ "HookMethod" ,
14
+ "RegisterHooks" ,
15
+ "RemoveHooks" ,
16
+ )
9
17
10
- _HookFunction = Callable [[unrealsdk .UObject , unrealsdk .UFunction , unrealsdk .FStruct ], Any ]
11
- _HookMethod = Callable [[object , unrealsdk .UObject , unrealsdk .UFunction , unrealsdk .FStruct ], Any ]
12
- _HookAny = Union [_HookFunction , _HookMethod ]
13
18
19
+ HookFunction = Callable [[unrealsdk .UObject , unrealsdk .UFunction , unrealsdk .FStruct ], Any ]
20
+ HookMethod = Callable [[object , unrealsdk .UObject , unrealsdk .UFunction , unrealsdk .FStruct ], Any ]
21
+ AnyHook = Union [HookFunction , HookMethod ]
14
22
15
- def Hook (target : str , name : str = "{0}.{1}" ) -> Callable [[_HookAny ], _HookAny ]:
23
+
24
+ def Hook (target : str , name : str = "{0}.{1}" ) -> Callable [[AnyHook ], AnyHook ]:
16
25
"""
17
26
A decorator for functions that should be invoked in response to an Unreal Engine method's
18
27
invokation.
@@ -47,7 +56,7 @@ def Hook(target: str, name: str = "{0}.{1}") -> Callable[[_HookAny], _HookAny]:
47
56
qualified name, separated by a ".". Argument `{1}` will contain the `id()` of the
48
57
function or mod instance.
49
58
"""
50
- def apply_hook (function : _HookAny ) -> _HookAny :
59
+ def apply_hook (function : AnyHook ) -> AnyHook :
51
60
# If the function has four parameters, it should be a method.
52
61
params = signature (function ).parameters
53
62
is_method = (len (params ) == 4 )
@@ -56,45 +65,48 @@ def apply_hook(function: _HookAny) -> _HookAny:
56
65
# initial setup on it now.
57
66
hook_targets = getattr (function , "HookTargets" , None )
58
67
if hook_targets is None :
59
- paramException = ValueError ("Hook functions must have the signature ([self,] caller: unrealsdk.UObject, function: unrealsdk.UFunction, params: unrealsdk.FStruct)" )
68
+ param_exception = ValueError (
69
+ "Hook functions must have the signature"
70
+ " ([self,] caller: unrealsdk.UObject, function: unrealsdk.UFunction, params: unrealsdk.FStruct)"
71
+ )
60
72
61
73
# If the function is an instance method, create a mutable list of the parameters and
62
74
# remove the `self` one, so we may check the remaining ones same as a non-method.
63
- params = list (params .values ())
75
+ param_list = list (params .values ())
64
76
if is_method :
65
- del params [0 ]
77
+ del param_list [0 ]
66
78
# If the function has neither 4 nor 3 parameters, it is invalid.
67
- elif len (params ) != 3 :
68
- raise paramException
79
+ elif len (param_list ) != 3 :
80
+ raise param_exception
69
81
# If the functions parameters do not accept positional arguments, it is invalid.
70
- for param in params :
82
+ for param in param_list :
71
83
if Parameter .POSITIONAL_ONLY != param .kind != Parameter .POSITIONAL_OR_KEYWORD :
72
- raise paramException
84
+ raise param_exception
73
85
74
86
# If the function is a method, store the name format string on it for formatting with
75
87
# future instances. If it's a simple function, format its name for use now.
76
- function .HookName = name if is_method else name .format (
88
+ function .HookName = name if is_method else name .format ( # type: ignore
77
89
f"{ function .__module__ } .{ function .__qualname__ } " , id (function )
78
90
)
79
91
80
92
# With the function now known as valid, create its set of targets.
81
- hook_targets = function .HookTargets = set ()
93
+ hook_targets = function .HookTargets = set () # type: ignore
82
94
83
95
hook_targets .add (target )
84
96
85
97
if not is_method :
86
- unrealsdk .RunHook (target , function .HookName , function )
98
+ unrealsdk .RunHook (target , function .HookName , function ) # type: ignore
87
99
88
100
return function
89
101
return apply_hook
90
102
91
103
92
- def _create_method_wrapper (obj_ref : weakref .ReferenceType [object ], obj_function : _HookMethod ) -> _HookFunction :
104
+ def _create_method_wrapper (obj_ref : weakref .ReferenceType [object ], obj_function : HookMethod ) -> HookFunction :
93
105
"""Return a "true" function for the given bound method, passable to `unrealsdk.RegisterHook`."""
94
106
@functools .wraps (obj_function )
95
107
def method_wrapper (caller : unrealsdk .UObject , function : unrealsdk .UFunction , params : unrealsdk .FStruct ) -> Any :
96
108
obj = obj_ref ()
97
- method = obj_function .__get__ (obj , type (obj ))
109
+ method = obj_function .__get__ (obj , type (obj )) # type: ignore
98
110
return method (caller , obj_function , params )
99
111
return method_wrapper
100
112
@@ -130,12 +142,12 @@ def RegisterHooks(obj: object) -> None:
130
142
setattr (obj , attribute_name , method_wrapper )
131
143
132
144
# Format the provided hook name.
133
- method_wrapper .HookName = function .HookName .format (
145
+ method_wrapper .HookName = function .HookName .format ( # type: ignore
134
146
f"{ function .__module__ } .{ function .__qualname__ } " , id (obj )
135
147
)
136
148
137
149
for target in hook_targets :
138
- unrealsdk .RunHook (target , method_wrapper .HookName , method_wrapper )
150
+ unrealsdk .RunHook (target , method_wrapper .HookName , method_wrapper ) # type: ignore
139
151
140
152
141
153
def RemoveHooks (obj : object ) -> None :
0 commit comments