Advertisement
DOGGYWOOF

Automatic system repair

Jul 3rd, 2024 (edited)
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. -- Doggy OS Automatic Repair on Startup Failure
  2.  
  3. local diskPath = "/disk/"
  4. local recoveryPath = "/recovery/"
  5. local noOsPath = "/no-os" -- Path to the no-os program
  6.  
  7. -- Function to check if a directory exists
  8. local function directoryExists(path)
  9. return fs.exists(path) and fs.isDir(path)
  10. end
  11.  
  12. -- Function to copy contents of one directory to another
  13. local function copyDirectoryContents(source, destination)
  14. if not directoryExists(source) then
  15. return false
  16. end
  17.  
  18. -- Clear the destination directory first if it exists
  19. if directoryExists(destination) then
  20. fs.delete(destination)
  21. end
  22.  
  23. -- Copy contents from source to destination
  24. fs.copy(source, destination)
  25. return true
  26. end
  27.  
  28. -- Function to display a centered message on the screen
  29. local function displayCenteredMessage(y, message)
  30. local w, h = term.getSize()
  31. local x = math.floor((w - #message) / 2)
  32. term.setCursorPos(x + 1, y)
  33. print(message)
  34. end
  35.  
  36. -- Function to display a boxed message with centered title
  37. local function displayBoxedMessage(title, message)
  38. local w, h = term.getSize()
  39. local boxWidth = 50
  40. local boxX = math.floor((w - boxWidth) / 2)
  41. local boxY = math.floor(h / 2) - 3
  42.  
  43. -- Draw box outline
  44. term.setBackgroundColor(colors.gray)
  45. term.setTextColor(colors.white)
  46. for i = boxY, boxY + 6 do
  47. term.setCursorPos(boxX, i)
  48. if i == boxY or i == boxY + 6 then
  49. write(string.rep(" ", boxWidth))
  50. else
  51. write(" ")
  52. term.setCursorPos(boxX + boxWidth, i)
  53. write(" ")
  54. end
  55. end
  56.  
  57. -- Display title and message inside the box
  58. term.setCursorPos(math.floor((w - #title) / 2) + 1, boxY + 1)
  59. print(title)
  60. term.setCursorPos(boxX + 2, boxY + 3)
  61. print(message)
  62.  
  63. -- Display press any key to continue message
  64. term.setCursorPos(math.floor((w - 28) / 2) + 1, boxY + 5)
  65. print("Press any key to continue...")
  66.  
  67. -- Wait for key press
  68. os.pullEvent("key")
  69. end
  70.  
  71. -- Function to display detailed error messages
  72. local function displayErrorMessage(errorType)
  73. local title = "Doggy OS Automatic Repair"
  74.  
  75. term.clear()
  76. term.setBackgroundColor(colors.black)
  77. term.setTextColor(colors.white)
  78. displayCenteredMessage(2, title)
  79. print("-------------------------------------------\n")
  80.  
  81. local errorMessage
  82.  
  83. if errorType == "OSDirectoryMissing" then
  84. errorMessage = [[
  85. Issue: OS Directory Missing
  86.  
  87. The /disk/ directory, which contains the operating system files, is missing.
  88. Please check your system and try again.
  89. ]]
  90. elseif errorType == "RestoreFailure" then
  91. errorMessage = [[
  92. Failed to restore from /recovery/ to /disk/.
  93.  
  94. The files could not be restored from the recovery directory to the system disk (/disk/).
  95. Please check the integrity of the recovery directory and try again.
  96. ]]
  97. elseif errorType == "CreationFailure" then
  98. errorMessage = [[
  99. Failed to create /disk/ from /recovery/.
  100.  
  101. The system disk (/disk/) could not be created from the recovery directory (/recovery/).
  102. Please ensure the recovery directory contains valid system files and try again.
  103. ]]
  104. else
  105. errorMessage = "Unknown error type."
  106. end
  107.  
  108. -- Display error message in a boxed format
  109. displayBoxedMessage(title, errorMessage)
  110. shell.run(noOsPath)
  111. end
  112.  
  113. -- Function to display a longer success message
  114. local function displayRepairSuccessMessage()
  115. local title = "Doggy OS Automatic Repair"
  116. local message = [[
  117. Repair successful!
  118. The system files have been restored or created from the recovery directory to the system disk (/disk/).
  119.  
  120. You can now continue using your Doggy OS installation as usual.
  121. ]]
  122.  
  123. -- Display success message in a boxed format
  124. displayBoxedMessage(title, message)
  125. os.reboot()
  126. end
  127.  
  128. -- Function to perform automatic repair
  129. local function automaticRepair()
  130. -- Check if /recovery/ exists
  131. if not directoryExists(recoveryPath) then
  132. shell.run(noOsPath)
  133. return
  134. end
  135.  
  136. -- Welcome message and instructions
  137. local title = "Doggy OS Automatic Repair"
  138. local welcomeMessage = "Doggy OS failed to boot. Starting automatic repair..."
  139.  
  140. term.clear()
  141. term.setBackgroundColor(colors.black)
  142. term.setTextColor(colors.white)
  143. displayCenteredMessage(2, title)
  144. print("-------------------------------------------\n")
  145. displayCenteredMessage(5, welcomeMessage)
  146.  
  147. -- Check if /disk/ exists
  148. if directoryExists(diskPath) then
  149. -- Attempt to copy contents of /recovery/ to /disk/
  150. local success = copyDirectoryContents(recoveryPath, diskPath)
  151. if success then
  152. displayRepairSuccessMessage()
  153. else
  154. -- Handle restore failure
  155. displayErrorMessage("RestoreFailure")
  156. end
  157. else
  158. -- Create /disk/ from /recovery/ if /disk/ doesn't exist
  159. local success = copyDirectoryContents(recoveryPath, diskPath)
  160. if success then
  161. displayRepairSuccessMessage()
  162. else
  163. -- Handle creation failure
  164. displayErrorMessage("CreationFailure")
  165. end
  166. end
  167. end
  168.  
  169. -- Start the automatic repair process
  170. automaticRepair()
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement