From 447bb8b39030559d9b1da891517ed3f04790fa63 Mon Sep 17 00:00:00 2001 From: Road-block Date: Mon, 18 Jun 2018 02:22:27 +0300 Subject: [PATCH] Improve compat shims, rename and move the file with other support files in libs. Remove Sol and dependencies. --- TourGuideVanilla.toc | 4 +- libs/LibStub.lua | 54 ------------- libs/Sol.lua | 181 ------------------------------------------- libs/compat.lua | 89 +++++++++++++++++++++ lua52to50compat.lua | 31 -------- 5 files changed, 90 insertions(+), 269 deletions(-) delete mode 100644 libs/LibStub.lua delete mode 100644 libs/Sol.lua create mode 100644 libs/compat.lua delete mode 100644 lua52to50compat.lua diff --git a/TourGuideVanilla.toc b/TourGuideVanilla.toc index 0bd4662..ed9163c 100644 --- a/TourGuideVanilla.toc +++ b/TourGuideVanilla.toc @@ -11,9 +11,7 @@ ## OptionalDeps: TomTom, pfQuest, LightHeaded -lua52to50compat.lua -libs\LibStub.lua -libs\Sol.lua +libs\compat.lua libs\Dongle.lua WidgetWarlock.lua diff --git a/libs/LibStub.lua b/libs/LibStub.lua deleted file mode 100644 index 65a6684..0000000 --- a/libs/LibStub.lua +++ /dev/null @@ -1,54 +0,0 @@ --- $Id$ --- LibStub is a simple versioning stub meant for use in Libraries. http://www.wowace.com/addons/libstub/ for more info --- LibStub is hereby placed in the Public Domain --- Credits: Kaelten, Cladhaire, ckknight, Mikk, Ammo, Nevcairiel, joshborke -local LIBSTUB_MAJOR, LIBSTUB_MINOR = "LibStub", 2 -- NEVER MAKE THIS AN SVN REVISION! IT NEEDS TO BE USABLE IN ALL REPOS! -local _G = getfenv(0) -local LibStub = _G[LIBSTUB_MAJOR] - --- Check to see is this version of the stub is obsolete -if not LibStub or LibStub.minor < LIBSTUB_MINOR then - LibStub = LibStub or {libs = {}, minors = {} } - _G[LIBSTUB_MAJOR] = LibStub - LibStub.minor = LIBSTUB_MINOR - - -- LibStub:NewLibrary(major, minor) - -- major (string) - the major version of the library - -- minor (string or number ) - the minor version of the library - -- - -- returns nil if a newer or same version of the lib is already present - -- returns empty library object or old library object if upgrade is needed - function LibStub:NewLibrary(major, minor) - assert(type(major) == "string", "Bad argument #2 to `NewLibrary' (string expected)") - local beg_pos, end_pos = string.find(minor, "%d+") - minor = assert(tonumber(string.sub(minor, beg_pos, end_pos)), - "Minor version must either be a number or contain a number.") - - local oldminor = self.minors[major] - if oldminor and oldminor >= minor then return nil end - self.minors[major], self.libs[major] = minor, self.libs[major] or {} - return self.libs[major], oldminor - end - - -- LibStub:GetLibrary(major, [silent]) - -- major (string) - the major version of the library - -- silent (boolean) - if true, library is optional, silently return nil if its not found - -- - -- throws an error if the library can not be found (except silent is set) - -- returns the library object if found - function LibStub:GetLibrary(major, silent) - if not self.libs[major] and not silent then - error(string.format("Cannot find a library instance of %q.", tostring(major)), 2) - end - return self.libs[major], self.minors[major] - end - - -- LibStub:IterateLibraries() - -- - -- Returns an iterator for the currently registered libraries - function LibStub:IterateLibraries() - return pairs(self.libs) - end - - setmetatable(LibStub, { __call = LibStub.GetLibrary }) -end diff --git a/libs/Sol.lua b/libs/Sol.lua deleted file mode 100644 index 535ae2d..0000000 --- a/libs/Sol.lua +++ /dev/null @@ -1,181 +0,0 @@ ---[[ - - Copyright (c) 2018 Martin Jesper Low Madsen - - This library is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as published by the - Free Software Foundation; either version 2.1 of the License, or (at your - option) any later version. - - This library is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this library; if not, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - ---]] - - -if not LibStub then return end - -local ADDON_NAME = 'Sol' - -local Sol = LibStub:NewLibrary(string.format('%s-1.0', ADDON_NAME), 0) -if not Sol then return end - -local _G = getfenv(0) - --- Error handler for more verbose error tracing -local function ErrorHandler(msg) - local traces = { string.split('\n', debugstack()) } - - local source = '' - for i = 1, select('#', unpack(traces)) do - if (not string.find(traces[i], ADDON_NAME) and - not string.find(traces[i], '%[C%]')) then - source = traces[i] - break - end - end - - local parts = { string.split(':', msg) } - message(source .. ':' .. parts[3]) -end - ---[[ - Global empty function ---]] -NOOP = function() end - - ---[[ - Math ---]] -math.modf = math.modf or function(num) - local int = math.floor(num) - local frac = math.abs(num) - math.abs(int) - - return int, frac -end - -math.fmod = math.fmod or function(x, y) - return x - math.floor(x / y) * y -end - - ---[[ - Table and indexing ---]] -string.match = string.match or function(str, pattern) - seterrorhandler(ErrorHandler) - - local str_type = type(str) - assert(str_type == 'string', - string.format("bad argument #1 to `match' (string expected, got %s)", str_type)) - - str_type = type(pattern) - assert(str_type == 'string', - string.format("bad argument #2 to `match' (string expected, got %s)", str_type)) - - seterrorhandler(message) - - local tbl_res = { string.find(str, pattern) } - - if tbl_res[3] then - return select(3, unpack(tbl_res)) - else - return tbl_res[1], tbl_res[2] - end -end - -string.gmatch = string.gmatch or function(str, pattern) - local init = 0 - - return function() - local tbl = { string.find(str, pattern, init) } - - local start_pos = tbl[1] - local end_pos = tbl[2] - - if start_pos then - init = end_pos + 1 - - if tbl[3] then - return unpack({select(3, unpack(tbl))}) - else - return string.sub(str, start_pos, end_pos) - end - end - end -end - -string.join = string.join or function(delim, ...) - if type(arg) == 'table' then - return table.concat(arg, delim) - else - return delim - end -end - -string.split = string.split or function(delim, s, limit) - local split_string = {} - local rest = {} - - local i = 1 - for str in string.gfind(s, '([^' .. delim .. ']+)' .. delim .. '?') do - if limit and i >= limit then - table.insert(rest, str) - else - table.insert(split_string, str) - end - - i = i + 1 - end - - if limit then - table.insert(split_string, string.join(delim, unpack(rest))) - end - - return unpack(split_string) -end - -string.trim = string.trim or function(str) - return string.gsub(str, '^%s*(.-)%s*$', '%1') -end - -strjoin = strjoin or string.join - - ---[[ - Table and indexing ---]] -select = select or function(idx, ...) - local len = table.getn(arg) - - if type(idx) == 'string' and idx == '#' then - return len - else - local tbl = {} - - for i = idx, len do - table.insert(tbl, arg[i]) - end - - return unpack(tbl) - end -end - -table.wipe = table.wipe or function(tbl) - for i = table.getn(tbl), 1, -1 do - tremove(tbl, i) - end - - for k in next, tbl do - tbl[k] = nil - end -end - -wipe = wipe or table.wipe diff --git a/libs/compat.lua b/libs/compat.lua new file mode 100644 index 0000000..fa4a34a --- /dev/null +++ b/libs/compat.lua @@ -0,0 +1,89 @@ +--[[ + Some shims for Lua and WoW API not available in 1.12.x + -- Roadblock & rsheep +]] +local _G = getfenv(0) +if not _G.select then + select = function(index,...) + assert(tonumber(index) or index=="#","Invalid argument #1 to select(). Usage: select(\"#\"|int,...)") + if index == "#" then + return tonumber(arg.n) or 0 + end + for i=1,index-1 do + table.remove(arg,1) + end + return unpack(arg) + end + _G.select = select +end +if not string.join then + string.join = function(delimiter, list) + assert(type(delimiter)=="string" and type(list)=="table", "Invalid arguments to join(). Usage: string.join(delimiter, list)") + local len = getn(list) + if len == 0 then + return "" + end + local s = list[1] + for i = 2, len do + s = string.format("%s%s%s",s,delimiter,list[i]) + end + return s + end +end +if not string.trim then + string.trim = function(s) + return (string.gsub(s,"^%s*(.-)%s*$", "%1")) + end +end +if not string.split then + string.split = function(...) -- separator, string + assert(arg.n>0 and type(arg[1])=="string", "Invalid arguments to split(). Usage: string.split([separator], subject)") + local sep, s = arg[1], arg[2] + if s == nil then + s, sep = sep, ":" + end + local fields = {} + local pattern = string.format("([^%s]+)", sep) + string.gsub(s, pattern, function(c) fields[table.getn(fields)+1] = c end) + return fields + end +end +if not math.modf then + math.modf = function(f) + if f > 0 then + return math.floor(f), math.mod(f,1) + end + return math.ceil(f), math.mod(f,1) + end +end +if not _G.InCombatLockdown then + InCombatLockdown = function() + return UnitAffectingCombat("player") + end + _G.InCombatLockdown = InCombatLockdown +end +if not _G.GetItemCount then + GetItemCount = function(itemID) + local itemInfoTexture = select(9, GetItemInfo(itemID)) + if itemInfoTexture == nil then return 0 end + local totalItemCount = 0 + for i=0,NUM_BAG_FRAMES do + local numSlots = GetContainerNumSlots(i) + if numSlots > 0 then + for k=1,numSlots do + local itemTexture, itemCount = GetContainerItemInfo(i, k) + if itemInfoTexture == itemTexture then + totalItemCount = totalItemCount + itemCount + end + end + end + end + return totalItemCount + end + _G.GetItemCount = GetItemCount +end + +if not _G.print then + print = function(str) DEFAULT_CHAT_FRAME:AddMessage(tostring(str)) end + _G.print = print +end diff --git a/lua52to50compat.lua b/lua52to50compat.lua deleted file mode 100644 index 2140d34..0000000 --- a/lua52to50compat.lua +++ /dev/null @@ -1,31 +0,0 @@ ---[[ - Some shims for Lua and WoW API not available in 1.12.x - -- Roadblock & rsheep -]] -local _G = getfenv(0) - -if not _G.GetItemCount then - GetItemCount = function(itemID) - local itemInfoTexture = select(9, GetItemInfo(itemID)) - if itemInfoTexture == nil then return 0 end - local totalItemCount = 0 - for i=0,4 do - local numSlots = GetContainerNumSlots(i) - if numSlots > 0 then - for k=1,numSlots do - local itemTexture, itemCount = GetContainerItemInfo(i, k) - if itemInfoTexture == itemTexture then - totalItemCount = totalItemCount + itemCount - end - end - end - end - return totalItemCount - end - _G.GetItemCount = GetItemCount -end - -if not _G.print then - print = function(str) DEFAULT_CHAT_FRAME:AddMessage(str) end - _G.print = print -end