diff --git a/LibEvaluator.lua b/LibEvaluator.lua new file mode 100644 index 0000000..5fc7b23 --- /dev/null +++ b/LibEvaluator.lua @@ -0,0 +1,81 @@ + + +local MAJOR = "LibEvaluator-1.0" +local MINOR = 1 +assert(LibStub, MAJOR.." requires LibStub") +local Evaluator = LibStub:NewLibrary(MAJOR, MINOR) +if not Evaluator then return end + +if not Evaluator.__index then + Evaluator.__index = Evaluator + Evaluator.pool = setmetatable({}, {__mode = "k"}) +end + +Evaluator.__call = function(...) + self.ExecuteCode(...) +end + +function Evaluator:New() + + local obj = next(self.pool) + + if obj then + self.pool[obj] = nil + else + obj = {} + end + + setmetatable(obj, self) + + return obj +end + +do + local pool = setmetatable({},{__mode='v'}) + Evaluator.ExecuteCode = function(self, tag, code, dontSandbox, defval) + if not defval and not dontDefault then defval = "" end + + if not self or not tag or not code then return end + + local runnable = pool[code] + local err + + if not runnable then + runnable, err = loadstring(code, tag) + pool[code] = runnable + end + + if not runnable then + StarTip:Print(err) + return nil, err, 0 + end + + + if not dontSandbox then + local table = StarTip.new() + table.self = self + table._G = _G + table.StarTip = StarTip + table.select = select + table.format = format + + setfenv(runnable, table) + + StarTip.del(table) + end + + local ret1, ret2, ret3 = runnable(xpcall, errorhandler) + + defval = loadstring('return ' .. (defval or ""), "defval") or function() StarTip:Print("Error at defval"); return "" end + + if not ret1 then + ret1 = defval + end + + if type(ret1) == "function" then + ret1 = ret1() + end + + return ret1, ret2 + end +end \ No newline at end of file diff --git a/LibProperty.lua b/LibProperty.lua new file mode 100644 index 0000000..a52c950 --- /dev/null +++ b/LibProperty.lua @@ -0,0 +1,75 @@ + +local MAJOR = "LibProperty-1.0" +local MINOR = 1 +assert(LibStub, MAJOR.." requires LibStub") +local LibProperty = LibStub:NewLibrary(MAJOR, MINOR) +local Evaluator = LibStub("LibEvaluator-1.0") + +if not LibProperty or not Evaluator then return end + +if not LibProperty.pool then + LibProperty.pool = setmetatable({},{__mode='k'}) + LibProperty.__index = LibProperty +end + +function LibProperty:New(v, line, name, defval) + self.visitor = v + self.is_valid = false + self.expression = line.left + + if self.expression ~= nil and type(expression) == "string" then + self.result = Evaluator(self.expression, defval) + if self.result == nil then + StarTip:Print(("Property: %s in \"%s\""):format(self.result, self.expression)) + end + elseif self.expression ~= nil then + StarTip:Print(("Property: <%s> has no expression."):format(name)) + end + + if not frame then + error("No frame specified") + end + + local obj = next(self.pool) + + if obj then + self.pool[obj] = nil + else + obj = {} + end + + setmetatable(obj, self) + + return obj +end + +function LibProperty:Eval() + if not self.is_valid then return -1 end + + local update = 1 + + local old = self.result + + self.result = strlen(StarTip:ExecuteCode(self.expression)) and 1 + + if old == result then + update = 0 + end + + return update +end + +function LibProperty:P2N() + if type(self.result) == "number" then + return self.result + else + return tonumber(self.result) + end +end + +function LibProperty:P2S() + if type(self.result) ~= "number" and type(self.result) ~= "string" then + return "" + end + return ("%s%d"):format(self.result, self.result) +end diff --git a/Modules/Text.lua b/Modules/Text.lua index 7a93663..031c11c 100644 --- a/Modules/Text.lua +++ b/Modules/Text.lua @@ -1,6 +1,7 @@ local mod = StarTip:NewModule("Text", "AceTimer-3.0", "AceEvent-3.0") mod.name = "Text" mod.toggled = true +local Evaluator = LibStub("LibEvaluator-1.0"):New() local _G = _G local GameTooltip = _G.GameTooltip local StarTip = _G.StarTip @@ -442,6 +443,7 @@ function mod:CreateLines() llines[i] = v end lines = setmetatable(llines, {__call=function(self) + local lineNum = 0 GameTooltip:ClearLines() for i, v in ipairs(self) do @@ -449,17 +451,18 @@ function mod:CreateLines() local left, right, c if v.right then - right, c = StarTip.ExecuteCode(mod, v.name, v.right) - left, cc = StarTip.ExecuteCode(mod, v.name, v.left) + right, c = Evaluator.ExecuteCode(mod, v.name, v.right) + left, cc = Evaluator.ExecuteCode(mod, v.name, v.left) else right = '' - left, c = StarTip.ExecuteCode(mod, v.name, v.left) + left, c = Evaluator.ExecuteCode(mod, v.name, v.left) end if v.marquee and v.width and left then left = makeMarquee(v, left) right = '' end + if left and right then lineNum = lineNum + 1 if v.right then diff --git a/StarTip.lua b/StarTip.lua index 96e4dda..25fee8a 100644 --- a/StarTip.lua +++ b/StarTip.lua @@ -241,56 +241,6 @@ local function copy(tbl) return localCopy end -do - local pool = setmetatable({},{__mode='v'}) - StarTip.ExecuteCode = function(self, tag, code, dontSandbox, defval, dontDefault) - if not defval and not dontDefault then defval = "" end - - if not self or not tag or not code then return end - - local runnable = pool[code] - local err - - if not runnable then - runnable, err = loadstring(code, tag) - if runnable then - pool[code] = runnable - end - end - - if not runnable then - StarTip:Print(err) - return nil, err, 0 - end - - - if not dontSandbox then - local table = StarTip.new() - table.self = self - table._G = _G - table.StarTip = StarTip - table.select = select - table.format = format - - setfenv(runnable, table) - - StarTip.del(table) - end - - local ret = runnable(xpcall, errorhandler) - - defval = loadstring('return ' .. (defval or ""), "defval") or function() StarTip:Print("Error at defval"); return "" end - - local isDefval = false - if not ret then ret = defval; isDefval = true end - - if type(ret) == "function" then - ret = ret() - end - - return ret, err, strlen(ret or "") - end -end --[[ local property = {} diff --git a/StarTip.toc b/StarTip.toc index 2bc6ced..c5a668f 100644 --- a/StarTip.toc +++ b/StarTip.toc @@ -15,6 +15,9 @@ embeds.xml Localization\enUS.lua +LibEvaluator.lua +LibProperty.lua + StarTip.lua Modules\Fade.lua Modules\Appearance.lua diff --git a/embeds.xml b/embeds.xml index 41596ec..20430cf 100644 --- a/embeds.xml +++ b/embeds.xml @@ -20,5 +20,6 @@ <Script file="Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua"/> <Script file="Libs\LibQtip-1.0\LibQtip-1.0.lua"/> <Script file="Property.lua"/> +<Script file="Evaluator.lua"/> </Ui>