forked from TekNoLogic/Buffet
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathUtility.lua
394 lines (363 loc) · 10.5 KB
/
Utility.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
local _, ns = ...
-- Local namespace
local Utility = {}
local addonName = "Buffet"
local coloredAddonName = "|cFF33FF99" .. addonName .. "|r:"
local debugColoredAddonName = "|cFF33BB99" .. addonName .. "|r:"
-- Localize functions
local string_match = string.match
local string_find = string.find
local string_lower = string.lower
local string_join = string.join
-- Parameters
do
Utility.DebugStatus = false
--@debug@
Utility.DebugStatus = true
--@end-debug@
Utility.buffetTooltip = nil
Utility.Mode = 1
Utility.IsClassic = false
Utility.IsTBC = false
Utility.IsWLK = false
Utility.IsCataclysm = false
Utility.IsRetail = false
local _, _, _, interfaceVersion = GetBuildInfo()
if interfaceVersion >= 10000 and interfaceVersion < 20500 then
Utility.IsClassic = true
elseif interfaceVersion >= 20500 and interfaceVersion < 30000 then
Utility.IsTBC = true
elseif interfaceVersion >= 30000 and interfaceVersion < 40000 then
Utility.IsWLK = true
elseif interfaceVersion >= 40000 and interfaceVersion < 50000 then
Utility.IsCataclysm = true
elseif interfaceVersion >= 90000 then
Utility.IsRetail = true
end
Utility.skillLineIds = {}
end
function Utility.GetTime()
return (debugprofilestop() / 1000)
end
function Utility.Print(...)
if Utility.Mode == 2 then
print(coloredAddonName, ...)
else
DEFAULT_CHAT_FRAME:AddMessage(string_join(" ", coloredAddonName, ...))
end
end
function Utility.Debug(...)
--@debug@
if not Utility.DebugStatus then
return
end
if Utility.Mode == 2 then
print(debugColoredAddonName, ...)
else
local arg = {...}
local t = ""
for i, v in ipairs(arg) do
if type(v) == "table" then
for k, w in pairs(v) do
t = t .. ", " .. k .. "=" .. tostring(w)
end
else
t = t .. " " .. tostring(v)
end
end
DEFAULT_CHAT_FRAME:AddMessage(debugColoredAddonName .. t)
end
--@end-debug@
end
function Utility.BoolToStr(value)
if value then
return "Yes"
end
return "No"
end
function Utility.Trim(s)
return string_match(s,'^()%s*$') and '' or string_match(s,'^%s*(.*%S)')
end
function Utility.StringContains(string, needle)
if string and needle then
local found = string_find(string, needle, 1, true)
if found == nil then
return false
end
return true
end
return false
end
function Utility.TableCount(table)
local c = 0
if table then
for _, v in pairs(table) do
if v then
c = c + 1
end
end
end
return c
end
function Utility.TableContainsKey(table, key)
if table then
for k, _ in pairs(table) do
if k == key then
return true
end
end
end
return false
end
function Utility.TableContainsValue(table, value)
if table then
for _, v in pairs(table) do
if v == value then
return true
end
end
end
return false
end
function Utility.GetTooltip()
local tooltip = Utility.buffetTooltip or CreateFrame("GameToolTip", "buffetTooltip", nil, "GameTooltipTemplate")
tooltip:SetOwner(WorldFrame, "ANCHOR_NONE")
tooltip:ClearLines()
return tooltip
end
function Utility.IsPlayerInInstanceId(ids)
local _,_,_,_,_,_,_,instanceId = GetInstanceInfo()
if instanceId then
for _,v in pairs(ids) do
if v == instanceId then
return true
end
end
end
return false
end
function Utility.IsPlayerInInstanceType(types)
local _,instanceType, diffId = GetInstanceInfo()
instanceType = instanceType or "none"
for _,v in pairs(types) do
if (v == "none" and diffId == 0 and instanceType ~= "pvp" and instanceType ~= "arena") then
return true
end
if (v == instanceType) then
return true
end
end
return false
end
function Utility.IsPlayerInMapId(ids)
local uiMapId = C_Map.GetBestMapForUnit("player");
if uiMapId then
repeat
for _,v in pairs(ids) do
if v == uiMapId then
return true
end
end
local mapInfo = C_Map.GetMapInfo(uiMapId);
uiMapId = mapInfo and mapInfo.parentMapID or 0;
until uiMapId == 0;
end
return false
end
function Utility.IsPlayerInSubZoneName(names)
local currentSubZone = string_lower(GetSubZoneText())
if currentSubZone ~= "" then
local babbleSubZone = LibStub("LibBabble-SubZone-3.0"):GetUnstrictLookupTable();
for k,v in pairs(names) do
local subZone = babbleSubZone[v] -- get locale subzone name from LibBabble
if subZone and (subZone:lower() == currentSubZone) then
return true
end
end
end
return false
end
function Utility.ShowPlayerZoneInfo()
local uiMapId = C_Map.GetBestMapForUnit("player");
if uiMapId then
repeat
Utility.Debug("uiMapId=" .. uiMapId)
local mapInfo = C_Map.GetMapInfo(uiMapId);
uiMapId = mapInfo and mapInfo.parentMapID or 0;
until uiMapId == 0;
end
local _,instanceType,diffId,_,_,_,_,instanceId = GetInstanceInfo()
instanceType = instanceType or "none"
if instanceId then
Utility.Debug("instanceId=" .. instanceId)
Utility.Debug("instanceType=" .. instanceType)
Utility.Debug("diffId=" .. diffId)
end
local currentSubZone = string_lower(GetSubZoneText())
if currentSubZone ~= "" then
local babbleSubZone = LibStub("LibBabble-SubZone-3.0"):GetUnstrictLookupTable();
for k, v in pairs(babbleSubZone) do
local subZone = v -- get locale subzone name from LibBabble
if subZone and (subZone:lower() == currentSubZone) then
Utility.Debug("currentSubZone=" .. currentSubZone .. ", EN=" .. k)
return
end
end
end
end
function Utility.LoadProfessions()
if not Utility.IsRetail then
-- from https://www.wowinterface.com/forums/showthread.php?t=57554
local profNames = Utility.ProfessionNames[GetLocale()]
local profNames_rev = tInvert(profNames)
for i = 1, GetNumSkillLines() do
local name = GetSkillLineInfo(i)
if profNames_rev[name] then
table.insert(Utility.skillLineIds, profNames_rev[name])
end
end
end
if Utility.IsRetail then
Utility.skillLineIds = C_TradeSkillUI.GetAllProfessionTradeSkillLines()
end
end
-- from https://www.wowinterface.com/forums/showthread.php?t=57554
Utility.ProfessionNames = {
enUS = {
[164] = "Blacksmithing",
[165] = "Leatherworking",
[171] = "Alchemy",
[182] = "Herbalism",
[185] = "Cooking",
[186] = "Mining",
[197] = "Tailoring",
[202] = "Engineering",
[333] = "Enchanting",
[356] = "Fishing",
[393] = "Skinning",
[755] = "Jewelcrafting",
[773] = "Inscription",
},
deDE = {
[164] = "Schmiedekunst",
[165] = "Lederverarbeitung",
[171] = "Alchemie",
[182] = "Kräuterkunde",
[185] = "Kochkunst",
[186] = "Bergbau",
[197] = "Schneiderei",
[202] = "Ingenieurskunst",
[333] = "Verzauberkunst",
[356] = "Angeln",
[393] = "Kürschnerei",
[755] = "Juwelierskunst",
[773] = "Inschriftenkunde",
},
frFR = {
[164] = "Forge",
[165] = "Travail du cuir",
[171] = "Alchimie",
[182] = "Herboristerie",
[185] = "Cuisine",
[186] = "Minage",
[197] = "Couture",
[202] = "Ingénierie",
[333] = "Enchantement",
[356] = "Pêche",
[393] = "Dépeçage",
[755] = "Joaillerie",
[773] = "Calligraphie",
},
esMX = {
[164] = "Herrería",
[165] = "Peletería",
[171] = "Alquimia",
[182] = "Herboristería",
[185] = "Cocina",
[186] = "Minería",
[197] = "Sastrería",
[202] = "Ingeniería",
[333] = "Encantamiento",
[356] = "Pesca",
[393] = "Desuello",
[755] = "Joyería",
[773] = "Inscripción",
},
ptBR = {
[164] = "Ferraria",
[165] = "Couraria",
[171] = "Alquimia",
[182] = "Herborismo",
[185] = "Culinária",
[186] = "Mineração",
[197] = "Alfaiataria",
[202] = "Engenharia",
[333] = "Encantamento",
[356] = "Pesca",
[393] = "Esfolamento",
[755] = "Joalheria",
[773] = "Escrivania",
},
ruRU = {
[164] = "Кузнечное дело",
[165] = "Кожевничество",
[171] = "Алхимия",
[182] = "Травничество",
[185] = "Кулинария",
[186] = "Горное дело",
[197] = "Портняжное дело",
[202] = "Инженерное дело",
[333] = "Наложение чар",
[356] = "Рыбная ловля",
[393] = "Снятие шкур",
[755] = "Ювелирное дело",
[773] = "Начертание",
},
zhCN = {
[164] = "锻造",
[165] = "制皮",
[171] = "炼金术",
[182] = "草药学",
[185] = "烹饪",
[186] = "采矿",
[197] = "裁缝",
[202] = "工程学",
[333] = "附魔",
[356] = "钓鱼",
[393] = "剥皮",
[755] = "珠宝加工",
[773] = "铭文",
},
zhTW = {
[164] = "鍛造",
[165] = "製皮",
[171] = "鍊金術",
[182] = "草藥學",
[185] = "烹飪",
[186] = "採礦",
[197] = "裁縫",
[202] = "工程學",
[333] = "附魔",
[356] = "釣魚",
[393] = "剝皮",
[755] = "珠寶設計",
[773] = "銘文學",
},
koKR = {
[164] = "대장기술",
[165] = "가죽세공",
[171] = "연금술",
[182] = "약초채집",
[185] = "요리",
[186] = "채광",
[197] = "재봉술",
[202] = "기계공학",
[333] = "마법부여",
[356] = "낚시",
[393] = "무두질",
[755] = "보석세공",
[773] = "주문각인",
},
}
-- Export
ns.Utility = Utility