LibItemSearch

This commit is contained in:
Bunny67
2018-01-12 19:33:13 +03:00
parent 325fa0520e
commit 5032865fcc
@@ -6,33 +6,27 @@
local Search = LibStub("CustomSearch-1.0"); local Search = LibStub("CustomSearch-1.0");
local Unfit = LibStub("Unfit-1.0"); local Unfit = LibStub("Unfit-1.0");
local Lib = LibStub:NewLibrary("LibItemSearch-1.2", 11); local Lib = LibStub:NewLibrary("LibItemSearch-1.2", 11);
if(Lib) then if Lib then
Lib.Filters = {}; Lib.Filters = {}
else else
return; return
end end
local match, lower = string.match, string.lower local tonumber = tonumber
local find, strmatch, lower = string.find, string.match, string.lower
--[[ User API ]]-- --[[ User API ]]--
function Lib:Matches(link, search) function Lib:Matches(link, search)
return Search(link, search, this.Filters); return Search(link, search, self.Filters)
end end
function Lib:Tooltip(link, search) function Lib:Tooltip(link, search)
return link and match(self.Filters.tip, link, nil, search); return link and strmatch(self.Filters.tip, link, nil, search)
end end
function Lib:TooltipPhrase(link, search) function Lib:TooltipPhrase(link, search)
return link and match(self.Filters.tipPhrases, link, nil, search); return link and strmatch(self.Filters.tipPhrases, link, nil, search)
end
function Lib:InSet(link, search)
if(IsEquippableItem(link)) then
local id = tonumber(match(link, "item:(%-?%d+)"));
return self:BelongsToSet(id, lower(search or ""));
end
end end
--[[ Basics ]]-- --[[ Basics ]]--
@@ -41,63 +35,63 @@ Lib.Filters.name = {
tags = {"n", "name"}, tags = {"n", "name"},
canSearch = function(self, operator, search) canSearch = function(self, operator, search)
return not operator and search; return not operator and search
end, end,
match = function(self, item, _, search) match = function(self, item, _, search)
local name = match(item, "%[(.-)%]"); local name = strmatch(item, "%[(.-)%]")
return Search:Find(search, name); return Search:Find(search, name)
end end
}; }
Lib.Filters.type = { Lib.Filters.type = {
tags = {"t", "type", "s", "slot"}, tags = {"t", "type", "s", "slot"},
canSearch = function(self, operator, search) canSearch = function(self, operator, search)
return not operator and search; return not operator and search
end, end,
match = function(self, item, _, search) match = function(self, item, _, search)
local type, subType, _, equipSlot = select(6, GetItemInfo(item)); local type, subType, _, equipSlot = select(6, GetItemInfo(item))
return Search:Find(search, type, subType, _G[equipSlot]); return Search:Find(search, type, subType, _G[equipSlot])
end end
}; }
Lib.Filters.level = { Lib.Filters.level = {
tags = {"l", "level", "lvl", "ilvl"}, tags = {"l", "level", "lvl", "ilvl"},
canSearch = function(self, _, search) canSearch = function(self, _, search)
return tonumber(search); return tonumber(search)
end, end,
match = function(self, link, operator, num) match = function(self, link, operator, num)
local lvl = select(4, GetItemInfo(link)); local _, _, _, lvl = GetItemInfo(link)
if(lvl) then if lvl then
return Search:Compare(operator, lvl, num); return Search:Compare(operator, lvl, num)
end end
end end
}; }
Lib.Filters.requiredlevel = { Lib.Filters.requiredlevel = {
tags = {"r", "req", "rl", "reql", "reqlvl"}, tags = {"r", "req", "rl", "reql", "reqlvl"},
canSearch = function(self, _, search) canSearch = function(self, _, search)
return tonumber(search); return tonumber(search)
end, end,
match = function(self, link, operator, num) match = function(self, link, operator, num)
local lvl = select(5, GetItemInfo(link)); local _, _, _, _, lvl = GetItemInfo(link)
if(lvl) then if lvl then
return Search:Compare(operator, lvl, num); return Search:Compare(operator, lvl, num)
end end
end end
}; }
--[[ Quality ]]-- --[[ Quality ]]--
local qualities = {}; local qualities = {}
for i = 0, getn(ITEM_QUALITY_COLORS) do for i = 0, getn(ITEM_QUALITY_COLORS) do
qualities[i] = lower(_G["ITEM_QUALITY" .. i .. "_DESC"]); qualities[i] = lower(_G["ITEM_QUALITY"..i.."_DESC"])
end end
Lib.Filters.quality = { Lib.Filters.quality = {
@@ -105,17 +99,17 @@ Lib.Filters.quality = {
canSearch = function(self, _, search) canSearch = function(self, _, search)
for i, name in pairs(qualities) do for i, name in pairs(qualities) do
if(find(namesearch)) then if find(namesearch) then
return i; return i
end end
end end
end, end,
match = function(self, link, operator, num) match = function(self, link, operator, num)
local quality = select(3, GetItemInfo(link)); local _, _, quality = GetItemInfo(link)
return Search:Compare(operator, quality, num); return Search:Compare(operator, quality, num)
end, end
}; }
--[[ Usable ]]-- --[[ Usable ]]--
@@ -123,20 +117,20 @@ Lib.Filters.usable = {
tags = {}, tags = {},
canSearch = function(self, operator, search) canSearch = function(self, operator, search)
return not operator and search == "usable"; return not operator and search == "usable"
end, end,
match = function(self, link) match = function(self, link)
if(not Unfit:IsItemUnusable(link)) then if(not Unfit:IsItemUnusable(link)) then
local lvl = select(5, GetItemInfo(link)); local _, _, _, _, lvl = GetItemInfo(link)
return lvl and (lvl ~= 0 and lvl <= UnitLevel("player")); return lvl and (lvl ~= 0 and lvl <= UnitLevel("player"))
end end
end end
}; }
--[[ Tooltip Searches ]]-- --[[ Tooltip Searches ]]--
local scanner = LibItemSearchTooltipScanner or CreateFrame("GameTooltip", "LibItemSearchTooltipScanner", UIParent, "GameTooltipTemplate"); local scanner = LibItemSearchTooltipScanner or CreateFrame("GameTooltip", "LibItemSearchTooltipScanner", UIParent, "GameTooltipTemplate")
Lib.Filters.tip = { Lib.Filters.tip = {
tags = {"tt", "tip", "tooltip"}, tags = {"tt", "tip", "tooltip"},
@@ -144,66 +138,66 @@ Lib.Filters.tip = {
onlyTags = true, onlyTags = true,
canSearch = function(self, _, search) canSearch = function(self, _, search)
return search; return search
end, end,
match = function(self, link, _, search) match = function(self, link, _, search)
if(find(link, "item:")) then if find(link, "item:") then
scanner:SetOwner(UIParent, "ANCHOR_NONE"); scanner:SetOwner(UIParent, "ANCHOR_NONE")
scanner:SetHyperlink(link) scanner:SetHyperlink(link)
for i = 1, scanner:NumLines() do for i = 1, scanner:NumLines() do
if(Search:Find(search, _G[scanner:GetName() .. "TextLeft" .. i]:GetText())) then if Search:Find(search, _G[scanner:GetName().."TextLeft"..i]:GetText()) then
return true; return true
end end
end end
end end
end end
}; }
local escapes = { local escapes = {
["|c%x%x%x%x%x%x%x%x"] = "", ["|c%x%x%x%x%x%x%x%x"] = "",
["|r"] = "" ["|r"] = ""
}; }
local function CleanString(str) local function CleanString(str)
for k, v in pairs(escapes) do for k, v in pairs(escapes) do
str = string.gsub(str, k, v); str = string.gsub(str, k, v)
end end
return str; return str
end end
Lib.Filters.tipPhrases = { Lib.Filters.tipPhrases = {
canSearch = function(self, _, search) canSearch = function(self, _, search)
return self.keywords[search]; return self.keywords[search]
end, end,
match = function(self, link, _, search) match = function(self, link, _, search)
local id = match(link, "item:(%d+)"); local id = strmatch(link, "item:(%d+)")
if(not id) then if not id then
return; return
end end
local cached = self.cache[search][id]; local cached = self.cache[search][id]
if(cached ~= nil) then if cached ~= nil then
return cached; return cached
end end
scanner:SetOwner(UIParent, "ANCHOR_NONE"); scanner:SetOwner(UIParent, "ANCHOR_NONE")
scanner:SetHyperlink(link); scanner:SetHyperlink(link)
local matches = false local matches = false
for i = 1, scanner:NumLines() do for i = 1, scanner:NumLines() do
local text = _G["LibItemSearchTooltipScannerTextLeft" .. i]:GetText(); local text = _G["LibItemSearchTooltipScannerTextLeft"..i]:GetText()
text = CleanString(text); text = CleanString(text)
if(search == text) then if search == text then
matches = true; matches = true
break; break
end end
end end
self.cache[search][id] = matches; self.cache[search][id] = matches
return matches; return matches
end, end,
cache = setmetatable({}, {__index = function(t, k) local v = {} t[k] = v return v end}), cache = setmetatable({}, {__index = function(t, k) local v = {} t[k] = v return v end}),
@@ -213,67 +207,6 @@ Lib.Filters.tipPhrases = {
["bound"] = ITEM_BIND_ON_PICKUP, ["bound"] = ITEM_BIND_ON_PICKUP,
["bop"] = ITEM_BIND_ON_PICKUP, ["bop"] = ITEM_BIND_ON_PICKUP,
["boe"] = ITEM_BIND_ON_EQUIP, ["boe"] = ITEM_BIND_ON_EQUIP,
["bou"] = ITEM_BIND_ON_USE, ["bou"] = ITEM_BIND_ON_USE
["boa"] = ITEM_BIND_TO_ACCOUNT, }
};
};
--[[ Equipment Sets ]]--
if IsAddOnLoaded("ItemRack") then
local sameID = ItemRack.SameID
function Lib:BelongsToSet(id, search)
for name, set in pairs(ItemRackUser.Sets) do
if string.sub(name,1,1) ~= "" and Search:Find(search, name) then
for _, item in pairs(set.equip) do
if sameID(id, item) then
return true
end
end
end
end
end
elseif IsAddOnLoaded("Wardrobe") then
function Lib:BelongsToSet(id, search)
for _, outfit in ipairs(Wardrobe.CurrentConfig.Outfit) do
local name = outfit.OutfitName
if Search:Find(search, name) or search == "matchall" then
for _, item in pairs(outfit.Item) do
if item.IsSlotUsed == 1 and item.ItemID == id then
return true
end
end
end
end
end
else
function Lib:BelongsToSet(id, search)
for i = 1, GetNumEquipmentSets() do
local name = GetEquipmentSetInfo(i)
if Search:Find(search, name) then
local items = GetEquipmentSetItemIDs(name)
for _, item in pairs(items) do
if id == item then
return true
end
end
end
end
end
end
Lib.Filters.sets = {
tags = {"s", "set"},
canSearch = function(self, operator, search)
return not operator and search
end,
match = function(self, link, _, search)
return Lib:InSet(link, search)
end,
} }