Quantcast

Introduce LibProperty

Scott Sibley [08-09-10 - 04:52]
Introduce LibProperty
Don't keep adding deleted default lines.
Filename
Modules/Text.lua
Property.lua
StarTip.lua
embeds.xml
diff --git a/Modules/Text.lua b/Modules/Text.lua
index 18bdb98..7a93663 100644
--- a/Modules/Text.lua
+++ b/Modules/Text.lua
@@ -377,17 +377,18 @@ function mod:OnInitialize()
 	end

 	for i, v in ipairs(defaultLines) do
-		if not v.tagged then
+		if not v.tagged and not v.deleted then
 			tinsert(self.db.profile.lines, v)
 		end
 	end

-	if self.db.profile.empty then
+	--[[if self.db.profile.empty then
 		for i, v in ipairs(defaultLines) do
 			tinsert(self.db.profile.lines, v)
 		end
 		self.db.profile.empty = false
-	end
+	end]]
+
     self.leftLines = StarTip.leftLines
     self.rightLines = StarTip.rightLines
     self:RegisterEvent("UPDATE_FACTION")
@@ -418,6 +419,18 @@ function mod:UPDATE_FACTION()
     end
 end

+
+function mod:MarqUpdate(line)
+	local str = ''
+	local update = 0
+
+	update = update + StarTip:EvalCode(line.prefix)
+	update = update + StarTip:EvalCode(line.postfix)
+	update = update + StarTip:EvalCode(line.style)
+
+	StarTip:EvalCode(line.left)
+end
+
 local function makeMarquee(line, text)

 	return text
@@ -432,7 +445,7 @@ function mod:CreateLines()
         local lineNum = 0
 		GameTooltip:ClearLines()
         for i, v in ipairs(self) do
-			if v.enabled then
+			if v.enabled and not v.deleted then

                 local left, right, c
                 if v.right then
@@ -521,7 +534,7 @@ function mod:RebuildOpts()
 		},
 	}
     for i, v in ipairs(self.db.profile.lines) do
-		if type(v) ~= "table" then break end
+		if type(v) ~= "table" or v.deleted then break end
         options["line" .. i] = {
             name = v.name,
             type = "group",
@@ -533,12 +546,15 @@ function mod:RebuildOpts()
                     get = function() return v.left end,
                     set = function(info, val) v.left = val; v.leftDirty = true end,
                     validate = function()
-						local ret, err = loadstring(v.left or "", "validate")
+						local ret, err = StarTip:ExecuteCode("validate", v.left)
+
 						if not ret then
 							StarTip:Print(("Code failed to load. Error message: %s"):format(err or ""))
+							v.error = true
 							return false
 						end
-						StarTip:Print(("Code loaded without error. Return value: %s"):format(ret(xpcall, errorhandler) or ""))
+
+						StarTip:Print(("Code loaded without error. Return value: %s"):format(ret or ""))
 					return true

 					end,
@@ -553,14 +569,16 @@ function mod:RebuildOpts()
                     get = function() return v.right end,
                     set = function(info, val) v.right = val; v.rightDirty = true end,
                     validate = function()
-						local ret, err = loadstring(v.right or "", "validate")
+						local ret, err = StarTip:ExecuteCode("validate", v.right)
+
 						if not ret then
-							local text = ("Code failed to execute. Error message: %s"):format(err or "")
-							StarTip:Print(text)
-							return text
+							StarTip:Print(("Code failed to load. Error message: %s"):format(err or ""))
+							return false
 						end
-						StarTip:Print(("Code executed without error. Return value: %s"):format(ret(xpcall, errorhandler) or ""))
-						return true
+
+						StarTip:Print(("Code loaded without error. Return value: %s"):format(ret or ""))
+					return true
+
 					end,
                     multiline = true,
 					width = "full",
@@ -638,7 +656,22 @@ function mod:RebuildOpts()
 					desc = "Delete this line",
 					type = "execute",
 					func = function()
-						table.remove(self.db.profile.lines, i)
+						local deleted
+
+						for j, vv in ipairs(defaultLines) do
+							if vv.name == v.name then
+								deleted = true
+							else
+
+							end
+						end
+
+						if deleted then
+							v.deleted = true
+						else
+							table.remove(self.db.profile.lines, i)
+						end
+
 						self:RebuildOpts()
 						StarTip:RebuildOpts()
 						self:CreateLines()
diff --git a/Property.lua b/Property.lua
new file mode 100644
index 0000000..5ae911f
--- /dev/null
+++ b/Property.lua
@@ -0,0 +1,73 @@
+
+local MAJOR = "LibProperty"
+local MINOR = 1
+assert(LibStub, MAJOR.." requires LibStub")
+local LibProperty = LibStub:NewLibrary(MAJOR, MINOR)
+if not LibProperty 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 = StarTip:ExecuteCode(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/StarTip.lua b/StarTip.lua
index 918942e..96e4dda 100644
--- a/StarTip.lua
+++ b/StarTip.lua
@@ -1,5 +1,5 @@
 StarTip = LibStub("AceAddon-3.0"):NewAddon("StarTip: @project-version@", "AceConsole-3.0", "AceHook-3.0", "AceEvent-3.0", "AceTimer-3.0")
-StarTip.version = GetAddOnMetadata("StarTip", "X-StarTip-Version")
+StarTip.version = GetAddOnMetadata("StarTip", "X-StarTip-Version") or ""
 local LibDBIcon = LibStub("LibDBIcon-1.0")
 local LSM = _G.LibStub("LibSharedMedia-3.0")
 local LDB = LibStub:GetLibrary("LibDataBroker-1.1")
@@ -243,7 +243,9 @@ end

 do
 	local pool = setmetatable({},{__mode='v'})
-	StarTip.ExecuteCode = function(self, tag, code, dontSandbox)
+	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]
@@ -258,7 +260,7 @@ do

 		if not runnable then
 			StarTip:Print(err)
-			return ""
+			return nil, err, 0
 		end


@@ -275,10 +277,49 @@ do
 			StarTip.del(table)
 		end

-		return runnable(xpcall, errorhandler)
+		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 = {}
+
+property.Eval = function(self, runnable)
+	local is_valid
+	local result
+end
+
+property.P2N = function(self)
+
+end
+
+property.P2S = function(self)

+end
+
+property.SetValue = function(self)
+
+end
+
+do
+	StarTip.EvalCode = function(self, property)
+		if not self or not property then return end
+
+
+	end
+
+end
+--]]
 StarTip:SetDefaultModuleState(false)

 function StarTip:OnInitialize()
diff --git a/embeds.xml b/embeds.xml
index 42762ba..41596ec 100644
--- a/embeds.xml
+++ b/embeds.xml
@@ -19,5 +19,6 @@
 <Script file="Libs\LibDatabroker-1.1\LibDatabroker-1.1.lua"/>
 <Script file="Libs\LibDBIcon-1.0\LibDBIcon-1.0.lua"/>
 <Script file="Libs\LibQtip-1.0\LibQtip-1.0.lua"/>
+<Script file="Property.lua"/>

 </Ui>