Advertisement
MCFunRide

CC 1.45 BIOS

Aug 3rd, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.08 KB | None | 0 0
  1.  
  2. --[[
  3. -- Install safe versions of various library functions
  4. -- These will not put cfunctions on the stack, so don't break serialisation
  5. xpcall = function( _fn, _fnErrorHandler )
  6. local typeT = type( _fn )
  7. assert( typeT == "function", "bad argument #1 to xpcall (function expected, got "..typeT..")" )
  8. local co = coroutine.create( _fn )
  9. local tResults = { coroutine.resume( co ) }
  10. while coroutine.status( co ) ~= "dead" do
  11. tResults = { coroutine.resume( co, coroutine.yield() ) }
  12. end
  13. if tResults[1] == true then
  14. return true, unpack( tResults, 2 )
  15. else
  16. return false, _fnErrorHandler( tResults[2] )
  17. end
  18. end
  19.  
  20. pcall = function( _fn, ... )
  21. local typeT = type( _fn )
  22. assert( typeT == "function", "bad argument #1 to pcall (function expected, got "..typeT..")" )
  23. local tArgs = { ... }
  24. return xpcall(
  25. function()
  26. return _fn( unpack( tArgs ) )
  27. end,
  28. function( _error )
  29. return _error
  30. end
  31. )
  32. end
  33. ]]
  34.  
  35. function pairs( _t )
  36. local typeT = type( _t )
  37. if typeT ~= "table" then
  38. error( "bad argument #1 to pairs (table expected, got "..typeT..")", 2 )
  39. end
  40. return next, _t, nil
  41. end
  42.  
  43. function ipairs( _t )
  44. local typeT = type( _t )
  45. if typeT ~= "table" then
  46. error( "bad argument #1 to ipairs (table expected, got "..typeT..")", 2 )
  47. end
  48. return function( t, var )
  49. var = var + 1
  50. local value = t[var]
  51. if value == nil then
  52. return
  53. end
  54. return var, value
  55. end, _t, 0
  56. end
  57.  
  58. function coroutine.wrap( _fn )
  59. local typeT = type( _fn )
  60. if typeT ~= "function" then
  61. error( "bad argument #1 to coroutine.wrap (function expected, got "..typeT..")", 2 )
  62. end
  63. local co = coroutine.create( _fn )
  64. return function( ... )
  65. local tResults = { coroutine.resume( co, ... ) }
  66. if tResults[1] then
  67. return unpack( tResults, 2 )
  68. else
  69. error( tResults[2], 2 )
  70. end
  71. end
  72. end
  73.  
  74. function string.gmatch( _s, _pattern )
  75. local type1 = type( _s )
  76. if type1 ~= "string" then
  77. error( "bad argument #1 to string.gmatch (string expected, got "..type1..")", 2 )
  78. end
  79. local type2 = type( _pattern )
  80. if type2 ~= "string" then
  81. error( "bad argument #2 to string.gmatch (string expected, got "..type2..")", 2 )
  82. end
  83.  
  84. local nPos = 1
  85. return function()
  86. local nFirst, nLast = string.find( _s, _pattern, nPos )
  87. if nFirst == nil then
  88. return
  89. end
  90. nPos = nLast + 1
  91. return string.match( _s, _pattern, nFirst )
  92. end
  93. end
  94.  
  95. local nativesetmetatable = setmetatable
  96. function setmetatable( _o, _t )
  97. if _t and type(_t) == "table" then
  98. local idx = rawget( _t, "__index" )
  99. if idx and type( idx ) == "table" then
  100. rawset( _t, "__index", function( t, k ) return idx[k] end )
  101. end
  102. local newidx = rawget( _t, "__newindex" )
  103. if newidx and type( newidx ) == "table" then
  104. rawset( _t, "__newindex", function( t, k, v ) newidx[k] = v end )
  105. end
  106. end
  107. return nativesetmetatable( _o, _t )
  108. end
  109.  
  110. -- Install lua parts of the os api
  111. function os.version()
  112. if turtle then
  113. return "TurtleOS 1.4"
  114. end
  115. return "CraftOS 1.4"
  116. end
  117.  
  118. function os.pullEventRaw( _sFilter )
  119. return coroutine.yield( _sFilter )
  120. end
  121.  
  122. function os.pullEvent( _sFilter )
  123. local event, p1, p2, p3, p4, p5 = os.pullEventRaw( _sFilter )
  124. if event == "terminate" then
  125. printError( "Terminated" )
  126. error()
  127. end
  128. return event, p1, p2, p3, p4, p5
  129. end
  130.  
  131. -- Install globals
  132. function sleep( _nTime )
  133. local timer = os.startTimer( _nTime )
  134. repeat
  135. local sEvent, param = os.pullEvent( "timer" )
  136. until param == timer
  137. end
  138.  
  139. function write( sText )
  140. local w,h = term.getSize()
  141. local x,y = term.getCursorPos()
  142.  
  143. local nLinesPrinted = 0
  144. local function newLine()
  145. if y + 1 <= h then
  146. term.setCursorPos(1, y + 1)
  147. else
  148. term.setCursorPos(1, h)
  149. term.scroll(1)
  150. end
  151. x, y = term.getCursorPos()
  152. nLinesPrinted = nLinesPrinted + 1
  153. end
  154.  
  155. -- Print the line with proper word wrapping
  156. while string.len(sText) > 0 do
  157. local whitespace = string.match( sText, "^[ \t]+" )
  158. if whitespace then
  159. -- Print whitespace
  160. term.write( whitespace )
  161. x,y = term.getCursorPos()
  162. sText = string.sub( sText, string.len(whitespace) + 1 )
  163. end
  164.  
  165. local newline = string.match( sText, "^\n" )
  166. if newline then
  167. -- Print newlines
  168. newLine()
  169. sText = string.sub( sText, 2 )
  170. end
  171.  
  172. local text = string.match( sText, "^[^ \t\n]+" )
  173. if text then
  174. sText = string.sub( sText, string.len(text) + 1 )
  175. if string.len(text) > w then
  176. -- Print a multiline word
  177. while string.len( text ) > 0 do
  178. if x > w then
  179. newLine()
  180. end
  181. term.write( text )
  182. text = string.sub( text, (w-x) + 2 )
  183. x,y = term.getCursorPos()
  184. end
  185. else
  186. -- Print a word normally
  187. if x + string.len(text) - 1 > w then
  188. newLine()
  189. end
  190. term.write( text )
  191. x,y = term.getCursorPos()
  192. end
  193. end
  194. end
  195.  
  196. return nLinesPrinted
  197. end
  198.  
  199. function print( ... )
  200. local nLinesPrinted = 0
  201. for n,v in ipairs( { ... } ) do
  202. nLinesPrinted = nLinesPrinted + write( tostring( v ) )
  203. end
  204. nLinesPrinted = nLinesPrinted + write( "\n" )
  205. return nLinesPrinted
  206. end
  207.  
  208. function printError( ... )
  209. if term.isColour() then
  210. term.setTextColour( colours.red )
  211. end
  212. print( ... )
  213. term.setTextColour( colours.white )
  214. end
  215.  
  216. function read( _sReplaceChar, _tHistory )
  217. term.setCursorBlink( true )
  218.  
  219. local sLine = ""
  220. local nHistoryPos = nil
  221. local nPos = 0
  222. if _sReplaceChar then
  223. _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  224. end
  225.  
  226. local w, h = term.getSize()
  227. local sx, sy = term.getCursorPos()
  228.  
  229. local function redraw( _sCustomReplaceChar )
  230. local nScroll = 0
  231. if sx + nPos >= w then
  232. nScroll = (sx + nPos) - w
  233. end
  234.  
  235. term.setCursorPos( sx, sy )
  236. local sReplace = _sCustomReplaceChar or _sReplaceChar
  237. if sReplace then
  238. term.write( string.rep(sReplace, string.len(sLine) - nScroll) )
  239. else
  240. term.write( string.sub( sLine, nScroll + 1 ) )
  241. end
  242. term.setCursorPos( sx + nPos - nScroll, sy )
  243. end
  244.  
  245. while true do
  246. local sEvent, param = os.pullEvent()
  247. if sEvent == "char" then
  248. sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  249. nPos = nPos + 1
  250. redraw()
  251.  
  252. elseif sEvent == "key" then
  253. if param == keys.enter then
  254. -- Enter
  255. break
  256.  
  257. elseif param == keys.left then
  258. -- Left
  259. if nPos > 0 then
  260. nPos = nPos - 1
  261. redraw()
  262. end
  263.  
  264. elseif param == keys.right then
  265. -- Right
  266. if nPos < string.len(sLine) then
  267. nPos = nPos + 1
  268. redraw()
  269. end
  270.  
  271. elseif param == keys.up or param == keys.down then
  272. -- Up or down
  273. if _tHistory then
  274. redraw(" ");
  275. if param == keys.up then
  276. -- Up
  277. if nHistoryPos == nil then
  278. if #_tHistory > 0 then
  279. nHistoryPos = #_tHistory
  280. end
  281. elseif nHistoryPos > 1 then
  282. nHistoryPos = nHistoryPos - 1
  283. end
  284. else
  285. -- Down
  286. if nHistoryPos == #_tHistory then
  287. nHistoryPos = nil
  288. elseif nHistoryPos ~= nil then
  289. nHistoryPos = nHistoryPos + 1
  290. end
  291. end
  292.  
  293. if nHistoryPos then
  294. sLine = _tHistory[nHistoryPos]
  295. nPos = string.len( sLine )
  296. else
  297. sLine = ""
  298. nPos = 0
  299. end
  300. redraw()
  301. end
  302. elseif param == keys.backspace then
  303. -- Backspace
  304. if nPos > 0 then
  305. redraw(" ");
  306. sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  307. nPos = nPos - 1
  308. redraw()
  309. end
  310. elseif param == keys.home then
  311. -- Home
  312. nPos = 0
  313. redraw()
  314. elseif param == keys.delete then
  315. if nPos < string.len(sLine) then
  316. redraw(" ");
  317. sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )
  318. redraw()
  319. end
  320. elseif param == keys["end"] then
  321. -- End
  322. nPos = string.len(sLine)
  323. redraw()
  324. end
  325. end
  326. end
  327.  
  328. term.setCursorBlink( false )
  329. term.setCursorPos( w + 1, sy )
  330. print()
  331.  
  332. return sLine
  333. end
  334.  
  335. loadfile = function( _sFile )
  336. local file = fs.open( _sFile, "r" )
  337. if file then
  338. local func, err = loadstring( file.readAll(), fs.getName( _sFile ) )
  339. file.close()
  340. return func, err
  341. end
  342. return nil, "File not found"
  343. end
  344.  
  345. dofile = function( _sFile )
  346. local fnFile, e = loadfile( _sFile )
  347. if fnFile then
  348. setfenv( fnFile, getfenv(2) )
  349. return fnFile()
  350. else
  351. error( e, 2 )
  352. end
  353. end
  354.  
  355. -- Install the rest of the OS api
  356. function os.run( _tEnv, _sPath, ... )
  357. local tArgs = { ... }
  358. local fnFile, err = loadfile( _sPath )
  359. if fnFile then
  360. local tEnv = _tEnv
  361. --setmetatable( tEnv, { __index = function(t,k) return _G[k] end } )
  362. setmetatable( tEnv, { __index = _G } )
  363. setfenv( fnFile, tEnv )
  364. local ok, err = pcall( function()
  365. fnFile( unpack( tArgs ) )
  366. end )
  367. if not ok then
  368. if err and err ~= "" then
  369. printError( err )
  370. end
  371. return false
  372. end
  373. return true
  374. end
  375. if err and err ~= "" then
  376. printError( err )
  377. end
  378. return false
  379. end
  380.  
  381. local nativegetmetatable = getmetatable
  382. local nativetype = type
  383. local nativeerror = error
  384. function getmetatable( _t )
  385. if nativetype( _t ) == "string" then
  386. nativeerror( "Attempt to access string metatable", 2 )
  387. return nil
  388. end
  389. return nativegetmetatable( _t )
  390. end
  391.  
  392. local tAPIsLoading = {}
  393. function os.loadAPI( _sPath )
  394. local sName = fs.getName( _sPath )
  395. if tAPIsLoading[sName] == true then
  396. printError( "API "..sName.." is already being loaded" )
  397. return false
  398. end
  399. tAPIsLoading[sName] = true
  400.  
  401. local tEnv = {}
  402. setmetatable( tEnv, { __index = _G } )
  403. local fnAPI, err = loadfile( _sPath )
  404. if fnAPI then
  405. setfenv( fnAPI, tEnv )
  406. fnAPI()
  407. else
  408. printError( err )
  409. tAPIsLoading[sName] = nil
  410. return false
  411. end
  412.  
  413. local tAPI = {}
  414. for k,v in pairs( tEnv ) do
  415. tAPI[k] = v
  416. end
  417.  
  418. _G[sName] = tAPI
  419. tAPIsLoading[sName] = nil
  420. return true
  421. end
  422.  
  423. function os.unloadAPI( _sName )
  424. if _sName ~= "_G" and type(_G[_sName]) == "table" then
  425. _G[_sName] = nil
  426. end
  427. end
  428.  
  429. function os.sleep( _nTime )
  430. sleep( _nTime )
  431. end
  432.  
  433. local nativeShutdown = os.shutdown
  434. function os.shutdown()
  435. nativeShutdown()
  436. while true do
  437. coroutine.yield()
  438. end
  439. end
  440.  
  441. -- Install the lua part of the HTTP api (if enabled)
  442. if http then
  443. local function wrapRequest( _url, _post )
  444. local requestID = http.request( _url, _post )
  445. while true do
  446. local event, param1, param2 = os.pullEvent()
  447. if event == "http_success" and param1 == _url then
  448. return param2
  449. elseif event == "http_failure" and param1 == _url then
  450. return nil
  451. end
  452. end
  453. end
  454.  
  455. http.get = function( _url )
  456. return wrapRequest( _url, nil )
  457. end
  458.  
  459. http.post = function( _url, _post )
  460. return wrapRequest( _url, _post or "" )
  461. end
  462. end
  463.  
  464. -- Install the lua part of the peripheral api
  465. peripheral.wrap = function( _sSide )
  466. if peripheral.isPresent( _sSide ) then
  467. local tMethods = peripheral.getMethods( _sSide )
  468. local tResult = {}
  469. for n,sMethod in ipairs( tMethods ) do
  470. tResult[sMethod] = function( ... )
  471. return peripheral.call( _sSide, sMethod, ... )
  472. end
  473. end
  474. return tResult
  475. end
  476. return nil
  477. end
  478.  
  479. -- Load APIs
  480. local tApis = fs.list( "rom/apis" )
  481. for n,sFile in ipairs( tApis ) do
  482. if string.sub( sFile, 1, 1 ) ~= "." then
  483. local sPath = fs.combine( "rom/apis", sFile )
  484. if not fs.isDir( sPath ) then
  485. os.loadAPI( sPath )
  486. end
  487. end
  488. end
  489.  
  490. if turtle then
  491. local tApis = fs.list( "rom/apis/turtle" )
  492. for n,sFile in ipairs( tApis ) do
  493. if string.sub( sFile, 1, 1 ) ~= "." then
  494. local sPath = fs.combine( "rom/apis/turtle", sFile )
  495. if not fs.isDir( sPath ) then
  496. os.loadAPI( sPath )
  497. end
  498. end
  499. end
  500. end
  501.  
  502. -- Run the shell
  503. local ok, err = pcall( function()
  504. parallel.waitForAny(
  505. function()
  506. rednet.run()
  507. end,
  508. function()
  509. os.run( {}, "rom/programs/shell" )
  510. end
  511. )
  512. end )
  513.  
  514. -- If the shell errored, let the user read it.
  515. if not ok then
  516. printError( err )
  517. end
  518.  
  519. pcall( function()
  520. term.setCursorBlink( false )
  521. print( "Press any key to continue" )
  522. os.pullEvent( "key" )
  523. end )
  524. os.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement