-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionBarRepository.lua
More file actions
265 lines (230 loc) · 7.79 KB
/
Copy pathActionBarRepository.lua
File metadata and controls
265 lines (230 loc) · 7.79 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
local _, MacroStudio = ...
local ActionBarRepository = {
usageByIndex = {},
scanCount = 0,
}
MacroStudio.ActionBarRepository = ActionBarRepository
-- Retail FrameXML defines 12 slots per page and native action-bar page indexes
-- through 18 (vehicle 16, temporary shapeshift 17, override 18).
ActionBarRepository.MAX_ACTION_SLOTS = 12 * 18
local function safeToNumber(value)
local ok, number = pcall(tonumber, value)
if ok and type(number) == "number" and number > 0 then
return number
end
return nil
end
local function safeToString(value)
local ok, text = pcall(tostring, value)
return ok and text or "<unavailable>"
end
local function getActionText(slot)
local provider = C_ActionBar and C_ActionBar.GetActionText or GetActionText
if type(provider) ~= "function" then
return nil
end
local ok, text = pcall(provider, slot)
return ok and type(text) == "string" and text or nil
end
local function getActionTexture(slot)
local provider = C_ActionBar and C_ActionBar.GetActionTexture or GetActionTexture
if type(provider) ~= "function" then
return nil
end
local ok, texture = pcall(provider, slot)
return ok and texture or nil
end
local function normalizeIcon(icon)
if type(icon) == "number" then
return icon
end
if type(icon) == "string" and type(GetFileIDFromPath) == "function" then
local ok, fileID = pcall(GetFileIDFromPath, icon)
if ok and type(fileID) == "number" then
return fileID
end
end
return icon
end
local function isDynamicMacroIcon(icon)
local normalized = normalizeIcon(icon)
if normalized == MacroStudio.DEFAULT_ICON then
return true
end
if type(icon) ~= "string" then
return false
end
local basename = icon:lower():gsub("\\", "/"):match("([^/]+)$") or ""
basename = basename:gsub("%.blp$", ""):gsub("%.tga$", "")
return basename == "inv_misc_questionmark"
end
local function iconsEqual(first, second)
if first == second then
return true
end
return normalizeIcon(first) == normalizeIcon(second)
end
local function getMacroSpellID(index)
if type(GetMacroSpell) ~= "function" then
return nil
end
local ok, first, second = pcall(GetMacroSpell, index)
if not ok then
return nil
end
return safeToNumber(first) or safeToNumber(second)
end
local function itemIDFromValue(value)
local direct = safeToNumber(value)
if direct then
return direct
end
if type(value) ~= "string" then
return nil
end
local linked = safeToNumber(value:match("item:(%d+)"))
if linked then
return linked
end
if C_Item and type(C_Item.GetItemIDForItemInfo) == "function" then
local ok, itemID = pcall(C_Item.GetItemIDForItemInfo, value)
if ok and safeToNumber(itemID) then
return safeToNumber(itemID)
end
end
if C_Item and type(C_Item.GetItemInfoInstant) == "function" then
local ok, itemID = pcall(C_Item.GetItemInfoInstant, value)
if ok and safeToNumber(itemID) then
return safeToNumber(itemID)
end
end
if type(GetItemInfoInstant) == "function" then
local ok, itemID = pcall(GetItemInfoInstant, value)
if ok and safeToNumber(itemID) then
return safeToNumber(itemID)
end
end
return nil
end
local function getMacroItemID(index)
if type(GetMacroItem) ~= "function" then
return nil
end
local ok, first, second = pcall(GetMacroItem, index)
if not ok then
return nil
end
return itemIDFromValue(second) or itemIDFromValue(first)
end
local function resolutionMatches(macro, actionID, actionSubtype)
local spellID = getMacroSpellID(macro.index)
local itemID = getMacroItemID(macro.index)
local resolvedActionID = safeToNumber(actionID)
if actionSubtype == "spell" then
return spellID ~= nil and resolvedActionID == spellID, true
end
if actionSubtype == "item" then
return itemID ~= nil and resolvedActionID == itemID, true
end
if actionSubtype ~= nil and actionSubtype ~= "" then
return false, false
end
return spellID == nil and itemID == nil, false
end
local function candidateMatches(macro, actionText, actionTexture, actionID, actionSubtype)
if macro.name ~= actionText then
return false
end
local resolutionMatch, hasResolutionEvidence = resolutionMatches(macro, actionID, actionSubtype)
if not resolutionMatch then
return false
end
if isDynamicMacroIcon(macro.selectedIcon or macro.icon) then
return hasResolutionEvidence
end
return actionTexture ~= nil and iconsEqual(macro.icon, actionTexture)
end
function ActionBarRepository:ResolveMacroAction(slot, actionID, actionSubtype)
local actionText = getActionText(slot)
local actionTexture = getActionTexture(slot)
if not actionText or actionText == "" then
return nil, "unresolved", actionText
end
local matches = {}
for _, macro in ipairs(MacroStudio.MacroRepository:GetAll()) do
if candidateMatches(macro, actionText, actionTexture, actionID, actionSubtype) then
matches[#matches + 1] = macro
end
end
if #matches > 1 then
return nil, "ambiguous", actionText
end
if #matches == 0 then
return nil, "unresolved", actionText
end
local listedMacro = matches[1]
local currentMacro = MacroStudio.MacroRepository:GetByIndex(listedMacro.index, listedMacro.scope)
if not currentMacro or not MacroStudio.MacroRepository:SnapshotsEqual(currentMacro, listedMacro) then
return nil, "unresolved", actionText
end
return currentMacro, "exact", actionText
end
local function copySlots(slots)
local copy = {}
for index, slot in ipairs(slots or {}) do
copy[index] = slot
end
return copy
end
function ActionBarRepository:Refresh()
local usageByIndex = {}
self.scanCount = self.scanCount + 1
if type(GetActionInfo) ~= "function" then
self.usageByIndex = usageByIndex
return usageByIndex
end
for slot = 1, self.MAX_ACTION_SLOTS do
local actionType, actionID, actionSubtype = GetActionInfo(slot)
if actionType == "macro" then
local currentMacro, identity, actionText = self:ResolveMacroAction(slot, actionID, actionSubtype)
MacroStudio:Debug(
"action-bar macro",
"slot", slot,
"type", safeToString(actionType),
"id", safeToString(actionID),
"subType", safeToString(actionSubtype),
"text", safeToString(actionText),
"resolvedIndex", currentMacro and currentMacro.index or "none",
"identity", identity
)
if currentMacro then
local usage = usageByIndex[currentMacro.index]
if not usage then
usage = {
macro = MacroStudio.Helpers:CopyMacro(currentMacro),
slots = {},
}
usageByIndex[currentMacro.index] = usage
end
usage.slots[#usage.slots + 1] = slot
end
end
end
self.usageByIndex = usageByIndex
MacroStudio:Debug("action-bar usage refreshed", self.scanCount)
return usageByIndex
end
function ActionBarRepository:GetUsage(macro)
if type(macro) ~= "table" or type(macro.index) ~= "number" then
return 0, {}
end
local usage = self.usageByIndex[macro.index]
if not usage or not MacroStudio.MacroRepository:SnapshotsEqual(usage.macro, macro) then
return 0, {}
end
local slots = copySlots(usage.slots)
return #slots, slots
end
function ActionBarRepository:GetScanCount()
return self.scanCount
end