-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathActionBarSaver.lua
526 lines (435 loc) · 15.7 KB
/
ActionBarSaver.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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
--[[
Action Bar Saver, Shadowed
]]
ActionBarSaver = select(2, ...)
local ABS = ActionBarSaver
local L = ABS.locals
local restoreErrors, spellCache, macroCache, macroNameCache, highestRanks = {}, {}, {}, {}, {}
local iconCache, playerClass
local MAX_MACROS = 54
local MAX_CHAR_MACROS = 18
local MAX_GLOBAL_MACROS = 36
local MAX_ACTION_BUTTONS = 144
local POSSESSION_START = 121
local POSSESSION_END = 132
function ABS:OnInitialize()
local defaults = {
macro = false,
checkCount = false,
restoreRank = true,
spellSubs = {},
sets = {}
}
ActionBarSaverDB = ActionBarSaverDB or {}
-- Load defaults in
for key, value in pairs(defaults) do
if( ActionBarSaverDB[key] == nil ) then
ActionBarSaverDB[key] = value
end
end
for classToken in pairs(RAID_CLASS_COLORS) do
ActionBarSaverDB.sets[classToken] = ActionBarSaverDB.sets[classToken] or {}
end
self.db = ActionBarSaverDB
playerClass = select(2, UnitClass("player"))
end
-- Text "compression" so it can be stored in our format fine
function ABS:CompressText(text)
text = string.gsub(text, "\n", "/n")
text = string.gsub(text, "/n$", "")
text = string.gsub(text, "||", "/124")
return string.trim(text)
end
function ABS:UncompressText(text)
text = string.gsub(text, "/n", "\n")
text = string.gsub(text, "/124", "|")
return string.trim(text)
end
-- Restore a saved profile
function ABS:SaveProfile(name)
self.db.sets[playerClass][name] = self.db.sets[playerClass][name] or {}
local set = self.db.sets[playerClass][name]
for actionID=1, MAX_ACTION_BUTTONS do
set[actionID] = nil
local type, id, subType, extraID = GetActionInfo(actionID)
if( type and id and ( actionID < POSSESSION_START or actionID > POSSESSION_END ) ) then
-- DB Format: <type>|<id>|<binding>|<name>|<extra ...>
-- Save a companion
if( type == "companion" ) then
set[actionID] = string.format("%s|%s|%s|%s|%s|%s", type, id, "", "", subType, "")
-- Save an equipment set
elseif( type == "equipmentset" ) then
set[actionID] = string.format("%s|%s|%s", type, id, "")
-- Save an item
elseif( type == "item" ) then
set[actionID] = string.format("%s|%d|%s|%s", type, id, "", (GetItemInfo(id)) or "")
-- Save a spell
elseif( type == "spell" and id > 0 ) then
local spellName, spellStance = GetSpellInfo(id)
if( spellName and spellStance ) then
set[actionID] = string.format("%s|%d|%s|%s|%s|%s", type, id, "", spellName, spellStance or "", extraID or "")
end
-- Save a macro
elseif( type == "macro" ) then
local name, icon, macro = GetMacroInfo(id)
if( name and icon and macro ) then
set[actionID] = string.format("%s|%d|%s|%s|%s|%s", type, actionID, "", self:CompressText(name), icon, self:CompressText(macro))
end
-- Flyout mnenu
elseif( type == "flyout" ) then
set[actionID] = string.format("%s|%d|%s|%s|%s", type, id, "", (GetFlyoutInfo(id)), "")
end
end
end
self:Print(string.format(L["Saved profile %s!"], name))
end
-- Finds the macroID in case it's changed
function ABS:FindMacro(id, name, data)
if( macroCache[id] == data ) then
return id
end
-- No such luck, check text
for id, currentMacro in pairs(macroCache) do
if( currentMacro == data ) then
return id
end
end
-- Still no luck, let us try name
if( macroNameCache[name] ) then
return macroNameCache[name]
end
return nil
end
-- Restore any macros that don't exist
function ABS:RestoreMacros(set)
local perCharacter = true
for id, data in pairs(set) do
local type, id, binding, macroName, macroIcon, macroData = string.split("|", data)
if( type == "macro" ) then
-- Do we already have a macro?
local macroID = self:FindMacro(id, macroName, macroData)
if( not macroID ) then
local globalNum, charNum = GetNumMacros()
-- Make sure we aren't at the limit
if( globalNum == MAX_GLOBAL_MACROS and charNum == MAX_CHAR_MACROS ) then
table.insert(restoreErrors, L["Unable to restore macros, you already have 36 global and 18 per character ones created."])
break
-- We ran out of space for per character, so use global
elseif( charNum == MAX_CHAR_MACROS ) then
perCharacter = false
end
-- When creating a macro, we have to pass the icon id not the icon path
if( not iconCache ) then
iconCache = {}
for i=1, GetNumMacroIcons() do
iconCache[(GetMacroIconInfo(i))] = i
end
end
macroName = self:UncompressText(macroName)
-- No macro name means a space has to be used or else it won't be created and saved
CreateMacro(macroName == "" and " " or macroName, iconCache[macroIcon] or 1, self:UncompressText(macroData), nil, perCharacter)
end
end
end
-- Recache macros due to any additions
local blacklist = {}
for i=1, MAX_MACROS do
local name, icon, macro = GetMacroInfo(i)
if( name ) then
-- If there are macros with the same name, then blacklist and don't look by name
if( macroNameCache[name] ) then
blacklist[name] = true
macroNameCache[name] = i
elseif( not blacklist[name] ) then
macroNameCache[name] = i
end
end
macroCache[i] = macro and self:CompressText(macro) or nil
end
end
-- Restore a saved profile
function ABS:RestoreProfile(name, overrideClass)
local set = self.db.sets[overrideClass or playerClass][name]
if( not set ) then
self:Print(string.format(L["No profile with the name \"%s\" exists."], set))
return
elseif( InCombatLockdown() ) then
self:Print(String.format(L["Unable to restore profile \"%s\", you are in combat."], set))
return
end
table.wipe(macroCache)
table.wipe(spellCache)
table.wipe(macroNameCache)
-- Cache spells
for book=1, MAX_SKILLLINE_TABS do
local _, _, offset, numSpells = GetSpellTabInfo(book)
for i=1, numSpells do
local index = offset + i
local spell, stance = GetSpellBookItemName(index, BOOKTYPE_SPELL)
-- This way we restore the max rank of spells
spellCache[spell] = index
spellCache[string.lower(spell)] = index
if( stance and stance ~= "" ) then
spellCache[spell .. stance] = index
end
end
end
-- Cache macros
local blacklist = {}
for i=1, MAX_MACROS do
local name, icon, macro = GetMacroInfo(i)
if( name ) then
-- If there are macros with the same name, then blacklist and don't look by name
if( macroNameCache[name] ) then
blacklist[name] = true
macroNameCache[name] = i
elseif( not blacklist[name] ) then
macroNameCache[name] = i
end
end
macroCache[i] = macro and self:CompressText(macro) or nil
end
-- Check if we need to restore any missing macros
if( self.db.macro ) then
self:RestoreMacros(set)
end
-- Start fresh with nothing on the cursor
ClearCursor()
-- Save current sound setting
local soundToggle = GetCVar("Sound_EnableAllSound")
-- Turn sound off
SetCVar("Sound_EnableAllSound", 0)
for i=1, MAX_ACTION_BUTTONS do
if( i < POSSESSION_START or i > POSSESSION_END ) then
local type, id = GetActionInfo(i)
-- Clear the current spot
if( id or type ) then
PickupAction(i)
ClearCursor()
end
-- Restore this spot
if( set[i] ) then
self:RestoreAction(i, string.split("|", set[i]))
end
end
end
-- Restore old sound setting
SetCVar("Sound_EnableAllSound", soundToggle)
-- Done!
if( #(restoreErrors) == 0 ) then
self:Print(string.format(L["Restored profile %s!"], name))
else
self:Print(string.format(L["Restored profile %s, failed to restore %d buttons type /abs errors for more information."], name, #(restoreErrors)))
end
end
function ABS:RestoreAction(i, type, actionID, binding, ...)
-- Restore a spell, flyout or companion
if( type == "spell" or type == "flyout" or type == "companion" ) then
local spellName, spellRank = ...
if( ( self.db.restoreRank or spellRank == "" ) and spellCache[spellName] ) then
PickupSpellBookItem(spellCache[spellName], BOOKTYPE_SPELL)
elseif( spellRank ~= "" and spellCache[spellName .. spellRank] ) then
PickupSpellBookItem(spellCache[spellName .. spellRank], BOOKTYPE_SPELL)
else
PickupSpell(actionID)
end
if( GetCursorInfo() ~= type ) then
-- Bad restore, check if we should link at all
local lowerSpell = string.lower(spellName)
for spell, linked in pairs(self.db.spellSubs) do
if( lowerSpell == spell and spellCache[linked] ) then
self:RestoreAction(i, type, actionID, binding, linked, nil, arg3)
return
elseif( lowerSpell == linked and spellCache[spell] ) then
self:RestoreAction(i, type, actionID, binding, spell, nil, arg3)
return
end
end
table.insert(restoreErrors, string.format(L["Unable to restore spell \"%s\" to slot #%d, it does not appear to have been learned yet."], spellName, i))
ClearCursor()
return
end
PlaceAction(i)
-- Restore flyout
elseif( type == "flyout" ) then
PickupSpell(actionID)
if( GetCursorInfo() ~= "flyout" ) then
table.insert(restoreErrors, string.format(L["Unable to restore flyout spell \"%s\" to slot #%d, it does not appear to exist anymore."], actionID, i))
ClearCursor()
return
end
PlaceAction(i)
-- Restore an equipment set button
elseif( type == "equipmentset" ) then
local slotID = -1
for i=1, GetNumEquipmentSets() do
if( GetEquipmentSetInfo(i) == actionID ) then
slotID = i
break
end
end
PickupEquipmentSet(slotID)
if( GetCursorInfo() ~= "equipmentset" ) then
table.insert(restoreErrors, string.format(L["Unable to restore equipment set \"%s\" to slot #%d, it does not appear to exist anymore."], actionID, i))
ClearCursor()
return
end
PlaceAction(i)
-- Restore an item
elseif( type == "item" ) then
PickupItem(actionID)
if( GetCursorInfo() ~= type ) then
local itemName = select(i, ...)
table.insert(restoreErrors, string.format(L["Unable to restore item \"%s\" to slot #%d, cannot be found in inventory."], itemName and itemName ~= "" and itemName or actionID, i))
ClearCursor()
return
end
PlaceAction(i)
-- Restore a macro
elseif( type == "macro" ) then
local name, _, content = ...
PickupMacro(self:FindMacro(actionID, name, content or -1))
if( GetCursorInfo() ~= type ) then
table.insert(restoreErrors, string.format(L["Unable to restore macro id #%d to slot #%d, it appears to have been deleted."], actionID, i))
ClearCursor()
return
end
PlaceAction(i)
end
end
function ABS:Print(msg)
DEFAULT_CHAT_FRAME:AddMessage("|cff33ff99ABS|r: " .. msg)
end
SLASH_ACTIONBARSAVER1 = nil
SlashCmdList["ACTIONBARSAVER"] = nil
SLASH_ABS1 = "/abs"
SLASH_ABS2 = "/actionbarsaver"
SlashCmdList["ABS"] = function(msg)
msg = msg or ""
local cmd, arg = string.split(" ", msg, 2)
cmd = string.lower(cmd or "")
arg = string.lower(arg or "")
local self = ABS
-- Profile saving
if( cmd == "save" and arg ~= "" ) then
self:SaveProfile(arg)
-- Spell sub
elseif( cmd == "link" and arg ~= "" ) then
local first, second = string.match(arg, "\"(.+)\" \"(.+)\"")
first = string.trim(first or "")
second = string.trim(second or "")
if( first == "" or second == "" ) then
self:Print(L["Invalid spells passed, remember you must put quotes around both of them."])
return
end
self.db.spellSubs[first] = second
self:Print(string.format(L["Spells \"%s\" and \"%s\" are now linked."], first, second))
-- Profile restoring
elseif( cmd == "restore" and arg ~= "" ) then
for i=#(restoreErrors), 1, -1 do table.remove(restoreErrors, i) end
if( not self.db.sets[playerClass][arg] ) then
self:Print(string.format(L["Cannot restore profile \"%s\", you can only restore profiles saved to your class."], arg))
return
end
self:RestoreProfile(arg, playerClass)
-- Profile renaming
elseif( cmd == "rename" and arg ~= "" ) then
local old, new = string.split(" ", arg, 2)
new = string.trim(new or "")
old = string.trim(old or "")
if( new == old ) then
self:Print(string.format(L["You cannot rename \"%s\" to \"%s\" they are the same profile names."], old, new))
return
elseif( new == "" ) then
self:Print(string.format(L["No name specified to rename \"%s\" to."], old))
return
elseif( self.db.sets[playerClass][new] ) then
self:Print(string.format(L["Cannot rename \"%s\" to \"%s\" a profile already exists for %s."], old, new, (UnitClass("player"))))
return
elseif( not self.db.sets[playerClass][old] ) then
self:Print(string.format(L["No profile with the name \"%s\" exists."], old))
return
end
self.db.sets[playerClass][new] = CopyTable(self.db.sets[playerClass][old])
self.db.sets[playerClass][old] = nil
self:Print(string.format(L["Renamed \"%s\" to \"%s\""], old, new))
-- Restore errors
elseif( cmd == "errors" ) then
if( #(restoreErrors) == 0 ) then
self:Print(L["No errors found!"])
return
end
self:Print(string.format(L["Errors found: %d"], #(restoreErrors)))
for _, text in pairs(restoreErrors) do
DEFAULT_CHAT_FRAME:AddMessage(text)
end
-- Delete profile
elseif( cmd == "delete" ) then
self.db.sets[playerClass][arg] = nil
self:Print(string.format(L["Deleted saved profile %s."], arg))
-- List profiles
elseif( cmd == "list" ) then
local classes = {}
local setList = {}
for class, sets in pairs(self.db.sets) do
table.insert(classes, class)
end
table.sort(classes, function(a, b)
return a < b
end)
for _, class in pairs(classes) do
for i=#(setList), 1, -1 do table.remove(setList, i) end
for setName in pairs(self.db.sets[class]) do
table.insert(setList, setName)
end
if( #(setList) > 0 ) then
DEFAULT_CHAT_FRAME:AddMessage(string.format("|cff33ff99%s|r: %s", L[class] or "???", table.concat(setList, ", ")))
end
end
-- Macro restoring
elseif( cmd == "macro" ) then
self.db.macro = not self.db.macro
if( self.db.macro ) then
self:Print(L["Auto macro restoration is now enabled!"])
else
self:Print(L["Auto macro restoration is now disabled!"])
end
-- Item counts
elseif( cmd == "count" ) then
self.db.checkCount = not self.db.checkCount
if( self.db.checkCount ) then
self:Print(L["Checking item count is now enabled!"])
else
self:Print(L["Checking item count is now disabled!"])
end
-- Rank restore
elseif( cmd == "rank" ) then
self.db.restoreRank = not self.db.restoreRank
if( self.db.restoreRank ) then
self:Print(L["Auto restoring highest spell rank is now enabled!"])
else
self:Print(L["Auto restoring highest spell rank is now disabled!"])
end
-- Halp
else
self:Print(L["Slash commands"])
DEFAULT_CHAT_FRAME:AddMessage(L["/abs save <profile> - Saves your current action bar setup under the given profile."])
DEFAULT_CHAT_FRAME:AddMessage(L["/abs restore <profile> - Changes your action bars to the passed profile."])
DEFAULT_CHAT_FRAME:AddMessage(L["/abs delete <profile> - Deletes the saved profile."])
DEFAULT_CHAT_FRAME:AddMessage(L["/abs rename <oldProfile> <newProfile> - Renames a saved profile from oldProfile to newProfile."])
DEFAULT_CHAT_FRAME:AddMessage(L["/abs link \"<spell 1>\" \"<spell 2>\" - Links a spell with another, INCLUDE QUOTES for example you can use \"Shadowmeld\" \"War Stomp\" so if War Stomp can't be found, it'll use Shadowmeld and vica versa."])
DEFAULT_CHAT_FRAME:AddMessage(L["/abs count - Toggles checking if you have the item in your inventory before restoring it, use if you have disconnect issues when restoring."])
DEFAULT_CHAT_FRAME:AddMessage(L["/abs macro - Attempts to restore macros that have been deleted for a profile."])
DEFAULT_CHAT_FRAME:AddMessage(L["/abs rank - Toggles if ABS should restore the highest rank of the spell, or the one saved originally."])
DEFAULT_CHAT_FRAME:AddMessage(L["/abs list - Lists all saved profiles."])
end
end
-- Check if we need to load
local frame = CreateFrame("Frame")
frame:RegisterEvent("ADDON_LOADED")
frame:SetScript("OnEvent", function(self, event, addon)
if( addon == "ActionBarSaver" ) then
ABS:OnInitialize()
self:UnregisterEvent("ADDON_LOADED")
end
end)