Advertisement
HangMan23

Untitled

Aug 13th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. -- called from /init.lua
  2. local raw_loadfile = ...
  3.  
  4. _G._OSVERSION = "OpenOS 1.7.4"
  5.  
  6. -- luacheck: globals component computer unicode _OSVERSION
  7. local component = component
  8. local computer = computer
  9. local unicode = unicode
  10.  
  11. -- Runlevel information.
  12. _G.runlevel = "S"
  13. local shutdown = computer.shutdown
  14. computer.runlevel = function() return _G.runlevel end
  15. computer.shutdown = function(reboot)
  16. _G.runlevel = reboot and 6 or 0
  17. if os.sleep then
  18. computer.pushSignal("shutdown")
  19. os.sleep(0.1) -- Allow shutdown processing.
  20. end
  21. shutdown(reboot)
  22. end
  23.  
  24. local w, h
  25. local screen = component.list("screen", true)()
  26. local gpu = screen and component.list("gpu", true)()
  27. if gpu then
  28. gpu = component.proxy(gpu)
  29. if not gpu.getScreen() then
  30. gpu.bind(screen)
  31. end
  32. _G.boot_screen = gpu.getScreen()
  33. w, h = gpu.maxResolution()
  34. gpu.setResolution(w, h)
  35. gpu.setBackground(0xFFFFFF)
  36. gpu.setForeground(0xFFFFFF)
  37. gpu.fill(1, 1, w, h, " ")
  38. end
  39.  
  40. -- Report boot progress if possible.
  41. local y = 1
  42. local uptime = computer.uptime
  43. -- we actually want to ref the original pullSignal here because /lib/event intercepts it later
  44. -- because of that, we must re-pushSignal when we use this, else things break badly
  45. local pull = computer.pullSignal
  46. local last_sleep = uptime()
  47. local function status(msg)
  48. if gpu then
  49.  
  50. gpu.setBackground (0xffffff)
  51. gpu.setForeground (0x646464)
  52. gpu.fill (1, h - 1, w, 2, " ")
  53.  
  54. gpu.set(1, h, msg)
  55.  
  56. end
  57. -- boot can be slow in some environments, protect from timeouts
  58. if uptime() - last_sleep > 1 then
  59. local signal = table.pack(pull(0))
  60. -- there might not be any signal
  61. if signal.n > 0 then
  62. -- push the signal back in queue for the system to use it
  63. computer.pushSignal(table.unpack(signal, 1, signal.n))
  64. end
  65. last_sleep = uptime()
  66. end
  67. end
  68.  
  69. status("Booting " .. _OSVERSION .. "...")
  70.  
  71. -- Custom low-level dofile implementation reading from our ROM.
  72. local function dofile(file)
  73. status("> " .. file)
  74. local program, reason = raw_loadfile(file)
  75. if program then
  76. local result = table.pack(pcall(program))
  77. if result[1] then
  78. return table.unpack(result, 2, result.n)
  79. else
  80. error(result[2])
  81. end
  82. else
  83. error(reason)
  84. end
  85. end
  86.  
  87. status("Initializing package management...")
  88.  
  89. -- Load file system related libraries we need to load other stuff moree
  90. -- comfortably. This is basically wrapper stuff for the file streams
  91. -- provided by the filesystem components.
  92. local package = dofile("/lib/package.lua")
  93.  
  94. do
  95. -- Unclutter global namespace now that we have the package module and a filesystem
  96. _G.component = nil
  97. _G.computer = nil
  98. _G.process = nil
  99. _G.unicode = nil
  100. -- Inject the package modules into the global namespace, as in Lua.
  101. _G.package = package
  102.  
  103. -- Initialize the package module with some of our own APIs.
  104. package.loaded.component = component
  105. package.loaded.computer = computer
  106. package.loaded.unicode = unicode
  107. package.loaded.buffer = dofile("/lib/buffer.lua")
  108. package.loaded.filesystem = dofile("/lib/filesystem.lua")
  109.  
  110. -- Inject the io modules
  111. _G.io = dofile("/lib/io.lua")
  112. end
  113.  
  114. status("Initializing file system...")
  115.  
  116. -- Mount the ROM and temporary file systems to allow working on the file
  117. -- system module from this point on.
  118. require("filesystem").mount(computer.getBootAddress(), "/")
  119.  
  120. status("Running boot scripts...")
  121.  
  122. -- Run library startup scripts. These mostly initialize event handlers.
  123. local function rom_invoke(method, ...)
  124. return component.invoke(computer.getBootAddress(), method, ...)
  125. end
  126.  
  127. local scripts = {}
  128. for _, file in ipairs(rom_invoke("list", "boot")) do
  129. local path = "boot/" .. file
  130. if not rom_invoke("isDirectory", path) then
  131. table.insert(scripts, path)
  132. end
  133. end
  134. table.sort(scripts)
  135.  
  136. gpu.setBackground (0xffffff)
  137. gpu.setForeground (0x000000)
  138.  
  139. local text = "Welcome!"
  140.  
  141. gpu.set (w / 2 - #text / 2, h / 2, text)
  142.  
  143. for i = 1, #scripts do
  144.  
  145. local a = ((math.pi * 2) / #scripts) * i
  146.  
  147. for A = 0, a, math.pi / 64 do
  148.  
  149. local a_ = math.pi * 2 - A
  150.  
  151. gpu.setBackground (0x646464)
  152. gpu.set (w / 2 + math.sin (a_) * 12, h / 2 + math.cos (a_) * 6, " ")
  153.  
  154. end
  155.  
  156. dofile(scripts[i])
  157.  
  158. end
  159.  
  160. status("Initializing components...")
  161.  
  162. for c, t in component.list() do
  163. computer.pushSignal("component_added", c, t)
  164. end
  165.  
  166. status("Initializing system...")
  167.  
  168. computer.pushSignal("init") -- so libs know components are initialized.
  169. require("event").pull(1, "init") -- Allow init processing.
  170. _G.runlevel = 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement