Firestorm002

Universal Program Updater API v0.3

Jul 22nd, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.10 KB | None | 0 0
  1. --[[
  2.   0.3
  3.   ^^ Do not touch ANYTHING above this line ^^
  4.   Universal Program Updater API
  5.   By: TisTheeIntrepid(Firestorm002)
  6. ]]
  7. --Functions--
  8. --Grabs the running programs path and name.
  9. function getRunningPath(runPath)
  10.   local runningProgram = runPath
  11.   local programName = fs.getName(runningProgram)
  12.   return runningProgram:sub(1,#runningProgram - #programName),programName  
  13. end
  14. --Runs the actually updating part of the process and uses the specified 'pasteBinRawURL'.
  15. function updater(pasteBinRawURL,runPath)
  16.     print("Updating...")
  17.   local httpPage = http.get(pasteBinRawURL)
  18.   local downloadedProgram = httpPage.readAll()
  19.   local pPath, pName = getRunningPath(runPath)
  20.     print("Downloading new version to '"..pPath..pName.."'!")
  21.   local dFile = fs.open(pPath..pName,"w")
  22.   dFile.write(downloadedProgram)
  23.   dFile.close()
  24. end
  25. --Checks the version number on line 2 of your program to the program's pastebin page
  26. -- via line 2.
  27. function verCheck(pasteBinRawURL,runPath)
  28.   local httpPage = http.get(pasteBinRawURL)
  29.   httpPage.readLine()
  30.   local httpVerNum = tonumber(httpPage.readLine())
  31.   local pPath, pName = getRunningPath(runPath)
  32.   local h = fs.open(pPath..pName,"r")
  33.   h.readLine()
  34.   local myVersionNum = tonumber(h.readLine())
  35.   h.close()
  36.   if myVersionNum == httpVerNum then
  37.     print("Everything up to date! On version, v"..tostring(myVersionNum))
  38.   else
  39.     print("Needs an update! Newest version is, v"..tostring(httpVerNum))
  40.     print("You're on version, v"..tostring(myVersionNum))
  41.     updater(pasteBinRawURL,runPath)
  42.   end
  43. end
  44. --Runs all the above functions, and checks for extra/optional arguments.
  45. function checkUpdate(pasteBinID,runPath,optionalArg)
  46.   --Converts pastebin file ID to pastebin raw url for other functions
  47.   local pasteBinRawURL = ("http://pastebin.com/raw.php?i="..pasteBinID)
  48.   if optionalArg == "forceUpdate" then
  49.     print("Forcing an update.")
  50.     updater(pasteBinRawURL,runPath)
  51.   elseif optionalArg == "noUpdate" then
  52.     print("Skipping updating process...")
  53.   else
  54.     print("Checking if in need of an update...")
  55.     verCheck(pasteBinRawURL,runPath)
  56.   end
  57. end
Advertisement
Add Comment
Please, Sign In to add comment