Advertisement
Mackan90096

SkyOS 0.0.1

May 16th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.81 KB | None | 0 0
  1. --[[
  2. SkyOS 0.0.1 Copyright Max Thor (thormax5@gmail.com)
  3. --]]
  4.  
  5. -- Variables
  6. local w, h = term.getSize()
  7. --[[
  8. States:
  9. 0: setup
  10. 1: running
  11. 2: programs
  12. 3: BSOD
  13. 4: Menu on start
  14. 5: Run window
  15. --]]
  16.  
  17. local state = 0
  18. local bgC = colors.blue
  19.  
  20. -- Splitlines
  21.  
  22. function split(str, max_line_length)
  23. local lines = {}
  24. local line
  25. str:gsub('(%s*)(%S+)',
  26. function(spc, word)
  27. if not line or #line + #spc + #word > max_line_length then
  28. table.insert(lines, line)
  29. line = word
  30. else
  31. line = line..spc..word
  32. end
  33. end
  34. )
  35. table.insert(lines, line)
  36. return lines
  37. end
  38.  
  39. function desktop()
  40. state = 1
  41. term.setTextColor(colors.white)
  42. term.setBackgroundColor(colors.cyan)
  43. term.clear()
  44. term.setCursorPos(1,1)
  45. term.setBackgroundColor(colors.blue)
  46. term.clearLine()
  47. term.setBackgroundColor(colors.cyan)
  48. print("Programs")
  49. end
  50.  
  51. function run()
  52. desktop()
  53. paintutils.drawFilledBox(w/2, h/2, w/2+10, h/2+2, colors.gray)
  54. term.setBackgroundColor(colors.lightGray)
  55. term.setCursorPos(w/2+2, h/2+1)
  56. term.setTextColor(colors.yellow)
  57. print("> ")
  58. term.setCursorPos(w/2+3, h/2+1)
  59. local prog = read()
  60. shell.switchTab( shell.openTab(prog) )
  61. desktop()
  62. end
  63.  
  64. function files()
  65. desktop()
  66. if not fs.exists("FileX") then
  67. shell.run("pastebin run AVqAFH8h")
  68. else
  69. shell.switchTab(shell.openTab("FileX"))
  70. end
  71. end
  72.  
  73. function login()
  74. term.setBackgroundColor(colors.cyan)
  75. term.clear()
  76. cPrint("SkyOS - Login", 2)
  77. cPrint("Name: ", 4)
  78. cPrint("Pass: ", 6)
  79. term.setCursorPos(math.max(math.floor((w / 2) - (string.len("Name: ") / 2)), 0)+5, 4)
  80. term.setBackgroundColor(colors.white)
  81. print(" ")
  82. term.setBackgroundColor(colors.cyan)
  83. term.setCursorPos(math.max(math.floor((w / 2) - (string.len("Pass: ") / 2)), 0)+5, 6)
  84. term.setBackgroundColor(colors.white)
  85. print(" ")
  86. term.setBackgroundColor(colors.cyan)
  87. term.setBackgroundColor(colors.white)
  88. term.setTextColor(colors.black)
  89. term.setCursorPos(math.max(math.floor((w / 2) - (string.len("Name: ") / 2)), 0)+5, 4)
  90. local name = read()
  91. term.setTextColor(colors.black)
  92. term.setCursorPos(math.max(math.floor((w / 2) - (string.len("Pass: ") / 2)), 0)+5, 6)
  93. local pass = read('*')
  94.  
  95. if(fs.exists("users/"..name)) then
  96. local f = fs.open("users/"..name, "r")
  97. local bPass = f.readAll()
  98. f.close()
  99. if pass == bPass then
  100. term.setTextColor(colors.white)
  101. return true
  102. else
  103. print("Wrong password or username!")
  104. sleep(1)
  105. os.reboot()
  106. end
  107. else
  108. print("Wrong password or username!")
  109. sleep(1)
  110. os.reboot()
  111. end
  112. end
  113.  
  114. -- Run OS
  115. function start()
  116. if login() then
  117. state = 1
  118. term.setBackgroundColor(colors.cyan)
  119. term.clear()
  120. term.setCursorPos(1,1)
  121. term.setBackgroundColor(colors.blue)
  122. term.clearLine()
  123. term.setBackgroundColor(colors.cyan)
  124. print("Programs")
  125.  
  126. -- Events
  127. while true do
  128. e, p1, p2, p3, p4, p5 = os.pullEvent()
  129. if e == "mouse_click" then
  130. if state == 1 then
  131. if p3 == 1 then
  132. if p2 >= 1 and p2 <= string.len("Programs") then
  133. state = 4
  134. term.setBackgroundColor(colors.gray)
  135. term.setCursorPos(1, 2)
  136. print(" Run ")
  137. term.setCursorPos(1, 3)
  138. print(" Files ")
  139. term.setCursorPos(1, 4)
  140. print("Shutdown")
  141. term.setCursorPos(1, 5)
  142. print(" Reboot ")
  143. term.setCursorPos(1, 6)
  144. print(" Exit ")
  145. end
  146. end
  147. elseif state == 4 then
  148. if p3 == 2 and p2 >= 1 and p2 <= string.len(" Run ") then
  149. run()
  150. elseif p3 == 3 and p2 >= 1 and p2 <= string.len(" Files ") then
  151. files()
  152. elseif p3 == 4 and p2 >= 1 and p2 <= string.len("Shutdown") then
  153. os.shutdown()
  154. elseif p3 == 5 and p2 >= 1 and p2 <= string.len(" Reboot ") then
  155. os.reboot()
  156. elseif p3 == 6 and p2 >= 1 and p2 <= string.len(" Exit ") then
  157. term.setBackgroundColor(colors.black)
  158. term.setTextColor(colors.white)
  159. term.clear()
  160. term.setCursorPos(1,1)
  161. return true
  162. else
  163. state = 1
  164. desktop()
  165. end
  166. end
  167. end
  168. end
  169. end
  170. end
  171.  
  172. -- First Run
  173.  
  174. -- Center print text
  175.  
  176. function cPrint(sText, y)
  177. local x = math.max(math.floor((w / 2) - (#sText / 2)), 0)
  178. term.setCursorPos(x, y)
  179. print(sText)
  180. end
  181.  
  182. function setup()
  183. if not fs.exists("FileX") then
  184. shell.run("pastebin run AVqAFH8h")
  185. end
  186. term.setBackgroundColor(colors.cyan)
  187. term.clear()
  188. cPrint("SkyOS Installer", 1)
  189. cPrint("Name: ", 3)
  190. term.setCursorPos(math.max(math.floor((w / 2) - (string.len("Name: ") / 2)), 0)+5, 3)
  191. term.setBackgroundColor(colors.white)
  192. print(" ")
  193. term.setBackgroundColor(colors.cyan)
  194. cPrint("Label: ", 5)
  195. term.setCursorPos(math.max(math.floor((w / 2) - (string.len("Label: ") / 2)), 0)+5, 5)
  196. term.setBackgroundColor(colors.white)
  197. print(" ")
  198. term.setBackgroundColor(colors.cyan)
  199. cPrint("Pass: ", 7)
  200. term.setCursorPos(math.max(math.floor((w / 2) - (string.len("Pass: ") / 2)), 0)+5, 7)
  201. term.setBackgroundColor(colors.white)
  202. print(" ")
  203. term.setBackgroundColor(colors.blue)
  204. cPrint(" Done! ", 9)
  205. --[[TODO:
  206. cPrint("Background Color: ", 7)
  207. --paintutils.drawPixel(math.max(math.floor((w / 2) - (string.len("Background Color: ") / 2)), 0)+string.len("Background Color: "), 7, bgC)
  208. -- BG Color choose
  209. local cols = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768}
  210. local x = 0
  211. for k, v in pairs(cols) do
  212. term.setBackgroundColor(v)
  213. if k < 9 then
  214. term.setCursorPos(math.max(math.floor((w / 2) - (string.len(" ") / 2)), 0)+x, 9)
  215. print(" ")
  216. x = x+1
  217. else
  218. x = x-1
  219. term.setCursorPos(math.max(math.floor((w / 2) - (string.len(" ") / 2)), 0)+x, 10)
  220. print(" ")
  221. end
  222. end
  223. --]]
  224.  
  225. -- Catch events
  226. while true do
  227. e, p1, p2, p3, p4, p5 = os.pullEvent()
  228. if e == "mouse_click" then
  229. if state == 0 then
  230. if p3 == 3 and p2 >= math.max(math.floor((w / 2) - (string.len("Name: ") / 2)), 0) and p2 <= math.max(math.floor((w / 2) - (string.len("Name: ") / 2)), 0)+5 then
  231. term.setBackgroundColor(colors.white)
  232. term.setTextColor(colors.black)
  233. term.setCursorPos(math.max(math.floor((w / 2) - (string.len("Name: ") / 2)), 0)+5, 3)
  234. name = read()
  235. print(name)
  236. elseif p3 == 5 and p2 >= math.max(math.floor((w / 2) - (string.len("Label: ") / 2)), 0) and p2 <= math.max(math.floor((w / 2) - (string.len("Label: ") / 2)), 0)+5 then
  237. term.setBackgroundColor(colors.white)
  238. term.setTextColor(colors.black)
  239. term.setCursorPos(math.max(math.floor((w / 2) - (string.len("Label: ") / 2)), 0)+5, 5)
  240. local label = read()
  241. os.setComputerLabel(label)
  242. elseif p3 == 7 and p2 >= math.max(math.floor((w / 2) - (string.len("Pass: ") / 2)), 0) and p2 <= math.max(math.floor((w / 2) - (string.len("Pass: ") / 2)), 0)+6 then
  243. term.setBackgroundColor(colors.white)
  244. term.setTextColor(colors.black)
  245. term.setCursorPos(math.max(math.floor((w / 2) - (string.len("Pass: ") / 2)), 0)+5, 7)
  246. pass = read('*')
  247. elseif p3 == 9 and p2 >= math.max(math.floor((w / 2) - (string.len(" Done! ") / 2)), 0) and p2 <= math.max(math.floor((w / 2) - (string.len(" Done! ") / 2)), 0)+7 then
  248. local f = fs.open("./config.cfg", "w")
  249. if not fs.exists("./users") then
  250. fs.makeDir("./users")
  251. end
  252. local q = fs.open("./users/"..name, "w")
  253. q.write(pass)
  254. q.close()
  255. --local data = textutils.serialize()
  256. f.write("Setup")
  257. f.close()
  258. os.reboot()
  259. end
  260. end
  261. end
  262. end
  263.  
  264. return true
  265. end
  266.  
  267. -- BSOD
  268.  
  269. function bsod(error)
  270. state = 3
  271. term.setTextColor(colors.white)
  272. term.setBackgroundColor(colors.cyan)
  273. term.clear()
  274. cPrint("Error!", 2)
  275. local x = 4
  276. local btn1 = 6
  277. local btn2 = 8
  278. for _, line in ipairs(split(error, 20)) do
  279. cPrint(line, x)
  280. x = x+1
  281. btn1 = btn1+1
  282. btn2 = btn2+1
  283. end
  284.  
  285. term.setBackgroundColor(colors.blue)
  286. cPrint(" Exit ", btn1)
  287. cPrint(" Reboot ", btn2)
  288.  
  289. -- Events
  290. while true do
  291. e, p1, p2, p3, p4, p5 = os.pullEvent()
  292. if e == "mouse_click" then
  293. if state == 3 then
  294. if p3 == btn1 and p2 >= math.max(math.floor((w / 2) - (string.len(" Exit ") / 2)), 0) and p2 <= math.max(math.floor((w / 2) - (string.len(" Exit ") / 2)), 0)+6 then
  295. term.setBackgroundColor(colors.black)
  296. term.setTextColor(colors.white)
  297. term.clear()
  298. term.setCursorPos(1,1)
  299. return true
  300. elseif p3 == btn2 and p2 >= math.max(math.floor((w / 2) - (string.len(" Reboot ") / 2)), 0) and p2 <= math.max(math.floor((w / 2) - (string.len(" Reboot ") / 2)), 0)+8 then
  301. os.reboot()
  302. end
  303. end
  304. end
  305. end
  306. end
  307.  
  308.  
  309.  
  310.  
  311. -- Boot thingy
  312.  
  313. if term.isColor() then
  314. if fs.exists('./config.cfg') then
  315. local ok, err = pcall(start)
  316. if not ok then
  317. -- BSOD
  318. bsod(err)
  319. end
  320. else
  321. local ok, err = pcall(setup)
  322. if not ok then
  323. -- BSOD
  324. bsod(err)
  325. end
  326. end
  327. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement