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