TimeTicksJoin Date: 2011-04-27 Post Count: 27115 |
local events = {}
local mt = {
__index = function(events,k)
if rawget(events,k) == nil then
local event = {}
setmetatable(event,{
__call = function(t,object,func,arg)
if type(object) ~= "userdata" then error("bad argument #1 to \'" .. k .. "\' (Object expected, got " .. type(object) .. ")",2) end
if type(func) ~= "function" then error("bad argument #2 to \'" .. k .. "\' (function expected, got " .. type(func) .. ")",2) end
if pcall(function() return object[k] end) then
local public,private = newproxy(true),{}
local mt = getmetatable(public)
private["Enabled"] = true
local conn = object[k]:connect(function(...)
if private.Enabled then
if arg ~= nil then
func(object,arg,...)
else
func(object,...)
end
end
end)
local active = true
private["Disconnect"] = function()
if type(event[object]) == "table" then
local pick
for k,v in pairs(event[object]) do if v == public then pick = k break end end
table.remove(event[object],pick)
if #event[object] == 1 then event[object] = event[object][1] end
else
event[object] = nil
local count = 0
for _ in pairs(event) do count = count + 1 end
if count == 0 then
event = nil
events[k] = nil
end
end
active = false
return conn:disconnect()
end
mt.__index = function(t,k)
if not active then error("event is no longer active",2) end
if private[k] ~= nil then
return private[k]
end
return error(k .. " is not a valid member",2)
end
mt.__newindex = function(t,k,v)
if not active then error("event is no longer active",2) end
if k == "Enabled" then
private.Enabled = not not v
else
return error(k .. " cannot be assigned to",2)
end
end
mt.__tostring = function(t)
return "event: " .. tostring(private):sub(8)
end
mt.__metatable = "nil"
if event[object] ~= nil then
if type(event[object]) ~= "table" then
local current = event[object]
event[object] = {}
table.insert(event[object],current)
end
table.insert(event[object],public)
else
event[object] = public
end
return public
else
error("Signal " .. k .. " does not exist in " .. tostring(object),2)
local count = 0
for _ in pairs(event) do count = count + 1 end
if count == 0 then
event = nil
events[k] = nil
end
end
end,
__tostring = function() return "EventHandler" end,
})
getmetatable(event).__metatable = "nil"
events[k] = event
end
return rawget(events,k)
end,
}
mt.__metatable = "nil"
setmetatable(events,mt)
function setToLocalEnv(functions)
if type(functions) ~= "table" then error("bad argument #1 to 'setToLocalEnv' (table expected, got " .. type(functions) .. ")",2) end
local env = {}
for index,value in pairs(_G) do env[index] = value end
env["_G"] = env
for index,value in pairs(functions) do env[index] = value end
for name,func in pairs(functions) do
if type(func) == "function" then
setfenv(func,env)
end
end
end
function RunAsEnv(env,func)
if type(env) ~= "table" then error("bad argument #1 to 'RunAsEnv' (table expected, got " .. type(env) .. ")",2) end
if type(func) ~= "function" then error("bad argument #2 to 'RunAsEnv' (function expected, got " .. type(func) .. ")",2) end
local function it(t)
local e = {}
for index,value in pairs(t) do
if type(value) == "table" then
for i,v in pairs(it(value)) do
e[i] = v
end
else
e[index] = value
end
end
return e
end
env = it(env)
setfenv(func,env)
func()
end
|