Web code download and launch

Hello,

I made a simple program for download some function strings from web and launch it.

It launches a simple web browser I made.

-- web program loader & launcher --

programurl = "http://pastebin.com/raw.php?i=Te159hKt"

-- Use this function to perform your initial setup
function setup() 
    count,global,allloaded,started,urllist,programname,message = 0,nil,nil,nil,{},nil,nil 
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    if(global and _G[global] and allloaded) then
        _G[global]:draw() 
        fill(170, 182, 150, 170); stroke(224, 226, 224, 68); strokeWidth(1); rectMode(CORNER)
        rect(0,0,50,24); fill(0, 0, 0, 135); textMode(CORNER); text("back",0,0);
        return 
    end
    strokeWidth(5); fill(171, 183, 150, 255) stroke(225, 226, 225, 255); rectMode(CORNER);
    rect(WIDTH/2-100,HEIGHT/2-100,200,200); fill(0, 0, 0, 255); textMode(CENTER)
    textWrapWidth(200)
    if started then text("loading...",WIDTH/2,HEIGHT/2)
    elseif message then text(message,WIDTH/2,HEIGHT/2)
    elseif programname then text(programname,WIDTH/2,HEIGHT/2) 
    else text("tap to download",WIDTH/2,HEIGHT/2) end
end

function touched(touch)
    if(global and _G[global] and allloaded) then
        if(touch.state == BEGAN) and touch.x < 50 and touch.y <24 then
            allloaded = nil; started = nil;
        else
            _G[global]:touched(touch) 
        end
    end
    if not started then
        if touch.x >= WIDTH/2-100 and touch.x <= WIDTH/2+100 then
            if touch.y >= HEIGHT/2-100 and touch.y <= HEIGHT/2+100 then
                if touch.state == BEGAN then start(); started = true; end end end end
end

function keyboard(key) 
    if(global and _G[global] and allloaded) then _G[global]:keyboard(key) end end

function start() 
    if global then count = 0; for i,v in pairs(urllist) do http.get(v,success,failed) end 
    else http.get(programurl,success,failed) end end

function success(data,status,headers)
    if(status == 200) then
        if global then
            count = count + 1
            tablename = string.match(data,"--%s*([^-]+)%s*--")
            print(tablename.." downloaded")
            local  j,data2 = 0,""
            for i=1,data:len() do char = data:sub(i,i);
                if char:byte() ~= 13 and char:byte() ~= 194 and char:byte() ~= 160 then 
                    j = j + 1; data2 = data2..char; end
                end
            _G[tablename] = loadstring(data2)()
            print("Table "..tablename..","..data2:len().."byte was loaded")
            if #urllist == count then message = nil
                print("all finished"); _G[global]:init(); allloaded = true; end;
        else
            crlf = string.char(13)..string.char(10)
            urllist = split(data:sub(data:find(crlf)+2),crlf)
            programname,global = data:match("([^,]+),([^,"..crlf.."]+)"..crlf)
            print("Program Name:"..programname..", Main table = "..global)
            print("table urls:")
            for i,v in ipairs(urllist) do print(v) end
            started = nil
            message = nil
        end
    else print(status..' : '..data) end
end

function failed(error) message = error; started = nil end

function split(str, delim)
    -- Eliminate bad cases...
    if string.find(str, delim) == nil then return { str } end

    local result,pat,lastpos = {},"(.-)" .. delim .. "()",nil
    for part, pos in string.gfind(str, pat) do table.insert(result, part); lastPos = pos; end
    table.insert(result, string.sub(str, lastPos))
    return result
end

WoW :smiley:

2nd program

-- web program loader & launcher --
programlisturl = "http://pastebin.com/raw.php?i=C87zFV2Z"
-- Use this function to perform your initial setup
function setup() 
    count,allloaded,started,programname,message = 0,nil,nil,nil,nil 
    menu = ProgramMenu()
    ring = LoadingRing()
end
-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    if(menu.loadpoint and _G[menu.loadpoint] and allloaded) then
        _G[menu.loadpoint]:draw() 
    end
    fill(170, 182, 150, 170); stroke(224, 226, 224, 68); strokeWidth(1); rectMode(CORNER)
    rect(0,0,50,24); fill(0, 0, 0, 135); textMode(CORNER);
    fontSize(20)
    if(menu.loadpoint and _G[menu.loadpoint] and allloaded) then
        text("back",0,0);
    else
        text(" end",0,0);
        menu:draw()
        if started then ring:draw(WIDTH/2,HEIGHT/2)
        elseif message then text(message,WIDTH/2,HEIGHT/2)
        end
    end
end
function touched(touch)
    if(menu.loadpoint and _G[menu.loadpoint] and allloaded) then
        if(touch.state == BEGAN) and touch.x < 50 and touch.y <24 then
            allloaded = nil; started = nil; menu.selecteditem = nil;
        else
            _G[menu.loadpoint]:touched(touch) 
        end
    elseif not started and menu:touched(touch) == true then start()
    elseif (touch.state == BEGAN) and touch.x < 50 and touch.y <24 then close() 
    end
end
function keyboard(key) 
    if(menu.loadpoint and _G[menu.loadpoint] and allloaded) then
        _G[menu.loadpoint]:keyboard(key) 
    end 
end
function start() 
    started = true
    if menu.loadpoint then count = 0; 
        for i,v in pairs(menu.selecteditem.urllist) do http.get(v,success,failed) end 
    elseif menu.selecteditem then http.get(menu.selecteditem.url,success,failed)
    else http.get(programlisturl,success,failed) end
end
function success(data,status,headers)
    if(status == 200) then
        if menu.loadpoint then
            count = count + 1
            tablename = string.match(data,"--%s*([^-]+)%s*--")
            print(tablename.." downloaded")
            local  j,data2 = 0,""
            for i=1,data:len() do char = data:sub(i,i);
                if char:byte() ~= 13 and char:byte() ~= 194 and char:byte() ~= 160 then 
                    j = j + 1; data2 = data2..char; end
                end
                _G[tablename] = loadstring(data2)()
                print("Table "..tablename..","..data2:len().."byte was loaded")
            if #menu.selecteditem.urllist == count then message = nil
                print("all finished"); _G[menu.loadpoint]:init(); allloaded = true; end;
        elseif menu.selecteditem then
            crlf = string.char(13)..string.char(10)
            menu.selecteditem.urllist = split(data:sub(data:find(crlf)+2),crlf)
            programname,menu.loadpoint = data:match("([^,]+),([^,"..crlf.."]+)"..crlf)
            print("Program Name:"..programname..", Main table = "..menu.loadpoint)
            print("table urls:")
            for i,v in ipairs(menu.selecteditem.urllist) do print(v) end
            started = nil
            message = nil
        else
            crlf = string.char(13)..string.char(10)
            applilist = split(data,crlf)
            for i,v in ipairs(applilist) do
                appliname,url = v:match("([^,]+),([^,]+)")
                menu:addItem(appliname,url)
            end
            started = nil
            message = nil
        end
    else print(status..' : '..data) end
end
function failed(error) message = error; started = nil end
function split(str, delim)
    -- Eliminate bad cases...
    if string.find(str, delim) == nil then return { str } end

    local result,pat,lastpos = {},"(.-)" .. delim .. "()",nil
    for part, pos in string.gfind(str, pat) do table.insert(result, part); lastPos = pos; end
    table.insert(result, string.sub(str, lastPos))
    return result
end

strLoadingRing = 
[[
--LoadingRing--
return function()
return {
    w = 100,
    h = 100,
    r = 0,
draw = function(self,x,y)
    -- Codea does not automatically call this method
    pushStyle()
    stroke(185, 96, 163, 255)
    fill(0, 0, 0, 0)
    strokeWidth(10)
    ellipse(x,y,self.w,self.h)
    local w = math.sin(self.r/180*math.pi)*self.w/3
    local h = math.cos(self.r/180*math.pi)*self.h/3
    line(x,y,x+w,y+h)
    ellipseMode()
    popStyle()
    self.r = self.r + 10
    if(self.r >= 360) then self.r = 0 end
end
};
end
]]
LoadingRing = loadstring(strLoadingRing)()

strProgramMenu = 
[[
--ProgramMenu--
return function()
return {
    items = nil,
    w = 0,
    selectedurl = nil,
    loadpoint = nil,
addItem = function(this,name,url)
    if not this.items then this.items = {} end
    item = ProgramMenuItem(name,url)
    table.insert(this.items,item)
    w = textSize(name) + 100
    if this.w < w then this.w = w end
end,
draw = function(this)
    -- Codea does not automatically call this method
    strokeWidth(5)
    textMode(CENTER)
    pushStyle()
    fontSize(30)
    rectMode(CORNERS)
    font("Arial-ItalicMT")
    if not this.items or (this.items and table.maxn(this.items) == 0 ) then
        local str = "tap to load list"
        w,h = textSize(str)
        w = 400
        h = 100
        stroke(168, 27, 27, 101)
        fill(192, 182, 168, 255)
        rect(WIDTH/2-w/2, HEIGHT/2-h/2,WIDTH/2+w/2,HEIGHT/2+h/2)
        fill(0, 0, 0, 255)
        text(str,WIDTH/2,HEIGHT/2)
    else
        _,h = textSize("1")
        h = h + 40
        stroke(168, 27, 27, 101)
        for i,v in ipairs(this.items) do
            if v == this.selecteditem then 
                if this.loadpoint then fill(215, 202, 61, 255)
                else fill(155, 178, 128, 255) end
            else fill(187, 128, 99, 255) end
            rect(WIDTH/2-this.w/2-50, HEIGHT/2-h/2-h*(i-1)+(table.maxn(this.items)-1)*h/2,
                 WIDTH/2+this.w/2+50, HEIGHT/2+h/2-h*(i-1)+(table.maxn(this.items)-1)*h/2)
            fill(0, 0, 0, 255)
            text(v.name,WIDTH/2,HEIGHT/2-h*(i-1)+(table.maxn(this.items)-1)*h/2)
        end
    end
    popStyle()
end,
touched = function(this,touch)
    if not this.items then
        if touch.x >= WIDTH/2-200 and touch.x <= WIDTH/2+200 then
            if touch.y >= HEIGHT/2-50 and touch.y <= HEIGHT/2+h/2+50 then
                return true
            end
        else
            return false
        end
    else
        for i,v in ipairs(this.items) do
            if touch.x >= WIDTH/2-this.w/2-50 and touch.x <= WIDTH/2+this.w/2+50 then
                if touch.y >= HEIGHT/2-h/2-h*(i-1)+(table.maxn(this.items)-1)*h/2 and
                    touch.y <= HEIGHT/2+h/2-h*(i-1)+(table.maxn(this.items)-1)*h/2 then
                    --print(v.name.." clicked")
                    if not this.selecteditem or this.selecteditem ~= v then this.loadpoint = nil end
                    this.selecteditem = v
                    return true
                end
            end
        end
        return false
    end
end,
};
end
]]
ProgramMenu = loadstring(strProgramMenu)()

strProgramMenuItem = 
[[
--ProgramMenuItem--
return function(s,url)
    if not s then s = "no title" end
return {
    name = s,
    url = url,
    urllist = nil,
};
end
]]

ProgramMenuItem = loadstring(strProgramMenuItem)()