Advertisement
Guest User

Reactor

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