Advertisement
programcreator

Sandbox

Apr 24th, 2015
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.17 KB | None | 0 0
  1. --[[
  2.     Sandbox
  3.     environment
  4.     by Creator
  5.     for TheOS
  6. ]]--
  7.  
  8. --Variables--
  9. local oldGetfenv = getfenv
  10. local oldLoadfile = loadfile
  11. local globalName = ""
  12. local protectedFuncs = {
  13.     OmniOS = {
  14.         loadAPI = true,
  15.         getFile = true,
  16.     },
  17.     System = {
  18.         newTask = true
  19.     }
  20. }
  21.  
  22. --Functions--
  23.  
  24. function newEnv(name)
  25.     globalName = name
  26.     toReturn = {
  27.         redstone = redstone,
  28.         gps = gps,
  29.         _VERSION = _VERSION,
  30.         keys = keys,
  31.         printError = printError,
  32.         peripheral = peripheral,
  33.         assert = assert,
  34.         getfenv = function(a)
  35.             if type(a) == "number" then
  36.                 oldGetfenv(1)
  37.             elseif type(a) == "function" then
  38.                 oldGetfenv(a)
  39.             else
  40.                 error("Expected function or number, got "..type(a))
  41.             end end,
  42.         bit = bit,
  43.         rawset = rawset,
  44.         tonumber = tonumber,
  45.         loadstring = function(str) local func = loadstring(str) setfenv(func,getfenv(1))end,
  46.         error = error,
  47.         tostring = tostring,
  48.         type = type,
  49.         coroutine = {
  50.             create = coroutine.create,
  51.             resume = coroutine.resume,
  52.             running = coroutine.running,
  53.             status = coroutine.status,
  54.             wrap = coroutine.wrap,
  55.         },
  56.         disk = disk,
  57.         window = window,
  58.         next = next,
  59.         unpack = unpack,
  60.         colours = colours,
  61.         pcall = pcall,
  62.         sleep = sleep,
  63.         loadfile = function(path) return setfenv(loadfile("OmniOS/Programs/"..globalName..".app/"..path),envToReturn) end,
  64.         math = math,
  65.         pairs = pairs,
  66.         fs = {
  67.             combine = fs.combine,
  68.             isReadOnly = function(path) log.log("SandboxPath","OmniOS/Programs/"..globalName..".app/"..path) return fs.isReadOnly("OmniOS/Programs/"..globalName..".app/"..path) end,
  69.             getSize = function(path) return fs.getSize("OmniOS/Programs/"..globalName..".app/"..path) end,
  70.             move = function(path1,path2) return fs.move("OmniOS/Programs/"..globalName..".app/"..path1,"OmniOS/Programs/"..globalName..".app/"..path2) end,
  71.             exists = function(path) return fs.exists("OmniOS/Programs/"..globalName..".app/"..path) end,
  72.             copy = function(path1,path2) return fs.copy("OmniOS/Programs/"..globalName..".app/"..path1,"OmniOS/Programs/"..globalName..".app/"..path2) end,
  73.             getFreeSpace = function(path) return fs.getFreeSpace("OmniOS/Programs/"..globalName..".app/"..path) end,
  74.             makeDir = function(path) return fs.makeDir("OmniOS/Programs/"..globalName..".app/"..path) end,
  75.             find = function(path) return fs.find("OmniOS/Programs/"..globalName..".app/"..path) end,
  76.             getDir = fs.getDir,
  77.             delete = function(path) return fs.delete("OmniOS/Programs/"..globalName..".app/"..path) end,
  78.             open = function(path,...) return fs.open("OmniOS/Programs/"..globalName..".app/"..path,...) end,
  79.             list = function(path) log.log("SandboxPath","OmniOS/Programs/"..globalName..".app/"..path) return fs.list("OmniOS/Programs/"..globalName..".app/"..path) end,
  80.             getDrive = function(path) return fs.getDrive("OmniOS/Programs/"..globalName..".app/"..path) end,
  81.             getName = fs.getName,
  82.             isDir = function(path) return fs.isDir("OmniOS/Programs/"..globalName..".app/"..path) end,
  83.         },
  84.         rawget = rawget,
  85.         _G = envToReturn,
  86.         __inext = __inext,
  87.         read = read,
  88.         rednet = rednet,
  89.         ipairs = ipairs,
  90.         xpcall = xpcall,
  91.         os = os,
  92.         help = help,
  93.         io = io,
  94.         rawequal = rawequal,
  95.         setfenv = setfenv,
  96.         rs = rs,
  97.         http = http,
  98.         write = write,
  99.         string = string,
  100.         setmetatable = setmetatable,
  101.         print = print,
  102.         getmetatable = getmetatable,
  103.         table = table,
  104.         parallel = parallel,
  105.         dofile = function(path) dofile("OmniOS/Programs/"..globalName..".app/"..path) end,
  106.         textutils = textutils,
  107.         term = term,
  108.         colors = colors,
  109.         vector = vectors,
  110.         select = select,
  111.         paintutils = paintutils,
  112.         System = System,
  113.         OmniOS = OmniOS,
  114.         log = log,
  115.     }
  116.     function env(tTable)
  117.         for i,v in pairs(tTable) do
  118.             local tType = type(v)
  119.             if tType == "table" then
  120.                 env(v)
  121.             elseif tType == "function" then
  122.                 setfenv(v,toReturn)
  123.             end
  124.         end
  125.     end
  126.     env(toReturn)
  127.     function globalEnv(tTable)
  128.         if not type(tTable) == "table" then error("Expected table",2) end
  129.         for i,v in pairs(tTable) do
  130.             local tType = type(v)
  131.             if tType == "table" then
  132.                 globalEnv(v)
  133.             elseif tType == "function" then
  134.                 setfenv(v,_G)
  135.             end
  136.         end
  137.     end
  138.     globalEnv(toReturn.OmniOS)
  139.     globalEnv(toReturn.System)
  140.     return toReturn
  141. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement