Skip to content

feat: add history tracking for hooks on specific keys in classes and m #26

Draft
LordMidas wants to merge 5 commits intomainfrom
feat-hooks-history
Draft

feat: add history tracking for hooks on specific keys in classes and m #26
LordMidas wants to merge 5 commits intomainfrom
feat-hooks-history

Conversation

@LordMidas
Copy link
Copy Markdown
Member

Each hook on a key in a class or its m table is now tracked. Mods can access this history and revert the state of that key back to a desired value.

All the operations below can be done on the m field as well, except MH_snipe which is only for functions in the class.

Consider the following scenario:

// Mod A
mod_A.hook("scripts/items/weapons/knife", function(q) {
	q.getID = @(__original) function()
	{
		::logInfo("Mod A's hook worked");
		return __original();
	}
});

// Mod B
mod_B.hook("scripts/items/weapons/knife", function(q) {
	q.getID = @(__original) function()
	{
		::logInfo("Mod B's hook worked");
		return __original();
	}
});

Now look at various outcomes:

knife.getID();
// output:
"Mod B's hook worked"
"Mod A's hook worked"
"weapon.knife" // the vanilla output
// Mod C
// Reverts getID back to its original declaration (the vanilla one in this case);
mod_C.hook("scripts/items/weapons/knife", function(q) {
	q.MH_revert("getID");
});

knife.getID()
// output:
"weapon.knife"
// Mod C
// Reverts getID back to how Mod_A hooked it
mod_C.hook("scripts/items/weapons/knife", function(q) {
	foreach (state in q.MH_getHistory("getID"))
	{
		// Many other checks are possible here e.g. Bucket, and Src of the hook	
		if (state.Mod.getID() == "mod_A")
		{
			q.MH_revert("getID", state.New);
			break;
		}
	}
});

knife.getID();
// output:
"Mod A's hook worked"
"weapon.knife"
// Mod C
// Removes Mod_A's hook from the hook stack. This means that the __original
// that is passed to the next hook will be the one from before Mod_A which in this
// case is the vanilla one.
mod_C.hook("scripts/items/weapons/knife", function(q) {
	foreach (i, state in q.MH_getHistory("getID"))
	{
		// Many other checks are possible here e.g. Bucket, and Src of the hook
		if (state.Mod.getID() == "mod_A")
		{
			q.MH_snipe("getID", i);
			break;
		}
	}
});

knife.getID();
// output:
"Mod B's hook worked"
"weapon.knife"

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