From 4fdd2dec593430ebfc06127f783e2bfa8e64a7d6 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Fri, 30 Dec 2022 14:35:57 +0100 Subject: [PATCH] Move core.run_callbacks and related to common folder --- builtin/common/register.lua | 74 +++++++++++++++++++++++++++++++++ builtin/game/init.lua | 3 +- builtin/game/register.lua | 81 ++----------------------------------- 3 files changed, 80 insertions(+), 78 deletions(-) create mode 100644 builtin/common/register.lua diff --git a/builtin/common/register.lua b/builtin/common/register.lua new file mode 100644 index 000000000..c300ef90f --- /dev/null +++ b/builtin/common/register.lua @@ -0,0 +1,74 @@ +local builtin_shared = ... + +do + local default = {mod = "??", name = "??"} + core.callback_origins = setmetatable({}, { + __index = function() + return default + end + }) +end + +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 = nil + for i = 1, cb_len do + local origin = core.callback_origins[callbacks[i]] + core.set_last_run_mod(origin.mod) + local cb_ret = callbacks[i](...) + + if mode == 0 and i == 1 then + ret = cb_ret + elseif 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 + +function builtin_shared.make_registration() + local t = {} + local registerfunc = function(func) + t[#t + 1] = func + core.callback_origins[func] = { + mod = core.get_current_modname() or "??", + name = debug.getinfo(1, "n").name or "??" + } + end + return t, registerfunc +end + +function builtin_shared.make_registration_reverse() + local t = {} + local registerfunc = function(func) + table.insert(t, 1, func) + core.callback_origins[func] = { + mod = core.get_current_modname() or "??", + name = debug.getinfo(1, "n").name or "??" + } + end + return t, registerfunc +end diff --git a/builtin/game/init.lua b/builtin/game/init.lua index b9d2c0baf..e6a8e800b 100644 --- a/builtin/game/init.lua +++ b/builtin/game/init.lua @@ -10,7 +10,8 @@ local builtin_shared = {} dofile(gamepath .. "constants.lua") assert(loadfile(commonpath .. "item_s.lua"))(builtin_shared) assert(loadfile(gamepath .. "item.lua"))(builtin_shared) -dofile(gamepath .. "register.lua") +assert(loadfile(commonpath .. "register.lua"))(builtin_shared) +assert(loadfile(gamepath .. "register.lua"))(builtin_shared) if core.settings:get_bool("profiler.load") then profiler = dofile(scriptpath .. "profiler" .. DIR_DELIM .. "init.lua") diff --git a/builtin/game/register.lua b/builtin/game/register.lua index cc58cad31..4ecf6ec71 100644 --- a/builtin/game/register.lua +++ b/builtin/game/register.lua @@ -1,5 +1,6 @@ -- Minetest: builtin/register.lua +local builtin_shared = ... local S = core.get_translator("__builtin") -- @@ -420,55 +421,6 @@ function core.override_item(name, redefinition) register_item_raw(item) end -do - local default = {mod = "??", name = "??"} - core.callback_origins = setmetatable({}, { - __index = function() - return default - end - }) -end - -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 = nil - for i = 1, cb_len do - local origin = core.callback_origins[callbacks[i]] - core.set_last_run_mod(origin.mod) - local cb_ret = callbacks[i](...) - - if mode == 0 and i == 1 then - ret = cb_ret - elseif 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 - function core.run_priv_callbacks(name, priv, caller, method) local def = core.registered_privileges[priv] if not def or not def["on_" .. method] or @@ -485,34 +437,6 @@ 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() or "??", - name = debug.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 function make_registration_reverse() - local t = {} - local registerfunc = function(func) - table.insert(t, 1, func) - core.callback_origins[func] = { - mod = core.get_current_modname() or "??", - name = debug.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 function make_registration_wrap(reg_fn_name, clear_fn_name) local list = {} @@ -600,6 +524,9 @@ core.registered_decorations = make_registration_wrap("register_decoration", "cle core.unregister_biome = make_wrap_deregistration(core.register_biome, core.clear_registered_biomes, core.registered_biomes) +local make_registration = builtin_shared.make_registration +local make_registration_reverse = builtin_shared.make_registration_reverse + core.registered_on_chat_messages, core.register_on_chat_message = make_registration() core.registered_on_chatcommands, core.register_on_chatcommand = make_registration() core.registered_globalsteps, core.register_globalstep = make_registration()