Advertisement
derkoch

rbbupdater

Oct 27th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.38 KB | None | 0 0
  1. -- ccGUI-API Updater by DerKoch AKA RoyalBingBong
  2. -- Original idea for updater by Henness's "Advanced Updater"
  3.  
  4. -- Config
  5. local author = "RoyalBingBong"
  6. local project = "ccGUI-API"
  7. local branch = "master"
  8. local files = {"guiElements", "helper", "screen", "config"}
  9. local dir = "RBB/"
  10.  
  11. --~ ##### Original Henness code START #####
  12. function getLink(file)
  13.     return "https://raw.github.com/" .. author .. "/" .. project .. "/" .. branch .. "/" .. file
  14. end
  15.  
  16. function download(fname, name)
  17.     write("Downloading: " .. fname)
  18.     local data = http.get(getLink(fname))
  19.     if data then
  20.         write("\t\tDone\n")
  21.         local file = fs.open(name,"w")
  22.         file.write(data.readAll())
  23.         file.close()
  24.         return true
  25.     end
  26.     return false
  27. end
  28. --~ ##### Original Henness code END #####
  29.  
  30. -- Checks if newfile is newer than oldfile
  31. -- Opens each files reads the first line and does some string operations
  32. -- to get the version number of each
  33. function isNewer(oldfile, newfile)
  34.     -- I don't want to kill your settings ;)
  35.     if oldfile == dir.."config" then
  36.         return false
  37.     end
  38.     fold = fs.open(oldfile, "r")
  39.     fnew = fs.open(newfile, "r")
  40.     if not fold then
  41.         return true
  42.     end
  43.     foldversion = fold.readLine()
  44.     fnewversion = fnew.readLine()
  45.     fold.close()
  46.     fnew.close()
  47.     local splitpos = string.find (foldversion, "=") + 2
  48.     foldversion = string.sub (foldversion, splitpos)
  49.     local splitpos = string.find (fnewversion, "=") + 2
  50.     fnewversion = string.sub (fnewversion, splitpos)
  51.     return tonumber(foldversion) < tonumber(fnewversion)   
  52. end
  53.  
  54. -- Downloads all files
  55. function downloadAll()
  56.     for _, fname in pairs(files) do
  57.         download(fname, dir .. fname .. "tmp")
  58.     end
  59. end
  60.  
  61. -- Deletes or renames files according to their version number
  62. function checkAll()
  63.     for _,fname in pairs(files) do
  64.         if fs.exists(dir..fname) then
  65.             if isNewer(dir..fname, dir..fname.."tmp") then
  66.                 print("Deleting old " .. fname)
  67.                 fs.delete(dir..fname)
  68.                 sleep(0.5)
  69.                 print("Renaming " .. fname.."tmp" .. " to " .. fname)
  70.                 fs.move(dir..fname.."tmp", dir..fname)
  71.             else
  72.                 print(fname .. " is newest version")
  73.                 fs.delete(dir..fname.."tmp")
  74.             end
  75.         else
  76.             print("Renaming " .. fname.."tmp" .. " to " .. fname)
  77.             fs.move(dir..fname.."tmp", dir..fname)
  78.         end
  79.     end
  80. end
  81.  
  82. -- Just creates the subdirectory for my API
  83. function setup()   
  84.     if not fs.exists(dir) then
  85.         fs.makeDir(dir)
  86.     end
  87. end
  88.  
  89. setup()
  90. downloadAll()
  91. checkAll()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement