minimite

updater

Dec 2nd, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.90 KB | None | 0 0
  1. print("Updating the Dungeon core files!")
  2. local all = {}
  3. if fs.exists(":dungeon/hashes") then
  4.     local open = fs.open(":dungeon/hashes", "r")
  5.     all = open.readAll()
  6.     open.close()
  7.     all = textutils.unserialize(all)
  8. end
  9. if not fs.exists(":dungeon/dcore/apis/JSON.lua") then
  10.     local x = http.get("https://raw.githubusercontent.com/wergat/DungeonMap/master/dcore/apis/JSON.lua").readAll()
  11.     local open = fs.open(":dungeon/dcore/apis/JSON.lua", "w")
  12.     open.write(x)
  13.     open.close()
  14. end
  15. JSON = assert(loadfile ":dungeon/dcore/apis/JSON.lua")()
  16. if not http then
  17.     printError("No HTTP!")
  18.     error()
  19. end
  20.  
  21. local files_updated = 0
  22. local function downloadFromDir(url)
  23.     local x = http.get(url)
  24.     if x then
  25.         x = x.readAll()
  26.     else
  27.         local now = http.get("http://www.timeapi.org/utc/now").readAll()
  28.         now = now:sub(15,16)
  29.         now = tonumber(now)
  30.         local minutes = 60 - now
  31.         local pstr = "minutes"
  32.         if minutes == 1 then
  33.             pstr = "minute"
  34.         end
  35.         printError("Oops! It looks like you've installed the DungeonMap files too much recently. Try again in about " .. minutes .. " " .. pstr .. "!")
  36.         error()
  37.     end
  38.     x = JSON:decode(x)
  39.     for i = 1, #x do
  40.         local f = x[i]
  41.         local path = f["path"]
  42.         if f["sha"] == all[path] then
  43.             print("Found file that doesn't need to be updated.")
  44.         elseif f["type"] == "dir" then
  45.             print("Downloading directory " .. f["sha"])
  46.             all[path] = f["sha"]
  47.             downloadFromDir(f["url"])
  48.         else
  49.             print("Downloading file " .. f["sha"])
  50.             local xt = http.get(f["download_url"]).readAll()
  51.             local open = fs.open("dungeon/" .. path, "w")
  52.             open.write(xt)
  53.             open.close()
  54.             all[path] = f["sha"]
  55.             files_updated = files_updated + 1
  56.         end
  57.     end
  58. end
  59.  
  60. downloadFromDir("https://api.github.com/repos/wergat/DungeonMap/contents/")
  61. print("Finished updating! " .. files_updated .. " files have been updated.")
  62. local open = fs.open(":dungeon/hashes", "w")
  63. open.write(textutils.serialize(all))
  64. open.close()
Advertisement
Add Comment
Please, Sign In to add comment