From 58c2c67aebaf1a8e4285bd7c20f21728a9f29329 Mon Sep 17 00:00:00 2001 From: NovaBot <154629622+NovaBot13@users.noreply.github.com> Date: Sat, 23 Mar 2024 19:23:45 -0400 Subject: [PATCH 1/3] [MIRROR] Fixes lua error logging and a few timer.lua functions (#1591) * Fixes lua error logging and a few timer.lua functions * Update lua_state.dm --------- Co-authored-by: Watermelon914 <37270891+Watermelon914@users.noreply.github.com> Co-authored-by: Bloop <13398309+vinylspiders@users.noreply.github.com> --- code/modules/admin/verbs/lua/lua_state.dm | 12 +++ lua/timer.lua | 109 ++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 lua/timer.lua diff --git a/code/modules/admin/verbs/lua/lua_state.dm b/code/modules/admin/verbs/lua/lua_state.dm index d5b2c6de65d..c60dae27fba 100644 --- a/code/modules/admin/verbs/lua/lua_state.dm +++ b/code/modules/admin/verbs/lua/lua_state.dm @@ -89,6 +89,18 @@ GLOBAL_PROTECT(lua_usr) return result +<<<<<<< HEAD +======= +/datum/lua_state/process(seconds_per_tick) + if(timer_enabled) + var/result = call_function("__Timer_timer_process", seconds_per_tick) + log_result(result, verbose = FALSE) + for(var/function as anything in functions_to_execute) + result = call_function(list("__Timer_callbacks", function)) + log_result(result, verbose = FALSE) + functions_to_execute.Cut() + +>>>>>>> 12ab2ab1664 ([MIRROR] Fixes lua error logging and a few timer.lua functions (#1591)) /datum/lua_state/proc/call_function(function, ...) var/call_args = length(args) > 1 ? args.Copy(2) : list() if(islist(function)) diff --git a/lua/timer.lua b/lua/timer.lua new file mode 100644 index 00000000000..605e5b98a2e --- /dev/null +++ b/lua/timer.lua @@ -0,0 +1,109 @@ +local SS13 = require("SS13_base") +local Timer = {} + +__Timer_timers = __Timer_timers or {} +__Timer_callbacks = __Timer_callbacks or {} + +function __add_internal_timer(func, time, loop) + local timer = { + loop = loop, + executeTime = time + dm.world:get_var("time") + } + __Timer_callbacks[tostring(func)] = function() + timer.executing = false + if loop and timer.terminate ~= true then + timer.executeTime = dm.world:get_var("time") + time + else + __stop_internal_timer(tostring(func)) + end + func() + end + __Timer_timers[tostring(func)] = timer + return tostring(func) +end + +function __stop_internal_timer(func) + local timer = __Timer_timers[func] + if timer then + if not timer.executing then + __Timer_timers[func] = nil + __Timer_callbacks[func] = nil + else + timer.terminate = true + end + end +end + +__Timer_timer_processing = __Timer_timer_processing or false +SS13.state:set_var("timer_enabled", 1) +__Timer_timer_process = function(seconds_per_tick) + if __Timer_timer_processing then + return 0 + end + __Timer_timer_processing = true + local time = dm.world:get_var("time") + for func, timeData in __Timer_timers do + if timeData.executing == true then + continue + end + if over_exec_usage(0.85) then + sleep() + end + if time >= timeData.executeTime then + SS13.state:get_var("functions_to_execute"):add(func) + timeData.executing = true + end + end + __Timer_timer_processing = false + return 1 +end + +function Timer.wait(time) + local next_yield_index = __next_yield_index + __add_internal_timer(function() + SS13.SSlua:call_proc("queue_resume", SS13.state, next_yield_index) + end, time * 10, false) + coroutine.yield() +end + +function Timer.set_timeout(time, func) + Timer.start_loop(time, 1, func) +end + +function Timer.start_loop(time, amount, func) + if not amount or amount == 0 then + return + end + if amount == -1 then + return __add_internal_timer(func, time * 10, true) + end + if amount == 1 then + return __add_internal_timer(func, time * 10, false) + end + -- Lua counts from 1 so let's keep consistent with that + local doneAmount = 1 + local funcId + local newFunc = function() + func(doneAmount) + doneAmount += 1 + if doneAmount > amount then + Timer.end_loop(funcId) + end + end + funcId = __add_internal_timer(newFunc, time * 10, true) + return funcId +end + +function Timer.end_loop(id) + __stop_internal_timer(id) +end + +function Timer.stop_all_loops() + for id, data in __Timer_timers do + if data.loop then + Timer.end_loop(id) + end + end +end + +return Timer From 940a714276a82528ea55c46c9d8a9e58ff21297a Mon Sep 17 00:00:00 2001 From: ReezeBL Date: Sun, 24 Mar 2024 20:15:19 +0300 Subject: [PATCH 2/3] Update lua_state.dm --- code/modules/admin/verbs/lua/lua_state.dm | 3 --- 1 file changed, 3 deletions(-) diff --git a/code/modules/admin/verbs/lua/lua_state.dm b/code/modules/admin/verbs/lua/lua_state.dm index c60dae27fba..1fe8cdaa30f 100644 --- a/code/modules/admin/verbs/lua/lua_state.dm +++ b/code/modules/admin/verbs/lua/lua_state.dm @@ -89,8 +89,6 @@ GLOBAL_PROTECT(lua_usr) return result -<<<<<<< HEAD -======= /datum/lua_state/process(seconds_per_tick) if(timer_enabled) var/result = call_function("__Timer_timer_process", seconds_per_tick) @@ -100,7 +98,6 @@ GLOBAL_PROTECT(lua_usr) log_result(result, verbose = FALSE) functions_to_execute.Cut() ->>>>>>> 12ab2ab1664 ([MIRROR] Fixes lua error logging and a few timer.lua functions (#1591)) /datum/lua_state/proc/call_function(function, ...) var/call_args = length(args) > 1 ? args.Copy(2) : list() if(islist(function)) From 0a593e5cf7b96af89a65cab3cd582b98e6eab2cf Mon Sep 17 00:00:00 2001 From: Iajret Date: Mon, 25 Mar 2024 23:01:09 +0300 Subject: [PATCH 3/3] empty commit