Xoandbit

Untitled

Dec 26th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. local tAPIsLoading = {}
  2. function os.loadAPI( _sPath )
  3. local sName = fs.getName( _sPath )
  4. if tAPIsLoading[sName] == true then
  5. printError( "API "..sName.." is already being loaded" )
  6. return false
  7. end
  8. tAPIsLoading[sName] = true
  9.  
  10. local tEnv = {}
  11. setmetatable( tEnv, { __index = _G } )
  12. local fnAPI, err = loadfile( _sPath )
  13. if fnAPI then
  14. setfenv( fnAPI, tEnv )
  15. local bSuccess, err = pcall( fnAPI )
  16. if not bSuccess then
  17. printError( err )
  18. tAPIsLoading[sName] = nil
  19. return false
  20. end
  21. else
  22. printError( err )
  23. tAPIsLoading[sName] = nil
  24. return false
  25. end
  26.  
  27. local tAPI = {}
  28. for k,v in pairs( tEnv ) do
  29. tAPI[k] = v
  30. end
  31.  
  32. _G[sName] = tAPI
  33. tAPIsLoading[sName] = nil
  34. return true
  35. end
Advertisement
Add Comment
Please, Sign In to add comment