annotate Libs/AceHook-3.0/AceHook-3.0.lua @ 0:c6ff7ba0e8f6

Reasonably functional now. Cleaning up some stuff which might have to be reverted.
author Zerotorescue
date Thu, 07 Oct 2010 17:17:43 +0200
parents
children
rev   line source
Zerotorescue@0 1 --- **AceHook-3.0** offers safe Hooking/Unhooking of functions, methods and frame scripts.
Zerotorescue@0 2 -- Using AceHook-3.0 is recommended when you need to unhook your hooks again, so the hook chain isn't broken
Zerotorescue@0 3 -- when you manually restore the original function.
Zerotorescue@0 4 --
Zerotorescue@0 5 -- **AceHook-3.0** can be embeded into your addon, either explicitly by calling AceHook:Embed(MyAddon) or by
Zerotorescue@0 6 -- specifying it as an embeded library in your AceAddon. All functions will be available on your addon object
Zerotorescue@0 7 -- and can be accessed directly, without having to explicitly call AceHook itself.\\
Zerotorescue@0 8 -- It is recommended to embed AceHook, otherwise you'll have to specify a custom `self` on all calls you
Zerotorescue@0 9 -- make into AceHook.
Zerotorescue@0 10 -- @class file
Zerotorescue@0 11 -- @name AceHook-3.0
Zerotorescue@0 12 -- @release $Id: AceHook-3.0.lua 877 2009-11-02 15:56:50Z nevcairiel $
Zerotorescue@0 13 local ACEHOOK_MAJOR, ACEHOOK_MINOR = "AceHook-3.0", 5
Zerotorescue@0 14 local AceHook, oldminor = LibStub:NewLibrary(ACEHOOK_MAJOR, ACEHOOK_MINOR)
Zerotorescue@0 15
Zerotorescue@0 16 if not AceHook then return end -- No upgrade needed
Zerotorescue@0 17
Zerotorescue@0 18 AceHook.embeded = AceHook.embeded or {}
Zerotorescue@0 19 AceHook.registry = AceHook.registry or setmetatable({}, {__index = function(tbl, key) tbl[key] = {} return tbl[key] end })
Zerotorescue@0 20 AceHook.handlers = AceHook.handlers or {}
Zerotorescue@0 21 AceHook.actives = AceHook.actives or {}
Zerotorescue@0 22 AceHook.scripts = AceHook.scripts or {}
Zerotorescue@0 23 AceHook.onceSecure = AceHook.onceSecure or {}
Zerotorescue@0 24 AceHook.hooks = AceHook.hooks or {}
Zerotorescue@0 25
Zerotorescue@0 26 -- local upvalues
Zerotorescue@0 27 local registry = AceHook.registry
Zerotorescue@0 28 local handlers = AceHook.handlers
Zerotorescue@0 29 local actives = AceHook.actives
Zerotorescue@0 30 local scripts = AceHook.scripts
Zerotorescue@0 31 local onceSecure = AceHook.onceSecure
Zerotorescue@0 32
Zerotorescue@0 33 -- Lua APIs
Zerotorescue@0 34 local pairs, next, type = pairs, next, type
Zerotorescue@0 35 local format = string.format
Zerotorescue@0 36 local assert, error = assert, error
Zerotorescue@0 37
Zerotorescue@0 38 -- WoW APIs
Zerotorescue@0 39 local issecurevariable, hooksecurefunc = issecurevariable, hooksecurefunc
Zerotorescue@0 40 local _G = _G
Zerotorescue@0 41
Zerotorescue@0 42 -- functions for later definition
Zerotorescue@0 43 local donothing, createHook, hook
Zerotorescue@0 44
Zerotorescue@0 45 local protectedScripts = {
Zerotorescue@0 46 OnClick = true,
Zerotorescue@0 47 }
Zerotorescue@0 48
Zerotorescue@0 49 -- upgrading of embeded is done at the bottom of the file
Zerotorescue@0 50
Zerotorescue@0 51 local mixins = {
Zerotorescue@0 52 "Hook", "SecureHook",
Zerotorescue@0 53 "HookScript", "SecureHookScript",
Zerotorescue@0 54 "Unhook", "UnhookAll",
Zerotorescue@0 55 "IsHooked",
Zerotorescue@0 56 "RawHook", "RawHookScript"
Zerotorescue@0 57 }
Zerotorescue@0 58
Zerotorescue@0 59 -- AceHook:Embed( target )
Zerotorescue@0 60 -- target (object) - target object to embed AceHook in
Zerotorescue@0 61 --
Zerotorescue@0 62 -- Embeds AceEevent into the target object making the functions from the mixins list available on target:..
Zerotorescue@0 63 function AceHook:Embed( target )
Zerotorescue@0 64 for k, v in pairs( mixins ) do
Zerotorescue@0 65 target[v] = self[v]
Zerotorescue@0 66 end
Zerotorescue@0 67 self.embeded[target] = true
Zerotorescue@0 68 -- inject the hooks table safely
Zerotorescue@0 69 target.hooks = target.hooks or {}
Zerotorescue@0 70 return target
Zerotorescue@0 71 end
Zerotorescue@0 72
Zerotorescue@0 73 -- AceHook:OnEmbedDisable( target )
Zerotorescue@0 74 -- target (object) - target object that is being disabled
Zerotorescue@0 75 --
Zerotorescue@0 76 -- Unhooks all hooks when the target disables.
Zerotorescue@0 77 -- this method should be called by the target manually or by an addon framework
Zerotorescue@0 78 function AceHook:OnEmbedDisable( target )
Zerotorescue@0 79 target:UnhookAll()
Zerotorescue@0 80 end
Zerotorescue@0 81
Zerotorescue@0 82 function createHook(self, handler, orig, secure, failsafe)
Zerotorescue@0 83 local uid
Zerotorescue@0 84 local method = type(handler) == "string"
Zerotorescue@0 85 if failsafe and not secure then
Zerotorescue@0 86 -- failsafe hook creation
Zerotorescue@0 87 uid = function(...)
Zerotorescue@0 88 if actives[uid] then
Zerotorescue@0 89 if method then
Zerotorescue@0 90 self[handler](self, ...)
Zerotorescue@0 91 else
Zerotorescue@0 92 handler(...)
Zerotorescue@0 93 end
Zerotorescue@0 94 end
Zerotorescue@0 95 return orig(...)
Zerotorescue@0 96 end
Zerotorescue@0 97 -- /failsafe hook
Zerotorescue@0 98 else
Zerotorescue@0 99 -- all other hooks
Zerotorescue@0 100 uid = function(...)
Zerotorescue@0 101 if actives[uid] then
Zerotorescue@0 102 if method then
Zerotorescue@0 103 return self[handler](self, ...)
Zerotorescue@0 104 else
Zerotorescue@0 105 return handler(...)
Zerotorescue@0 106 end
Zerotorescue@0 107 elseif not secure then -- backup on non secure
Zerotorescue@0 108 return orig(...)
Zerotorescue@0 109 end
Zerotorescue@0 110 end
Zerotorescue@0 111 -- /hook
Zerotorescue@0 112 end
Zerotorescue@0 113 return uid
Zerotorescue@0 114 end
Zerotorescue@0 115
Zerotorescue@0 116 function donothing() end
Zerotorescue@0 117
Zerotorescue@0 118 function hook(self, obj, method, handler, script, secure, raw, forceSecure, usage)
Zerotorescue@0 119 if not handler then handler = method end
Zerotorescue@0 120
Zerotorescue@0 121 -- These asserts make sure AceHooks's devs play by the rules.
Zerotorescue@0 122 assert(not script or type(script) == "boolean")
Zerotorescue@0 123 assert(not secure or type(secure) == "boolean")
Zerotorescue@0 124 assert(not raw or type(raw) == "boolean")
Zerotorescue@0 125 assert(not forceSecure or type(forceSecure) == "boolean")
Zerotorescue@0 126 assert(usage)
Zerotorescue@0 127
Zerotorescue@0 128 -- Error checking Battery!
Zerotorescue@0 129 if obj and type(obj) ~= "table" then
Zerotorescue@0 130 error(format("%s: 'object' - nil or table expected got %s", usage, type(obj)), 3)
Zerotorescue@0 131 end
Zerotorescue@0 132 if type(method) ~= "string" then
Zerotorescue@0 133 error(format("%s: 'method' - string expected got %s", usage, type(method)), 3)
Zerotorescue@0 134 end
Zerotorescue@0 135 if type(handler) ~= "string" and type(handler) ~= "function" then
Zerotorescue@0 136 error(format("%s: 'handler' - nil, string, or function expected got %s", usage, type(handler)), 3)
Zerotorescue@0 137 end
Zerotorescue@0 138 if type(handler) == "string" and type(self[handler]) ~= "function" then
Zerotorescue@0 139 error(format("%s: 'handler' - Handler specified does not exist at self[handler]", usage), 3)
Zerotorescue@0 140 end
Zerotorescue@0 141 if script then
Zerotorescue@0 142 if not secure and obj:IsProtected() and protectedScripts[method] then
Zerotorescue@0 143 error(format("Cannot hook secure script %q; Use SecureHookScript(obj, method, [handler]) instead.", method), 3)
Zerotorescue@0 144 end
Zerotorescue@0 145 if not obj or not obj.GetScript or not obj:HasScript(method) then
Zerotorescue@0 146 error(format("%s: You can only hook a script on a frame object", usage), 3)
Zerotorescue@0 147 end
Zerotorescue@0 148 else
Zerotorescue@0 149 local issecure
Zerotorescue@0 150 if obj then
Zerotorescue@0 151 issecure = onceSecure[obj] and onceSecure[obj][method] or issecurevariable(obj, method)
Zerotorescue@0 152 else
Zerotorescue@0 153 issecure = onceSecure[method] or issecurevariable(method)
Zerotorescue@0 154 end
Zerotorescue@0 155 if issecure then
Zerotorescue@0 156 if forceSecure then
Zerotorescue@0 157 if obj then
Zerotorescue@0 158 onceSecure[obj] = onceSecure[obj] or {}
Zerotorescue@0 159 onceSecure[obj][method] = true
Zerotorescue@0 160 else
Zerotorescue@0 161 onceSecure[method] = true
Zerotorescue@0 162 end
Zerotorescue@0 163 elseif not secure then
Zerotorescue@0 164 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 165 end
Zerotorescue@0 166 end
Zerotorescue@0 167 end
Zerotorescue@0 168
Zerotorescue@0 169 local uid
Zerotorescue@0 170 if obj then
Zerotorescue@0 171 uid = registry[self][obj] and registry[self][obj][method]
Zerotorescue@0 172 else
Zerotorescue@0 173 uid = registry[self][method]
Zerotorescue@0 174 end
Zerotorescue@0 175
Zerotorescue@0 176 if uid then
Zerotorescue@0 177 if actives[uid] then
Zerotorescue@0 178 -- Only two sane choices exist here. We either a) error 100% of the time or b) always unhook and then hook
Zerotorescue@0 179 -- choice b would likely lead to odd debuging conditions or other mysteries so we're going with a.
Zerotorescue@0 180 error(format("Attempting to rehook already active hook %s.", method))
Zerotorescue@0 181 end
Zerotorescue@0 182
Zerotorescue@0 183 if handlers[uid] == handler then -- turn on a decative hook, note enclosures break this ability, small memory leak
Zerotorescue@0 184 actives[uid] = true
Zerotorescue@0 185 return
Zerotorescue@0 186 elseif obj then -- is there any reason not to call unhook instead of doing the following several lines?
Zerotorescue@0 187 if self.hooks and self.hooks[obj] then
Zerotorescue@0 188 self.hooks[obj][method] = nil
Zerotorescue@0 189 end
Zerotorescue@0 190 registry[self][obj][method] = nil
Zerotorescue@0 191 else
Zerotorescue@0 192 if self.hooks then
Zerotorescue@0 193 self.hooks[method] = nil
Zerotorescue@0 194 end
Zerotorescue@0 195 registry[self][method] = nil
Zerotorescue@0 196 end
Zerotorescue@0 197 handlers[uid], actives[uid], scripts[uid] = nil, nil, nil
Zerotorescue@0 198 uid = nil
Zerotorescue@0 199 end
Zerotorescue@0 200
Zerotorescue@0 201 local orig
Zerotorescue@0 202 if script then
Zerotorescue@0 203 orig = obj:GetScript(method) or donothing
Zerotorescue@0 204 elseif obj then
Zerotorescue@0 205 orig = obj[method]
Zerotorescue@0 206 else
Zerotorescue@0 207 orig = _G[method]
Zerotorescue@0 208 end
Zerotorescue@0 209
Zerotorescue@0 210 if not orig then
Zerotorescue@0 211 error(format("%s: Attempting to hook a non existing target", usage), 3)
Zerotorescue@0 212 end
Zerotorescue@0 213
Zerotorescue@0 214 uid = createHook(self, handler, orig, secure, not (raw or secure))
Zerotorescue@0 215
Zerotorescue@0 216 if obj then
Zerotorescue@0 217 self.hooks[obj] = self.hooks[obj] or {}
Zerotorescue@0 218 registry[self][obj] = registry[self][obj] or {}
Zerotorescue@0 219 registry[self][obj][method] = uid
Zerotorescue@0 220
Zerotorescue@0 221 if not secure then
Zerotorescue@0 222 self.hooks[obj][method] = orig
Zerotorescue@0 223 end
Zerotorescue@0 224
Zerotorescue@0 225 if script then
Zerotorescue@0 226 -- If the script is empty before, HookScript will not work, so use SetScript instead
Zerotorescue@0 227 -- This will make the hook insecure, but shouldnt matter, since it was empty before.
Zerotorescue@0 228 -- It does not taint the full frame.
Zerotorescue@0 229 if not secure or orig == donothing then
Zerotorescue@0 230 obj:SetScript(method, uid)
Zerotorescue@0 231 elseif secure then
Zerotorescue@0 232 obj:HookScript(method, uid)
Zerotorescue@0 233 end
Zerotorescue@0 234 else
Zerotorescue@0 235 if not secure then
Zerotorescue@0 236 obj[method] = uid
Zerotorescue@0 237 else
Zerotorescue@0 238 hooksecurefunc(obj, method, uid)
Zerotorescue@0 239 end
Zerotorescue@0 240 end
Zerotorescue@0 241 else
Zerotorescue@0 242 registry[self][method] = uid
Zerotorescue@0 243
Zerotorescue@0 244 if not secure then
Zerotorescue@0 245 _G[method] = uid
Zerotorescue@0 246 self.hooks[method] = orig
Zerotorescue@0 247 else
Zerotorescue@0 248 hooksecurefunc(method, uid)
Zerotorescue@0 249 end
Zerotorescue@0 250 end
Zerotorescue@0 251
Zerotorescue@0 252 actives[uid], handlers[uid], scripts[uid] = true, handler, script and true or nil
Zerotorescue@0 253 end
Zerotorescue@0 254
Zerotorescue@0 255 --- Hook a function or a method on an object.
Zerotorescue@0 256 -- The hook created will be a "safe hook", that means that your handler will be called
Zerotorescue@0 257 -- before the hooked function ("Pre-Hook"), and you don't have to call the original function yourself,
Zerotorescue@0 258 -- however you cannot stop the execution of the function, or modify any of the arguments/return values.\\
Zerotorescue@0 259 -- 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 260 -- @paramsig [object], method, [handler], [hookSecure]
Zerotorescue@0 261 -- @param object The object to hook a method from
Zerotorescue@0 262 -- @param method If object was specified, the name of the method, or the name of the function to hook.
Zerotorescue@0 263 -- @param handler The handler for the hook, a funcref or a method name. (Defaults to the name of the hooked function)
Zerotorescue@0 264 -- @param hookSecure If true, AceHook will allow hooking of secure functions.
Zerotorescue@0 265 -- @usage
Zerotorescue@0 266 -- -- create an addon with AceHook embeded
Zerotorescue@0 267 -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("HookDemo", "AceHook-3.0")
Zerotorescue@0 268 --
Zerotorescue@0 269 -- function MyAddon:OnEnable()
Zerotorescue@0 270 -- -- Hook ActionButton_UpdateHotkeys, overwriting the secure status
Zerotorescue@0 271 -- self:Hook("ActionButton_UpdateHotkeys", true)
Zerotorescue@0 272 -- end
Zerotorescue@0 273 --
Zerotorescue@0 274 -- function MyAddon:ActionButton_UpdateHotkeys(button, type)
Zerotorescue@0 275 -- print(button:GetName() .. " is updating its HotKey")
Zerotorescue@0 276 -- end
Zerotorescue@0 277 function AceHook:Hook(object, method, handler, hookSecure)
Zerotorescue@0 278 if type(object) == "string" then
Zerotorescue@0 279 method, handler, hookSecure, object = object, method, handler, nil
Zerotorescue@0 280 end
Zerotorescue@0 281
Zerotorescue@0 282 if handler == true then
Zerotorescue@0 283 handler, hookSecure = nil, true
Zerotorescue@0 284 end
Zerotorescue@0 285
Zerotorescue@0 286 hook(self, object, method, handler, false, false, false, hookSecure or false, "Usage: Hook([object], method, [handler], [hookSecure])")
Zerotorescue@0 287 end
Zerotorescue@0 288
Zerotorescue@0 289 --- RawHook a function or a method on an object.
Zerotorescue@0 290 -- The hook created will be a "raw hook", that means that your handler will completly replace
Zerotorescue@0 291 -- the original function, and your handler has to call the original function (or not, depending on your intentions).\\
Zerotorescue@0 292 -- The original function will be stored in `self.hooks[object][method]` or `self.hooks[functionName]` respectively.\\
Zerotorescue@0 293 -- 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 294 -- or want to control execution of the original function.
Zerotorescue@0 295 -- @paramsig [object], method, [handler], [hookSecure]
Zerotorescue@0 296 -- @param object The object to hook a method from
Zerotorescue@0 297 -- @param method If object was specified, the name of the method, or the name of the function to hook.
Zerotorescue@0 298 -- @param handler The handler for the hook, a funcref or a method name. (Defaults to the name of the hooked function)
Zerotorescue@0 299 -- @param hookSecure If true, AceHook will allow hooking of secure functions.
Zerotorescue@0 300 -- @usage
Zerotorescue@0 301 -- -- create an addon with AceHook embeded
Zerotorescue@0 302 -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("HookDemo", "AceHook-3.0")
Zerotorescue@0 303 --
Zerotorescue@0 304 -- function MyAddon:OnEnable()
Zerotorescue@0 305 -- -- Hook ActionButton_UpdateHotkeys, overwriting the secure status
Zerotorescue@0 306 -- self:RawHook("ActionButton_UpdateHotkeys", true)
Zerotorescue@0 307 -- end
Zerotorescue@0 308 --
Zerotorescue@0 309 -- function MyAddon:ActionButton_UpdateHotkeys(button, type)
Zerotorescue@0 310 -- if button:GetName() == "MyButton" then
Zerotorescue@0 311 -- -- do stuff here
Zerotorescue@0 312 -- else
Zerotorescue@0 313 -- self.hooks.ActionButton_UpdateHotkeys(button, type)
Zerotorescue@0 314 -- end
Zerotorescue@0 315 -- end
Zerotorescue@0 316 function AceHook:RawHook(object, method, handler, hookSecure)
Zerotorescue@0 317 if type(object) == "string" then
Zerotorescue@0 318 method, handler, hookSecure, object = object, method, handler, nil
Zerotorescue@0 319 end
Zerotorescue@0 320
Zerotorescue@0 321 if handler == true then
Zerotorescue@0 322 handler, hookSecure = nil, true
Zerotorescue@0 323 end
Zerotorescue@0 324
Zerotorescue@0 325 hook(self, object, method, handler, false, false, true, hookSecure or false, "Usage: RawHook([object], method, [handler], [hookSecure])")
Zerotorescue@0 326 end
Zerotorescue@0 327
Zerotorescue@0 328 --- SecureHook a function or a method on an object.
Zerotorescue@0 329 -- This function is a wrapper around the `hooksecurefunc` function in the WoW API. Using AceHook
Zerotorescue@0 330 -- extends the functionality of secure hooks, and adds the ability to unhook once the hook isn't
Zerotorescue@0 331 -- required anymore, or the addon is being disabled.\\
Zerotorescue@0 332 -- Secure Hooks should be used if the secure-status of the function is vital to its function,
Zerotorescue@0 333 -- and taint would block execution. Secure Hooks are always called after the original function was called
Zerotorescue@0 334 -- ("Post Hook"), and you cannot modify the arguments, return values or control the execution.
Zerotorescue@0 335 -- @paramsig [object], method, [handler]
Zerotorescue@0 336 -- @param object The object to hook a method from
Zerotorescue@0 337 -- @param method If object was specified, the name of the method, or the name of the function to hook.
Zerotorescue@0 338 -- @param handler The handler for the hook, a funcref or a method name. (Defaults to the name of the hooked function)
Zerotorescue@0 339 function AceHook:SecureHook(object, method, handler)
Zerotorescue@0 340 if type(object) == "string" then
Zerotorescue@0 341 method, handler, object = object, method, nil
Zerotorescue@0 342 end
Zerotorescue@0 343
Zerotorescue@0 344 hook(self, object, method, handler, false, true, false, false, "Usage: SecureHook([object], method, [handler])")
Zerotorescue@0 345 end
Zerotorescue@0 346
Zerotorescue@0 347 --- Hook a script handler on a frame.
Zerotorescue@0 348 -- The hook created will be a "safe hook", that means that your handler will be called
Zerotorescue@0 349 -- before the hooked script ("Pre-Hook"), and you don't have to call the original function yourself,
Zerotorescue@0 350 -- however you cannot stop the execution of the function, or modify any of the arguments/return values.\\
Zerotorescue@0 351 -- This is the frame script equivalent of the :Hook safe-hook. It would typically be used to be notified
Zerotorescue@0 352 -- when a certain event happens to a frame.
Zerotorescue@0 353 -- @paramsig frame, script, [handler]
Zerotorescue@0 354 -- @param frame The Frame to hook the script on
Zerotorescue@0 355 -- @param script The script to hook
Zerotorescue@0 356 -- @param handler The handler for the hook, a funcref or a method name. (Defaults to the name of the hooked script)
Zerotorescue@0 357 -- @usage
Zerotorescue@0 358 -- -- create an addon with AceHook embeded
Zerotorescue@0 359 -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("HookDemo", "AceHook-3.0")
Zerotorescue@0 360 --
Zerotorescue@0 361 -- function MyAddon:OnEnable()
Zerotorescue@0 362 -- -- Hook the OnShow of FriendsFrame
Zerotorescue@0 363 -- self:HookScript(FriendsFrame, "OnShow", "FriendsFrameOnShow")
Zerotorescue@0 364 -- end
Zerotorescue@0 365 --
Zerotorescue@0 366 -- function MyAddon:FriendsFrameOnShow(frame)
Zerotorescue@0 367 -- print("The FriendsFrame was shown!")
Zerotorescue@0 368 -- end
Zerotorescue@0 369 function AceHook:HookScript(frame, script, handler)
Zerotorescue@0 370 hook(self, frame, script, handler, true, false, false, false, "Usage: HookScript(object, method, [handler])")
Zerotorescue@0 371 end
Zerotorescue@0 372
Zerotorescue@0 373 --- RawHook a script handler on a frame.
Zerotorescue@0 374 -- The hook created will be a "raw hook", that means that your handler will completly replace
Zerotorescue@0 375 -- the original script, and your handler has to call the original script (or not, depending on your intentions).\\
Zerotorescue@0 376 -- The original script will be stored in `self.hooks[frame][script]`.\\
Zerotorescue@0 377 -- 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 378 -- or want to control execution of the original script.
Zerotorescue@0 379 -- @paramsig frame, script, [handler]
Zerotorescue@0 380 -- @param frame The Frame to hook the script on
Zerotorescue@0 381 -- @param script The script to hook
Zerotorescue@0 382 -- @param handler The handler for the hook, a funcref or a method name. (Defaults to the name of the hooked script)
Zerotorescue@0 383 -- @usage
Zerotorescue@0 384 -- -- create an addon with AceHook embeded
Zerotorescue@0 385 -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("HookDemo", "AceHook-3.0")
Zerotorescue@0 386 --
Zerotorescue@0 387 -- function MyAddon:OnEnable()
Zerotorescue@0 388 -- -- Hook the OnShow of FriendsFrame
Zerotorescue@0 389 -- self:RawHookScript(FriendsFrame, "OnShow", "FriendsFrameOnShow")
Zerotorescue@0 390 -- end
Zerotorescue@0 391 --
Zerotorescue@0 392 -- function MyAddon:FriendsFrameOnShow(frame)
Zerotorescue@0 393 -- -- Call the original function
Zerotorescue@0 394 -- self.hooks[frame].OnShow(frame)
Zerotorescue@0 395 -- -- Do our processing
Zerotorescue@0 396 -- -- .. stuff
Zerotorescue@0 397 -- end
Zerotorescue@0 398 function AceHook:RawHookScript(frame, script, handler)
Zerotorescue@0 399 hook(self, frame, script, handler, true, false, true, false, "Usage: RawHookScript(object, method, [handler])")
Zerotorescue@0 400 end
Zerotorescue@0 401
Zerotorescue@0 402 --- SecureHook a script handler on a frame.
Zerotorescue@0 403 -- This function is a wrapper around the `frame:HookScript` function in the WoW API. Using AceHook
Zerotorescue@0 404 -- extends the functionality of secure hooks, and adds the ability to unhook once the hook isn't
Zerotorescue@0 405 -- required anymore, or the addon is being disabled.\\
Zerotorescue@0 406 -- Secure Hooks should be used if the secure-status of the function is vital to its function,
Zerotorescue@0 407 -- and taint would block execution. Secure Hooks are always called after the original function was called
Zerotorescue@0 408 -- ("Post Hook"), and you cannot modify the arguments, return values or control the execution.
Zerotorescue@0 409 -- @paramsig frame, script, [handler]
Zerotorescue@0 410 -- @param frame The Frame to hook the script on
Zerotorescue@0 411 -- @param script The script to hook
Zerotorescue@0 412 -- @param handler The handler for the hook, a funcref or a method name. (Defaults to the name of the hooked script)
Zerotorescue@0 413 function AceHook:SecureHookScript(frame, script, handler)
Zerotorescue@0 414 hook(self, frame, script, handler, true, true, false, false, "Usage: SecureHookScript(object, method, [handler])")
Zerotorescue@0 415 end
Zerotorescue@0 416
Zerotorescue@0 417 --- Unhook from the specified function, method or script.
Zerotorescue@0 418 -- @paramsig [obj], method
Zerotorescue@0 419 -- @param obj The object or frame to unhook from
Zerotorescue@0 420 -- @param method The name of the method, function or script to unhook from.
Zerotorescue@0 421 function AceHook:Unhook(obj, method)
Zerotorescue@0 422 local usage = "Usage: Unhook([obj], method)"
Zerotorescue@0 423 if type(obj) == "string" then
Zerotorescue@0 424 method, obj = obj, nil
Zerotorescue@0 425 end
Zerotorescue@0 426
Zerotorescue@0 427 if obj and type(obj) ~= "table" then
Zerotorescue@0 428 error(format("%s: 'obj' - expecting nil or table got %s", usage, type(obj)), 2)
Zerotorescue@0 429 end
Zerotorescue@0 430 if type(method) ~= "string" then
Zerotorescue@0 431 error(format("%s: 'method' - expeting string got %s", usage, type(method)), 2)
Zerotorescue@0 432 end
Zerotorescue@0 433
Zerotorescue@0 434 local uid
Zerotorescue@0 435 if obj then
Zerotorescue@0 436 uid = registry[self][obj] and registry[self][obj][method]
Zerotorescue@0 437 else
Zerotorescue@0 438 uid = registry[self][method]
Zerotorescue@0 439 end
Zerotorescue@0 440
Zerotorescue@0 441 if not uid or not actives[uid] then
Zerotorescue@0 442 -- Declining to error on an unneeded unhook since the end effect is the same and this would just be annoying.
Zerotorescue@0 443 return false
Zerotorescue@0 444 end
Zerotorescue@0 445
Zerotorescue@0 446 actives[uid], handlers[uid] = nil, nil
Zerotorescue@0 447
Zerotorescue@0 448 if obj then
Zerotorescue@0 449 registry[self][obj][method] = nil
Zerotorescue@0 450 registry[self][obj] = next(registry[self][obj]) and registry[self][obj] or nil
Zerotorescue@0 451
Zerotorescue@0 452 -- if the hook reference doesnt exist, then its a secure hook, just bail out and dont do any unhooking
Zerotorescue@0 453 if not self.hooks[obj] or not self.hooks[obj][method] then return true end
Zerotorescue@0 454
Zerotorescue@0 455 if scripts[uid] and obj:GetScript(method) == uid then -- unhooks scripts
Zerotorescue@0 456 obj:SetScript(method, self.hooks[obj][method] ~= donothing and self.hooks[obj][method] or nil)
Zerotorescue@0 457 scripts[uid] = nil
Zerotorescue@0 458 elseif obj and self.hooks[obj] and self.hooks[obj][method] and obj[method] == uid then -- unhooks methods
Zerotorescue@0 459 obj[method] = self.hooks[obj][method]
Zerotorescue@0 460 end
Zerotorescue@0 461
Zerotorescue@0 462 self.hooks[obj][method] = nil
Zerotorescue@0 463 self.hooks[obj] = next(self.hooks[obj]) and self.hooks[obj] or nil
Zerotorescue@0 464 else
Zerotorescue@0 465 registry[self][method] = nil
Zerotorescue@0 466
Zerotorescue@0 467 -- if self.hooks[method] doesn't exist, then this is a SecureHook, just bail out
Zerotorescue@0 468 if not self.hooks[method] then return true end
Zerotorescue@0 469
Zerotorescue@0 470 if self.hooks[method] and _G[method] == uid then -- unhooks functions
Zerotorescue@0 471 _G[method] = self.hooks[method]
Zerotorescue@0 472 end
Zerotorescue@0 473
Zerotorescue@0 474 self.hooks[method] = nil
Zerotorescue@0 475 end
Zerotorescue@0 476 return true
Zerotorescue@0 477 end
Zerotorescue@0 478
Zerotorescue@0 479 --- Unhook all existing hooks for this addon.
Zerotorescue@0 480 function AceHook:UnhookAll()
Zerotorescue@0 481 for key, value in pairs(registry[self]) do
Zerotorescue@0 482 if type(key) == "table" then
Zerotorescue@0 483 for method in pairs(value) do
Zerotorescue@0 484 self:Unhook(key, method)
Zerotorescue@0 485 end
Zerotorescue@0 486 else
Zerotorescue@0 487 self:Unhook(key)
Zerotorescue@0 488 end
Zerotorescue@0 489 end
Zerotorescue@0 490 end
Zerotorescue@0 491
Zerotorescue@0 492 --- Check if the specific function, method or script is already hooked.
Zerotorescue@0 493 -- @paramsig [obj], method
Zerotorescue@0 494 -- @param obj The object or frame to unhook from
Zerotorescue@0 495 -- @param method The name of the method, function or script to unhook from.
Zerotorescue@0 496 function AceHook:IsHooked(obj, method)
Zerotorescue@0 497 -- we don't check if registry[self] exists, this is done by evil magicks in the metatable
Zerotorescue@0 498 if type(obj) == "string" then
Zerotorescue@0 499 if registry[self][obj] and actives[registry[self][obj]] then
Zerotorescue@0 500 return true, handlers[registry[self][obj]]
Zerotorescue@0 501 end
Zerotorescue@0 502 else
Zerotorescue@0 503 if registry[self][obj] and registry[self][obj][method] and actives[registry[self][obj][method]] then
Zerotorescue@0 504 return true, handlers[registry[self][obj][method]]
Zerotorescue@0 505 end
Zerotorescue@0 506 end
Zerotorescue@0 507
Zerotorescue@0 508 return false, nil
Zerotorescue@0 509 end
Zerotorescue@0 510
Zerotorescue@0 511 --- Upgrade our old embeded
Zerotorescue@0 512 for target, v in pairs( AceHook.embeded ) do
Zerotorescue@0 513 AceHook:Embed( target )
Zerotorescue@0 514 end