cobra_tomtrein

Untitled

Jun 23rd, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.15 KB | None | 0 0
  1. -- Install lua parts of the os api
  2.  
  3. function setUp()
  4. function os.version()
  5. if turtle then
  6. return "TurtleOS 1.3 - OCL 0.5"
  7. end
  8. return "CraftOS 1.3 - OCL 0.5"
  9. end
  10.  
  11. function os.pullEventRaw( _sFilter )
  12. return coroutine.yield( _sFilter )
  13. end
  14.  
  15. function os.pullEvent( _sFilter )
  16. local event, p1, p2, p3, p4, p5 = os.pullEventRaw( _sFilter )
  17. if event == "terminate" then
  18. print( "Terminated" )
  19. error()
  20. end
  21. return event, p1, p2, p3, p4, p5
  22. end
  23.  
  24. -- Install globals
  25. function sleep( _nTime )
  26. local timer = os.startTimer( _nTime )
  27. repeat
  28. local sEvent, param = os.pullEvent( "timer" )
  29. until param == timer
  30. end
  31.  
  32. function write( sText )
  33. local w,h = term.getSize()
  34. local x,y = term.getCursorPos()
  35.  
  36. local nLinesPrinted = 0
  37. local function newLine()
  38. if y + 1 <= h then
  39. term.setCursorPos(1, y + 1)
  40. else
  41. term.scroll(1)
  42. term.setCursorPos(1, h)
  43. end
  44. x, y = term.getCursorPos()
  45. nLinesPrinted = nLinesPrinted + 1
  46. end
  47.  
  48. -- Print the line with proper word wrapping
  49. while string.len(sText) > 0 do
  50. local whitespace = string.match( sText, "^[ \t]+" )
  51. if whitespace then
  52. -- Print whitespace
  53. term.write( whitespace )
  54. x,y = term.getCursorPos()
  55. sText = string.sub( sText, string.len(whitespace) + 1 )
  56. end
  57.  
  58. local newline = string.match( sText, "^\n" )
  59. if newline then
  60. -- Print newlines
  61. newLine()
  62. sText = string.sub( sText, 2 )
  63. end
  64.  
  65. local text = string.match( sText, "^[^ \t\n]+" )
  66. if text then
  67. sText = string.sub( sText, string.len(text) + 1 )
  68. if string.len(text) > w then
  69. -- Print a multiline word
  70. while string.len( text ) > 0 do
  71. if x > w then
  72. newLine()
  73. end
  74. term.write( text )
  75. text = string.sub( text, (w-x) + 2 )
  76. x,y = term.getCursorPos()
  77. end
  78. else
  79. -- Print a word normally
  80. if x + string.len(text) > w then
  81. newLine()
  82. end
  83. term.write( text )
  84. x,y = term.getCursorPos()
  85. end
  86. end
  87. end
  88.  
  89. return nLinesPrinted
  90. end
  91.  
  92. function print( ... )
  93. local nLinesPrinted = 0
  94. for n,v in ipairs( { ... } ) do
  95. nLinesPrinted = nLinesPrinted + write( tostring( v ) )
  96. end
  97. nLinesPrinted = nLinesPrinted + write( "\n" )
  98. return nLinesPrinted
  99. end
  100.  
  101. function read( _sReplaceChar, _tHistory )
  102. term.setCursorBlink( true )
  103.  
  104. local sLine = ""
  105. local nHistoryPos = nil
  106. local nPos = 0
  107. if _sReplaceChar then
  108. _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  109. end
  110.  
  111. local w, h = term.getSize()
  112. local sx, sy = term.getCursorPos()
  113. local function redraw()
  114. local nScroll = 0
  115. if sx + nPos >= w then
  116. nScroll = (sx + nPos) - w
  117. end
  118.  
  119. term.setCursorPos( sx, sy )
  120. term.write( string.rep(" ", w - sx + 1) )
  121. term.setCursorPos( sx, sy )
  122. if _sReplaceChar then
  123. term.write( string.rep(_sReplaceChar, string.len(sLine) - nScroll) )
  124. else
  125. term.write( string.sub( sLine, nScroll + 1 ) )
  126. end
  127. term.setCursorPos( sx + nPos - nScroll, sy )
  128. end
  129.  
  130. while true do
  131. local sEvent, param = os.pullEvent()
  132. if sEvent == "char" then
  133. sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  134. nPos = nPos + 1
  135. redraw()
  136.  
  137. elseif sEvent == "key" then
  138. if param == 28 then
  139. -- Enter
  140. break
  141.  
  142. elseif param == 203 then
  143. -- Left
  144. if nPos > 0 then
  145. nPos = nPos - 1
  146. redraw()
  147. end
  148.  
  149. elseif param == 205 then
  150. -- Right
  151. if nPos < string.len(sLine) then
  152. nPos = nPos + 1
  153. redraw()
  154. end
  155.  
  156. elseif param == 200 or param == 208 then
  157. -- Up or down
  158. if _tHistory then
  159. if param == 200 then
  160. -- Up
  161. if nHistoryPos == nil then
  162. if #_tHistory > 0 then
  163. nHistoryPos = #_tHistory
  164. end
  165. elseif nHistoryPos > 1 then
  166. nHistoryPos = nHistoryPos - 1
  167. end
  168. else
  169. -- Down
  170. if nHistoryPos == #_tHistory then
  171. nHistoryPos = nil
  172. elseif nHistoryPos ~= nil then
  173. nHistoryPos = nHistoryPos + 1
  174. end
  175. end
  176.  
  177. if nHistoryPos then
  178. sLine = _tHistory[nHistoryPos]
  179. nPos = string.len( sLine )
  180. else
  181. sLine = ""
  182. nPos = 0
  183. end
  184. redraw()
  185. end
  186. elseif param == 14 then
  187. -- Backspace
  188. if nPos > 0 then
  189. sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  190. nPos = nPos - 1
  191. redraw()
  192. end
  193. end
  194. end
  195. end
  196.  
  197. term.setCursorBlink( false )
  198. term.setCursorPos( w + 1, sy )
  199. print()
  200.  
  201. return sLine
  202. end
  203.  
  204. loadfile = function( _sFile )
  205. local file = fs.open( _sFile, "r" )
  206. if file then
  207. local func, err = loadstring( file.readAll(), fs.getName( _sFile ) )
  208. file.close()
  209. return func, err
  210. end
  211. return nil, "File not found"
  212. end
  213.  
  214. dofile = function( _sFile )
  215. local fnFile, e = loadfile( _sFile )
  216. if fnFile then
  217. setfenv( fnFile, getfenv(2) )
  218. fnFile()
  219. else
  220. error( e )
  221. end
  222. end
  223.  
  224. -- Install the rest of the OS api
  225. function os.run( _tEnv, _sPath, ... )
  226. local tArgs = { ... }
  227. local fnFile, err = loadfile( _sPath )
  228. if fnFile then
  229. local tEnv = _tEnv
  230. setmetatable( tEnv, { __index = _G } )
  231. setfenv( fnFile, tEnv )
  232. local ok, err = pcall( function()
  233. fnFile( unpack( tArgs ) )
  234. end )
  235. if not ok then
  236. if err and err ~= "" then
  237. print( err )
  238. end
  239. return false
  240. end
  241. return true
  242. end
  243. if err and err ~= "" then
  244. print( err )
  245. end
  246. return false
  247. end
  248.  
  249. local nativegetmetatable = getmetatable
  250. function getmetatable( _t )
  251. if type( _t ) == "string" then
  252. error( "Attempt to access string metatable" )
  253. end
  254. return nativegetmetatable( _t )
  255. end
  256.  
  257. local bProtected = true
  258. local function protect( _t )
  259. local meta = getmetatable( _t )
  260. if meta == "Protected" then
  261. -- already protected
  262. return
  263. end
  264.  
  265. setmetatable( _t, {
  266. __newindex = function( t, k, v )
  267. if bProtected then
  268. error( "Attempt to write to global" )
  269. else
  270. rawset( t, k, v )
  271. end
  272. end,
  273. __metatable = "Protected",
  274. } )
  275. end
  276.  
  277. local tAPIsLoading = {}
  278. function os.loadAPI( _sPath )
  279. local sName = fs.getName( _sPath )
  280. if tAPIsLoading[sName] == true then
  281. print( "API "..sName.." is already being loaded" )
  282. return false
  283. end
  284. tAPIsLoading[sName] = true
  285.  
  286. local tEnv = {}
  287. setmetatable( tEnv, { __index = _G } )
  288. local fnAPI, err = loadfile( _sPath )
  289. if fnAPI then
  290. setfenv( fnAPI, tEnv )
  291. fnAPI()
  292. else
  293. print( err )
  294. return false
  295. end
  296.  
  297. local tAPI = {}
  298. for k,v in pairs( tEnv ) do
  299. tAPI[k] = v
  300. end
  301. protect( tAPI )
  302.  
  303. bProtected = false
  304. _G[sName] = tAPI
  305. bProtected = true
  306.  
  307. tAPIsLoading[sName] = nil
  308. return true
  309. end
  310.  
  311. function os.unloadAPI( _sName )
  312. if _sName ~= "_G" and type(_G[_sName]) == "table" then
  313. bProtected = false
  314. _G[_sName] = nil
  315. bProtected = true
  316. end
  317. end
  318.  
  319. function os.sleep( _nTime )
  320. sleep( _nTime )
  321. end
  322.  
  323. local nativeShutdown = os.shutdown
  324. function os.shutdown()
  325. nativeShutdown()
  326. while true do
  327. coroutine.yield()
  328. end
  329. end
  330.  
  331. -- Install the lua part of the HTTP api (if enabled)
  332. if http then
  333. local function wrapRequest( _url, _post )
  334. local requestID = http.request( _url, _post )
  335. while true do
  336. local event, param1, param2 = os.pullEvent()
  337. if event == "http_success" and param1 == _url then
  338. return param2
  339. elseif event == "http_failure" and param1 == _url then
  340. return nil
  341. end
  342. end
  343. end
  344.  
  345. http.get = function( _url )
  346. return wrapRequest( _url, nil )
  347. end
  348.  
  349. http.post = function( _url, _post )
  350. return wrapRequest( _url, _post or "" )
  351. end
  352. end
  353.  
  354. -- Install the lua part of the peripheral api
  355. peripheral.wrap = function( _sSide )
  356. if peripheral.isPresent( _sSide ) then
  357. local tMethods = peripheral.getMethods( _sSide )
  358. local tResult = {}
  359. for n,sMethod in ipairs( tMethods ) do
  360. tResult[sMethod] = function( ... )
  361. return peripheral.call( _sSide, sMethod, ... )
  362. end
  363. end
  364. return tResult
  365. end
  366. return nil
  367. end
  368.  
  369. -- Protect the global table against modifications
  370. protect( _G )
  371. for k,v in pairs( _G ) do
  372. if type(v) == "table" then
  373. protect( v )
  374. end
  375. end
  376.  
  377. -- Load APIs
  378. local tApis = fs.list( "rom/apis" )
  379. for n,sFile in ipairs( tApis ) do
  380. if string.sub( sFile, 1, 1 ) ~= "." then
  381. local sPath = fs.combine( "rom/apis", sFile )
  382. if not fs.isDir( sPath ) then
  383. os.loadAPI( sPath )
  384. end
  385. end
  386. end
  387.  
  388. if turtle then
  389. local tApis = fs.list( "rom/apis/turtle" )
  390. for n,sFile in ipairs( tApis ) do
  391. if string.sub( sFile, 1, 1 ) ~= "." then
  392. local sPath = fs.combine( "rom/apis/turtle", sFile )
  393. if not fs.isDir( sPath ) then
  394. os.loadAPI( sPath )
  395. end
  396. end
  397. end
  398. end
  399.  
  400. os.loadAPI("rom/OCL/CraftOS.os/OCL")
  401.  
  402. -- Run the shell
  403. local ok, err = pcall( os.run, {}, "rom/shell" )
  404.  
  405. -- If the shell errored, let the user read it.
  406. if not ok then
  407. print( err )
  408. end
  409.  
  410. pcall( function()
  411. term.setCursorBlink( false )
  412. print( "Press any key to continue" )
  413. os.pullEvent( "key" )
  414. end )
  415. os.shutdown()
  416. end
  417.  
  418. -- Run the shell
  419. local ok, err = pcall( setUp )
  420.  
  421. -- If the shell errored, let the user read it.
  422. if not ok then
  423. term.write( err )
  424. en
  425. os.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment