local y,w="\065\099\101\072\111\111\107-\051\046\048",9 local O,H=LibStub:NewLibrary(y,w)if not O then return end O.embeded=O.embeded or{}O.registry=O.registry or setmetatable({},{__index=function(y,w)y[w]={}return y[w]end})O.handlers=O.handlers or{}O.actives=O.actives or{}O.scripts=O.scripts or{}O.onceSecure=O.onceSecure or{}O.hooks=O.hooks or{}local C=O.registry local M=O.handlers local T=O.actives local I=O.scripts local b=O.onceSecure local t,z,x=pairs,next,type local e=string.format local W,X=assert,error local R,D=issecurevariable,hooksecurefunc local l=_G local Z,U,q local u={OnClick=true}local J={"\072\111\111\107";"\083\101\099\117\114\101\072\111\111\107","\072\111\111\107\083\099\114\105\112\116";"\083\101\099\117\114\101\072\111\111\107\083\099\114\105\112\116";"\085\110\104\111\111\107";"\085\110\104\111\111\107\065\108\108";"\073\115\072\111\111\107\101\100";"\082\097\119\072\111\111\107";"\082\097\119\072\111\111\107\083\099\114\105\112\116"}function O.Embed(w,y)for O,H in t(J)do y[H]=w[H]end w.embeded[y]=true y.hooks=y.hooks or{}return y end function O.OnEmbedDisable(w,y)y:UnhookAll()end function U(y,w,O,H,C)local M local I=x(w)=="\115\116\114\105\110\103"if C and not H then M=function(...)if T[M]then if I then y[w](y,...)else w(...)end end return O(...)end else M=function(...)if T[M]then if I then return y[w](y,...)else return w(...)end elseif not H then return O(...)end end end return M end function Z() end function q(y,w,O,H,t,z,q,J,g)if not H then H=O end W(not t or x(t)=="\098\111\111\108\101\097\110")W(not z or x(z)=="\098\111\111\108\101\097\110")W(not q or x(q)=="\098\111\111\108\101\097\110")W(not J or x(J)=="\098\111\111\108\101\097\110")W(g)if w and x(w)~="\116\097\098\108\101"then X(e("\037\115\058 \'\111\098\106\101\099\116\' - \110\105\108 \111\114 \116\097\098\108\101 \101\120\112\101\099\116\101\100 \103\111\116 \037\115",g,x(w)),3)end if x(O)~="\115\116\114\105\110\103"then X(e("\037\115\058 \'\109\101\116\104\111\100\' - \115\116\114\105\110\103 \101\120\112\101\099\116\101\100 \103\111\116 \037\115",g,x(O)),3)end if x(H)~="\115\116\114\105\110\103"and x(H)~="\102\117\110\099\116\105\111\110"then X(e("\037\115\058 \'\104\097\110\100\108\101\114\' - \110\105\108\044 \115\116\114\105\110\103\044 \111\114 \102\117\110\099\116\105\111\110 \101\120\112\101\099\116\101\100 \103\111\116 \037\115",g,x(H)),3)end if x(H)=="\115\116\114\105\110\103"and x(y[H])~="\102\117\110\099\116\105\111\110"then X(e("\037\115\058 \'\104\097\110\100\108\101\114\' - \072\097\110\100\108\101\114 \115\112\101\099\105\102\105\101\100 \100\111\101\115 \110\111\116 \101\120\105\115\116 \097\116 \115\101\108\102\091\104\097\110\100\108\101\114\093",g),3)end if t then if not w or not w.GetScript or not w:HasScript(O)then X(e("\037\115\058 \089\111\117 \099\097\110 \111\110\108\121 \104\111\111\107 \097 \115\099\114\105\112\116 \111\110 \097 \102\114\097\109\101 \111\098\106\101\099\116",g),3)end if not z and(w.IsProtected and(w:IsProtected()and u[O]))then X(e("\067\097\110\110\111\116 \104\111\111\107 \115\101\099\117\114\101 \115\099\114\105\112\116 \037\113\059 \085\115\101 \083\101\099\117\114\101\072\111\111\107\083\099\114\105\112\116\040\111\098\106\044 \109\101\116\104\111\100\044 \091\104\097\110\100\108\101\114\093\041 \105\110\115\116\101\097\100\046",O),3)end else local y if w then y=b[w]and b[w][O]or R(w,O)else y=b[O]or R(O)end if y then if J then if w then b[w]=b[w]or{}b[w][O]=true else b[O]=true end elseif not z then X(e("\037\115\058 \065\116\116\101\109\112\116 \116\111 \104\111\111\107 \115\101\099\117\114\101 \102\117\110\099\116\105\111\110 \037\115\046 \085\115\101 \096\083\101\099\117\114\101\072\111\111\107\' \111\114 \097\100\100 \096\116\114\117\101\' \116\111 \116\104\101 \097\114\103\117\109\101\110\116 \108\105\115\116 \116\111 \111\118\101\114\114\105\100\101\046",g,O),3)end end end local n if w then n=C[y][w]and C[y][w][O]else n=C[y][O]end if n then if T[n]then X(e("\065\116\116\101\109\112\116\105\110\103 \116\111 \114\101\104\111\111\107 \097\108\114\101\097\100\121 \097\099\116\105\118\101 \104\111\111\107 \037\115\046",O))end if M[n]==H then T[n]=true return elseif w then if y.hooks and y.hooks[w]then y.hooks[w][O]=nil end C[y][w][O]=nil else if y.hooks then y.hooks[O]=nil end C[y][O]=nil end M[n],T[n],I[n]=nil,nil,nil end local N if t then N=w:GetScript(O)or Z elseif w then N=w[O]else N=l[O]end if not N then X(e("\037\115\058 \065\116\116\101\109\112\116\105\110\103 \116\111 \104\111\111\107 \097 \110\111\110 \101\120\105\115\116\105\110\103 \116\097\114\103\101\116",g),3)end n=U(y,H,N,z,not(q or z))if w then y.hooks[w]=y.hooks[w]or{}C[y][w]=C[y][w]or{}C[y][w][O]=n if not z then y.hooks[w][O]=N end if t then if not z then w:SetScript(O,n)else w:HookScript(O,n)end else if not z then w[O]=n else D(w,O,n)end end else C[y][O]=n if not z then l[O]=n y.hooks[O]=N else D(O,n)end end T[n],M[n],I[n]=true,H,t and true or nil end function O.Hook(C,y,w,O,H)if x(y)=="\115\116\114\105\110\103"then w,O,H,y=y,w,O,nil end if O==true then O,H=nil,true end q(C,y,w,O,false,false,false,H or false,"\085\115\097\103\101\058 \072\111\111\107\040\091\111\098\106\101\099\116\093\044 \109\101\116\104\111\100\044 \091\104\097\110\100\108\101\114\093\044 \091\104\111\111\107\083\101\099\117\114\101\093\041")end function O.RawHook(C,y,w,O,H)if x(y)=="\115\116\114\105\110\103"then w,O,H,y=y,w,O,nil end if O==true then O,H=nil,true end q(C,y,w,O,false,false,true,H or false,"\085\115\097\103\101\058 \082\097\119\072\111\111\107\040\091\111\098\106\101\099\116\093\044 \109\101\116\104\111\100\044 \091\104\097\110\100\108\101\114\093\044 \091\104\111\111\107\083\101\099\117\114\101\093\041")end function O.SecureHook(H,y,w,O)if x(y)=="\115\116\114\105\110\103"then w,O,y=y,w,nil end q(H,y,w,O,false,true,false,false,"\085\115\097\103\101\058 \083\101\099\117\114\101\072\111\111\107\040\091\111\098\106\101\099\116\093\044 \109\101\116\104\111\100\044 \091\104\097\110\100\108\101\114\093\041")end function O.HookScript(H,y,w,O)q(H,y,w,O,true,false,false,false,"\085\115\097\103\101\058 \072\111\111\107\083\099\114\105\112\116\040\111\098\106\101\099\116\044 \109\101\116\104\111\100\044 \091\104\097\110\100\108\101\114\093\041")end function O.RawHookScript(H,y,w,O)q(H,y,w,O,true,false,true,false,"\085\115\097\103\101\058 \082\097\119\072\111\111\107\083\099\114\105\112\116\040\111\098\106\101\099\116\044 \109\101\116\104\111\100\044 \091\104\097\110\100\108\101\114\093\041")end function O.SecureHookScript(H,y,w,O)q(H,y,w,O,true,true,false,false,"\085\115\097\103\101\058 \083\101\099\117\114\101\072\111\111\107\083\099\114\105\112\116\040\111\098\106\101\099\116\044 \109\101\116\104\111\100\044 \091\104\097\110\100\108\101\114\093\041")end function O.Unhook(O,y,w)local H="\085\115\097\103\101\058 \085\110\104\111\111\107\040\091\111\098\106\093\044 \109\101\116\104\111\100\041"if x(y)=="\115\116\114\105\110\103"then w,y=y,nil end if y and x(y)~="\116\097\098\108\101"then X(e("\037\115\058 \'\111\098\106\' - \101\120\112\101\099\116\105\110\103 \110\105\108 \111\114 \116\097\098\108\101 \103\111\116 \037\115",H,x(y)),2)end if x(w)~="\115\116\114\105\110\103"then X(e("\037\115\058 \'\109\101\116\104\111\100\' - \101\120\112\101\116\105\110\103 \115\116\114\105\110\103 \103\111\116 \037\115",H,x(w)),2)end local b if y then b=C[O][y]and C[O][y][w]else b=C[O][w]end if not b or not T[b]then return false end T[b],M[b]=nil,nil if y then C[O][y][w]=nil C[O][y]=z(C[O][y])and C[O][y]or nil if not O.hooks[y]or not O.hooks[y][w]then return true end if I[b]and y:GetScript(w)==b then y:SetScript(w,O.hooks[y][w]~=Z and O.hooks[y][w]or nil)I[b]=nil elseif y and(O.hooks[y]and(O.hooks[y][w]and y[w]==b))then y[w]=O.hooks[y][w]end O.hooks[y][w]=nil O.hooks[y]=z(O.hooks[y])and O.hooks[y]or nil else C[O][w]=nil if not O.hooks[w]then return true end if O.hooks[w]and l[w]==b then l[w]=O.hooks[w]end O.hooks[w]=nil end return true end function O.UnhookAll(y)for w,H in t(C[y])do if x(w)=="\116\097\098\108\101"then for H in t(H)do O.Unhook(y,w,H)end else O.Unhook(y,w)end end end function O.IsHooked(O,y,w)if x(y)=="\115\116\114\105\110\103"then if C[O][y]and T[C[O][y]]then return true,M[C[O][y]]end else if C[O][y]and(C[O][y][w]and T[C[O][y][w]])then return true,M[C[O][y][w]]end end return false,nil end for y,w in t(O.embeded)do O:Embed(y)end
--- **AceHook-3.0** offers safe Hooking/Unhooking of functions, methods and frame scripts.
-- Using AceHook-3.0 is recommended when you need to unhook your hooks again, so the hook chain isn't broken
-- when you manually restore the original function.
--
-- **AceHook-3.0** can be embeded into your addon, either explicitly by calling AceHook:Embed(MyAddon) or by
-- specifying it as an embeded library in your AceAddon. All functions will be available on your addon object
-- and can be accessed directly, without having to explicitly call AceHook itself.\\
-- It is recommended to embed AceHook, otherwise you'll have to specify a custom `self` on all calls you
-- make into AceHook.
-- @class file
-- @name AceHook-3.0
-- @release $Id: AceHook-3.0.lua 1284 2022-09-25 09:15:30Z nevcairiel $
local ACEHOOK_MAJOR, ACEHOOK_MINOR = "AceHook-3.0", 9
local AceHook, oldminor = LibStub:NewLibrary(ACEHOOK_MAJOR, ACEHOOK_MINOR)

if not AceHook then return end -- No upgrade needed

AceHook.embeded = AceHook.embeded or {}
AceHook.registry = AceHook.registry or setmetatable({}, {__index = function(tbl, key) tbl[key] = {} return tbl[key] end })
AceHook.handlers = AceHook.handlers or {}
AceHook.actives = AceHook.actives or {}
AceHook.scripts = AceHook.scripts or {}
AceHook.onceSecure = AceHook.onceSecure or {}
AceHook.hooks = AceHook.hooks or {}

-- local upvalues
local registry = AceHook.registry
local handlers = AceHook.handlers
local actives = AceHook.actives
local scripts = AceHook.scripts
local onceSecure = AceHook.onceSecure

-- Lua APIs
local pairs, next, type = pairs, next, type
local format = string.format
local assert, error = assert, error

-- WoW APIs
local issecurevariable, hooksecurefunc = issecurevariable, hooksecurefunc
local _G = _G

-- functions for later definition
local donothing, createHook, hook

local protectedScripts = {
	OnClick = true,
}

-- upgrading of embeded is done at the bottom of the file

local mixins = {
	"Hook", "SecureHook",
	"HookScript", "SecureHookScript",
	"Unhook", "UnhookAll",
	"IsHooked",
	"RawHook", "RawHookScript"
}

-- AceHook:Embed( target )
-- target (object) - target object to embed AceHook in
--
-- Embeds AceEevent into the target object making the functions from the mixins list available on target:..
function AceHook:Embed( target )
	for k, v in pairs( mixins ) do
		target[v] = self[v]
	end
	self.embeded[target] = true
	-- inject the hooks table safely
	target.hooks = target.hooks or {}
	return target
end

-- AceHook:OnEmbedDisable( target )
-- target (object) - target object that is being disabled
--
-- Unhooks all hooks when the target disables.
-- this method should be called by the target manually or by an addon framework
function AceHook:OnEmbedDisable( target )
	target:UnhookAll()
end

function createHook(self, handler, orig, secure, failsafe)
	local uid
	local method = type(handler) == "string"
	if failsafe and not secure then
		-- failsafe hook creation
		uid = function(...)
			if actives[uid] then
				if method then
					self[handler](self, ...)
				else
					handler(...)
				end
			end
			return orig(...)
		end
		-- /failsafe hook
	else
		-- all other hooks
		uid = function(...)
			if actives[uid] then
				if method then
					return self[handler](self, ...)
				else
					return handler(...)
				end
			elseif not secure then -- backup on non secure
				return orig(...)
			end
		end
		-- /hook
	end
	return uid
end

function donothing() end

function hook(self, obj, method, handler, script, secure, raw, forceSecure, usage)
	if not handler then handler = method end

	-- These asserts make sure AceHooks's devs play by the rules.
	assert(not script or type(script) == "boolean")
	assert(not secure or type(secure) == "boolean")
	assert(not raw or type(raw) == "boolean")
	assert(not forceSecure or type(forceSecure) == "boolean")
	assert(usage)

	-- Error checking Battery!
	if obj and type(obj) ~= "table" then
		error(format("%s: 'object' - nil or table expected got %s", usage, type(obj)), 3)
	end
	if type(method) ~= "string" then
		error(format("%s: 'method' - string expected got %s", usage, type(method)), 3)
	end
	if type(handler) ~= "string" and type(handler) ~= "function" then
		error(format("%s: 'handler' - nil, string, or function expected got %s", usage, type(handler)), 3)
	end
	if type(handler) == "string" and type(self[handler]) ~= "function" then
		error(format("%s: 'handler' - Handler specified does not exist at self[handler]", usage), 3)
	end
	if script then
		if not obj or not obj.GetScript or not obj:HasScript(method) then
			error(format("%s: You can only hook a script on a frame object", usage), 3)
		end
		if not secure and obj.IsProtected and obj:IsProtected() and protectedScripts[method] then
			error(format("Cannot hook secure script %q; Use SecureHookScript(obj, method, [handler]) instead.", method), 3)
		end
	else
		local issecure
		if obj then
			issecure = onceSecure[obj] and onceSecure[obj][method] or issecurevariable(obj, method)
		else
			issecure = onceSecure[method] or issecurevariable(method)
		end
		if issecure then
			if forceSecure then
				if obj then
					onceSecure[obj] = onceSecure[obj] or {}
					onceSecure[obj][method] = true
				else
					onceSecure[method] = true
				end
			elseif not secure then
				error(format("%s: Attempt to hook secure function %s. Use `SecureHook' or add `true' to the argument list to override.", usage, method), 3)
			end
		end
	end

	local uid
	if obj then
		uid = registry[self][obj] and registry[self][obj][method]
	else
		uid = registry[self][method]
	end

	if uid then
		if actives[uid] then
			-- Only two sane choices exist here.  We either a) error 100% of the time or b) always unhook and then hook
			-- choice b would likely lead to odd debuging conditions or other mysteries so we're going with a.
			error(format("Attempting to rehook already active hook %s.", method))
		end

		if handlers[uid] == handler then -- turn on a decative hook, note enclosures break this ability, small memory leak
			actives[uid] = true
			return
		elseif obj then -- is there any reason not to call unhook instead of doing the following several lines?
			if self.hooks and self.hooks[obj] then
				self.hooks[obj][method] = nil
			end
			registry[self][obj][method] = nil
		else
			if self.hooks then
				self.hooks[method] = nil
			end
			registry[self][method] = nil
		end
		handlers[uid], actives[uid], scripts[uid] = nil, nil, nil
	end

	local orig
	if script then
		orig = obj:GetScript(method) or donothing
	elseif obj then
		orig = obj[method]
	else
		orig = _G[method]
	end

	if not orig then
		error(format("%s: Attempting to hook a non existing target", usage), 3)
	end

	uid = createHook(self, handler, orig, secure, not (raw or secure))

	if obj then
		self.hooks[obj] = self.hooks[obj] or {}
		registry[self][obj] = registry[self][obj] or {}
		registry[self][obj][method] = uid

		if not secure then
			self.hooks[obj][method] = orig
		end

		if script then
			if not secure then
				obj:SetScript(method, uid)
			else
				obj:HookScript(method, uid)
			end
		else
			if not secure then
				obj[method] = uid
			else
				hooksecurefunc(obj, method, uid)
			end
		end
	else
		registry[self][method] = uid

		if not secure then
			_G[method] = uid
			self.hooks[method] = orig
		else
			hooksecurefunc(method, uid)
		end
	end

	actives[uid], handlers[uid], scripts[uid] = true, handler, script and true or nil
end

--- Hook a function or a method on an object.
-- The hook created will be a "safe hook", that means that your handler will be called
-- before the hooked function ("Pre-Hook"), and you don't have to call the original function yourself,
-- however you cannot stop the execution of the function, or modify any of the arguments/return values.\\
-- This type of hook is typically used if you need to know if some function got called, and don't want to modify it.
-- @paramsig [object], method, [handler], [hookSecure]
-- @param object The object to hook a method from
-- @param method If object was specified, the name of the method, or the name of the function to hook.
-- @param handler The handler for the hook, a funcref or a method name. (Defaults to the name of the hooked function)
-- @param hookSecure If true, AceHook will allow hooking of secure functions.
-- @usage
-- -- create an addon with AceHook embeded
-- MyAddon = LibStub("AceAddon-3.0"):NewAddon("HookDemo", "AceHook-3.0")
--
-- function MyAddon:OnEnable()
--   -- Hook ActionButton_UpdateHotkeys, overwriting the secure status
--   self:Hook("ActionButton_UpdateHotkeys", true)
-- end
--
-- function MyAddon:ActionButton_UpdateHotkeys(button, type)
--   print(button:GetName() .. " is updating its HotKey")
-- end
function AceHook:Hook(object, method, handler, hookSecure)
	if type(object) == "string" then
		method, handler, hookSecure, object = object, method, handler, nil
	end

	if handler == true then
		handler, hookSecure = nil, true
	end

	hook(self, object, method, handler, false, false, false, hookSecure or false, "Usage: Hook([object], method, [handler], [hookSecure])")
end

--- RawHook a function or a method on an object.
-- The hook created will be a "raw hook", that means that your handler will completly replace
-- the original function, and your handler has to call the original function (or not, depending on your intentions).\\
-- The original function will be stored in `self.hooks[object][method]` or `self.hooks[functionName]` respectively.\\
-- This type of hook can be used for all purposes, and is usually the most common case when you need to modify arguments
-- or want to control execution of the original function.
-- @paramsig [object], method, [handler], [hookSecure]
-- @param object The object to hook a method from
-- @param method If object was specified, the name of the method, or the name of the function to hook.
-- @param handler The handler for the hook, a funcref or a method name. (Defaults to the name of the hooked function)
-- @param hookSecure If true, AceHook will allow hooking of secure functions.
-- @usage
-- -- create an addon with AceHook embeded
-- MyAddon = LibStub("AceAddon-3.0"):NewAddon("HookDemo", "AceHook-3.0")
--
-- function MyAddon:OnEnable()
--   -- Hook ActionButton_UpdateHotkeys, overwriting the secure status
--   self:RawHook("ActionButton_UpdateHotkeys", true)
-- end
--
-- function MyAddon:ActionButton_UpdateHotkeys(button, type)
--   if button:GetName() == "MyButton" then
--     -- do stuff here
--   else
--     self.hooks.ActionButton_UpdateHotkeys(button, type)
--   end
-- end
function AceHook:RawHook(object, method, handler, hookSecure)
	if type(object) == "string" then
		method, handler, hookSecure, object = object, method, handler, nil
	end

	if handler == true then
		handler, hookSecure = nil, true
	end

	hook(self, object, method, handler, false, false, true, hookSecure or false,  "Usage: RawHook([object], method, [handler], [hookSecure])")
end

--- SecureHook a function or a method on an object.
-- This function is a wrapper around the `hooksecurefunc` function in the WoW API. Using AceHook
-- extends the functionality of secure hooks, and adds the ability to unhook once the hook isn't
-- required anymore, or the addon is being disabled.\\
-- Secure Hooks should be used if the secure-status of the function is vital to its function,
-- and taint would block execution. Secure Hooks are always called after the original function was called
-- ("Post Hook"), and you cannot modify the arguments, return values or control the execution.
-- @paramsig [object], method, [handler]
-- @param object The object to hook a method from
-- @param method If object was specified, the name of the method, or the name of the function to hook.
-- @param handler The handler for the hook, a funcref or a method name. (Defaults to the name of the hooked function)
function AceHook:SecureHook(object, method, handler)
	if type(object) == "string" then
		method, handler, object = object, method, nil
	end

	hook(self, object, method, handler, false, true, false, false,  "Usage: SecureHook([object], method, [handler])")
end

--- Hook a script handler on a frame.
-- The hook created will be a "safe hook", that means that your handler will be called
-- before the hooked script ("Pre-Hook"), and you don't have to call the original function yourself,
-- however you cannot stop the execution of the function, or modify any of the arguments/return values.\\
-- This is the frame script equivalent of the :Hook safe-hook. It would typically be used to be notified
-- when a certain event happens to a frame.
-- @paramsig frame, script, [handler]
-- @param frame The Frame to hook the script on
-- @param script The script to hook
-- @param handler The handler for the hook, a funcref or a method name. (Defaults to the name of the hooked script)
-- @usage
-- -- create an addon with AceHook embeded
-- MyAddon = LibStub("AceAddon-3.0"):NewAddon("HookDemo", "AceHook-3.0")
--
-- function MyAddon:OnEnable()
--   -- Hook the OnShow of FriendsFrame
--   self:HookScript(FriendsFrame, "OnShow", "FriendsFrameOnShow")
-- end
--
-- function MyAddon:FriendsFrameOnShow(frame)
--   print("The FriendsFrame was shown!")
-- end
function AceHook:HookScript(frame, script, handler)
	hook(self, frame, script, handler, true, false, false, false,  "Usage: HookScript(object, method, [handler])")
end

--- RawHook a script handler on a frame.
-- The hook created will be a "raw hook", that means that your handler will completly replace
-- the original script, and your handler has to call the original script (or not, depending on your intentions).\\
-- The original script will be stored in `self.hooks[frame][script]`.\\
-- This type of hook can be used for all purposes, and is usually the most common case when you need to modify arguments
-- or want to control execution of the original script.
-- @paramsig frame, script, [handler]
-- @param frame The Frame to hook the script on
-- @param script The script to hook
-- @param handler The handler for the hook, a funcref or a method name. (Defaults to the name of the hooked script)
-- @usage
-- -- create an addon with AceHook embeded
-- MyAddon = LibStub("AceAddon-3.0"):NewAddon("HookDemo", "AceHook-3.0")
--
-- function MyAddon:OnEnable()
--   -- Hook the OnShow of FriendsFrame
--   self:RawHookScript(FriendsFrame, "OnShow", "FriendsFrameOnShow")
-- end
--
-- function MyAddon:FriendsFrameOnShow(frame)
--   -- Call the original function
--   self.hooks[frame].OnShow(frame)
--   -- Do our processing
--   -- .. stuff
-- end
function AceHook:RawHookScript(frame, script, handler)
	hook(self, frame, script, handler, true, false, true, false, "Usage: RawHookScript(object, method, [handler])")
end

--- SecureHook a script handler on a frame.
-- This function is a wrapper around the `frame:HookScript` function in the WoW API. Using AceHook
-- extends the functionality of secure hooks, and adds the ability to unhook once the hook isn't
-- required anymore, or the addon is being disabled.\\
-- Secure Hooks should be used if the secure-status of the function is vital to its function,
-- and taint would block execution. Secure Hooks are always called after the original function was called
-- ("Post Hook"), and you cannot modify the arguments, return values or control the execution.
-- @paramsig frame, script, [handler]
-- @param frame The Frame to hook the script on
-- @param script The script to hook
-- @param handler The handler for the hook, a funcref or a method name. (Defaults to the name of the hooked script)
function AceHook:SecureHookScript(frame, script, handler)
	hook(self, frame, script, handler, true, true, false, false, "Usage: SecureHookScript(object, method, [handler])")
end

--- Unhook from the specified function, method or script.
-- @paramsig [obj], method
-- @param obj The object or frame to unhook from
-- @param method The name of the method, function or script to unhook from.
function AceHook:Unhook(obj, method)
	local usage = "Usage: Unhook([obj], method)"
	if type(obj) == "string" then
		method, obj = obj, nil
	end

	if obj and type(obj) ~= "table" then
		error(format("%s: 'obj' - expecting nil or table got %s", usage, type(obj)), 2)
	end
	if type(method) ~= "string" then
		error(format("%s: 'method' - expeting string got %s", usage, type(method)), 2)
	end

	local uid
	if obj then
		uid = registry[self][obj] and registry[self][obj][method]
	else
		uid = registry[self][method]
	end

	if not uid or not actives[uid] then
		-- Declining to error on an unneeded unhook since the end effect is the same and this would just be annoying.
		return false
	end

	actives[uid], handlers[uid] = nil, nil

	if obj then
		registry[self][obj][method] = nil
		registry[self][obj] = next(registry[self][obj]) and registry[self][obj] or nil

		-- if the hook reference doesnt exist, then its a secure hook, just bail out and dont do any unhooking
		if not self.hooks[obj] or not self.hooks[obj][method] then return true end

		if scripts[uid] and obj:GetScript(method) == uid then  -- unhooks scripts
			obj:SetScript(method, self.hooks[obj][method] ~= donothing and self.hooks[obj][method] or nil)
			scripts[uid] = nil
		elseif obj and self.hooks[obj] and self.hooks[obj][method] and obj[method] == uid then -- unhooks methods
			obj[method] = self.hooks[obj][method]
		end

		self.hooks[obj][method] = nil
		self.hooks[obj] = next(self.hooks[obj]) and self.hooks[obj] or nil
	else
		registry[self][method] = nil

		-- if self.hooks[method] doesn't exist, then this is a SecureHook, just bail out
		if not self.hooks[method] then return true end

		if self.hooks[method] and _G[method] == uid then -- unhooks functions
			_G[method] = self.hooks[method]
		end

		self.hooks[method] = nil
	end
	return true
end

--- Unhook all existing hooks for this addon.
function AceHook:UnhookAll()
	for key, value in pairs(registry[self]) do
		if type(key) == "table" then
			for method in pairs(value) do
				AceHook.Unhook(self, key, method)
			end
		else
			AceHook.Unhook(self, key)
		end
	end
end

--- Check if the specific function, method or script is already hooked.
-- @paramsig [obj], method
-- @param obj The object or frame to unhook from
-- @param method The name of the method, function or script to unhook from.
function AceHook:IsHooked(obj, method)
	-- we don't check if registry[self] exists, this is done by evil magicks in the metatable
	if type(obj) == "string" then
		if registry[self][obj] and actives[registry[self][obj]] then
			return true, handlers[registry[self][obj]]
		end
	else
		if registry[self][obj] and registry[self][obj][method] and actives[registry[self][obj][method]] then
			return true, handlers[registry[self][obj][method]]
		end
	end

	return false, nil
end

--- Upgrade our old embeded
for target, v in pairs( AceHook.embeded ) do
	AceHook:Embed( target )
end
