Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.97 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1,1)
  3. print ("Logon")
  4. print ("Username: ")
  5. username = read()
  6. if username == "Admin" then
  7. print("Password: ")
  8. else
  9. print("User Not found.")
  10. sleep(5)
  11. os.shutdown()
  12. end
  13. password = read("*")
  14. if password == "lucas2004" then
  15. term.setCursorPos(1,1)
  16. term.clear()
  17. print("Checking")
  18. sleep(2)
  19. term.setCursorPos(1,1)
  20. term.clear()
  21. print("Logging in.")
  22. sleep(1)
  23. if vtype=='table' then
  24. local rval = {}
  25. -- Consider arrays separately
  26. local bArray, maxCount = isArray(v)
  27. if bArray then
  28. for i = 1,maxCount do
  29. table.insert(rval, encode(v[i]))
  30. end
  31. else -- An object, not an array
  32. for i,j in base.pairs(v) do
  33. if isEncodable(i) and isEncodable(j) then
  34. table.insert(rval, '"' .. encodeString(i) .. '":' .. encode(j))
  35. end
  36. end
  37. end
  38. if bArray then
  39. return '[' .. table.concat(rval,',') ..']'
  40. else
  41. return '{' .. table.concat(rval,',') .. '}'
  42. end
  43. end
  44.  
  45. -- Handle null values
  46. if vtype=='function' and v==null then
  47. return 'null'
  48. end
  49.  
  50. base.assert(false,'encode attempt to encode unsupported type ' .. vtype .. ':' .. base.tostring(v))
  51. end
  52.  
  53.  
  54. --- Decodes a JSON string and returns the decoded value as a Lua data structure / value.
  55. -- @param s The string to scan.
  56. -- @param [startPos] Optional starting position where the JSON string is located. Defaults to 1.
  57. -- @param Lua object, number The object that was scanned, as a Lua table / string / number / boolean or nil,
  58. -- and the position of the first character after
  59. -- the scanned JSON object.
  60. function decode(s, startPos)
  61. startPos = startPos and startPos or 1
  62. startPos = decode_scanWhitespace(s,startPos)
  63. base.assert(startPos<=string.len(s), 'Unterminated JSON encoded object found at position in [' .. s .. ']')
  64. local curChar = string.sub(s,startPos,startPos)
  65. -- Object
  66. if curChar=='{' then
  67. return decode_scanObject(s,startPos)
  68. end
  69. -- Array
  70. if curChar=='[' then
  71. return decode_scanArray(s,startPos)
  72. end
  73. -- Number
  74. if string.find("+-0123456789.e", curChar, 1, true) then
  75. return decode_scanNumber(s,startPos)
  76. end
  77. -- String
  78. if curChar=='"' or curChar=="'" then
  79. return decode_scanString(s,startPos)
  80. end
  81. if string.sub(s,startPos,startPos+1)=='/*' then
  82. return decode(s, decode_scanComment(s,startPos))
  83. end
  84. -- Otherwise, it must be a constant
  85. return decode_scanConstant(s,startPos)
  86. end
  87.  
  88. --- The null function allows one to specify a null value in an associative array (which is otherwise
  89. -- discarded if you set the value with 'nil' in Lua. Simply set t = { first=json.null }
  90. function null()
  91. return null -- so json.null() will also return null ;-)
  92. end
  93. -----------------------------------------------------------------------------
  94. -- Internal, PRIVATE functions.
  95. -- Following a Python-like convention, I have prefixed all these 'PRIVATE'
  96. -- functions with an underscore.
  97. -----------------------------------------------------------------------------
  98.  
  99. --- Scans an array from JSON into a Lua object
  100. -- startPos begins at the start of the array.
  101. -- Returns the array and the next starting position
  102. -- @param s The string being scanned.
  103. -- @param startPos The starting position for the scan.
  104. -- @return table, int The scanned array as a table, and the position of the next character to scan.
  105. function decode_scanArray(s,startPos)
  106. local array = {} -- The return value
  107. local stringLen = string.len(s)
  108. base.assert(string.sub(s,startPos,startPos)=='[','decode_scanArray called but array does not start at position ' .. startPos .. ' in string:\n'..s )
  109. startPos = startPos + 1
  110. -- Infinite loop for array elements
  111. repeat
  112. startPos = decode_scanWhitespace(s,startPos)
  113. base.assert(startPos<=stringLen,'JSON String ended unexpectedly scanning array.')
  114. local curChar = string.sub(s,startPos,startPos)
  115. if (curChar==']') then
  116. return array, startPos+1
  117. end
  118. if (curChar==',') then
  119. startPos = decode_scanWhitespace(s,startPos+1)
  120. end
  121. base.assert(startPos<=stringLen, 'JSON String ended unexpectedly scanning array.')
  122. object, startPos = decode(s,startPos)
  123. table.insert(array,object)
  124. until false
  125. end
  126.  
  127. --- Scans a comment and discards the comment.
  128. -- Returns the position of the next character following the comment.
  129. -- @param string s The JSON string to scan.
  130. -- @param int startPos The starting position of the comment
  131. function decode_scanComment(s, startPos)
  132. base.assert( string.sub(s,startPos,startPos+1)=='/*', "decode_scanComment called but comment does not start at position " .. startPos)
  133. local endPos = string.find(s,'*/',startPos+2)
  134. base.assert(endPos~=nil, "Unterminated comment in string at " .. startPos)
  135. return endPos+2
  136. end
  137.  
  138. --- Scans for given constants: true, false or null
  139. -- Returns the appropriate Lua type, and the position of the next character to read.
  140. -- @param s The string being scanned.
  141. -- @param startPos The position in the string at which to start scanning.
  142. -- @return object, int The object (true, false or nil) and the position at which the next character should be
  143. -- scanned.
  144. function decode_scanConstant(s, startPos)
  145. local consts = { ["true"] = true, ["false"] = false, ["null"] = nil }
  146. local constNames = {"true","false","null"}
  147.  
  148. for i,k in base.pairs(constNames) do
  149. --print ("[" .. string.sub(s,startPos, startPos + string.len(k) -1) .."]", k)
  150. if string.sub(s,startPos, startPos + string.len(k) -1 )==k then
  151. return consts[k], startPos + string.len(k)
  152. end
  153. end
  154. base.assert(nil, 'Failed to scan constant from string ' .. s .. ' at starting position ' .. startPos)
  155. end
  156.  
  157. --- Scans a number from the JSON encoded string.
  158. -- (in fact, also is able to scan numeric +- eqns, which is not
  159. -- in the JSON spec.)
  160. -- Returns the number, and the position of the next character
  161. -- after the number.
  162. -- @param s The string being scanned.
  163. -- @param startPos The position at which to start scanning.
  164. -- @return number, int The extracted number and the position of the next character to scan.
  165. function decode_scanNumber(s,startPos)
  166. local endPos = startPos+1
  167. local stringLen = string.len(s)
  168. local acceptableChars = "+-0123456789.e"
  169. while (string.find(acceptableChars, string.sub(s,endPos,endPos), 1, true)
  170. and endPos<=stringLen
  171. ) do
  172. endPos = endPos + 1
  173. end
  174. local stringValue = 'return ' .. string.sub(s,startPos, endPos-1)
  175. local stringEval = base.loadstring(stringValue)
  176. base.assert(stringEval, 'Failed to scan number [ ' .. stringValue .. '] in JSON string at position ' .. startPos .. ' : ' .. endPos)
  177. return stringEval(), endPos
  178. end
  179.  
  180. --- Scans a JSON object into a Lua object.
  181. -- startPos begins at the start of the object.
  182. -- Returns the object and the next starting position.
  183. -- @param s The string being scanned.
  184. -- @param startPos The starting position of the scan.
  185. -- @return table, int The scanned object as a table and the position of the next character to scan.
  186. function decode_scanObject(s,startPos)
  187. local object = {}
  188. local stringLen = string.len(s)
  189. local key, value
  190. base.assert(string.sub(s,startPos,startPos)=='{','decode_scanObject called but object does not start at position ' .. startPos .. ' in string:\n' .. s)
  191. startPos = startPos + 1
  192. repeat
  193. startPos = decode_scanWhitespace(s,startPos)
  194. base.assert(startPos<=stringLen, 'JSON string ended unexpectedly while scanning object.')
  195. local curChar = string.sub(s,startPos,startPos)
  196. if (curChar=='}') then
  197. return object,startPos+1
  198. end
  199. if (curChar==',') then
  200. startPos = decode_scanWhitespace(s,startPos+1)
  201. end
  202. base.assert(startPos<=stringLen, 'JSON string ended unexpectedly scanning object.')
  203. -- Scan the key
  204. key, startPos = decode(s,startPos)
  205. base.assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
  206. startPos = decode_scanWhitespace(s,startPos)
  207. base.assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
  208. base.assert(string.sub(s,startPos,startPos)==':','JSON object key-value assignment mal-formed at ' .. startPos)
  209. startPos = decode_scanWhitespace(s,startPos+1)
  210. base.assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
  211. value, startPos = decode(s,startPos)
  212. object[key]=value
  213. until false -- infinite loop while key-value pairs are found
  214. end
  215.  
  216. --- Scans a JSON string from the opening inverted comma or single quote to the
  217. -- end of the string.
  218. -- Returns the string extracted as a Lua string,
  219. -- and the position of the next non-string character
  220. -- (after the closing inverted comma or single quote).
  221. -- @param s The string being scanned.
  222. -- @param startPos The starting position of the scan.
  223. -- @return string, int The extracted string as a Lua string, and the next character to parse.
  224. function decode_scanString(s,startPos)
  225. base.assert(startPos, 'decode_scanString(..) called without start position')
  226. local startChar = string.sub(s,startPos,startPos)
  227. base.assert(startChar=="'" or startChar=='"','decode_scanString called for a non-string')
  228. local escaped = false
  229. local endPos = startPos + 1
  230. local bEnded = false
  231. local stringLen = string.len(s)
  232. repeat
  233. local curChar = string.sub(s,endPos,endPos)
  234. -- Character escaping is only used to escape the string delimiters
  235. if not escaped then
  236. if curChar=='\\' then
  237. escaped = true
  238. else
  239. bEnded = curChar==startChar
  240. end
  241. else
  242. -- If we're escaped, we accept the current character come what may
  243. escaped = false
  244. end
  245. endPos = endPos + 1
  246. base.assert(endPos <= stringLen+1, "String decoding failed: unterminated string at position " .. endPos)
  247. until bEnded
  248. local stringValue = 'return ' .. string.sub(s, startPos, endPos-1)
  249. local stringEval = base.loadstring(stringValue)
  250. base.assert(stringEval, 'Failed to load string [ ' .. stringValue .. '] in JSON4Lua.decode_scanString at position ' .. startPos .. ' : ' .. endPos)
  251. return stringEval(), endPos
  252. end
  253.  
  254. --- Scans a JSON string skipping all whitespace from the current start position.
  255. -- Returns the position of the first non-whitespace character, or nil if the whole end of string is reached.
  256. -- @param s The string being scanned
  257. -- @param startPos The starting position where we should begin removing whitespace.
  258. -- @return int The first position where non-whitespace was encountered, or string.len(s)+1 if the end of string
  259. -- was reached.
  260. function decode_scanWhitespace(s,startPos)
  261. local whitespace=" \n\r\t"
  262. local stringLen = string.len(s)
  263. while ( string.find(whitespace, string.sub(s,startPos,startPos), 1, true) and startPos <= stringLen) do
  264. startPos = startPos + 1
  265. end
  266. return startPos
  267. end
  268.  
  269. --- Encodes a string to be JSON-compatible.
  270. -- This just involves back-quoting inverted commas, back-quotes and newlines, I think ;-)
  271. -- @param s The string to return as a JSON encoded (i.e. backquoted string)
  272. -- @return The string appropriately escaped.
  273. function encodeString(s)
  274. s = string.gsub(s,'\\','\\\\')
  275. s = string.gsub(s,'"','\\"')
  276. s = string.gsub(s,"'","\\'")
  277. s = string.gsub(s,'\n','\\n')
  278. s = string.gsub(s,'\t','\\t')
  279. return s
  280. end
  281.  
  282. -- Determines whether the given Lua type is an array or a table / dictionary.
  283. -- We consider any table an array if it has indexes 1..n for its n items, and no
  284. -- other data in the table.
  285. -- I think this method is currently a little 'flaky', but can't think of a good way around it yet...
  286. -- @param t The table to evaluate as an array
  287. -- @return boolean, number True if the table can be represented as an array, false otherwise. If true,
  288. -- the second returned value is the maximum
  289. -- number of indexed elements in the array.
  290. function isArray(t)
  291. -- Next we count all the elements, ensuring that any non-indexed elements are not-encodable
  292. -- (with the possible exception of 'n')
  293. local maxIndex = 0
  294. for k,v in base.pairs(t) do
  295. if (base.type(k)=='number' and math.floor(k)==k and 1<=k) then -- k,v is an indexed pair
  296. if (not isEncodable(v)) then return false end -- All array elements must be encodable
  297. maxIndex = math.max(maxIndex,k)
  298. else
  299. if (k=='n') then
  300. if v ~= table.getn(t) then return false end -- False if n does not hold the number of elements
  301. else -- Else of (k=='n')
  302. if isEncodable(v) then return false end
  303. end -- End of (k~='n')
  304. end -- End of k,v not an indexed pair
  305. end -- End of loop across all pairs
  306. return true, maxIndex
  307. end
  308.  
  309. --- Determines whether the given Lua object / table / variable can be JSON encoded. The only
  310. -- types that are JSON encodable are: string, boolean, number, nil, table and json.null.
  311. -- In this implementation, all other types are ignored.
  312. -- @param o The object to examine.
  313. -- @return boolean True if the object should be JSON encoded, false if it should be ignored.
  314. function isEncodable(o)
  315. local t = base.type(o)
  316. return (t=='string' or t=='boolean' or t=='number' or t=='nil' or t=='table') or (t=='function' and o==null)
  317. end
  318. ]]
  319.  
  320. function loadJSON()
  321. local sName = 'JSON'
  322.  
  323. local tEnv = {}
  324. setmetatable( tEnv, { __index = _G } )
  325. local fnAPI, err = loadstring(_jstr)
  326. if fnAPI then
  327. setfenv( fnAPI, tEnv )
  328. fnAPI()
  329. else
  330. printError( err )
  331. return false
  332. end
  333.  
  334. local tAPI = {}
  335. for k,v in pairs( tEnv ) do
  336. tAPI[k] = v
  337. end
  338.  
  339. _G[sName] = tAPI
  340. return true
  341. end
  342.  
  343. local mainTitle = 'OneOS Installer'
  344. local subTitle = 'Please wait...'
  345.  
  346. function Draw()
  347. sleep(0)
  348. term.setBackgroundColour(colours.white)
  349. term.clear()
  350. local w, h = term.getSize()
  351. term.setTextColour(colours.lightBlue)
  352. term.setCursorPos(math.ceil((w-#mainTitle)/2), 8)
  353. term.write(mainTitle)
  354. term.setTextColour(colours.blue)
  355. term.setCursorPos(math.ceil((w-#subTitle)/2), 10)
  356. term.write(subTitle)
  357. end
  358.  
  359. tArgs = {...}
  360.  
  361. Settings = {
  362. InstallPath = '/', --Where the program's installed, don't always asume root (if it's run under something like OneOS)
  363. Hidden = false, --Whether or not the update is hidden (doesn't write to the screen), useful for background updates
  364. GitHubUsername = 'oeed', --Your GitHub username as it appears in the URL
  365. GitHubRepoName = 'OneOS', --The repo name as it appears in the URL
  366. DownloadReleases = true, --If true it will download the latest release, otherwise it will download the files as they currently appear
  367. UpdateFunction = nil, --Sent when something happens (file downloaded etc.)
  368. TotalBytes = 0, --Do not change this value (especially programatically)!
  369. DownloadedBytes = 0, --Do not change this value (especially programatically)!
  370. Status = '',
  371. SecondaryStatus = '',
  372. }
  373.  
  374. loadJSON()
  375.  
  376. function downloadJSON(path)
  377. local _json = http.get(path)
  378. if not _json then
  379. error('Could not download: '..path..' Check your connection.')
  380. end
  381. return JSON.decode(_json.readAll())
  382. end
  383.  
  384. if http then
  385. subTitle = 'HTTP enabled, attempting update...'
  386. Draw()
  387. else
  388. subTitle = 'HTTP is required to update.'
  389. Draw()
  390. error('')
  391. end
  392.  
  393. subTitle = 'Determining Latest Version'
  394. Draw()
  395. local releases = downloadJSON('https://api.github.com/repos/'..Settings.GitHubUsername..'/'..Settings.GitHubRepoName..'/releases')
  396. local latestReleaseTag = releases[1].tag_name
  397. if not tArgs or #tArgs ~= 1 and tArgs[1] ~= 'beta' then
  398. for i, v in ipairs(releases) do
  399. if not v.prerelease then
  400. latestReleaseTag = v.tag_name
  401. break
  402. end
  403. end
  404. end
  405. subTitle = 'Optaining Latest Version URL'
  406. Draw()
  407. local refs = downloadJSON('https://api.github.com/repos/'..Settings.GitHubUsername..'/'..Settings.GitHubRepoName..'/git/refs/tags/'..latestReleaseTag)
  408. local latestReleaseSha = refs.object.sha
  409.  
  410. subTitle = 'Downloading File Listing'
  411. Draw()
  412.  
  413. local tree = downloadJSON('https://api.github.com/repos/'..Settings.GitHubUsername..'/'..Settings.GitHubRepoName..'/git/trees/'..latestReleaseSha..'?recursive=1').tree
  414.  
  415. local blacklist = {
  416. '/.gitignore',
  417. '/README.md',
  418. '/TODO',
  419. '/Desktop/.Desktop.settings',
  420. '/.version'
  421. }
  422.  
  423. function isBlacklisted(path)
  424. for i, item in ipairs(blacklist) do
  425. if item == path then
  426. return true
  427. end
  428. end
  429. return false
  430. end
  431.  
  432. Settings.TotalFiles = 0
  433. Settings.TotalBytes = 0
  434. for i, v in ipairs(tree) do
  435. if not isBlacklisted(Settings.InstallPath..v.path) and v.size then
  436. Settings.TotalBytes = Settings.TotalBytes + v.size
  437. Settings.TotalFiles = Settings.TotalFiles + 1
  438. end
  439. end
  440.  
  441. Settings.DownloadedBytes = 0
  442. Settings.DownloadedFiles = 0
  443. function downloadBlob(v)
  444. if isBlacklisted(Settings.InstallPath..v.path) then
  445. return
  446. end
  447. if v.type == 'tree' then
  448. -- subTitle = 'Making folder: '..'/'..Settings.InstallPath..v.path
  449. Draw()
  450. fs.makeDir('/'..Settings.InstallPath..v.path)
  451. else
  452. -- subTitle = 'Starting download for: '..Settings.InstallPath..v.path
  453. Draw()
  454.  
  455. local tries, f = 0
  456. repeat
  457. f = http.get(('https://raw.github.com/'..Settings.GitHubUsername..'/'..Settings.GitHubRepoName..'/'..latestReleaseTag..Settings.InstallPath..v.path):gsub(' ','%%20'))
  458. if not f then sleep(5) end
  459. tries = tries + 1
  460. until tries > 5 or f
  461.  
  462. if not f then
  463. error('Downloading failed, try again. '..('https://raw.github.com/'..Settings.GitHubUsername..'/'..Settings.GitHubRepoName..'/'..latestReleaseTag..Settings.InstallPath..v.path):gsub(' ','%%20'))
  464. end
  465.  
  466. local h = fs.open('/'..Settings.InstallPath..v.path, 'w')
  467. h.write(f.readAll())
  468. h.close()
  469. -- subTitle = 'Downloading: ' .. math.floor(100*(Settings.DownloadedBytes/Settings.TotalBytes))..'%'
  470. subTitle = 'Downloading: ' .. math.floor(100*(Settings.DownloadedFiles/Settings.TotalFiles))..'%' -- using the number of files over the number of bytes actually appears to be more accurate, the connection takes longer than sending the data
  471. -- subTitle = '('..math.floor(100*(Settings.DownloadedBytes/Settings.TotalBytes))..'%) Downloaded: '..Settings.InstallPath..v.path
  472. Draw()
  473. if v.size then
  474. Settings.DownloadedBytes = Settings.DownloadedBytes + v.size
  475. Settings.DownloadedFiles = Settings.DownloadedFiles + 1
  476. end
  477. end
  478. end
  479.  
  480. local connectionLimit = 5
  481. local downloads = {}
  482. for i, v in ipairs(tree) do
  483. local queueNumber = math.ceil(i / connectionLimit)
  484. if not downloads[queueNumber] then
  485. downloads[queueNumber] = {}
  486. end
  487. table.insert(downloads[queueNumber], function()
  488. downloadBlob(v)
  489. end)
  490. end
  491.  
  492. for i, queue in ipairs(downloads) do
  493. parallel.waitForAll(unpack(queue))
  494. end
  495.  
  496. local h = fs.open('/System/.version', 'w')
  497. h.write(latestReleaseTag)
  498. h.close()
  499.  
  500. mainTitle = 'Installation Complete!'
  501. subTitle = 'Rebooting in 1 second...'
  502. Draw()
  503. sleep(1)
  504. os.reboot()
  505. else
  506. term.setCursorPos(1,1)
  507. term.clear()
  508. print ("Checking")
  509. sleep(1)
  510. term.setCursorPos(1,1)
  511. term.clear()
  512. print("Incorrect")
  513. sleep(2)
  514. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement