daily pastebin goal
18%
SHARE
TWEET

uBoot

theblu Mar 22nd, 2019 (edited) 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Universal Boot Loader, used for SpiralOS(Teal)
  2.  
  3.  
  4. --Variables
  5. sides = {"front","back","right","left","bottom","top"}
  6.  
  7. -- Different boot modes default values
  8. local craftOSBoot = false
  9. local verboseMode = false
  10.  
  11. local OSFound = false
  12.  
  13. -- Lists of sides that are for different peripherals.
  14. diskDrivePeripherals = {}
  15. diskDrivePaths = {}
  16. monitorPeripherals = {}
  17. modemPeripherals = {}
  18. printerPeripherals = {}
  19.  
  20. deviceType = computer
  21.  
  22. screenSizeX, screenSizeY = term.getSize()
  23. --Functions
  24. local function cls()
  25.     term.setCursorPos(1,1)
  26.     term.setBackgroundColor(32768)
  27.     term.clear()
  28. end
  29.  
  30. local function printCenteredText(t, ycord)
  31.     if ycord ~= nil then
  32.         term.setCursorPos(math.ceil((screenSizeX - #t) / 2),(math.ceil(screenSizeY / 2) + ycord))
  33.     else
  34.         term.setCursorPos(math.ceil((screenSizeX - #t) / 2), math.ceil((screenSizeY -1) /2))
  35.     end
  36.     print(t)
  37. end
  38.  
  39. local function findCenteredImagePos(imgLength, imgHeight)
  40.     imgDrawX = math.ceil((screenSizeX - imgLength) / 2) + 1
  41.     imgDrawY = math.ceil((screenSizeY - imgHeight) / 2)
  42.     return imgDrawX, imgDrawY
  43. end
  44.  
  45. local function getKeyPress()
  46.     local event, key, isHeld = os.pullEvent("key")
  47.         if key ~= nil then
  48.         if key == 46 then
  49.             craftOSBoot = true
  50.             cls()
  51.         end
  52.         if key == 47 then
  53.             verboseMode = true
  54.             term.setCursorPos(1,1)
  55.         end
  56.     end
  57. end
  58.  
  59. local function timeOut()
  60.     sleep(2)
  61. end
  62. --Main
  63.  
  64. -- Set initial background color
  65. term.setBackgroundColor(32768)
  66.  
  67. -- Get logo if it doesn't exist
  68. if fs.exists(".ubootlogo") ~= true then
  69.     if term.isColor() then
  70.         shell.run("pastebin get R3jNSvmY .ubootlogo")
  71.     else
  72.         shell.run("pastebin get V3jaR1wL .ubootlogo")
  73.     end
  74.     cls()
  75. end
  76.  
  77. -- Load the image from the file
  78. uOSLogo = paintutils.loadImage(".ubootlogo")
  79.  
  80. -- Clear the screen and draw the logo
  81. cls()
  82. if fs.exists(".ubootlogo") then
  83.     imgX, imgY = findCenteredImagePos(13,5)
  84.     paintutils.drawImage(OSBoot, imgX, imgY)
  85. else
  86.     printCenteredText("Unable to get uBoot logo.")
  87. end
  88.  
  89. -- Set the the colors
  90. term.setBackgroundColor(1)
  91. term.setTextColor(32768)
  92.  
  93. -- Wait 1 second or accept a key press. If there was a key press, check what one and set modes.
  94. parallel.waitForAny(getKeyPress,timeOut)
  95. -- Verbose information
  96.  
  97. if verboseMode == true then
  98.     print("found screen resolution to be "..screenSizeX.." by "..screenSizeY)
  99. end
  100.  
  101. term.setCursorPos(1,2)
  102. -- Scan for peripherals.
  103. for i=1,#sides do
  104.     if peripheral.isPresent(sides[i]) == true then
  105.         if peripheral.getType(sides[i]) == "drive" then
  106.             table.insert(diskDrivePeripherals, sides[i])
  107.             if verboseMode == true then
  108.                 print("found disk drive on side "..sides[i])
  109.             end
  110.             if disk.isPresent(sides[i]) then
  111.                     if disk.hasData(sides[i]) then
  112.                         table.insert(diskDrivePaths, disk.getMountPath(sides[i]))
  113.                     end  
  114.             end
  115.         end
  116.         if peripheral.getType(sides[i]) == "printer" then
  117.             table.insert(printerPeripherals, sides[i])
  118.             if verboseMode == true then
  119.                 print("found printer on side "..sides[i])
  120.             end
  121.         end
  122.         if peripheral.getType(sides[i]) == "modem" then
  123.             table.insert(modemPeripherals, sides[i])
  124.             if verboseMode == true then
  125.                 print("found modem on side "..sides[i])
  126.             end
  127.         end
  128.         if peripheral.getType(sides[i]) == "monitor" then
  129.             table.insert(monitorPeripherals, sides[i])
  130.             if verboseMode == true then
  131.                 print("found monitor on side "..sides[i])
  132.             end
  133.         end
  134.     end
  135. end
  136. -- Allow for the user to process verbose info
  137. if verboseMode == true then
  138.     sleep(2)
  139. end
  140.  
  141. -- If the craftOS boot mode is false, we will start the system and prepare for crashes.
  142.  
  143. if craftOSBoot == false then
  144. -- Scan for the uOS operating system
  145. if fs.exists("/uOS") then
  146.     OSFound = true
  147.     nouOS = false
  148.     shell.run("uOS")
  149.     if verboseMode == true then
  150.         print("booting off of internal drive")
  151.     end
  152. else
  153.     for i=1,#diskDrivePeripherals do
  154.         if OSFound == false then
  155.             if disk.hasData(diskDrivePeripherals[i]) then
  156.                 if fs.exists(disk.getMountPath(diskDrivePeripherals[i])) then
  157.                     shell.run(disk.getMountPath(diskDrivePeripherals[i]).."/".."spiral")
  158.                     OSFound = true
  159.                     noOS = false
  160.                     if verboseMode == true then
  161.                         print("booting off of "..screenSizeX.."disk drive")
  162.                     end
  163.                 end
  164.             end
  165.         end
  166.     end
  167. end
  168. if noOS then
  169.     print("unable to find a supported OS")
  170.     print("reverting to craftOS...")
  171.     sleep(3)
  172.     cls()
  173. end
  174. -- Error screen for system crashes
  175. if fs.exists(".developer") then
  176.     sleep(1)
  177. end
  178. if OSFound then
  179.     term.setTextColor(32768)
  180.     if term.isColor() then
  181.     term.setBackgroundColor(256)
  182.     term.setTextColor(1)
  183.     else
  184.     term.setBackgroundColor(32768)
  185.     term.setTextColor(1)
  186.     end
  187.     cls()
  188.     term.setCursorPos(1,1)
  189.     printCenteredText("A fatal error occurred.")
  190.     print("")
  191.     printCenteredText("Press any key to reboot, or press", 2)
  192.     printCenteredText("C to revert to craftos.",3)
  193.     local event, key, isHeld = os.pullEvent("key")
  194.     if key == 46 then
  195.         cls()
  196.     end
  197.     if key ~= 46 then
  198.         os.reboot()
  199.     end
  200. end
  201. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top