Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local verNumber = "1.265"
- if not term.isColor() then
- print("Sorry, there is no support for normal\ncomputers yet. Please get an advanced computer\nor become REALLY patient :D\nI AM working on it though...")
- end
- function setDLPath(path)
- local f = io.open(".AppStore/dlpath", 'w')
- f:write(path)
- f:close()
- dlPath = path
- end
- function firstrun()
- setDLPath("apps")
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- print("This seems to be the first time\nyou've used the Appstore.")
- print("I need your CC forums name to begin.")
- write("Computercraft forums name: ")
- name=read()
- textutils.slowPrint("Thank you, "..name.."!")
- http.post(
- "http://zacpierson.com/computercraft/message.php?message="..textutils.urlEncode("firsttimer: "..name)
- )
- sleep(2)
- local uname = io.open(".AppStore/username", 'w')
- uname:write(name)
- uname:close()
- end
- function update()
- local serverVer = http.get("https://dl.dropbox.com/u/133665124/computercraft/appstore-install/ver.txt")
- local newver = serverVer.readAll()
- serverVer.close()
- if not(verNumber == newver) then
- print("Updating...")
- sleep(1)
- local download = http.get("https://dl.dropbox.com/u/133665124/computercraft/appstore-install/installAppStore")
- local udate = io.open(".AppStore/updater", 'w')
- udate:write(download.readAll())
- udate:close()
- shell.run(".AppStore/updater", "silent")
- error()
- end
- end
- update()
- if not(fs.exists(".AppStore/username")) then firstrun() end
- function getLines ( xfile ) -- Quickly get a table of file lines!
- local tmpfile = io.open(xfile, "r")
- local slines = {}
- local currentTxt = tmpfile.read()
- while currentTxt do
- slines[#slines+1] = currentTxt
- currentTxt = tmpfile.read()
- end
- tmpfile:close()
- return slines
- end
- local dlPath = getLines(".AppStore/dlpath")[1]
- if(dlPath==nil) then
- setDLPath("apps")
- end
- local appbg = paintutils.loadImage(".AppStore/img/appBox.nfp")
- local bg = paintutils.loadImage(".AppStore/img/mainFrame.nfp")
- local installbtn = paintutils.loadImage(".AppStore/img/install.nfp")
- local descbtn = paintutils.loadImage(".AppStore/img/description.nfp")
- local backbtn = paintutils.loadImage(".AppStore/img/back.nfp")
- local status = "Init..."
- local detailsMenu = false
- local descrMenu = false
- local detailsMenu_id = 0
- function createDirs(path)
- path = string.gsub(path, "\\", "/")
- pieces = split(path,"/")
- tempPath = ""
- textutils.slowPrint("Creating dirs for "..path..".")
- for i=1,#pieces,1 do
- if tempPath then
- tempPath = tempPath.."/"..pieces[i]
- else
- tempPath=pieces[i]
- end
- if(fs.exists(tempPath)) then
- if not(fs.isDir(tempPath)) then
- fs.delete(tempPath)
- fs.makeDir(tempPath)
- end
- else
- fs.makeDir(tempPath)
- end
- end
- end
- function canBeDir(path)
- path = string.gsub(path, "\\", "/")
- pieces = split(path,"/")
- tempPath = ""
- for i=1,#pieces,1 do
- if tempPath then
- tempPath = tempPath.."/"..pieces[i]
- else
- tempPath=pieces[i]
- end
- if(fs.exists(tempPath)) then
- if not(fs.isDir(tempPath)) then
- return false, tempPath
- end
- end
- end
- return true
- end
- function DLPath_conf(path)
- noFiles, theFile = canBeDir(path)
- if not noFiles then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.red)
- term.setCursorPos(1,1)
- term.clear()
- print("There is a file named: "..theFile..".")
- term.setTextColor(colors.white)
- write("Delete it?\n(Y/N): ")
- inp = read()
- if(inp=="y" or inp=="Y") then
- createDirs(path)
- return true
- else
- print("Please choose a new download path.")
- return false
- end
- end
- return true
- end
- function changeDLPath()
- print("Change download path.")
- print("Current path:"..dlPath)
- write("New path: ")
- local newPath=read()
- if not newPath then
- return changeDLPath()
- end
- if not DLPath_conf(newPath) then
- return changeDLPath()
- end
- createDirs(newPath)
- slPath = newPath
- setDLPath(newPath)
- dlPath = getLines(".AppStore/dlpath")[1]
- textutils.slowPrint("New path set!")
- textutils.slowPrint("Files will be downloaded to: "..dlPath)
- sleep(1)
- if(descrMenu) then
- return showDescription(downloadFromSource("programs/"..softwareList[detailsMenu_id][3].."/longDesc.txt",softwareList[detailsMenu_id][4]))
- end
- if(detailsMenu) then
- return appDetails(detailsMenu_id)
- end
- return displayItems()
- end
- function redrawStatus()
- local w,h = term.getSize()
- term.setBackgroundColor(colors.lightBlue)
- term.setTextColor(colors.black)
- term.setCursorPos((w/2)-(#status/2),h-1)
- term.clearLine()
- write(status)
- term.setCursorPos(1,h)
- write("Add app\|")
- local dldir = "\|Download Path"
- term.setCursorPos(w-#dldir,h)
- write(dldir)
- end
- function setStatus(text)
- status = text
- redrawStatus()
- end
- function draw(path,x,y)
- paintutils.drawImage(path,x,y)
- end
- function split(pString, pPattern)
- local Table = {}
- local fpat = "(.-)" .. pPattern
- local last_end = 1
- local s, e, cap = pString:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(Table,cap)
- end
- last_end = e+1
- s, e, cap = pString:find(fpat, last_end)
- end
- if last_end <= #pString then
- cap = pString:sub(last_end)
- table.insert(Table, cap)
- end
- return Table
- end
- function getSources()
- local readFile = io.open(".AppStore/softwareSources", "r")
- local slines = {}
- local currentTxt = readFile:read()
- while currentTxt do
- if not(currentTxt:sub(1,2) == "--") then
- slines[#slines+1] = currentTxt
- end
- currentTxt = readFile.read()
- end
- setStatus("Sources loaded!")
- readFile:close()
- return slines
- end
- function addApp()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(1,1)
- term.clear()
- print("Suggest a program")
- write("Program name: ")
- local appname=read()
- write("\nAuthor: ")
- local author=read()
- write("\nComputercraft Forums URL (Optional)\nURL:")
- local url=read()
- local usernamefile = io.open(".AppStore/username",'r')
- local suggestee = usernamefile:read()
- usernamefile:close()
- textutils.slowPrint("Thank you!\nI will consider adding this program.")
- http.post("http://zacpierson.com/computercraft/suggest.php?message="..textutils.urlEncode("Program: "..appname.." Author: "..author.." URL:"..url.." Suggestee:"..suggestee))
- if(descrMenu) then
- return showDescription(downloadFromSource("programs/"..softwareList[detailsMenu_id][3].."/longDesc.txt",softwareList[detailsMenu_id][4]))
- end
- if(detailsMenu) then
- return appDetails(detailsMenu_id)
- end
- return displayItems()
- end
- local SourceList = getSources()
- function downloadFromSource(url,id)
- local filecontents = nil
- local link=http.get(SourceList[tonumber(id)]..url)
- if not link then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.red)
- term.clear()
- term.setCursorPos(1,1)
- print("Zacpier's appstore is having trouble connecting\nto the remote server at:\n"..SourceList[tonumber(id)]..url.."\nPlease try again later.")
- error()
- end
- setStatus("DL: Source["..id.."]"..url)
- filecontents = link.readAll()
- link.close()
- return filecontents
- end
- function download(url)
- local filecontents=nil
- for i=1,#SourceList,1 do
- setStatus("DL: Source["..i.."]"..url)
- local downloadlink = http.get(SourceList[i]..url)
- if not downloadlink then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.red)
- term.clear()
- term.setCursorPos(1,1)
- print("Zacpier's appstore is having trouble connecting\nto the remote server at:\n"..SourceList[i]..url.."\nPlease try again later.")
- error()
- end
- local output = downloadlink.readAll()
- local addition_array = split(output,"\|")
- local addition = ""
- for j=1,#addition_array,1 do
- if addition then
- addition = addition..addition_array[j]..";"..i.."\|"
- else
- addition = addition_array[j]..";"..i.."\|"
- end
- end
- if filecontents then
- filecontents = filecontents..addition
- else
- filecontents = addition
- end
- downloadlink:close()
- end
- return filecontents
- end
- softwareList = {}
- descList = {}
- function splash()
- local w,h = term.getSize()
- term.setBackgroundColor(colors.white)
- term.clear()
- drawGUI()
- --draw(bg,0,0)
- --term.setBackgroundColor(colors.white)
- --term.setTextColor(colors.black)
- local text1 = "Please wait... Gathering software..."
- local text2 = "Starting software_source DL..."
- --term.setCursorPos((w/2)-(#text1/2), h)
- --write(text1)
- setStatus(text1)
- sleep(1)
- setStatus(text2)
- --term.setCursorPos((w/2)-(#text2/2),h)
- --term.clearLine()
- --write(text2)
- local fileList = download("programlist.txt")
- local halfDoneFileList = split(fileList,"|")
- for i=1,#halfDoneFileList,1 do
- softwareList[#softwareList+1] = split(halfDoneFileList[i],";")
- local descr = downloadFromSource("programs/"..softwareList[i][3].."/shortDesc.txt",softwareList[i][4])
- descList[#softwareList] = descr
- end
- end
- function titleBar()
- term.setBackgroundColor(colors.lightBlue)
- term.setTextColor(colors.white)
- w,h = term.getSize()
- term.setCursorPos(1,1)
- term.clearLine()
- write("App Store by Zacpier - version "..verNumber)
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.black)
- term.setCursorPos(w-1,1)
- write(" x")
- end
- function drawGUI()
- draw(bg,1,1)
- redrawStatus()
- titleBar()
- end
- itemScrollOffset = 0
- itemListH = 0
- function displayItems()
- local currentY = itemScrollOffset+4
- local w,DONTUSEME_DERP = term.getSize()
- term.setBackgroundColor(colors.white)
- term.clear()
- for i=1,#softwareList,1 do
- draw(appbg,4,currentY)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(10,currentY+1)
- write(softwareList[i][2])
- term.setCursorPos(w-7-(#string.gsub(softwareList[i][1], "\n", "")),currentY+1)
- write(string.gsub(softwareList[i][1], "\n", ""))
- local tempDesc = split(descList[i],"\n")
- term.setCursorPos(10,currentY+2)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- write(tempDesc[1])
- term.setCursorPos(10,currentY+3)
- write(tempDesc[2])
- currentY = currentY + 6
- end
- itemListH = currentY - itemScrollOffset -18
- drawGUI()
- end
- function handleClickDescr(btn,x,y)
- if(btn==1 and x > 2 and x < 10 and y>1 and y<7) then
- descrMenu = false
- return appDetails(detailsMenu_id)
- end
- end
- function showDescription(desc)
- descrMenu = true
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.clear()
- drawGUI()
- draw(backbtn,2,2)
- lines = split(desc,"\n")
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- for i=1,#lines,1 do
- term.setCursorPos(4,6+i)
- write(lines[i])
- end
- end
- function handleClickDetails(btn,x,y)
- if(btn==1 and x > 2 and x < 10 and y>1 and y<7) then
- descrMenu = false
- detailsMenu = false
- print(#softwareList)
- setStatus("Done - "..#softwareList.." programs loaded!")
- return displayItems()
- end
- if(btn==1 and x > 3 and x < 11 and y>13 and y<18) then
- local app = downloadFromSource("programs/"..softwareList[detailsMenu_id][3].."/app.txt",softwareList[detailsMenu_id][4])
- --if not fs.exists(dlPath) then
- -- fs.makeDir(dlPath)
- --end
- --print(dlPath)
- --print(softwareList[detailsMenu_id][3])
- --print(dlPath.."/"..softwareList[detailsMenu_id][3])
- if(fs.isDir(dlPath .. "/" .. softwareList[detailsMenu_id][3])) then
- fs.delete(dlPath .. "/" .. softwareList[detailsMenu_id][3])
- end
- local out = io.open(dlPath .. "/" .. softwareList[detailsMenu_id][3], "w")
- out:write(app)
- out:close()
- setStatus("Downloaded "..softwareList[detailsMenu_id][2].." to "..dlPath.."/"..softwareList[detailsMenu_id][3].."!")
- end
- if(btn==1 and x > 41 and x < 49 and y>13 and y<18) then
- showDescription(downloadFromSource("programs/"..softwareList[detailsMenu_id][3].."/longDesc.txt",softwareList[detailsMenu_id][4]))
- end
- end
- function appDetails(id)
- detailsMenu_id = id
- term.setBackgroundColor(colors.white)
- term.clear()
- local w,h = term.getSize()
- term.setTextColor(colors.black)
- term.setCursorPos((w/2)-(#softwareList[id][2]/2),3)
- write(softwareList[id][2])
- local author = "By "..string.gsub(softwareList[id][1], "\n", "")
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos((w/2)-(#author/2),4)
- term.clearLine()
- write(author)
- drawGUI()
- draw(backbtn,2,2)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- setStatus("Viewing details for: "..softwareList[id][2])
- detailsMenu = true
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.lightGray)
- for tempy=13,17,1 do
- term.setCursorPos(4,tempy)
- write(" ")
- end
- draw(installbtn,3,13)
- draw(descbtn,41,13)
- end
- function handleClick(btn,x,y)
- local w,h = term.getSize()
- if(btn==1 and x>w-2 and y==1) then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.yellow)
- term.clear()
- term.setCursorPos(1,1)
- print("Thank you for using zacpier's appstore!")
- error()
- end
- if(btn==1 and x<9 and y==h) then
- addApp()
- end
- if(btn==1 and x>w-14 and y==h) then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(1,1)
- term.clear()
- changeDLPath()
- end
- if descrMenu then return handleClickDescr(btn,x,y) end
- if detailsMenu then return handleClickDetails(btn,x,y) end
- if not btn==1 then return end
- if (y>h-2) or (y<3) then return end
- y = y + -itemScrollOffset
- if x > 7 and x < 46 then
- for i=1, #softwareList, 1 do
- if y > (i * 6 - 3) and y < (i * 6 + 3) then
- appDetails(i)
- end
- end
- end
- end
- splash()
- setStatus("Rendering app boxes.")
- displayItems()
- setStatus("Done - "..#softwareList.." programs loaded!")
- while true do
- event, a1, a2, a3 = os.pullEvent()
- if(event == "mouse_scroll") then
- if(a1==1) then
- if(itemScrollOffset > -itemListH-1) then
- itemScrollOffset = itemScrollOffset - 2
- end
- else
- if(itemScrollOffset < -1) then
- itemScrollOffset = itemScrollOffset + 2
- end
- end
- displayItems()
- elseif(event=="mouse_click" or event=="monitor_touch") then handleClick(a1,a2,a3) end
- if event=="key" then
- if(a1==201) then
- if(itemScrollOffset < -1) then
- itemScrollOffset = itemScrollOffset + 2
- end
- elseif(a1==209) then
- if(itemScrollOffset > -itemListH-1) then
- itemScrollOffset = itemScrollOffset - 2
- end
- end
- end
- end
- term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement