Advertisement
Larvix

Framework

Feb 6th, 2024 (edited)
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.73 KB | None | 0 0
  1. local findFile = function(name)
  2.     if not name or type(name) ~= "string" then
  3.         error("Argument #1 when getting path: Requires string file name, got '"..type(name).."'.")
  4.     end
  5.     local gotPath = false
  6.     local files = fs.list("//")
  7.     for _,path in pairs(files) do
  8.         if fs.getName(path) == name or path == name then
  9.             return path
  10.         elseif fs.isDir(path) and path ~= "rom" then
  11.             local dir = fs.list(path)
  12.             for _,file in pairs(dir) do
  13.                 table.insert(files,path.."/"..file)
  14.             end
  15.         end
  16.     end
  17. end
  18.  
  19. local loadFile = function(path)
  20.     local data = nil
  21.     if fs.exists(path) then
  22.         local fileHandle = fs.open(path,"r")
  23.         data = fileHandle.readAll()
  24.         fileHandle.close()
  25.         local table = textutils.unserialise(data)
  26.         if type(table) == "table" then
  27.             return table
  28.         end
  29.     end
  30.     return data
  31. end
  32.  
  33. saveFile = function(path, data)
  34.     if type(data) == "table" then
  35.         data = textutils.serialise(data)
  36.     end
  37.     local fileHandle = fs.open(path,"w")
  38.     fileHandle.write(data)
  39.     fileHandle.close()
  40. end
  41.  
  42. local getMonitors = function()
  43.     local monitors = {}
  44.     for _,side in pairs({"top","bottom","left","right","front","back"}) do
  45.         if peripheral.getType(side) == "monitor" then
  46.             table.insert(monitors,side)
  47.         elseif peripheral.getType(side) == "modem" then
  48.             local check = peripheral.wrap(side)
  49.             if not check.isWireless() then
  50.                 wired = check.getNamesRemote()
  51.                 for _,name in pairs(wired) do
  52.                     if check.getTypeRemote(name) == "monitor" then
  53.                         table.insert(monitors,name)
  54.                     end
  55.                 end
  56.             end
  57.         end
  58.     end
  59.     return monitors
  60. end
  61.  
  62. return {
  63. findFile = findFile,
  64. loadFile = loadFile,
  65. saveFile = saveFile,
  66. monitors = getMonitors,
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement