This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Cozzimoto's profile picture

[Question] Making a autoupdater

Started by Cozzimoto, 26 December 2012 - 07:12 AM
Cozzimoto #1
Posted 26 December 2012 - 08:12 AM
Hey guys, i wrote a completely new turtle api that auto detects its facing and uses gps to get around and there is a quarry program i also wrote in there as well to dig three layers at a time, but the next thing i want to do is to add an updater check in the api to check the version located on the turtle vs the one on my pastebin. i do not have access to mysql and i am having trouble wrapping my head around how i would attempt to do it.

my idea is to download the one from pastebin into a updater folder and compare the very first line to the file on the turtle that the line would be something like v = 1.0 and it takes that variable and compares it

any Ideas???
Doyle3694 #2
Posted 26 December 2012 - 08:46 AM
This is what I use:
function UpdateProgram(program, adress)
   fs.delete("update")
   shell.run("pastebin get "..adress.." update")
   term.setCursorPos(1,1)
   local update = fs.open("update", "r")
   local prev = fs.open(program, "a")
   prev.close()
   local prev = fs.open(program, "r")
   local tprev = {update.readAll()}
   local tupdate = {prev.readAll()}
   prev.close()
   update.close()
   if #tprev == #tupdate then
	  for i = 1, #tupdate do
		 if tprev[i] ~= tupdate[i] then
		    fs.delete(program)
		    fs.copy("update", program)
		    fs.delete("update")
		    os.reboot()
		 end
	  end
   else
	  fs.delete(program)
	  fs.copy("update", program)
	  fs.delete("update")
	  os.reboot()
   end
end

And then I have that and a couple of calls on it for all the files to update in a program on their own. And from my main program I just shell.run()

I don't know how advanced you are, so please tell me all you don't understand. While I don't want to provide code just like this I don't want to spend half an hour commenting it either. So just tell me what is unclear and I'll explain it.
Cozzimoto #3
Posted 26 December 2012 - 09:08 AM
so if i am reading this right you delete the update file and get the updated file you want to check
then open it and place everything in a table
and what you do is replace everything from the updated file to the one on the computer/turtle.

this is a nice code, but it gives me an idea of how a updater works.
what i am after is just reading the first line of the file and comparing that to the current version on the computer/turtle and if its outdated then just delete the old one and redownload it in its place. so maybe i can use how you have your table to read everything to just read the first line and use that to compare the versions maybe…
Doyle3694 #4
Posted 26 December 2012 - 11:58 AM
yep ;)/> Sounds good to me ;)/>
remiX #5
Posted 26 December 2012 - 12:19 PM
If you're using the simple use of having ver = 1.0 at the top of the file, this function should work:


args = {...}
program = args[1]
pastebin = args[2]

function CheckForUpdate(_programName, pastebinURL)
    shell.run("pastebin get "..pastebinURL.." update")
    if not fs.exists("update") then
        print("Could not download file at " .. pastebinURL)
    end
    fileProgram = fs.open(_programName, "r")
    fileUpdate = fs.open("update", "r")
    s_programNameLine = fileProgram.readLine()
    s_programUpdateLine = fileUpdate.readLine()
    fileProgram.close()
    fileUpdate.close()
    if s_programNameLine == s_programUpdateLine then
        print("No update needed.")
        fs.delete("update")
    else
        print("Updated to version " .. string.sub(s_programUpdateLine, #s_programUpdateLine-2, #s_programUpdateLine))
        fs.delete(_programName)
        fs.copy("update", _programName)
        fs.delete("update")
    end
end

CheckForUpdate(program, pastebin)