Advertisement
kaibochan

storage_controller.lua

Nov 7th, 2022 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.05 KB | None | 0 0
  1. local computers = {}
  2.  
  3. function GetComputers()
  4.     local peripherals = peripheral.getNames()
  5.    
  6.     for k, p in ipairs(peripherals) do
  7.         local i, j = string.find(p, "computer")
  8.        
  9.         if i and j then
  10.             table.insert(computers, peripheral.wrap(p))
  11.         end
  12.     end
  13. end
  14.  
  15. function ShutdownSystem()
  16.     for k, cpu in ipairs(computers) do
  17.         cpu.shutdown()
  18.     end
  19. end
  20.  
  21. function TurnOnSystem()
  22.     for k, cpu in ipairs(computers) do
  23.         cpu.turnOn()
  24.     end
  25. end
  26.  
  27. function RebootSystem()
  28.     ShutdownSystem()
  29.     sleep(5)
  30.     TurnOnSystem()
  31. end
  32.  
  33. --backup startup.lua
  34. local function backupSU()
  35.     if fs.exists("/disk/startup.lua") then
  36.         fs.copy("/disk/startup.lua", "/disk/startup.lua.bak")
  37.     end
  38. end
  39.  
  40. --replace temp startup.lua with backed up version
  41. local function restoreSU()
  42.     fs.delete("/disk/startup.lua")
  43.     fs.move("/disk/startup.lua.bak", "/disk/startup.lua")
  44. end
  45.  
  46. --reboot to have computers run temp startup.lua
  47. function ExecuteSU()
  48.     RebootSystem()
  49.     sleep(5)
  50. end
  51.  
  52. --all mass file operations are done by
  53. --editing disk/startup.lua and rebooting system
  54. function CopyToAll(filePath, fileName)
  55.     backupSU()
  56.    
  57.     --write copying code to temp startup.lua
  58.     local su = fs.open("/disk/startup.lua", "w")
  59.     su.write("fs.copy(\""..filePath.."\", \"/"..fileName.."\")")
  60.     su.close()
  61.  
  62.     ExecuteSU()
  63.     restoreSU()
  64. end
  65.  
  66. function DeleteFromAll(fileName)
  67.     backupSU()
  68.  
  69.     --write deletion code to temp startup.lua
  70.     local su = fs.open("/disk/startup.lua", "w")
  71.     su.write("fs.delete(\"/"..fileName.."\")")
  72.     su.close()
  73.  
  74.     ExecuteSU()
  75.     restoreSU()
  76. end
  77.  
  78. function RunArbitraryCode(filePath)
  79.     backupSU()
  80.  
  81.     --read in arbitrary code as string
  82.     local codeFile = fs.open(filePath, "r")
  83.     local code = codeFile.readAll()
  84.     codeFile.close()
  85.  
  86.     --write arbitrary code to temp startup.lua
  87.     local su = fs.open("/disk/startup.lua", "w")
  88.     su.write(code)
  89.     su.close()
  90.  
  91.     ExecuteSU()
  92.     restoreSU()
  93. end
  94.  
  95. GetComputers()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement