From 50213c71add32dceba365fd583a1b9b051772df4 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Sun, 16 Feb 2020 17:25:05 +0000 Subject: [PATCH] Added Async --- expcore/async.lua | 71 +++++++++++++++++++++++++++++++ expcore/permission_groups.lua | 27 +++++++----- expcore/roles.lua | 14 +++---- expcore/sudo.lua | 76 ---------------------------------- modules/commands/interface.lua | 2 +- 5 files changed, 95 insertions(+), 95 deletions(-) create mode 100644 expcore/async.lua delete mode 100644 expcore/sudo.lua diff --git a/expcore/async.lua b/expcore/async.lua new file mode 100644 index 00000000..21ae7080 --- /dev/null +++ b/expcore/async.lua @@ -0,0 +1,71 @@ +--[[-- Core Module - Async + - An extention of task and token to allow a single require to register and run async functions. + @core Async + @alias Async + + @usage + -- To use Async you must register the allowed functions when the files are loaded, often this will just be giving access to + -- some functions within a module if you expect that some parts may be blocked by in game permissions or a custom system you have made + -- you may also want to register functions that you want to have a delayed call, such as waiting 2 seconds before printing a message + + -- This player.admin is called (command or gui element event) by a player who isnt admin then it will error + -- here we register the function to promote the player so that it will run async + local promote_player = + Async.register(function(player) + player.admin = true + end) + + -- This will allow us to bypass this by running one tick later outside of any player scope + Async(promote_player,game.player) + + -- Here we make an sync function that we want to have a delay, note the delay is not defined here + local print_message = + Async.register(function(player,message) + player.print(message) + end) + + -- We can then call the async function with a delay using the wait function + Async.wait(60,print_message,game.player,'One second has passed!') + +]] +local Task = require 'utils.task' --- @dep utils.task +local Token = require 'utils.token' --- @dep utils.token + +local Async = {} + +local internal_run = +Token.register(function(params) + local func = Token.get(params.token) + func(unpack(params.params)) +end) + +--- Register a new async function, must called when the file is loaded +-- @tparam function callback the function that will become an async function +Async.register = Token.register + +--- Runs the async function linked to this token, you may supply any number of params as needed +-- @tparam string token the token of the async function you want to run +-- @tparam[opt] any ... the other params that you want to pass to your function +function Async.run(token,...) + Task.queue_task(internal_run, { + token = token, + params = {...} + }) +end + +--- Runs the async function linked to this token after the given number of ticks, you may supply any number of params as needed +-- @tparam number ticks the number of ticks that you want the function to run after +-- @tparam string token the token of the async function you want to run +-- @tparam[opt] any ... the other params that you want to pass to your function +function Async.wait(ticks,token,...) + Task.set_timeout_in_ticks(ticks, internal_run, { + token = token, + params = {...} + }) +end + +return setmetatable(Async,{ + __call = function(self,...) + self.run(...) + end +}) \ No newline at end of file diff --git a/expcore/permission_groups.lua b/expcore/permission_groups.lua index 4fb10c05..136717e0 100644 --- a/expcore/permission_groups.lua +++ b/expcore/permission_groups.lua @@ -30,13 +30,27 @@ local Game = require 'utils.game' --- @dep utils.game local Event = require 'utils.event' --- @dep utils.event -local Sudo = require 'expcore.sudo' --- @dep expcore.sudo +local Async = require 'expcore.async' --- @dep expcore.async local Permissions_Groups = { groups={}, -- store for the different groups that are created _prototype={} -- stores functions that are used on group instances } +-- Async function to add players to permission groups +local add_to_permission_group = +Async.register(function(permission_group,player) + permission_group.add_player(player) +end) +Permissions_Groups.async_token_add_to_permission_group = add_to_permission_group + +-- Async function to remove players from permission groups +local remove_from_permission_group = +Async.register(function(permission_group,player) + permission_group.remove_player(player) +end) +Permissions_Groups.async_token_remove_from_permission_group = remove_from_permission_group + --- Getters. -- Functions that get permission groups -- @section getters @@ -224,7 +238,7 @@ function Permissions_Groups._prototype:add_player(player) player = Game.get_player_from_any(player) local group = self:get_raw() if not group or not player then return false end - Sudo('add-player-to-permission-group',group,player) + Async(add_to_permission_group, group, player) return true end @@ -235,7 +249,7 @@ function Permissions_Groups._prototype:remove_player(player) player = Game.get_player_from_any(player) local group = self:get_raw() if not group or not player then return false end - Sudo('remove-player-from-permission-group',group,player) + Async(remove_from_permission_group, group, player) return true end @@ -275,11 +289,4 @@ Event.on_init(function() Permissions_Groups.reload_permissions() end) -Sudo.register('add-player-to-permission-group',function(permission_group,player) - permission_group.add_player(player) -end) -Sudo.register('remove-player-from-permission-group',function(permission_group,player) - permission_group.remove_player(player) -end) - return Permissions_Groups \ No newline at end of file diff --git a/expcore/roles.lua b/expcore/roles.lua index e6b6e956..a9d68404 100644 --- a/expcore/roles.lua +++ b/expcore/roles.lua @@ -116,7 +116,7 @@ local Game = require 'utils.game' --- @dep utils.game local Global = require 'utils.global' --- @dep utils.global local Event = require 'utils.event' --- @dep utils.event local Groups = require 'expcore.permission_groups' --- @dep expcore.permission_groups -local Sudo = require 'expcore.sudo' --- @dep expcore.sudo +local Async = require 'expcore.async' --- @dep expcore.async local Colours = require 'resources.color_presets' --- @dep resources.color_presets local write_json = ext_require('expcore.common','write_json') --- @dep expcore.common @@ -443,9 +443,7 @@ end -- flag param - player - the player that has had they roles changed -- flag param - state - the state of the flag, aka if the flag is present function Roles.define_flag_trigger(name,callback) - local sudo_name = 'role-flag-'..name - Roles.config.flags[name] = sudo_name - Sudo.register(sudo_name,callback) + Roles.config.flags[name] = Async.register(callback) end --- Sets the default role which every player will have, this needs to be called at least once @@ -748,9 +746,9 @@ end local function role_update(event) local player = Game.get_player_by_index(event.player_index) -- Updates flags given to the player - for flag,sudo_name in pairs(Roles.config.flags) do + for flag, async_token in pairs(Roles.config.flags) do local state = Roles.player_has_flag(player,flag) - Sudo(sudo_name,player,state) + Async(async_token, player, state) end -- Updates the players permission group local highest = Roles.get_player_highest_role(player) @@ -758,10 +756,10 @@ local function role_update(event) if highest.permission_group[1] then local group = game.permissions.get_group(highest.permission_group[2]) if group then - Sudo('add-player-to-permission-group',group,player) + Async(Groups.async_token_add_to_permission_group, group, player) end else - Groups.set_player_group(player,highest.permission_group) + Groups.set_player_group(player, highest.permission_group) end end end diff --git a/expcore/sudo.lua b/expcore/sudo.lua deleted file mode 100644 index 630e50af..00000000 --- a/expcore/sudo.lua +++ /dev/null @@ -1,76 +0,0 @@ ---[[-- Core Module - Sudo - - An extention of task and token to allow a single require to register and run functions bypassing all permissions. - @core Sudo - @alias Sudo - - @usage - -- To use sudo you must register the allowed functions when the files are loaded, often this will just be giving access to - -- some functions within a module if you expect that some parts may be blocked by in game permissions or a custom system you have made - - -- This will be blocked if the current player (from a command or gui) is not admin - local function make_admin(player) - player.admin = true - end - - -- Here we give sudo access to the function under the name "make-admin" - Sudo.register('make-admin',make_admin) - - -- This will allow us to bypass this by running one tick later outside of any player scope - Sudo.run('make-admin',game.player) -]] -local Task = require 'utils.task' --- @dep utils.task -local Token = require 'utils.token' --- @dep utils.token - -local Sudo = { - tokens={} -} - -local internal_run = -Token.register(function(params) - local func = Token.get(params.token) - func(unpack(params.params)) -end) - ---- Registers a new callback under the given name, used to avoid desyncs --- @tparam string name the name that will be used to call this function --- @tparam function callback the function that will be called by this name -function Sudo.register(name,callback) - if _LIFECYCLE == 8 then - error('Calling Sudo.register after on_init() or on_load() has run is a desync risk.', 2) - end - - if Sudo.tokens[name] then - error(name..' is already registered',2) - end - - Sudo.tokens[name] = Token.register(callback) -end - ---- Gets the function that is registered under the given name --- @tparam string name the name of the function you want to get -function Sudo.get(name) - local token = Sudo.tokens[name] - return token and Token.get(token) -end - ---- Runs the function that is registered under the given name, you may supply any number of params as needed --- @tparam string name the name of the function you want to run --- @tparam[opt] any ... the other params that you want to pass to your function -function Sudo.run(name,...) - local token = Sudo.tokens[name] - - if not token then - error('Sudo does not have access to run "'..tostring(name)..'" please make sure it is registered to sudo',2) - end - - Task.set_timeout_in_ticks(1, internal_run, { - token = token, - params = {...} - }) -end - -return setmetatable(Sudo,{ - __call = function(self,...) - self.run(...) - end -}) \ No newline at end of file diff --git a/modules/commands/interface.lua b/modules/commands/interface.lua index 51b20ebf..67dbd035 100644 --- a/modules/commands/interface.lua +++ b/modules/commands/interface.lua @@ -17,7 +17,7 @@ local interface_modules = { ['Roles']='expcore.roles', ['Store']='expcore.store', ['Gui']='expcore.gui', - ['Sudo']='expcore.sudo' + ['Async']='expcore.async' } -- loads all the modules given in the above table