@@ -50,20 +50,21 @@ var script_paths_hooked := {}
5050func process_begin () -> void :
5151 hashmap .clear ()
5252
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 :
5555 var start_time := Time .get_ticks_msec ()
5656 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 )
5858 ModLoaderLog .debug ("Finished processing script at path: %s in %s ms" % [path , Time .get_ticks_msec () - start_time ], LOG_NAME )
5959 return processed
6060
6161
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 :
6366 var current_script := load (path ) as GDScript
64-
6567 var source_code := current_script .source_code
66-
6768 var source_code_additions := ""
6869
6970 # 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:
7273 var method_store : Array [String ] = []
7374
7475 var getters_setters := collect_getters_and_setters (source_code )
75-
7676 var moddable_methods := current_script .get_script_method_list ().filter (
7777 is_func_moddable .bind (source_code , getters_setters )
7878 )
7979
8080 var methods_hooked := {}
81-
8281 for method in moddable_methods :
8382 if method .name in method_store :
8483 continue
8584
86- var prefix := "%s%s _" % [METHOD_PREFIX , class_prefix ]
85+ var full_prefix := "%s%s _" % [METHOD_PREFIX , class_prefix ]
8786
8887 # Check if the method name starts with the prefix added by `edit_vanilla_method()`.
8988 # This indicates that the method was previously processed, possibly by the export plugin.
9089 # 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 )
9392 methods_hooked [method_name_vanilla ] = true
9493 continue
95-
9694 # This ensures we avoid creating a hook for the 'imposter' method, which
9795 # is generated by `build_mod_hook_string()` and has the vanilla method name.
9896 if methods_hooked .has (method .name ):
9997 continue
10098
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+
101105 var type_string := get_return_type_string (method .return )
102106 var is_static := true if method .flags == METHOD_FLAG_STATIC + METHOD_FLAG_NORMAL else false
103107
@@ -152,7 +156,7 @@ func process_script(path: String, enable_hook_check := false) -> String:
152156 is_static ,
153157 is_async ,
154158 hook_id ,
155- METHOD_PREFIX + class_prefix ,
159+ full_prefix ,
156160 enable_hook_check
157161 )
158162
@@ -167,7 +171,7 @@ func process_script(path: String, enable_hook_check := false) -> String:
167171 source_code ,
168172 func_def ,
169173 func_body ,
170- METHOD_PREFIX + class_prefix
174+ full_prefix
171175 )
172176 source_code_additions += "\n %s " % mod_loader_hook_string
173177
@@ -327,15 +331,15 @@ func edit_vanilla_method(
327331) -> String :
328332 text = fix_method_super (method_name , func_body , text )
329333 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 ])
331335
332336 return text
333337
334338
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 :
336340 if engine_version_hex < ENGINE_VERSION_HEX_4_2_2 :
337341 return fix_method_super_before_4_2_2 (method_name , func_body , text )
338-
342+
339343 return regex_super_call .sub (
340344 text , "super.%s " % method_name ,
341345 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)
344348
345349# https://github.com/godotengine/godot/pull/86052
346350# Quote:
347- # When the end argument of RegEx.sub was used,
351+ # When the end argument of RegEx.sub was used,
348352# it would truncate the Subject String before even doing the substitution.
349353func fix_method_super_before_4_2_2 (method_name : String , func_body : RegExMatch , text : String ) -> String :
350354 var text_after_func_body_end := text .substr (func_body .get_end ())
351-
355+
352356 text = regex_super_call .sub (
353357 text , "super.%s " % method_name ,
354358 true , func_body .get_start (), func_body .get_end ()
355359 )
356-
360+
357361 text = text + text_after_func_body_end
358-
362+
359363 return text
360364
361365
@@ -397,10 +401,9 @@ static func build_mod_hook_string(
397401 return_string , await_string , method_prefix , method_name , method_arg_string_names_only
398402 ) if enable_hook_check else ""
399403
400-
401404 return """
402405{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}
404407""" .format ({
405408 "METHOD_PREFIX" : method_prefix ,
406409 "METHOD_NAME" : method_name ,
@@ -551,7 +554,7 @@ static func get_hook_check_else_string(
551554 method_name : String ,
552555 method_arg_string_names_only : String
553556) -> 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 (
555558 {
556559 "RETURN" : return_string ,
557560 "AWAIT" : await_string ,
0 commit comments