@@ -50,20 +50,21 @@ var script_paths_hooked := {}
50
50
func process_begin () -> void :
51
51
hashmap .clear ()
52
52
53
-
54
- func process_script_verbose (path : String , enable_hook_check := false ) -> String :
53
+ ## Calls [method process_script] with additional logging
54
+ func process_script_verbose (path : String , enable_hook_check := false , method_mask : Array [ String ] = [] ) -> String :
55
55
var start_time := Time .get_ticks_msec ()
56
56
ModLoaderLog .debug ("Start processing script at path: %s " % path , LOG_NAME )
57
- var processed := process_script (path , enable_hook_check )
57
+ var processed := process_script (path , enable_hook_check , method_mask )
58
58
ModLoaderLog .debug ("Finished processing script at path: %s in %s ms" % [path , Time .get_ticks_msec () - start_time ], LOG_NAME )
59
59
return processed
60
60
61
61
62
- func process_script (path : String , enable_hook_check := false ) -> String :
62
+ ## [param path]: File path to the script to be processed.[br]
63
+ ## [param enable_hook_check]: Adds a check that ModLoaderStore.any_mod_hooked is [code]true[/code] to the processed method, reducing hash checks.[br]
64
+ ## [param method_mask]: If provided, only methods in this [Array] will be processed.[br]
65
+ func process_script (path : String , enable_hook_check := false , method_mask : Array [String ] = []) -> String :
63
66
var current_script := load (path ) as GDScript
64
-
65
67
var source_code := current_script .source_code
66
-
67
68
var source_code_additions := ""
68
69
69
70
# We need to stop all vanilla methods from forming inheritance chains,
@@ -72,32 +73,35 @@ func process_script(path: String, enable_hook_check := false) -> String:
72
73
var method_store : Array [String ] = []
73
74
74
75
var getters_setters := collect_getters_and_setters (source_code )
75
-
76
76
var moddable_methods := current_script .get_script_method_list ().filter (
77
77
is_func_moddable .bind (source_code , getters_setters )
78
78
)
79
79
80
80
var methods_hooked := {}
81
-
82
81
for method in moddable_methods :
83
82
if method .name in method_store :
84
83
continue
85
84
86
- var prefix := "%s%s _" % [METHOD_PREFIX , class_prefix ]
85
+ var full_prefix := "%s%s _" % [METHOD_PREFIX , class_prefix ]
87
86
88
87
# Check if the method name starts with the prefix added by `edit_vanilla_method()`.
89
88
# This indicates that the method was previously processed, possibly by the export plugin.
90
89
# If so, store the method name (excluding the prefix) in `methods_hooked`.
91
- if method .name .begins_with (prefix ):
92
- var method_name_vanilla : String = method .name .trim_prefix (prefix )
90
+ if method .name .begins_with (full_prefix ):
91
+ var method_name_vanilla : String = method .name .trim_prefix (full_prefix )
93
92
methods_hooked [method_name_vanilla ] = true
94
93
continue
95
-
96
94
# This ensures we avoid creating a hook for the 'imposter' method, which
97
95
# is generated by `build_mod_hook_string()` and has the vanilla method name.
98
96
if methods_hooked .has (method .name ):
99
97
continue
100
98
99
+ # If a mask is provided, only methods with their name in the mask will be converted.
100
+ # Can't be filtered before the loop since it removes prefixed methods required by the previous check.
101
+ if not method_mask .is_empty ():
102
+ if not method .name in method_mask :
103
+ continue
104
+
101
105
var type_string := get_return_type_string (method .return )
102
106
var is_static := true if method .flags == METHOD_FLAG_STATIC + METHOD_FLAG_NORMAL else false
103
107
@@ -152,7 +156,7 @@ func process_script(path: String, enable_hook_check := false) -> String:
152
156
is_static ,
153
157
is_async ,
154
158
hook_id ,
155
- METHOD_PREFIX + class_prefix ,
159
+ full_prefix ,
156
160
enable_hook_check
157
161
)
158
162
@@ -167,7 +171,7 @@ func process_script(path: String, enable_hook_check := false) -> String:
167
171
source_code ,
168
172
func_def ,
169
173
func_body ,
170
- METHOD_PREFIX + class_prefix
174
+ full_prefix
171
175
)
172
176
source_code_additions += "\n %s " % mod_loader_hook_string
173
177
@@ -327,15 +331,15 @@ func edit_vanilla_method(
327
331
) -> String :
328
332
text = fix_method_super (method_name , func_body , text )
329
333
text = text .erase (func_def .get_start (), func_def .get_end () - func_def .get_start ())
330
- text = text .insert (func_def .get_start (), "func %s _ %s (" % [prefix , method_name ])
334
+ text = text .insert (func_def .get_start (), "func %s %s (" % [prefix , method_name ])
331
335
332
336
return text
333
337
334
338
335
- func fix_method_super (method_name : String , func_body : RegExMatch , text : String ) -> String :
339
+ func fix_method_super (method_name : String , func_body : RegExMatch , text : String ) -> String :
336
340
if engine_version_hex < ENGINE_VERSION_HEX_4_2_2 :
337
341
return fix_method_super_before_4_2_2 (method_name , func_body , text )
338
-
342
+
339
343
return regex_super_call .sub (
340
344
text , "super.%s " % method_name ,
341
345
true , func_body .get_start (), func_body .get_end ()
@@ -344,18 +348,18 @@ func fix_method_super(method_name: String, func_body: RegExMatch, text: String)
344
348
345
349
# https://github.com/godotengine/godot/pull/86052
346
350
# Quote:
347
- # When the end argument of RegEx.sub was used,
351
+ # When the end argument of RegEx.sub was used,
348
352
# it would truncate the Subject String before even doing the substitution.
349
353
func fix_method_super_before_4_2_2 (method_name : String , func_body : RegExMatch , text : String ) -> String :
350
354
var text_after_func_body_end := text .substr (func_body .get_end ())
351
-
355
+
352
356
text = regex_super_call .sub (
353
357
text , "super.%s " % method_name ,
354
358
true , func_body .get_start (), func_body .get_end ()
355
359
)
356
-
360
+
357
361
text = text + text_after_func_body_end
358
-
362
+
359
363
return text
360
364
361
365
@@ -397,10 +401,9 @@ static func build_mod_hook_string(
397
401
return_string , await_string , method_prefix , method_name , method_arg_string_names_only
398
402
) if enable_hook_check else ""
399
403
400
-
401
404
return """
402
405
{STATIC} func {METHOD_NAME} ({METHOD_PARAMS} ){RETURN_TYPE_STRING} :
403
- {HOOK_CHECK}{RETURN}{AWAIT} _ModLoaderHooks.call_hooks{ASYNC} ({METHOD_PREFIX} _ {METHOD_NAME} , [{METHOD_ARGS} ], {HOOK_ID} ){HOOK_CHECK_ELSE}
406
+ {HOOK_CHECK}{RETURN}{AWAIT} _ModLoaderHooks.call_hooks{ASYNC} ({METHOD_PREFIX}{METHOD_NAME} , [{METHOD_ARGS} ], {HOOK_ID} ){HOOK_CHECK_ELSE}
404
407
""" .format ({
405
408
"METHOD_PREFIX" : method_prefix ,
406
409
"METHOD_NAME" : method_name ,
@@ -551,7 +554,7 @@ static func get_hook_check_else_string(
551
554
method_name : String ,
552
555
method_arg_string_names_only : String
553
556
) -> String :
554
- return "\n\t else:\n\t\t {RETURN}{AWAIT}{METHOD_PREFIX} _ {METHOD_NAME} ({METHOD_ARGS} )" .format (
557
+ return "\n\t else:\n\t\t {RETURN}{AWAIT}{METHOD_PREFIX}{METHOD_NAME} ({METHOD_ARGS} )" .format (
555
558
{
556
559
"RETURN" : return_string ,
557
560
"AWAIT" : await_string ,
0 commit comments