Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Packages/libs
- 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
- local pm = require("api.peripheral_manager")
- -- peripherals
- local mon = pm.first("monitor")
- local speaker = pm.first("speaker")
- -- Function to check if a disk drive (excluding drive_02) has a disk
- local function hasUserDisk()
- local drives = pm.find("drive")
- for _, drive in ipairs(drives) do
- -- Skip drive_02 (the source disk drive)
- if drive.name ~= "drive_02" then
- if drive.isDiskPresent() then
- return drive
- end
- end
- end
- return nil
- end
- -- Function to label the disk and copy files from drive_02
- local function processDisk(userDrive)
- if not userDrive then return false end
- -- Label the disk
- local success, err = pcall(function()
- userDrive.setDiskLabel("USER")
- end)
- if not success then
- mon.write("Error labeling disk: " .. tostring(err))
- return false
- end
- -- Get the source drive (drive_02)
- local sourceDrive = pm.get("drive_02")
- if not sourceDrive or not sourceDrive.isDiskPresent() then
- mon.write("Source disk not found in drive_02!")
- return false
- end
- -- Mount paths for both drives
- local sourceMountPath = sourceDrive.getMountPath() or "disk_02"
- local userMountPath = userDrive.getMountPath() or "disk"
- -- Copy file from source disk to user disk
- local sourcePath = fs.combine(sourceMountPath, "sys/template/music.lua")
- local destPath = fs.combine(userMountPath, "sys/bin/music.lua")
- -- Check if source file exists
- if not fs.exists(sourcePath) then
- mon.write("Source file not found on template disk!")
- return false
- end
- -- Create destination directory if it doesn't exist
- local destDir = fs.combine(userMountPath, "sys/bin")
- if not fs.exists(destDir) then
- fs.makeDir(destDir)
- end
- -- Copy the file
- local success, err = pcall(function()
- local sourceFile = fs.open(sourcePath, "r")
- local destFile = fs.open(destPath, "w")
- if sourceFile and destFile then
- local content = sourceFile.readAll()
- destFile.write(content)
- sourceFile.close()
- destFile.close()
- return true
- else
- if sourceFile then sourceFile.close() end
- if destFile then destFile.close() end
- return false
- end
- end)
- if not success then
- mon.write("Error copying file: " .. tostring(err))
- return false
- end
- return true
- end
- -- Function to eject disk from a specific drive
- local function ejectDisk(drive)
- if drive then
- drive.ejectDisk()
- end
- end
- -- Main loop
- while true do
- mon.clear()
- mon.setCursorPos(1,1)
- mon.write("Welcome user!")
- mon.setCursorPos(1,2)
- mon.write("Please place a floppy disk")
- mon.setCursorPos(1,3)
- mon.write("in the disk drive below.")
- -- Wait for disk to be inserted (excluding drive_02)
- local userDrive = nil
- while not userDrive do
- userDrive = hasUserDisk()
- if not userDrive then
- sleep(0.5)
- end
- end
- -- Disk detected, process it
- mon.clear()
- mon.setCursorPos(1,1)
- mon.write("Disk detected in " .. userDrive.name .. "!")
- mon.setCursorPos(1,2)
- mon.write("Processing...")
- if processDisk(userDrive) then
- mon.setCursorPos(1,3)
- mon.write("Disk labeled: USER")
- mon.setCursorPos(1,4)
- mon.write("File copied successfully!")
- -- Eject disk
- sleep(2)
- ejectDisk(userDrive)
- mon.setCursorPos(1,6)
- mon.write("Disk ejected!")
- mon.setCursorPos(1,7)
- mon.write("You can now click the")
- mon.setCursorPos(1,8)
- mon.write("button on the wall")
- -- Wait a moment before resetting
- sleep(5)
- os.reboot() -- Reset the computer
- else
- mon.setCursorPos(1,3)
- mon.write("Error processing disk!")
- mon.setCursorPos(1,4)
- mon.write("Please try another disk")
- sleep(3)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment