Inksaver

getTools download many of these files

Oct 2nd, 2020 (edited)
1,313
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. version = 20210102.2118
  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. function clear()
  9.     term.clear()
  10.     term.setCursorPos(1, 1)
  11. end
  12.  
  13. function checkLabel()
  14.     if os.getComputerLabel() == nil then
  15.         local noname = true
  16.         while noname do
  17.             clear()
  18.             print("Type a name for this turtle (no spaces):_")
  19.             name = read()
  20.             if name == '' then
  21.                 print("Just pressing Enter does no work")
  22.             elseif string.find(name, ' ') ~= nil then
  23.                 print("NO SPACES!")
  24.             else
  25.                 noname = false
  26.             end
  27.             if noname then
  28.                 sleep(2)
  29.             end
  30.         end
  31.        
  32.         os.setComputerLabel(name)
  33.         print("Computer label set to "..os.getComputerLabel())
  34.     end
  35. end
  36.  
  37. function checkLibs(libDir, filename)
  38.     local fileExists = false
  39.     if libDir == '' then
  40.         if fs.exists(filename) or fs.exists(filename..".lua") then
  41.             fileExists = true
  42.         end
  43.     else
  44.         if fs.exists(libDir) then
  45.             if not fs.isDir(libDir) then
  46.                 fs.move(libDir, libDir.."Renamed")
  47.                 fs.makeDir(libDir)
  48.             end
  49.         else
  50.             fs.makeDir(libDir)
  51.         end
  52.         if fs.exists(fs.combine(libDir, filename)) or fs.exists(fs.combine(libDir, filename..".lua")) then
  53.             fileExists = true
  54.         end
  55.     end
  56.     return fileExists
  57. end
  58.  
  59. function checkFileExists(filename, isLib)
  60.     -- check in root or lib/ only (existing files)
  61.     local fileExists = false
  62.     if isLib then
  63.         if fs.exists(fs.combine("lib", filename)) or fs.exists(fs.combine("lib", filename..".lua")) then
  64.             fileExists = true
  65.         end
  66.     else
  67.         if fs.exists(filename) or fs.exists(filename..".lua") then
  68.             fileExists = true
  69.         end
  70.     end
  71.     return fileExists
  72. end
  73.  
  74. function createTempDir(tempDir)
  75.     if fs.exists(tempDir) then
  76.         if not fs.exists(fs.combine(tempDir, "lib")) then
  77.             fs.makeDir(fs.combine(tempDir, "lib"))
  78.         end
  79.     else
  80.         fs.makeDir(tempDir)
  81.         fs.makeDir(fs.combine(tempDir, "lib"))
  82.     end
  83. end
  84.  
  85. function createLibDir(libDir)
  86.     if fs.exists(libDir) then
  87.         if not fs.isDir(libDir) then --file called lib (unlikely...)
  88.             fs.move(libDir, libDir.."Renamed") --lib -> libRenamed
  89.             fs.makeDir(libDir)
  90.         end
  91.     else
  92.         fs.makeDir(libDir)
  93.     end
  94. end
  95.  
  96. function getFile(files, isLib, fileName, saveAs, tmpDir, tmpLibDir)
  97.     -- use pastebin get to download file (to libs folder if isLib)
  98.     if saveAs == "" then
  99.         saveAs = fileName -- use given filename if alternative not supplied
  100.     end
  101.     local saveDir = tmpDir
  102.     if isLib then
  103.         saveDir = tmpLibDir
  104.     end
  105.     success = true
  106.     print("Downloading from Pastebin...")
  107.     if shell.run("pastebin", "get", files[fileName], fs.combine(saveDir, saveAs..".lua")) then
  108.         print(saveAs..".lua copied to "..saveDir)
  109.     else
  110.         print("failed to copy "..saveAs..".lua from Pastebin")
  111.         success = false
  112.     end
  113.    
  114.     return success
  115. end
  116.  
  117. function moveFile(isLib, tmpDir, tmpLibDir, fileName) -- movgee tmp copy to /lib/
  118.     local saveDir = tmpDir
  119.     if isLib then
  120.         saveDir = tmpLibDir
  121.         fs.delete(fs.combine("lib", fileName))
  122.         fs.move(fs.combine(saveDir, fileName), fs.combine("lib", fileName)) -- move to lib/
  123.         print(fileName.." updated")
  124.     else
  125.         fs.delete(fileName)
  126.         fs.move(fs.combine(saveDir, fileName), fs.combine("./", fileName)) --move to root
  127.         print("/lib/"..fileName.." updated")
  128.     end
  129. end
  130.  
  131. function getVersion(line)
  132.     -- version = 20201223.1104
  133.     line = string.gsub(line, " ", "") -- remove spaces
  134.     local start = string.find(line, "=")
  135.     local version = 0
  136.     if start ~= nil then -- found
  137.         local dateString = string.sub(line, start + 1)
  138.         version = tonumber(dateString)
  139.     end
  140.     return version
  141. end
  142.  
  143. function isNewer(isLib, tmpDir, tmpLibDir, fileName)
  144.     -- open files to read file versions
  145.     local old = ""
  146.     local saveDir = tmpDir
  147.     local checkDir = ""
  148.     local move = false
  149.     if isLib then
  150.         if checkFileExists(fileName, true) then
  151.             old = fs.open(fs.combine("lib", fileName), "r")
  152.             saveDir = tmpLibDir
  153.             checkDir = "lib"
  154.         else
  155.             move = true
  156.         end
  157.     else
  158.         if checkFileExists(fileName, false) then
  159.             old = fs.open(fileName, "r")
  160.         else
  161.             move = true
  162.         end
  163.     end
  164.     if not move then -- previous version of file exists
  165.         if fs.exists(fs.combine(saveDir, fileName)) then
  166.             local new = fs.open(fs.combine(saveDir, fileName), "r")
  167.             local oldVer = getVersion(old.readLine())
  168.             local newVer = getVersion(new.readLine())
  169.             if oldVer ~= nil and newVer ~= nil then
  170.                 if newVer > oldVer then
  171.                     move = true
  172.                 end
  173.             elseif oldVer == nil and newVer ~= nil then
  174.                 move = true
  175.             end
  176.             new.close()
  177.             old.close()
  178.         end
  179.     end
  180.    
  181.     return move
  182. end
  183.  
  184. function main()
  185.     local doContinue = true
  186.     local tmpDir = "tmp"
  187.     local tmpLibDir = fs.combine(tmpDir, "lib")
  188.     local libDir = "lib"
  189.     checkLabel() -- make sure turtle label is set
  190.     createTempDir(tmpDir) -- create /tmp/ and /tmp/lib/
  191.     createLibDir(libDir) -- check/create lib dir
  192.     local libFiles = {}
  193.     libFiles['clsTurtle'] = "tvfj90gK"
  194.     libFiles['menu'] = "BhjbYsw4"
  195.  
  196.     local files = {}
  197.     files['go'] = "xQqK3VcK"
  198.     files['lavaRefuel'] = "kFZsXu99"
  199.     files['d'] = "i2MRYcsZ"
  200.     files['u'] = "idtySGKX"
  201.     files['b'] = "g7DjxRbr"
  202.     files['f'] = "KXCakmNn"
  203.     files['r'] = "DH6smTHb"
  204.     files['l'] = "yWDKZpvj"
  205.     files['p'] = "D25pg0QQ"
  206.     files['tk'] = "UFvjc1bw"
  207.     files['flint'] = "dBJ0frzj"
  208.     -- download into tmp folder
  209.     for k,v in pairs(libFiles) do -- download from pastebin to tmp/lib/
  210.         clear()
  211.         --getFile(files, isLib, filename, saveAs, tmpDir, libDir) files is table of files with text index, isLib = library content, saveAs = filename
  212.         getFile(libFiles, true, k, "", tmpDir, tmpLibDir) -- download file to /tmp/lib
  213.         sleep(2)
  214.     end
  215.     for k,v in pairs(files) do -- download from pastebin to tmp/
  216.         clear()
  217.         getFile(files, false, k, "", tmpDir, tmpLibDir)
  218.         sleep(2)
  219.     end
  220.     -- check if tmp/lib/file exists in root
  221.     local updated = 0
  222.     for k,v in pairs(libFiles) do
  223.         clear()
  224.         local fileName = k..".lua"
  225.         print("Checking: "..fileName)
  226.         if isNewer(true, tmpDir, tmpLibDir, fileName) then
  227.             moveFile(true, tmpDir, tmpLibDir, fileName) -- move tmp copy to /lib/#
  228.             updated = updated  + 1
  229.         else
  230.             print(fileName.." is newer or unchanged")
  231.             fs.delete(fs.combine(tmpLibDir, fileName))
  232.         end
  233.         sleep(2)
  234.     end
  235.     for k,v in pairs(files) do
  236.         clear()
  237.         local fileName = k..".lua"
  238.         print("Checking: "..fileName)
  239.         if isNewer(false, tmpDir, tmpLibDir, fileName) then
  240.             moveFile(false, tmpDir, tmpLibDir, fileName) -- move tmp copy to /lib/
  241.             updated = updated  + 1
  242.         else
  243.             print(fileName.." is newer or unchanged")
  244.             fs.delete(fs.combine(tmpDir, fileName))
  245.         end
  246.     end
  247.     print("Removing temporary files")
  248.     local status, retval = pcall(fs.delete, tmpLibDir)
  249.     if not status then
  250.         print(retval)
  251.     end
  252.     status, retval = pcall(fs.delete, tmpDir)
  253.     if not status then
  254.         print(retval)
  255.     end
  256.     print("\nOperation Complete "..updated.." files updated")
  257.    
  258. end
  259.  
  260. main()
RAW Paste Data