Fix Minetest blaming the wrong mod for errors (#12241)

Covers the case where mods insert their callbacks manually into "minetest.registered_<callbacks>" (often to achieve a particular order of execution).
This commit is contained in:
Lars Müller 2022-05-09 20:42:43 +02:00 committed by sfan5
parent 21f7e3a987
commit f065d3a06b
3 changed files with 10 additions and 9 deletions

@ -1,4 +1,3 @@
core.callback_origins = {} core.callback_origins = {}
local getinfo = debug.getinfo local getinfo = debug.getinfo

@ -676,9 +676,7 @@ function core.node_dig(pos, node, digger)
-- Run script hook -- Run script hook
for _, callback in ipairs(core.registered_on_dignodes) do for _, callback in ipairs(core.registered_on_dignodes) do
local origin = core.callback_origins[callback] local origin = core.callback_origins[callback]
if origin then core.set_last_run_mod(origin.mod)
core.set_last_run_mod(origin.mod)
end
-- Copy pos and node because callback can modify them -- Copy pos and node because callback can modify them
local pos_copy = vector.new(pos) local pos_copy = vector.new(pos)

@ -403,8 +403,14 @@ function core.override_item(name, redefinition)
register_item_raw(item) register_item_raw(item)
end end
do
core.callback_origins = {} local default = {mod = "??", name = "??"}
core.callback_origins = setmetatable({}, {
__index = function()
return default
end
})
end
function core.run_callbacks(callbacks, mode, ...) function core.run_callbacks(callbacks, mode, ...)
assert(type(callbacks) == "table") assert(type(callbacks) == "table")
@ -419,9 +425,7 @@ function core.run_callbacks(callbacks, mode, ...)
local ret = nil local ret = nil
for i = 1, cb_len do for i = 1, cb_len do
local origin = core.callback_origins[callbacks[i]] local origin = core.callback_origins[callbacks[i]]
if origin then core.set_last_run_mod(origin.mod)
core.set_last_run_mod(origin.mod)
end
local cb_ret = callbacks[i](...) local cb_ret = callbacks[i](...)
if mode == 0 and i == 1 then if mode == 0 and i == 1 then