Update all code styles

This commit is contained in:
Cooldude2606
2024-09-28 01:56:54 +01:00
parent 5e2a62ab27
commit 292c1a1b68
194 changed files with 9817 additions and 9703 deletions

View File

@@ -80,13 +80,13 @@ local Async = {
-- @event on_function_complete
-- @tparam AsyncFunction async_id The function which finished execution, comparable to the return of register
-- @tparam table return_values An array representing the values returned by the completed function
on_function_complete = script.generate_event_name()
on_function_complete = script.generate_event_name(),
}
Async._metatable = {
__call = function(self, ...) Async._prototype.start_soon(self, ...) end,
__index = Async._prototype,
__class = "AsyncFunction"
__class = "AsyncFunction",
}
script.register_metatable("AsyncFunction", Async._metatable)
@@ -98,16 +98,17 @@ local on_tick_mutex = false -- It is not safe to modify the globals while this v
--- Insert an item into the priority queue
local function add_to_queue(pending)
local tick = pending.tick
for index = #async_queue, 1, -1 do
if async_queue[index].tick >= tick then
async_queue[index + 1] = pending
return
else
async_queue[index + 1] = async_queue[index]
end
end
async_queue[1] = pending
local tick = pending.tick
for index = #async_queue, 1, -1 do
if async_queue[index].tick >= tick then
async_queue[index + 1] = pending
return
else
async_queue[index + 1] = async_queue[index]
end
end
async_queue[1] = pending
end
--- Static Methods.
@@ -119,7 +120,7 @@ end
-- @treturn AsyncFunction The newly registered async function
function Async.register(func)
ExpUtil.assert_not_runtime()
ExpUtil.assert_argument_type(func, "function", 1, "func")
ExpUtil.assert_argument_type(func, "function", 1, "func")
local id = ExpUtil.get_function_name(func)
Async._functions[id] = func
@@ -135,13 +136,13 @@ end
--- Run an async function on the next tick, this is the default and can be used to bypass permission groups
-- @param ... The arguments to call the function with
function Async._prototype:start_soon(...)
assert(not on_tick_mutex, "Cannot queue new async call during execution of another")
assert(not on_tick_mutex, "Cannot queue new async call during execution of another")
assert(Async._functions[self.id], "Async function is not registered")
Async._queue_pressure[self.id] = Async._queue_pressure[self.id] + 1
async_next[#async_next + 1] = {
id = self.id,
args = {...}
args = { ... },
}
end
@@ -149,22 +150,22 @@ end
-- @tparam number ticks The number of ticks to call the function after
-- @param ... The arguments to call the function with
function Async._prototype:start_after(ticks, ...)
ExpUtil.assert_argument_type(ticks, "number", 1, "ticks")
assert(not on_tick_mutex, "Cannot queue new async call during execution of another")
ExpUtil.assert_argument_type(ticks, "number", 1, "ticks")
assert(not on_tick_mutex, "Cannot queue new async call during execution of another")
assert(Async._functions[self.id], "Async function is not registered")
Async._queue_pressure[self.id] = Async._queue_pressure[self.id] + 1
add_to_queue({
add_to_queue{
id = self.id,
args = {...},
tick = game.tick + ticks
})
args = { ... },
tick = game.tick + ticks,
}
end
--- Run an async function on the next tick if the function is not already queued, allows singleton task/thread behaviour
-- @param ... The arguments to call the function with
function Async._prototype:start_task(...)
assert(not on_tick_mutex, "Cannot queue new async call during execution of another")
assert(not on_tick_mutex, "Cannot queue new async call during execution of another")
assert(Async._functions[self.id], "Async function is not registered")
if Async._queue_pressure[self.id] > 0 then return end
self:start_soon(...)
@@ -173,9 +174,9 @@ end
--- Run an async function on this tick, then queue it based on its return value
-- @param ... The arguments to call the function with
function Async._prototype:start_now(...)
assert(not on_tick_mutex, "Cannot queue new async call during execution of another")
assert(not on_tick_mutex, "Cannot queue new async call during execution of another")
assert(Async._functions[self.id], "Async function is not registered")
local status, rtn1, rtn2 = Async._functions[self.id](...)
local status, rtn1, rtn2 = Async._functions[self.id](...)
if status == Async.status.continue then
self:start_soon(table.unpack(rtn1))
elseif status == Async.status.delay then
@@ -186,10 +187,10 @@ function Async._prototype:start_now(...)
event = Async.on_function_complete,
tick = game.tick,
async_id = self.id,
returned = rtn1
returned = rtn1,
})
else
error("Async function " .. self.id .. " returned an invalid status: " .. table.inspect(status))
else
error("Async function " .. self.id .. " returned an invalid status: " .. table.inspect(status))
end
end
@@ -198,35 +199,35 @@ end
-- @section async-status
local empty_table = setmetatable({}, {
__index = function() error("Field 'Returned' is Immutable") end
__index = function() error("Field 'Returned' is Immutable") end,
}) -- File scope to allow for reuse
--- Default status, will raise on_function_complete
-- @param ... The return value of the async call
function Async.status.complete(...)
if ... == nil then
return Async.status.complete, empty_table
end
return Async.status.complete, {...}
if ... == nil then
return Async.status.complete, empty_table
end
return Async.status.complete, { ... }
end
--- Will queue the function to be called again on the next tick using the new arguments
-- @param ... The arguments to call the function with
function Async.status.continue(...)
if ... == nil then
return Async.status.continue, empty_table
end
return Async.status.continue, {...}
if ... == nil then
return Async.status.continue, empty_table
end
return Async.status.continue, { ... }
end
--- Will queue the function to be called again on a later tick using the new arguments
-- @param ... The arguments to call the function with
function Async.status.delay(ticks, ...)
ExpUtil.assert_argument_type(ticks, "number", 1, "ticks")
if ... == nil then
return Async.status.continue, ticks, empty_table
end
return Async.status.delay, ticks, {...}
ExpUtil.assert_argument_type(ticks, "number", 1, "ticks")
if ... == nil then
return Async.status.continue, ticks, empty_table
end
return Async.status.delay, ticks, { ... }
end
--- Executes an async function and processes the return value
@@ -237,49 +238,51 @@ local function exec(pending, tick, new_next, new_queue)
pending.tick = nil
pending.args = rtn1
elseif status == Async.status.delay then
new_queue[#new_queue + 1] = pending
new_queue[#new_queue + 1] = pending
pending.tick = tick + rtn1
pending.args = rtn2
elseif status == Async.status.complete or status == nil then
elseif status == Async.status.complete or status == nil then
-- The function has finished execution, raise the custom event
Async._queue_pressure[pending.id] = Async._queue_pressure[pending.id] - 1
script.raise_event(Async.on_function_complete, {
event = Async.on_function_complete,
tick = tick,
async_id = pending.id,
returned = rtn1
returned = rtn1,
})
else
error("Async function " .. pending.id .. " returned an invalid status: " .. table.inspect(status))
else
error("Async function " .. pending.id .. " returned an invalid status: " .. table.inspect(status))
end
end
local new_next, new_queue = {}, {} -- File scope to allow for reuse
--- Each tick, run all next tick functions, then check if any in the queue need to be executed
local function on_tick()
if async_next == nil then return end
if async_next == nil then return end
local tick = game.tick
-- Execute all pending functions
-- Execute all pending functions
for index = 1, #async_next, 1 do
exec(async_next[index], tick, new_next, new_queue)
async_next[index] = nil
end
for index = #async_queue, 1, -1 do
local pending = async_queue[index]
local pending = async_queue[index]
if pending.tick > tick then
break;
end
break
end
exec(pending, tick, new_next, new_queue)
async_queue[index] = nil
end
-- Queue any functions that did not complete
for index = 1, #new_next, 1 do
-- Queue any functions that did not complete
for index = 1, #new_next, 1 do
async_next[index] = new_next[index]
new_next[index] = nil
end
for index = 1, #new_queue, 1 do
for index = 1, #new_queue, 1 do
add_to_queue(new_next[index])
new_next[index] = nil
end
@@ -287,36 +290,37 @@ end
--- On load, check the queue status and update the pressure values
function Async.on_load()
if storage.exp_async_next == nil then return end
if storage.exp_async_next == nil then return end
async_next = storage.exp_async_next
async_queue = storage.exp_async_queue
async_queue = storage.exp_async_queue
for _, pending in ipairs(async_next) do
local count = Async._queue_pressure[pending.id]
if count == nil then
log("Warning: Pending async function missing after load: " .. pending.id)
Async._functions[pending.id] = function() end -- NOP
count = 0
end
Async._queue_pressure[pending.id] = count + 1
end
if count == nil then
log("Warning: Pending async function missing after load: " .. pending.id)
Async._functions[pending.id] = function() end -- NOP
count = 0
end
Async._queue_pressure[pending.id] = count + 1
end
for _, pending in ipairs(async_queue) do
local count = Async._queue_pressure[pending.id]
if count == nil then
log("Warning: Pending async function missing after load: " .. pending.id)
Async._functions[pending.id] = function() end -- NOP
count = 0
end
Async._queue_pressure[pending.id] = count + 1
if count == nil then
log("Warning: Pending async function missing after load: " .. pending.id)
Async._functions[pending.id] = function() end -- NOP
count = 0
end
Async._queue_pressure[pending.id] = count + 1
end
end
--- On server startup initialise the storage data
function Async.on_init()
if storage.exp_async_next == nil then
storage.exp_async_next = {}
storage.exp_async_queue = {}
end
Async.on_load()
if storage.exp_async_next == nil then
storage.exp_async_next = {}
storage.exp_async_queue = {}
end
Async.on_load()
end
Async.events[defines.events.on_tick] = on_tick