WoW:API hooksecurefunc: Difference between revisions

m
Move page script moved page API hooksecurefunc to WoW:API hooksecurefunc without leaving a redirect
m (Move page script moved page API hooksecurefunc to WoW:API hooksecurefunc without leaving a redirect)
 
(4 intermediate revisions by 4 users not shown)
Line 11: Line 11:


==Example==
==Example==
local echoHook = function (...)
  hooksecurefunc("CastSpellByName", print); -- Hooks the global CastSpellByName
  local msg = "";
  hooksecurefunc(GameTooltip, "SetUnitBuff", print); -- Hooks GameTooltip.SetUnitBuff
  for k,v in pairs({...}) do msg = msg .. k .. "=[" .. tostring(v) .."] "; end;
  DEFAULT_CHAT_FRAME:AddMessage("[Echo] " .. msg);
end;
  hooksecurefunc("CastSpellByName", echoHook); -- Hooks the global CastSpellByName
  hooksecurefunc(GameTooltip, "SetUnitBuff", echoHook); -- Hooks GameTooltip.SetUnitBuff
===Result===
===Result===
Hooks CastSpellByName and GameTooltip.SetUnitBuff without compromising their secure status. When those functions are called, prints their argument list into the default chat frame.
Hooks CastSpellByName and GameTooltip.SetUnitBuff without compromising their secure status. When those functions are called, prints their argument list into the default chat frame.
Line 41: Line 36:
   hooksecurefunc("ToggleBackpack", function() print(1) end)
   hooksecurefunc("ToggleBackpack", function() print(1) end)
   hooksecurefunc("ToggleBackpack", function() print(2) end)
   hooksecurefunc("ToggleBackpack", function() print(2) end)
   ToggleBackpack()
   ToggleBackpack()
   > 1
   > 1
   > 2
   > 2
Also note that using hooksecurefunc() may not always produce the expected result. Keep in mind that it actually replaces the original function with a new one (the function reference changes).
For example, if you try to hook the global function "MacroFrame_OnHide" (after the macro frame has been displayed), your function will not be called when the macro frame is closed.
It's simply because in Blizzard_MacroUI.xml the OnHide function call is registered like this:
<OnHide function="MacroFrame_OnHide"/>
The function ID called when the frame is hidden is the former one, before you hooked "MacroFrame_OnHide".
The workaround is to hook a function called by "MacroFrame_OnHide":
hooksecurefunc(MacroPopupFrame, "Hide", MyFunction)
However, when you want to hook a frame's script handler, it's better to use [[API_Frame_HookScript|Frame:HookScript]] like this:
MacroPopupFrame:HookScript("OnHide", MyFunction) -- the client will call MacroFrame_OnHide(MacroPopupFrame) first followed by MyFunction(MacroPopupFrame)
Anonymous user