Zerotorescue@0: --- **AceHook-3.0** offers safe Hooking/Unhooking of functions, methods and frame scripts. Zerotorescue@0: -- Using AceHook-3.0 is recommended when you need to unhook your hooks again, so the hook chain isn't broken Zerotorescue@0: -- when you manually restore the original function. Zerotorescue@0: -- Zerotorescue@0: -- **AceHook-3.0** can be embeded into your addon, either explicitly by calling AceHook:Embed(MyAddon) or by Zerotorescue@0: -- specifying it as an embeded library in your AceAddon. All functions will be available on your addon object Zerotorescue@0: -- and can be accessed directly, without having to explicitly call AceHook itself.\\ Zerotorescue@0: -- It is recommended to embed AceHook, otherwise you'll have to specify a custom `self` on all calls you Zerotorescue@0: -- make into AceHook. Zerotorescue@0: -- @class file Zerotorescue@0: -- @name AceHook-3.0 Zerotorescue@0: -- @release $Id: AceHook-3.0.lua 877 2009-11-02 15:56:50Z nevcairiel $ Zerotorescue@0: local ACEHOOK_MAJOR, ACEHOOK_MINOR = "AceHook-3.0", 5 Zerotorescue@0: local AceHook, oldminor = LibStub:NewLibrary(ACEHOOK_MAJOR, ACEHOOK_MINOR) Zerotorescue@0: Zerotorescue@0: if not AceHook then return end -- No upgrade needed Zerotorescue@0: Zerotorescue@0: AceHook.embeded = AceHook.embeded or {} Zerotorescue@0: AceHook.registry = AceHook.registry or setmetatable({}, {__index = function(tbl, key) tbl[key] = {} return tbl[key] end }) Zerotorescue@0: AceHook.handlers = AceHook.handlers or {} Zerotorescue@0: AceHook.actives = AceHook.actives or {} Zerotorescue@0: AceHook.scripts = AceHook.scripts or {} Zerotorescue@0: AceHook.onceSecure = AceHook.onceSecure or {} Zerotorescue@0: AceHook.hooks = AceHook.hooks or {} Zerotorescue@0: Zerotorescue@0: -- local upvalues Zerotorescue@0: local registry = AceHook.registry Zerotorescue@0: local handlers = AceHook.handlers Zerotorescue@0: local actives = AceHook.actives Zerotorescue@0: local scripts = AceHook.scripts Zerotorescue@0: local onceSecure = AceHook.onceSecure Zerotorescue@0: Zerotorescue@0: -- Lua APIs Zerotorescue@0: local pairs, next, type = pairs, next, type Zerotorescue@0: local format = string.format Zerotorescue@0: local assert, error = assert, error Zerotorescue@0: Zerotorescue@0: -- WoW APIs Zerotorescue@0: local issecurevariable, hooksecurefunc = issecurevariable, hooksecurefunc Zerotorescue@0: local _G = _G Zerotorescue@0: Zerotorescue@0: -- functions for later definition Zerotorescue@0: local donothing, createHook, hook Zerotorescue@0: Zerotorescue@0: local protectedScripts = { Zerotorescue@0: OnClick = true, Zerotorescue@0: } Zerotorescue@0: Zerotorescue@0: -- upgrading of embeded is done at the bottom of the file Zerotorescue@0: Zerotorescue@0: local mixins = { Zerotorescue@0: "Hook", "SecureHook", Zerotorescue@0: "HookScript", "SecureHookScript", Zerotorescue@0: "Unhook", "UnhookAll", Zerotorescue@0: "IsHooked", Zerotorescue@0: "RawHook", "RawHookScript" Zerotorescue@0: } Zerotorescue@0: Zerotorescue@0: -- AceHook:Embed( target ) Zerotorescue@0: -- target (object) - target object to embed AceHook in Zerotorescue@0: -- Zerotorescue@0: -- Embeds AceEevent into the target object making the functions from the mixins list available on target:.. Zerotorescue@0: function AceHook:Embed( target ) Zerotorescue@0: for k, v in pairs( mixins ) do Zerotorescue@0: target[v] = self[v] Zerotorescue@0: end Zerotorescue@0: self.embeded[target] = true Zerotorescue@0: -- inject the hooks table safely Zerotorescue@0: target.hooks = target.hooks or {} Zerotorescue@0: return target Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: -- AceHook:OnEmbedDisable( target ) Zerotorescue@0: -- target (object) - target object that is being disabled Zerotorescue@0: -- Zerotorescue@0: -- Unhooks all hooks when the target disables. Zerotorescue@0: -- this method should be called by the target manually or by an addon framework Zerotorescue@0: function AceHook:OnEmbedDisable( target ) Zerotorescue@0: target:UnhookAll() Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: function createHook(self, handler, orig, secure, failsafe) Zerotorescue@0: local uid Zerotorescue@0: local method = type(handler) == "string" Zerotorescue@0: if failsafe and not secure then Zerotorescue@0: -- failsafe hook creation Zerotorescue@0: uid = function(...) Zerotorescue@0: if actives[uid] then Zerotorescue@0: if method then Zerotorescue@0: self[handler](self, ...) Zerotorescue@0: else Zerotorescue@0: handler(...) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: return orig(...) Zerotorescue@0: end Zerotorescue@0: -- /failsafe hook Zerotorescue@0: else Zerotorescue@0: -- all other hooks Zerotorescue@0: uid = function(...) Zerotorescue@0: if actives[uid] then Zerotorescue@0: if method then Zerotorescue@0: return self[handler](self, ...) Zerotorescue@0: else Zerotorescue@0: return handler(...) Zerotorescue@0: end Zerotorescue@0: elseif not secure then -- backup on non secure Zerotorescue@0: return orig(...) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: -- /hook Zerotorescue@0: end Zerotorescue@0: return uid Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: function donothing() end Zerotorescue@0: Zerotorescue@0: function hook(self, obj, method, handler, script, secure, raw, forceSecure, usage) Zerotorescue@0: if not handler then handler = method end Zerotorescue@0: Zerotorescue@0: -- These asserts make sure AceHooks's devs play by the rules. Zerotorescue@0: assert(not script or type(script) == "boolean") Zerotorescue@0: assert(not secure or type(secure) == "boolean") Zerotorescue@0: assert(not raw or type(raw) == "boolean") Zerotorescue@0: assert(not forceSecure or type(forceSecure) == "boolean") Zerotorescue@0: assert(usage) Zerotorescue@0: Zerotorescue@0: -- Error checking Battery! Zerotorescue@0: if obj and type(obj) ~= "table" then Zerotorescue@0: error(format("%s: 'object' - nil or table expected got %s", usage, type(obj)), 3) Zerotorescue@0: end Zerotorescue@0: if type(method) ~= "string" then Zerotorescue@0: error(format("%s: 'method' - string expected got %s", usage, type(method)), 3) Zerotorescue@0: end Zerotorescue@0: if type(handler) ~= "string" and type(handler) ~= "function" then Zerotorescue@0: error(format("%s: 'handler' - nil, string, or function expected got %s", usage, type(handler)), 3) Zerotorescue@0: end Zerotorescue@0: if type(handler) == "string" and type(self[handler]) ~= "function" then Zerotorescue@0: error(format("%s: 'handler' - Handler specified does not exist at self[handler]", usage), 3) Zerotorescue@0: end Zerotorescue@0: if script then Zerotorescue@0: if not secure and obj:IsProtected() and protectedScripts[method] then Zerotorescue@0: error(format("Cannot hook secure script %q; Use SecureHookScript(obj, method, [handler]) instead.", method), 3) Zerotorescue@0: end Zerotorescue@0: if not obj or not obj.GetScript or not obj:HasScript(method) then Zerotorescue@0: error(format("%s: You can only hook a script on a frame object", usage), 3) Zerotorescue@0: end Zerotorescue@0: else Zerotorescue@0: local issecure Zerotorescue@0: if obj then Zerotorescue@0: issecure = onceSecure[obj] and onceSecure[obj][method] or issecurevariable(obj, method) Zerotorescue@0: else Zerotorescue@0: issecure = onceSecure[method] or issecurevariable(method) Zerotorescue@0: end Zerotorescue@0: if issecure then Zerotorescue@0: if forceSecure then Zerotorescue@0: if obj then Zerotorescue@0: onceSecure[obj] = onceSecure[obj] or {} Zerotorescue@0: onceSecure[obj][method] = true Zerotorescue@0: else Zerotorescue@0: onceSecure[method] = true Zerotorescue@0: end Zerotorescue@0: elseif not secure then Zerotorescue@0: error(format("%s: Attempt to hook secure function %s. Use `SecureHook' or add `true' to the argument list to override.", usage, method), 3) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local uid Zerotorescue@0: if obj then Zerotorescue@0: uid = registry[self][obj] and registry[self][obj][method] Zerotorescue@0: else Zerotorescue@0: uid = registry[self][method] Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: if uid then Zerotorescue@0: if actives[uid] then Zerotorescue@0: -- Only two sane choices exist here. We either a) error 100% of the time or b) always unhook and then hook Zerotorescue@0: -- choice b would likely lead to odd debuging conditions or other mysteries so we're going with a. Zerotorescue@0: error(format("Attempting to rehook already active hook %s.", method)) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: if handlers[uid] == handler then -- turn on a decative hook, note enclosures break this ability, small memory leak Zerotorescue@0: actives[uid] = true Zerotorescue@0: return Zerotorescue@0: elseif obj then -- is there any reason not to call unhook instead of doing the following several lines? Zerotorescue@0: if self.hooks and self.hooks[obj] then Zerotorescue@0: self.hooks[obj][method] = nil Zerotorescue@0: end Zerotorescue@0: registry[self][obj][method] = nil Zerotorescue@0: else Zerotorescue@0: if self.hooks then Zerotorescue@0: self.hooks[method] = nil Zerotorescue@0: end Zerotorescue@0: registry[self][method] = nil Zerotorescue@0: end Zerotorescue@0: handlers[uid], actives[uid], scripts[uid] = nil, nil, nil Zerotorescue@0: uid = nil Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local orig Zerotorescue@0: if script then Zerotorescue@0: orig = obj:GetScript(method) or donothing Zerotorescue@0: elseif obj then Zerotorescue@0: orig = obj[method] Zerotorescue@0: else Zerotorescue@0: orig = _G[method] Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: if not orig then Zerotorescue@0: error(format("%s: Attempting to hook a non existing target", usage), 3) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: uid = createHook(self, handler, orig, secure, not (raw or secure)) Zerotorescue@0: Zerotorescue@0: if obj then Zerotorescue@0: self.hooks[obj] = self.hooks[obj] or {} Zerotorescue@0: registry[self][obj] = registry[self][obj] or {} Zerotorescue@0: registry[self][obj][method] = uid Zerotorescue@0: Zerotorescue@0: if not secure then Zerotorescue@0: self.hooks[obj][method] = orig Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: if script then Zerotorescue@0: -- If the script is empty before, HookScript will not work, so use SetScript instead Zerotorescue@0: -- This will make the hook insecure, but shouldnt matter, since it was empty before. Zerotorescue@0: -- It does not taint the full frame. Zerotorescue@0: if not secure or orig == donothing then Zerotorescue@0: obj:SetScript(method, uid) Zerotorescue@0: elseif secure then Zerotorescue@0: obj:HookScript(method, uid) Zerotorescue@0: end Zerotorescue@0: else Zerotorescue@0: if not secure then Zerotorescue@0: obj[method] = uid Zerotorescue@0: else Zerotorescue@0: hooksecurefunc(obj, method, uid) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: else Zerotorescue@0: registry[self][method] = uid Zerotorescue@0: Zerotorescue@0: if not secure then Zerotorescue@0: _G[method] = uid Zerotorescue@0: self.hooks[method] = orig Zerotorescue@0: else Zerotorescue@0: hooksecurefunc(method, uid) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: actives[uid], handlers[uid], scripts[uid] = true, handler, script and true or nil Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --- Hook a function or a method on an object. Zerotorescue@0: -- The hook created will be a "safe hook", that means that your handler will be called Zerotorescue@0: -- before the hooked function ("Pre-Hook"), and you don't have to call the original function yourself, Zerotorescue@0: -- however you cannot stop the execution of the function, or modify any of the arguments/return values.\\ Zerotorescue@0: -- This type of hook is typically used if you need to know if some function got called, and don't want to modify it. Zerotorescue@0: -- @paramsig [object], method, [handler], [hookSecure] Zerotorescue@0: -- @param object The object to hook a method from Zerotorescue@0: -- @param method If object was specified, the name of the method, or the name of the function to hook. Zerotorescue@0: -- @param handler The handler for the hook, a funcref or a method name. (Defaults to the name of the hooked function) Zerotorescue@0: -- @param hookSecure If true, AceHook will allow hooking of secure functions. Zerotorescue@0: -- @usage Zerotorescue@0: -- -- create an addon with AceHook embeded Zerotorescue@0: -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("HookDemo", "AceHook-3.0") Zerotorescue@0: -- Zerotorescue@0: -- function MyAddon:OnEnable() Zerotorescue@0: -- -- Hook ActionButton_UpdateHotkeys, overwriting the secure status Zerotorescue@0: -- self:Hook("ActionButton_UpdateHotkeys", true) Zerotorescue@0: -- end Zerotorescue@0: -- Zerotorescue@0: -- function MyAddon:ActionButton_UpdateHotkeys(button, type) Zerotorescue@0: -- print(button:GetName() .. " is updating its HotKey") Zerotorescue@0: -- end Zerotorescue@0: function AceHook:Hook(object, method, handler, hookSecure) Zerotorescue@0: if type(object) == "string" then Zerotorescue@0: method, handler, hookSecure, object = object, method, handler, nil Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: if handler == true then Zerotorescue@0: handler, hookSecure = nil, true Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: hook(self, object, method, handler, false, false, false, hookSecure or false, "Usage: Hook([object], method, [handler], [hookSecure])") Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --- RawHook a function or a method on an object. Zerotorescue@0: -- The hook created will be a "raw hook", that means that your handler will completly replace Zerotorescue@0: -- the original function, and your handler has to call the original function (or not, depending on your intentions).\\ Zerotorescue@0: -- The original function will be stored in `self.hooks[object][method]` or `self.hooks[functionName]` respectively.\\ Zerotorescue@0: -- This type of hook can be used for all purposes, and is usually the most common case when you need to modify arguments Zerotorescue@0: -- or want to control execution of the original function. Zerotorescue@0: -- @paramsig [object], method, [handler], [hookSecure] Zerotorescue@0: -- @param object The object to hook a method from Zerotorescue@0: -- @param method If object was specified, the name of the method, or the name of the function to hook. Zerotorescue@0: -- @param handler The handler for the hook, a funcref or a method name. (Defaults to the name of the hooked function) Zerotorescue@0: -- @param hookSecure If true, AceHook will allow hooking of secure functions. Zerotorescue@0: -- @usage Zerotorescue@0: -- -- create an addon with AceHook embeded Zerotorescue@0: -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("HookDemo", "AceHook-3.0") Zerotorescue@0: -- Zerotorescue@0: -- function MyAddon:OnEnable() Zerotorescue@0: -- -- Hook ActionButton_UpdateHotkeys, overwriting the secure status Zerotorescue@0: -- self:RawHook("ActionButton_UpdateHotkeys", true) Zerotorescue@0: -- end Zerotorescue@0: -- Zerotorescue@0: -- function MyAddon:ActionButton_UpdateHotkeys(button, type) Zerotorescue@0: -- if button:GetName() == "MyButton" then Zerotorescue@0: -- -- do stuff here Zerotorescue@0: -- else Zerotorescue@0: -- self.hooks.ActionButton_UpdateHotkeys(button, type) Zerotorescue@0: -- end Zerotorescue@0: -- end Zerotorescue@0: function AceHook:RawHook(object, method, handler, hookSecure) Zerotorescue@0: if type(object) == "string" then Zerotorescue@0: method, handler, hookSecure, object = object, method, handler, nil Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: if handler == true then Zerotorescue@0: handler, hookSecure = nil, true Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: hook(self, object, method, handler, false, false, true, hookSecure or false, "Usage: RawHook([object], method, [handler], [hookSecure])") Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --- SecureHook a function or a method on an object. Zerotorescue@0: -- This function is a wrapper around the `hooksecurefunc` function in the WoW API. Using AceHook Zerotorescue@0: -- extends the functionality of secure hooks, and adds the ability to unhook once the hook isn't Zerotorescue@0: -- required anymore, or the addon is being disabled.\\ Zerotorescue@0: -- Secure Hooks should be used if the secure-status of the function is vital to its function, Zerotorescue@0: -- and taint would block execution. Secure Hooks are always called after the original function was called Zerotorescue@0: -- ("Post Hook"), and you cannot modify the arguments, return values or control the execution. Zerotorescue@0: -- @paramsig [object], method, [handler] Zerotorescue@0: -- @param object The object to hook a method from Zerotorescue@0: -- @param method If object was specified, the name of the method, or the name of the function to hook. Zerotorescue@0: -- @param handler The handler for the hook, a funcref or a method name. (Defaults to the name of the hooked function) Zerotorescue@0: function AceHook:SecureHook(object, method, handler) Zerotorescue@0: if type(object) == "string" then Zerotorescue@0: method, handler, object = object, method, nil Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: hook(self, object, method, handler, false, true, false, false, "Usage: SecureHook([object], method, [handler])") Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --- Hook a script handler on a frame. Zerotorescue@0: -- The hook created will be a "safe hook", that means that your handler will be called Zerotorescue@0: -- before the hooked script ("Pre-Hook"), and you don't have to call the original function yourself, Zerotorescue@0: -- however you cannot stop the execution of the function, or modify any of the arguments/return values.\\ Zerotorescue@0: -- This is the frame script equivalent of the :Hook safe-hook. It would typically be used to be notified Zerotorescue@0: -- when a certain event happens to a frame. Zerotorescue@0: -- @paramsig frame, script, [handler] Zerotorescue@0: -- @param frame The Frame to hook the script on Zerotorescue@0: -- @param script The script to hook Zerotorescue@0: -- @param handler The handler for the hook, a funcref or a method name. (Defaults to the name of the hooked script) Zerotorescue@0: -- @usage Zerotorescue@0: -- -- create an addon with AceHook embeded Zerotorescue@0: -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("HookDemo", "AceHook-3.0") Zerotorescue@0: -- Zerotorescue@0: -- function MyAddon:OnEnable() Zerotorescue@0: -- -- Hook the OnShow of FriendsFrame Zerotorescue@0: -- self:HookScript(FriendsFrame, "OnShow", "FriendsFrameOnShow") Zerotorescue@0: -- end Zerotorescue@0: -- Zerotorescue@0: -- function MyAddon:FriendsFrameOnShow(frame) Zerotorescue@0: -- print("The FriendsFrame was shown!") Zerotorescue@0: -- end Zerotorescue@0: function AceHook:HookScript(frame, script, handler) Zerotorescue@0: hook(self, frame, script, handler, true, false, false, false, "Usage: HookScript(object, method, [handler])") Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --- RawHook a script handler on a frame. Zerotorescue@0: -- The hook created will be a "raw hook", that means that your handler will completly replace Zerotorescue@0: -- the original script, and your handler has to call the original script (or not, depending on your intentions).\\ Zerotorescue@0: -- The original script will be stored in `self.hooks[frame][script]`.\\ Zerotorescue@0: -- This type of hook can be used for all purposes, and is usually the most common case when you need to modify arguments Zerotorescue@0: -- or want to control execution of the original script. Zerotorescue@0: -- @paramsig frame, script, [handler] Zerotorescue@0: -- @param frame The Frame to hook the script on Zerotorescue@0: -- @param script The script to hook Zerotorescue@0: -- @param handler The handler for the hook, a funcref or a method name. (Defaults to the name of the hooked script) Zerotorescue@0: -- @usage Zerotorescue@0: -- -- create an addon with AceHook embeded Zerotorescue@0: -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("HookDemo", "AceHook-3.0") Zerotorescue@0: -- Zerotorescue@0: -- function MyAddon:OnEnable() Zerotorescue@0: -- -- Hook the OnShow of FriendsFrame Zerotorescue@0: -- self:RawHookScript(FriendsFrame, "OnShow", "FriendsFrameOnShow") Zerotorescue@0: -- end Zerotorescue@0: -- Zerotorescue@0: -- function MyAddon:FriendsFrameOnShow(frame) Zerotorescue@0: -- -- Call the original function Zerotorescue@0: -- self.hooks[frame].OnShow(frame) Zerotorescue@0: -- -- Do our processing Zerotorescue@0: -- -- .. stuff Zerotorescue@0: -- end Zerotorescue@0: function AceHook:RawHookScript(frame, script, handler) Zerotorescue@0: hook(self, frame, script, handler, true, false, true, false, "Usage: RawHookScript(object, method, [handler])") Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --- SecureHook a script handler on a frame. Zerotorescue@0: -- This function is a wrapper around the `frame:HookScript` function in the WoW API. Using AceHook Zerotorescue@0: -- extends the functionality of secure hooks, and adds the ability to unhook once the hook isn't Zerotorescue@0: -- required anymore, or the addon is being disabled.\\ Zerotorescue@0: -- Secure Hooks should be used if the secure-status of the function is vital to its function, Zerotorescue@0: -- and taint would block execution. Secure Hooks are always called after the original function was called Zerotorescue@0: -- ("Post Hook"), and you cannot modify the arguments, return values or control the execution. Zerotorescue@0: -- @paramsig frame, script, [handler] Zerotorescue@0: -- @param frame The Frame to hook the script on Zerotorescue@0: -- @param script The script to hook Zerotorescue@0: -- @param handler The handler for the hook, a funcref or a method name. (Defaults to the name of the hooked script) Zerotorescue@0: function AceHook:SecureHookScript(frame, script, handler) Zerotorescue@0: hook(self, frame, script, handler, true, true, false, false, "Usage: SecureHookScript(object, method, [handler])") Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --- Unhook from the specified function, method or script. Zerotorescue@0: -- @paramsig [obj], method Zerotorescue@0: -- @param obj The object or frame to unhook from Zerotorescue@0: -- @param method The name of the method, function or script to unhook from. Zerotorescue@0: function AceHook:Unhook(obj, method) Zerotorescue@0: local usage = "Usage: Unhook([obj], method)" Zerotorescue@0: if type(obj) == "string" then Zerotorescue@0: method, obj = obj, nil Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: if obj and type(obj) ~= "table" then Zerotorescue@0: error(format("%s: 'obj' - expecting nil or table got %s", usage, type(obj)), 2) Zerotorescue@0: end Zerotorescue@0: if type(method) ~= "string" then Zerotorescue@0: error(format("%s: 'method' - expeting string got %s", usage, type(method)), 2) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local uid Zerotorescue@0: if obj then Zerotorescue@0: uid = registry[self][obj] and registry[self][obj][method] Zerotorescue@0: else Zerotorescue@0: uid = registry[self][method] Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: if not uid or not actives[uid] then Zerotorescue@0: -- Declining to error on an unneeded unhook since the end effect is the same and this would just be annoying. Zerotorescue@0: return false Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: actives[uid], handlers[uid] = nil, nil Zerotorescue@0: Zerotorescue@0: if obj then Zerotorescue@0: registry[self][obj][method] = nil Zerotorescue@0: registry[self][obj] = next(registry[self][obj]) and registry[self][obj] or nil Zerotorescue@0: Zerotorescue@0: -- if the hook reference doesnt exist, then its a secure hook, just bail out and dont do any unhooking Zerotorescue@0: if not self.hooks[obj] or not self.hooks[obj][method] then return true end Zerotorescue@0: Zerotorescue@0: if scripts[uid] and obj:GetScript(method) == uid then -- unhooks scripts Zerotorescue@0: obj:SetScript(method, self.hooks[obj][method] ~= donothing and self.hooks[obj][method] or nil) Zerotorescue@0: scripts[uid] = nil Zerotorescue@0: elseif obj and self.hooks[obj] and self.hooks[obj][method] and obj[method] == uid then -- unhooks methods Zerotorescue@0: obj[method] = self.hooks[obj][method] Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: self.hooks[obj][method] = nil Zerotorescue@0: self.hooks[obj] = next(self.hooks[obj]) and self.hooks[obj] or nil Zerotorescue@0: else Zerotorescue@0: registry[self][method] = nil Zerotorescue@0: Zerotorescue@0: -- if self.hooks[method] doesn't exist, then this is a SecureHook, just bail out Zerotorescue@0: if not self.hooks[method] then return true end Zerotorescue@0: Zerotorescue@0: if self.hooks[method] and _G[method] == uid then -- unhooks functions Zerotorescue@0: _G[method] = self.hooks[method] Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: self.hooks[method] = nil Zerotorescue@0: end Zerotorescue@0: return true Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --- Unhook all existing hooks for this addon. Zerotorescue@0: function AceHook:UnhookAll() Zerotorescue@0: for key, value in pairs(registry[self]) do Zerotorescue@0: if type(key) == "table" then Zerotorescue@0: for method in pairs(value) do Zerotorescue@0: self:Unhook(key, method) Zerotorescue@0: end Zerotorescue@0: else Zerotorescue@0: self:Unhook(key) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --- Check if the specific function, method or script is already hooked. Zerotorescue@0: -- @paramsig [obj], method Zerotorescue@0: -- @param obj The object or frame to unhook from Zerotorescue@0: -- @param method The name of the method, function or script to unhook from. Zerotorescue@0: function AceHook:IsHooked(obj, method) Zerotorescue@0: -- we don't check if registry[self] exists, this is done by evil magicks in the metatable Zerotorescue@0: if type(obj) == "string" then Zerotorescue@0: if registry[self][obj] and actives[registry[self][obj]] then Zerotorescue@0: return true, handlers[registry[self][obj]] Zerotorescue@0: end Zerotorescue@0: else Zerotorescue@0: if registry[self][obj] and registry[self][obj][method] and actives[registry[self][obj][method]] then Zerotorescue@0: return true, handlers[registry[self][obj][method]] Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: return false, nil Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --- Upgrade our old embeded Zerotorescue@0: for target, v in pairs( AceHook.embeded ) do Zerotorescue@0: AceHook:Embed( target ) Zerotorescue@0: end