2017-01-21 15:02:08 +01:00
|
|
|
|
|
|
|
core.callback_origins = {}
|
|
|
|
|
2017-01-28 17:24:25 +01:00
|
|
|
local getinfo = debug.getinfo
|
|
|
|
debug.getinfo = nil
|
|
|
|
|
2020-10-21 22:05:32 +02:00
|
|
|
--- 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
|
2017-01-21 15:02:08 +01:00
|
|
|
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() or "??",
|
2017-01-28 17:24:25 +01:00
|
|
|
name = getinfo(1, "n").name or "??"
|
2017-01-21 15:02:08 +01:00
|
|
|
}
|
|
|
|
--local origin = core.callback_origins[func]
|
|
|
|
--print(origin.name .. ": " .. origin.mod .. " registering cbk " .. tostring(func))
|
|
|
|
end
|
|
|
|
return t, registerfunc
|
|
|
|
end
|
|
|
|
|
2017-01-22 11:17:41 +01:00
|
|
|
core.registered_globalsteps, core.register_globalstep = make_registration()
|
2018-06-06 12:53:59 +02:00
|
|
|
core.registered_on_mods_loaded, core.register_on_mods_loaded = make_registration()
|
2017-01-21 15:02:08 +01:00
|
|
|
core.registered_on_shutdown, core.register_on_shutdown = make_registration()
|
2017-06-09 15:48:04 +02:00
|
|
|
core.registered_on_receiving_chat_message, core.register_on_receiving_chat_message = make_registration()
|
|
|
|
core.registered_on_sending_chat_message, core.register_on_sending_chat_message = make_registration()
|
2020-10-03 18:38:08 +02:00
|
|
|
core.registered_on_chatcommand, core.register_on_chatcommand = make_registration()
|
2017-01-22 00:20:55 +01:00
|
|
|
core.registered_on_death, core.register_on_death = make_registration()
|
|
|
|
core.registered_on_hp_modification, core.register_on_hp_modification = make_registration()
|
|
|
|
core.registered_on_damage_taken, core.register_on_damage_taken = make_registration()
|
2017-01-24 17:26:15 +01:00
|
|
|
core.registered_on_formspec_input, core.register_on_formspec_input = make_registration()
|
2017-01-29 18:43:44 +01:00
|
|
|
core.registered_on_dignode, core.register_on_dignode = make_registration()
|
2017-01-29 19:28:38 +01:00
|
|
|
core.registered_on_punchnode, core.register_on_punchnode = make_registration()
|
2017-04-29 12:08:16 +02:00
|
|
|
core.registered_on_placenode, core.register_on_placenode = make_registration()
|
2017-05-06 21:12:44 +02:00
|
|
|
core.registered_on_item_use, core.register_on_item_use = make_registration()
|
2017-09-26 00:11:20 +02:00
|
|
|
core.registered_on_modchannel_message, core.register_on_modchannel_message = make_registration()
|
|
|
|
core.registered_on_modchannel_signal, core.register_on_modchannel_signal = make_registration()
|
2017-10-02 22:09:49 +02:00
|
|
|
core.registered_on_inventory_open, core.register_on_inventory_open = make_registration()
|