-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'what-im-currently-using' into 4.x
- Loading branch information
Showing
5 changed files
with
245 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
class_name ModLoaderHookChain | ||
extends RefCounted | ||
## Small class to keep the state of hook execution chains and move between mod hook calls.[br] | ||
## For examples, see [method ModLoaderMod.add_hook]. | ||
|
||
|
||
## The reference object is usually the [Node] that the vanilla script is attached to. [br] | ||
## If the hooked method is [code]static[/code], it will contain the [GDScript] itself. | ||
var reference_object: Object | ||
|
||
var _callbacks := [] | ||
var _callback_index := -1 | ||
|
||
|
||
const LOG_NAME := "ModLoaderHookChain" | ||
|
||
|
||
func _init(reference_object: Object, callbacks: Array) -> void: | ||
self.reference_object = reference_object | ||
_callbacks = callbacks | ||
_callback_index = callbacks.size() | ||
|
||
|
||
## Will execute the next mod hook callable or vanilla method and return the result.[br] | ||
## Make sure to call this method [i][color=orange]once[/color][/i] somewhere in the [param mod_callable] you pass to [method ModLoaderMod.add_hook]. [br] | ||
## | ||
## [br][b]Parameters:[/b][br] | ||
## - [param args] ([Array]): An array of all arguments passed into the vanilla function. [br] | ||
## | ||
## [br][b]Returns:[/b] [Variant][br][br] | ||
func execute_next(args := []) -> Variant: | ||
_callback_index -= 1 | ||
|
||
if not _callback_index >= 0: | ||
ModLoaderLog.fatal( | ||
"The hook chain index should never be negative. " + | ||
"A mod hook has called execute_next twice or ModLoaderHookChain was modified in an unsupported way.", | ||
LOG_NAME | ||
) | ||
return | ||
|
||
var callback = _callbacks[_callback_index] | ||
|
||
# Vanilla call is always at index 0 and needs to be called without the hook chain being passed | ||
if _callback_index == 0: | ||
return callback.callv(args) | ||
|
||
return callback.callv([self] + args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.