Add startable legacy code

This commit is contained in:
Cooldude2606
2024-09-22 23:38:58 +01:00
parent 65145b5d34
commit 2a1206c498
163 changed files with 756 additions and 2573 deletions

View File

@@ -1,101 +0,0 @@
--[[-- 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 file is loaded, often this will just be giving access to
-- some functions within a module if you expect that at part 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 time delay, such as waiting 2 seconds before printing a message
-- When player.admin is called (either 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 and outside the player scope
local promote_player =
Async.register(function(player)
player.admin = true
end)
-- This will allow us to bypass the error 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)
return func(table.unpack(params.params))
end)
--[[-- Register a new async function, must called when the file is loaded
@function register
@tparam function callback the function that can be called as an async function
@treturn string the uid of the async function which can be passed to Async.run and Async.wait
@usage-- Registering a function to set the admin state of a player
local set_admin =
Async.register(function(player, state)
if player.valid then
player.admin = state
end
end)
@usage-- Registering a function to print to a player
local print_to_player =
Async.register(function(player, message)
if player.valid then
player.print(message)
end
end)
]]
Async.register = Token.register
--[[-- Runs an async function, you may supply any number of arguments as required by that function
@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
@usage-- Make a player admin regardless of if you are admin
Async.run(set_admin, player, true)
]]
function Async.run(token, ...)
Task.queue_task(internal_run, {
token = token,
params = {...}
})
end
--[[-- Runs an async function after the given number of ticks, you may supply any number of arguments as required by that function
@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
@usage-- Print a message to a player after 5 seconds
Async.wait(300, print_to_player, 'Hello, World!')
]]
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
})

View File

@@ -185,7 +185,6 @@ end)
]]
local Game = require 'utils.game' --- @dep utils.game
local player_return, write_json = _C.player_return, _C.write_json --- @dep expcore.common
local trace = debug.traceback
@@ -392,20 +391,13 @@ local commands = Commands.get()
]]
function Commands.get(player)
player = Game.get_player_from_any(player)
if not player then
return Commands.commands
end
if not player then return Commands.commands end
local allowed = {}
for name, command_data in pairs(Commands.commands) do
if Commands.authorize(player, name) then
allowed[name] = command_data
end
end
return allowed
end
@@ -425,7 +417,6 @@ function Commands.search(keyword, player)
local custom_commands = Commands.get(player)
local matches = {}
keyword = keyword:lower()
-- Loops over custom commands
for name, command_data in pairs(custom_commands) do
-- combines name help and aliases into one message to be searched
@@ -435,7 +426,6 @@ function Commands.search(keyword, player)
matches[name] = command_data
end
end
-- Loops over the names of game commands
for name, description in pairs(commands.game_commands) do
if name:lower():match(keyword) then
@@ -448,7 +438,6 @@ function Commands.search(keyword, player)
}
end
end
return matches
end
@@ -480,9 +469,7 @@ function Commands.new_command(name, help, descr)
}, {
__index = Commands._prototype
})
Commands.commands[name] = command
return command
end

View File

@@ -4,8 +4,8 @@
@alias Common
]]
local Colours = require 'utils.color_presets' --- @dep utils.color_presets
local Game = require 'utils.game' --- @dep utils.game
local Colours = require("modules/exp_util/include/color")
local Game = require("modules.exp_legacy.utils.game") --- @dep utils.game
local Common = {}
@@ -138,21 +138,12 @@ end
--- Will raise an error if called during runtime
-- @usage error_if_runtime()
function Common.error_if_runtime()
if _LIFECYCLE == 8 then
if package.lifecycle == 8 then
local function_name = debug.getinfo(2, 'n').name or '<anon>'
error(function_name..' can not be called during runtime', 3)
end
end
--- Will raise an error if the function is a closure
-- @usage error_if_runetime_closure(func)
function Common.error_if_runetime_closure(func)
if _LIFECYCLE == 8 and Debug.is_closure(func) then
local function_name = debug.getinfo(2, 'n').name or '<anon>'
error(function_name..' can not be called during runtime with a closure', 3)
end
end
--- Value Returns.
-- @section valueReturns
@@ -255,7 +246,7 @@ local file_path = get_file_path()
]]
function Common.get_file_path(offset)
offset = offset or 0
return debug.getinfo(offset+2, 'S').source:match('^.+/currently%-playing/(.+)$'):sub(1, -5)
return debug.getinfo(offset+2, 'S').short_src:sub(10, -5)
end
--[[-- Converts a table to an enum

View File

@@ -146,7 +146,7 @@ PlayerData.Statistics:combine('JoinCount')
]]
local Event = require 'utils.event' --- @dep utils.event
local Storage = require("modules/exp_util/storage")
local DatastoreManager = {}
local Datastores = {}
@@ -156,9 +156,8 @@ local copy = table.deep_copy
local trace = debug.traceback
--- Save datastores in the global table
global.datastores = Data
Event.on_load(function()
Data = global.datastores
Storage.register(Data, function(tbl)
Data = tbl
for datastoreName, datastore in pairs(Datastores) do
datastore.data = Data[datastoreName]
end
@@ -187,7 +186,7 @@ local ExampleData = Datastore.connect('ExampleData', true) -- saveToDisk
]]
function DatastoreManager.connect(datastoreName, saveToDisk, autoSave, propagateChanges)
if Datastores[datastoreName] then return Datastores[datastoreName] end
if _LIFECYCLE ~= _STAGE.control then
if package.lifecycle ~= package.lifecycle_stage.control then
-- Only allow this function to be called during the control stage
error('New datastore connection can not be created during runtime', 2)
end

View File

@@ -4,7 +4,7 @@
@alias External
@usage-- Printing all server to chat
local External = require 'expcore.external' --- @dep expcore.external
local External = require("modules.exp_legacy.expcore.external") --- @dep expcore.external
local message = 'id: %s name: %s version: %s status: %s'
for server_id, server in pairs(External.get_servers()) do
@@ -29,11 +29,11 @@ end
]]
function External.valid()
if global.ext == nil then return false end
if ext == global.ext and var == ext.var then
if storage.ext == nil then return false end
if ext == storage.ext and var == ext.var then
return var ~= nil
else
ext = global.ext
ext = storage.ext
var = ext.var
return var ~= nil
end

View File

@@ -1 +1 @@
return require 'expcore.gui._require'
return require("modules.exp_legacy.expcore.gui._require")

View File

@@ -121,15 +121,15 @@ end)
]]
local Gui = require 'expcore.gui.prototype'
require 'expcore.gui.helper_functions'
require 'expcore.gui.core_defines'
require 'expcore.gui.top_flow'
require 'expcore.gui.left_flow'
require 'expcore.gui.defines'
local Gui = require("modules.exp_legacy.expcore.gui.prototype")
require("modules.exp_legacy.expcore.gui.helper_functions")
require("modules.exp_legacy.expcore.gui.core_defines")
require("modules.exp_legacy.expcore.gui.top_flow")
require("modules.exp_legacy.expcore.gui.left_flow")
require("modules.exp_legacy.expcore.gui.defines")
local Roles = _C.opt_require('expcore.roles')
local Event = _C.opt_require('utils.event')
local Roles = _C.opt_require("modules.exp_legacy.expcore.roles")
local Event = _C.opt_require("modules/exp_legacy/utils/event")
if Roles and Event then
Event.add(Roles.events.on_role_assigned, function(e)

View File

@@ -3,8 +3,8 @@
@module Gui
]]
local Gui = require 'expcore.gui.prototype'
local Event = require 'utils.event'
local Gui = require("modules.exp_legacy.expcore.gui.prototype")
local Event = require("modules/exp_legacy/utils/event")
--- Core Defines.
-- @section coreDefines

View File

@@ -3,7 +3,7 @@
@module Gui
]]
local Gui = require 'expcore.gui.prototype'
local Gui = require("modules.exp_legacy.expcore.gui.prototype")
--- Defines.
-- @section defines

View File

@@ -3,7 +3,7 @@
@module Gui
]]
local Gui = require 'expcore.gui.prototype'
local Gui = require("modules.exp_legacy.expcore.gui.prototype")
--- Helper Functions.
-- @section helperFunctions

View File

@@ -3,7 +3,7 @@
@module Gui
]]
local Gui = require 'expcore.gui.prototype'
local Gui = require("modules.exp_legacy.expcore.gui.prototype")
local mod_gui = require 'mod-gui'
local hide_left_flow = Gui.core_defines.hide_left_flow.name
@@ -89,7 +89,7 @@ end
function Gui.inject_left_flow_order(provider)
Gui.get_left_flow_order = provider
local debug_info = debug.getinfo(2, "Sn")
local file_name = debug_info.source:match('^.+/currently%-playing/(.+)$'):sub(1, -5)
local file_name = debug_info.short_src:sub(10, -5)
local func_name = debug_info.name or ("<anonymous:"..debug_info.linedefined..">")
Gui._left_flow_order_src = file_name..":"..func_name
end

View File

@@ -3,7 +3,7 @@
@module Gui
]]
local Event = require 'utils.event' --- @dep utils.event
local Event = require("modules/exp_legacy/utils/event") --- @dep utils.event
local Gui = {
--- The current highest uid that is being used by a define, will not increase during runtime
@@ -58,7 +58,7 @@ end
--- Get where a function was defined as a string
local function get_defined_at(level)
local debug_info = debug.getinfo(level, "Sn")
local file_name = debug_info.source:match('^.+/currently%-playing/(.+)$'):sub(1, -5)
local file_name = debug_info.short_src:sub(10, -5)
local func_name = debug_info.name or ("<anonymous:"..debug_info.linedefined..">")
return file_name..":"..func_name
end

View File

@@ -3,7 +3,7 @@
@module Gui
]]
local Gui = require 'expcore.gui.prototype'
local Gui = require("modules.exp_legacy.expcore.gui.prototype")
local mod_gui = require 'mod-gui' --- @dep mod-gui
local toolbar_button_size = 36
@@ -107,7 +107,7 @@ end
function Gui.inject_top_flow_order(provider)
Gui.get_top_flow_order = provider
local debug_info = debug.getinfo(2, "Sn")
local file_name = debug_info.source:match('^.+/currently%-playing/(.+)$'):sub(1, -5)
local file_name = debug_info.short_src:sub(10, -5)
local func_name = debug_info.name or ("<anonymous:"..debug_info.linedefined..">")
Gui._top_flow_order_src = file_name..":"..func_name
end

View File

@@ -24,9 +24,9 @@ Permission_Groups.new_group('Restricted') -- this defines a new group called "Re
]]
local Game = require 'utils.game' --- @dep utils.game
local Event = require 'utils.event' --- @dep utils.event
local Async = require 'expcore.async' --- @dep expcore.async
local Game = require("modules.exp_legacy.utils.game") --- @dep utils.game
local Event = require("modules/exp_legacy/utils/event") --- @dep utils.event
local Async = require("modules/exp_util/async")
local Permissions_Groups = {
groups={}, -- store for the different groups that are created
@@ -34,18 +34,18 @@ local Permissions_Groups = {
}
-- Async function to add players to permission groups
local add_to_permission_group =
local add_to_permission_group_async =
Async.register(function(permission_group, player)
permission_group.add_player(player)
end)
Permissions_Groups.async_token_add_to_permission_group = add_to_permission_group
Permissions_Groups.add_to_permission_group_async = add_to_permission_group_async
-- Async function to remove players from permission groups
local remove_from_permission_group =
local remove_from_permission_group_async =
Async.register(function(permission_group, player)
permission_group.remove_player(player)
end)
Permissions_Groups.async_token_remove_from_permission_group = remove_from_permission_group
Permissions_Groups.remove_from_permission_group_async = remove_from_permission_group_async
--- Getters.
-- Functions that get permission groups
@@ -286,7 +286,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
Async(add_to_permission_group, group, player)
add_to_permission_group_async(group, player)
return true
end
@@ -302,7 +302,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
Async(remove_from_permission_group, group, player)
remove_from_permission_group_async(group, player)
return true
end

View File

@@ -3,7 +3,7 @@
@core PlayerData
@usage-- Adding a colour setting for players
local PlayerData = require 'expcore.player_data'
local PlayerData = require("modules.exp_legacy.expcore.player_data")
local PlayerColors = PlayerData.Settings:combine('Color')
-- Set the players color when their data is loaded
@@ -19,8 +19,8 @@ PlayerColors:on_save(function(player_name, _)
end)
@usage-- Add a playtime statistic for players
local Event = require 'utils.event'
local PlayerData = require 'expcore.player_data'
local Event = require("modules/exp_legacy/utils/event")
local PlayerData = require("modules.exp_legacy.expcore.player_data")
local Playtime = PlayerData.Statistics:combine('Playtime')
-- When playtime reaches an hour interval tell the player and say thanks
@@ -41,11 +41,11 @@ end)
]]
local Event = require 'utils.event' --- @dep utils.event
local Async = require 'expcore.async' --- @dep expcore.async
local Datastore = require 'expcore.datastore' --- @dep expcore.datastore
local Commands = require 'expcore.commands' --- @dep expcore.commands
require 'config.expcore.command_general_parse' --- @dep config.expcore.command_general_parse
local Async = require("modules/exp_util/async")
local Event = require("modules/exp_legacy/utils/event") --- @dep utils.event
local Datastore = require("modules.exp_legacy.expcore.datastore") --- @dep expcore.datastore
local Commands = require("modules.exp_legacy.expcore.commands") --- @dep expcore.commands
require("modules.exp_legacy.config.expcore.command_general_parse") --- @dep config.expcore.command_general_parse
--- Common player data that acts as the root store for player data
local PlayerData = Datastore.connect('PlayerData', true) -- saveToDisk
@@ -86,7 +86,8 @@ Commands.new_command('save-data', 'Writes all your player data to a file on your
end)
--- Async function called after 5 seconds with no player data loaded
local check_data_loaded = Async.register(function(player)
local check_data_loaded_async =
Async.register(function(player)
local player_data = PlayerData:get(player)
if not player_data or not player_data.valid then
player.print{'expcore-data.data-failed'}
@@ -127,7 +128,7 @@ end)
--- Load player data when they join
Event.add(defines.events.on_player_joined_game, function(event)
local player = game.players[event.player_index]
Async.wait(300, check_data_loaded, player)
check_data_loaded_async:start_after(300, player)
PlayerData:raw_set(player.name)
PlayerData:request(player)
end)

View File

@@ -108,12 +108,12 @@ Roles.define_role_order{
]]
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 Async = require 'expcore.async' --- @dep expcore.async
local Colours = require 'utils.color_presets' --- @dep utils.color_presets
local Async = require("modules/exp_util/async")
local Storage = require("modules/exp_util/storage")
local Game = require("modules.exp_legacy.utils.game") --- @dep utils.game
local Event = require("modules/exp_legacy/utils/event") --- @dep utils.event
local Groups = require("modules.exp_legacy.expcore.permission_groups") --- @dep expcore.permission_groups
local Colours = require("modules/exp_util/include/color")
local write_json = _C.write_json --- @dep expcore.common
local Roles = {
@@ -134,7 +134,10 @@ local Roles = {
}
--- When global is loaded it will have the metatable re-assigned to the roles
Global.register({ Roles.config.players, Roles.config.deferred_roles }, function(tbl)
Storage.register({
Roles.config.players,
Roles.config.deferred_roles
}, function(tbl)
Roles.config.players = tbl[1]
Roles.config.deferred_roles = tbl[2]
end)
@@ -652,7 +655,6 @@ end)
]]
function Roles.define_flag_trigger(name, callback)
_C.error_if_runtime()
Roles.config.flags[name] = Async.register(callback)
end
@@ -1076,9 +1078,9 @@ end
local function role_update(event)
local player = game.players[event.player_index]
-- Updates flags given to the player
for flag, async_token in pairs(Roles.config.flags) do
for flag, async_function in pairs(Roles.config.flags) do
local state = Roles.player_has_flag(player, flag)
Async(async_token, player, state)
async_function(player, state)
end
-- Updates the players permission group
local highest = Roles.get_player_highest_role(player)
@@ -1086,7 +1088,7 @@ local function role_update(event)
if highest.permission_group[1] then
local group = game.permissions.get_group(highest.permission_group[2])
if group then
Async(Groups.async_token_add_to_permission_group, group, player)
Groups.add_to_permission_group_async(group, player)
end
else
Groups.set_player_group(player, highest.permission_group)