Skip to content

Commit ffaf025

Browse files
committed
feat: 🚧 added @not-moddable option to exclude methods
improved performance by exiting early when nothing is hooked
1 parent 220bae9 commit ffaf025

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

addons/mod_loader/_export_plugin/export_plugin.gd

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func _export_file(path: String, type: String, features: PackedStringArray) -> vo
2222
if path.begins_with("res://addons") or path.begins_with("res://mods-unpacked"):
2323
return
2424

25-
if type != "GDScript":
25+
if type != "GDScript":
2626
return
2727

2828
var current_script := load(path) as GDScript
@@ -214,9 +214,11 @@ static func get_mod_loader_hook(
214214

215215
return """
216216
{%STATIC%}func {%METHOD_NAME%}({%METHOD_PARAMS%}){%RETURN_TYPE_STRING%}:
217-
ModLoaderMod.call_hooks({%SELF%}, [{%METHOD_ARGS%}], {%HOOK_ID_BEFORE%})
217+
if ModLoaderStore.any_mod_hooked:
218+
ModLoaderMod.call_hooks({%SELF%}, [{%METHOD_ARGS%}], {%HOOK_ID_BEFORE%})
218219
{%METHOD_RETURN_VAR%}{%METHOD_PREFIX%}_{%METHOD_NAME%}({%METHOD_ARGS%})
219-
ModLoaderMod.call_hooks({%SELF%}, [{%METHOD_ARGS%}], {%HOOK_ID_AFTER%})
220+
if ModLoaderStore.any_mod_hooked:
221+
ModLoaderMod.call_hooks({%SELF%}, [{%METHOD_ARGS%}], {%HOOK_ID_AFTER%})
220222
{%METHOD_RETURN%}
221223
""".format({
222224
"%METHOD_PREFIX%": method_prefix,
@@ -255,9 +257,14 @@ static func get_previous_line_to(text: String, index: int) -> String:
255257
return text.substr(start_index, end_index - start_index + 1)
256258

257259
static func is_func_moddable(method_start_idx, text) -> bool:
260+
var prevline = get_previous_line_to(text, method_start_idx)
261+
262+
if prevline.contains("@not-moddable"):
263+
return false
258264
if not REQUIRE_EXPLICIT_ADDITION:
259265
return true
260-
return get_previous_line_to(text, method_start_idx).contains("@moddable")
266+
267+
return prevline.contains("@moddable")
261268

262269
static func get_index_at_method_start(method_name: String, text: String) -> int:
263270
# Regular expression to match the function definition with arbitrary whitespace

addons/mod_loader/api/mod.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ static func set_modding_hooks(new_callable_stack: Dictionary) -> void:
1616

1717

1818
static func add_hook(mod_callable: Callable, script_path: String, method_name: String, is_before := false) -> void:
19+
ModLoaderStore.any_mod_hooked = true
1920
var hash = get_hook_hash(script_path,method_name,is_before)
2021
if not ModLoaderStore.modding_hooks.has(hash):
2122
ModLoaderStore.modding_hooks[hash] = []

addons/mod_loader/mod_loader_store.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const LOG_NAME = "ModLoader:Store"
3939
# }
4040
# }
4141
# }
42-
42+
var any_mod_hooked := false
4343
var modding_hooks := {}
4444

4545
# Order for mods to be loaded in, set by `get_load_order`

0 commit comments

Comments
 (0)