From cd5abfc225ab4b1976a32963892f9b57cb3e0dce Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Fri, 7 Sep 2018 17:54:01 +0100 Subject: [PATCH] Added way to load modules to /interface --- modules/ExpGamingAdmin/AdminLib/control.lua | 1 + modules/ExpGamingCore/Gui/src/server.lua | 4 +++- modules/ExpGamingCore/Ranking/src/server.lua | 6 +++++- modules/ExpGamingCore/Server/src/commands.lua | 12 ++++++++++-- modules/ExpGamingCore/Sync/control.lua | 1 + 5 files changed, 20 insertions(+), 4 deletions(-) diff --git a/modules/ExpGamingAdmin/AdminLib/control.lua b/modules/ExpGamingAdmin/AdminLib/control.lua index f25d80fd..e1e7f848 100644 --- a/modules/ExpGamingAdmin/AdminLib/control.lua +++ b/modules/ExpGamingAdmin/AdminLib/control.lua @@ -15,6 +15,7 @@ local Admin = { on_init=function() if loaded_modules['ExpGamingCore.Ranking@^4.0.0'] then Ranking = require('ExpGamingCore.Ranking@^4.0.0') end if loaded_modules['ExpGamingCore.Sync@^4.0.0'] then Sync = require('ExpGamingCore.Sync@^4.0.0') end + if loaded_modules['ExoGamingCore.Server'] then Server.add_module_to_interface('Admin','ExpGamingAdmin') end end, actions={}, action_names={} diff --git a/modules/ExpGamingCore/Gui/src/server.lua b/modules/ExpGamingCore/Gui/src/server.lua index 0fb9818e..30977e8f 100644 --- a/modules/ExpGamingCore/Gui/src/server.lua +++ b/modules/ExpGamingCore/Gui/src/server.lua @@ -40,4 +40,6 @@ script.on_event(-1,function(event) end end end):open() -end) \ No newline at end of file +end) + +Server.add_module_to_interface('ExpGui') \ No newline at end of file diff --git a/modules/ExpGamingCore/Ranking/src/server.lua b/modules/ExpGamingCore/Ranking/src/server.lua index 468b8330..c8c3261b 100644 --- a/modules/ExpGamingCore/Ranking/src/server.lua +++ b/modules/ExpGamingCore/Ranking/src/server.lua @@ -10,6 +10,7 @@ local Game = require('FactorioStdLib.Game') local Color = require('FactorioStdLib.Color') local Server = require('ExpGamingCore.Server') +local Ranking = Ranking --- Print a message to all players of this rank -- @usage rank:print('foo') -- prints to all members of this rank @@ -44,4 +45,7 @@ script.on_event('on_tick',function(event) Ranking.find_preset(player,tick) end):open() end -end) \ No newline at end of file +end) + +Server.add_to_interface('rank',function() return Ranking.get_rank(game.player) end) +Server.add_module_to_interface('Ranking') \ No newline at end of file diff --git a/modules/ExpGamingCore/Server/src/commands.lua b/modules/ExpGamingCore/Server/src/commands.lua index 9ebc0fa6..dd08aee0 100644 --- a/modules/ExpGamingCore/Server/src/commands.lua +++ b/modules/ExpGamingCore/Server/src/commands.lua @@ -10,6 +10,14 @@ local Game = require('FactorioStdLib.Game') local Server = Server +Server.interface = {} +function Server.add_to_interface(loadAs,callback) Server.interface[loadAs] = callback end + +function Server.add_module_to_interface(loadAs,moduleName,version) + local moduleName = module_name or version and moduleName..'@'..version or moduleName or error('No module name supplied') + Server.add_to_interface(loadAs,function() return require(moduleName) end) +end + --- Runs the given input from the script -- @command interface -- @param code The code that will be ran @@ -28,9 +36,9 @@ commands.add_command('interface',{'Server.interface-description'}, { env.position = game.player.position env.entity = game.player.selected env.tile = game.player.surface.get_tile(game.player.position) - -- this works if loaded modules is not present as Mamager will always have the modules - if Manager['ExpGamingCore.Ranking'] then env.rank = Manager['ExpGamingCore.Ranking'].get_rank(game.player) end end + -- adds custom callbacks to the interface + for name,callback in pairs(Server.interface) do env[name] = callback() end -- runs the function local success, err = Server.interface(callback,false,env) -- if there is an error then it will remove the stacktrace and return the error diff --git a/modules/ExpGamingCore/Sync/control.lua b/modules/ExpGamingCore/Sync/control.lua index 442e168a..8a1b522e 100644 --- a/modules/ExpGamingCore/Sync/control.lua +++ b/modules/ExpGamingCore/Sync/control.lua @@ -311,6 +311,7 @@ script.on_event('on_rocket_launched',Sync.emit_update) function Sync:on_init() if loaded_modules['ExpGamingCore.Gui'] then verbose('ExpGamingCore.Gui is installed; Loading gui src') require(module_path..'/src/gui',{Sync=Sync}) end if loaded_modules['ExpGamingCore.Ranking'] then verbose('ExpGamingCore.Ranking is installed; Loading ranking src') require(module_path..'/src/ranking',{Sync=Sync}) end + if loaded_modules['ExoGamingCore.Server'] then Server.add_module_to_interface('Sync') end end function Sync:on_post()