Guest User

oneos boot script

a guest
Feb 2nd, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. cleanEnvironment = {} --enviroment gets sad without this ;-;
  2. for k, v in pairs(_G) do
  3. cleanEnvironment[k] = v
  4. end
  5.  
  6. local Extension = function(path, addDot)
  7. if not path then
  8. return nil
  9. elseif not string.find(fs.getName(path), '%.') then
  10. if not addDot then
  11. return fs.getName(path)
  12. else
  13. return ''
  14. end
  15. else
  16. local _path = path
  17. if path:sub(#path) == '/' then
  18. _path = path:sub(1,#path-1)
  19. end
  20. local extension = _path:gmatch('%.[0-9a-z]+$')()
  21. if extension then
  22. extension = extension:sub(2)
  23. else
  24. --extension = nil
  25. return ''
  26. end
  27. if addDot then
  28. extension = '.'..extension
  29. end
  30. return extension:lower()
  31. end
  32. end
  33.  
  34. local RemoveExtension = function(path)
  35. if path:sub(1,1) == '.' then
  36. return path
  37. end
  38. local extension = Extension(path)
  39. if extension == path then
  40. return fs.getName(path)
  41. end
  42. return string.gsub(path, extension, ''):sub(1, -2)
  43. end
  44.  
  45. local tAPIsLoading = {}
  46. function LoadAPI(_sPath)
  47. local sName = RemoveExtension(fs.getName( _sPath ))
  48. if tAPIsLoading[sName] == true then
  49. return true
  50. end
  51. Chameleon.Log.l("INFO", 'Loading: '.._sPath)
  52.  
  53. tAPIsLoading[sName] = true
  54.  
  55. local tEnv = {isStartup = true }
  56. setmetatable( tEnv, { __index = getfenv()} )
  57. local fnAPI, err = loadfile( _sPath )
  58. if fnAPI then
  59. setfenv( fnAPI, tEnv )
  60. fnAPI()
  61. else
  62. printError( err )
  63. Chameleon.Log.l("ERROR", 'Error: '..err)
  64. tAPIsLoading[sName] = nil
  65. return false
  66. end
  67.  
  68. local tAPI = {}
  69. for k,v in pairs( tEnv ) do
  70. tAPI[k] = v
  71. end
  72.  
  73. if not tAPI then
  74. Chameleon.Log.l("ERROR", 'Could not find API: '..sName)
  75. error('Could not find API: '..sName)
  76. end
  77.  
  78. getfenv()[sName] = tAPI
  79.  
  80. return true
  81. end
  82.  
  83. Chameleon.Log.l("INFO", "Starting OneOS. OneOS logged to /System/OneOS.log.")
  84. Chameleon.Log.l("INFO", "Boot progress will continue to be logged.")
  85.  
  86. LoadAPI('System/API/Log.lua')
  87. Log.Initialise()
  88. Log.i('Starting OneOS (via Chameleon)')
  89. Log.i(os.version())
  90. Log.i('Free space: '..fs.getFreeSpace('/'))
  91.  
  92. local h = fs.open('/System/.version', 'r')
  93. local version = '?'
  94. if h then
  95. version = h.readAll()
  96. h.close()
  97. else
  98. version = 'Not set'
  99. end
  100. Log.i('OneOS Version: '..version)
  101.  
  102. LoadAPI('System/API/Settings.lua')
  103.  
  104. local _side = Settings:GetValues()['Monitor']
  105. if not term.setTextScale and _side then
  106. if peripheral.isPresent(_side) and peripheral.getType(_side) == 'monitor' and peripheral.call(_side, 'isColour') == true then
  107. Chameleon.Log.l("WARN", "Not using monitor.")
  108. Log.w("Not using monitor.")
  109. else
  110. Settings:SetValue('Monitor', nil)
  111. end
  112. end
  113.  
  114. if fs.getFreeSpace and fs.getFreeSpace('/') < 51200 then
  115. Chameleon.Log.l("WARN", "Less than 50KB of space left!")
  116. Chameleon.Status = "<50KB disk space left!"
  117. Log.w("Less than 50KB disk space available.")
  118. sleep(1)
  119. end
  120.  
  121. local totalAPIs = #fs.list(root .. 'System/API/')-- + #fs.list(root .. 'System/Objects/')
  122. local apis = {}
  123. for _, file in pairs(fs.list(root .. 'System/API/')) do
  124. if string.sub(file,1,1) ~= "." then
  125. table.insert(apis, 'System/API/' .. file)
  126. end
  127. end
  128.  
  129. for _, file in pairs(apis) do
  130. if not LoadAPI(root .. file) then
  131. return false
  132. end
  133. end
  134.  
  135. Chameleon.Log.l("INFO", "Entering GUI...")
  136. Chameleon.Running = false
  137.  
  138. function ChameleonDone()
  139. os.run(getfenv(), '/System/main.lua')
  140.  
  141. local ok = nil
  142. if not fs.exists('/System/.version') or not fs.exists('/System/.OneOS.settings') then
  143. Log.i('No .version or .OneOS.settings! Running First Setup')
  144. if fs.exists('/System/.OneOS.settings') then
  145. fs.delete('/System/.OneOS.settings')
  146. end
  147. xpcall(FirstSetup, function(err)
  148. Log.e('[> First Setup] '..err)
  149. ok = {false, err}
  150. end)
  151. else
  152. xpcall(Initialise, function(err)
  153. ok = {false, err}
  154. end)
  155. end
  156.  
  157. return ok
  158. end
Advertisement
Add Comment
Please, Sign In to add comment