Advertisement
Imgoodisher

Googol Installer

Feb 24th, 2013
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.87 KB | None | 0 0
  1. local runningApp = "search"
  2.  
  3. local alreadyUpdated = {}
  4.  
  5. local pinwheel = {[0] = "|", "/", "-", "\\"}
  6. local pinwheel_index = 0
  7. function checkForUpdates(app, path, onlyIfAbsent)
  8.     local w, h = term.getSize()
  9.     if not alreadyUpdated[app] then
  10.         if fs.exists(path) and onlyIfAbsent then
  11.             return false
  12.         end
  13.         term.setBackgroundColor(colors.white)
  14.         term.setTextColor(colors.blue)
  15.         term.clear()
  16.         http.request("http://www.googol.comli.com/data/"..app)
  17.         pinwheel_timer = os.startTimer(0.1)
  18.         while true do
  19.             local event = { os.pullEvent() }
  20.             if event[1] == "http_success" then
  21.                 newdata = event[3]:readAll()
  22.                 break
  23.             elseif event[1] == "http_failure" then
  24.                 error("Error updating "..app, 0)
  25.             elseif event[1] == "timer" and event[2] == pinwheel_timer then
  26.                 str = "Updating "..app
  27.                 term.setCursorPos(w/2-str:len()/2, h/2-1)
  28.                 print(str)
  29.                 term.setCursorPos(w/2, h/2+1)
  30.                 print(pinwheel[pinwheel_index])
  31.                 pinwheel_index = (pinwheel_index + 1) % (#pinwheel+1)
  32.                 pinwheel_timer = os.startTimer(0.1)
  33.             end
  34.         end
  35.        
  36.         local file = io.open(path or "/.googol/apps/"..app, "r")
  37.         local currentdata
  38.         if file then
  39.             currentdata = file:read("*a")
  40.             file:close()
  41.         else
  42.             currentdata = ""
  43.         end
  44.    
  45.         if newdata ~= currentdata then
  46.             local file = io.open(path or "/.googol/apps/"..app, "w")
  47.             file:write(newdata)
  48.             file:close()
  49.            
  50.             alreadyUpdated[app] = true
  51.             return true
  52.         end
  53.        
  54.         alreadyUpdated[app] = true
  55.         return false
  56.     end
  57. end
  58.  
  59. if not fs.exists("/.googol") then fs.makeDir("/.googol") end
  60. if not fs.exists("/.googol/media") then fs.makeDir("/.googol/media") end
  61. if not fs.exists("/.googol/apps") then fs.makeDir("/.googol/apps") end
  62. local updated = checkForUpdates("googol", shell.getRunningProgram())
  63. checkForUpdates("api.lua", "/.googol/api.lua")
  64. checkForUpdates("LibCompress.lua", "/.googol/LibCompress.lua", true)
  65. checkForUpdates("logo.nfp", "/.googol/media/logo.nfp", true)
  66. checkForUpdates("gamelogo.nfp", "/.googol/media/gamelogo.nfp", true)
  67.  
  68. if updated then
  69.     local w, h = term.getSize()
  70.     local str = "Googol has been updated"
  71.     term.clear()
  72.     term.setCursorPos(w/2-str:len()/2, h/2)
  73.     term.setTextColor(colors.blue)
  74.     print(str)
  75.     sleep(3)
  76.    
  77.     shell.run(shell.getRunningProgram())
  78.    
  79.     error("", 0)
  80. end
  81.  
  82. shell.run("/.googol/api.lua")
  83.  
  84. while true do
  85.     checkForUpdates(runningApp, "/.googol/apps/"..runningApp)
  86.    
  87.     googol.clearButtons()
  88.     shell.run("/.googol/apps/"..runningApp)
  89.     local timeout = os.startTimer(0.5)
  90.    
  91.     while true do
  92.         local event = { os.pullEvent() }
  93.         if event[1] == "GOOGOL_APP_CHANGE" then
  94.             runningApp = event[2]
  95.             break
  96.         elseif event[1] == "GOOGOL_EXIT" then
  97.             term.clear()
  98.             term.setCursorPos(1, 1)
  99.             term.setTextColor(colors.yellow)
  100.             print(os.version())
  101.             error("", 0)
  102.         elseif event[1] == "timer" and event[2] == timeout then
  103.             runningApp = "search"
  104.             break
  105.         end
  106.     end
  107. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement