Ports upstream LUA PRs (#15200)

* 1

* Launch Dreamseeker is the default vscode launch config once again (#68666)

Co-authored-by: William Wallace <me@wiox.me>
This commit is contained in:
Zonespace
2022-07-27 15:14:22 -07:00
committed by GitHub
co-authored by William Wallace
parent b2d2a90916
commit a1aee41c68
10 changed files with 190 additions and 86 deletions
+62 -25
View File
@@ -17,7 +17,7 @@ function SS13.istype(thing, type)
end
function SS13.new(type, ...)
local datum = dm.global_proc("_new", type, {...})
local datum = dm.global_proc("_new", type, { ... })
local references = SS13.state:get_var("references")
references:add(datum)
return datum
@@ -40,40 +40,60 @@ end
function SS13.wait(time, timer)
local index = #__yield_table + 1
local callback = SS13.new("/datum/callback", SS13.SSlua, "queue_resume", SS13.state, index)
local timedevent = dm.global_proc("_addtimer", callback, time*10, 8, timer, debug.info(1, "sl"))
local timedevent = dm.global_proc("_addtimer", callback, time * 10, 8, timer, debug.info(1, "sl"))
coroutine.yield()
dm.global_proc("deltimer", timedevent, timer)
end
function SS13.register_signal(datum, signal, func, make_easy_clear_function)
if not SS13.signal_handlers then
SS13.signal_handlers = {}
end
if not SS13.istype(datum, "/datum") then return end
if not SS13.istype(datum, "/datum") then
return
end
local ref = dm.global_proc("REF", datum)
if not SS13.signal_handlers[ref] then
SS13.signal_handlers[ref] = {}
end
if signal == "_cleanup" then return end
if signal == "_cleanup" then
return
end
if not SS13.signal_handlers[ref][signal] then
SS13.signal_handlers[ref][signal] = {}
end
local callback = SS13.new("/datum/callback", SS13.state, "call_function_return_first")
callback:call_proc("RegisterSignal", datum, signal, "Invoke")
local callback_ref = dm.global_proc("REF", callback)
local path = {"SS13", "signal_handlers", ref, signal, callback_ref, "func"}
callback:set_var("arguments", {path})
local path = { "SS13", "signal_handlers", ref, signal, callback_ref, "func" }
callback:set_var("arguments", { path })
if not SS13.signal_handlers[ref]["_cleanup"] then
local cleanup_path = {"SS13", "signal_handlers", ref, "_cleanup"}
local cleanup_path = { "SS13", "signal_handlers", ref, "_cleanup", "func" }
local cleanup_callback = SS13.new("/datum/callback", SS13.state, "call_function_return_first", cleanup_path)
cleanup_callback:call_proc("RegisterSignal", datum, "parent_qdeleting", "Invoke")
SS13.signal_handlers[ref]["_cleanup"] = function(datum)
SS13.signal_handler_cleanup(datum)
dm.global_proc("qdel", cleanup_callback)
SS13.signal_handlers[ref]["_cleanup"] = {
func = function(datum)
SS13.signal_handler_cleanup(datum)
dm.global_proc("qdel", cleanup_callback)
end,
callback = cleanup_callback,
}
end
if signal == "parent_qdeleting" then --We want to make sure that the cleanup function is the very last signal handler called.
local comp_lookup = datum:get_var("comp_lookup")
if comp_lookup then
local lookup_table = comp_lookup:to_table()
local lookup_for_signal = lookup_table.parent_qdeleting
if lookup_for_signal and not SS13.istype(lookup_for_signal, "/datum") then
local cleanup_callback_index =
dm.global_proc("_list_find", lookup_for_signal, SS13.signal_handlers[ref]["_cleanup"].callback)
if cleanup_callback_index ~= 0 then
dm.global_proc("_list_swap", lookup_for_signal, cleanup_callback_index, lookup_for_signal.len)
end
end
end
end
SS13.signal_handlers[ref][signal][callback_ref] = {func=func, callback=callback}
SS13.signal_handlers[ref][signal][callback_ref] = { func = func, callback = callback }
if make_easy_clear_function then
local clear_function_name = "clear_signal_" .. ref .. "_" .. signal .. "_" .. callback_ref
SS13[clear_function_name] = function()
@@ -86,17 +106,22 @@ function SS13.register_signal(datum, signal, func, make_easy_clear_function)
return callback
end
function SS13.unregister_signal(datum, signal, callback)
local function clear_handler(handler_info)
if not handler_info then return end
if not handler_info.callback then return end
if not handler_info then
return
end
if not handler_info.callback then
return
end
local handler_callback = handler_info.callback
handler_callback:call_proc("UnregisterSignal", datum, signal)
dm.global_proc("qdel", handler_callback)
end
if not SS13.signal_handlers then return end
if not SS13.signal_handlers then
return
end
local ref = dm.global_proc("REF", datum)
local function clear_easy_clear_function(callback_ref)
@@ -104,9 +129,15 @@ function SS13.unregister_signal(datum, signal, callback)
SS13[clear_function_name] = nil
end
if not SS13.signal_handlers[ref] then return end
if signal == "_cleanup" then return end
if not SS13.signal_handlers[ref][signal] then return end
if not SS13.signal_handlers[ref] then
return
end
if signal == "_cleanup" then
return
end
if not SS13.signal_handlers[ref][signal] then
return
end
if not callback then
for handler_key, handler_info in SS13.signal_handlers[ref][signal] do
@@ -115,7 +146,9 @@ function SS13.unregister_signal(datum, signal, callback)
end
SS13.signal_handlers[ref][signal] = nil
else
if not SS13.istype(callback, "/datum/callback") then return end
if not SS13.istype(callback, "/datum/callback") then
return
end
local callback_ref = dm.global_proc("REF", callback)
clear_easy_clear_function(callback_ref)
clear_handler(SS13.signal_handlers[ref][signal][callback_ref])
@@ -124,9 +157,13 @@ function SS13.unregister_signal(datum, signal, callback)
end
function SS13.signal_handler_cleanup(datum)
if not SS13.signal_handlers then return end
if not SS13.signal_handlers then
return
end
local ref = dm.global_proc("REF", datum)
if not SS13.signal_handlers[ref] then return end
if not SS13.signal_handlers[ref] then
return
end
for signal, _ in SS13.signal_handlers[ref] do
SS13.unregister_signal(datum, signal)
@@ -145,9 +182,9 @@ function SS13.set_timeout(time, func)
SS13.timeouts[callback_ref] = nil
func()
end
local path = {"SS13", "timeouts", callback_ref}
callback:set_var("arguments", {path})
dm.global_proc("_addtimer", callback, time*10, 8, nil, debug.info(1, "sl"))
local path = { "SS13", "timeouts", callback_ref }
callback:set_var("arguments", { path })
dm.global_proc("_addtimer", callback, time * 10, 8, nil, debug.info(1, "sl"))
end
return SS13