ddlcforever

Untitled

Sep 16th, 2025 (edited)
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.35 KB | None | 0 0
  1. -- Packages/libs
  2. package.path = debug.getinfo(1,"S").source:sub(2):match("(.*/sys/)") .. "?.lua;" .. debug.getinfo(1,"S").source:sub(2):match("(.*/sys/)") .. "?/init.lua;" .. package.path
  3. local pm = require("api.peripheral_manager")
  4.  
  5. -- peripherals
  6. local mon = pm.first("monitor")
  7. local speaker = pm.first("speaker")
  8.  
  9. -- Function to check if a disk drive (excluding drive_02) has a disk
  10. local function hasUserDisk()
  11.     local drives = pm.find("drive")
  12.     for _, drive in ipairs(drives) do
  13.         -- Skip drive_02 (the source disk drive)
  14.         if drive.name ~= "drive_02" then
  15.             if drive.isDiskPresent() then
  16.                 return drive
  17.             end
  18.         end
  19.     end
  20.     return nil
  21. end
  22.  
  23. -- Function to label the disk and copy files from drive_02
  24. local function processDisk(userDrive)
  25.     if not userDrive then return false end
  26.    
  27.     -- Label the disk
  28.     local success, err = pcall(function()
  29.         userDrive.setDiskLabel("USER")
  30.     end)
  31.    
  32.     if not success then
  33.         mon.write("Error labeling disk: " .. tostring(err))
  34.         return false
  35.     end
  36.    
  37.     -- Get the source drive (drive_02)
  38.     local sourceDrive = pm.get("drive_02")
  39.     if not sourceDrive or not sourceDrive.isDiskPresent() then
  40.         mon.write("Source disk not found in drive_02!")
  41.         return false
  42.     end
  43.    
  44.     -- Mount paths for both drives
  45.     local sourceMountPath = sourceDrive.getMountPath() or "disk_02"
  46.     local userMountPath = userDrive.getMountPath() or "disk"
  47.    
  48.     -- Copy file from source disk to user disk
  49.     local sourcePath = fs.combine(sourceMountPath, "sys/template/music.lua")
  50.     local destPath = fs.combine(userMountPath, "sys/bin/music.lua")
  51.    
  52.     -- Check if source file exists
  53.     if not fs.exists(sourcePath) then
  54.         mon.write("Source file not found on template disk!")
  55.         return false
  56.     end
  57.    
  58.     -- Create destination directory if it doesn't exist
  59.     local destDir = fs.combine(userMountPath, "sys/bin")
  60.     if not fs.exists(destDir) then
  61.         fs.makeDir(destDir)
  62.     end
  63.    
  64.     -- Copy the file
  65.     local success, err = pcall(function()
  66.         local sourceFile = fs.open(sourcePath, "r")
  67.         local destFile = fs.open(destPath, "w")
  68.        
  69.         if sourceFile and destFile then
  70.             local content = sourceFile.readAll()
  71.             destFile.write(content)
  72.             sourceFile.close()
  73.             destFile.close()
  74.             return true
  75.         else
  76.             if sourceFile then sourceFile.close() end
  77.             if destFile then destFile.close() end
  78.             return false
  79.         end
  80.     end)
  81.    
  82.     if not success then
  83.         mon.write("Error copying file: " .. tostring(err))
  84.         return false
  85.     end
  86.    
  87.     return true
  88. end
  89.  
  90. -- Function to eject disk from a specific drive
  91. local function ejectDisk(drive)
  92.     if drive then
  93.         drive.ejectDisk()
  94.     end
  95. end
  96.  
  97. -- Main loop
  98. while true do
  99.     mon.clear()
  100.     mon.setCursorPos(1,1)
  101.     mon.write("Welcome user!")
  102.     mon.setCursorPos(1,2)
  103.     mon.write("Please place a floppy disk")
  104.     mon.setCursorPos(1,3)
  105.     mon.write("in the disk drive below.")
  106.    
  107.     -- Wait for disk to be inserted (excluding drive_02)
  108.     local userDrive = nil
  109.     while not userDrive do
  110.         userDrive = hasUserDisk()
  111.         if not userDrive then
  112.             sleep(0.5)
  113.         end
  114.     end
  115.    
  116.     -- Disk detected, process it
  117.     mon.clear()
  118.     mon.setCursorPos(1,1)
  119.     mon.write("Disk detected in " .. userDrive.name .. "!")
  120.     mon.setCursorPos(1,2)
  121.     mon.write("Processing...")
  122.    
  123.     if processDisk(userDrive) then
  124.         mon.setCursorPos(1,3)
  125.         mon.write("Disk labeled: USER")
  126.         mon.setCursorPos(1,4)
  127.         mon.write("File copied successfully!")
  128.        
  129.         -- Eject disk
  130.         sleep(2)
  131.         ejectDisk(userDrive)
  132.        
  133.         mon.setCursorPos(1,6)
  134.         mon.write("Disk ejected!")
  135.         mon.setCursorPos(1,7)
  136.         mon.write("You can now click the")
  137.         mon.setCursorPos(1,8)
  138.         mon.write("button on the wall")
  139.        
  140.         -- Wait a moment before resetting
  141.         sleep(5)
  142.         os.reboot() -- Reset the computer
  143.     else
  144.         mon.setCursorPos(1,3)
  145.         mon.write("Error processing disk!")
  146.         mon.setCursorPos(1,4)
  147.         mon.write("Please try another disk")
  148.         sleep(3)
  149.     end
  150. end
Advertisement
Add Comment
Please, Sign In to add comment