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

How do you make an updater

Started by IsaacTBeast, 15 March 2014 - 03:48 PM
IsaacTBeast #1
Posted 15 March 2014 - 04:48 PM
How do you make an updater from http API…
Shnupbups #2
Posted 15 March 2014 - 11:05 PM
You can do this with any website, but for what I'm going to say here, we'll use Pastebin.

Make sure you have a pastebin account and are logged in (pastebin.com) as this will ensure you can edit your files. Post all the files you want updateable to pastebin *on the website* whilst logged in. DO NOT USE PASTEBIN PUT!

If you don't already have these files on the computer you're working on, you can download it with pastebin get. Use the FS api to open the file you're going to update. Then, use readAll to put the entire contents into a string. Now, use:

local f = http.get("http://pastebin.com/raw.php?i=PBCODE") --# Opens up the website as if it were a file
local fl = f.readAll() --# Reads the entire contents of the website and places them in a variable
f.close() --# Closes the website
Make sure to replace PBCODE with the code on pastebin, and f and fl with variables of your choice. Then you can simply use:

if u ~= fl then --# Checks to see if u (the file we're updating) and fl (the version on pastebin) are the same if not, the other stuff happens.
  fs.delete("file")  --# So, if they're not the same, delete the old file,
  local nf = fs.open("file","w") --# open it back up,
  nf.write(fl) --# write the new code to it,
  nf.close() --# and close it again.
end
Make sure to replace u with the variable you used at the start for the contents of the file we're updating, fl to the variable you used for the contents of the file on pastebin, file to the name of the file we're updating and nf to the variable of your choice. Repeat for all files you want to update. When you want to update a file, log in to our pastebin account, go into 'my pastes' and edit it.

So what have we done? We've:
  • Posted a file to pastebin.
  • Saved the entire contents of a file to a variable.
  • Opened up pastebin and saved the entire contents of the pastebin copy to another variable.
  • Compared the pastebin copy to the computer's copy.
  • If they weren't the same, we deleted the old copy on the computer and replaced it with the copy on pastebin.
Now, there are many ways to improve upon this code, but for now, this'll work.

Code for updating an entire folder

local cds = {
file1 = "MKccRkwa";
file2 = "2ZYa5fes";
}

local function updateFile(path,code)
  local p = fs.open(path,"r")
  local u = p.readAll()
  p.close()
  local f = http.get("http://pastebin.com/raw.php?i="..code)
  local fl = f.readAll()
  f.close()
  if u ~= fl then
	fs.delete(path)
	local nf = fs.open(path,"w")
	nf.write(fl)
	nf.close()
	return true
  end
  return false
end

local fls = fs.list("folder")
for i = 1,#fls do
  updateFile("folder/"..fls[i],cds[fls[i]])
end
Again, this can be improved upon in many ways, and I haven't even tested it, so I'm not sure if it even works :P/> but it's good enough.
EDIT: Tested, works.
Edited on 16 March 2014 - 12:00 AM
theoriginalbit #3
Posted 16 March 2014 - 01:57 AM
READ THE COMPUTERCRAFT WIKI! Each API has working examples of how to use each function within them!
IsaacTBeast #4
Posted 16 March 2014 - 01:58 AM
-_-/>… I forgot to edit that I already made an Updater… -_-/> sorry for that.