mirevall

Patch OneOS

Mar 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.81 KB | None | 0 0
  1. -- Bedrock.Helpers = Helpers
  2. local bedrock = Bedrock:Initialise('/System')
  3. bedrock.ViewPath ='/System/Views/'
  4. -- _G.Helpers = Helpers
  5. -- error(Helpers.IconForFile)
  6. bedrock.AllowTerminate = false
  7.  
  8. if type(term.native) == 'function' then
  9. local cur = term.current()
  10. restoreTerm = function()term.redirect(cur)end
  11. else
  12. restoreTerm = function()term.restore()end
  13. end
  14.  
  15. Current = {
  16. ProgramView = nil,
  17. Overlay = nil,
  18. Programs = {},
  19. Program = nil,
  20. Desktop = nil,
  21. Bedrock = bedrock,
  22. SearchActive = true
  23. }
  24.  
  25. function UpdateOverlay()
  26. bedrock:GetObject('Overlay'):UpdateButtons()
  27. end
  28.  
  29. bedrock.OnKeyChar = function(self, event, keychar)
  30. if isDebug and keychar == '\\' then
  31. --Restart()
  32. AnimateShutdown(true)
  33. end
  34. end
  35.  
  36. bedrock.OnTimer = function(self, event, timer)
  37. for i, program in ipairs(Current.Programs) do
  38. for i2, _timer in ipairs(program.Timers) do
  39. if _timer == timer then
  40. program:QueueEvent('timer', timer)
  41. end
  42. end
  43. end
  44. end
  45.  
  46. bedrock:RegisterEvent('modem_message', function(self, event, side, channel, replyChannel, message, distance)
  47. if pocket and channel == Wireless.Channels.UltimateDoorlockPing then
  48. message = textutils.unserialize(message)
  49. if message then
  50. message.content = textutils.unserialize(message.content)
  51. if message.content then
  52. Wireless.SendMessage(Wireless.Channels.UltimateDoorlockRequest, fingerprint, Wireless.Channels.UltimateDoorlockRequestReply, nil, message.senderID)
  53. return true
  54. end
  55. end
  56. end
  57. Current.Program:QueueEvent(event, side, channel, replyChannel, message, distance)
  58. end)
  59.  
  60. bedrock.EventHandler = function(self)
  61. for i, program in ipairs(Current.Programs) do
  62. for i, event in ipairs(program.EventQueue) do
  63. program:Resume(unpack(event))
  64. end
  65. program.EventQueue = {}
  66. end
  67. local event = { os.pullEventRaw() }
  68.  
  69. local s = 'Event: '
  70. for i, v in ipairs(event) do
  71. s = s..tostring(v)..', '
  72. end
  73. Log.i(s)
  74.  
  75. if self.EventHandlers[event[1]] then
  76. for i, e in ipairs(self.EventHandlers[event[1]]) do
  77. e(self, unpack(event))
  78. end
  79. end
  80. end
  81.  
  82. function Shutdown(force, restart, animate)
  83. Log.i(bedrock.View.Name)
  84. if bedrock.View.Name == 'firstsetup' then
  85. os.reboot()
  86. end
  87. Log.i('Trying to shutdown/restart. Restart: '..tostring(restart))
  88. local success = true
  89. if not force then
  90. for i, program in ipairs(Current.Programs) do
  91. if not program.Hidden and not program:Close() then
  92. success = false
  93. end
  94. end
  95. end
  96.  
  97. if success then
  98. AnimateShutdown(restart, animate)
  99. else
  100. Log.w('Shutdown/restart aborted')
  101. Current.Desktop:SwitchTo()
  102. local shutdownLabel = (restart and 'restart' or 'shutdown')
  103. local shutdownLabelCaptital = (restart and 'Restart' or 'Shutdown')
  104.  
  105. bedrock:DisplayAlertWindow("Programs Still Open", "You have unsaved work. Save your work and close the program or click 'Force "..shutdownLabelCaptital.."'.", {'Force '..shutdownLabelCaptital, 'Cancel'}, function(value)
  106. if value ~= 'Cancel' then
  107. AnimateShutdown(restart, animate)
  108. end
  109. end)
  110. end
  111. end
  112.  
  113. function AnimateShutdown(restart, animate)
  114. Log.w('System safely stopping.')
  115. if Settings:GetValues()['UseAnimations'] and animate then
  116. Log.i('Animating')
  117. Drawing.Clear(colours.white)
  118. Drawing.DrawBuffer()
  119. sleep(0)
  120. local x = 0
  121. local y = 0
  122. local w = 0
  123. local h = 0
  124. for i = 1, 8 do
  125. local percent = (i * 0.05)
  126. Drawing.Clear(colours.black)
  127. x = Drawing.Screen.Width * (i * 0.01)
  128. y = math.floor(Drawing.Screen.Height * (i * 0.05)) + 3
  129. w = Drawing.Screen.Width - (2 * x) + 1
  130. h = Drawing.Screen.Height - (2 * y) + 1
  131.  
  132. if h < 1 then
  133. h = 1
  134. end
  135.  
  136. Drawing.DrawBlankArea(x + 1, y, w, h, colours.white)
  137. Drawing.DrawBuffer()
  138. sleep(0)
  139. end
  140.  
  141. Drawing.DrawBlankArea(x + 1, y, w, h, colours.lightGrey)
  142. Drawing.DrawBuffer()
  143. sleep(0)
  144.  
  145. Drawing.DrawBlankArea(x + 1, y, w, h, colours.grey)
  146. Drawing.DrawBuffer()
  147. sleep(0)
  148. Log.i('Done animation')
  149. end
  150.  
  151. term.setBackgroundColour(colours.black)
  152. term.clear()
  153. if restart then
  154. sleep(0.2)
  155. Log.i('Rebooting now.')
  156. os.reboot()
  157. else
  158. Log.i('Shutting down now.')
  159. os.shutdown()
  160. end
  161. end
  162.  
  163. function Restart(force, animate)
  164. Shutdown(force, true, animate)
  165. end
  166.  
  167. function StartDoorWireless()
  168. if pocket and Wireless.Present() then
  169. Wireless.Open(Wireless.Channels.UltimateDoorlockPing)
  170. Wireless.Open(Wireless.Channels.UltimateDoorlockRequest)
  171. if fs.exists('/System/.fingerprint') then
  172. local h = fs.open('/System/.fingerprint', 'r')
  173. if h then
  174. fingerprint = h.readAll()
  175. h.close()
  176. end
  177. else
  178. local function GenerateFingerprint()
  179. local str = ""
  180. for _ = 1, 256 do
  181. local char = math.random(32, 126)
  182. str = str .. string.char(char)
  183. end
  184. return str
  185. end
  186. fingerprint = GenerateFingerprint()
  187. local h = fs.open('/System/.fingerprint', 'w')
  188. if h then
  189. h.write(fingerprint)
  190. h.close()
  191. end
  192. end
  193. end
  194. end
  195.  
  196. local checkAutoUpdateArg = nil
  197.  
  198. function CheckAutoUpdate(arg)
  199. Log.i('Checking for updates...')
  200. checkAutoUpdateArg = arg
  201. if http then
  202. if checkAutoUpdateArg then
  203. bedrock:DisplayAlertWindow("Update OneOS", "Checking for updates, this may take a moment.", {'Ok'})
  204. end
  205. http.request('https://api.github.com/repos/oeed/OneOS/releases#')
  206. elseif arg then
  207. Log.e('Update failed. HTTP is not enabled.')
  208. bedrock:DisplayAlertWindow("HTTP Not Enabled!", "Turn on the HTTP API to update.", {'Ok'})
  209. else
  210. Log.e('Update failed. HTTP is not enabled.')
  211. end
  212. end
  213.  
  214. function split(str, sep)
  215. local sep, fields = sep or ":", {}
  216. local pattern = string.format("([^%s]+)", sep)
  217. str:gsub(pattern, function(c) fields[#fields+1] = c end)
  218. return fields
  219. end
  220.  
  221. function GetSematicVersion(tag)
  222. tag = tag:sub(2)
  223. return split(tag, '.')
  224. end
  225.  
  226. --Returns true if the FIRST version is NEWER
  227. function SematicVersionIsNewer(version, otherVersion)
  228. if version[1] > otherVersion[1] then
  229. return true
  230. elseif version[2] > otherVersion[2] then
  231. return true
  232. elseif version[3] > otherVersion[3] then
  233. return true
  234. end
  235. return false
  236. end
  237.  
  238. function AutoUpdateFail(self, event, url, data)
  239. if url == 'https://api.github.com/repos/oeed/OneOS/releases#' then
  240. Log.w('Auto update failed. (http_failure)')
  241. if checkAutoUpdateArg then
  242. if bedrock.Window then
  243. bedrock.Window:Close()
  244. end
  245. bedrock:DisplayAlertWindow("Update Check Failed", "Check your connection and try again.", {'Ok'})
  246. end
  247. else
  248. Current.Program:QueueEvent(event, url, data)
  249. end
  250. end
  251.  
  252. function AutoUpdateResponse(self, event, url, data)
  253. if url == 'https://api.github.com/repos/oeed/OneOS/releases#' then
  254. os.loadAPI('/System/JSON')
  255. if not data then
  256. Log.w('Auto update failed. (no)')
  257. return
  258. end
  259. local releases = JSON.decode(data.readAll())
  260. os.unloadAPI('JSON')
  261. if not releases or not releases[1] or not releases[1].tag_name then
  262. Log.w('Auto update failed. (misformatted)')
  263. if checkAutoUpdateArg then
  264. if bedrock.Window then
  265. bedrock.Window:Close()
  266. end
  267. bedrock:DisplayAlertWindow("Update Check Failed", "Check your connection and try again.", {'Ok'})
  268. end
  269. return
  270. end
  271. local latestReleaseTag = releases[1].tag_name
  272.  
  273. if not Settings:GetValues()['DownloadPrereleases'] then
  274. Log.i('Not downloading prereleases')
  275. for i, v in ipairs(releases) do
  276. if not v.prerelease then
  277. latestReleaseTag = v.tag_name
  278. break
  279. end
  280. end
  281. end
  282. Log.i('Latest tag: '..latestReleaseTag)
  283.  
  284. local h = fs.open('/System/.version', 'r')
  285. local version = h.readAll()
  286. h.close()
  287.  
  288. if version == latestReleaseTag then
  289. --using latest version
  290. Log.i('OneOS is up to date.')
  291. if checkAutoUpdateArg then
  292. if bedrock.Window then
  293. bedrock.Window:Close()
  294. end
  295. bedrock:DisplayAlertWindow("Up to date!", "OneOS is up to date!", {'Ok'})
  296. end
  297. return
  298. elseif SematicVersionIsNewer(GetSematicVersion(latestReleaseTag), GetSematicVersion(version)) then
  299. Log.i('New version of OneOS available. (from '..version..' to '..latestReleaseTag..')')
  300. if bedrock.Window then
  301. bedrock.Window:Close()
  302. end
  303. bedrock:DisplayAlertWindow("Update OneOS", "There is a new version of OneOS available, do you want to update?", {'Yes', 'No'}, function(value)
  304. if value == 'Yes' then
  305. Helpers.OpenFile('System/Programs/Update OneOS.program')
  306. end
  307. end)
  308. else
  309. Log.i('OneOS is neither up to date or behind. (.version probably edited)')
  310. end
  311. else
  312. Current.Program:QueueEvent(event, url, data)
  313. end
  314. end
  315.  
  316. bedrock:RegisterEvent('http_success', AutoUpdateResponse)
  317. bedrock:RegisterEvent('http_failure', AutoUpdateFail)
  318.  
  319. function FirstSetup()
  320. bedrock:Run(function()
  321. Log.i('Reached First Setup GUI')
  322. bedrock:LoadView('firstsetup', false)
  323. Log.i('First Setup GUI Loaded')
  324.  
  325. Current.ProgramView = bedrock:GetObject('ProgramView')
  326. Helpers.OpenFile('System/Programs/First Setup.program', {isHidden = true})
  327. end)
  328. end
  329.  
  330. function Initialise()
  331. bedrock:Run(function()
  332. Log.i('Reached GUI')
  333. bedrock:LoadView('main', false)
  334. Log.i('GUI Loaded')
  335.  
  336. Current.ProgramView = bedrock:GetObject('ProgramView')
  337. Current.LoginView = bedrock:GetObject('LoginView')
  338. Current.Overlay = bedrock:GetObject('Overlay')
  339. Indexer.RefreshIndex()
  340.  
  341. bedrock:GetObject('ClickCatcherView').OnClick = function()
  342. if Current.SearchActive then
  343. Search.Close()
  344. end
  345. end
  346.  
  347. Current.Desktop = Helpers.OpenFile('System/Programs/Desktop.program', {isHidden = true})
  348.  
  349. Current.LoginView.OnUnlock = function(self, sleepMode)
  350. if not sleepMode then
  351. if Settings:GetValues()['StartupProgram'] then
  352. Helpers.OpenFile('Programs/'..Settings:GetValues()['StartupProgram'])
  353. UpdateOverlay()
  354. end
  355. UpdateOverlay()
  356. StartDoorWireless()
  357. CheckAutoUpdate()
  358. end
  359. end
  360. Current.LoginView:Lock()
  361. end)
  362. end
Add Comment
Please, Sign In to add comment