Advertisement
Ocawesome101

Open Kernel Installer

Feb 10th, 2020
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.99 KB | None | 0 0
  1. -- Should, in theory, install Open Kernel to a selected medium. --
  2.  
  3. local args = {...}
  4.  
  5. -- Multi-platform compat right here
  6. local term = term or require("term")
  7. local read = read or io.read
  8. local http = http or internet or require("component").internet
  9. local fs = fs or require("filesystem")
  10.  
  11. local write = write or term.write
  12.  
  13. local path = args[1] or nil
  14.  
  15. local function install_computerCraft(path)
  16.   error("ComputerCraft support is not implemented yet")
  17. end
  18.  
  19. local function install_openComputers(path)
  20.   local url = "https://raw.githubusercontent.com/ocawesome101/open-kernel/master/files.list"
  21.   local function download(URL)
  22.     local h
  23.     if http.request then
  24.       h = http.request(URL)
  25.       h.finishConnect()
  26.     else
  27.       h = http.get(URL)
  28.     end
  29.     return h
  30.   end
  31.   local handle = download(url)
  32.   local data = ""
  33.   repeat
  34.     local chunk = handle.read(0xFFFF)
  35.     data = data .. (chunk or "")
  36.   until not chunk
  37.  
  38.   handle:close()
  39.  
  40.   local lines = {}
  41.   local word = ""
  42.   -- Split the files list into lines
  43.   for char in string.gmatch(data, ".") do
  44.     if char == "\n" then
  45.       table.insert(lines, word)
  46.       word = ""
  47.     else
  48.       word = word .. char
  49.     end
  50.   end
  51.   -- Finally, download the files
  52.   local baseURL = "https://raw.githubusercontent.com/ocawesome101/open-kernel/master/"
  53.   for i=1, #lines, 1 do
  54.     if lines[i]:sub(-1) == "/" then
  55.       print("Creating " .. path .. "/" .. lines[i])
  56.       fs.makeDirectory(path .. lines[i])
  57.     else
  58.       print("Downloading " .. lines[i])
  59.       local handle = download(baseURL .. lines[i])
  60.       local data = ""
  61.       repeat
  62.         local chunk = handle.read(0xFFFF)
  63.         data = data .. (chunk or "")
  64.       until not chunk
  65.  
  66.       handle:close()
  67.  
  68.       print("Writing " .. lines[i])
  69.       local handle = fs.open(path .. "/" .. lines[i], "w")
  70.       if fs.proxy then
  71.         handle:write(data)
  72.         handle:close()
  73.       else
  74.         handle.write(data)
  75.         handle.close()
  76.       end
  77.     end
  78.   end
  79.   fs.setLabel(path, "Open Kernel")
  80. end
  81.  
  82. local mounts = fs.list("/mnt")
  83. local choice
  84. while true do
  85.   print("Please select an install medium.")
  86.   if type(mounts) == "function" then
  87.     local m = {}
  88.     for mount in mounts do
  89.       print(i, "/mnt/" .. mount)
  90.       table.insert(m, mount)
  91.     end
  92.     mounts = m
  93.   else
  94.     for i=1, #mounts, 1 do
  95.       print(i, "/mnt/" .. mounts[i])
  96.     end
  97.   end
  98.  
  99.   write("> ")
  100.  
  101.   local input = read()
  102.   if not mounts[tonumber(input)] then
  103.     print("Invalid selection")
  104.   else
  105.     choice = tonumber(input)
  106.     break
  107.   end
  108. end
  109.  
  110. local selection = path or "/mnt/" .. mounts[choice]
  111.  
  112. print("You have selected " .. selection .. ". Continue?")
  113. write("[y/N]: ")
  114.  
  115. local input = read()
  116.  
  117. if input:lower() ~= "y" then
  118.   print("Answer was not yes, assuming no. Have a good day.")
  119.   return false
  120. end
  121.  
  122. local platform = 1
  123.  
  124. if platform == 1 then
  125.   install_openComputers(selection)
  126. elseif platform == 2 then
  127.   install_computerCraft(selection)
  128. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement