forked from DennisWG/Roid-Macros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtensionsManager.lua
212 lines (173 loc) · 7 KB
/
ExtensionsManager.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
--[[
Author: Dennis Werner Garske (DWG)
License: MIT License
]]
local _G = _G or getfenv(0)
local Roids = _G.Roids or {}
-- A list of all registered extensions
Roids.Extensions = Roids.Extensions or {};
-- Calls the "OnLoad" function of every extension
function Roids.InitializeExtensions()
for k, v in pairs(Roids.Extensions) do
local func = v["OnLoad"];
if not func then
Roids.Print("Roids.InitializeExtensions - Couldn't find 'OnLoad' function for extension "..k);
else
func();
end
end
end
-- Removes the given hook
-- hook: The hook to remove
function Roids.RemoveHook(hook)
_G[hook.name] = hook.origininal;
end
-- Removes the given hook from the given object
-- object: The object to remove the hook from
-- hook: The hook to remove
function Roids.RemoveMethodHook(object, hook)
object[hook.name] = hook.origininal;
end
-- Clears all previously declared hooks
function Roids.ClearHooks()
for k, v in pairs(Roids.Extensions) do
for k2, v2 in pairs(v.internal.memberHooks) do
for k3, v3 in pairs(v2) do
Roids.RemoveMethodHook(k2, v3);
end
end
for k2, v2 in pairs(v.internal.hooks) do
Roids.RemoveHook(v2);
end
end
end
-- Creates a new extension with the given name
-- name: the name of the extension
function Roids.RegisterExtension(name)
local extension = {
internal =
{
frame = CreateFrame("FRAME"),
eventHandlers = {},
hooks = {},
memberHooks = {},
},
};
extension.RegisterEvent = function(eventName, callbackName)
Roids.RegisterEvent(name, eventName, callbackName);
end;
extension.Hook = function(functionName, callbackName, dontCallOriginal)
Roids.RegisterHook(name, functionName, callbackName, dontCallOriginal);
end;
extension.HookMethod = function(object, functionName, callbackName, dontCallOriginal)
Roids.RegisterMethodHook(name, object, functionName, callbackName, dontCallOriginal);
end;
extension.UnregisterEvent = function(eventName, callbackName)
Roids.UnregisterEvent(name, eventName, callbackName);
end;
extension.internal.OnEvent = function()
local callbackName = extension.internal.eventHandlers[event];
if callbackName then
extension[callbackName]();
end
end;
extension.internal.OnHook = function(object, functionName, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10)
local hook;
if object then
hook = extension.internal.memberHooks[object][functionName];
else
hook = extension.internal.hooks[functionName];
end
hook.callback(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
if not hook.dontCallOriginal then
hook.origininal(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
end
end;
extension.internal.frame:SetScript("OnEvent", extension.internal.OnEvent);
Roids.Extensions[name] = extension;
return extension;
end
-- Registers a callback function for a given event name
-- extensionName: The name of the extension trying to register the callback
-- eventName: The event's name we'd like to register a callback for
-- callbackName: The name of the callback that get's called when the event fires
function Roids.RegisterEvent(extensionName, eventName, callbackName)
local extension = Roids.Extensions[extensionName];
extension.internal.eventHandlers[eventName] = callbackName;
extension.internal.frame:RegisterEvent(eventName);
end
-- Hooks the given function by it's name
-- extensionName: The name of the extension trying to register the callback
-- functionName: The name of the function that'll be hooked
-- callbackName: The name of the callback that get's called when the hooked function is called
-- dontCallOriginal: Set to true when the original function should not be called
function Roids.RegisterHook(extensionName, functionName, callbackName, dontCallOriginal)
local orig = _G[functionName];
local extension = Roids.Extensions[extensionName];
if not extension then
Roids.Print("Roids.RegisterHook - Invalid extension: "..extension);
return;
end
if not extension[callbackName] then
Roids.Print("Roids.RegisterHook - Couldn't find callback: "..callbackName);
return;
end
extension.internal.hooks[functionName] = {
origininal = orig,
callback = extension[callbackName],
name = functionName,
dontCallOriginal = dontCallOriginal
};
_G[functionName] = function(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10)
extension.internal.OnHook(nil, functionName, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
end;
end
-- Hooks the given object's function
-- object: The object you want to hook
-- extensionName: The name of the extension trying to register the callback
-- functionName: The name of the function that'll be hooked
-- callbackName: The name of the callback that get's called when the hooked function is called
function Roids.RegisterMethodHook(extensionName, object, functionName, callbackName, dontCallOriginal)
if not object then
Roids.Print("Roids.RegisterMethodHook - The object could not be found!");
return;
end
if type(object) ~= "table" then
Roids.Print("Roids.RegisterMethodHook - The object needs to be a table!");
return;
end
local orig = object[functionName];
if not orig then
Roids.Print("Roids.RegisterMethodHook - The object doesn't have a function named "..functionName);
return;
end
local extension = Roids.Extensions[extensionName];
if not extension then
Roids.Print("Roids.RegisterMethodHook - Invalid extension: "..extension);
return;
end
if not extension[callbackName] then
Roids.Print("Roids.RegisterMethodHook - Couldn't find callback: "..callbackName);
return;
end
if not extension.internal.memberHooks[object] then
extension.internal.memberHooks[object] = {};
end
extension.internal.memberHooks[object][functionName] = {
origininal = orig,
callback = extension[callbackName],
name = functionName,
dontCallOriginal = dontCallOriginal
};
object[functionName] = function(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10)
extension.internal.OnHook(object, functionName, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
end;
end
function Roids.UnregisterEvent(extensionName, eventName, callbackName)
local extension = Roids.Extensions[extensionName];
if extension.internal.eventHandlers[eventName] then
extension.internal.eventHandlers[eventName] = nil;
extension.internal.frame:UnregisterEvent(eventName);
end
end
_G["Roids"] = Roids