Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --add this script to an Advanced Computer with the command:
- -- pastebin get VbZ5tvq9 update
- -- then use 'update' to get the stable version
- -- or 'update wip' to get the WIP version
- -- process command line parameters
- local tArgs = { ... } -- get the command line arguments
- local wip_path = nil
- if #tArgs == 1 then
- wip_path = tArgs[1]
- elseif #tArgs > 1 then
- print("Usage: update <wip_version_subfolder_on_the_url>")
- return
- end
- local repo_url = "https://www.20000blocks.com/twenty-k-scripts/"..((wip_path) and (wip_path.."/") or "stable/")
- --- see if the file exists
- function file_exists(_filename, _folder)
- local fullname = fs.combine( _folder or "", _filename )
- local f = fs.open(fullname, "rb")
- if f then f:close() end
- return f ~= nil
- end
- --- creates a new text file and writes a string to it
- local function file_write(text, filename, path)
- if path then fs.makeDir(path) end
- local fullname = fs.combine(path or "",filename)
- local file = assert(fs.open(fullname, "w"), "error writing to file:"..fullname)
- file.write(text)
- file.close()
- return fullname
- end
- --- get all lines from a file, returns an empty
- -- list/table if the file does not exist
- function lines_from(file)
- if not file_exists(file) then return {} end
- lines = {}
- for line in io.lines(file) do
- lines[#lines + 1] = line
- end
- return lines
- end
- local function downloadScript(script_name, script_local_folder, extension_on_remote)
- -- the user agent needs to be renamed otherwise the default one is Java
- -- and Java is blocked by the .htaccess file on the www.20000blocks.com website
- local headers = {
- [ "User-Agent" ] = "20.000 BLOCKS"
- }
- local download_url = repo_url..script_name..(extension_on_remote or "")
- --print("Downloading from: "..download_url)
- local link = http.get( download_url, headers )
- if link then
- local script_body = link.readAll()
- return file_write(script_body, script_name, script_local_folder)
- else
- term.setTextColor(colors.red)
- print("Failed to download: "..script_name)
- return false
- end
- return false
- end
- local listfile = "filelist.txt"
- local function downloadScripts()
- local scriptlistfile = downloadScript(listfile)
- if not scriptlistfile then
- print("requested version does not exist on the server")
- return false
- end
- local scriptlist = lines_from(scriptlistfile)
- for i, script in ipairs(scriptlist) do
- local i, j = string.find(script, "/")
- local scriptfolder = nil
- local scriptname = nil
- if i then
- scriptfolder = script:sub(1,i-1)
- scriptname = script:sub(i+1,-1)
- else
- scriptname = script
- end
- if not downloadScript(scriptname, scriptfolder, ".lua") then return false end
- end
- return true
- end
- -- do the installation
- term.setTextColor(colors.green)
- print("Updating to "..((wip_path) and ("VERSION: "..wip_path) or "STABLE VERSION"))
- print("Installing 20.000 BLOCKS scripts...")
- term.setTextColor(colors.white)
- if not downloadScripts() then
- print("downloading scripts failed")
- os.exit()
- end
- term.setTextColor(colors.green)
- print("20.000 BLOCKS scripts installed successfully!")
- term.setTextColor(colors.white)
- print("update complete!")
- --[[
- print("update complete! Rebooting...")
- os.sleep(0.5)
- shell.run("reboot")
- --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement