Skip to content

Commit 70f54ca

Browse files
authored
fix: 🐛 check if a method is already hooked (#493)
* fix: 🐛 check if a method is already hooked * fix: ✏️ fix typo * docs: 📝 better comment
1 parent 292d59f commit 70f54ca

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

addons/mod_loader/internal/mod_hook_packer.gd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ static func start() -> void:
5050

5151
var processed_source_code := hook_pre_processor.process_script_verbose(path)
5252

53+
# Skip writing to the zip if no new hooks were created for this script
54+
if not hook_pre_processor.script_paths_hooked.has(path):
55+
ModLoaderLog.debug("No new hooks were created in \"%s\", skipping writing to hook pack." % path, LOG_NAME)
56+
continue
57+
5358
zip_writer.start_file(path.trim_prefix("res://"))
5459
zip_writer.write_file(processed_source_code.to_utf8_buffer())
5560
zip_writer.close_file()

addons/mod_loader/internal/mod_hook_preprocessor.gd

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var regex_keyword_await := RegEx.create_from_string("\\bawait\\b")
3333

3434

3535
var hashmap := {}
36+
var script_paths_hooked := {}
3637

3738

3839
func process_begin() -> void:
@@ -65,10 +66,27 @@ func process_script(path: String, enable_hook_check := false) -> String:
6566
is_func_moddable.bind(source_code, getters_setters)
6667
)
6768

69+
var methods_hooked := {}
70+
6871
for method in moddable_methods:
6972
if method.name in method_store:
7073
continue
7174

75+
var prefix := "%s%s_" % [METHOD_PREFIX, class_prefix]
76+
77+
# Check if the method name starts with the prefix added by `edit_vanilla_method()`.
78+
# This indicates that the method was previously processed, possibly by the export plugin.
79+
# If so, store the method name (excluding the prefix) in `methods_hooked`.
80+
if method.name.begins_with(prefix):
81+
var method_name_vanilla: String = method.name.trim_prefix(prefix)
82+
methods_hooked[method_name_vanilla] = true
83+
continue
84+
85+
# This ensures we avoid creating a hook for the 'imposter' method, which
86+
# is generated by `build_mod_hook_string()` and has the vanilla method name.
87+
if methods_hooked.has(method.name):
88+
continue
89+
7290
var type_string := get_return_type_string(method.return)
7391
var is_static := true if method.flags == METHOD_FLAG_STATIC + METHOD_FLAG_NORMAL else false
7492

@@ -132,6 +150,8 @@ func process_script(path: String, enable_hook_check := false) -> String:
132150
)
133151
source_code_additions += "\n%s" % mod_loader_hook_string
134152

153+
script_paths_hooked[path] = true
154+
135155
# If we have some additions to the code, append them at the end
136156
if source_code_additions != "":
137157
source_code = "%s\n%s\n%s" % [source_code, MOD_LOADER_HOOKS_START_STRING, source_code_additions]

0 commit comments

Comments
 (0)