Skip to content

feat: allow hooks to return a table with the function declared inside#20

Open
LordMidas wants to merge 1 commit into
mainfrom
feat-preserve-function-names
Open

feat: allow hooks to return a table with the function declared inside#20
LordMidas wants to merge 1 commit into
mainfrom
feat-preserve-function-names

Conversation

@LordMidas

@LordMidas LordMidas commented Apr 15, 2025

Copy link
Copy Markdown
Member

In response to this feedback on Discord, this allows preserving function names in the stack info when hooking and overwriting existing functions.

Testing

Paste this code to run a test:

::Hooks.__Mod.queue(function() {
	::Hooks.__Mod.hook("scripts/items/weapons/knife", function(q) {
		q.getID = @(__original) function()
		{
			::logInfo("Wrapped as function. Name: " + ::getstackinfos(1).func);
			return __original();
		}

		q.getName = @(__original) {
			function getName()
			{
				::logInfo("Wrapped as table. Name: " + ::getstackinfos(1).func);
				return __original();
			}
		}

		q.getIcon = @() function()
		{
			::logInfo("Wrapped as function without __original. Name: " + ::getstackinfos(1).func);
			return this.m.Icon;
		}

		q.getIconLarge = @() {
			function getIconLarge()
			{
				::logInfo("Wrapped as table without __original. Name: " + ::getstackinfos(1).func);
				return this.m.IconLarge;
			}
		}

		q.foo <- function()
		{
			::logInfo("New function as function. Name: " + ::getstackinfos(1).func);
		}

		q.bar <- {
			function bar()
			{
				::logInfo("New function as table. Name: " + ::getstackinfos(1).func);
			}
		}
	});
});

::Hooks.__Mod.queue(function() {
	local knife = ::new("scripts/items/weapons/knife");
	knife.getID();
	knife.getName();
	knife.getIcon();
	knife.getIconLarge();
	knife.foo();
	knife.bar();
}, ::Hooks.QueueBucket.AfterHooks)

Output:

04:40:30SQ
Wrapped as function. Name: unknown
04:40:30SQ
Wrapped as table. Name: getName
04:40:30SQ
Wrapped as function without __original. Name: unknown
04:40:30SQ
Wrapped as table without __original. Name: getIconLarge
04:40:30SQ
New function as function. Name: unknown
04:40:30SQ
New function as table. Name: bar

This allows preserving function names when hooking and overwriting existing functions.
@LordMidas LordMidas requested a review from Enduriel April 15, 2025 04:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant