Advertisement
Axeer

oc_dependency_installer.lua

Dec 18th, 2021
958
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.35 KB | None | 0 0
  1. -- хуита для закачивания на комп файлов с инета
  2. -- на вход принимает файл в формате .in .Контент должен быть типа:
  3. -- список файлов через запятую в квадратных скобках [main.lua, notmain.lua, lessthanmain.lua]
  4. -- список url адресов, через которые можно достать эти файлы, в угловых {https://pastebin.com/raw/CxK8TbMD, https://raw.githubusercontent.com/IgorTimofeev/MineOS/master/Installer/BIOS.lua, https://pastebin.com/raw/ABOBASOSI}
  5.  
  6. local component     = require("component")
  7. local computer      = require("computer")
  8. local shell         = require("shell")
  9. local filesystem    = require("filesystem")
  10.  
  11. local isParserExist = false
  12.  
  13. if (filesystem.exists("home\\Parser\\parser.lua")) then
  14.     isParserExist = true
  15.     StrParser = require("Parser/parser")
  16. else
  17.     print("Parser file is not found. using own")
  18.     isParserExist = false
  19.     Parser = {}
  20.     StrParser = Parser
  21. end
  22.  
  23. Parser.getWordsInStringArray = function(string, separator, stop)
  24.     local word          = ""
  25.     local words_array   = {}
  26.     for i in string:gmatch"." do
  27.  
  28.         if i ~= separator then
  29.             if i ~= ' ' and i ~= '\t' and i ~= '\n' and i ~= '\r' then
  30.                 word = word..i
  31.             end
  32.         else
  33.             table.insert(words_array, word)
  34.             word = ""
  35.         end
  36.  
  37.     end
  38.     return words_array
  39. end
  40.  
  41. Parser.getWordsInTable = function(array, separator)
  42.     local word          = ""
  43.     local words_array   = {}
  44.     for _, ii in pairs(array) do
  45.         for i in ii:gmatch"." do
  46.             if i ~= separator then
  47.                 if i ~= ' ' and i ~= '\t' and i ~= '\n' and i ~= '\r' then
  48.                     word = word..i
  49.                 end
  50.             else
  51.             end
  52.         end
  53.         if word ~= "" and word ~= nil then table.insert(words_array, word) end
  54.         word = ""
  55.     end
  56.     return words_array
  57. end
  58.  
  59. Parser.getStringArrayInBlock = function(arr, block_start, block_end)
  60.     local str       = ""
  61.     local to_read   = false
  62.     local str_out   = {}
  63.     if arr == nil then
  64.         error("[!] Get nil string on parser input")
  65.     end
  66.  
  67.     if block_start == nil or block_end == nil then
  68.         error("[!] nil block symbol on parser input")
  69.     end
  70.  
  71.     for i in arr:gmatch"." do   --- посимвольный перебор строки
  72.         if i == block_end then
  73.             to_read = false
  74.         end
  75.         if to_read and i ~= nil and i ~= '\t' and i ~= '\r' and i ~= '\n' and i ~= ' ' and i ~= ',' then
  76.             str = str..i
  77.         end
  78.         if i == block_start then
  79.             to_read = true
  80.         end
  81.         if i == ',' then
  82.             table.insert(str_out, str)
  83.             str = ""
  84.         end
  85.     end
  86.     table.insert(str_out, str)
  87.     return str_out
  88. end
  89.  
  90. local args, ops = shell.parse(...)
  91.  
  92. local temporaryFilesystemProxy, selectedFilesystemProxy
  93.  
  94. local function filesystemPath(path)
  95.     return path:match("^(.+%/).") or ""
  96. end
  97.  
  98. local function filesystemName(path)
  99.     return path:match("%/?([^%/]+%/?)$")
  100. end
  101.  
  102. local function filesystemHideExtension(path)
  103.     return path:match("(.+)%..+") or path
  104. end
  105.  
  106. local function getComponentAddress(name)
  107.     return component.list(name)() or error("Required " .. name .. " component is missing")
  108. end
  109.  
  110. local function getComponentProxy(name)
  111.     return component.proxy(getComponentAddress(name))
  112. end
  113.  
  114. local EEPROMProxy, internetProxy, GPUProxy =
  115.     getComponentProxy("eeprom"),
  116.     getComponentProxy("internet"),
  117.     getComponentProxy("gpu")
  118.  
  119. local function readHandle(handle)
  120.     local chunk = nil
  121.     local data  = ""
  122.         while true do
  123.             chunk = handle.read(math.huge)
  124.             if chunk then
  125.                 data = data .. chunk
  126.             else
  127.                 break
  128.             end
  129.         end
  130.     return data
  131. end
  132.  
  133. local function rawRequest(url, chunkHandler)
  134.     --local internetHandle, reason = internetProxy.request(repositoryURL or "" .. url:gsub("([^%w%-%_%.%~])", function(char)
  135.     local internetHandle, reason = internetProxy.request(url)
  136.  
  137.     if internetHandle then
  138.         local chunk, reason
  139.         while true do
  140.             chunk, reason = internetHandle.read(math.huge)
  141.             if chunk then
  142.                 chunkHandler(chunk)
  143.             else
  144.                 if reason then
  145.                     error("Internet request failed: " .. tostring(reason))
  146.                 end
  147.  
  148.                 break
  149.             end
  150.         end
  151.  
  152.         internetHandle.close()
  153.     else
  154.         error("Connection failed: " .. url)
  155.     end
  156. end
  157.  
  158. local function request(url)
  159.     local data = ""
  160.    
  161.     rawRequest(url, function(chunk)
  162.         data = data .. chunk
  163.     end)
  164.  
  165.     return data
  166. end
  167.  
  168. local function download(url, path)
  169.     selectedFilesystemProxy.makeDirectory(filesystemPath(path))
  170.  
  171.     local fileHandle, reason = selectedFilesystemProxy.open(path, "wb")
  172.  
  173.     if fileHandle then
  174.         rawRequest(url,
  175.             function(chunk)
  176.                 selectedFilesystemProxy.write(fileHandle, chunk)
  177.             end)
  178.         selectedFilesystemProxy.close(fileHandle)
  179.     else
  180.         error("File opening failed: " .. tostring(reason))
  181.     end
  182. end
  183.  
  184. local function deserialize(text)
  185.     local result, reason = load("return " .. text, "=string")
  186.     if result then
  187.         return result()
  188.     else
  189.         error(reason)
  190.     end
  191. end
  192.  
  193. for address in component.list("filesystem") do
  194.     local proxy = component.proxy(address)
  195.     if proxy.spaceTotal() >= 1 * 1024 * 1024 then
  196.         temporaryFilesystemProxy, selectedFilesystemProxy = proxy, proxy
  197.         break
  198.     end
  199. end
  200.  
  201.  
  202.  
  203.  
  204. if ops["f"] == true then
  205.     local filename = ""
  206.     for _, arg in pairs(args) do filename = tostring(arg) break end
  207.     FileData    = filesystem.open("home/"..filename,'r'):read(math.huge)
  208.     local urls  = StrParser.getStringArrayInBlock(FileData, '{', '}')
  209.     Url         = StrParser.getWordsInTable(urls, ',')
  210.     for index, value in ipairs(Url) do print(index .. '\t'.. " url " .. value) end
  211.     if Url == nil then error("[!] File ".. filename.." are hasn't url or file doesn't exist" ) end
  212. else
  213.     Url = tostring(args)
  214. end
  215.  
  216. if args == nil and ops["f"] ~= true then error("[!] Required github url or github url file and -f option") end
  217.  
  218. local names = StrParser.getStringArrayInBlock(FileData, '[', ']')
  219. names       = StrParser.getWordsInTable(names, ',')
  220. for k, v in pairs(Url) do download(v, "home/download/"..names[k]) end
  221.  
  222.  
  223. --guthub link example: https://raw.githubusercontent.com/sample/Project/master/file.lua
  224. --[[
  225.     [
  226.         robotcontrol.lua,
  227.         library.lua
  228.     ]
  229.     {
  230.         https://pastebin.com/raw/w4eTWFz3,
  231.         https://pastebin.com/raw/9ziy3Wn2
  232.     }
  233. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement