I am not good with metatables, but I get the basic idea of them.
But I am not too sure, by following the wiki, it does not say how to use this: http://wiki.roblox.com/index.php?title=Sandboxing
If anyone can tell me how to use the code below, I set a bounty of 30 robux.
local needssandboxing = function(i) end
sandbox = {}
sandbox.cache = {}
local function safeDestroy(obj)
if obj:IsA("Script") then
print("You cannot destroy a Script")
elseif obj:IsA("Part") then
print("You cannot destroy a part")
end
obj:Destroy()
end
local function safeGetChildren(obj)
local res = {}
for k,v in pairs(obj:GetChildren()) do
if not v.Name:match("^Hide") then
table.insert(res,v)
end
end
return res
end
sandbox.mt = {
__index = function(self, k)
local original = sandbox.cache[self]
local v = original[k]
if k:lower() == "destroy" then
return sandbox.any(safeDestroy)
elseif k:lower() == "getchildren" or k:lower() == "children" then
return sandbox.any(safeGetChildren)
end
return sandbox.any(v)
end,
__newindex = function(self, k, v)
local original = sandbox.cache[self]
original[k] = unsandbox.any(v)
end
}
function sandbox.any(a)
if sandbox.cache[a] then
return a
elseif type(a) == "function" then
return sandbox.func(a)
elseif type(a) == "table" then
return sandbox.table(a)
else
return value
end
end
function sandbox.object(o)
local sandboxed = setmetatable({}, sandbox.mt)
sandbox.cache[sandboxed] = o
return sandboxed
end
function sandbox.func(f)
local sandboxed = function(...)
return sandbox(f(unsandbox(...)))
end
sandbox.cache[sandboxed] = f
return sandboxed
end
function sandbox.table(t)
local sandboxed = {}
for k, v in pairs(t) do
sandboxed[sandbox.any(k)] = sandbox.any(v)
end
return sandboxed
end
unsandbox = {}
unsandbox.any = function(a)
if sandbox.cache[a] then
return sandbox.cache[a]
elseif type(a) == "function" then
return unsandbox.func(a)
elseif type(a) == "table" then
return unsandbox.table(a)
else
return a
end
end
unsandbox.table = function(t)
local unsandboxed = {}
for k, v in pairs(t) do
unsandboxed[unsandbox.any(k)] = unsandbox.any(v)
end
return unsandboxed
end
unsandbox.func = function(f)
local raw = function(...)
return unsandbox(f(sandbox(...)))
end
sandbox.cache[f] = raw
return raw
end
local callable_mt = {
__call = function(self, first, ...)
if select('#', ...) == 0 then
return self.any(first)
else
return self.any(first), self(...)
end
end
}
setmetatable(sandbox, callable_mt)
setmetatable(unsandbox, callable_mt)
