Advertisement
monster010

CC - Debiget [CC: Tweaked]

May 13th, 2021
1,399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Debiget - (c) monster010
  2. local pUrl = "http://pastebin.com/raw/"
  3.  
  4. local debiget_path = '/debiget.d'
  5. local debiget_backuppath = debiget_path..'/backup'
  6. local debiget_installpath = debiget_path..'/installed.list'
  7.  
  8. local debiget_sources = {}
  9. local debiget_installed = {}
  10.  
  11. local tArgs = { ... }
  12. local peripSides = peripheral.getNames()
  13.  
  14.  
  15. if not http or not http.checkURL(pUrl) then
  16.     print("Bei dir ist in der ComputerCraft-config http nicht aktiviert.")
  17.     print("Stelle sicher das 'pastebin.com' in der http.whitelist eingetragen ist.")
  18.     print("Aktiviere es, starte Minecraft neu und fuehre debiget erneut aus.")
  19.     return
  20. end
  21.  
  22.  
  23.  
  24. local function clear()
  25.     term.clear()
  26.     term.setCursorPos(1,1)
  27.     term.setTextColor(colors.white)
  28. end
  29.  
  30.  
  31.  
  32. function table.removeKey(tables, key)
  33.     local i = 0
  34.     local ks, vs = {}, {}
  35.  
  36.     for k, v in pairs(tables) do
  37.         i = i + 1
  38.         ks[i] = k
  39.         vs[i] = v
  40.     end
  41.  
  42.     while i > 0 do
  43.         if ks[i] == key then
  44.             table.remove(ks, i)
  45.             table.remove(vs, i)
  46.             break
  47.         end
  48.  
  49.         i = i - 1
  50.     end
  51.  
  52.     local a = {}
  53.     for i = 1, #ks do
  54.         a[ks[i]] = vs[i]
  55.     end
  56.  
  57.     return a
  58. end
  59.  
  60.  
  61.  
  62. local function loadSources()
  63.     local request, err = http.get(pUrl.."01W8xQyC")
  64.  
  65.     if not request then exit("Fehler:", err) end
  66.  
  67.     if request then
  68.         debiget_sources = textutils.unserialize(request.readAll())
  69.         request.close()
  70.     else
  71.         exit("Konnte sources.list nicht aufrufen! Bitte kontaktiere monster8570 auf Twitter oder YouTube!")
  72.     end
  73. end
  74.  
  75.  
  76.  
  77. local function showDebiget()
  78.     print("----- Debiget -----")
  79.     print("-- by monster010 --")
  80.     print()
  81. end
  82.  
  83. local function showSources()
  84.     for name, data in pairs(debiget_sources) do
  85.         term.setTextColor(colors.green)
  86.         write(name)
  87.         term.setTextColor(colors.white)
  88.         write(' -- ')
  89.         term.setTextColor(colors.lightBlue)
  90.         write(data["desc"].."\n")
  91.         term.setTextColor(colors.white)
  92.     end
  93. end
  94.  
  95. local function showInstalled()
  96.     for name, data in pairs(debiget_installed) do
  97.         term.setTextColor(colors.green)
  98.         write(name)
  99.         term.setTextColor(colors.white)
  100.         write(' -- ')
  101.         term.setTextColor(colors.lightBlue)
  102.         write('v.'..data["version"].."\n")
  103.         term.setTextColor(colors.white)
  104.     end
  105. end
  106.  
  107. local function showHelp()
  108.     print("debiget help               -- Zeigt dieses Hilfemenu an")
  109.     print("debiget install <programm> -- Installieren von Programmen")
  110.     print("debiget update <programm>  -- Updaten von Programmen")
  111.     print("debiget remove <programm>  -- Entfernen von Programmen")
  112.     print("debiget config <programm>  -- Zum Teilkonfigurieren")
  113. end
  114.  
  115.  
  116.  
  117. local function readInput()
  118.     term.setTextColor(colors.yellow)
  119.     write("\n>")
  120.     term.setTextColor(colors.white)
  121.  
  122.     local input = read():lower()
  123.  
  124.     return input
  125. end
  126.  
  127. local function checkInput(table, input)
  128.     local selected = false
  129.  
  130.     for name, data in pairs(table) do
  131.         if not selected then
  132.             if input == name then
  133.                 selected = true
  134.             else
  135.                 selected = false
  136.             end
  137.         end
  138.     end
  139.  
  140.     return selected
  141. end
  142.  
  143. local function checkInputNum(table, input, text)
  144.     local exists = false
  145.  
  146.     for i = 1, #table do
  147.         if input == table[i] then
  148.             exists = true
  149.         end
  150.     end
  151.  
  152.     if exists == false then
  153.         print(text)
  154.         checkInputNum(table, input)
  155.     else
  156.         return input
  157.     end
  158. end
  159.  
  160. local function exit(text, err)
  161.     if not err then err = false end
  162.  
  163.     if err then
  164.         term.setTextColor(colors.red)
  165.     else
  166.         term.setTextColor(colors.green)
  167.     end
  168.     write(text.."\n")
  169.     term.setTextColor(colors.white)
  170.     error()
  171. end
  172.  
  173. local function confirm()
  174.     local input = read():lower()
  175.  
  176.     if input == "j" or input == "ja" then
  177.         return true
  178.     else
  179.         return false
  180.     end
  181. end
  182.  
  183.  
  184.  
  185. local function loadInstall()
  186.     if fs.exists(debiget_installpath) then
  187.         local file = fs.open(debiget_installpath, "r")
  188.         debiget_installed = textutils.unserialize(file.readAll())
  189.         file.close()
  190.     end
  191. end
  192.  
  193. local function saveInstall()
  194.     local file = fs.open(debiget_installpath, "w")
  195.     file.write(textutils.serialize(debiget_installed))
  196.     file.close()
  197. end
  198.  
  199. local function setInstallProg(table, program)
  200.     debiget_installed[program] = table
  201.     saveInstall()
  202. end
  203.  
  204. local function setInstall(program, key, val)
  205.     debiget_installed[program][key] = val
  206.     saveInstall()
  207. end
  208.  
  209. local function addInstall(table, program)
  210.     debiget_installed[program] = table
  211.     saveInstall()
  212. end
  213.  
  214. local function removeInstall(program)
  215.     debiget_installed = table.removeKey(debiget_installed, program)
  216.     saveInstall()
  217. end
  218.  
  219. local function checkInstall(programm)
  220.     if debiget_installed[programm] ~= nil then
  221.         return true
  222.     end
  223.  
  224.     return false
  225. end
  226.  
  227. local function startupInstall(program)
  228.     if debiget_sources[program] == nil then
  229.         exit("Programm '"..program.."' existiert nicht!", true)
  230.     elseif debiget_installed[program] == nil then
  231.         exit("Programm '"..program.."' ist nicht installiert!", true)
  232.     end
  233.  
  234.     local startup = debiget_sources[program]["startup"]
  235.  
  236.     if fs.exists('startup') then
  237.         if fs.exists('startup-old') then
  238.             fs.delete('startup-old')
  239.         end
  240.         fs.move('startup', 'startup-old')
  241.     end
  242.  
  243.     local sfile = fs.open("startup", "w")
  244.     sfile.write(startup)
  245.     sfile.close()
  246. end
  247.  
  248.  
  249.  
  250. local function checkSides(pType)
  251.     local rSides = {}
  252.     local nSides = 0
  253.  
  254.     for i = 1, #peripSides do
  255.         if peripheral.getType(peripSides[i]) == pType then
  256.             nSides = nSides + 1
  257.             rSides[nSides] = peripSides[i]
  258.  
  259.             write(peripSides[i])
  260.  
  261.             if i ~= #peripSides then
  262.                 write(", ")
  263.             end
  264.         end
  265.     end
  266.  
  267.     return rSides
  268. end
  269.  
  270.  
  271.  
  272. local function writeFile(path, content)
  273.     local file = fs.open(path, 'w')
  274.     file.write(content)
  275.     file.close()
  276. end
  277.  
  278. local function writeFiles(files)
  279.     for path, content in pairs(files) do
  280.         writeFile(path, content)
  281.     end
  282. end
  283.  
  284. local function backupFile(path, name)
  285.     if fs.exists(path..'/'..name) then
  286.         fs.move(path..name, debiget_backuppath..name)
  287.     end
  288. end
  289.  
  290. local function restoreFile(path, name)
  291.     if fs.exists(debiget_backuppath..'/'..name) then
  292.         fs.move(debiget_backuppath..name, path..name)
  293.     end
  294. end
  295.  
  296. local function downloadFile(name, pid, url)
  297.     local download, err = http.get(url)
  298.     local file
  299.  
  300.     if not download then exit("Fehler: ", err) end
  301.  
  302.     if download then
  303.         print("Fetching "..name.." ("..pid..")")
  304.         file = download.readAll()
  305.         download.close()
  306.         return file
  307.     else
  308.         exit("Konnte "..name.." ("..pid..") nicht herunterladen!\nHerunterladen fehlgeschlagen!\n", true)
  309.     end
  310. end
  311.  
  312.  
  313.  
  314. local function install(program)
  315.     local filelist = debiget_sources[program]["files"]
  316.     local path = debiget_sources[program]["path"]
  317.     local name = debiget_sources[program]["name"]
  318.     local files = {}
  319.  
  320.     if fs.exists(path) or checkInstall(program) then
  321.         exit("Programm "..name.." ist bereits installiert!", true)
  322.     else
  323.         fs.makeDir(path)
  324.     end
  325.  
  326.     print("Installiere "..name.."...")
  327.  
  328.     for __, data in pairs(filelist) do
  329.         local url = pUrl..data["pid"]
  330.         local lpath = path..'/'..data["name"]
  331.         local download = downloadFile(data["name"], data["pid"], url)
  332.  
  333.         files[lpath] = download
  334.     end
  335.  
  336.     writeFiles(files)
  337.     addInstall(debiget_sources[program], program)
  338.  
  339.     startupInstall(program)
  340.  
  341.     exit("Installation von "..name.." abgeschlossen!")
  342. end
  343.  
  344. local function update(program)
  345.     if debiget_sources[program] == nil then
  346.         exit("Programm '"..program.."' existiert nicht!", true)
  347.     elseif debiget_installed[program] == nil then
  348.         exit("Programm '"..program.."' ist nicht installiert!", true)
  349.     end
  350.  
  351.     local filelist = debiget_sources[program]["files"]
  352.     local path = debiget_installed[program]["path"]
  353.     local name = debiget_installed[program]["name"]
  354.     local cfgs = debiget_sources[program]["cfgs"]
  355.     local lversion = debiget_installed[program]["version"]
  356.     local sversion = debiget_sources[program]["version"]
  357.     local files = {}
  358.  
  359.     if lversion == sversion then
  360.         exit(name.." ist bereits auf der neusten Version!")
  361.     end
  362.  
  363.     print("Update "..name.."...")
  364.  
  365.     if #cfgs ~= 0 then
  366.         print("Erstelle Backup von "..name.."...")
  367.  
  368.         for i=1, #cfgs do
  369.             backupFile(path, cfgs[i])
  370.         end
  371.  
  372.         term.setTextColor(colors.green)
  373.         print("Backup wurde erstellt!")
  374.         term.setTextColor(colors.white)
  375.     end
  376.  
  377.     fs.delete(path)
  378.     fs.makeDir(path)
  379.  
  380.     for __, data in pairs(filelist) do
  381.         local url = pUrl..data["pid"]
  382.         local lpath = path..'/'..data["name"]
  383.         local download = downloadFile(data["name"], data["pid"], url)
  384.  
  385.         files[lpath] = download
  386.     end
  387.  
  388.     writeFiles(files)
  389.  
  390.     if #cfgs ~= 0 then
  391.         print("Stelle Backup von "..name.." wieder her...")
  392.  
  393.         for i=1, #cfgs do
  394.             restoreFile(path, cfgs[i])
  395.         end
  396.  
  397.         term.setTextColor(colors.green)
  398.         print("Backup wurde wiederhergestellt!")
  399.         term.setTextColor(colors.white)
  400.     end
  401.  
  402.     setInstallProg(debiget_sources[program], program)
  403.  
  404.     exit("Update von "..name.." abgeschlossen!")
  405. end
  406.  
  407. local function remove(program)
  408.     if debiget_sources[program] == nil then
  409.         exit("Programm '"..program.."' existiert nicht!", true)
  410.     elseif debiget_installed[program] == nil then
  411.         exit("Programm '"..program.."' ist nicht installiert!", true)
  412.     end
  413.  
  414.     local path = debiget_installed[program]["path"]
  415.     local name = debiget_installed[program]["name"]
  416.  
  417.     print("Deinstalliere "..name.."...")
  418.  
  419.     if fs.exists(path) then
  420.         fs.delete(path)
  421.     end
  422.  
  423.     removeInstall(program)
  424.  
  425.     exit("Deinstallation von "..name.." abgeschlossen!")
  426. end
  427.  
  428. local function config(program)
  429.     if debiget_sources[program] == nil then
  430.         exit("Programm '"..program.."' existiert nicht!", true)
  431.     elseif debiget_installed[program] == nil then
  432.         exit("Programm '"..program.."' ist nicht installiert!", true)
  433.     end
  434.  
  435.     local path = debiget_installed[program]["path"]
  436.     local name = debiget_installed[program]["name"]
  437.     local needs = debiget_installed[program]["needs"]
  438.     local dataCfg = {}
  439.     local cfg = {}
  440.  
  441.     print("Konfiguration von "..name.."...")
  442.  
  443.     if needs["monitor"] == true then
  444.         print("Auf welcher Seite befindet sich der Monitor?")
  445.         write("Moegliche Seiten sind ")
  446.  
  447.         local rSides = checkSides("monitor")
  448.  
  449.         if #rSides == 1 then
  450.             cfg["monSide"] = rSides[1]
  451.             print("Benutze Monitor "..rSides[1])
  452.         elseif #rSides == 0 then
  453.             exit("Kein Monitor vorhanden!", true)
  454.         else
  455.             local input = readInput()
  456.             local exists = checkInputNum(rSides, input, "An der angegeben Seite ist kein Monitor angeschlossen!")
  457.  
  458.             cfg["monSide"] = exists
  459.         end
  460.     end
  461.  
  462.     if needs["redstone"] == true then
  463.         print("Auf welcher Seite befindet sich das BundledCable?")
  464.         term.setTextColor(colors.blue)
  465.         print("Info: Es kann nicht geprueft werden, ob sich an der angegebenen Seite ein BundledCable befindet!")
  466.  
  467.         local input = readInput()
  468.         cfg["redSide"] = input
  469.     end
  470.  
  471.     if needs["modem"] == true then
  472.         print("Auf welcher Seite befindet sich das Modem?")
  473.         write("Moegliche Seiten sind ")
  474.  
  475.         local rSides = checkSides("modem")
  476.  
  477.         if #rSides == 1 then
  478.             cfg["modSide"] = rSides[1]
  479.             print("Benutze Modem "..rSides[1])
  480.         elseif #rSides == 0 then
  481.             exit("Kein Modem vorhanden!", true)
  482.         else
  483.             local input = readInput()
  484.             local exists = checkInputNum(rSides, input, "An der angegeben Seite ist kein Modem angeschlossen!")
  485.  
  486.             cfg["modSide"] = exists
  487.         end
  488.     end
  489.  
  490.     if fs.exists(path..'/'..'cfg') then
  491.         local rFile = fs.open(path..'/'..'cfg', "r")
  492.         dataCfg = textutils.unserialize(rFile.readAll())
  493.         rFile.close()
  494.     end
  495.  
  496.     dataCfg["monSide"] = cfg["monSide"]
  497.     dataCfg["redSide"] = cfg["redSide"]
  498.     dataCfg["modSide"] = cfg["modSide"]
  499.  
  500.     local wFile = fs.open(path..'/'..'cfg', "w")
  501.     wFile.write(textutils.serialize(dataCfg))
  502.     wFile.close()
  503.  
  504.     exit("Konfiguration von "..name.." abgeschlossen!")
  505. end
  506.  
  507.  
  508.  
  509.  
  510. --showDebiget()
  511. loadSources()
  512. loadInstall()
  513.  
  514. if #tArgs <= 0 then
  515.     showHelp()
  516. end
  517.  
  518. if #tArgs >= 1 and tArgs[1] == "help" then
  519.     showHelp()
  520. elseif #tArgs >= 1 and tArgs[1] == "install" then
  521.     local installFound = false
  522.     local input
  523.  
  524.     if #tArgs == 2 then
  525.         input = tArgs[2]:lower()
  526.     else
  527.         print("Folgende Programme koennen installiert werden:")
  528.         showSources()
  529.         input = readInput()
  530.     end
  531.  
  532.     installFound = checkInput(debiget_sources, input)
  533.  
  534.     if installFound == false then
  535.         if input == "" then
  536.             exit("Es wurde kein Programm angegeben!", true)
  537.         else
  538.             exit("Es konnte kein Programm Names '"..input.."' gefunden werden!", true)
  539.         end
  540.     else
  541.         install(input)
  542.     end
  543. elseif #tArgs >= 1 and tArgs[1] == "remove" then
  544.     local progFound = false
  545.     local input
  546.  
  547.     if #tArgs == 2 then
  548.         input = tArgs[2]:lower()
  549.     else
  550.         print("Folgende Programme koennen entfernt werden:")
  551.         showInstalled()
  552.         input = readInput()
  553.     end
  554.  
  555.     progFound = checkInput(debiget_installed, input)
  556.  
  557.     if progFound == false then
  558.         if input == "" then
  559.             exit("Es wurde kein Programm angegeben!", true)
  560.         else
  561.             exit("Es konnte kein Programm Names '"..input.."' gefunden werden!", true)
  562.         end
  563.     else
  564.         remove(input)
  565.     end
  566. elseif #tArgs >= 1 and tArgs[1] == "update" then
  567.     local progFound = false
  568.     local input
  569.  
  570.     if #tArgs == 2 then
  571.         input = tArgs[2]:lower()
  572.     else
  573.         print("Folgende Programme koennen geupdatet werden:")
  574.         showInstalled()
  575.         input = readInput()
  576.     end
  577.  
  578.     progFound = checkInput(debiget_installed, input)
  579.  
  580.     if progFound == false then
  581.         if input == "" then
  582.             exit("Es wurde kein Programm angegeben!", true)
  583.         else
  584.             exit("Es konnte kein Programm Names '"..input.."' gefunden werden!", true)
  585.         end
  586.     else
  587.         update(input)
  588.     end
  589. elseif #tArgs >= 1 and tArgs[1] == "config" then
  590.     local progFound = false
  591.     local input
  592.  
  593.     if #tArgs == 2 then
  594.         input = tArgs[2]:lower()
  595.     else
  596.         print("Folgende Programme koennen konfiguriert werden:")
  597.         showInstalled()
  598.         input = readInput()
  599.     end
  600.  
  601.     progFound = checkInput(debiget_installed, input)
  602.  
  603.     if progFound == false then
  604.         if input == "" then
  605.             exit("Es wurde kein Programm angegeben!", true)
  606.         else
  607.             exit("Es konnte kein Programm Names '"..input.."' gefunden werden!", true)
  608.         end
  609.     else
  610.         config(input)
  611.     end
  612. elseif #tArgs >= 1 then
  613.     exit("Der Befehl "..tArgs[1].." wurde nicht gefunden!", true)
  614. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement