forked from simulationcraft/simc-addon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.lua
606 lines (521 loc) · 19.4 KB
/
core.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
local _, Simulationcraft = ...
Simulationcraft = LibStub("AceAddon-3.0"):NewAddon(Simulationcraft, "Simulationcraft", "AceConsole-3.0", "AceEvent-3.0")
ItemUpgradeInfo = LibStub("LibItemUpgradeInfo-1.0")
LibRealmInfo = LibStub("LibRealmInfo")
-- Set up DataBroker for minimap button
SimcLDB = LibStub("LibDataBroker-1.1"):NewDataObject("SimulationCraft", {
type = "data source",
text = "SimulationCraft",
label = "SimulationCraft",
icon = "Interface\\AddOns\\SimulationCraft\\logo",
OnClick = function()
if SimcCopyFrame:IsShown() then
SimcCopyFrame:Hide()
else
Simulationcraft:PrintSimcProfile(false, false)
end
end,
OnTooltipShow = function(tt)
tt:AddLine("SimulationCraft")
tt:AddLine(" ")
tt:AddLine("Click to show SimC input")
tt:AddLine("To toggle minimap button, type '/simc minimap'")
end
})
LibDBIcon = LibStub("LibDBIcon-1.0")
local OFFSET_ITEM_ID = 1
local OFFSET_ENCHANT_ID = 2
local OFFSET_GEM_ID_1 = 3
local OFFSET_GEM_ID_2 = 4
local OFFSET_GEM_ID_3 = 5
local OFFSET_GEM_ID_4 = 6
local OFFSET_GEM_BASE = OFFSET_GEM_ID_1
local OFFSET_SUFFIX_ID = 7
local OFFSET_FLAGS = 11
local OFFSET_CONTEXT = 12
local OFFSET_BONUS_ID = 13
local OFFSET_UPGRADE_ID = 14 -- Flags = 0x4
local SocketInventoryItem = _G.SocketInventoryItem
local Timer = _G.C_Timer
local AzeriteEmpoweredItem = _G.C_AzeriteEmpoweredItem
local AzeriteItem = _G.C_AzeriteItem
-- load stuff from extras.lua
local upgradeTable = Simulationcraft.upgradeTable
local slotNames = Simulationcraft.slotNames
local simcSlotNames = Simulationcraft.simcSlotNames
local specNames = Simulationcraft.SpecNames
local profNames = Simulationcraft.ProfNames
local regionString = Simulationcraft.RegionString
-- Most of the guts of this addon were based on a variety of other ones, including
-- Statslog, AskMrRobot, and BonusScanner. And a bunch of hacking around with AceGUI.
-- Many thanks to the authors of those addons, and to reia for fixing my awful amateur
-- coding mistakes regarding objects and namespaces.
function Simulationcraft:OnInitialize()
-- init databroker
self.db = LibStub("AceDB-3.0"):New("SimulationCraftDB", {
profile = {
minimap = {
hide = false,
},
},
});
LibDBIcon:Register("SimulationCraft", SimcLDB, self.db.profile.minimap)
Simulationcraft:UpdateMinimapButton()
Simulationcraft:RegisterChatCommand('simc', 'HandleChatCommand')
end
function Simulationcraft:OnEnable()
SimulationcraftTooltip:SetOwner(_G["UIParent"],"ANCHOR_NONE")
end
function Simulationcraft:OnDisable()
end
function Simulationcraft:UpdateMinimapButton()
if (self.db.profile.minimap.hide) then
LibDBIcon:Hide("SimulationCraft")
else
LibDBIcon:Show("SimulationCraft")
end
end
local function getLinks(input)
local separatedLinks = {}
for link in input:gmatch("|c.-|h|r") do
separatedLinks[#separatedLinks + 1] = link
end
return separatedLinks
end
function Simulationcraft:HandleChatCommand(input)
local args = {strsplit(' ', input)}
local debugOutput = false
local noBags = false
local links = getLinks(input)
for _, arg in ipairs(args) do
if arg == 'debug' then
debugOutput = true
elseif arg == 'nobag' or arg == 'nobags' or arg == 'nb' then
noBags = true
elseif arg == 'minimap' then
self.db.profile.minimap.hide = not self.db.profile.minimap.hide
DEFAULT_CHAT_FRAME:AddMessage("SimulationCraft: Minimap button is now " .. (self.db.profile.minimap.hide and "hidden" or "shown"))
Simulationcraft:UpdateMinimapButton()
return
end
end
self:PrintSimcProfile(debugOutput, noBags, links)
end
local function GetItemSplit(itemLink)
local itemString = string.match(itemLink, "item:([%-?%d:]+)")
local itemSplit = {}
-- Split data into a table
for _, v in ipairs({strsplit(":", itemString)}) do
if v == "" then
itemSplit[#itemSplit + 1] = 0
else
itemSplit[#itemSplit + 1] = tonumber(v)
end
end
return itemSplit
end
-- char size for utf8 strings
local function ChrSize(char)
if not char then
return 0
elseif char > 240 then
return 4
elseif char > 225 then
return 3
elseif char > 192 then
return 2
else
return 1
end
end
-- SimC tokenize function
local function Tokenize(str)
str = str or ""
-- convert to lowercase and remove spaces
str = string.lower(str)
str = string.gsub(str, ' ', '_')
-- keep stuff we want, dumpster everything else
local s = ""
for i=1,str:len() do
local b = str:byte(i)
-- keep digits 0-9
if b >= 48 and b <= 57 then
s = s .. str:sub(i,i)
-- keep lowercase letters
elseif b >= 97 and b <= 122 then
s = s .. str:sub(i,i)
-- keep %, +, ., _
elseif b == 37 or b == 43 or b == 46 or b == 95 then
s = s .. str:sub(i,i)
-- save all multibyte chars
elseif ChrSize(b) > 1 then
local offset = ChrSize(b) - 1
s = s .. str:sub(i, i + offset)
i = i + offset
end
end
-- strip trailing spaces
if string.sub(s, s:len())=='_' then
s = string.sub(s, 0, s:len()-1)
end
return s
end
-- method to add spaces to UnitRace names for proper tokenization
local function FormatRace(str)
str = str or ""
local matches = {}
for match, _ in string.gmatch(str, '([%u][%l]*)') do
matches[#matches+1] = match
end
return string.join(' ', unpack(matches))
end
-- method for constructing the talent string
local function CreateSimcTalentString()
local talentInfo = {}
local maxTiers = 7
local maxColumns = 3
for tier = 1, maxTiers do
for column = 1, maxColumns do
local talentID, name, iconTexture, selected, available = GetTalentInfo(tier, column, GetActiveSpecGroup())
if selected then
talentInfo[tier] = column
end
end
end
local str = 'talents='
for i = 1, maxTiers do
if talentInfo[i] then
str = str .. talentInfo[i]
else
str = str .. '0'
end
end
return str
end
-- function that translates between the game's role values and ours
local function TranslateRole(spec_id, str)
local spec_role = Simulationcraft.RoleTable[spec_id]
if spec_role ~= nil then
return spec_role
end
if str == 'TANK' then
return 'tank'
elseif str == 'DAMAGER' then
return 'attack'
elseif str == 'HEALER' then
return 'attack'
else
return ''
end
end
-- =================== Item Information =========================
local function GetGemItemID(itemLink, index)
local _, gemLink = GetItemGem(itemLink, index)
if gemLink ~= nil then
local itemIdStr = string.match(gemLink, "item:(%d+)")
if itemIdStr ~= nil then
return tonumber(itemIdStr)
end
end
return 0
end
local function GetItemStringFromItemLink(slotNum, itemLink, itemLoc, debugOutput)
local itemSplit = GetItemSplit(itemLink)
local simcItemOptions = {}
local gems = {}
-- Item id
local itemId = itemSplit[OFFSET_ITEM_ID]
simcItemOptions[#simcItemOptions + 1] = ',id=' .. itemId
-- Enchant
if itemSplit[OFFSET_ENCHANT_ID] > 0 then
simcItemOptions[#simcItemOptions + 1] = 'enchant_id=' .. itemSplit[OFFSET_ENCHANT_ID]
end
-- Gems
for gemOffset = OFFSET_GEM_ID_1, OFFSET_GEM_ID_4 do
local gemIndex = (gemOffset - OFFSET_GEM_BASE) + 1
if itemSplit[gemOffset] > 0 then
local gemId = GetGemItemID(itemLink, gemIndex)
if gemId > 0 then
gems[gemIndex] = gemId
end
else
gems[gemIndex] = 0
end
end
-- Remove any trailing zeros from the gems array
while #gems > 0 and gems[#gems] == 0 do
table.remove(gems, #gems)
end
if #gems > 0 then
simcItemOptions[#simcItemOptions + 1] = 'gem_id=' .. table.concat(gems, '/')
end
-- New style item suffix, old suffix style not supported
if itemSplit[OFFSET_SUFFIX_ID] ~= 0 then
simcItemOptions[#simcItemOptions + 1] = 'suffix=' .. itemSplit[OFFSET_SUFFIX_ID]
end
local flags = itemSplit[OFFSET_FLAGS]
local bonuses = {}
for index=1, itemSplit[OFFSET_BONUS_ID] do
bonuses[#bonuses + 1] = itemSplit[OFFSET_BONUS_ID + index]
end
if #bonuses > 0 then
simcItemOptions[#simcItemOptions + 1] = 'bonus_id=' .. table.concat(bonuses, '/')
end
local linkOffset = OFFSET_BONUS_ID + #bonuses + 1
-- Upgrade level
if bit.band(flags, 0x4) == 0x4 then
local upgradeId = itemSplit[linkOffset]
if upgradeTable and upgradeTable[upgradeId] ~= nil and upgradeTable[upgradeId] > 0 then
simcItemOptions[#simcItemOptions + 1] = 'upgrade=' .. upgradeTable[upgradeId]
end
linkOffset = linkOffset + 1
end
-- Some leveling quest items seem to use this, it'll include the drop level of the item
if bit.band(flags, 0x200) == 0x200 then
simcItemOptions[#simcItemOptions + 1] = 'drop_level=' .. itemSplit[linkOffset]
linkOffset = linkOffset + 1
end
-- Get item creation context. Can be used to determine unlock/availability of azerite tiers for 3rd parties
if itemSplit[OFFSET_CONTEXT] ~= 0 then
simcItemOptions[#simcItemOptions + 1] = 'context=' .. itemSplit[OFFSET_CONTEXT]
end
-- Azerite powers - only run in BfA client
if itemLoc and AzeriteEmpoweredItem then
if AzeriteEmpoweredItem.IsAzeriteEmpoweredItem(itemLoc) then
-- C_AzeriteEmpoweredItem.GetAllTierInfo(ItemLocation:CreateFromEquipmentSlot(5))
-- C_AzeriteEmpoweredItem.GetPowerInfo(ItemLocation:CreateFromEquipmentSlot(5), 111)
local azeritePowers = {}
local powerIndex = 1
local tierInfo = AzeriteEmpoweredItem.GetAllTierInfo(itemLoc)
for azeriteTier, tierInfo in pairs(tierInfo) do
for _, powerId in pairs(tierInfo.azeritePowerIDs) do
if AzeriteEmpoweredItem.IsPowerSelected(itemLoc, powerId) then
azeritePowers[powerIndex] = powerId
powerIndex = powerIndex + 1
end
end
end
simcItemOptions[#simcItemOptions + 1] = 'azerite_powers=' .. table.concat(azeritePowers, '/')
end
if AzeriteItem.IsAzeriteItem(itemLoc) then
simcItemOptions[#simcItemOptions + 1] = 'azerite_level=' .. AzeriteItem.GetPowerLevel(itemLoc)
end
end
local itemStr = ''
if debugOutput then
itemStr = itemStr .. '# ' .. itemString .. '\n'
end
itemStr = itemStr .. simcSlotNames[slotNum] .. "=" .. table.concat(simcItemOptions, ',')
return itemStr
end
function Simulationcraft:GetItemStrings(debugOutput)
local items = {}
for slotNum=1, #slotNames do
local slotId = GetInventorySlotInfo(slotNames[slotNum])
local itemLink = GetInventoryItemLink('player', slotId)
-- if we don't have an item link, we don't care
if itemLink then
local itemLoc
if ItemLocation then
itemLoc = ItemLocation:CreateFromEquipmentSlot(slotId)
end
items[slotNum] = GetItemStringFromItemLink(slotNum, itemLink, itemLoc, debugOutput)
end
end
return items
end
function Simulationcraft:GetBagItemStrings()
local bagItems = {}
for slotNum=1, #slotNames do
local slotName = slotNames[slotNum]
-- Ignore "double" slots, results in doubled output which isn't useful
if slotName and slotName ~= 'Trinket1Slot' and slotName ~= 'Finger1Slot' then
local slotItems = {}
local slotId, _, _ = GetInventorySlotInfo(slotNames[slotNum])
GetInventoryItemsForSlot(slotId, slotItems)
for locationBitstring, itemID in pairs(slotItems) do
local player, bank, bags, voidstorage, slot, bag = EquipmentManager_UnpackLocation(locationBitstring)
local itemLoc
if ItemLocation then
if bag == nil then
-- this is a default bank slot (not a bank bag). these exist on the character equipment, not a bag
itemLoc = ItemLocation:CreateFromEquipmentSlot(slot)
else
itemLoc = ItemLocation:CreateFromBagAndSlot(bag, slot)
end
end
if bags or bank then
local container
if bags then
container = bag
elseif bank then
-- Default bank slots (the innate ones, not ones from bags-in-the-bank) are weird
-- slot starts at 39, I believe that is based on some older location values
-- GetContainerItemInfo uses a 0-based slot index
-- So take the slot from the unpack and subtract 39 to get the right index for GetContainerItemInfo.
-- 2018/01/17 - Change magic number to 47 to account for new backpack slots. Not sure why it went up by 8
-- instead of 4, possible blizz is leaving the door open to more expansion in the future?
container = BANK_CONTAINER
slot = slot - 47
end
_, _, _, _, _, _, itemLink, _, _, itemId = GetContainerItemInfo(container, slot)
if itemLink then
local name, link, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice = GetItemInfo(itemLink)
-- get correct level for scaling gear
local level = ItemUpgradeInfo:GetUpgradedItemLevel(link) or 0
-- find all equippable, non-artifact items
if IsEquippableItem(itemLink) and quality ~= 6 then
bagItems[#bagItems + 1] = {
string = GetItemStringFromItemLink(slotNum, itemLink, itemLoc, false),
name = name .. ' (' .. level .. ')'
}
end
end
end
end
end
end
return bagItems
end
function Simulationcraft:GetReoriginationArrayStacks()
local questStart = 53571
local questEnd = 53580
local stacks = 0
for questId = questStart, questEnd do
if IsQuestFlaggedCompleted(questId) then
stacks = stacks + 1
end
end
return stacks
end
-- This is the workhorse function that constructs the profile
function Simulationcraft:PrintSimcProfile(debugOutput, noBags, links)
-- addon metadata
local versionComment = '# SimC Addon ' .. GetAddOnMetadata('Simulationcraft', 'Version')
-- Basic player info
local _, realmName, _, _, _, _, region, _, _, realmLatinName, _ = LibRealmInfo:GetRealmInfoByUnit('player')
local playerName = UnitName('player')
local _, playerClass = UnitClass('player')
local playerLevel = UnitLevel('player')
-- Try Latin name for Russian servers first, then realm name from LibRealmInfo, then Realm Name from the game
-- Latin name for Russian servers as most APIs use the latin name, not the cyrillic name
local playerRealm = realmLatinName or realmName or GetRealmName()
-- Try region from LibRealmInfo first, then use default API
-- Default API can be wrong for region-switching players
local playerRegion = region or regionString[GetCurrentRegion()]
-- Race info
local _, playerRace = UnitRace('player')
-- fix some races to match SimC format
if playerRace == 'Scourge' then --lulz
playerRace = 'Undead'
else
playerRace = FormatRace(playerRace)
end
-- Spec info
local role, globalSpecID
local specId = GetSpecialization()
if specId then
globalSpecID,_,_,_,_,role = GetSpecializationInfo(specId)
end
local playerSpec = specNames[ globalSpecID ]
-- Professions
local pid1, pid2 = GetProfessions()
local firstProf, firstProfRank, secondProf, secondProfRank, profOneId, profTwoId
if pid1 then
_,_,firstProfRank,_,_,_,profOneId = GetProfessionInfo(pid1)
end
if pid2 then
secondProf,_,secondProfRank,_,_,_,profTwoId = GetProfessionInfo(pid2)
end
firstProf = profNames[ profOneId ]
secondProf = profNames[ profTwoId ]
local playerProfessions = ''
if pid1 or pid2 then
playerProfessions = 'professions='
if pid1 then
playerProfessions = playerProfessions..Tokenize(firstProf)..'='..tostring(firstProfRank)..'/'
end
if pid2 then
playerProfessions = playerProfessions..Tokenize(secondProf)..'='..tostring(secondProfRank)
end
else
playerProfessions = ''
end
-- Construct SimC-compatible strings from the basic information
local player = Tokenize(playerClass) .. '="' .. playerName .. '"'
playerLevel = 'level=' .. playerLevel
playerRace = 'race=' .. Tokenize(playerRace)
playerRole = 'role=' .. TranslateRole(globalSpecID, role)
playerSpec = 'spec=' .. Tokenize(playerSpec)
playerRealm = 'server=' .. Tokenize(playerRealm)
playerRegion = 'region=' .. Tokenize(playerRegion)
-- Talents are more involved - method to handle them
local playerTalents = CreateSimcTalentString()
-- Build the output string for the player (not including gear)
local simulationcraftProfile = versionComment .. '\n'
local simcPrintError = nil
simulationcraftProfile = simulationcraftProfile .. player .. '\n'
simulationcraftProfile = simulationcraftProfile .. playerLevel .. '\n'
simulationcraftProfile = simulationcraftProfile .. playerRace .. '\n'
simulationcraftProfile = simulationcraftProfile .. playerRegion .. '\n'
simulationcraftProfile = simulationcraftProfile .. playerRealm .. '\n'
simulationcraftProfile = simulationcraftProfile .. playerRole .. '\n'
simulationcraftProfile = simulationcraftProfile .. playerProfessions .. '\n'
simulationcraftProfile = simulationcraftProfile .. playerTalents .. '\n'
simulationcraftProfile = simulationcraftProfile .. playerSpec .. '\n'
simulationcraftProfile = simulationcraftProfile .. '\n'
-- Method that gets gear information
local items = Simulationcraft:GetItemStrings(debugOutput)
-- output gear
for slotNum=1, #slotNames do
if items[slotNum] then
simulationcraftProfile = simulationcraftProfile .. items[slotNum] .. '\n'
end
end
simulationcraftProfile = simulationcraftProfile .. '\n'
-- output gear from bags
if noBags == false then
local bagItems = Simulationcraft:GetBagItemStrings()
simulationcraftProfile = simulationcraftProfile .. '### Gear from Bags\n'
for i=1, #bagItems do
simulationcraftProfile = simulationcraftProfile .. '#\n'
simulationcraftProfile = simulationcraftProfile .. '# ' .. bagItems[i].name .. '\n'
simulationcraftProfile = simulationcraftProfile .. '# ' .. bagItems[i].string .. '\n'
end
end
-- collect additional info and output in comments
local reoriginationArrayStacks = Simulationcraft:GetReoriginationArrayStacks()
if reoriginationArrayStacks > 0 then
simulationcraftProfile = simulationcraftProfile .. '\n'
simulationcraftProfile = simulationcraftProfile .. '# Stacks of reorigination array based on hidden quest completion\n'
simulationcraftProfile = simulationcraftProfile .. '# bfa.reorigination_array_stacks=' .. reoriginationArrayStacks .. '\n'
end
if links and #links > 0 then
simulationcraftProfile = simulationcraftProfile .. '\n### Linked gear\n'
for i,v in pairs(links) do
local name,_,_,_,_,_,_,_,invType = GetItemInfo(v)
if name and invType ~= "" then
local slotNum = Simulationcraft.invTypeToSlotNum[invType]
simulationcraftProfile = simulationcraftProfile .. '#\n'
simulationcraftProfile = simulationcraftProfile .. '# ' .. name .. '\n'
simulationcraftProfile = simulationcraftProfile .. '# ' .. GetItemStringFromItemLink(slotNum, v, nil, debugOutput) .. "\n"
else -- Someone linked something that was not gear.
simcPrintError = "Error: " .. v .. " is not gear."
break
end
end
end
-- sanity checks - if there's anything that makes the output completely invalid, punt!
if specId==nil then
simcPrintError = "Error: You need to pick a spec!"
end
-- show the appropriate frames
SimcCopyFrame:Show()
SimcCopyFrameScroll:Show()
SimcCopyFrameScrollText:Show()
SimcCopyFrameScrollText:SetText(simcPrintError or simulationcraftProfile)
SimcCopyFrameScrollText:HighlightText()
SimcCopyFrameScrollText:SetScript("OnEscapePressed", function(self)
SimcCopyFrame:Hide()
end)
end