WoWInterface SVN RagePerSecond

[/] [SilvanasAddonsLib.lua] - Rev 2

Compare with Previous | Blame | View Log

--[[
--This Lib is made for addons made by Silvanas it gives some commen functions like print,printf,select,varstring
--i use it so i dont have to redecleare the function so i wont use duoble memory space, if u have multiple addons installed
--it is not a stand alone addon just a file to include. and other ui mods are welkome to use it aslog as they dont moddify it :P
]]--

SilvanasLibVersion = 5.7

if (not Silvanas) or (Silvanas.Version < SilvanasLibVersion) then

        Silvanas = {
            --Libary version
                Version = SilvanasLibVersion,
                --author
                Author = "|cff993366Silvanas|r",
                --hold a list of mods and thier info of registred mods --removed
                LoadedMods = {},

                --Basic print function
                Print = function(msg)
                        if msg and DEFAULT_CHAT_FRAME then
                                DEFAULT_CHAT_FRAME:AddMessage(msg)
                        else
                                Silvanas.Message(msg)
                        end
                end,

                --Basic print function to the 2nd chat window
                Print2 = function(msg)
                    if msg then
                        if ChatFrame2 then
                                ChatFrame2:AddMessage(msg)
                                elseif DEFAULT_CHAT_FRAME then
                                DEFAULT_CHAT_FRAME:AddMessage(msg)
                                else
                                    Silvanas.Message(msg)
                                end
                        end
                end,

                --Print whit text formatting
                Printf = function(...)
                        Silvanas.Print(string.format(unpack(arg)))
                end,

                --Returns a string represention of the variable
                VarString = function(variable)
                        local vartype = type(variable)
                        if vartype == "string" then
                                return variable
                        elseif vartype == "number" then
                                return tostring(variable)
                elseif vartype == "boolean" then
                        if variable then
                                return "true"
                                else
                                        return "false"
                                end
                        else
                        return vartype
                        end
                end,
                
                --Placeholder for old function
                VarPrint = function(variable)
                        return Silvanas.VarString(variable)
                end,

        --Argument selector
                Select = select,

                --Popup message
                Message = function(msg)
                        message("|cFFFFFFFF".. msg .."|r")
                end,

                --/ Command addfunction
                AddCmd = function(Name,Function,CmdTable)
                    if (type(CmdTable) == "table") and (type(Function) == "function") and (type(Name) == "string") then
                if #CmdTable > 0 then
                            SlashCmdList[Name] = Function
                    
                    local i
                            for i=1,#CmdTable,1 do
                                                setglobal("SLASH_".. Name .. i,"/".. CmdTable[i])
                                        end
                                end
                        end
                end,

        --register all events for the frame that are inside the table
        RegisterEvents = function(self,EventArray)
            if type(EventArray) == "table" then
                local Event
                for Event in pairs(EventArray) do   
                                        self:RegisterEvent(Event)
                                end
            end
        end,

                --Registers a mod in the libary (removed)
                AddMod = function() end,
                
                --The player his name (has tobe a function since u can't get UnitName() at load time)
                Me = UnitName("player"),
                
                --Prints out the entire variable to chat (should be called whitout (dept))
                DebugVar = function(variable,name,dept)
                        local prefix,vartype,key,value,suffix
                        if not dept then dept = 0 end
                        if dept > 0 then suffix = ',' else suffix = ';' end                     
                        if not name then name = "Variable" end
                
                        prefix = string.rep(' ',(dept*4))
                        vartype = type(variable)
            
                        if vartype == "string" then
                                Silvanas.Print(prefix .. name .." = \"".. variable .."\"".. suffix)
                        elseif vartype == "number" then
                                Silvanas.Print(prefix .. name .." = ".. variable .. suffix)
                        elseif vartype == "boolean" then
                            if variable then
                                Silvanas.Print(prefix .. name .." = true".. suffix)
                                else
                                        Silvanas.Print(prefix .. name .." = false".. suffix)
                                end
                        elseif vartype == "table" then
                                if type(name) == "number" then name = "[".. name .."]" end
                                Silvanas.Print(prefix .. name .." = {")
                
                if dept >= 5 then
                    Silvanas.Print(prefix .. string.rep(' ',4) .. "..." .. suffix)   
                    return
                end
                
                                for key,value in pairs(variable) do
                    if(value == variable) then
                        Silvanas.DebugVar("SELF-REFERENCE",key,(dept+1))
                    else
                                            Silvanas.DebugVar(value,key,(dept+1))
                    end
                                end
                            Silvanas.Print(prefix .."}".. suffix)
                        else
                                Silvanas.Print(prefix .. name .." = ".. vartype .. suffix)
                        end
                end,
                
                --transform the colored version string into a number for easyer comparisation
                GetVersionNumber = function(version)
                local Vb,Vm,Vs
                Vb,Vm,Vs = string.match(version,"%x%x%x%x%x%x%x%x(%d)|.*%x%x%x%x%x%x%x%x(%d)|.*%x%x%x%x%x%x%x%x(%d)|")
                
                if Vb then
                        return tonumber(Vb .. Vm .. Vs)
                        else
                                return -1
                        end             
                end,
        
        --queued functions
        DelayTable = {},
        
        --timescale hook
        InitTimeDelay = function()
            if not Silvanas.OnUpdate then
                Silvanas.OnUpdate = function()
                    Silvanas.DelayCore()
                end
                
                CreateFrame("Frame"):SetScript("OnUpdate",Silvanas.OnUpdate)
            end
        end,
        
        DelayCore = function()
            local time = time()
            for k,v in pairs(Silvanas.DelayTable) do
                if time >= v.time then
                    local funct = v
                    table.remove(Silvanas.DelayTable,k)
                    funct.run(unpack(funct.arg))
                end
            end
        end,
        
        DelayFunc = function(secs,func,...)
            if not Silvanas.OnUpdate then
                Silvanas.Message("'Silvanas.DelayFunc(secs,func,<params>)' called while not Intialsed whit 'Silvanas.InitTimeDelay()'")
                return
            end
            if type(func) ~= "function" or type(secs) ~= "number" then
                Silvanas.Message("'Silvanas.DelayFunc(secs,func,<params>)' usage: seconds to delay, function to call, optional parametes'")
                return
            end
            table.insert(Silvanas.DelayTable,{
                time = time() + secs,
                run = func,
                arg = {...},
            })    
        end,
        
        RemoveDelayedFunc = function(func)
            if type(func) ~= "function"  then
                Silvanas.Message("'Silvanas.RemoveDelayedFunc(func)' usage: function to remove of the list call")
                return
            end
            
            local newTable = {}
            local _, value
            for _, value in pairs(Silvanas.DelayTable) do
                if value.func ~= func then
                    table.insert(newTable, value)
                end   
            end
            
            Silvanas.DelayTable = newTable
        end,
        }
end

Compare with Previous | Blame