nik1111

Untitled

May 19th, 2022 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.73 KB | None | 0 0
  1. --
  2. -- Created by Grcpils
  3. -- 03/14/2021
  4. -- Github: https://github.com/grcpils/cc-gui_api
  5. -- Please do not delete this header ;)
  6. --
  7.  
  8. require("table")
  9.  
  10. local args = {...}
  11.  
  12. local GUI_API_CDN_URL = "http://cdn.grcpils.fr/cc-files/GUI_API/"
  13. local GUI_API_VERSION_FILE = "version.lua"
  14. local GUI_API_DIRECTORY = "GUI"
  15. local GUI_API_DIRECTORY_TEMP = "GUI/tmp"
  16. local GUI_API_FILES = {
  17.     "init.lua",
  18.     "button.lua",
  19.     "group.lua",
  20.     "progress.lua",
  21.     "utils.lua",
  22.     "version.lua"
  23. }
  24. local GUI_API_HELP_MESSAGE = "Use one of the following argument:\n    -i or --install  Install the current version of the GUI_API\n    -f or --force    Force reinstall current version of the GUI_API\n    -u or --update   Check update\n    -r or --remove   Remove GUI_API from the computer"
  25.  
  26. local printf = function(s,...)
  27.     return io.write(s:format(...))
  28. end
  29.  
  30. function download(url, file)
  31.     local content = http.get(url).readAll()
  32.     if not content then
  33.       error("Could not connect to website")
  34.     end
  35.     f = fs.open(file, "w")
  36.     f.write(content)
  37.     f.close()
  38. end
  39.  
  40. function exist ()
  41.     if fs.exists(GUI_API_DIRECTORY) then
  42.         return false
  43.     elseif fs.exists(GUI_API_DIRECTORY.."/version.lua") then
  44.         return false
  45.     else
  46.         return true
  47.     end
  48. end
  49.  
  50. function install ()
  51.     if not exist() then
  52.         term.setTextColor(colors.red)
  53.         printf("[GUI] GUI already installed\n")
  54.         term.setTextColor(colors.white)
  55.         return
  56.     end
  57.     table.foreach(GUI_API_FILES, function(k,f)
  58.         printf("[GUI] Downloading %s ... ", f)
  59.         download(GUI_API_CDN_URL..f, GUI_API_DIRECTORY.."/"..f)
  60.         printf("done\n")
  61.     end)
  62.     if exist() then
  63.         term.setTextColor(colors.red)
  64.         printf("[GUI] Error with installation, please retry\n")
  65.         term.setTextColor(colors.white)
  66.         return
  67.     end
  68.     printf("[GUI] Install finished :)\n")
  69. end
  70.  
  71. function update ()
  72.     if not exist() then
  73.         download(GUI_API_CDN_URL.."version.lua", GUI_API_DIRECTORY_TEMP.."/version.lua")
  74.         local version = require(GUI_API_DIRECTORY.."/version")
  75.         local nversion = require(GUI_API_DIRECTORY_TEMP.."/version")
  76.         if version.get() == nversion.get() then
  77.             fs.delete(GUI_API_DIRECTORY_TEMP)
  78.             printf("[GUI] GUI is up-to-date ;)\n")
  79.         else
  80.             printf("[GUI] new version is available (%s)\n[GUI] Do you want download this update ? (y, n)\n", nversion.get())
  81.             input = read()
  82.             if input == "y" or input == "Y" then
  83.                 printf("[GUI] Starting update :)\n")
  84.                 remove()
  85.                 install()
  86.             else
  87.                 fs.delete(GUI_API_DIRECTORY_TEMP)
  88.                 printf("[GUI] Update canceled :(\n")
  89.                 return
  90.             end
  91.         end
  92.     end
  93. end
  94.  
  95. function version()
  96.     local version = require(GUI_API_DIRECTORY.."/version")
  97.     printf("[GUI] Version: %s\n", version.get())
  98. end
  99.  
  100. function remove()
  101.     if not exist() then
  102.         fs.delete(GUI_API_DIRECTORY)
  103.         printf("[GUI] gui_api succesfully removed\n")
  104.     else
  105.         term.setTextColor(colors.red)
  106.         printf("[GUI] Error: no GUI_API installation found\n")
  107.         term.setTextColor(colors.white)
  108.     end
  109. end
  110.  
  111. function init ()
  112.     if args[1] == "-i" or args[1] == "--install" then
  113.         install()
  114.     elseif args[1] == "-f" or args[1] == "--force" then
  115.         remove()
  116.         install()
  117.     elseif args[1] == "-u" or args[1] == "--update" then
  118.         update()
  119.     elseif args[1] == "-r" or args[1] == "--remove" then
  120.         remove()
  121.     elseif args[1] == "-v" or args[1] == "--version" then
  122.         version()
  123.     else
  124.         print(GUI_API_HELP_MESSAGE)
  125.     end
  126. end
  127.  
  128. init()
Add Comment
Please, Sign In to add comment