forked from doorknob6/Inspector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinspector.lua
More file actions
226 lines (202 loc) · 7.45 KB
/
inspector.lua
File metadata and controls
226 lines (202 loc) · 7.45 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
_G = getfenv(0);
Inspector = CreateFrame("Frame", nil, UIParent);
Inspector:RegisterEvent("ADDON_LOADED");
Inspector:RegisterEvent("VARIABLES_LOADED");
-- Load saved variables
Inspector_Cache = {};
Inspector_Config = {};
Inspector_Debug = true;
Inspector:SetScript("OnEvent", function()
if (event == "ADDON_LOADED") then ---@diagnostic disable-line: undefined-global
if (arg1 == "Inspector") then ---@diagnostic disable-line: undefined-global
Inspector:Initialise();
else
if (not Inspector.initialised) then return; end
for addon, compat in pairs(Inspector.compats) do
if ((arg1 == addon or getglobal(addon)) and (not compat.hasRun)) then ---@diagnostic disable-line: undefined-global
compat:compatFunc();
end
end
end
elseif (event == "VARIABLES_LOADED") then ---@diagnostic disable-line: undefined-global
Inspector:InitialiseConfig();
end
end);
function Inspector:Initialise()
if (self.initialised) then return; end
self.sets = {};
self.canInspect = nil;
self.updateInspect = nil;
self.inspected = {};
self.talentsLoadedFor = nil;
self.hooks = {};
self.initialised = false;
self.compats = {
["pfUI"] = { compatFunc = function() self:Compat_pfUI(); end, hasRun = false }
};
-- override CanInspect
self:OverrideFunction("CanInspect", function(unit)
return self:SetUpInspect(unit);
end);
self.ui:Initialise();
self.initialised = true;
end
-- set default config
function Inspector:InitialiseConfig()
if (not Inspector_Config) then Inspector_Config = {}; end
for option, value in pairs(self.constants.DEFAULT_CONFIG) do
if (Inspector_Config[option] == nil) then
Inspector_Config[option] = value;
end
end
end
-- Inspects the given unit
function Inspector:InspectUnit(unit)
HideUIPanel(InspectFrame);
self:SetUpInspect(unit);
NotifyInspect(unit);
ShowUIPanel(InspectFrame);
end
-- Sets up the inspect for the given unit
function Inspector:SetUpInspect(unit)
if (unit == "player" and UnitExists("target")) then
InspectFrame_Show("target");
return false;
elseif (not unit or not UnitExists(unit)) then
InspectFrame_Show("player");
return false;
end
InspectFrame.unit = unit;
self.canInspect = self:CanInspectUnit(unit);
if (self.canInspect) then
self.inspected.name = UnitName(InspectFrame.unit);
self.updateInspect = (not CheckInteractDistance(InspectFrame.unit, 1));
else
self.updateInspect = false;
self.inspected = {};
end
self.sets = {};
return true;
end
-- Checks if the unit can be inspected
function Inspector:CanInspectUnit(unit)
if (UnitExists(unit) and UnitIsPlayer(unit)) then
return true;
end
return false;
end
-- Resets the saved stats for the given `ItemSlotButton` and returns the `itemSlot` name
function Inspector:ItemSlotButtonResetStats(button)
local itemSlot = self:ItemSlotButtonGetItemSlot(button);
self.stats.itemSlots[itemSlot] = {};
return itemSlot;
end
-- gets the stats for the item slot. Returns the `itemLink` if found.
function Inspector:ItemSlotButtonGetStats(button)
local itemSlot = self:ItemSlotButtonResetStats(button);
local link = Inspector.scanner:ScanItemSlotButton(button,
InspectFrame.unit,
self.stats.itemSlots[itemSlot],
self.sets);
if (link) then -- scan successful
self.stats.itemSlots[itemSlot].link = link;
self:ItemSlotButtonSetCache(itemSlot);
else -- scan unsuccesful, try to restory from cache.
local cache = self:ItemSlotButtonGetCache(itemSlot);
if (cache and next(cache)) then
-- rescan cached item for set bonuses
link = Inspector.scanner:ScanItemLink(cache.link,
self.stats.itemSlots[itemSlot],
self.sets);
end
end
return link;
end
-- caches the currently available data for the itemslot, if caching is turned on
function Inspector:ItemSlotButtonSetCache(itemSlot)
if (not Inspector_Config.EnableCache) then return; end
if (self.inspected.name) then
local name = self.inspected.name;
if (not Inspector_Cache[name]) then
Inspector_Cache[name] = {};
end
if (not Inspector_Cache[name]["itemSlots"]) then
Inspector_Cache[name]["itemSlots"] = {};
end
Inspector_Cache[name].itemSlots[itemSlot] = self.stats.itemSlots[itemSlot];
end
end
-- gets the cache for the given itemslot, if caching is turned on
function Inspector:ItemSlotButtonGetCache(itemSlot)
if (not Inspector_Config.EnableCache) then return; end
if (self.inspected.name) then
local name = self.inspected.name;
if (not Inspector_Cache[name]) then
return;
end
if (not Inspector_Cache[name]["itemSlots"]) then
return;
end
return Inspector_Cache[name].itemSlots[itemSlot];
end
end
-- Gets the `itemSlot` the `ItemSlotButton` is defined for.
function Inspector:ItemSlotButtonGetItemSlot(button)
return string.sub(button:GetName(), 8);
end
-- check if the `Inspect` skin is loaded in pfUI
function Inspector:Check_pfUI()
return (pfUI and
pfUI.skin and
pfUI.skin["Inspect"] and
not (pfUI_config["disabled"] and pfUI_config["disabled"]["skin_Inspect"] == "1"));
end
-- converts some UI items to match pfUI visual style
function Inspector:Compat_pfUI()
if (not self:Check_pfUI()) then return; end
if (self.ui and self.ui.statsButton) then
pfUI.api.SkinButton(self.ui.statsButton);
end
if (self.ui and self.ui.statsFrame and self.ui.statsFrame.scrollParent) then
pfUI.api.SkinScrollbar(getglobal(self.ui.statsFrame.scrollParent:GetName() .. "ScrollBar"));
end
end
-- thanks Shagu
function Inspector:HookFunction(name, func, before)
if (not _G[name]) then return; end
self.hooks[tostring(func)] = {}
self.hooks[tostring(func)]["original"] = _G[name]
self.hooks[tostring(func)]["new"] = func
if (before) then
self.hooks[tostring(func)]["function"] = function(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10)
self.hooks[tostring(func)]["new"](p1, p2, p3, p4, p5, p6, p7, p8, p9, p10)
self.hooks[tostring(func)]["original"](p1, p2, p3, p4, p5, p6, p7, p8, p9, p10)
end;
else
self.hooks[tostring(func)]["function"] = function(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10)
self.hooks[tostring(func)]["original"](p1, p2, p3, p4, p5, p6, p7, p8, p9, p10)
self.hooks[tostring(func)]["new"](p1, p2, p3, p4, p5, p6, p7, p8, p9, p10)
end;
end
_G[name] = self.hooks[tostring(func)]["function"]
end
-- overrides the global function
function Inspector:OverrideFunction(name, func)
if (not _G[name]) then return; end
_G[name] = func;
end
-- thanks Shagu
function Inspector:PostHookScript(f, script, func)
local prev = f:GetScript(script)
f:SetScript(script, function(a1, a2, a3, a4, a5, a6, a7, a8, a9)
if prev then prev(a1, a2, a3, a4, a5, a6, a7, a8, a9) end
func(a1, a2, a3, a4, a5, a6, a7, a8, a9)
end)
end
function Inspector:Debug(text, stack)
if (not Inspector_Debug) then return; end
if (stack) then
DEFAULT_CHAT_FRAME:AddMessage("Inspector debug: " .. debugstack(), 0.5, 0.8, 1);
end
DEFAULT_CHAT_FRAME:AddMessage("Inspector debug: " .. (text or ""), 0.5, 0.8, 1);
end