Advertisement
Guest User

Untitled

a guest
May 12th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.71 KB | None | 0 0
  1. local fs = require("filesystem")
  2. local term = require("term")
  3. local computer = require("computer")
  4. local keyboard = require("keyboard")
  5. local event = require("event")
  6. local gpu = require("component").gpu
  7. local serialization = require("serialization")
  8.  
  9. local logger = {};
  10.  
  11. function logger:init(program)
  12. self.loggerFolder = "/var/"..program.."/"
  13. local logNumber = 0
  14. local found = false
  15. while( not found) do
  16. if(fs.exists(self.loggerFolder.."log-"..logNumber..".log")) then
  17. logNumber = logNumber + 1
  18. else
  19. self.logNumber = logNumber
  20. found = true
  21. end
  22. end
  23.  
  24. self.initTime = os.time()
  25. end
  26.  
  27. function logger:ensurelogFolderExists()
  28. if not fs.exists(self.loggerFolder) then
  29. fs.makeDirectory(self.loggerFolder)
  30. end
  31. end
  32.  
  33. function logger:log(message)
  34. self:ensurelogFolderExists()
  35. local file = io.open(self.loggerFolder.."log-"..self.logNumber..".log","a")
  36. file:write("["..string.format("%07d",(os.time()-self.initTime)).."]"..message.."\n")
  37. file:close()
  38. end
  39.  
  40.  
  41. local versionNum = 0
  42. local versionName = "0.0"
  43.  
  44. logger:init("blockio")
  45.  
  46. logger:log("Launching Block.io")
  47. logger:log("Version "..versionName.." ("..versionNum..")")
  48.  
  49.  
  50.  
  51. if not term.isAvailable() then
  52. logger:log("Term is not avalible.")
  53. return
  54. end
  55.  
  56.  
  57.  
  58.  
  59.  
  60. local currentTab = 0
  61.  
  62. local tabs = {}
  63.  
  64. local packages = {}
  65.  
  66. blockio = {}
  67. local core = {}
  68.  
  69. local loadingProgress = 0
  70.  
  71.  
  72. function core.prePreInitCore()
  73. logger:log("Pre Pre Initializing core")
  74. logger:log("Free memory: " .. computer.freeMemory())
  75. gpu.setBackground(0xFFFFFF)
  76. gpu.setForeground(0x000000)
  77. local w , h = gpu.getResolution()
  78. gpu.fill(1,1,w,h," ")
  79. gpu.set(w/2 - 4, h/2, "BLOCK.IO")
  80. gpu.setBackground(0x000000)
  81. gpu.setForeground(0xFFFFFF)
  82. core.updateLoadingScreen(4.15)
  83.  
  84. function blockio.log(message)
  85. logger:log(message)
  86. end
  87.  
  88. function blockio.isPackageInstalled(packageName)
  89. checkArg(1,packageName,"string")
  90. return packages[packageName] ~= nil
  91. end
  92.  
  93. function blockio.getPaclageList()
  94. local keyset={}
  95. local n=0
  96.  
  97. for k,v in pairs(tab) do
  98. n=n+1
  99. keyset[n]=k
  100. end
  101.  
  102. return keyset
  103. end
  104.  
  105.  
  106. core.updateLoadingScreen(4.15)
  107.  
  108. end
  109.  
  110. function core.preInitCore()
  111. logger:log("Pre Initializing core")
  112. logger:log("Free memory: " .. computer.freeMemory())
  113.  
  114. core.updateLoadingScreen(4.15)
  115. core.updateLoadingScreen(4.15)
  116.  
  117. end
  118.  
  119. function core.postInitCore()
  120. logger:log("Post Initializing core")
  121. logger:log("Free memory: " .. computer.freeMemory())
  122. function blockio.addTab(tab)
  123. checkArg(1,tab,"table")
  124. core.addTab(tab)
  125. end
  126. function blockio.switchToTab(tab)
  127. checkArg(1,tab,"number")
  128. core.switchToTab(tab)
  129. end
  130. core.updateLoadingScreen(4.15)
  131. core.updateLoadingScreen(4.15)
  132.  
  133. end
  134.  
  135. function core.postPostInitCore()
  136. logger:log("Post Post Initializing core")
  137. logger:log("Free memory: " .. computer.freeMemory())
  138. function blockio.quit()
  139. logger:log("blockio.quit called.")
  140. core.softExit()
  141. end
  142. core.updateLoadingScreen(4.15)
  143. core.updateLoadingScreen(4.15)
  144.  
  145. end
  146.  
  147. function core.updateLoadingScreen(progress)
  148. loadingProgress = loadingProgress + progress
  149. local w , h = gpu.getResolution()
  150. gpu.setBackground(0x000000)
  151. gpu.setForeground(0xFFFFFF)
  152. gpu.set(w/2 - 5, h/2 + 1, string.format("%02d",loadingProgress).."% Loaded")
  153.  
  154. end
  155.  
  156. function core.addTab(tab)
  157. logger:log("Adding new tab.")
  158. local foundUID = false
  159. local UID = 0
  160. while not foundUID do
  161. UID = math.random(0xFFFFFFFF)
  162. foundUID = tabs[UID] == nil
  163. end
  164.  
  165. logger:log("New tab UID is "..UID)
  166.  
  167. tabs[UID] = tab
  168. tabs[UID].UID = UID
  169. if(tabs[UID]["init"]~= nil) then
  170. logger:log("init found in new tab , calling")
  171. local status, err = pcall(tabs[UID].init,tabs[UID])
  172.  
  173. if(status) then
  174. logger:log("init successful")
  175. else
  176. logger:log("init error: ")
  177. logger:log(serialization.serialize(err))
  178. end
  179. end
  180.  
  181.  
  182. end
  183.  
  184. function core.switchToTab(tab)
  185. logger:log("Switching from tab "..currentTab.." to tab "..tab)
  186.  
  187. if tabs[tab] ~= nil then
  188. error("There is no tab with UID "..tab)
  189. end
  190.  
  191.  
  192. if tabs[currentTab] ~= nil then
  193. if(tabs[currentTab]["leaveTab"]~= nil) then
  194. logger:log("leaveTab found in old tab , calling")
  195. local status, err = pcall(tabs[currentTab].leaveTab,tabs[currentTab])
  196.  
  197. if(status) then
  198. logger:log("leaveTab successful")
  199. else
  200. logger:log("leaveTab error: ")
  201. logger:log(serialization.serialize(err))
  202. end
  203. end
  204. end
  205.  
  206. if(tabs[tab]["enterTab"]~= nil) then
  207. logger:log("enterTab found in new tab , calling")
  208. local status, err = pcall(tabs[tab].enterTab,tabs[tab])
  209.  
  210. if(status) then
  211. logger:log("enterTab successful")
  212. else
  213. logger:log("enterTab error: ")
  214. logger:log(serialization.serialize(err))
  215. end
  216. end
  217. currentTab = tab
  218. end
  219.  
  220.  
  221.  
  222. function core.loadPackages()
  223. logger:log("Loading Packages")
  224. logger:log("Loading Packages into memoery")
  225. if(not fs.exists("/etc/blockio/")) then
  226. fs.makeDirectory("/etc/blockio/")
  227. end
  228. local packageFiles = fs.list("/etc/blockio/")
  229. local numberOfFiles = 0
  230. for packageFile in packageFiles do
  231. numberOfFiles = numberOfFiles + 1;
  232. end
  233. packageFiles = fs.list("/etc/blockio/")
  234. local numberOfPackages = 0
  235. for packageFile in packageFiles do
  236.  
  237. local path,name,extension = string.match(packageFile, "(.-)([^/]-([^/%.]+))$")
  238. name = string.gsub(name,".lua","")
  239. fileLoc = "/etc/blockio/"..name.."."..extension
  240.  
  241. logger:log("Found package :"..packageFile)
  242.  
  243. if(extension == "lua") then
  244. logger:log(name.." is lua , loading into packages")
  245. packages[name] = dofile(fileLoc)
  246.  
  247. numberOfPackages = numberOfPackages + 1
  248. end
  249. core.updateLoadingScreen(16.6/numberOfFiles)
  250. end
  251.  
  252. for packageName,table in pairs(packages) do
  253. for values , func in pairs(table) do
  254. logger:log(packageName.." contains ".. values)
  255. end
  256. end
  257.  
  258. for packageName,table in pairs(packages) do
  259. local loadingPerStep = 16.6/numberOfPackages
  260. logger:log("Pre initializing "..packageName)
  261. table.name = packageName;
  262. if(table["preInit"]~= nil) then
  263.  
  264. local status, err = pcall(table.preInit,table,loadingPerStep)
  265. if(status) then
  266. logger:log(packageName.."preInit successful")
  267. else
  268. logger:log("preInit error: ")
  269. logger:log(serialization.serialize(err))
  270. end
  271.  
  272. else
  273. logger:log("Pre init not found.")
  274. end
  275.  
  276. core.updateLoadingScreen(loadingPerStep)
  277. end
  278. core.preInitCore()
  279. for packageName,table in pairs(packages) do
  280. local loadingPerStep = 16.6/numberOfPackages
  281. logger:log("Initializing "..packageName)
  282. if(table["init"] ~= nil) then
  283. pcall(table.init,table,loadingPerStep)
  284. else
  285. logger:log("Init not found.")
  286. end
  287.  
  288. core.updateLoadingScreen(loadingPerStep)
  289. end
  290. core.postInitCore()
  291. for packageName,table in pairs(packages) do
  292. local loadingPerStep = 16.6/numberOfPackages
  293. logger:log("Post initializing "..packageName)
  294. if(table["postInit"] ~= nil) then
  295. pcall(table.postInit,table,loadingPerStep)
  296. else
  297. logger:log("Post init not found.")
  298. end
  299.  
  300. core.updateLoadingScreen(loadingPerStep)
  301. end
  302.  
  303.  
  304.  
  305. end
  306.  
  307. core.exit = false;
  308. function core.softExit()
  309. logger:log("softExit called.")
  310. core.exit = true
  311. end
  312.  
  313. function core.hardExit()
  314. logger:log("hardExit called.")
  315. logger:log("Forcing exit with os.exit().")
  316. os.exit()
  317. end
  318. function core.onKeyDown(char, code)
  319. if(keyboard.pressedCodes[code] == nil) then
  320. table.insert(keyboard.pressedCodes,code)
  321. table.insert(keyboard.pressedChars,char)
  322. end
  323. if keyboard.isControlDown() and code == keyboard.keys.q then
  324. core.softExit()
  325. end
  326. end
  327. function core.onKeyUp(char, code)
  328. if(keyboard.pressedCodes[code] ~= nil) then
  329. table.remove(keyboard.pressedCodes,code)
  330. table.remove(keyboard.pressedChars,char)
  331. end
  332. end
  333.  
  334. function core.main()
  335.  
  336. core.prePreInitCore()
  337. core.loadPackages()
  338. core.postPostInitCore()
  339.  
  340. logger:log("Entering Main Loop")
  341. while(not core.exit) do
  342. local event, address, arg1, arg2, arg3 = event.pull()
  343. if event == "key_down" then
  344. core.onKeyDown(arg1, arg2)
  345. elseif event == "key_up" then
  346. core.onKeyUp(arg1, arg2)
  347. end
  348. end
  349.  
  350. logger:log("Left main loop")
  351.  
  352. end
  353. logger:log("Entering Main")
  354. local status, err = pcall(core.main)
  355. if(status) then
  356. logger:log("Block.io exited with no errors. See you next time!")
  357. else
  358. logger:log("Block.io exited with the following error: ")
  359. logger:log(serialization.serialize(err))
  360. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement