Advertisement
Maineiac

update.lua

Jun 18th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.15 KB | None | 0 0
  1. local currentVersion = "0"
  2. local connection = false
  3. local files = {}
  4. table.insert(files, {name = "/version", link = "0MzLR5RW"})
  5. local args = {...}
  6. if args[1] == nil then
  7.     print("Update : No command entered...")
  8. elseif args[1] == "client" then
  9.     table.insert(files, {name = "/client.lua", link = "uf8hScVN"})
  10.     table.insert(files, {name = "/startup.lua", link = "etVRTS8Z"})
  11. elseif args[1] == "server" then
  12.     table.insert(files, {name = "/server.lua", link = "myehJ0nH"})
  13.     table.insert(files, {name = "/startup.lua", link = "vmM0ztz2"})
  14.     monitor = peripheral.wrap("back")
  15.     monitor.clear()
  16.     monitor.setTextColor(colors.white)
  17.     monitor.setTextScale(0.5)
  18.     monitor.setCursorPos(1,1)
  19.     monitor.write("Updating")
  20.  
  21. end
  22. table.insert(files, {name = "/update.lua", link = "75iZjThu"})
  23. local function downloadFiles(url, path)
  24.     for i = 1, 3 do
  25.     term.setTextColor(colors.green)
  26.     print("Attempting to download : "..path)
  27.     term.setTextColor(colors.white)
  28.     local response = http.get(url)
  29.         if response then
  30.         local data = response.readAll()
  31.             if path then
  32.             local f = io.open(path, "w")
  33.             f:write(data)
  34.             f:close()
  35.             end
  36.             os.sleep(1)
  37.         return true
  38.         end
  39.     end
  40.     return false
  41. end
  42. local function checkForUpdates(updateUrl)
  43.     for i = 1, 3 do
  44.         if connection == false then
  45.             local response = http.get(updateUrl)
  46.             if response ~= nil then
  47.                 connection = true
  48.                 local data = response.readAll()
  49.                 if data ~= currentVersion then
  50.                     sleep(1)
  51.                     print("There is a new version available.")
  52.                     sleep(1)
  53.                     for k, v in pairs(files) do
  54.                         local l = files[k].link
  55.                         local n = files[k].name
  56.                         if data ~= currentVersion or not fs.exists(n) then
  57.                             term.setTextColor(colors.red)
  58.                             if fs.exists(n) then
  59.                                 print("Deleting current : "..n)
  60.                                 fs.delete(n)
  61.                                 downloadFiles("https://pastebin.com/raw/"..l, n)
  62.                             else
  63.                                 print("MISSING FILE : "..n)
  64.                                 downloadFiles("https://pastebin.com/raw/"..l, n)
  65.                             end
  66.                         end
  67.                     end
  68.                     term.setTextColor(colors.white)
  69.                 end
  70.             end
  71.         end
  72.     end    
  73. end
  74. if fs.exists(files[1].name) then
  75.         local h = fs.open(files[1].name, "r")
  76.         local text = h.readAll()
  77.         h.close()
  78.         currentVersion = text
  79. else
  80.         print("No version file in this directory.")
  81.         sleep(1)
  82.         print("Setting version to 0, this will force an update")
  83.         sleep(1)
  84. end
  85. checkForUpdates("http://pastebin.com/raw/"..files[1].link)
  86. if connection then
  87.     print("Your system is up to date!")
  88. else
  89.     print("Unable to connect to update server.")
  90. end
  91.  
  92. --shell.run("/server")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement