Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- 0.3
- ^^ Do not touch ANYTHING above this line ^^
- Universal Program Updater API
- By: TisTheeIntrepid(Firestorm002)
- ]]
- --Functions--
- --Grabs the running programs path and name.
- function getRunningPath(runPath)
- local runningProgram = runPath
- local programName = fs.getName(runningProgram)
- return runningProgram:sub(1,#runningProgram - #programName),programName
- end
- --Runs the actually updating part of the process and uses the specified 'pasteBinRawURL'.
- function updater(pasteBinRawURL,runPath)
- print("Updating...")
- local httpPage = http.get(pasteBinRawURL)
- local downloadedProgram = httpPage.readAll()
- local pPath, pName = getRunningPath(runPath)
- print("Downloading new version to '"..pPath..pName.."'!")
- local dFile = fs.open(pPath..pName,"w")
- dFile.write(downloadedProgram)
- dFile.close()
- end
- --Checks the version number on line 2 of your program to the program's pastebin page
- -- via line 2.
- function verCheck(pasteBinRawURL,runPath)
- local httpPage = http.get(pasteBinRawURL)
- httpPage.readLine()
- local httpVerNum = tonumber(httpPage.readLine())
- local pPath, pName = getRunningPath(runPath)
- local h = fs.open(pPath..pName,"r")
- h.readLine()
- local myVersionNum = tonumber(h.readLine())
- h.close()
- if myVersionNum == httpVerNum then
- print("Everything up to date! On version, v"..tostring(myVersionNum))
- else
- print("Needs an update! Newest version is, v"..tostring(httpVerNum))
- print("You're on version, v"..tostring(myVersionNum))
- updater(pasteBinRawURL,runPath)
- end
- end
- --Runs all the above functions, and checks for extra/optional arguments.
- function checkUpdate(pasteBinID,runPath,optionalArg)
- --Converts pastebin file ID to pastebin raw url for other functions
- local pasteBinRawURL = ("http://pastebin.com/raw.php?i="..pasteBinID)
- if optionalArg == "forceUpdate" then
- print("Forcing an update.")
- updater(pasteBinRawURL,runPath)
- elseif optionalArg == "noUpdate" then
- print("Skipping updating process...")
- else
- print("Checking if in need of an update...")
- verCheck(pasteBinRawURL,runPath)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment