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

Auto Updater

Started by Zacklogan, 17 January 2014 - 12:27 PM
Zacklogan #1
Posted 17 January 2014 - 01:27 PM
Hi, I have a question. How would I go about creating an auto updater so it downloads the program then checks if its the same as the other download they did before and if it is not, it will replace the older one with the newest one. This would help me a lot.

Kind Regards,
zacklogan.
Agoldfish #2
Posted 17 January 2014 - 01:50 PM
I'm pretty sure it is something like this.
As the program states, this isn't tested.

--THIS IS NOT TESTED--
local function versionCheck()
if version ~= 1.0 then
print"A new version is available. Would you like to download it?"
vConf = read()
if vConf == "Y" then
print"shell.run pastebin files go here"
else
print"Oh, nevermind"
end
local version = 1.0 --Makes a version
versionCheck()
print"This is a program."
print"Notice the version in the first line."
CoLDarkness #3
Posted 17 January 2014 - 02:24 PM
Hi, I have a question. How would I go about creating an auto updater so it downloads the program then checks if its the same as the other download they did before and if it is not, it will replace the older one with the newest one. This would help me a lot.

Kind Regards,
zacklogan.


local function dofile (filename) -- we need this later.
	  local f = assert(loadfile(filename))
	  return f()
	end
local function get(paste) -- This function is from pastebin program.
	local response = http.get(
		"http://pastebin.com/raw.php?i="..textutils.urlEncode( paste )
	)
	  
	if response then
		--print( "Success." )
	  
		local sResponse = response.readAll()
		response.close()
		return sResponse
	else
		print( "Update failed." )
	end
end
local version = "1.6" -- Put the version in here.
local pastetoget = "ENTERYOURPASTEIDHERE" -- Enter your paste
local dataweneed = get(pastetoget)
if string.find( dataweneed, 'local version = "'..version..'"' ) then -- This if part can be commented out if you want to disable UPDATES on your own computer.
-- if you are coding it in-game etc.




-- If this happens, our program found a different version on pastebin
local handsofthedead = fs.open(".updatedprog","w")
handsofthedead.write(dataweneed)
handsofthedead.close()
fs.move(shell.getRunningProgram(),"noneedanymore")
fs.move(".updatedprog",shell.getRunningProgram() )
fs.delete("noneedanymore")
-- Process ends after here. Update done. TADAA.
-- We should add some kind of thing so user knows wtf happened
print("The program updated.")
dofile(shell.getRunningProgram())
return
end
The code above can be perfectly fine.


PS: If you are coding in-game, you will have to comment the line with checkver().
Edited on 17 January 2014 - 01:25 PM
Lyqyd #4
Posted 17 January 2014 - 02:30 PM
This sort of thing is generally a bad idea. Updating should be a user-initiated task, especially if you are checking for updates by downloading a file from a site like pastebin rather than your own hosting.
CoLDarkness #5
Posted 17 January 2014 - 02:46 PM
This sort of thing is generally a bad idea. Updating should be a user-initiated task, especially if you are checking for updates by downloading a file from a site like pastebin rather than your own hosting.

Why not?
Zacklogan #6
Posted 17 January 2014 - 06:10 PM
Thanks for all who posted. It helped a lot. An admin/mod can go ahead and lock this topic.

Kind Regards,
Zacklogan.
Lyqyd #7
Posted 17 January 2014 - 08:01 PM
This sort of thing is generally a bad idea. Updating should be a user-initiated task, especially if you are checking for updates by downloading a file from a site like pastebin rather than your own hosting.

Why not?

Because it's rude to uselessly hammer websites you don't own, repeatedly downloading a file that very rarely changes just to see if it's changed.