From f39679dfb836eab3cc41b1ed01218837400ecc1e Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 7 Jun 2018 17:52:38 +0100 Subject: [PATCH] Few Bug Fixes --- FactorioSoftmodManager.lua | 24 ++++++++++------ modules/ExpGamingCore/Gui/control.lua | 36 ++++++++++++------------ modules/ExpGamingCore/Gui/src/left.lua | 4 +-- modules/ExpGamingCore/Server/control.lua | 10 ++++--- modules/ExpGamingLib/control.lua | 1 + 5 files changed, 43 insertions(+), 32 deletions(-) diff --git a/FactorioSoftmodManager.lua b/FactorioSoftmodManager.lua index 276043ab..238cd39e 100644 --- a/FactorioSoftmodManager.lua +++ b/FactorioSoftmodManager.lua @@ -160,7 +160,7 @@ Manager.verbose('The verbose state is: '..tostring(Manager.setVerbose.selfInit), -- @treturn table the new global table for that module Manager.global=setmetatable({__defaults={},__global={ __call=function(tbl,default) return Manager.global(default) end, - __index=function(tbl,key) return Manager.global() == tbl and nil or rawget(Manager.global(),key) or tbl(key) end, + __index=function(tbl,key) return rawget(Manager.global(),key) or tbl(key) end, __newindex=function(tbl,key,value) rawset(Manager.global(),key,value) end, __pairs=function(tbl) local tbl = Manager.global() @@ -171,12 +171,13 @@ Manager.global=setmetatable({__defaults={},__global={ return next_pair, tbl, nil end }},{ - __call=function(tbl,default) + __call=function(tbl,default,tbl2) local global = _G.global - local module_name = type(default) == 'string' and default or module_name - local module_path = type(default) == 'string' and moduleIndex[default] or module_path + local tbl2 = type(tbl2) == 'table' and getmetatable(tbl2) or nil + local module_name = type(default) == 'string' and default or tbl2 and tbl2.name or module_name + local module_path = type(default) == 'string' and moduleIndex[default] or tbl2 and tbl2.path or module_path if not module_path or not module_name then return _G.global end - if type(default) == 'table' then rawset(rawget(tbl,'__defaults'),tostring(module_name),default) end + if type(default) == 'table' then Manager.verbose('Default global has been set for: global'..module_path:gsub('/','.')) rawset(rawget(tbl,'__defaults'),tostring(module_name),default) end local path = 'global' local new_dir = false for dir in module_path:gmatch('%a+') do @@ -185,10 +186,17 @@ Manager.global=setmetatable({__defaults={},__global={ global = rawget(global,dir) end if (new_dir or default == true) 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 + Manager.verbose('Set Global Dir: '..path..' to its default') + -- cant set it to be equle otherwise it will lose its global propeity + local function deepcopy(tbl) if type(tbl) ~= 'table' then return tbl end local rtn = {} for key,value in pairs(tbl) do rtn[key] = deepcopy(value) end return rtn end + for key,value in pairs(global) do rawset(global,key,nil) end + for key,value in pairs(rawget(rawget(tbl,'__defaults'),tostring(module_name))) do rawset(global,key,deepcopy(value)) end end - return global + return setmetatable(global,{ + __call=function(tbl,default) return Manager.global(default,tbl) end, + __index=function(tbl,key) return rawget(Manager.global(),key) or tbl(key) end, + path=module_path,name=module_name + }) end, __index=function(tbl,key) return rawget(tbl(),key) or rawget(_G.global,key) or tbl(key) end, __newindex=function(tbl,key,value) rawset(tbl(),key,value) end, diff --git a/modules/ExpGamingCore/Gui/control.lua b/modules/ExpGamingCore/Gui/control.lua index 0b637c5a..3b3a851c 100644 --- a/modules/ExpGamingCore/Gui/control.lua +++ b/modules/ExpGamingCore/Gui/control.lua @@ -6,7 +6,7 @@ local Gui = {} local Gui_data = {} - +local global = global() --- Used to set and get data about different guis -- @usage Gui.data[location] -- returns the gui data for that gui location ex center -- @usage Gui.data(location,gui_name,gui_data) -- adds gui data for a gui at a location @@ -94,21 +94,21 @@ function Gui.cam_link(data) data.cam.style.height = data.height or 100 else return end if not Server or not Server._thread or not Server.get_thread('camera-follow') then - if not global().cams then - global().cams = {} - global().cam_index = 1 + if not global.cams then + global.cams = {} + global.cam_index = 1 end if data.cam then local surface = data.surface and data.surface.index or nil - table.insert(global().cams,{cam=data.cam,entity=data.entity,surface=surface}) + table.insert(global.cams,{cam=data.cam,entity=data.entity,surface=surface}) end - if not global().players then - global().players = {} + if not global.players then + global.players = {} end if data.respawn_open then if data.entity.player then - if not global().players[data.entity.player.index] then global().players[data.entity.player.index] = {} end - table.insert(global().players[data.entity.player.index],data.cam) + if not global.players[data.entity.player.index] then global.players[data.entity.player.index] = {} end + table.insert(global.players[data.entity.player.index],data.cam) end end else @@ -135,26 +135,26 @@ script.on_event('on_tick', function(event) if Gui.left and ((event.tick+10)/(3600*game.speed)) % 15 == 0 then Gui.left.update() end - if global().cams and is_type(global().cams,'table') and #global().cams > 0 then - local _cam = global().cams[global().cam_index] - if not _cam then global().cam_index = 1 _cam = global().cams[global().cam_index] end + if global.cams and is_type(global.cams,'table') and #global.cams > 0 then + local _cam = global.cams[global.cam_index] + if not _cam then global.cam_index = 1 _cam = global.cams[global.cam_index] end if not _cam then return end - if not _cam.cam.valid then table.remove(global().cams,global().cam_index) - elseif not _cam.entity.valid then table.remove(global().cams,global().cam_index) - else _cam.cam.position = _cam.entity.position if not _cam.surface then _cam.cam.surface_index = _cam.entity.surface.index end global().cam_index = global().cam_index+1 + if not _cam.cam.valid then table.remove(global.cams,global.cam_index) + elseif not _cam.entity.valid then table.remove(global.cams,global.cam_index) + else _cam.cam.position = _cam.entity.position if not _cam.surface then _cam.cam.surface_index = _cam.entity.surface.index end global.cam_index = global.cam_index+1 end end end) script.on_event('on_player_respawned',function(event) - if global().players and is_type(global().players,'table') and #global().players > 0 and global().players[event.player_index] then + if global.players and is_type(global.players,'table') and #global.players > 0 and global.players[event.player_index] then local remove = {} - for index,cam in pairs(global().players[event.player_index]) do + for index,cam in pairs(global.players[event.player_index]) do Gui.cam_link{cam=cam,entity=Game.get_player(event).character} if not cam.valid then table.insert(remove,index) end end for _,index in pairs(remove) do - table.remove(global().players[event.player_index],index) + table.remove(global.players[event.player_index],index) end end end) diff --git a/modules/ExpGamingCore/Gui/src/left.lua b/modules/ExpGamingCore/Gui/src/left.lua index 4de283e0..af76079a 100644 --- a/modules/ExpGamingCore/Gui/src/left.lua +++ b/modules/ExpGamingCore/Gui/src/left.lua @@ -12,7 +12,7 @@ left._left = {} -- used for debugging function left.override_open(state) - global().over_ride_left_can_open = state + global.over_ride_left_can_open = state end --- Used to add a left gui frame -- @usage Gui.left.add{name='foo',caption='Foo',tooltip='just testing',open_on_join=true,can_open=function,draw=function} @@ -143,7 +143,7 @@ function left._left.toggle(event) local success, err = pcall(_left.can_open,player) if not success then error(err) elseif err == true then open = true - elseif global().over_ride_left_can_open then + elseif global.over_ride_left_can_open then if is_type(Ranking,'table') and Ranking.meta.rank_count > 0 then if Ranking.get_rank(player):allowed(_left.name) then open = true else open = {gui.unauthorized} end diff --git a/modules/ExpGamingCore/Server/control.lua b/modules/ExpGamingCore/Server/control.lua index b4d2f837..7b3c47a0 100644 --- a/modules/ExpGamingCore/Server/control.lua +++ b/modules/ExpGamingCore/Server/control.lua @@ -48,13 +48,15 @@ end) Server.threads = setmetatable({},{ __metatable=false, __call=function(tbl) return global.all._n end, + __len=function(tbl) return global.all._n end, __index=function(tbl,key) return rawget(global.all,key) end, __newindex=function(tbl,key,value) rawset(global.all,key,value) end, __pairs=function(tbl) local tbl = global.all local function next_pair(tbl,k) k, v = next(tbl, k) - if type(v) ~= nil and k ~= '_n' then return k,v end + if type(v) ~= nil and k ~= '_n' then return k,v + else return next(tbl, k) end end return next_pair, tbl, nil end @@ -97,7 +99,7 @@ end function Server.close_all_threads(with_force) if not with_force then for uuid,thread in pairs(Server.threads) do thread:close() end - else global(true) end + else global.Server(true) end -- idk why you cant just use global even when global is defined at the top to be over ridren end --- Runs all the theads which have opened with an on_tick event @@ -227,11 +229,11 @@ function Server.interface(callback,use_thread,env,...) end if is_type(env,'table') and env._env == true then local sandbox, success, err = Manager.sandbox(_callback,env,...) - if not success then error(err) return success,err + if not success then return success,err else return success, unpack(err) end else local sandbox, success, err = Manager.sandbox(_callback,{},env,...) - if not success then error(err) return success,err + if not success then return success,err else return success, unpack(err) end end end diff --git a/modules/ExpGamingLib/control.lua b/modules/ExpGamingLib/control.lua index 1c2deb8f..d65db0ac 100644 --- a/modules/ExpGamingLib/control.lua +++ b/modules/ExpGamingLib/control.lua @@ -194,6 +194,7 @@ end -- @tparam table tbl table to convert -- @treturn string the converted table function table.tostring(tbl) + if type(tbl) ~= 'table' then return tostring(tbl) end local result, done = {}, {} for k, v in ipairs(tbl) do table.insert(result,table.val_to_str(v))