-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMacroRepository.lua
More file actions
616 lines (537 loc) · 20.7 KB
/
Copy pathMacroRepository.lua
File metadata and controls
616 lines (537 loc) · 20.7 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
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
local _, MacroStudio = ...
local MacroRepository = {
macros = {},
accountCount = 0,
characterCount = 0,
duplicateGroups = {},
}
MacroStudio.MacroRepository = MacroRepository
local FALLBACK_ACCOUNT_CAPACITY = 120
local FALLBACK_CHARACTER_CAPACITY = 30
local function accountCapacity()
local macroConstants = Constants and Constants.MacroConsts
return tonumber(macroConstants and macroConstants.MAX_ACCOUNT_MACROS)
or tonumber(MAX_ACCOUNT_MACROS)
or FALLBACK_ACCOUNT_CAPACITY
end
local function characterCapacity()
local macroConstants = Constants and Constants.MacroConsts
return tonumber(macroConstants and macroConstants.MAX_CHARACTER_MACROS)
or tonumber(MAX_CHARACTER_MACROS)
or FALLBACK_CHARACTER_CAPACITY
end
local function normalizeCount(value)
value = tonumber(value) or 0
if value < 0 then
return 0
end
return math.floor(value)
end
local function isInCombat()
return type(InCombatLockdown) == "function" and InCombatLockdown() and true or false
end
local function getSelectedIcon(index, fallback)
local provider = C_Macro and C_Macro.GetSelectedMacroIcon
if type(provider) == "function" then
local ok, icon = pcall(provider, index)
if ok and (type(icon) == "number" or (type(icon) == "string" and icon ~= "")) then
return icon
end
end
return fallback or MacroStudio.DEFAULT_ICON
end
local function iconForIdentity(macro)
return macro and (macro.selectedIcon or macro.icon)
end
function MacroRepository:GetScopeForIndex(index)
if type(index) ~= "number" then
return nil
end
return index <= accountCapacity() and "ACCOUNT" or "CHARACTER"
end
function MacroRepository:GetByIndex(index, expectedScope)
if type(GetMacroInfo) ~= "function" or type(index) ~= "number" then
return nil
end
local name, icon, body = GetMacroInfo(index)
if type(name) ~= "string" then
return nil
end
local scope = self:GetScopeForIndex(index)
if expectedScope and scope ~= expectedScope then
return nil
end
return {
index = index,
name = name,
icon = icon or MacroStudio.DEFAULT_ICON,
selectedIcon = getSelectedIcon(index, icon),
body = type(body) == "string" and body or "",
scope = scope,
}
end
function MacroRepository:DetectDuplicateNames()
local candidates = {}
self.duplicateGroups = {}
for _, macro in ipairs(self.macros) do
macro.duplicateName = nil
macro.duplicateCount = nil
if macro.name ~= "" then
local key = macro.scope .. "\031" .. macro.name:lower()
candidates[key] = candidates[key] or {}
candidates[key][#candidates[key] + 1] = macro
end
end
for key, group in pairs(candidates) do
if #group > 1 then
self.duplicateGroups[key] = group
for _, macro in ipairs(group) do
macro.duplicateName = true
macro.duplicateCount = #group
end
MacroStudio:Debug("duplicate macro name detected", group[1].scope, group[1].name, #group)
end
end
end
function MacroRepository:Refresh()
local refreshed = {}
local accountCount, characterCount = 0, 0
if type(GetNumMacros) == "function" then
accountCount, characterCount = GetNumMacros()
end
self.accountCount = normalizeCount(accountCount)
self.characterCount = normalizeCount(characterCount)
for index = 1, self.accountCount do
local macro = self:GetByIndex(index, "ACCOUNT")
if macro then
refreshed[#refreshed + 1] = macro
end
end
local firstCharacterIndex = accountCapacity() + 1
for offset = 0, self.characterCount - 1 do
local macro = self:GetByIndex(firstCharacterIndex + offset, "CHARACTER")
if macro then
refreshed[#refreshed + 1] = macro
end
end
self.macros = refreshed
self:DetectDuplicateNames()
MacroStudio:Debug("repository refreshed", #refreshed, "macros")
return refreshed
end
function MacroRepository:GetAll()
return self.macros
end
function MacroRepository:GetDuplicateGroups()
return self.duplicateGroups
end
function MacroRepository:GetCapacity(scope)
if scope == "CHARACTER" then
return self.characterCount, characterCapacity()
end
return self.accountCount, accountCapacity()
end
function MacroRepository:FindByIndex(index)
for _, macro in ipairs(self.macros) do
if macro.index == index then
return macro
end
end
return nil
end
function MacroRepository:SnapshotsEqual(first, second)
return type(first) == "table"
and type(second) == "table"
and first.index == second.index
and first.scope == second.scope
and first.name == second.name
and MacroStudio.Helpers:IconsEqual(iconForIdentity(first), iconForIdentity(second))
and first.body == second.body
end
function MacroRepository:IsSnapshotCurrent(snapshot)
if type(snapshot) ~= "table" or type(snapshot.index) ~= "number" then
return false
end
return self:SnapshotsEqual(self:GetByIndex(snapshot.index, snapshot.scope), snapshot)
end
function MacroRepository:Pickup(snapshot)
if type(snapshot) ~= "table" or type(snapshot.index) ~= "number" then
return false, nil, "This macro is no longer available to drag."
end
if isInCombat() then
return false, nil, "Macros can't be moved to action bars during combat."
end
if type(PickupMacro) ~= "function" then
return false, nil, "WoW's native macro pickup API is unavailable."
end
local current = self:GetByIndex(snapshot.index, snapshot.scope)
if not self:SnapshotsEqual(current, snapshot) then
return false, nil, "This macro changed before the drag started. Try again after the list updates."
end
-- Match Blizzard's Macro UI: hand the exact native slot to WoW and let
-- its cursor and action-bar system own the payload from this point on.
PickupMacro(current.index)
return true, current
end
function MacroRepository:ResolveLatest(snapshot, refreshFirst)
if type(snapshot) ~= "table" then
return nil, "No macro is selected."
end
if refreshFirst ~= false then
self:Refresh()
end
local sameIndex = self:FindByIndex(snapshot.index)
local exactMatch
local exactMatches = 0
for _, macro in ipairs(self.macros) do
if macro.scope == snapshot.scope
and macro.name == snapshot.name
and MacroStudio.Helpers:IconsEqual(iconForIdentity(macro), iconForIdentity(snapshot))
and macro.body == snapshot.body then
exactMatch = macro
exactMatches = exactMatches + 1
end
end
if exactMatches == 1 then
return exactMatch
end
local likelyMatch
local likelyMatches = 0
for _, macro in ipairs(self.macros) do
if macro.scope == snapshot.scope
and macro.name == snapshot.name
and MacroStudio.Helpers:IconsEqual(iconForIdentity(macro), iconForIdentity(snapshot)) then
likelyMatch = macro
likelyMatches = likelyMatches + 1
end
end
if likelyMatches == 1 then
return likelyMatch
elseif likelyMatches > 1 then
return nil, "The selected macro is ambiguous because duplicate macros match it."
end
local iconAndBodyMatch
local iconAndBodyMatches = 0
for _, macro in ipairs(self.macros) do
if macro.scope == snapshot.scope
and MacroStudio.Helpers:IconsEqual(iconForIdentity(macro), iconForIdentity(snapshot))
and macro.body == snapshot.body then
iconAndBodyMatch = macro
iconAndBodyMatches = iconAndBodyMatches + 1
end
end
if iconAndBodyMatches == 1 then
return iconAndBodyMatch
elseif iconAndBodyMatches > 1 then
return nil, "The selected macro is ambiguous after its name changed outside MacroStudio."
end
local nameAndBodyMatch
local nameAndBodyMatches = 0
for _, macro in ipairs(self.macros) do
if macro.scope == snapshot.scope
and macro.name == snapshot.name
and macro.body == snapshot.body then
nameAndBodyMatch = macro
nameAndBodyMatches = nameAndBodyMatches + 1
end
end
if nameAndBodyMatches == 1 then
return nameAndBodyMatch
elseif nameAndBodyMatches > 1 then
return nil, "The selected macro is ambiguous after its icon changed outside MacroStudio."
end
if sameIndex
and sameIndex.scope == snapshot.scope
and sameIndex.name == snapshot.name
and MacroStudio.Helpers:IconsEqual(iconForIdentity(sameIndex), iconForIdentity(snapshot)) then
return nil, "The selected index now matches multiple duplicate macros, so MacroStudio will not guess."
end
if exactMatches > 1 then
return nil, "The selected macro is ambiguous because duplicate macros match it."
end
return nil, "The selected macro no longer exists or changed identity."
end
local function sameSavedIdentity(first, second)
return type(first) == "table"
and type(second) == "table"
and first.scope == second.scope
and first.name == second.name
and MacroStudio.Helpers:IconsEqual(iconForIdentity(first), iconForIdentity(second))
and first.body == second.body
end
function MacroRepository:ResolveExternalConflict(snapshot, previousMacros, refreshFirst)
if type(snapshot) ~= "table" or type(previousMacros) ~= "table" then
return nil, "ambiguous", "This macro changed outside MacroStudio and could not be reconciled safely."
end
if refreshFirst ~= false then
self:Refresh()
end
local baselineFound = false
local previousScopeCount = 0
local currentScopeCount = 0
local previousIdentityCount = 0
local currentIdentityCount = 0
local currentIdentityMatch
for _, macro in ipairs(previousMacros) do
if macro.scope == snapshot.scope then
previousScopeCount = previousScopeCount + 1
if self:SnapshotsEqual(macro, snapshot) then
baselineFound = true
end
if sameSavedIdentity(macro, snapshot) then
previousIdentityCount = previousIdentityCount + 1
end
end
end
if not baselineFound then
return nil, "ambiguous", "This macro changed outside MacroStudio and could not be reconciled safely."
end
for _, macro in ipairs(self.macros) do
if macro.scope == snapshot.scope then
currentScopeCount = currentScopeCount + 1
if sameSavedIdentity(macro, snapshot) then
currentIdentityCount = currentIdentityCount + 1
currentIdentityMatch = macro
end
end
end
-- A unique full saved identity may move when unrelated native indices shift.
if previousIdentityCount == 1 and currentIdentityCount == 1 then
return currentIdentityMatch, "resolved"
end
-- When only the selected slot changed, every surrounding slot and the
-- scope count remain stable. This safely supports external edits that
-- replace name, icon, and body together without guessing by name.
local sameIndex = self:FindByIndex(snapshot.index)
if sameIndex and sameIndex.scope == snapshot.scope and previousScopeCount == currentScopeCount then
local stableTopology = true
for _, previous in ipairs(previousMacros) do
if previous.scope == snapshot.scope and previous.index ~= snapshot.index then
if not self:SnapshotsEqual(previous, self:FindByIndex(previous.index)) then
stableTopology = false
break
end
end
end
if stableTopology then
return sameIndex, "resolved"
end
end
if currentScopeCount < previousScopeCount then
if not sameIndex then
return nil, "deleted", "The selected macro was deleted outside MacroStudio."
end
for _, previous in ipairs(previousMacros) do
if previous.scope == snapshot.scope
and previous.index ~= snapshot.index
and sameSavedIdentity(previous, sameIndex) then
return nil, "deleted", "The selected macro was deleted outside MacroStudio."
end
end
end
return nil, "ambiguous", "This macro changed outside MacroStudio and could not be reconciled safely."
end
function MacroRepository:NormalizeMacroName(value)
local name = type(value) == "string" and value or ""
return (name:gsub('"', ""))
end
function MacroRepository:ValidateMacroName(value)
local name = self:NormalizeMacroName(value)
if MacroStudio.Helpers:Trim(name) == "" then
return false, "Enter a macro name."
end
if MacroStudio.Helpers:TextLength(name) > MacroStudio.MAX_NAME_LENGTH then
return false, string.format("Macro names are limited to %d characters.", MacroStudio.MAX_NAME_LENGTH)
end
return true, nil, name
end
function MacroRepository:ValidateMacroContent(request)
request = type(request) == "table" and request or {}
local validName, message, name = self:ValidateMacroName(request.name)
if not validName then
return false, message
end
local body = type(request.body) == "string" and request.body or ""
if MacroStudio.Helpers:TextLength(body) > MacroStudio.MAX_BODY_LENGTH then
return false, string.format("Macro bodies are limited to %d characters.", MacroStudio.MAX_BODY_LENGTH)
end
local icon = request.icon
if type(icon) ~= "number" and (type(icon) ~= "string" or icon == "") then
return false, "Choose a valid macro icon."
end
return true, nil, {
name = name,
icon = icon,
body = body,
}
end
function MacroRepository:ValidateCreateRequest(request)
request = type(request) == "table" and request or {}
local scope = request.scope
if isInCombat() then
return false, "Combat Lockdown - native macros cannot be created until combat ends."
end
if type(CreateMacro) ~= "function" then
return false, "The WoW CreateMacro API is unavailable."
end
local validContent, contentMessage = self:ValidateMacroContent(request)
if not validContent then
return false, contentMessage
end
if scope ~= "ACCOUNT" and scope ~= "CHARACTER" then
return false, "Choose Account or Character scope."
end
local count, capacity = self:GetCapacity(scope)
if count >= capacity then
return false, string.format("%s macros are full (%d/%d).", scope == "ACCOUNT" and "Account" or "Character", count, capacity)
end
return true
end
local function matchesCreatedResult(macro, request)
return macro
and macro.scope == request.scope
and macro.name == request.name
and MacroStudio.Helpers:IconsEqual(iconForIdentity(macro), request.icon)
and macro.body == request.body
end
function MacroRepository:Create(request)
request = type(request) == "table" and request or {}
request = {
name = MacroStudio.Helpers:Trim(self:NormalizeMacroName(request.name)),
body = type(request.body) == "string" and request.body or "",
scope = request.scope,
icon = request.icon or MacroStudio.DEFAULT_ICON,
}
self:Refresh()
local valid, message = self:ValidateCreateRequest(request)
if not valid then
return false, nil, message
end
local ok, returnedIndex = pcall(CreateMacro, request.name, request.icon, request.body, request.scope == "CHARACTER")
if not ok then
return false, nil, "WoW rejected the macro creation request: " .. tostring(returnedIndex)
end
self:Refresh()
local created = tonumber(returnedIndex) and self:FindByIndex(tonumber(returnedIndex)) or nil
if matchesCreatedResult(created, request) then
return true, created
end
local uniqueMatch
local matchCount = 0
for _, macro in ipairs(self.macros) do
if matchesCreatedResult(macro, request) then
uniqueMatch = macro
matchCount = matchCount + 1
end
end
if matchCount == 1 then
return true, uniqueMatch
end
if matchCount > 1 then
return true, nil, "Created, but duplicate macros made automatic selection ambiguous."
end
return false, nil, "WoW did not confirm the new macro."
end
local function matchesSavedResult(macro, original, draft)
return macro
and macro.scope == original.scope
and macro.name == draft.name
and MacroStudio.Helpers:IconsEqual(iconForIdentity(macro), draft.icon)
and macro.body == draft.body
end
function MacroRepository:Update(snapshot, draft)
MacroStudio:Debug("save attempted", snapshot and snapshot.index or "no index")
if type(snapshot) ~= "table" or type(snapshot.index) ~= "number" then
return false, nil, "No macro is selected."
end
if type(draft) == "string" then
draft = {
name = snapshot.name,
icon = iconForIdentity(snapshot),
body = draft,
}
end
if type(draft) ~= "table" then
return false, nil, "The editor buffer is unavailable."
end
if isInCombat() then
return false, nil, "Combat Lockdown - native macros cannot be modified until combat ends."
end
local valid, validationMessage, normalized = self:ValidateMacroContent(draft)
if not valid then
return false, nil, validationMessage .. " Nothing was saved."
end
draft = normalized
local current = self:GetByIndex(snapshot.index, snapshot.scope)
if not current then
return false, nil, "The selected macro no longer exists at its expected index."
end
if not self:SnapshotsEqual(current, snapshot) then
return false, nil, "This macro changed outside MacroStudio. Revert to load the latest version."
end
if type(EditMacro) ~= "function" then
return false, nil, "The WoW EditMacro API is unavailable."
end
local ok, returnedIndex = pcall(EditMacro, current.index, draft.name, draft.icon, draft.body)
if not ok then
return false, nil, "WoW rejected the macro update request: " .. tostring(returnedIndex)
end
self:Refresh()
local returnedMacro = tonumber(returnedIndex) and self:FindByIndex(tonumber(returnedIndex)) or nil
if matchesSavedResult(returnedMacro, current, draft) then
if returnedMacro.index ~= current.index then
MacroStudio:Debug("macro index changed after save", current.index, "->", returnedMacro.index)
end
MacroStudio:Debug("save succeeded", returnedMacro.index)
return true, returnedMacro
end
local originalIndexMacro = self:FindByIndex(current.index)
if matchesSavedResult(originalIndexMacro, current, draft) then
MacroStudio:Debug("save succeeded", originalIndexMacro.index)
return true, originalIndexMacro
end
local uniqueMatch
local matchCount = 0
for _, macro in ipairs(self.macros) do
if matchesSavedResult(macro, current, draft) then
uniqueMatch = macro
matchCount = matchCount + 1
end
end
if matchCount == 1 then
MacroStudio:Debug("macro index changed after save", current.index, "->", uniqueMatch.index)
MacroStudio:Debug("save succeeded", uniqueMatch.index)
return true, uniqueMatch
end
if matchCount > 1 then
return true, nil, "Saved, but duplicate macros made automatic re-selection ambiguous. Refresh and select the macro again."
end
return false, nil, "WoW did not confirm the macro update. The editor buffer was preserved."
end
function MacroRepository:Delete(snapshot)
if type(snapshot) ~= "table" or type(snapshot.index) ~= "number" then
return false, "No macro is selected."
end
if isInCombat() then
return false, "Combat Lockdown - native macros cannot be deleted until combat ends."
end
if type(DeleteMacro) ~= "function" then
return false, "The WoW DeleteMacro API is unavailable."
end
self:Refresh()
local current = self:GetByIndex(snapshot.index, snapshot.scope)
if not current or not self:SnapshotsEqual(current, snapshot) then
return false, "This macro changed outside MacroStudio. Revert before deleting it."
end
local previousCount = current.scope == "ACCOUNT" and self.accountCount or self.characterCount
local ok, failure = pcall(DeleteMacro, current.index)
if not ok then
return false, "WoW rejected the delete request: " .. tostring(failure)
end
self:Refresh()
local currentCount = current.scope == "ACCOUNT" and self.accountCount or self.characterCount
if currentCount == previousCount - 1 then
return true
end
return false, "WoW did not confirm that the macro was deleted."
end