Advertisement
Inksaver

getTools download many of these files

Oct 2nd, 2020 (edited)
3,092
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.18 KB | None | 0 0
  1. version = 20230824.1330
  2. --[[
  3.     https://pastebin.com/8qbeZevX
  4.     Last edited: see version YYYYMMDD.HHMM
  5.     use ' pastebin run 8qbeZevX ' in turtle terminal
  6.     This will load useful tools and utilities
  7. ]]
  8.  
  9. local libDir = "lib"    -- set library directory name
  10. local tmpDir = "tmp"    -- set temp folder name to 'tmp'
  11. local root = "."
  12. local files = {}        -- table of main files
  13. local libFiles = {}     -- table of lib files (clsTurtle.lua and menu.lua)
  14. local fileSizes = {}        -- table of current file sizes
  15. local libFileSizes = {} -- table of current library file sizes
  16.  
  17. local function clear()
  18.     term.clear()
  19.     term.setCursorPos(1, 1)
  20. end
  21.  
  22. local function log(text)
  23.     print(text)
  24.     local h = fs.open("update.log", "a")
  25.     h.writeLine(text)
  26.     h.close()
  27. end
  28.  
  29. local function checkLabel()
  30.     if os.getComputerLabel() == nil then
  31.         local noname = true
  32.         while noname do
  33.             clear()
  34.             log("Give this turtle a name (no spaces)_")
  35.             name = read()
  36.             if name == '' then
  37.                 print("Just pressing Enter does not work")
  38.             elseif string.find(name, ' ') ~= nil then
  39.                 print("NO SPACES!")
  40.             else
  41.                 noname = false
  42.             end
  43.             if noname then
  44.                 sleep(2)
  45.             end
  46.         end
  47.        
  48.         os.setComputerLabel(name)
  49.         log("Computer label set to "..os.getComputerLabel())
  50.     end
  51. end
  52.  
  53. local function checkFileExists(fileName, isLib)
  54.     --[[ check in ./ or ./lib/ only (existing files) ]]
  55.     if isLib then   -- checking a library file
  56.         if fs.exists(fs.combine(libDir, fileName)) or fs.exists(fs.combine(libDir, fileName..".lua")) then
  57.             return true
  58.         end
  59.     else
  60.         if fs.exists(fileName) or fs.exists(fileName..".lua") then
  61.             return true
  62.         end
  63.     end
  64.     return false
  65. end
  66.  
  67. local function createTempDir()
  68.     if fs.exists(tmpDir) then
  69.         log("./"..tmpDir.." already exists")
  70.     else
  71.         log("./"..tmpDir.." created")
  72.         fs.makeDir(tmpDir)
  73.     end
  74. end
  75.  
  76. local function getFile(pastebin, fileName)
  77.     --[[eg "UFvjc1bw", "tmp", "tk"
  78.         use pastebin get to download file to tmp]]
  79.     log("Fetching "..pastebin.." from Pastebin...")
  80.     status, retval = pcall(shell.run, "pastebin", "get", pastebin, fs.combine(tmpDir, fileName))
  81.     --if shell.run("pastebin", "get", pastebin, fs.combine(tmpDir, fileName)) then
  82.     if status then
  83.         log("Fetch error message "..tostring(retVal))
  84.         log("Fetch success: "..tostring(status))
  85.         log(fileName.." copied to ./"..tmpDir)
  86.         return true
  87.     else
  88.         log(retval)
  89.         log("failed to copy "..fileName.." from Pastebin")
  90.         return false
  91.     end
  92. end
  93.  
  94. local function moveFile(isLib, fileName) -- movgee tmp copy to /lib/
  95.     if isLib then
  96.         fs.delete(fs.combine(libDir, fileName))
  97.         sleep(1)
  98.         fs.move(fs.combine(tmpDir, fileName), fs.combine(libDir, fileName)) -- move to lib/
  99.         log("Moved: "..fileName.." from ./"..tmpDir.." to ./"..libDir.."/")
  100.     else
  101.         fs.delete(fileName)
  102.         sleep(1)
  103.         fs.move(fs.combine(tmpDir, fileName), fs.combine(root, fileName)) --move to root
  104.         log("Moved: "..fileName.." from ./"..tmpDir.." to ./")
  105.     end
  106. end
  107.  
  108. local function getVersion(line, fileName)
  109.     --[[ version = 20201223.1104 ]]
  110.     local version = 0
  111.     if line == nil then
  112.         log("Error reading "..fileName)
  113.     else
  114.         line = line:gsub(" ", "") -- remove spaces
  115.         local start = line:find("=")
  116.        
  117.         if start ~= nil then -- found
  118.             version = tonumber(line:sub(start + 1))
  119.         end
  120.     end
  121.    
  122.     return version
  123. end
  124.  
  125. local function isNewer(isLib, fileName)
  126.     --[[ open files in tmp and .. or ../tmp to read file version ]]
  127.     local old = nil                                     -- declare old file handle
  128.     local new = nil                                     -- declare new file handle
  129.     local move = true                                   -- set move flag to true
  130.     local oldOpen, newOpen = false, false
  131.    
  132.     if isLib then                                       -- file is in lib/
  133.         if checkFileExists(fileName, true) then
  134.             old = fs.open(fs.combine(libDir, fileName), "r")
  135.             move = false
  136.             oldOpen = true
  137.         end
  138.     else                                                -- not a library file
  139.         if checkFileExists(fileName, false) then
  140.             old = fs.open(fileName, "r")
  141.             move = false
  142.             oldOpen = true
  143.         end
  144.     end
  145.    
  146.     if not move then                                    -- previous version of file exists
  147.         if fs.exists(fs.combine(tmpDir, fileName)) then -- should exist in tmp folder
  148.             new = fs.open(fs.combine(tmpDir, fileName), "r")
  149.             newOpen = true
  150.             local oldVer = getVersion(old.readLine(),fileName)
  151.             log("Existing "..fileName.." version: "..oldVer)
  152.             local newVer = getVersion(new.readLine(), fileName)
  153.             log("Downloaded "..fileName.." version: "..newVer)
  154.             if oldVer > 0 and newVer > 0 then
  155.                 if newVer > oldVer then
  156.                     move = true
  157.                 end
  158.             elseif oldVer == 0 and newVer > 0 then
  159.                 move = true
  160.             end
  161.         end
  162.     end
  163.    
  164.     if newOpen then
  165.         new.close()
  166.     end
  167.     if oldOpen then
  168.         old.close()
  169.     end
  170.     return move
  171. end
  172.  
  173. local function process(key, value, isLib)
  174.     local fileName = key..".lua"
  175.     local count = 0
  176.     local fileSize = 0
  177.     local freeSpace = fs.getFreeSpace("./")
  178.     log("\nAvailable space: "..freeSpace)
  179.     log("Checking: "..fileName)
  180.     if isLib then
  181.         fileSize = libFileSizes[key]
  182.     else
  183.         fileSize = fileSizes[key]
  184.     end
  185.     log(fileName.." size:"..fileSize)
  186.     log("Available space after fetch: "..freeSpace - fileSize)
  187.     if freeSpace - fileSize < 50000 then
  188.         clear()
  189.         local message =
  190. [[Insufficient disk space for update.
  191. 1. Save your game
  192. 2. Edit the file:
  193. serverconfig/computercraft-server.toml
  194. (in your game save folder)
  195. 3. Change line 2:
  196. computer_space_limit = 1000000 to
  197. computer_space_limit = 2000000 or more
  198. 4. Re-start the game
  199. 5. Run the updater again
  200. ]]
  201.         log(message)
  202.         return -1
  203.     else
  204.         if getFile(value, fileName) then        -- download file to /tmp
  205.             if isNewer(isLib, fileName) then
  206.                 moveFile(isLib, fileName)       -- move tmp copy to /lib
  207.                 count = count  + 1
  208.             else
  209.                 log(fileName.." is newer or unchanged")
  210.             end
  211.             sleep(1)
  212.             log("Removing "..tmpDir.."/"..fileName)
  213.             fs.delete(fs.combine(tmpDir, fileName))
  214.             sleep(2)
  215.         end
  216.     end
  217.    
  218.     return count
  219. end
  220.  
  221. local function addFileSize(isLib, tblName, key)
  222.     local fileName = key..".lua"
  223.     if checkFileExists(fileName, isLib) then
  224.         if isLib then
  225.             tblName[key] = fs.getSize(fs.combine(libDir, fileName))
  226.             log("File size "..libDir.."/"..fileName..": "..tblName[key])
  227.         else
  228.             tblName[key] = fs.getSize(fileName)
  229.             log("File size "..fileName..": "..tblName[key])
  230.         end
  231.     else
  232.         tblName[key] = 0
  233.         log("File size "..fileName.."\t: "..tblName[key].." (file not present)")
  234.     end
  235.    
  236. end
  237.  
  238. function main()
  239.     local doContinue = true                         -- set continue flag                       
  240.     clear()
  241.     checkLabel()                                    -- make sure turtle label is set
  242.     createTempDir()                                 -- create /tmp/
  243.     local tk = "UFvjc1bw"
  244.    
  245.     libFiles['clsTurtle']   = "tvfj90gK"
  246.     libFiles['menu']        = "BhjbYsw4"
  247.                            
  248.     --files["tk"]       = "UFvjc1bw"
  249.     files['go']         = "xQqK3VcK"
  250.     files['lavaRefuel'] = "kFZsXu99"
  251.     files['d']          = "i2MRYcsZ"
  252.     files['u']          = "idtySGKX"
  253.     files['b']          = "g7DjxRbr"
  254.     files['f']          = "KXCakmNn"
  255.     files['r']          = "DH6smTHb"
  256.     files['l']          = "yWDKZpvj"
  257.     files['p']          = "D25pg0QQ"
  258.     files['x']          = 'Z9GBSM8e'
  259.     files['flint']      = "dBJ0frzj"
  260.     files['data']       = "fCKDc9Vi"
  261.    
  262.     local updated = 0
  263.     local libUpdated = 0
  264.     local h = fs.open("update.log", "w")
  265.     h.writeLine('Update Log:')
  266.     h.close()
  267.    
  268.     log("Checking file sizes:\n")
  269.  
  270.     for key, _ in pairs(libFiles) do
  271.         addFileSize(true, libFileSizes, key)
  272.     end
  273.     addFileSize(false, fileSizes, "tk")
  274.     for key, _ in pairs(files) do
  275.         addFileSize(false, fileSizes, key)
  276.     end
  277.     local freeSpace = fs.getFreeSpace("./")
  278.     log("Free space: "..freeSpace.."\n")
  279.     clear()
  280.     local updated, libUpdated = 0, 0
  281.     updated = updated + process("tk", tk, false)            -- start with tk as may cause out of space error
  282.     if updated >= 0 then
  283.         for key,value in pairs(libFiles) do                    
  284.             libUpdated = libUpdated + process(key, value, true)     -- download lib files from pastebin to tmp/
  285.         end
  286.         for key,value in pairs(files) do
  287.             updated = updated + process(key, value, false)          -- download root files from pastebin to tmp/
  288.         end
  289.         sleep(1)
  290.         log("Attempting deletion of ./"..tmpDir)
  291.         status, retval = pcall(fs.delete, tmpDir)
  292.         if status then
  293.             log("./"..tmpDir.." deleted")
  294.         else
  295.             log("Directory delete error message:\n "..tostring(retVal))
  296.         end
  297.         log("\nOperation Complete ")
  298.         log(libUpdated.." library files updated")
  299.         log(updated.." root files updated")
  300.     end
  301.     print("See update.log for details")
  302. end
  303.  
  304. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement