You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
8.6 KiB

5 years ago
local ADDON_NAME, ns = ...
-------------------------------------------------------------------------------
------------------------------ DATAMINE TOOLTIP -------------------------------
-------------------------------------------------------------------------------
4 years ago
local function CreateDatamineTooltip(name)
local f = CreateFrame('GameTooltip', name, UIParent, 'GameTooltipTemplate')
f:SetOwner(UIParent, 'ANCHOR_NONE')
5 years ago
return f
end
local NameResolver = {
cache = {},
prepared = {},
4 years ago
preparer = CreateDatamineTooltip(ADDON_NAME .. '_NamePreparer'),
resolver = CreateDatamineTooltip(ADDON_NAME .. '_NameResolver')
5 years ago
}
4 years ago
function NameResolver:IsLink(link)
5 years ago
if link == nil then return link end
return strsub(link, 1, 5) == 'unit:'
end
4 years ago
function NameResolver:Prepare(link)
5 years ago
if self:IsLink(link) and not (self.cache[link] or self.prepared[link]) then
-- use a separate tooltip to spam load NPC names, doing this with the
-- main tooltip can sometimes cause it to become unresponsive and never
-- update its text until a reload
self.preparer:SetHyperlink(link)
self.prepared[link] = true
end
end
4 years ago
function NameResolver:Resolve(link)
5 years ago
-- may be passed a raw name or a hyperlink to be resolved
if not self:IsLink(link) then return link or UNKNOWN end
-- all npcs must be prepared ahead of time to avoid breaking the resolver
if not self.prepared[link] then
ns.Debug('ERROR: npc link not prepared:', link)
end
local name = self.cache[link]
if name == nil then
self.resolver:SetHyperlink(link)
4 years ago
name = _G[self.resolver:GetName() .. 'TextLeft1']:GetText() or UNKNOWN
5 years ago
if name == UNKNOWN then
ns.Debug('NameResolver returned UNKNOWN, recreating tooltip ...')
4 years ago
self.resolver = CreateDatamineTooltip(ADDON_NAME .. '_NameResolver')
5 years ago
else
self.cache[link] = name
end
end
return name
end
-------------------------------------------------------------------------------
-------------------------------- LINK RENDERER --------------------------------
-------------------------------------------------------------------------------
local function PrepareLinks(str)
if not str then return end
for type, id in str:gmatch('{(%l+):(%d+)(%l*)}') do
id = tonumber(id)
if type == 'npc' then
4 years ago
NameResolver:Prepare(('unit:Creature-0-0-0-0-%d'):format(id))
5 years ago
elseif type == 'item' then
C_Item.RequestLoadItemDataByID(id) -- prime item info
elseif type == 'daily' or type == 'quest' then
C_QuestLog.RequestLoadQuestByID(id) -- prime quest title
elseif type == 'spell' then
C_Spell.RequestLoadSpellData(id) -- prime spell info
end
end
end
local function RenderLinks(str, nameOnly)
-- render numberic ids
4 years ago
local links, _ = str:gsub('{(%l+):(%d+)(%l*)}', function(type, id, suffix)
5 years ago
id = tonumber(id)
if type == 'npc' then
4 years ago
local name = NameResolver:Resolve(
('unit:Creature-0-0-0-0-%d'):format(id))
name = name .. (suffix or '')
5 years ago
if nameOnly then return name end
return ns.color.NPC(name)
elseif type == 'achievement' then
if nameOnly then
local _, name = GetAchievementInfo(id)
if name then return name end
else
local link = GetAchievementLink(id)
if link then
4 years ago
return ns.GetIconLink('achievement', 15) .. link
5 years ago
end
end
elseif type == 'currency' then
local info = C_CurrencyInfo.GetCurrencyInfo(id)
if info then
if nameOnly then return info.name end
local link = C_CurrencyInfo.GetCurrencyLink(id, 0)
if link then
4 years ago
return '|T' .. info.iconFileID .. ':0:0:1:-1|t ' .. link
5 years ago
end
end
elseif type == 'faction' then
local name = GetFactionInfoByID(id)
if nameOnly then return name end
return ns.color.NPC(name) -- TODO: colorize based on standing?
elseif type == 'item' then
local name, link, _, _, _, _, _, _, _, icon = GetItemInfo(id)
if link and icon then
4 years ago
if nameOnly then return name .. (suffix or '') end
return '|T' .. icon .. ':0:0:1:-1|t ' .. link
5 years ago
end
elseif type == 'daily' or type == 'quest' then
local name = C_QuestLog.GetTitleForQuestID(id)
if name then
if nameOnly then return name end
local icon = (type == 'daily') and 'quest_ab' or 'quest_ay'
4 years ago
return ns.GetIconLink(icon, 12) ..
ns.color.Yellow('[' .. name .. ']')
5 years ago
end
elseif type == 'spell' then
local name, _, icon = GetSpellInfo(id)
if name and icon then
if nameOnly then return name end
4 years ago
local spell = ns.color.Spell(
'|Hspell:' .. id .. '|h[' .. name .. ']|h')
return '|T' .. icon .. ':0:0:1:-1|t ' .. spell
5 years ago
end
end
4 years ago
return type .. '+' .. id
5 years ago
end)
-- render non-numeric ids
4 years ago
links, _ = links:gsub('{(%l+):([^}]+)}', function(type, id)
5 years ago
if type == 'wq' then
local icon = ns.GetIconLink('world_quest', 16, 0, -1)
4 years ago
return icon .. ns.color.Yellow('[' .. id .. ']')
5 years ago
end
4 years ago
return type .. '+' .. id
5 years ago
end)
return links
end
-------------------------------------------------------------------------------
-------------------------------- BAG FUNCTIONS --------------------------------
-------------------------------------------------------------------------------
local function PlayerHasItem(item, count)
4 years ago
return GetItemCount(item, true) >= (count and count > 1 and count or 1)
5 years ago
end
-------------------------------------------------------------------------------
------------------------------ DATABASE FUNCTIONS -----------------------------
-------------------------------------------------------------------------------
local function GetDatabaseTable(...)
4 years ago
local db = _G[ADDON_NAME .. 'DB']
5 years ago
for _, key in ipairs({...}) do
if db[key] == nil then db[key] = {} end
db = db[key]
end
return db
end
-------------------------------------------------------------------------------
------------------------------ LOCALE FUNCTIONS -------------------------------
-------------------------------------------------------------------------------
--[[
Wrap the AceLocale NewLocale() function to return a slightly modified locale
table. This table will ignore assignments of `nil`, allowing locales to include
noop translation lines in their files without overriding the default enUS
strings. This allows us to keep all the locale files in sync with the exact
same keys in the exact same order even before actual translations are done.
--]]
4 years ago
local AceLocale = LibStub('AceLocale-3.0')
5 years ago
local LOCALES = {}
4 years ago
local function NewLocale(locale)
5 years ago
if LOCALES[locale] then return LOCALES[locale] end
local L = AceLocale:NewLocale(ADDON_NAME, locale, (locale == 'enUS'), true)
if not L then return end
local wrapper = {}
setmetatable(wrapper, {
4 years ago
__index = function(self, key) return L[key] end,
__newindex = function(self, key, value)
5 years ago
if value == nil then return end
L[key] = value
end
})
return wrapper
end
-------------------------------------------------------------------------------
------------------------------ TABLE CONVERTERS -------------------------------
-------------------------------------------------------------------------------
4 years ago
local function AsTable(value, class)
5 years ago
-- normalize to table of scalars
if type(value) == 'nil' then return end
if type(value) ~= 'table' then return {value} end
if class and ns.IsInstance(value, class) then return {value} end
return value
end
4 years ago
local function AsIDTable(value)
5 years ago
-- normalize to table of id objects
if type(value) == 'nil' then return end
4 years ago
if type(value) ~= 'table' then return {{id = value}} end
5 years ago
if value.id then return {value} end
for i, v in ipairs(value) do
4 years ago
if type(v) == 'number' then value[i] = {id = v} end
5 years ago
end
return value
end
-------------------------------------------------------------------------------
ns.AsIDTable = AsIDTable
ns.AsTable = AsTable
ns.GetDatabaseTable = GetDatabaseTable
ns.NameResolver = NameResolver
ns.NewLocale = NewLocale
ns.PlayerHasItem = PlayerHasItem
ns.PrepareLinks = PrepareLinks
ns.RenderLinks = RenderLinks