Fix to loading modules inside threads

This commit is contained in:
Cooldude2606
2018-10-25 15:36:54 +01:00
parent 5b9a55113c
commit e0d69f63b0
3 changed files with 30 additions and 16 deletions

View File

@@ -271,8 +271,11 @@ function Server._thread:create(obj)
local obj = obj or {}
setmetatable(obj,{__index=Server._thread})
obj.uuid = tostring(Server.uuid)
obj._env = get_env()
obj._env.obj = nil -- provents infinte recusion
obj._env = get_upvalues(2)
obj._env._modules = {}
for name,value in pairs(obj._env) do if value._module_name and loaded_modules[value._module_name] == value then obj._env._modules[name] = value._module_name obj._env[name] = nil end end
obj._env._env = true
obj._env._ENV = nil -- provents infinte recusion
local name = obj.name or 'Anon'
verbose('Created new thread: '..name..' ('..obj.uuid..')')
return obj
@@ -480,7 +483,7 @@ end)
script.on_event(-2,function(event)
-- sets up metatable again so that threads contiune to work
for uuid,thread in pairs(Server.threads) do setmetatable(thread,{__index=Server._thread}) end
for uuid,thread in pairs(Server.threads) do setmetatable(thread,{__index=Server._thread}) setmetatable(thread._env,{__index=function(tbl,key) if rawget(tbl,'_modules') and tbl._modules[key] then return require(tbl._modules[key]) end end}) end
end)
function Server:on_init()