Skip to content

Commit 1199e80

Browse files
authored
feat: disable hook check when not needed (#429)
* feat: disable hook check when not needed * feat: split off verbose hook preprocessing * space
1 parent 87780ba commit 1199e80

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

addons/mod_loader/_export_plugin/export_plugin.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ func _export_file(path: String, type: String, features: PackedStringArray) -> vo
2222
skip()
2323
add_file(
2424
path,
25-
hook_pre_processor.process_script(path).to_utf8_buffer(),
25+
hook_pre_processor.process_script(path, true).to_utf8_buffer(),
2626
false
2727
)

addons/mod_loader/internal/mod_hook_preprocessor.gd

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func process_begin() -> void:
3636
hashmap.clear()
3737

3838

39-
func process_script(path: String) -> String:
39+
func process_script(path: String, enable_hook_check := false) -> String:
4040
var start_time := Time.get_ticks_msec()
4141
ModLoaderLog.debug("Start processing script at path: %s" % path, LOG_NAME)
4242
var current_script := load(path) as GDScript
@@ -67,8 +67,8 @@ func process_script(path: String) -> String:
6767

6868
var hash_before := _ModLoaderHooks.get_hook_hash(path, method.name, true)
6969
var hash_after := _ModLoaderHooks.get_hook_hash(path, method.name, false)
70-
var hash_before_data := [path, method.name,true]
71-
var hash_after_data := [path, method.name,false]
70+
var hash_before_data := [path, method.name, true]
71+
var hash_after_data := [path, method.name, false]
7272
if hashmap.has(hash_before):
7373
push_error(HASH_COLLISION_ERROR%[hashmap[hash_before], hash_before_data])
7474
if hashmap.has(hash_after):
@@ -87,6 +87,7 @@ func process_script(path: String) -> String:
8787
hash_before,
8888
hash_after,
8989
METHOD_PREFIX + class_prefix,
90+
enable_hook_check
9091
)
9192

9293
# Store the method name
@@ -261,24 +262,24 @@ static func get_mod_loader_hook(
261262
return_prop_usage: int,
262263
is_static: bool,
263264
script_path: String,
264-
hash_before:int,
265-
hash_after:int,
266-
method_prefix := METHOD_PREFIX
265+
hash_before: int,
266+
hash_after: int,
267+
method_prefix := METHOD_PREFIX,
268+
enable_hook_check := false,
267269
) -> String:
268270
var type_string := " -> %s" % method_type if not method_type.is_empty() else ""
269271
var static_string := "static " if is_static else ""
270272
# Cannot use "self" inside a static function.
271273
var self_string := "null" if is_static else "self"
272274
var return_var := "var %s = " % "return_var" if not method_type.is_empty() or return_prop_usage == 131072 else ""
273275
var method_return := "return %s" % "return_var" if not method_type.is_empty() or return_prop_usage == 131072 else ""
276+
var hook_check := 'if ModLoaderStore.get("any_mod_hooked") and ModLoaderStore.any_mod_hooked:\n\t\t' if enable_hook_check else ""
274277

275278
return """
276279
{STATIC}func {METHOD_NAME}({METHOD_PARAMS}){RETURN_TYPE_STRING}:
277-
if ModLoaderStore.get("any_mod_hooked") and ModLoaderStore.any_mod_hooked:
278-
_ModLoaderHooks.call_hooks({SELF}, [{METHOD_ARGS}], {HOOK_ID_BEFORE})
280+
{HOOKS_CHECK}_ModLoaderHooks.call_hooks({SELF}, [{METHOD_ARGS}], {HOOK_ID_BEFORE})
279281
{METHOD_RETURN_VAR}{METHOD_PREFIX}_{METHOD_NAME}({METHOD_ARGS})
280-
if ModLoaderStore.get("any_mod_hooked") and ModLoaderStore.any_mod_hooked:
281-
_ModLoaderHooks.call_hooks({SELF}, [{METHOD_ARGS}], {HOOK_ID_AFTER})
282+
{HOOK_CHECK}_ModLoaderHooks.call_hooks({SELF}, [{METHOD_ARGS}], {HOOK_ID_AFTER})
282283
{METHOD_RETURN}""".format({
283284
"METHOD_PREFIX": method_prefix,
284285
"METHOD_NAME": method_name,
@@ -290,8 +291,9 @@ static func get_mod_loader_hook(
290291
"METHOD_RETURN": method_return,
291292
"STATIC": static_string,
292293
"SELF": self_string,
293-
"HOOK_ID_BEFORE" : hash_before,
294-
"HOOK_ID_AFTER" : hash_after,
294+
"HOOK_ID_BEFORE": hash_before,
295+
"HOOK_ID_AFTER": hash_after,
296+
"HOOK_CHECK": hook_check,
295297
})
296298

297299

0 commit comments

Comments
 (0)