cleanup & optimizations

This commit is contained in:
Pinya
2018-07-17 21:01:03 +03:00
parent 9ed4720ddf
commit 0ad6c8a7a3
3 changed files with 106 additions and 63 deletions
+13 -15
View File
@@ -2,6 +2,7 @@
local error = error local error = error
local geterrorhandler = geterrorhandler local geterrorhandler = geterrorhandler
local loadstring = loadstring local loadstring = loadstring
local next = next
local pairs = pairs local pairs = pairs
local pcall = pcall local pcall = pcall
local tonumber = tonumber local tonumber = tonumber
@@ -10,21 +11,12 @@ local type = type
local unpack = unpack local unpack = unpack
local abs, ceil, exp, floor = math.abs, math.ceil, math.exp, math.floor local abs, ceil, exp, floor = math.abs, math.ceil, math.exp, math.floor
local find, format, gfind, gsub, len, sub = string.find, string.format, string.gfind, string.gsub, string.len, string.sub local find, format, gfind, gsub, len, sub = string.find, string.format, string.gfind, string.gsub, string.len, string.sub
local concat, tinsert, getn, setn = table.concat, table.insert, table.getn, table.setn local concat, getn, setn, tremove = table.concat, table.getn, table.setn, table.remove
math.fmod = math.mod math.fmod = math.mod
math.huge = 1/0 math.huge = 1/0
string.gmatch = string.gfind string.gmatch = string.gfind
gmatch = string.gfind
function difftime(time2, time1)
if type(time2) ~= "number" then
error(format("bad argument #1 to 'difftime' (number expected, got %s)", time2 and type(time2) or "no value"), 2)
elseif time1 and type(time1) ~= "number" then
error(format("bad argument #2 to 'difftime' (number expected, got %s)", time1 and type(time1) or "no value"), 2)
end
return time1 and time2 - time1 or time2
end
function select(n, ...) function select(n, ...)
if not (type(n) == "number" or (type(n) == "string" and n == "#")) then if not (type(n) == "number" or (type(n) == "string" and n == "#")) then
@@ -231,6 +223,7 @@ function string.reverse(str)
return str return str
end end
strrev = string.reverse
function string.split(delimiter, str) function string.split(delimiter, str)
if type(delimiter) ~= "string" and type(delimiter) ~= "number" then if type(delimiter) ~= "string" and type(delimiter) ~= "number" then
@@ -240,7 +233,7 @@ function string.split(delimiter, str)
end end
local fields = {} local fields = {}
gsub(str, format("([^%s]+)", delimiter), function(c) tinsert(fields, c) end) gsub(str, format("([^%s]+)", delimiter), function(c) fields[getn(fields) + 1] = c end)
return unpack(fields) return unpack(fields)
end end
@@ -303,10 +296,15 @@ function string.trim(str, chars)
local patternStart = loadstring("return \"^["..pattern.."](.-)$\"")() local patternStart = loadstring("return \"^["..pattern.."](.-)$\"")()
local patternEnd = loadstring("return \"^(.-)["..pattern.."]$\"")() local patternEnd = loadstring("return \"^(.-)["..pattern.."]$\"")()
local trimed, x, y = 1 local trimed, x, y = 1, 1, 1
while trimed > 0 do while trimed > 0 do
str, x = gsub(str, patternStart, "%1") if x > 0 then
str, y = gsub(str, patternEnd, "%1") str, x = gsub(str, patternStart, "%1")
end
if y > 0 then
str, y = gsub(str, patternEnd, "%1")
end
trimed = x + y trimed = x + y
end end
+92 -45
View File
@@ -7,9 +7,13 @@ local select = select
local tonumber = tonumber local tonumber = tonumber
local type = type local type = type
local unpack = unpack local unpack = unpack
local find, format, gsub, lower, match, upper = string.find, string.format, string.gsub, string.lower, string.match, string.upper local find, format, gmatch, gsub, lower, match, upper = string.find, string.format, string.gmatch, string.gsub, string.lower, string.match, string.upper
local getn = table.getn local getn = table.getn
--WoW API --WoW API
local debugstack = debugstack
local GetContainerItemInfo = GetContainerItemInfo
local GetContainerItemLink = GetContainerItemLink
local GetContainerNumSlots = GetContainerNumSlots
local GetInventoryItemTexture = GetInventoryItemTexture local GetInventoryItemTexture = GetInventoryItemTexture
local GetItemInfo = GetItemInfo local GetItemInfo = GetItemInfo
local GetQuestGreenRange = GetQuestGreenRange local GetQuestGreenRange = GetQuestGreenRange
@@ -20,12 +24,14 @@ local IsInInstance = IsInInstance
local UnitLevel = UnitLevel local UnitLevel = UnitLevel
--WoW Variables --WoW Variables
local DUNGEON_DIFFICULTY1 = DUNGEON_DIFFICULTY1 local DUNGEON_DIFFICULTY1 = DUNGEON_DIFFICULTY1
local DURABILITY_TEMPLATE = gsub(DURABILITY_TEMPLATE, "%%d / %%d", "(%%d+) / (%%d+)")
local NUM_BAG_FRAMES = NUM_BAG_FRAMES
local TIMEMANAGER_AM = gsub(TIME_TWELVEHOURAM, "^.-(%w+)$", "%1") local TIMEMANAGER_AM = gsub(TIME_TWELVEHOURAM, "^.-(%w+)$", "%1")
local TIMEMANAGER_PM = gsub(TIME_TWELVEHOURPM, "^.-(%w+)$", "%1") local TIMEMANAGER_PM = gsub(TIME_TWELVEHOURPM, "^.-(%w+)$", "%1")
local DURABILITY_TEMPLATE = gsub(DURABILITY_TEMPLATE, "%%d / %%d", "(%%d+) / (%%d+)")
--Libs --Libs
local LBC = LibStub("LibBabble-Class-3.0"):GetLookupTable() local LBC = LibStub("LibBabble-Class-3.0"):GetLookupTable()
local LBZ = LibStub("LibBabble-Zone-3.0"):GetLookupTable() local LBZ = LibStub("LibBabble-Zone-3.0"):GetLookupTable()
local IFDB = LibStub("ItemFamilyDB")
CLASS_SORT_ORDER = { CLASS_SORT_ORDER = {
"WARRIOR", "WARRIOR",
@@ -114,6 +120,31 @@ function hooksecurefunc(a1, a2, a3)
end end
end end
local secureFuncs = {
CameraOrSelectOrMoveStop = true,
MoveBackwardStop = true,
MoveForwardStop = true,
PitchDownStop = true,
PitchUpStop = true,
StrafeLeftStart = true,
StrafeRightStart = true,
ToggleMouseMove = true,
TurnLeftStart = true,
TurnOrActionStop = true,
TurnRightStart = true,
CameraOrSelectOrMoveStart = true,
Jump = true,
MoveBackwardStart = true,
MoveForwardStart = true,
PitchDownStart = true,
PitchUpStart = true,
StrafeLeftStop = true,
StrafeRightStop = true,
ToggleRun = true,
TurnLeftStop = true,
TurnOrActionStart = true,
TurnRightStop = true
}
--[[ issecurevariable([table], variable) --[[ issecurevariable([table], variable)
Returns 1, nil for undefined variables. This is because an undefined variable is secure since you have not tainted it. Returns 1, nil for undefined variables. This is because an undefined variable is secure since you have not tainted it.
Returns 1, nil for all untainted variables (i.e. Blizzard variables). Returns 1, nil for all untainted variables (i.e. Blizzard variables).
@@ -125,7 +156,18 @@ function issecurevariable(t, var)
-- if type(var) ~= "table" or type(var) ~= "string" then -- if type(var) ~= "table" or type(var) ~= "string" then
-- error("Usage: issecurevariable([table,] \"variable\")", 2) -- error("Usage: issecurevariable([table,] \"variable\")", 2)
-- end -- end
return
local isSecure = secureFuncs[t] and 1 or nil
if not isSecure then
return
end
local taint
for x in gmatch(debugstack(), "\\AddOns\\(.-)\\") do
taint = x
end
return isSecure, taint
end end
function tContains(table, item) function tContains(table, item)
@@ -155,6 +197,16 @@ function UnitAura(unit, i, filter)
end end
end end
function difftime(time2, time1)
if type(time2) ~= "number" then
error(format("bad argument #1 to 'difftime' (number expected, got %s)", time2 and type(time2) or "no value"), 2)
elseif time1 and type(time1) ~= "number" then
error(format("bad argument #2 to 'difftime' (number expected, got %s)", time1 and type(time1) or "no value"), 2)
end
return time1 and time2 - time1 or time2
end
function BetterDate(formatString, timeVal) function BetterDate(formatString, timeVal)
local dateTable = date("*t", timeVal) local dateTable = date("*t", timeVal)
local amString = (dateTable.hour >= 12) and "PM" or "AM" local amString = (dateTable.hour >= 12) and "PM" or "AM"
@@ -265,29 +317,6 @@ function GetMapNameByID(id)
return mapByID[tonumber(id)] return mapByID[tonumber(id)]
end end
local LBIBF = LibStub("LibBabble-ItemBagFamily-3.0")
function GetItemFamily(id)
if not id then return end
if type(id) == "table" or type(id) == "function" then return end
if type(id) == "string" then
id = tonumber(match(id, "item:(%d+)"))
end
return LBIBF.ItemFamily[id] or 0
end
function GetBagFamily(id)
if not id then return end
if type(id) == "table" or type(id) == "function" then return end
if type(id) == "string" then
id = tonumber(match(id, "item:(%d+)"))
end
return LBIBF.ItemBagFamily[id] or 0
end
local arrow local arrow
function GetPlayerFacing() function GetPlayerFacing()
if not arrow then if not arrow then
@@ -417,6 +446,7 @@ function GetItemInfoByName(itemName)
end end
if find(itemName, " of ") then if find(itemName, " of ") then
-- random enchantments
itemName = gsub(itemName, " of Spirit", "") itemName = gsub(itemName, " of Spirit", "")
itemName = gsub(itemName, " of Intellect", "") itemName = gsub(itemName, " of Intellect", "")
itemName = gsub(itemName, " of Strength", "") itemName = gsub(itemName, " of Strength", "")
@@ -461,7 +491,7 @@ function GetItemInfoByName(itemName)
for itemID = 1, LAST_ITEM_ID do for itemID = 1, LAST_ITEM_ID do
name = GetItemInfo(itemID) name = GetItemInfo(itemID)
if name ~= "" and name ~= nil then if name ~= nil and name ~= "" then
itemInfoDB[name] = itemID itemInfoDB[name] = itemID
if name == itemName then if name == itemName then
@@ -476,19 +506,36 @@ function GetItemInfoByName(itemName)
return GetItemInfo(itemInfoDB[itemName]) return GetItemInfo(itemInfoDB[itemName])
end end
function GetItemFamily(item, isBag)
if not item or type(item) ~= "number" or type(item) ~= "string" then return end
if type(item) == "string" then
local _, _, itemID = find(itemLink, "(%d+):")
if not itemID then return end
item = tonumber(itemID)
end
if item > LAST_ITEM_ID then return end
return (isBag and IFDB.ItemBagFamily[item] or IFDB.ItemFamily[item]) or 0
end
function GetItemCount(itemName) function GetItemCount(itemName)
if type(itemName) ~= "string" then return 0 end
local count = 0 local count = 0
for bag = NUM_BAG_FRAMES, 0, -1 do for bag = NUM_BAG_FRAMES, 0, -1 do
for slot = 1, GetContainerNumSlots(bag) do for slot = 1, GetContainerNumSlots(bag) do
local _, itemCount = GetContainerItemInfo(bag, slot) local _, itemCount = GetContainerItemInfo(bag, slot)
if itemCount then if itemCount then
local itemLink = GetContainerItemLink(bag, slot) local itemLink = GetContainerItemLink(bag, slot)
local _, _, itemParse = find(itemLink, "(%d+):") local _, _, itemID = find(itemLink, "(%d+):")
local queryName, _, _, _, _, _ = GetItemInfo(itemParse) local queryName = GetItemInfo(itemID)
if queryName and queryName ~= "" then
if queryName == itemName then if queryName and queryName == itemName then
count = count + itemCount count = count + itemCount
end
end end
end end
end end
@@ -497,22 +544,22 @@ function GetItemCount(itemName)
return count return count
end end
local scan local scanTooltip
function GetInventoryItemDurability(slot) function GetInventoryItemDurability(slot)
if not GetInventoryItemTexture("player", slot) then return nil, nil end if not GetInventoryItemTexture("player", slot) then return end
if not scan then if not scanTooltip then
scan = CreateFrame("GameTooltip", "DurabilityScan", nil, "ShoppingTooltipTemplate") scanTooltip = CreateFrame("GameTooltip", "Compatibility_ScanTooltip", nil, "ShoppingTooltipTemplate")
scan:SetOwner(UIParent, "ANCHOR_NONE") scanTooltip:SetOwner(UIParent, "ANCHOR_NONE")
end end
scan:ClearLines() scanTooltip:ClearLines()
scan:SetInventoryItem("player", slot) scanTooltip:SetInventoryItem("player", slot)
for i = 4, scan:NumLines() do for i = 4, scanTooltip:NumLines() do
local text = _G[scan:GetName().."TextLeft"..i]:GetText() local text = _G["Compatibility_ScanTooltipTextLeft"..i]:GetText()
for durability, max in string.gfind(text, DURABILITY_TEMPLATE) do for current, maximum in gmatch(text, DURABILITY_TEMPLATE) do
return tonumber(durability), tonumber(max) return tonumber(current), tonumber(maximum)
end end
end end
end end
+1 -3
View File
@@ -7,10 +7,8 @@ Dependencies: None
License: MIT License: MIT
]] ]]
local match = string.match
local MAJOR_VERSION = "LibBabble-Zone-3.0" local MAJOR_VERSION = "LibBabble-Zone-3.0"
local MINOR_VERSION = 90000 + tonumber(match("$Revision: 107 $", "%d+")) local MINOR_VERSION = 90000 + tonumber(string.match("$Revision: 107 $", "%d+"))
if not LibStub then error(MAJOR_VERSION .. " requires LibStub.") end if not LibStub then error(MAJOR_VERSION .. " requires LibStub.") end
local lib = LibStub("LibBabble-3.0"):New(MAJOR_VERSION, MINOR_VERSION) local lib = LibStub("LibBabble-3.0"):New(MAJOR_VERSION, MINOR_VERSION)