Advertisement
Imgoodisher

Silver Installer

Nov 6th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.95 KB | None | 0 0
  1. local auto_update = true
  2. local root_URL = "https://raw.github.com/Imgoodisher/Googol-Silver/master/"
  3. local version_URL = root_URL.."version"
  4. local silver_URL = root_URL.."silver.lua"
  5. local filelist_URL = root_URL.."filelist"
  6.  
  7. local w, h = term.getSize()
  8.  
  9. -- Installer
  10. function install(o)
  11.     setfenv(assert(loadstring(o), "Corrupt File Package/Bad HTTP Request"), setmetatable({shell=shell},{__index=_G}))(".silver")
  12. end
  13.  
  14. term.setBackgroundColor(colors.white)
  15. term.setTextColor(colors.black)
  16. term.clear()
  17. term.setCursorPos(1, 1)
  18. print("Updating Silver...")
  19. if auto_update then
  20.     local f = http.get(version_URL)
  21.     local version = f.readAll()
  22.     f.close()
  23.    
  24.     local f = io.open("/.silver/version", "r")
  25.     if (not f) or version:match("[%.%w]") ~= f:read("*a"):match("[%.%w]") then
  26.         print("Updating Silver to v"..version)
  27.         if f then f:close() end
  28.        
  29.         local files = http.get(filelist_URL)
  30.         if not files then term.setTextColor(colors.red) print("Could not retrieve file list") end
  31.         local filelist = textutils.unserialize(files.readAll())
  32.         files.close()
  33.        
  34.         local todownload = 1
  35.         term.setTextColor(colors.green)
  36.         function rrequest(name, t)
  37.             for i,v in pairs(t) do
  38.                 if type(v) == "table" then
  39.                     --print("Making folder .silver/"..name.."/"..i)
  40.                     fs.makeDir("/.silver/"..name.."/"..i)
  41.                     rrequest(name.."/"..i, v)
  42.                 else
  43.                     --print("Requesting .silver/"..name.."/"..v)
  44.                     http.request(root_URL.."silver/"..name.."/"..v)
  45.                     todownload = todownload + 1
  46.                 end
  47.             end
  48.         end
  49.         fs.makeDir("/.silver")
  50.         rrequest("", filelist)
  51.        
  52.         http.request(silver_URL)
  53.         --http.request(package_URL)
  54.         local silversaved, pkgsaved = false, false
  55.         while true do
  56.             local evt = {os.pullEvent()}
  57.             if evt[1] == "http_success" then
  58.                 if evt[2] == silver_URL then
  59.                     silversaved = true
  60.                     local v = io.open(shell.getRunningProgram(), "w")
  61.                     v:write(evt[3].readAll():gsub("$$VERSION", version))
  62.                     v:close()
  63.                     evt[3].close()
  64.                     term.setTextColor(colors.green)
  65.                 --[[elseif evt[2] == package_URL then
  66.                     pkgsaved = true
  67.                     term.setTextColor(colors.green)
  68.                     install(evt[3].readAll())
  69.                     evt[3].close()
  70.                     print("Successfully downloaded and saved the Silver filesystem package")]]
  71.                 else
  72.                     local file = evt[2]:match("/master/(.+)"):gsub("silver", ".silver")
  73.                     local f = io.open(file, "w")
  74.                     f:write(evt[3].readAll())
  75.                     f:close()
  76.                     print("Successfully downloaded "..file)
  77.                 end
  78.                 todownload = todownload - 1
  79.                 if todownload == 0 then break end
  80.             elseif evt[1] == "http_failure" then
  81.                 term.setTextColor(colors.red)
  82.                 print("Failed to download "..evt[2]:match("/master/(.+)"))
  83.                 print("Failed to update Silver.")
  84.                 error()
  85.             end
  86.         end
  87.        
  88.         local f2 = io.open("/.silver/version", "w")
  89.         f2:write(version)
  90.         f2:close()
  91.        
  92.         term.setTextColor(colors.green)
  93.         print("Successfully updated Silver to v"..version)
  94.         sleep(3)
  95.         shell.run(shell.getRunningProgram())
  96.         error()
  97.     end
  98. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement