From 20f6d70f7ba798e303f17b3e3f39fc2e8b9dc159 Mon Sep 17 00:00:00 2001 From: grorp Date: Wed, 1 Jan 2025 18:08:42 +0100 Subject: [PATCH] Refactor duplicated CSM registration code --- builtin/client/init.lua | 5 ++- builtin/client/register.lua | 66 +-------------------------------- builtin/common/register.lua | 6 ++- builtin/pause_menu/init.lua | 7 +++- builtin/pause_menu/register.lua | 5 +++ 5 files changed, 20 insertions(+), 69 deletions(-) create mode 100644 builtin/pause_menu/register.lua diff --git a/builtin/client/init.lua b/builtin/client/init.lua index 1f83771ea..ee0f267db 100644 --- a/builtin/client/init.lua +++ b/builtin/client/init.lua @@ -2,7 +2,10 @@ local scriptpath = core.get_builtin_path() local clientpath = scriptpath.."client"..DIR_DELIM local commonpath = scriptpath.."common"..DIR_DELIM -dofile(clientpath .. "register.lua") +local builtin_shared = {} + +assert(loadfile(commonpath .. "register.lua"))(builtin_shared) +assert(loadfile(clientpath .. "register.lua"))(builtin_shared) dofile(commonpath .. "after.lua") dofile(commonpath .. "mod_storage.lua") dofile(commonpath .. "chatcommands.lua") diff --git a/builtin/client/register.lua b/builtin/client/register.lua index d2f4afb82..0e3e68fee 100644 --- a/builtin/client/register.lua +++ b/builtin/client/register.lua @@ -1,68 +1,6 @@ -core.callback_origins = {} +local builtin_shared = ... -local getinfo = debug.getinfo -debug.getinfo = nil - ---- Runs given callbacks. --- --- Note: this function is also called from C++ --- @tparam table callbacks a table with registered callbacks, like `core.registered_on_*` --- @tparam number mode a RunCallbacksMode, as defined in src/script/common/c_internal.h --- @param ... arguments for the callback --- @return depends on mode -function core.run_callbacks(callbacks, mode, ...) - assert(type(callbacks) == "table") - local cb_len = #callbacks - if cb_len == 0 then - if mode == 2 or mode == 3 then - return true - elseif mode == 4 or mode == 5 then - return false - end - end - local ret - for i = 1, cb_len do - local cb_ret = callbacks[i](...) - - if mode == 0 and i == 1 or mode == 1 and i == cb_len then - ret = cb_ret - elseif mode == 2 then - if not cb_ret or i == 1 then - ret = cb_ret - end - elseif mode == 3 then - if cb_ret then - return cb_ret - end - ret = cb_ret - elseif mode == 4 then - if (cb_ret and not ret) or i == 1 then - ret = cb_ret - end - elseif mode == 5 and cb_ret then - return cb_ret - end - end - return ret -end - --- --- Callback registration --- - -local function make_registration() - local t = {} - local registerfunc = function(func) - t[#t + 1] = func - core.callback_origins[func] = { - mod = core.get_current_modname and core.get_current_modname() or "??", - name = getinfo(1, "n").name or "??" - } - --local origin = core.callback_origins[func] - --print(origin.name .. ": " .. origin.mod .. " registering cbk " .. tostring(func)) - end - return t, registerfunc -end +local make_registration = builtin_shared.make_registration core.registered_globalsteps, core.register_globalstep = make_registration() core.registered_on_mods_loaded, core.register_on_mods_loaded = make_registration() diff --git a/builtin/common/register.lua b/builtin/common/register.lua index c300ef90f..cbeac7c64 100644 --- a/builtin/common/register.lua +++ b/builtin/common/register.lua @@ -54,7 +54,8 @@ function builtin_shared.make_registration() local registerfunc = function(func) t[#t + 1] = func core.callback_origins[func] = { - mod = core.get_current_modname() or "??", + -- may be nil or return nil + mod = core.get_current_modname and core.get_current_modname() or "??", name = debug.getinfo(1, "n").name or "??" } end @@ -66,7 +67,8 @@ function builtin_shared.make_registration_reverse() local registerfunc = function(func) table.insert(t, 1, func) core.callback_origins[func] = { - mod = core.get_current_modname() or "??", + -- may be nil or return nil + mod = core.get_current_modname and core.get_current_modname() or "??", name = debug.getinfo(1, "n").name or "??" } end diff --git a/builtin/pause_menu/init.lua b/builtin/pause_menu/init.lua index b56f59d46..035d2ba99 100644 --- a/builtin/pause_menu/init.lua +++ b/builtin/pause_menu/init.lua @@ -1,9 +1,12 @@ local scriptpath = core.get_builtin_path() -local clientpath = scriptpath.."client"..DIR_DELIM +local pausepath = scriptpath.."pause_menu"..DIR_DELIM local commonpath = scriptpath.."common"..DIR_DELIM -- we're in-game, so no absolute paths are needed defaulttexturedir = "" -dofile(clientpath .. "register.lua") +local builtin_shared = {} + +assert(loadfile(commonpath .. "register.lua"))(builtin_shared) +assert(loadfile(pausepath .. "register.lua"))(builtin_shared) dofile(commonpath .. "settings" .. DIR_DELIM .. "init.lua") diff --git a/builtin/pause_menu/register.lua b/builtin/pause_menu/register.lua new file mode 100644 index 000000000..ea97ca281 --- /dev/null +++ b/builtin/pause_menu/register.lua @@ -0,0 +1,5 @@ +local builtin_shared = ... + +local make_registration = builtin_shared.make_registration + +core.registered_on_formspec_input, core.register_on_formspec_input = make_registration()