Advertisement
MCFunRide

again

Aug 1st, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.29 KB | None | 0 0
  1. local nativestringfind = string.find
  2. local nativestringsub = string.sub
  3. function string.sub( ... )
  4. local r = nativestringsub( ... )
  5. if r then
  6. return r .. ""
  7. end
  8. return nil
  9. end
  10. function string.find( s, ... )
  11. return nativestringfind( s .. "", ... );
  12. end
  13.  
  14. -- Install lua parts of the os api
  15. function os.version()
  16. return "CraftOS 1.7"
  17. end
  18.  
  19. function os.pullEventRaw( sFilter )
  20. return coroutine.yield( sFilter )
  21. end
  22.  
  23. function os.pullEvent( sFilter )
  24. local eventData = { os.pullEventRaw( sFilter ) }
  25. if eventData[1] == "terminate" then
  26. error( "Terminated", 0 )
  27. end
  28. return unpack( eventData )
  29. end
  30.  
  31. -- Install globals
  32. function sleep( nTime )
  33. local timer = os.startTimer( nTime or 0 )
  34. repeat
  35. local sEvent, param = os.pullEvent( "timer" )
  36. until param == timer
  37. end
  38.  
  39. function write( sText )
  40. local w,h = term.getSize()
  41. local x,y = term.getCursorPos()
  42.  
  43. local nLinesPrinted = 0
  44. local function newLine()
  45. if y + 1 <= h then
  46. term.setCursorPos(1, y + 1)
  47. else
  48. term.setCursorPos(1, h)
  49. term.scroll(1)
  50. end
  51. x, y = term.getCursorPos()
  52. nLinesPrinted = nLinesPrinted + 1
  53. end
  54.  
  55. -- Print the line with proper word wrapping
  56. while string.len(sText) > 0 do
  57. local whitespace = string.match( sText, "^[ \t]+" )
  58. if whitespace then
  59. -- Print whitespace
  60. term.write( whitespace )
  61. x,y = term.getCursorPos()
  62. sText = string.sub( sText, string.len(whitespace) + 1 )
  63. end
  64.  
  65. local newline = string.match( sText, "^\n" )
  66. if newline then
  67. -- Print newlines
  68. newLine()
  69. sText = string.sub( sText, 2 )
  70. end
  71.  
  72. local text = string.match( sText, "^[^ \t\n]+" )
  73. if text then
  74. sText = string.sub( sText, string.len(text) + 1 )
  75. if string.len(text) > w then
  76. -- Print a multiline word
  77. while string.len( text ) > 0 do
  78. if x > w then
  79. newLine()
  80. end
  81. term.write( text )
  82. text = string.sub( text, (w-x) + 2 )
  83. x,y = term.getCursorPos()
  84. end
  85. else
  86. -- Print a word normally
  87. if x + string.len(text) - 1 > w then
  88. newLine()
  89. end
  90. term.write( text )
  91. x,y = term.getCursorPos()
  92. end
  93. end
  94. end
  95.  
  96.  
  97. -- Setup paths
  98. local sPath = ".:/rom/programs"
  99. if term.isColor() then
  100. sPath = sPath..":/rom/programs/advanced"
  101. end
  102. if turtle then
  103. sPath = sPath..":/rom/programs/turtle"
  104. else
  105. sPath = sPath..":/rom/programs/rednet:/rom/programs/fun"
  106. if term.isColor() then
  107. sPath = sPath..":/rom/programs/fun/advanced"
  108. end
  109. end
  110. if pocket then
  111. sPath = sPath..":/rom/programs/pocket"
  112. end
  113. if commands then
  114. sPath = sPath..":/rom/programs/command"
  115. end
  116. if http then
  117. sPath = sPath..":/rom/programs/http"
  118. end
  119. shell.setPath( sPath )
  120. help.setPath( "/rom/help" )
  121.  
  122. -- Setup aliases
  123. shell.setAlias( "ls", "list" )
  124. shell.setAlias( "dir", "list" )
  125. shell.setAlias( "cp", "copy" )
  126. shell.setAlias( "mv", "move" )
  127. shell.setAlias( "rm", "delete" )
  128. shell.setAlias( "clr", "clear" )
  129. shell.setAlias( "rs", "redstone" )
  130. shell.setAlias( "sh", "shell" )
  131. if term.isColor() then
  132. shell.setAlias( "background", "bg" )
  133. shell.setAlias( "foreground", "fg" )
  134. end
  135.  
  136. -- Run autorun files
  137. if fs.exists( "/rom/autorun" ) and fs.isDir( "/rom/autorun" ) then
  138. local tFiles = fs.list( "/rom/autorun" )
  139. table.sort( tFiles )
  140. for n, sFile in ipairs( tFiles ) do
  141. if string.sub( sFile, 1, 1 ) ~= "." then
  142. local sPath = "/rom/autorun/"..sFile
  143. if not fs.isDir( sPath ) then
  144. shell.run( sPath )
  145. end
  146. end
  147. end
  148. end
  149.  
  150. -- Run the user created startup, either from disk drives or the root
  151. local sUserStartup = shell.resolveProgram( "/startup" )
  152. local bEnableDiskStartup = (commands == nil)
  153. if bEnableDiskStartup then
  154. for n,sName in pairs( peripheral.getNames() ) do
  155. if disk.isPresent( sName ) and disk.hasData( sName ) then
  156. local sDiskStartup = shell.resolveProgram( "/" .. disk.getMountPath( sName ) .. "/startup" )
  157. if sDiskStartup then
  158. sUserStartup = sDiskStartup
  159. break
  160. end
  161. end
  162. end
  163. end
  164. if sUserStartup then
  165. shell.run( sUserStartup )
  166. end
  167.  
  168.  
  169. return nLinesPrinted
  170. end
  171.  
  172. function print( ... )
  173. local nLinesPrinted = 0
  174. for n,v in ipairs( { ... } ) do
  175. nLinesPrinted = nLinesPrinted + write( tostring( v ) )
  176. end
  177. nLinesPrinted = nLinesPrinted + write( "\n" )
  178. return nLinesPrinted
  179. end
  180.  
  181. function printError( ... )
  182. if term.isColour() then
  183. term.setTextColour( colours.red )
  184. end
  185. local x,y = term.getCursorPos()
  186. print( ... )
  187. term.setTextColour( colours.white )
  188. end
  189.  
  190. function read( _sReplaceChar, _tHistory )
  191. term.setCursorBlink( true )
  192.  
  193. local sLine = ""
  194. local nHistoryPos
  195. local nPos = 0
  196. if _sReplaceChar then
  197. _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  198. end
  199.  
  200. local w = term.getSize()
  201. local sx = term.getCursorPos()
  202.  
  203. local function redraw( _sCustomReplaceChar )
  204. local nScroll = 0
  205. if sx + nPos >= w then
  206. nScroll = (sx + nPos) - w
  207. end
  208.  
  209. local cx,cy = term.getCursorPos()
  210. term.setCursorPos( sx, cy )
  211. local sReplace = _sCustomReplaceChar or _sReplaceChar
  212. if sReplace then
  213. term.write( string.rep( sReplace, math.max( string.len(sLine) - nScroll, 0 ) ) )
  214. else
  215. term.write( string.sub( sLine, nScroll + 1 ) )
  216. end
  217. term.setCursorPos( sx + nPos - nScroll, cy )
  218. end
  219.  
  220. while true do
  221. local sEvent, param = os.pullEvent()
  222. if sEvent == "char" then
  223. -- Typed key
  224. sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  225. nPos = nPos + 1
  226. redraw()
  227.  
  228. elseif sEvent == "paste" then
  229. -- Pasted text
  230. sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  231. nPos = nPos + string.len( param )
  232. redraw()
  233.  
  234. elseif sEvent == "key" then
  235. if param == keys.enter then
  236. -- Enter
  237. break
  238.  
  239. elseif param == keys.left then
  240. -- Left
  241. if nPos > 0 then
  242. nPos = nPos - 1
  243. redraw()
  244. end
  245.  
  246. elseif param == keys.right then
  247. -- Right
  248. if nPos < string.len(sLine) then
  249. redraw(" ")
  250. nPos = nPos + 1
  251. redraw()
  252. end
  253.  
  254. elseif param == keys.up or param == keys.down then
  255. -- Up or down
  256. if _tHistory then
  257. redraw(" ")
  258. if param == keys.up then
  259. -- Up
  260. if nHistoryPos == nil then
  261. if #_tHistory > 0 then
  262. nHistoryPos = #_tHistory
  263. end
  264. elseif nHistoryPos > 1 then
  265. nHistoryPos = nHistoryPos - 1
  266. end
  267. else
  268. -- Down
  269. if nHistoryPos == #_tHistory then
  270. nHistoryPos = nil
  271. elseif nHistoryPos ~= nil then
  272. nHistoryPos = nHistoryPos + 1
  273. end
  274. end
  275. if nHistoryPos then
  276. sLine = _tHistory[nHistoryPos]
  277. nPos = string.len( sLine )
  278. else
  279. sLine = ""
  280. nPos = 0
  281. end
  282. redraw()
  283. end
  284. elseif param == keys.backspace then
  285. -- Backspace
  286. if nPos > 0 then
  287. redraw(" ")
  288. sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  289. nPos = nPos - 1
  290. redraw()
  291. end
  292. elseif param == keys.home then
  293. -- Home
  294. redraw(" ")
  295. nPos = 0
  296. redraw()
  297. elseif param == keys.delete then
  298. -- Delete
  299. if nPos < string.len(sLine) then
  300. redraw(" ")
  301. sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )
  302. redraw()
  303. end
  304. elseif param == keys["end"] then
  305. -- End
  306. redraw(" ")
  307. nPos = string.len(sLine)
  308. redraw()
  309. end
  310.  
  311. elseif sEvent == "term_resize" then
  312. -- Terminal resized
  313. w = term.getSize()
  314. redraw()
  315.  
  316. end
  317. end
  318.  
  319. local cx, cy = term.getCursorPos()
  320. term.setCursorBlink( false )
  321. term.setCursorPos( w + 1, cy )
  322. print()
  323.  
  324. return sLine
  325. end
  326.  
  327. loadfile = function( _sFile )
  328. local file = fs.open( _sFile, "r" )
  329. if file then
  330. local func, err = loadstring( file.readAll(), fs.getName( _sFile ) )
  331. file.close()
  332. return func, err
  333. end
  334. return nil, "File not found"
  335. end
  336.  
  337. dofile = function( _sFile )
  338. local fnFile, e = loadfile( _sFile )
  339. if fnFile then
  340. setfenv( fnFile, getfenv(2) )
  341. return fnFile()
  342. else
  343. error( e, 2 )
  344. end
  345. end
  346.  
  347. -- Install the rest of the OS api
  348. function os.run( _tEnv, _sPath, ... )
  349. local tArgs = { ... }
  350. local fnFile, err = loadfile( _sPath )
  351. if fnFile then
  352. local tEnv = _tEnv
  353. --setmetatable( tEnv, { __index = function(t,k) return _G[k] end } )
  354. setmetatable( tEnv, { __index = _G } )
  355. setfenv( fnFile, tEnv )
  356. local ok, err = pcall( function()
  357. fnFile( unpack( tArgs ) )
  358. end )
  359. if not ok then
  360. if err and err ~= "" then
  361. printError( err )
  362. end
  363. return false
  364. end
  365. return true
  366. end
  367. if err and err ~= "" then
  368. printError( err )
  369. end
  370. return false
  371. end
  372.  
  373. -- Prevent access to metatables or environments of strings, as these are global between all computers
  374. do
  375. local nativegetfenv = getfenv
  376. local nativegetmetatable = getmetatable
  377. local nativeerror = error
  378. local nativetype = type
  379. local string_metatable = nativegetmetatable("")
  380. local string_env = nativegetfenv(("").gsub)
  381. function getmetatable( t )
  382. local mt = nativegetmetatable( t )
  383. if mt == string_metatable or mt == string_env then
  384. nativeerror( "Attempt to access string metatable", 2 )
  385. else
  386. return mt
  387. end
  388. end
  389. function getfenv( env )
  390. if env == nil then
  391. env = 2
  392. elseif nativetype( env ) == "number" and env > 0 then
  393. env = env + 1
  394. end
  395. local fenv = nativegetfenv(env)
  396. if fenv == string_metatable or fenv == string_env then
  397. --nativeerror( "Attempt to access string metatable", 2 )
  398. return nativegetfenv( 0 )
  399. else
  400. return fenv
  401. end
  402. end
  403. end
  404.  
  405. local tAPIsLoading = {}
  406. function os.loadAPI( _sPath )
  407. local sName = fs.getName( _sPath )
  408. if tAPIsLoading[sName] == true then
  409. printError( "API "..sName.." is already being loaded" )
  410. return false
  411. end
  412. tAPIsLoading[sName] = true
  413.  
  414. local tEnv = {}
  415. setmetatable( tEnv, { __index = _G } )
  416. local fnAPI, err = loadfile( _sPath )
  417. if fnAPI then
  418. setfenv( fnAPI, tEnv )
  419. local ok, err = pcall( fnAPI )
  420. if not ok then
  421. printError( err )
  422. tAPIsLoading[sName] = nil
  423. return false
  424. end
  425. else
  426. printError( err )
  427. tAPIsLoading[sName] = nil
  428. return false
  429. end
  430.  
  431. local tAPI = {}
  432. for k,v in pairs( tEnv ) do
  433. tAPI[k] = v
  434. end
  435.  
  436. _G[sName] = tAPI
  437. tAPIsLoading[sName] = nil
  438. return true
  439. end
  440.  
  441. function os.unloadAPI( _sName )
  442. if _sName ~= "_G" and type(_G[_sName]) == "table" then
  443. _G[_sName] = nil
  444. end
  445. end
  446.  
  447. function os.sleep( nTime )
  448. sleep( nTime )
  449. end
  450.  
  451. local nativeShutdown = os.shutdown
  452. function os.shutdown()
  453. nativeShutdown()
  454. while true do
  455. coroutine.yield()
  456. end
  457. end
  458.  
  459. local nativeReboot = os.reboot
  460. function os.reboot()
  461. nativeReboot()
  462. while true do
  463. coroutine.yield()
  464. end
  465. end
  466.  
  467. -- Install the lua part of the HTTP api (if enabled)
  468. if http then
  469. local nativeHTTPRequest = http.request
  470.  
  471. local function wrapRequest( _url, _post, _headers )
  472. local ok, err = nativeHTTPRequest( _url, _post, _headers )
  473. if ok then
  474. while true do
  475. local event, param1, param2 = os.pullEvent()
  476. if event == "http_success" and param1 == _url then
  477. return param2
  478. elseif event == "http_failure" and param1 == _url then
  479. return nil, param2
  480. end
  481. end
  482. end
  483. return nil, err
  484. end
  485.  
  486. http.get = function( _url, _headers )
  487. return wrapRequest( _url, nil, _headers )
  488. end
  489.  
  490. http.post = function( _url, _post, _headers )
  491. return wrapRequest( _url, _post or "", _headers )
  492. end
  493.  
  494. http.request = function( _url, _post, _headers )
  495. local ok, err = nativeHTTPRequest( _url, _post, _headers )
  496. if not ok then
  497. os.queueEvent( "http_failure", _url, err )
  498. end
  499. return ok, err
  500. end
  501. end
  502.  
  503. -- Load APIs
  504. local bAPIError = false
  505. local tApis = fs.list( "rom/apis" )
  506. for n,sFile in ipairs( tApis ) do
  507. if string.sub( sFile, 1, 1 ) ~= "." then
  508. local sPath = fs.combine( "rom/apis", sFile )
  509. if not fs.isDir( sPath ) then
  510. if not os.loadAPI( sPath ) then
  511. bAPIError = true
  512. end
  513. end
  514. end
  515. end
  516.  
  517. if turtle then
  518. -- Load turtle APIs
  519. local tApis = fs.list( "rom/apis/turtle" )
  520. for n,sFile in ipairs( tApis ) do
  521. if string.sub( sFile, 1, 1 ) ~= "." then
  522. local sPath = fs.combine( "rom/apis/turtle", sFile )
  523. if not fs.isDir( sPath ) then
  524. if not os.loadAPI( sPath ) then
  525. bAPIError = true
  526. end
  527. end
  528. end
  529. end
  530. end
  531.  
  532. if pocket and fs.isDir( "rom/apis/pocket" ) then
  533. -- Load pocket APIs
  534. local tApis = fs.list( "rom/apis/pocket" )
  535. for n,sFile in ipairs( tApis ) do
  536. if string.sub( sFile, 1, 1 ) ~= "." then
  537. local sPath = fs.combine( "rom/apis/pocket", sFile )
  538. if not fs.isDir( sPath ) then
  539. if not os.loadAPI( sPath ) then
  540. bAPIError = true
  541. end
  542. end
  543. end
  544. end
  545. end
  546.  
  547. if commands and fs.isDir( "rom/apis/command" ) then
  548. -- Load command APIs
  549. if os.loadAPI( "rom/apis/command/commands" ) then
  550. -- Add a special case-insensitive metatable to the commands api
  551. local tCaseInsensitiveMetatable = {
  552. __index = function( table, key )
  553. local value = rawget( table, key )
  554. if value ~= nil then
  555. return value
  556. end
  557. if type(key) == "string" then
  558. local value = rawget( table, string.lower(key) )
  559. if value ~= nil then
  560. return value
  561. end
  562. end
  563. return nil
  564. end
  565. }
  566. setmetatable( commands, tCaseInsensitiveMetatable )
  567. setmetatable( commands.async, tCaseInsensitiveMetatable )
  568.  
  569. -- Add global "exec" function
  570. exec = commands.exec
  571. else
  572. bAPIError = true
  573. end
  574. end
  575.  
  576. if bAPIError then
  577. print( "Press any key to continue" )
  578. os.pullEvent( "key" )
  579. term.clear()
  580. term.setCursorPos( 1,1 )
  581. end
  582.  
  583. -- Run the shell
  584. local ok, err = pcall( function()
  585. parallel.waitForAny(
  586. function()
  587. if term.isColour() then
  588. os.run( {}, "rom/programs/advanced/multishell" )
  589. else
  590. os.run( {}, "rom/programs/shell" )
  591. end
  592. os.run( {}, "rom/programs/shutdown" )
  593. end,
  594. function()
  595. rednet.run()
  596. end )
  597. end )
  598.  
  599. -- If the shell errored, let the user read it.
  600. term.redirect( term.native() )
  601. if not ok then
  602. printError( err )
  603. pcall( function()
  604. term.setCursorBlink( false )
  605. print( "Press any key to continue" )
  606. os.pullEvent( "key" )
  607. end )
  608. end
  609.  
  610. -- End
  611. os.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement