-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMSA_KeyHelpers.lua
More file actions
363 lines (317 loc) · 11.8 KB
/
MSA_KeyHelpers.lua
File metadata and controls
363 lines (317 loc) · 11.8 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
-- ########################################################
-- MSA_KeyHelpers.lua
-- Key type helpers, display name, icon lookup
--
-- v2: Optimized string matching: string.find with plain flag
-- and string.sub instead of string.match (regex) for
-- hot-path key type checks.
-- ########################################################
local tonumber, tostring, type = tonumber, tostring, type
local GetItemInfo = GetItemInfo
local GetItemIcon = GetItemIcon
local strfind = string.find
local strsub = string.sub
local strmatch = string.match
local DRAFT_ICON = "Interface\\Icons\\INV_Misc_QuestionMark"
-----------------------------------------------------------
-- Key type checks (v2: optimized with find+sub)
-----------------------------------------------------------
function MSWA_IsItemKey(key)
if type(key) ~= "string" then return false end
-- Check for "item:" prefix; accepts "item:123" and "item:123:N"
if strsub(key, 1, 5) ~= "item:" then return false end
local rest = strsub(key, 6)
if rest == "" then return false end
return strmatch(rest, "^%d+$") ~= nil or strmatch(rest, "^%d+:%d+$") ~= nil
end
function MSWA_IsItemInstanceKey(key)
if type(key) ~= "string" then return false end
return strmatch(key, "^item:%d+:%d+$") ~= nil
end
function MSWA_KeyToItemID(key)
if type(key) ~= "string" then return nil end
if strsub(key, 1, 5) ~= "item:" then return nil end
local id = strmatch(key, "^item:(%d+)")
return tonumber(id)
end
function MSWA_NewItemInstanceKey(itemID)
local db = MSWA_GetDB()
db._instanceCounter = (db._instanceCounter or 0) + 1
return ("item:%d:%d"):format(itemID, db._instanceCounter)
end
function MSWA_IsDraftKey(key)
if type(key) ~= "string" then return false end
if strsub(key, 1, 6) ~= "DRAFT:" then return false end
local rest = strsub(key, 7)
return rest ~= "" and strmatch(rest, "^%d+$") ~= nil
end
function MSWA_NewDraftKey()
local db = MSWA_GetDB()
db._draftCounter = (db._draftCounter or 0) + 1
return ("DRAFT:%d"):format(db._draftCounter)
end
function MSWA_IsSpellInstanceKey(key)
if type(key) ~= "string" then return false end
if strsub(key, 1, 6) ~= "spell:" then return false end
-- Must match spell:NUMBER:NUMBER
return strmatch(key, "^spell:%d+:%d+$") ~= nil
end
function MSWA_KeyToSpellID(key)
if type(key) == "number" then return key end
if type(key) == "string" then
if strsub(key, 1, 6) ~= "spell:" then return nil end
local id = strmatch(key, "^spell:(%d+):%d+$")
if id then return tonumber(id) end
end
return nil
end
function MSWA_NewSpellInstanceKey(spellID)
local db = MSWA_GetDB()
db._instanceCounter = (db._instanceCounter or 0) + 1
return ("spell:%d:%d"):format(spellID, db._instanceCounter)
end
function MSWA_IsSpellKey(key)
return type(key) == "number" or MSWA_IsSpellInstanceKey(key)
end
-----------------------------------------------------------
-- Trinket key checks
-----------------------------------------------------------
function MSWA_IsTrinketKey(key)
return key == "trinket:13" or key == "trinket:14"
end
function MSWA_KeyToTrinketSlot(key)
if key == "trinket:13" then return 13 end
if key == "trinket:14" then return 14 end
return nil
end
--- Returns the equipped item ID for a trinket slot, or nil
function MSWA_GetTrinketItemID(slot)
if not slot then return nil end
if GetInventoryItemID then
return GetInventoryItemID("player", slot)
end
return nil
end
-----------------------------------------------------------
-- Auto Buff check
-----------------------------------------------------------
function MSWA_IsAutoBuff(key)
if key == nil then return false end
local db = MSWA_GetDB()
if not db or not db.spellSettings then return false end
local s = db.spellSettings[key] or db.spellSettings[tostring(key)]
return s and (s.auraMode == "AUTOBUFF" or s.auraMode == "BUFF_THEN_CD")
end
function MSWA_IsBuffThenCD(key)
if key == nil then return false end
local db = MSWA_GetDB()
if not db or not db.spellSettings then return false end
local s = db.spellSettings[key] or db.spellSettings[tostring(key)]
return s and s.auraMode == "BUFF_THEN_CD"
end
-----------------------------------------------------------
-- Display name / icon
-----------------------------------------------------------
function MSWA_GetDisplayNameForKey(key)
local db = MSWA_GetDB()
if db.customNames and db.customNames[key] and db.customNames[key] ~= "" then
return db.customNames[key]
end
if MSWA_IsTrinketKey(key) then
local slot = MSWA_KeyToTrinketSlot(key)
local itemID = MSWA_GetTrinketItemID(slot)
if itemID and GetItemInfo then
local name = GetItemInfo(itemID)
if name then return name end
end
return slot == 13 and "Trinket 1 (Slot 13)" or "Trinket 2 (Slot 14)"
elseif MSWA_IsDraftKey(key) then
return "???"
elseif MSWA_IsItemInstanceKey(key) then
local itemID = MSWA_KeyToItemID(key)
if itemID and GetItemInfo then
local name = GetItemInfo(itemID)
if name then return name .. " (copy)" end
end
return ("Item %d (copy)"):format(itemID or 0)
elseif MSWA_IsItemKey(key) then
local itemID = MSWA_KeyToItemID(key)
if itemID and GetItemInfo then
local name = GetItemInfo(itemID)
if name then return name end
end
return ("Item %d"):format(itemID or 0)
elseif MSWA_IsSpellInstanceKey(key) then
local spellID = MSWA_KeyToSpellID(key)
local name = spellID and MSWA_GetSpellName(spellID) or nil
return (name or "Spell") .. " (copy)"
else
local name = (type(key) == "number") and MSWA_GetSpellName(key) or nil
return name or "Unknown"
end
end
function MSWA_GetIconForKey(key)
-- Custom icon override (per-aura setting)
local db = MSWA_GetDB()
local s = select(1, MSWA_GetSpellSettings(db, key))
if s and s.customIconID then
local cid = tonumber(s.customIconID)
if cid and cid > 0 then return cid end
end
if MSWA_IsTrinketKey(key) then
local slot = MSWA_KeyToTrinketSlot(key)
if slot and GetInventoryItemTexture then
local tex = GetInventoryItemTexture("player", slot)
if tex then return tex end
end
return 136243
elseif MSWA_IsDraftKey(key) then
return DRAFT_ICON
elseif MSWA_IsItemKey(key) then
-- Handles both item:123 and item:123:N
local itemID = MSWA_KeyToItemID(key)
if itemID and GetItemIcon then
local tex = GetItemIcon(itemID)
if tex then return tex end
end
return 136243
else
local spellID = MSWA_KeyToSpellID(key)
if spellID then
return MSWA_GetSpellIcon(spellID) or 136243
end
return 136243
end
end
-----------------------------------------------------------
-- Settings key resolution (number/string equivalence)
-----------------------------------------------------------
function MSWA_ResolveSpellSettingsKey(db, key)
if not db then return key end
db.spellSettings = db.spellSettings or {}
if key == nil then return nil end
if db.spellSettings[key] ~= nil then
return key
end
local t = type(key)
if t == "number" then
local sk = tostring(key)
if db.spellSettings[sk] ~= nil then return sk end
elseif t == "string" then
local nk = tonumber(key)
if nk and db.spellSettings[nk] ~= nil then return nk end
end
return key
end
function MSWA_GetSpellSettings(db, key)
if not db or key == nil then return nil, key end
local resolved = MSWA_ResolveSpellSettingsKey(db, key)
return (db.spellSettings or {})[resolved], resolved
end
function MSWA_GetOrCreateSpellSettings(db, key)
if not db or key == nil then return nil, key end
db.spellSettings = db.spellSettings or {}
local resolved = MSWA_ResolveSpellSettingsKey(db, key)
local s = db.spellSettings[resolved]
if not s then
s = {}
db.spellSettings[resolved] = s
end
return s, resolved
end
function MSWA_KeyEquals(a, b)
if a == b then return true end
local na, nb = tonumber(a), tonumber(b)
if na and nb and na == nb then return true end
return tostring(a) == tostring(b)
end
-----------------------------------------------------------
-- Rekey: change spell/item ID, preserve all settings
-----------------------------------------------------------
function MSWA_RekeyAura(oldKey, newSpellOrItemID)
if oldKey == nil or not newSpellOrItemID then return false, "Invalid arguments" end
local db = MSWA_GetDB()
if not db then return false, "No database" end
local newID = tonumber(newSpellOrItemID)
if not newID or newID <= 0 then return false, "Invalid ID" end
-- Determine old key type and build new key
local newKey
local isItem = MSWA_IsItemKey(oldKey)
local isItemInst = MSWA_IsItemInstanceKey(oldKey)
local isSpellInst = MSWA_IsSpellInstanceKey(oldKey)
if isItemInst then
-- "item:OLD:N" -> "item:NEW:N"
local inst = strmatch(tostring(oldKey), "^item:%d+:(%d+)$")
newKey = ("item:%d:%s"):format(newID, inst)
elseif isItem then
-- "item:OLD" -> "item:NEW"
newKey = ("item:%d"):format(newID)
elseif isSpellInst then
-- "spell:OLD:N" -> "spell:NEW:N"
local inst = strmatch(tostring(oldKey), "^spell:%d+:(%d+)$")
newKey = ("spell:%d:%s"):format(newID, inst)
else
-- Numeric spell ID
newKey = newID
end
-- Check collision
if MSWA_KeyEquals(oldKey, newKey) then return false, "Same ID" end
if db.spellSettings and db.spellSettings[newKey] then
return false, "ID " .. newID .. " already exists"
end
-- 1) Move spellSettings
db.spellSettings = db.spellSettings or {}
local settings = db.spellSettings[oldKey]
if settings then
db.spellSettings[newKey] = settings
db.spellSettings[oldKey] = nil
end
-- 2) Move trackedSpells / trackedItems
if isItem or isItemInst then
if db.trackedItems and db.trackedItems[oldKey] then
db.trackedItems[newKey] = db.trackedItems[oldKey]
db.trackedItems[oldKey] = nil
end
else
if db.trackedSpells and db.trackedSpells[oldKey] then
db.trackedSpells[newKey] = db.trackedSpells[oldKey]
db.trackedSpells[oldKey] = nil
end
end
-- 3) Move customNames
if db.customNames and db.customNames[oldKey] then
db.customNames[newKey] = db.customNames[oldKey]
db.customNames[oldKey] = nil
end
-- 4) Move group assignment
if db.auraGroups and db.auraGroups[oldKey] then
local gid = db.auraGroups[oldKey]
db.auraGroups[newKey] = gid
db.auraGroups[oldKey] = nil
-- Update groupMembers array
if db.groupMembers and db.groupMembers[gid] then
local members = db.groupMembers[gid]
for i = 1, #members do
if MSWA_KeyEquals(members[i], oldKey) then
members[i] = newKey
break
end
end
end
end
-- 5) Update selection
if MSWA.selectedSpellID and MSWA_KeyEquals(MSWA.selectedSpellID, oldKey) then
MSWA.selectedSpellID = newKey
end
return true, newKey
end
-- Globals for cross-file access
_G.MSWA_GetSpellSettings = MSWA_GetSpellSettings
_G.MSWA_GetOrCreateSpellSettings = MSWA_GetOrCreateSpellSettings
_G.MSWA_ResolveSpellSettingsKey = MSWA_ResolveSpellSettingsKey
_G.MSWA_IsItemInstanceKey = MSWA_IsItemInstanceKey
_G.MSWA_NewItemInstanceKey = MSWA_NewItemInstanceKey
_G.MSWA_IsBuffThenCD = MSWA_IsBuffThenCD
_G.MSWA_IsTrinketKey = MSWA_IsTrinketKey
_G.MSWA_KeyToTrinketSlot = MSWA_KeyToTrinketSlot
_G.MSWA_GetTrinketItemID = MSWA_GetTrinketItemID