cobra_tomtrein

Untitled

Jun 24th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. function os.pullEventRaw( sFilter )
  2. return coroutine.yield( sFilter )
  3. end
  4.  
  5. function os.pullEvent( sFilter )
  6. local eventData = table.pack( os.pullEventRaw( sFilter ) )
  7. if eventData[1] == "terminate" then
  8. error( "Terminated", 0 )
  9. end
  10. return table.unpack( eventData, 1, eventData.n )
  11. end
  12.  
  13. -- Install globals
  14. function sleep( nTime )
  15. if nTime ~= nil and type( nTime ) ~= "number" then
  16. error( "bad argument #1 (expected number, got " .. type( nTime ) .. ")", 2 )
  17. end
  18. local timer = os.startTimer( nTime or 0 )
  19. repeat
  20. local sEvent, param = os.pullEvent( "timer" )
  21. until param == timer
  22. end
  23.  
  24. local tAPIsLoading = {}
  25. function os.loadAPI( _sPath )
  26. if type( _sPath ) ~= "string" then
  27. error( "bad argument #1 (expected string, got " .. type( _sPath ) .. ")", 2 )
  28. end
  29. local sName = fs.getName( _sPath )
  30. if sName:sub(-4) == ".lua" then
  31. sName = sName:sub(1,-5)
  32. end
  33. if tAPIsLoading[sName] == true then
  34. printError( "API "..sName.." is already being loaded" )
  35. return false
  36. end
  37. tAPIsLoading[sName] = true
  38.  
  39. local tEnv = {}
  40. setmetatable( tEnv, { __index = _G } )
  41. local fnAPI, err = loadfile( _sPath, tEnv )
  42. if fnAPI then
  43. local ok, err = pcall( fnAPI )
  44. if not ok then
  45. printError( err )
  46. tAPIsLoading[sName] = nil
  47. return false
  48. end
  49. else
  50. printError( err )
  51. tAPIsLoading[sName] = nil
  52. return false
  53. end
  54.  
  55. local tAPI = {}
  56. for k,v in pairs( tEnv ) do
  57. if k ~= "_ENV" then
  58. tAPI[k] = v
  59. end
  60. end
  61.  
  62. _G[sName] = tAPI
  63. tAPIsLoading[sName] = nil
  64. return true
  65. end
  66.  
  67. os.loadAPI("/rom/textutils")
  68. os.loadAPI("/rom/peripheral")
  69.  
  70. loadfile = function( _sFile, _tEnv )
  71. if type( _sFile ) ~= "string" then
  72. error( "bad argument #1 (expected string, got " .. type( _sFile ) .. ")", 2 )
  73. end
  74. if _tEnv ~= nil and type( _tEnv ) ~= "table" then
  75. error( "bad argument #2 (expected table, got " .. type( _tEnv ) .. ")", 2 )
  76. end
  77. local file = fs.open( _sFile, "r" )
  78. if file then
  79. local func, err = loadstring( file.readAll(), fs.getName( _sFile ), "t", _tEnv )
  80. file.close()
  81. return func, err
  82. end
  83. return nil, "File not found"
  84. end
  85.  
  86. dofile = function( _sFile )
  87. if type( _sFile ) ~= "string" then
  88. error( "bad argument #1 (expected string, got " .. type( _sFile ) .. ")", 2 )
  89. end
  90. local fnFile, e = loadfile( _sFile, _G )
  91. if fnFile then
  92. return fnFile()
  93. else
  94. error( e, 2 )
  95. end
  96. end
Advertisement
Add Comment
Please, Sign In to add comment