forked from corporategoth/rotationmaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.lua
765 lines (676 loc) · 20.2 KB
/
utils.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
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
local addon_name, addon = ...
local AceConsole = LibStub("AceConsole-3.0")
local L = LibStub("AceLocale-3.0"):GetLocale(addon_name)
local libc = LibStub("LibCompress")
local _G, tostring, tonumber, pairs, color, unpack, type, string = _G, tostring, tonumber, pairs, color, unpack, type, string
local random, floor = math.random, math.floor
local operators = addon.operators
addon.PopupError = function(s, onaccept)
StaticPopupDialogs["ROTATIONMASTER_ERROR"] = {
text = s,
button1 = ACCEPT,
OnAccept = onaccept,
showAlert = 1,
timeout = 0,
whileDead = 1,
hideOnEscape = 1
}
StaticPopup_Show("ROTATIONMASTER_ERROR")
end
addon.HideOnEscape = function(frame)
frame.frame:EnableKeyboard(true)
frame.frame:SetPropagateKeyboardInput(true)
frame.frame:SetScript("OnKeyDown", function (self, key)
self:SetPropagateKeyboardInput(key ~= "ESCAPE")
if key == "ESCAPE" then
frame:Hide()
end
end)
end
addon.uuid = function()
local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
return string.gsub(template, '[xy]', function (c)
local v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
return string.format('%x', v)
end)
end
addon.compare = function(operator, left, right)
if operator == "LESSTHAN" then
return left < right
elseif operator == "LESSTHANOREQUALS" then
return left <= right
elseif operator == "GREATERTHAN" then
return left > right
elseif operator == "GREATERTHANOREQUALS" then
return left >= right
elseif operator == "EQUALS" then
return left == right
elseif operator == "NOTEQUALS" then
return left ~= right
elseif operator == "DIVISIBLE" then
return (left % right) == 0
else
addon:warn("Invalid Operator %s", operator)
end
end
addon.nullable = function(value, default)
if value == nil then
if default ~= nil then
return color.RED .. default .. color.RESET
else
return color.RED .. L["<value>"] .. color.RESET
end
end
return value
end
addon.nullable_value = function(value, array, default)
if value == nil or array == nil or type(array) ~= "table" then
if default ~= nil then
return color.RED .. default .. color.RESET
else
return color.RED .. L["<value>"] .. color.RESET
end
end
return addon.nullable(array[value], default)
end
addon.playerize = function(unit, player, nonplayer)
if (unit == "player") then
return player
else
return nonplayer
end
end
addon.compareString = function(operator, left, right)
return left .. " " .. addon.nullable(operators[operator], L["<operator>"]) .. " " .. right
end
addon.keys = function(array)
local rv = {}
for k,_ in pairs(array) do
table.insert(rv, k)
end
-- Sort the keys
table.sort(rv, function (lhs, rhs) return array[lhs] < array[rhs] end)
return rv
end
addon.spairs = function (t, order)
-- collect the keys
local keys = {}
for k in pairs(t) do keys[#keys+1] = k end
-- if order function given, sort by it by passing the table and keys a, b,
-- otherwise just sort the keys
if order then
table.sort(keys, function(a,b) return order(t, a, b) end)
else
table.sort(keys)
end
-- return the iterator function
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end
addon.tomap = function(array)
local rv = {}
for k,v in pairs(array) do
rv[tostring(k)] = v
end
return rv
end
addon.index = function(array, value)
if not array then return nil end
for idx,v in pairs(array) do
if (v == value) then
return idx
end
end
end
addon.isin = function(array, value)
return array[value] ~= nil
end
addon.tablelength = function(T)
if T == nil then return 0 end
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
addon.round = function (num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return floor(num * mult + 0.5) / mult
end
addon.isint = function(num)
if type(num) == "number" then
return floor(num) == num
elseif type(num) == "string" then
local tmp = tonumber(num)
if tmp ~= nil then
return tostring(floor(tmp)) == num
end
end
return false
end
addon.starts_with = function(str, start)
return str:sub(1, #start) == start
end
addon.ends_with = function(str, ending)
return ending == "" or str:sub(-#ending) == ending
end
addon.multiinsert = function(t, values)
for i,v in ipairs(values) do
table.insert(t, i, v)
end
end
addon.cleanArray = function(array, except, invert)
if array == nil then
return
end
if invert == nil then
invert = false
end
for k,_ in pairs(array) do
local skip = invert
if except ~= nil then
for _,v2 in pairs(except) do
if (k == v2) then
skip = not invert
break
end
end
end
if not skip then
array[k] = nil
end
end
end
addon.deepcopy = function(array, except, invert)
if array == nil then
return nil
end
if invert == nil then
invert = false
end
local rv = {}
for k,v in pairs(array) do
local skip = invert
if except ~= nil then
for _,v2 in pairs(except) do
if (k == v2) then
skip = not invert
break
end
end
end
if not skip then
rv[k] = v
end
end
return rv
end
addon.compareArray = function(lhs, rhs)
for k,v in pairs(lhs) do
if rhs[k] ~= v then
return false
end
end
for k,v in pairs(rhs) do
if lhs[k] ~= v then
return false
end
end
return true
end
local cacheHits = 0
local cacheMisses = 0
local function getCachedInternal(cache, recordnil, func, ...)
if func == nil then
return
end
if cache == nil then
return func(...)
end
local args = { ... }
if cache[func] == nil then
cache[func] = {}
end
local hash = libc:fcs16init()
for i=1,#args do
hash = libc:fcs16update(hash, tostring(args[i]))
end
hash = libc:fcs16final(hash)
if cache[func][hash] ~= nil then
cacheHits = cacheHits + 1
return unpack(cache[func][hash])
end
cacheMisses = cacheMisses + 1
local result
if (type(func) == "function") then
result = { func(...) }
elseif type(func) == "string" then
result = { _G[func](...) }
else
return nil
end
if #result > 0 or recordnil then
cache[func][hash] = result
end
return unpack(result)
end
addon.getCached = function(cache, func, ...)
return getCachedInternal(cache, true, func, ...)
end
addon.getRetryCached = function(cache, func, ...)
return getCachedInternal(cache, false, func, ...)
end
function addon:ReportCacheStats()
addon:debug(L["Cache hit rate at %.02f%%"], (cacheHits / (cacheHits + cacheMisses)) * 100)
cacheHits = 0
cacheMisses = 0
end
-- pass in the spec BOOKTYPE_PET ("pet") to get pet spells
addon.isSpellOnSpec = function(spec, spellid)
if spec ~= nil and spellid ~= nil and addon.specSpells[spec] ~= nil then
return addon.specSpells[spec][spellid] ~= nil
end
return false
end
addon.isActivePetSpell = function(spellid)
return FindSpellBookSlotBySpellID(spellid, true) ~= nil
end
-- pass in the spec BOOKTYPE_PET ("pet") to get pet spells
addon.getSpecSpellID = function(spec, name)
if spec ~= nil and name ~= nil and addon.specSpellsReverse[spec] ~= nil then
local spellid = addon.specSpellsReverse[spec][name]
if addon.isSpellOnSpec(spec, spellid) then
return spellid
end
end
return nil
end
function addon:verbose(message, ...)
if self.db.profile.loglevel >= 4 then
AceConsole:Printf(color.WHITE .. "[" .. color.CYAN .. addon.pretty_name .. color.WHITE .. "] " .. color.DEBUG .. message .. color.RESET, ...)
end
end
function addon:debug(message, ...)
if self.db.profile.loglevel >= 3 then
AceConsole:Printf(color.WHITE .. "[" .. color.CYAN .. addon.pretty_name .. color.WHITE .. "] " .. color.DEBUG .. message .. color.RESET, ...)
end
end
function addon:info(message, ...)
if self.db.profile.loglevel >= 2 then
AceConsole:Printf(color.WHITE .. "[" .. color.CYAN .. addon.pretty_name .. color.WHITE .. "] " .. color.INFO .. message .. color.RESET, ...)
end
end
function addon:warn(message, ...)
if self.db.profile.loglevel >= 1 then
AceConsole:Printf(color.WHITE .. "[" .. color.CYAN .. addon.pretty_name .. color.WHITE .. "] " .. color.WARN .. message .. color.RESET, ...)
end
end
function addon:print(message, ...)
AceConsole:Printf(color.WHITE .. "[" .. color.CYAN .. addon.pretty_name .. color.WHITE .. "] " .. color.INFO .. message .. color.RESET, ...)
end
function addon:announce(message, ...)
AceConsole:Printf(color.WHITE .. "[" .. color.CYAN .. addon.pretty_name .. color.WHITE .. "] " .. color.ANNOUNCE .. message .. color.RESET, ...)
end
-- character table string
local b64_chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
-- encoding
function base64enc(data)
return ((data:gsub('.', function(x)
local r,b='',x:byte()
for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
return r;
end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
if (#x < 6) then return '' end
local c=0
for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
return b64_chars:sub(c+1,c+1)
end)..({ '', '==', '=' })[#data%3+1])
end
-- decoding
function base64dec(data)
data = string.gsub(data, '[^'..b64_chars..'=]', '')
return (data:gsub('.', function(x)
if (x == '=') then return '' end
local r,f='',(b64_chars:find(x)-1)
for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
return r;
end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
if (#x ~= 8) then return '' end
local c=0
for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
return string.char(c)
end))
end
function width_split(s, sz)
if s == nil then
return nil
end
local rv = ""
local offs = 1;
while offs < s:len() do
if (offs > 1) then
rv = rv .. "\n"
end
rv = rv .. s:sub(offs, offs+sz)
offs = offs + sz + 1
end
return rv
end
function addon.split(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
function addon.wrap(str, limit)
limit = limit or 72
local here = 1
-- the "".. is there because :gsub returns multiple values
return ""..str:gsub("(%s+)()(%S+)()",
function(_, st, word, fi)
if fi-here > limit then
here = st
return "\n"..word
end
end)
end
function addon.trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
-- This will look at the damage done over damage_history, and gives progressively more weight
-- to more recent damage (so that theoretically if damage has decreased/increased it will adjust.)
function addon.calculate_trend_old(health)
if not health then
return 0
end
local damage_history = addon.db.profile.damage_history
local now = GetTime()
local total, count, seconds = 0, 0, 0
local last_health, last_offs, last_max
local offs_total = 0
for _,v in ipairs(health) do
if v.value >= v.max or v.value == 0 then
break
end
if last_max ~= v.max then
last_health = nil
last_max = v.max
end
local offs = addon.round(now - v.time)
if last_health ~= nil then
offs_total = offs_total + (last_health - v.value)
if last_offs ~= offs then
local scale = damage_history - offs
count = count + scale
total = total + (offs_total * scale)
seconds = seconds + 1
offs_total = 0
end
end
last_health = v.value
last_offs = offs
end
if seconds then
return total / count, seconds
end
return 0
end
function addon.calculate_trend(history, window, noheals, nodmg)
if not history then
return 0
end
if not window then
window = addon.db.profile.damage_history
end
local now = GetTime()
local damage_total, damage_count, damage_seconds = 0, 0, 0
local heal_total, heal_count, heal_seconds = 0, 0, 0
local maxoffs, data = 0, {}
if not nodmg then
for _,v in ipairs(history.damage) do
local diff = now - v.time
local scale = window - diff
maxoffs = addon.round(diff)
if maxoffs > window then
break
end
if v.value then
data[maxoffs] = (data[maxoffs] or 0) + (v.value * scale)
end
end
damage_seconds = maxoffs
for i=0,maxoffs do
damage_total = damage_total + (data[i] or 0)
damage_count = damage_count + (window - i)
end
end
if not noheals then
maxoffs, data = 0, {}
for _,v in ipairs(history.heals) do
local diff = now - v.time
local scale = window - diff
maxoffs = addon.round(diff)
if maxoffs > window then
break
end
if v.value then
data[maxoffs] = (data[maxoffs] or 0) + (v.value * scale)
end
end
heal_seconds = maxoffs
for i=0,maxoffs do
heal_total = heal_total + (data[i] or 0)
heal_count = heal_count + (window - i)
end
end
return (((damage_count > 0) and (-damage_total / damage_count) or 0) +
((heal_count > 0) and (heal_total / heal_count) or 0)),
(damage_seconds > heal_seconds and damage_seconds or heal_seconds)
end
function addon.AddTooltip(frame, text)
frame:SetCallback("OnEnter", function()
GameTooltip:SetOwner(frame.frame, "ANCHOR_BOTTOMRIGHT", 3)
GameTooltip:SetText(text, 1, 1, 1, 1, true)
end)
frame:SetCallback("OnLeave", function()
if GameTooltip:IsOwned(frame.frame) then
GameTooltip:Hide()
end
end)
end
function addon:configure_frame(obj)
if obj.children ~= nil then
for _, child in pairs(obj.children) do
addon:configure_frame(child)
end
end
if (obj.configure ~= nil) then
obj.configure()
-- obj.configure = nil
end
end
function addon:GetSpecialization()
if WOW_PROJECT_ID == WOW_PROJECT_MAINLINE then
return GetSpecialization()
elseif LE_EXPANSION_LEVEL_CURRENT >= 2 then
return GetActiveTalentGroup()
else
return 0
end
end
function addon:UpdateItem_ID_ID(item, text, icon, attempt)
if not item then
if text then
text:SetText(nil)
end
if icon then
icon:SetText(nil)
if GameTooltip:IsOwned(icon.frame) and GameTooltip:IsVisible() then
GameTooltip:Hide()
end
end
return
end
if not attempt then
attempt = 1
if text then
text:SetText(item)
end
if icon then
icon:SetText(nil)
if GameTooltip:IsOwned(icon.frame) and GameTooltip:IsVisible() then
GameTooltip:Hide()
end
end
end
local itemid = self.getRetryCached(addon.longtermCache, GetItemInfoInstant, item)
if itemid then
if text then
text:SetText(itemid)
end
if icon then
icon:SetText(itemid)
if GameTooltip:IsOwned(icon.frame) and GameTooltip:IsVisible() then
GameTooltip:SetHyperlink("item:" .. itemid)
end
end
elseif attempt < 4 then
self:ScheduleTimer("UpdateItem_ID_ID", 0.5, item, text, icon, attempt + 1)
end
end
function addon:UpdateItem_ID_Image(item, text, icon, attempt)
if not item then
if text then
text:SetText(nil)
end
if icon then
icon:SetImage(nil)
if GameTooltip:IsOwned(icon.frame) and GameTooltip:IsVisible() then
GameTooltip:Hide()
end
end
return
end
if not attempt then
attempt = 1
if text then
text:SetText(item)
end
if icon then
icon:SetImage(nil)
if GameTooltip:IsOwned(icon.frame) and GameTooltip:IsVisible() then
GameTooltip:Hide()
end
end
end
local itemid, _, _, _, texture = self.getRetryCached(addon.longtermCache, GetItemInfoInstant, item)
if itemid then
if text then
text:SetText(itemid)
end
if icon then
icon:SetImage(texture)
if GameTooltip:IsOwned(icon.frame) and GameTooltip:IsVisible() then
GameTooltip:SetHyperlink("item:" .. itemid)
end
end
elseif attempt < 4 then
self:ScheduleTimer("UpdateItem_ID_Image", 0.5, item, text, icon, attempt + 1)
end
end
function addon:UpdateItem_Name_ID(item, text, icon, attempt)
if not item then
if text then
text:SetText(nil)
end
if icon then
icon:SetText(nil)
if GameTooltip:IsOwned(icon.frame) and GameTooltip:IsVisible() then
GameTooltip:Hide()
end
end
return
end
if not attempt then
attempt = 1
if text then
text:SetText(item)
end
if icon then
icon:SetText(nil)
if GameTooltip:IsOwned(icon.frame) and GameTooltip:IsVisible() then
GameTooltip:Hide()
end
end
end
local itemid = self.getRetryCached(addon.longtermCache, GetItemInfoInstant, item)
local name
if itemid then
name = self.getRetryCached(addon.longtermCache, GetItemInfo, itemid)
end
if itemid and name then
if text then
text:SetText(name)
end
if icon then
icon:SetText(itemid)
if GameTooltip:IsOwned(icon.frame) and GameTooltip:IsVisible() then
GameTooltip:SetHyperlink("item:" .. itemid)
end
end
elseif attempt < 4 then
self:ScheduleTimer("UpdateItem_Name_ID", 0.5, item, text, icon, attempt + 1)
end
end
function addon:UpdateItem_Name_Image(item, text, icon, attempt)
if not item then
if text then
text:SetText(nil)
end
if icon then
icon:SetImage(nil)
if GameTooltip:IsOwned(icon.frame) and GameTooltip:IsVisible() then
GameTooltip:Hide()
end
end
return
end
if not attempt then
attempt = 1
if text then
text:SetText(item)
end
if icon then
icon:SetImage(nil)
if GameTooltip:IsOwned(icon.frame) and GameTooltip:IsVisible() then
GameTooltip:Hide()
end
end
end
local itemid = self.getRetryCached(addon.longtermCache, GetItemInfoInstant, item)
local name, texture, _
if itemid then
name = self.getRetryCached(addon.longtermCache, GetItemInfo, itemid)
texture = select(10, self.getRetryCached(addon.longtermCache, GetItemInfo, itemid))
end
if name then
if text then
text:SetText(name)
end
if icon then
icon:SetImage(texture)
if GameTooltip:IsOwned(icon.frame) and GameTooltip:IsVisible() then
GameTooltip:SetHyperlink("item:" .. itemid)
end
end
elseif attempt < 4 then
self:ScheduleTimer("UpdateItem_Name_Image", 0.5, item, text, icon, attempt + 1)
end
end