zahar0401

CC Sandbox

Jul 11th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.83 KB | None | 0 0
  1. -- Original by http://www.computercraft.info/forums2/index.php?/user/10136-engineer/
  2.  
  3. local args = { ... }
  4.  
  5. if fs.exists( args[1] ) then
  6.         local f = fs.open( args[1], "r" )
  7.         local content = f.readAll()
  8.         f.close()
  9.        
  10.         local func, err = loadstring( content, args[1] )
  11.         if not func then error( "Cannot propely sandbox this file: ".. err, 0 ) end
  12.        
  13.         local tArgs = {}
  14.         for k, v in pairs( args ) do
  15.                 if k ~= 1 then
  16.                         tArgs[#tArgs + 1] = v
  17.                 end
  18.         end
  19.        
  20.         setfenv( func, setmetatable( {}, { __index = _G; __newindex = function( t, k, v ) printError( "Program is accessing global variables.", 0 ) end } ))
  21.         func( unpack(tArgs) )
  22. else
  23.         printError( "Cannot sandbox a non-existing file!", 0 )
  24. end
Add Comment
Please, Sign In to add comment