Advertisement
Guest User

Untitled

a guest
Jan 6th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.49 KB | None | 0 0
  1. local verNumber = "1.265"
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8. if not term.isColor() then
  9.     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...")
  10. end
  11. function setDLPath(path)
  12.     local f = io.open(".AppStore/dlpath", 'w')
  13.     f:write(path)
  14.     f:close()
  15.     dlPath = path
  16. end
  17. function firstrun()
  18.     setDLPath("apps")
  19.     term.setBackgroundColor(colors.white)
  20.     term.setTextColor(colors.black)
  21.     term.clear()
  22.     term.setCursorPos(1,1)
  23.     print("This seems to be the first time\nyou've used the Appstore.")
  24.     print("I need your CC forums name to begin.")
  25.     write("Computercraft forums name: ")
  26.     name=read()
  27.     textutils.slowPrint("Thank you, "..name.."!")
  28.     http.post(
  29.         "http://zacpierson.com/computercraft/message.php?message="..textutils.urlEncode("firsttimer: "..name)
  30.     )
  31.     sleep(2)
  32.     local uname = io.open(".AppStore/username", 'w')
  33.     uname:write(name)
  34.     uname:close()
  35. end
  36. function update()
  37.     local serverVer = http.get("https://dl.dropbox.com/u/133665124/computercraft/appstore-install/ver.txt")
  38.     local newver = serverVer.readAll()
  39.     serverVer.close()
  40.     if not(verNumber == newver) then
  41.             print("Updating...")
  42.             sleep(1)
  43.             local download = http.get("https://dl.dropbox.com/u/133665124/computercraft/appstore-install/installAppStore")
  44.             local udate = io.open(".AppStore/updater", 'w')
  45.             udate:write(download.readAll())
  46.             udate:close()
  47.             shell.run(".AppStore/updater", "silent")
  48.             error()
  49.     end
  50. end
  51. update()
  52. if not(fs.exists(".AppStore/username")) then firstrun() end
  53.  
  54.  
  55. function getLines ( xfile ) -- Quickly get a table of file lines!
  56.   local tmpfile = io.open(xfile, "r")
  57.   local slines = {}
  58.   local currentTxt = tmpfile.read()
  59.  
  60.   while currentTxt do
  61.     slines[#slines+1] = currentTxt
  62.     currentTxt = tmpfile.read()
  63.   end
  64.   tmpfile:close()
  65.   return slines
  66. end
  67.  
  68.  
  69. local dlPath = getLines(".AppStore/dlpath")[1]
  70.  
  71. if(dlPath==nil) then
  72.     setDLPath("apps")
  73. end
  74.  
  75.  
  76. local appbg = paintutils.loadImage(".AppStore/img/appBox.nfp")
  77. local bg = paintutils.loadImage(".AppStore/img/mainFrame.nfp")
  78. local installbtn = paintutils.loadImage(".AppStore/img/install.nfp")
  79. local descbtn = paintutils.loadImage(".AppStore/img/description.nfp")
  80. local backbtn = paintutils.loadImage(".AppStore/img/back.nfp")
  81. local status = "Init..."
  82. local detailsMenu = false
  83. local descrMenu   = false
  84. local detailsMenu_id = 0
  85.  
  86. function createDirs(path)
  87.     path = string.gsub(path, "\\", "/")
  88.     pieces = split(path,"/")
  89.     tempPath = ""
  90.     textutils.slowPrint("Creating dirs for "..path..".")
  91.     for i=1,#pieces,1 do
  92.         if tempPath then
  93.             tempPath = tempPath.."/"..pieces[i]
  94.         else
  95.             tempPath=pieces[i]
  96.         end
  97.         if(fs.exists(tempPath)) then
  98.             if not(fs.isDir(tempPath)) then
  99.                 fs.delete(tempPath)
  100.                 fs.makeDir(tempPath)
  101.             end
  102.         else
  103.             fs.makeDir(tempPath)
  104.         end
  105.     end
  106. end
  107.  
  108. function canBeDir(path)
  109.     path = string.gsub(path, "\\", "/")
  110.     pieces = split(path,"/")
  111.     tempPath = ""
  112.     for i=1,#pieces,1 do
  113.         if tempPath then
  114.             tempPath = tempPath.."/"..pieces[i]
  115.         else
  116.             tempPath=pieces[i]
  117.         end
  118.         if(fs.exists(tempPath)) then
  119.             if not(fs.isDir(tempPath)) then
  120.                 return false, tempPath
  121.             end
  122.         end
  123.     end
  124.     return true
  125. end
  126.  
  127. function DLPath_conf(path)
  128.     noFiles, theFile = canBeDir(path)
  129.     if not noFiles then
  130.         term.setBackgroundColor(colors.black)
  131.         term.setTextColor(colors.red)
  132.         term.setCursorPos(1,1)
  133.         term.clear()
  134.         print("There is a file named: "..theFile..".")
  135.         term.setTextColor(colors.white)
  136.         write("Delete it?\n(Y/N): ")
  137.         inp = read()
  138.         if(inp=="y" or inp=="Y") then
  139.             createDirs(path)
  140.             return true
  141.         else
  142.             print("Please choose a new download path.")
  143.             return false
  144.         end
  145.     end
  146.     return true
  147. end
  148.  
  149. function changeDLPath()
  150.     print("Change download path.")
  151.     print("Current path:"..dlPath)
  152.     write("New path: ")
  153.     local newPath=read()
  154.     if not newPath then
  155.         return changeDLPath()
  156.     end
  157.     if not DLPath_conf(newPath) then
  158.         return changeDLPath()
  159.     end
  160.     createDirs(newPath)
  161.     slPath = newPath
  162.     setDLPath(newPath)
  163.     dlPath = getLines(".AppStore/dlpath")[1]
  164.     textutils.slowPrint("New path set!")
  165.     textutils.slowPrint("Files will be downloaded to: "..dlPath)
  166.     sleep(1)
  167.     if(descrMenu) then
  168.         return showDescription(downloadFromSource("programs/"..softwareList[detailsMenu_id][3].."/longDesc.txt",softwareList[detailsMenu_id][4]))
  169.     end
  170.     if(detailsMenu) then
  171.         return appDetails(detailsMenu_id)
  172.     end
  173.     return displayItems()
  174. end
  175. function redrawStatus()
  176.   local w,h = term.getSize()
  177.   term.setBackgroundColor(colors.lightBlue)
  178.   term.setTextColor(colors.black)
  179.   term.setCursorPos((w/2)-(#status/2),h-1)
  180.   term.clearLine()
  181.   write(status)
  182.     term.setCursorPos(1,h)
  183.     write("Add app\|")
  184.     local dldir = "\|Download Path"
  185.     term.setCursorPos(w-#dldir,h)
  186.     write(dldir)
  187. end
  188.  
  189. function setStatus(text)
  190.   status = text
  191.     redrawStatus()
  192. end
  193.  
  194.  
  195. function draw(path,x,y)
  196.   paintutils.drawImage(path,x,y)
  197. end
  198.  
  199.  
  200. function split(pString, pPattern)
  201.    local Table = {}
  202.    local fpat = "(.-)" .. pPattern
  203.    local last_end = 1
  204.    local s, e, cap = pString:find(fpat, 1)
  205.    while s do
  206.       if s ~= 1 or cap ~= "" then
  207.      table.insert(Table,cap)
  208.       end
  209.       last_end = e+1
  210.       s, e, cap = pString:find(fpat, last_end)
  211.    end
  212.    if last_end <= #pString then
  213.       cap = pString:sub(last_end)
  214.       table.insert(Table, cap)
  215.    end
  216.    return Table
  217. end
  218.  
  219. function getSources()
  220.   local readFile = io.open(".AppStore/softwareSources", "r")
  221.   local slines = {}
  222.   local currentTxt = readFile:read()
  223.  
  224.   while currentTxt do
  225.     if not(currentTxt:sub(1,2) == "--") then
  226.       slines[#slines+1] = currentTxt
  227.     end
  228.     currentTxt = readFile.read()
  229.   end
  230.   setStatus("Sources loaded!")
  231.   readFile:close()
  232.   return slines
  233. end
  234.  
  235. function addApp()
  236. term.setBackgroundColor(colors.black)
  237. term.setTextColor(colors.white)
  238. term.setCursorPos(1,1)
  239. term.clear()
  240. print("Suggest a program")
  241. write("Program name: ")
  242. local appname=read()
  243. write("\nAuthor: ")
  244. local author=read()
  245. write("\nComputercraft Forums URL (Optional)\nURL:")
  246. local url=read()
  247. local usernamefile = io.open(".AppStore/username",'r')
  248. local suggestee = usernamefile:read()
  249. usernamefile:close()
  250. textutils.slowPrint("Thank you!\nI will consider adding this program.")
  251. http.post("http://zacpierson.com/computercraft/suggest.php?message="..textutils.urlEncode("Program: "..appname.."   Author: "..author.."   URL:"..url.."   Suggestee:"..suggestee))
  252. if(descrMenu) then
  253.     return showDescription(downloadFromSource("programs/"..softwareList[detailsMenu_id][3].."/longDesc.txt",softwareList[detailsMenu_id][4]))
  254. end
  255. if(detailsMenu) then
  256.     return appDetails(detailsMenu_id)
  257. end
  258. return displayItems()
  259. end
  260.  
  261.  
  262.  
  263.  
  264.  
  265. local SourceList = getSources()
  266.  
  267. function downloadFromSource(url,id)
  268.   local filecontents = nil
  269.   local link=http.get(SourceList[tonumber(id)]..url)
  270.   if not link then
  271.     term.setBackgroundColor(colors.black)
  272.     term.setTextColor(colors.red)
  273.     term.clear()
  274.     term.setCursorPos(1,1)
  275.     print("Zacpier's appstore is having trouble connecting\nto the remote server at:\n"..SourceList[tonumber(id)]..url.."\nPlease try again later.")
  276.     error()
  277. end
  278.   setStatus("DL: Source["..id.."]"..url)
  279.   filecontents = link.readAll()
  280.   link.close()
  281.   return filecontents
  282. end
  283.  
  284. function download(url)
  285.   local filecontents=nil
  286.   for i=1,#SourceList,1 do
  287.     setStatus("DL: Source["..i.."]"..url)
  288.     local downloadlink = http.get(SourceList[i]..url)
  289.     if not downloadlink then
  290.         term.setBackgroundColor(colors.black)
  291.         term.setTextColor(colors.red)
  292.         term.clear()
  293.         term.setCursorPos(1,1)
  294.         print("Zacpier's appstore is having trouble connecting\nto the remote server at:\n"..SourceList[i]..url.."\nPlease try again later.")
  295.         error()
  296.     end
  297.     local output = downloadlink.readAll()
  298.     local addition_array = split(output,"\|")
  299.     local addition = ""
  300.     for j=1,#addition_array,1 do
  301.       if addition then
  302.         addition = addition..addition_array[j]..";"..i.."\|"
  303.       else
  304.         addition = addition_array[j]..";"..i.."\|"
  305.       end
  306.     end
  307.    
  308.     if filecontents then
  309.       filecontents = filecontents..addition
  310.     else
  311.       filecontents = addition
  312.     end
  313.     downloadlink:close()
  314.   end
  315.   return filecontents
  316. end
  317.  
  318. softwareList = {}
  319. descList = {}
  320. function splash()
  321.   local w,h = term.getSize()
  322.   term.setBackgroundColor(colors.white)
  323.   term.clear()
  324.   drawGUI()
  325.   --draw(bg,0,0)
  326.   --term.setBackgroundColor(colors.white)
  327.   --term.setTextColor(colors.black)
  328.   local text1 = "Please wait... Gathering software..."
  329.   local text2 = "Starting software_source DL..."
  330.   --term.setCursorPos((w/2)-(#text1/2), h)
  331.   --write(text1)
  332.   setStatus(text1)
  333.   sleep(1)
  334.   setStatus(text2)
  335.   --term.setCursorPos((w/2)-(#text2/2),h)
  336.   --term.clearLine()
  337.   --write(text2)
  338.   local fileList = download("programlist.txt")
  339.   local halfDoneFileList = split(fileList,"|")
  340.   for i=1,#halfDoneFileList,1 do
  341.     softwareList[#softwareList+1] = split(halfDoneFileList[i],";")
  342.     local descr = downloadFromSource("programs/"..softwareList[i][3].."/shortDesc.txt",softwareList[i][4])
  343.     descList[#softwareList] = descr
  344.   end
  345. end
  346.  
  347. function titleBar()
  348. term.setBackgroundColor(colors.lightBlue)
  349. term.setTextColor(colors.white)
  350. w,h = term.getSize()
  351. term.setCursorPos(1,1)
  352. term.clearLine()
  353. write("App Store by Zacpier - version "..verNumber)
  354. term.setBackgroundColor(colors.red)
  355. term.setTextColor(colors.black)
  356. term.setCursorPos(w-1,1)
  357. write(" x")
  358. end
  359.  
  360. function drawGUI()
  361. draw(bg,1,1)
  362. redrawStatus()
  363. titleBar()
  364. end
  365.  
  366.  
  367. itemScrollOffset = 0
  368. itemListH = 0
  369. function displayItems()
  370.   local currentY = itemScrollOffset+4
  371.   local w,DONTUSEME_DERP = term.getSize()
  372.   term.setBackgroundColor(colors.white)
  373.   term.clear()
  374.   for i=1,#softwareList,1 do
  375.     draw(appbg,4,currentY)
  376.     term.setBackgroundColor(colors.black)
  377.     term.setTextColor(colors.white)
  378.     term.setCursorPos(10,currentY+1)
  379.     write(softwareList[i][2])
  380.     term.setCursorPos(w-7-(#string.gsub(softwareList[i][1], "\n", "")),currentY+1)
  381.     write(string.gsub(softwareList[i][1], "\n", ""))
  382.     local tempDesc = split(descList[i],"\n")
  383.     term.setCursorPos(10,currentY+2)
  384.     term.setBackgroundColor(colors.white)
  385.     term.setTextColor(colors.black)
  386.     write(tempDesc[1])
  387.     term.setCursorPos(10,currentY+3)
  388.     write(tempDesc[2])
  389.     currentY = currentY + 6
  390.   end
  391.   itemListH = currentY - itemScrollOffset -18
  392.   drawGUI()
  393. end
  394.  
  395. function handleClickDescr(btn,x,y)
  396.  
  397.     if(btn==1 and x > 2 and x < 10 and y>1 and y<7) then
  398.         descrMenu = false
  399.         return appDetails(detailsMenu_id)
  400.     end
  401. end
  402.  
  403. function showDescription(desc)
  404.     descrMenu = true
  405.     term.setBackgroundColor(colors.white)
  406.     term.setTextColor(colors.black)
  407.     term.clear()
  408.     drawGUI()
  409.     draw(backbtn,2,2)
  410.     lines = split(desc,"\n")
  411.     term.setBackgroundColor(colors.white)
  412.     term.setTextColor(colors.black)
  413.     for i=1,#lines,1 do
  414.         term.setCursorPos(4,6+i)
  415.         write(lines[i])
  416.     end
  417. end
  418.  
  419. function handleClickDetails(btn,x,y)
  420.     if(btn==1 and x > 2 and x < 10 and y>1 and y<7) then
  421.         descrMenu = false
  422.         detailsMenu = false
  423.         print(#softwareList)
  424.         setStatus("Done - "..#softwareList.." programs loaded!")
  425.         return displayItems()
  426.     end
  427.     if(btn==1 and x > 3 and x < 11 and y>13 and y<18) then
  428.         local app = downloadFromSource("programs/"..softwareList[detailsMenu_id][3].."/app.txt",softwareList[detailsMenu_id][4])
  429.         --if not fs.exists(dlPath) then
  430.         --  fs.makeDir(dlPath)
  431.         --end
  432.         --print(dlPath)
  433.         --print(softwareList[detailsMenu_id][3])
  434.         --print(dlPath.."/"..softwareList[detailsMenu_id][3])
  435.         if(fs.isDir(dlPath .. "/" .. softwareList[detailsMenu_id][3])) then
  436.             fs.delete(dlPath .. "/" .. softwareList[detailsMenu_id][3])
  437.         end
  438.         local out = io.open(dlPath .. "/" .. softwareList[detailsMenu_id][3], "w")
  439.         out:write(app)
  440.         out:close()
  441.         setStatus("Downloaded "..softwareList[detailsMenu_id][2].." to "..dlPath.."/"..softwareList[detailsMenu_id][3].."!")
  442.     end
  443.     if(btn==1 and x > 41 and x < 49 and y>13 and y<18) then
  444.         showDescription(downloadFromSource("programs/"..softwareList[detailsMenu_id][3].."/longDesc.txt",softwareList[detailsMenu_id][4]))
  445.     end
  446. end
  447.  
  448. function appDetails(id)
  449.     detailsMenu_id = id
  450.     term.setBackgroundColor(colors.white)
  451.     term.clear()
  452.     local w,h = term.getSize()
  453.  
  454.     term.setTextColor(colors.black)
  455.     term.setCursorPos((w/2)-(#softwareList[id][2]/2),3)
  456.     write(softwareList[id][2])
  457.     local author = "By "..string.gsub(softwareList[id][1], "\n", "")
  458.     term.setBackgroundColor(colors.white)
  459.     term.setTextColor(colors.black)
  460.     term.setCursorPos((w/2)-(#author/2),4)
  461.     term.clearLine()
  462.     write(author)
  463.     drawGUI()
  464.     draw(backbtn,2,2)
  465.     term.setBackgroundColor(colors.white)
  466.     term.setTextColor(colors.black)
  467.     setStatus("Viewing details for: "..softwareList[id][2])
  468.     detailsMenu = true
  469.     term.setBackgroundColor(colors.gray)
  470.     term.setTextColor(colors.lightGray)
  471.     for tempy=13,17,1 do
  472.         term.setCursorPos(4,tempy)
  473.         write("                                             ")
  474.     end
  475.     draw(installbtn,3,13)
  476.     draw(descbtn,41,13)
  477.  
  478. end
  479.  
  480. function handleClick(btn,x,y)
  481. local w,h = term.getSize()
  482. if(btn==1 and x>w-2 and y==1) then
  483.     term.setBackgroundColor(colors.black)
  484.     term.setTextColor(colors.yellow)
  485.     term.clear()
  486.     term.setCursorPos(1,1)
  487.     print("Thank you for using zacpier's appstore!")
  488.     error()
  489. end
  490.  
  491. if(btn==1 and x<9 and y==h) then
  492.     addApp()
  493. end
  494.  
  495. if(btn==1 and x>w-14 and y==h) then
  496.     term.setBackgroundColor(colors.black)
  497.     term.setTextColor(colors.white)
  498.     term.setCursorPos(1,1)
  499.     term.clear()
  500.     changeDLPath()
  501. end
  502.  
  503. if descrMenu then return handleClickDescr(btn,x,y) end
  504. if detailsMenu then return handleClickDetails(btn,x,y) end
  505. if not btn==1 then return end
  506. if (y>h-2) or (y<3) then return end
  507.  
  508.     y = y + -itemScrollOffset
  509.     if x > 7 and x < 46 then
  510.         for i=1, #softwareList, 1 do
  511.             if y > (i * 6 - 3) and y < (i * 6 + 3) then
  512.                 appDetails(i)
  513.             end
  514.         end
  515.     end
  516.  
  517. end
  518.  
  519.  
  520.  
  521.  
  522.  
  523. splash()
  524. setStatus("Rendering app boxes.")
  525. displayItems()
  526. setStatus("Done - "..#softwareList.." programs loaded!")
  527. while true do
  528.   event, a1, a2, a3 = os.pullEvent()
  529.   if(event == "mouse_scroll") then
  530.     if(a1==1) then
  531.       if(itemScrollOffset > -itemListH-1) then
  532.         itemScrollOffset = itemScrollOffset - 2
  533.       end
  534.     else
  535.       if(itemScrollOffset < -1) then
  536.         itemScrollOffset = itemScrollOffset + 2
  537.       end
  538.     end
  539.     displayItems()
  540.   elseif(event=="mouse_click" or event=="monitor_touch") then handleClick(a1,a2,a3) end
  541.   if event=="key" then
  542.     if(a1==201) then
  543.         if(itemScrollOffset < -1) then
  544.             itemScrollOffset = itemScrollOffset + 2
  545.         end
  546.     elseif(a1==209) then
  547.       if(itemScrollOffset > -itemListH-1) then
  548.         itemScrollOffset = itemScrollOffset - 2
  549.       end
  550.     end
  551.     end
  552. end
  553.  
  554. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement