Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Doggy OS Automatic Repair on Startup Failure
- local diskPath = "/disk/"
- local recoveryPath = "/recovery/"
- local noOsPath = "/no-os" -- Path to the no-os program
- -- Function to check if a directory exists
- local function directoryExists(path)
- return fs.exists(path) and fs.isDir(path)
- end
- -- Function to copy contents of one directory to another
- local function copyDirectoryContents(source, destination)
- if not directoryExists(source) then
- return false
- end
- -- Clear the destination directory first if it exists
- if directoryExists(destination) then
- fs.delete(destination)
- end
- -- Copy contents from source to destination
- fs.copy(source, destination)
- return true
- end
- -- Function to display a centered message on the screen
- local function displayCenteredMessage(y, message)
- local w, h = term.getSize()
- local x = math.floor((w - #message) / 2)
- term.setCursorPos(x + 1, y)
- print(message)
- end
- -- Function to display a boxed message with centered title
- local function displayBoxedMessage(title, message)
- local w, h = term.getSize()
- local boxWidth = 50
- local boxX = math.floor((w - boxWidth) / 2)
- local boxY = math.floor(h / 2) - 3
- -- Draw box outline
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- for i = boxY, boxY + 6 do
- term.setCursorPos(boxX, i)
- if i == boxY or i == boxY + 6 then
- write(string.rep(" ", boxWidth))
- else
- write(" ")
- term.setCursorPos(boxX + boxWidth, i)
- write(" ")
- end
- end
- -- Display title and message inside the box
- term.setCursorPos(math.floor((w - #title) / 2) + 1, boxY + 1)
- print(title)
- term.setCursorPos(boxX + 2, boxY + 3)
- print(message)
- -- Display press any key to continue message
- term.setCursorPos(math.floor((w - 28) / 2) + 1, boxY + 5)
- print("Press any key to continue...")
- -- Wait for key press
- os.pullEvent("key")
- end
- -- Function to display detailed error messages
- local function displayErrorMessage(errorType)
- local title = "Doggy OS Automatic Repair"
- term.clear()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- displayCenteredMessage(2, title)
- print("-------------------------------------------\n")
- local errorMessage
- if errorType == "OSDirectoryMissing" then
- errorMessage = [[
- Issue: OS Directory Missing
- The /disk/ directory, which contains the operating system files, is missing.
- Please check your system and try again.
- ]]
- elseif errorType == "RestoreFailure" then
- errorMessage = [[
- Failed to restore from /recovery/ to /disk/.
- The files could not be restored from the recovery directory to the system disk (/disk/).
- Please check the integrity of the recovery directory and try again.
- ]]
- elseif errorType == "CreationFailure" then
- errorMessage = [[
- Failed to create /disk/ from /recovery/.
- The system disk (/disk/) could not be created from the recovery directory (/recovery/).
- Please ensure the recovery directory contains valid system files and try again.
- ]]
- else
- errorMessage = "Unknown error type."
- end
- -- Display error message in a boxed format
- displayBoxedMessage(title, errorMessage)
- shell.run(noOsPath)
- end
- -- Function to display a longer success message
- local function displayRepairSuccessMessage()
- local title = "Doggy OS Automatic Repair"
- local message = [[
- Repair successful!
- The system files have been restored or created from the recovery directory to the system disk (/disk/).
- You can now continue using your Doggy OS installation as usual.
- ]]
- -- Display success message in a boxed format
- displayBoxedMessage(title, message)
- os.reboot()
- end
- -- Function to perform automatic repair
- local function automaticRepair()
- -- Check if /recovery/ exists
- if not directoryExists(recoveryPath) then
- shell.run(noOsPath)
- return
- end
- -- Welcome message and instructions
- local title = "Doggy OS Automatic Repair"
- local welcomeMessage = "Doggy OS failed to boot. Starting automatic repair..."
- term.clear()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- displayCenteredMessage(2, title)
- print("-------------------------------------------\n")
- displayCenteredMessage(5, welcomeMessage)
- -- Check if /disk/ exists
- if directoryExists(diskPath) then
- -- Attempt to copy contents of /recovery/ to /disk/
- local success = copyDirectoryContents(recoveryPath, diskPath)
- if success then
- displayRepairSuccessMessage()
- else
- -- Handle restore failure
- displayErrorMessage("RestoreFailure")
- end
- else
- -- Create /disk/ from /recovery/ if /disk/ doesn't exist
- local success = copyDirectoryContents(recoveryPath, diskPath)
- if success then
- displayRepairSuccessMessage()
- else
- -- Handle creation failure
- displayErrorMessage("CreationFailure")
- end
- end
- end
- -- Start the automatic repair process
- automaticRepair()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement