Global Now Works No Bugs

This commit is contained in:
Cooldude2606
2018-06-01 00:30:00 +01:00
parent 2ddbd96476
commit 121dcbe70e
3 changed files with 62 additions and 35 deletions

View File

@@ -70,8 +70,9 @@ Manager.verbose = function(rtn,action)
local settings = Manager.setVerbose local settings = Manager.setVerbose
local state = Manager.currentState local state = Manager.currentState
-- if ran in a module the the global module_name is present -- if ran in a module the the global module_name is present
if module_name then rtn='['..module_name..'] '..tostring(rtn) local rtn = type(rtn) == table and serpent.line(rtn) or tostring(rtn)
else rtn='[FSM] '..tostring(rtn) end if module_name then rtn='['..module_name..'] '..rtn
else rtn='[FSM] '..rtn end
-- module_verbose is a local override for a file, action is used in the manager to describe an extra type, state is the current state -- module_verbose is a local override for a file, action is used in the manager to describe an extra type, state is the current state
-- if action is true then it will always trigger verbose -- if action is true then it will always trigger verbose
if module_verbose or (action and (action == true or settings[action])) or (not action and settings[state]) then if module_verbose or (action and (action == true or settings[action])) or (not action and settings[state]) then
@@ -150,30 +151,52 @@ Manager.verbose('Current state is now: "selfInit"; The verbose state is: '..tost
-- @usage global{'foo','bar'} -- sets the default value -- @usage global{'foo','bar'} -- sets the default value
-- @tparam[opt={}] table default the default value of global -- @tparam[opt={}] table default the default value of global
-- @treturn table the new global table for that module -- @treturn table the new global table for that module
Manager.global=setmetatable({__global=global,__defaults={}},{ Manager.global=setmetatable({__defaults={},__global={
__call=function(tbl,default) __call=function(tbl,default) Manager.global(default) end,
local global = rawget(tbl,'__global') __index=function(tbl,key) return Manager.global() == tbl and nil or rawget(Manager.global(),key) end,
if not module_path or not module_name then return global end __newindex=function(tbl,key,value) rawset(Manager.global(),key,value) end,
if default then rawset(rawget(tbl,'__defaults'),tostring(module_name),default) end __pairs=function(tbl)
local path = 'global' Manager.verbose('Global Pair 1')
local new_dir = false local tbl = Manager.global()
for dir in module_path:gmatch('%a+') do Manager.verbose('Global Pair 2')
path = path..'.'..dir local function next_pair(tbl,k)
if not rawget(global,dir) then new_dir=true Manager.verbose('Added Global Dir: '..path) rawset(global,dir,{}) end k, v = next(tbl, k)
global = rawget(global,dir) if type(v) ~= nil then return k,v end
end end
if new_dir then return next_pair, tbl, nil
Manager.verbose('Set Global Dir: '..path..' to its default') end
for key,value in pairs(rawget(rawget(tbl,'__defaults'),tostring(module_name))) do rawset(global,key,value) end }},{
end __call=function(tbl,default)
return global local global = _G.global
end, if not module_path or not module_name then return _G.global end
__index=function(tbl,key) return rawget(tbl(),key) or rawget(_G.global,key) end, if default then rawset(rawget(tbl,'__defaults'),tostring(module_name),default) end
__newindex=function(tbl,key,value) rawset(tbl(),key,value) end, local path = 'global'
__pairs=function(tbl) return pairs(tbl()) end, local new_dir = false
__ipairs=function(tbl) return ipairs(tbl()) end for dir in module_path:gmatch('%a+') do
path = path..'.'..dir
if not rawget(global,dir) then new_dir=true Manager.verbose('Added Global Dir: '..path) rawset(global,dir,{}) end
global = rawget(global,dir)
end
if new_dir and rawget(rawget(tbl,'__defaults'),tostring(module_name)) then
Manager.verbose('Set Global Dir: '..path..' to its default')
for key,value in pairs(rawget(rawget(tbl,'__defaults'),tostring(module_name))) do rawset(global,key,value) end
end
return global
end,
__index=function(tbl,key) Manager.verbose('Manager Index') return rawget(tbl(),key) or rawget(_G.global,key) end,
__newindex=function(tbl,key,value) rawset(tbl(),key,value) end,
__pairs=function(tbl)
Manager.verbose('Manager Pair 1')
local tbl = Manager.global()
Manager.verbose('Manager Pair 2')
local function next_pair(tbl,k)
k, v = next(tbl, k)
if type(v) ~= nil then return k,v end
end
return next_pair, tbl, nil
end
}) })
global=Manager.global setmetatable(global,Manager.global.__global)
--- Creates a sand box envorment and runs a callback in that sand box; provents global pollution --- Creates a sand box envorment and runs a callback in that sand box; provents global pollution
-- @function Manager.sandbox -- @function Manager.sandbox
@@ -186,7 +209,6 @@ Manager.sandbox = setmetatable({
-- can not use existing keys of _G -- can not use existing keys of _G
verbose=Manager.verbose, verbose=Manager.verbose,
loaded_modules=ReadOnlyManager, loaded_modules=ReadOnlyManager,
global=Manager.global,
module_verbose=false, module_verbose=false,
module_exports=false module_exports=false
},{ },{
@@ -448,7 +470,7 @@ Manager.event = setmetatable({
Manager.verbose('Added Handler: "'..tbl.names[key]..'"','eventRegistered') Manager.verbose('Added Handler: "'..tbl.names[key]..'"','eventRegistered')
-- checks that the event has a valid table to store callbacks; if its not valid it will creat it and register a real event handler -- checks that the event has a valid table to store callbacks; if its not valid it will creat it and register a real event handler
if not rawget(rawget(tbl,'__events'),key) then if not rawget(rawget(tbl,'__events'),key) then
if key == -1 then -- this already has a handler if key == -1 or key == -2 then -- this already has a handler
elseif key < 0 then rawget(tbl,tbl.names[key])(function(...) tbl(key,...) end) elseif key < 0 then rawget(tbl,tbl.names[key])(function(...) tbl(key,...) end)
else rawget(tbl,'__event')(key,function(...) tbl(key,...) end) end else rawget(tbl,'__event')(key,function(...) tbl(key,...) end) end
rawset(rawget(tbl,'__events'),key,{}) end rawset(rawget(tbl,'__events'),key,{}) end
@@ -521,9 +543,13 @@ rawset(Manager.event,'names',setmetatable({},{
})) }))
script.on_init(function(...) script.on_init(function(...)
--rawset(Manager.global,'_global',global) setmetatable(global,Manager.global.__global)
--global = Manager.global Manager.event(-1,...)
Manager.event(key,...) end)
script.on_load(function(...)
setmetatable(global,Manager.global.__global)
Manager.event(-2,...)
end) end)
--over rides for the base values; can be called though Event --over rides for the base values; can be called though Event
Event=setmetatable({},{__call=Manager.event,__index=function(tbl,key) return Manager.event[key] or script[key] or error('Invalid Index To Table Event') end}) Event=setmetatable({},{__call=Manager.event,__index=function(tbl,key) return Manager.event[key] or script[key] or error('Invalid Index To Table Event') end})

View File

@@ -190,6 +190,7 @@ end
-- @treturn boolean success was the data set -- @treturn boolean success was the data set
Sync.info = setmetatable({},{ Sync.info = setmetatable({},{
__index=global, __index=global,
__newindex=global,
__call=function(tbl,set) __call=function(tbl,set)
if not is_type(set,'table') then return false end if not is_type(set,'table') then return false end
for key,value in pairs(set) do global[key] = value end for key,value in pairs(set) do global[key] = value end
@@ -258,7 +259,7 @@ function Sync:on_init()
return true return true
end,function() local info = Sync.info return info.time..' (+'..(game.tick-info.time_set[1])..' Ticks)' end) end,function() local info = Sync.info return info.time..' (+'..(game.tick-info.time_set[1])..' Ticks)' end)
-- updates installed mods -- updates installed mods
Sync.info{mods=table.keys(loaded_modules)} --Sync.info{mods=table.keys(loaded_modules)}
-- optinal dependies -- optinal dependies
if loaded_modules.Gui then verbose('ExpGamingCore.Gui is installed; Loading gui lib') require(module_path..'/src/gui') end if loaded_modules.Gui then verbose('ExpGamingCore.Gui is installed; Loading gui lib') require(module_path..'/src/gui') end
if loaded_modules.Ranking then verbose('ExpGamingCore.Ranking is installed; Loading ranking lib') require(module_path..'/src/ranking') end if loaded_modules.Ranking then verbose('ExpGamingCore.Ranking is installed; Loading ranking lib') require(module_path..'/src/ranking') end

View File

@@ -442,10 +442,10 @@ function table.tostring(tbl)
done[k] = true done[k] = true
end end
for k, v in pairs(tbl) do for k, v in pairs(tbl) do
if not done[k] then if not done[k] then
table.insert(result, table.insert(result,
table.key_to_str(k).."="..table.val_to_str(v)) table.key_to_str(k).."="..table.val_to_str(v))
end end
end end
return "{"..table.concat(result,",") .."}" return "{"..table.concat(result,",") .."}"
end end