Advertisement
antonsavov

WIP setup

Nov 29th, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.06 KB | None | 0 0
  1. --add this script to an Advanced Computer with the command:
  2. -- pastebin get VbZ5tvq9 update
  3. -- then use 'update' to get the stable version
  4. -- or 'update wip' to get the WIP version
  5.  
  6. -- process command line parameters
  7. local tArgs = { ... } -- get the command line arguments
  8. local wip_path = nil
  9. if #tArgs == 1 then
  10.     wip_path = tArgs[1]
  11. elseif #tArgs > 1 then
  12.     print("Usage: update <wip_version_subfolder_on_the_url>")
  13.     return
  14. end
  15.  
  16. local repo_url = "https://www.20000blocks.com/twenty-k-scripts/"..((wip_path) and (wip_path.."/") or "stable/")
  17.  
  18. --- see if the file exists
  19. function file_exists(_filename, _folder)
  20.     local fullname = fs.combine( _folder or "", _filename )
  21.     local f = fs.open(fullname, "rb")
  22.     if f then f:close() end
  23.     return f ~= nil
  24. end
  25.  
  26. --- creates a new text file and writes a string to it
  27. local function file_write(text, filename, path)
  28.     if path then fs.makeDir(path) end
  29.     local fullname = fs.combine(path or "",filename)
  30.     local file = assert(fs.open(fullname, "w"), "error writing to file:"..fullname)
  31.     file.write(text)
  32.     file.close()
  33.     return fullname
  34. end
  35.  
  36. --- get all lines from a file, returns an empty
  37. -- list/table if the file does not exist
  38. function lines_from(file)
  39.   if not file_exists(file) then return {} end
  40.   lines = {}
  41.   for line in io.lines(file) do
  42.     lines[#lines + 1] = line
  43.   end
  44.   return lines
  45. end
  46.  
  47. local function downloadScript(script_name, script_local_folder, extension_on_remote)
  48.     -- the user agent needs to be renamed otherwise the default one is Java
  49.     -- and Java is blocked by the .htaccess file on the www.20000blocks.com website
  50.     local headers = {
  51.         [ "User-Agent" ] = "20.000 BLOCKS"
  52.     }
  53.     local download_url = repo_url..script_name..(extension_on_remote or "")
  54.     --print("Downloading from: "..download_url)
  55.     local link = http.get( download_url, headers )
  56.     if link then
  57.         local script_body = link.readAll()
  58.         return file_write(script_body, script_name, script_local_folder)
  59.     else
  60.         term.setTextColor(colors.red)
  61.         print("Failed to download: "..script_name)
  62.         return false
  63.     end
  64.     return false
  65. end
  66.  
  67. local listfile = "filelist.txt"
  68.  
  69. local function downloadScripts()
  70.     local scriptlistfile = downloadScript(listfile)
  71.     if not scriptlistfile then
  72.         print("requested version does not exist on the server")
  73.         return false
  74.     end
  75.     local scriptlist = lines_from(scriptlistfile)
  76.     for i, script in ipairs(scriptlist) do
  77.         local i, j = string.find(script, "/")
  78.         local scriptfolder = nil
  79.         local scriptname = nil
  80.         if i then
  81.             scriptfolder = script:sub(1,i-1)
  82.             scriptname = script:sub(i+1,-1)
  83.         else
  84.             scriptname = script
  85.         end
  86.         if not downloadScript(scriptname, scriptfolder, ".lua") then return false end
  87.     end
  88.     return true
  89. end
  90.  
  91. -- do the installation
  92. term.setTextColor(colors.green)
  93. print("Updating to "..((wip_path) and ("VERSION: "..wip_path) or "STABLE VERSION"))
  94. print("Installing 20.000 BLOCKS scripts...")
  95. term.setTextColor(colors.white)
  96.  
  97. if not downloadScripts() then
  98.     print("downloading scripts failed")
  99.     os.exit()
  100. end
  101.  
  102. term.setTextColor(colors.green)
  103. print("20.000 BLOCKS scripts installed successfully!")
  104. term.setTextColor(colors.white)
  105. print("update complete!")
  106. --[[
  107. print("update complete! Rebooting...")
  108. os.sleep(0.5)
  109. shell.run("reboot")
  110. --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement