How to run your code before/after functions (PreHook/PostHook)

From ESOUI Wiki

Jump to: navigation, search

There do exist some ZOs functions to "hook" into functions or eventhandlers like "OnMouseUp" of other controls/functions.
They are either Pre-Hooks (run before the original function and able to suppress the original code):
ZO_PreHook
ZO_PreHookHandler

Or Post-Hooks (run after the original function):
SecurePostHook
ZO_PostHookHandler

Information about "secure": If you isnert your code into e.g. the inventory and your code get's executed before the inventory is "securely" build,
it may raise error messages as the secure environment got tainted by your code.
That's why ZOs also added the SecurePostHook in order to apply your code after their code was run.

Please check this link for additional information:
[1]



You can also build your own PostHook function instead of using SecurePostHook:

for _, v in pairs(PLAYER_INVENTORY.inventories) do
		local listView = v.listView
		if listView and listView.dataTypes and listView.dataTypes[1] then
                        --Save the original functions (and all attached other PreHooks and PostHooks) code here in variable hookedFunctions  
			local hookedFunctions = listView.dataTypes[1].setupCallback
                        --Override the function now with your new one
			listView.dataTypes[1].setupCallback = function(rowControl, slot)
                                --First call the original function within your new function
				hookedFunctions(rowControl, slot)
                                --Then run your additional "PostHook" code 
				-->your code here
			end
		end
	end


The example above will read all inventories's listViews (ZO_Scrolllist) and use the setupCallback function (function run as the row of a scrolllist get's shown).
You "backup" the original setupCallback function of the listView in variable "hookedFunctions".
After that you overwrite the setupCallback function of the listview with your own new function "listView.dataTypes[1].setupCallback = function(rowControl, slot)".
Inside this function you first call your backuped original function (this will call the original source code and all attached ZO_PreHooks of other addons!).
After that you call your own posthook code, e.g. add marker textures to the rows.

The above example can be also created like this using the SecurePostHook function:

	for _, v in pairs(PLAYER_INVENTORY.inventories) do
		local listView = v.listView
		if listView and listView.dataTypes and listView.dataTypes[1] then
			SecurePostHook(listView.dataTypes[1], "setupCallback", function(rowControl, slot)
				AddResearchIndicatorToSlot(rowControl, GetItemLink)
			end)
		end
	end

Personal tools
Namespaces
Variants
Actions
Menu
Wiki
Toolbox