Shefla

SimplestBootloader

May 28th, 2015
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.41 KB | None | 0 0
  1. if _G.bootloaded == true then return end -- ensures that startup runs only once
  2. --------------------------------------------------------------------------------
  3. --- Simplest Bootloader for ComputerCraft 1.6
  4. -- @version 0.4-beta
  5. -- @author  Shefla
  6. -- @license MIT
  7. -- @changelog
  8. --    0.1 Initial version adapted from Geforce Fan's TLCO concept
  9. --    0.2 Added rednet.run spoofing to avoid forced autostart
  10. --        Switched from terminate event to modem_message to trigger printError
  11. --    0.3 Public release, added comments to config section
  12. --    0.4 Removed hardcoded CraftOS version from default config (use os.version)
  13. --------------------------------------------------------------------------------
  14.  
  15. -- Title of the bootloader screen
  16. local title  = 'Simplest Bootloader v0.4-beta'
  17.  
  18. -- Bootable entries configuration
  19. -- If there is only one entry it is booted without displaying menu
  20. local entries = {
  21.     {
  22.         -- name is used when displaying menu
  23.         name = os.version()
  24.         -- boot function executed on selection (shell API not available)
  25.     , boot = function ()
  26.             -- from bios.lua
  27.             parallel.waitForAny(
  28.                 function ()
  29.                     os.run({}, term.isColor()
  30.                         and '/rom/programs/advanced/multishell'
  31.                         or  '/rom/programs/shell'
  32.                     )
  33.                 end
  34.             , function () rednet.run() end
  35.             )
  36.         end
  37.     }
  38. , {
  39.         name = os.version()..' (no rednet, no multishell)'
  40.     , boot = function ()
  41.             term.redirect(term.native())
  42.             os.run({ multishell = false }, '/rom/programs/shell')
  43.         end
  44.     }
  45. }
  46.  
  47. --8<-- Probably no need to edit below --8<--------------------------------------
  48. local nativePrintError = _G.printError
  49. local nativeRednetRun  = _G.rednet.run
  50. function _G.rednet.run () error('', 0) end
  51. function _G.printError ()
  52.     local entry
  53.     _G.bootloaded = true
  54.     _G.printError = nativePrintError
  55.     _G.rednet.run = nativeRednetRun
  56.     if #entries == 1 then entry = entries[1]
  57.     else
  58.         term.clear()
  59.         term.setCursorPos(1, 1)
  60.         print(title)
  61.         print()
  62.         for index, entry in pairs(entries) do
  63.             print('F'..index..': '..entry.name)
  64.         end
  65.         while true do
  66.             local event, key = os.pullEvent('key')
  67.             local index = key - 58
  68.             if entries[index] ~= nil then
  69.                 entry = entries[index]
  70.                 break
  71.             end
  72.         end
  73.     end
  74.     print('Launching '..entry.name)
  75.     local ok, err = pcall(entry.boot)
  76.     if not ok then
  77.         printError(err)
  78.         print('Press any key to continue')
  79.         os.pullEvent('key')
  80.     end
  81.     os.shutdown()
  82. end
  83. os.queueEvent('modem_message')
Advertisement
Add Comment
Please, Sign In to add comment