Alyssa

Program Installer

Oct 22nd, 2014
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.37 KB | None | 0 0
  1. function cWrite(text)
  2.  scrx, scry = term.getSize()
  3.  xt = scrx/2
  4.  xt = xt -#text/2
  5.  x,y = term.getCursorPos()
  6.  term.setCursorPos(xt,y)
  7.  write(text)
  8. end
  9.  
  10. function hitbox(hit, object)
  11.  scrx, scry = term.getSize()
  12.  xt = scrx/2
  13.  xt2 = xt +#object/2
  14.  xt = xt -#object/2
  15.  if hit >= xt and hit <= xt2 then
  16.   return true
  17.  else
  18.   return false
  19.  end
  20. end
  21.  
  22. function rWrite(text)
  23.  scrx, scry = term.getSize()
  24.  xt = scrx -#text
  25.  x,y = term.getCursorPos()
  26.  term.setCursorPos(xt,y)
  27.  write(text)
  28. end
  29.  
  30. function getList()
  31.  list = http.get( "http://galatical.com/list" )
  32.  
  33.  if list then
  34.   local tLines = {}
  35.   local sLine = list.readLine()
  36.   while sLine do
  37.    table.insert(tLines, sLine)
  38.    sLine = list.readLine()
  39.   end
  40.   list.close()
  41.   return tLines
  42.  else
  43.   print("Failed to get programs list")
  44.  end
  45. end
  46.  
  47. function split(line)
  48.  continue = 1
  49.  counter = 0
  50.  while continue == 1 do
  51.   counter = counter+1
  52.   sleep(0)
  53.   if string.sub(line, counter, counter+1) == "||" then
  54.    prgrm = string.sub(line, 1, counter-2)
  55.    ver = string.sub(line, counter+3, #line)
  56.    continue = 0
  57.   end
  58.  end
  59.  if not prgrm then
  60.   return nil
  61.  end
  62.  if not ver then
  63.   return nil
  64.  end
  65.  return prgrm , ver
  66. end
  67.  
  68. pglist = {}
  69. verlist = {}
  70. programs = getList()
  71. --print("Got data")
  72. for i  = 1, #programs do
  73.  --print("Splitting line "..i.."/"..#programs)
  74.  pglist[i], verlist[i] = split(programs[i])
  75.  if pglist[i] == nil or verlist[i] == nil then
  76.   table.remove(pglist[i])
  77.   table.remove(pglist[i])
  78.  end
  79. end
  80. --[[for b = 1, #pglist do
  81.  print(pglist[b].." version "..verlist[b])
  82. end]]
  83.  
  84. term.setBackgroundColor(colors.white)
  85. term.clear()
  86. term.setTextColor(colors.black)
  87. term.setCursorPos(1,1)
  88. cWrite("LPI")
  89. for c = 1, #pglist do
  90.  term.setCursorPos(1,c+1)
  91.  cWrite(pglist[c])
  92.  rWrite("[v"..verlist[c].."]")
  93. end
  94. start = true
  95. while start do
  96.  e,t,cx,cy = os.pullEvent("mouse_click")
  97.  if pglist[cy-1] then
  98.   if hitbox(cx, pglist[cy-1]) then
  99.    response = http.get( "http://galatical.com/"..pglist[cy-1])
  100.    if response then
  101.     sR = response.readAll()
  102.     response.close()
  103.     if fs.exists(pglist[cy-1]) then
  104.      fs.delete(pglist[cy-1])
  105.     end
  106.     file = fs.open(pglist[cy-1],"w")
  107.     file.write( sR )
  108.     file.close()
  109.     scrx, scry = term.getSize()
  110.     term.setCursorPos(1, scry)
  111.     write("                                                  ")
  112.     term.setCursorPos(1, scry)
  113.     cWrite("Downloaded "..pglist[cy-1])
  114.    else
  115.     scrx, scry = term.getSize()
  116.     term.setCursorPos(1, scry)
  117.     write("                                                  ")
  118.     term.setCursorPos(1, scry)
  119.     cWrite("Failed.")
  120.    end
  121.   end
  122.  end
  123.  if cx == 1 and cy == scry then
  124.   term.setCursorPos(1, scry-1)
  125.   id = read()
  126.   term.setCursorPos(1, scry-1)
  127.   write("                    ")
  128.   response = http.get( "http://galatical.com/"..id)
  129.   if response then
  130.    sR = response.readAll()
  131.    response.close()
  132.    if fs.exists(id) then
  133.     fs.delete(id)
  134.    end
  135.    file = fs.open(id,"w")
  136.    file.write( sR )
  137.    file.close()
  138.    scrx, scry = term.getSize()
  139.    term.setCursorPos(1, scry)
  140.    write("                                                  ")
  141.    term.setCursorPos(1, scry)
  142.    cWrite("Downloaded "..id)
  143.   else
  144.    scrx, scry = term.getSize()
  145.    term.setCursorPos(1, scry)
  146.    write("                                                  ")
  147.    term.setCursorPos(1, scry)
  148.    cWrite("Failed.")
  149.   end
  150.  end
  151. end
Advertisement
Add Comment
Please, Sign In to add comment