Advertisement
DOGGYWOOF

Untitled

Sep 7th, 2024 (edited)
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. local USERS_FOLDER = "/disk/users/"
  2. local WARNING_TIME = 5 -- Time in seconds to display the warning message
  3.  
  4. -- Function to draw a centered popup window with a warning message
  5. local function drawWarningWindow(message)
  6. term.clear() -- Clear the screen before drawing the warning window
  7.  
  8. local w, h = term.getSize()
  9. local maxLength = #message
  10. local windowWidth = maxLength + 4 -- Width of the warning window
  11. local windowHeight = 5 -- Height of the warning window (fixed for simplicity)
  12. local xStart = math.floor(w / 2 - windowWidth / 2)
  13. local yStart = math.floor(h / 2 - windowHeight / 2)
  14.  
  15. -- Draw border
  16. term.setCursorPos(xStart, yStart)
  17. term.write("+" .. string.rep("-", windowWidth - 2) .. "+")
  18. term.setCursorPos(xStart, yStart + 1)
  19. term.write("|" .. string.rep(" ", windowWidth - 2) .. "|")
  20. term.setCursorPos(xStart, yStart + 2)
  21. term.write("|" .. string.rep(" ", windowWidth - 2) .. "|")
  22. term.setCursorPos(xStart, yStart + 3)
  23. term.write("|" .. string.rep(" ", windowWidth - 2) .. "|")
  24. term.setCursorPos(xStart, yStart + 4)
  25. term.write("+" .. string.rep("-", windowWidth - 2) .. "+")
  26.  
  27. -- Draw message
  28. term.setCursorPos(xStart + 2, yStart + 2)
  29. term.write(message)
  30. end
  31.  
  32. -- Function to monitor for disk events
  33. local function monitorDisks()
  34. local lastDiskIDs = {} -- Store the last known disk IDs
  35.  
  36. -- Function to get current disk IDs
  37. local function getCurrentDiskIDs()
  38. local peripherals = peripheral.getNames()
  39. local diskIDs = {}
  40.  
  41. for _, name in ipairs(peripherals) do
  42. if peripheral.getType(name) == "drive" then
  43. local diskID = disk.getID(name)
  44. if diskID then
  45. table.insert(diskIDs, diskID)
  46. end
  47. end
  48. end
  49.  
  50. return diskIDs
  51. end
  52.  
  53. -- Initialize with the current disk IDs
  54. lastDiskIDs = getCurrentDiskIDs()
  55.  
  56. while true do
  57. os.sleep(1) -- Check every second
  58.  
  59. local currentDiskIDs = getCurrentDiskIDs()
  60. local diskRemoved = false
  61.  
  62. -- Check for disk removal
  63. for _, lastID in ipairs(lastDiskIDs) do
  64. local found = false
  65. for _, currentID in ipairs(currentDiskIDs) do
  66. if lastID == currentID then
  67. found = true
  68. break
  69. end
  70. end
  71. if not found then
  72. diskRemoved = true
  73. break
  74. end
  75. end
  76.  
  77. if diskRemoved then
  78. drawWarningWindow("Warning: Disk has been removed!")
  79. os.sleep(WARNING_TIME) -- Display the warning message for the specified time
  80.  
  81. -- Force logoff by running the logoff script
  82. shell.run("/disk/ACPI/logoff")
  83. break -- Exit the monitoring loop after logging off
  84. end
  85.  
  86. -- Update the last known disk IDs
  87. lastDiskIDs = currentDiskIDs
  88. end
  89. end
  90.  
  91. -- Function to start the monitoring script in a new shell and switch to it
  92. local function setupMultishell()
  93. -- Launch the monitoring script in a new shell
  94. local shellID = multishell.launch("CardSystem_Service", "card_service.lua")
  95.  
  96. -- Switch to the new shell
  97. multishell.setActive(shellID)
  98. end
  99.  
  100. -- Run the multishell setup and monitoring function
  101. setupMultishell()
  102. parallel.waitForAny(monitorDisks)
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement