register.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. core.callback_origins = {}
  2. local getinfo = debug.getinfo
  3. debug.getinfo = nil
  4. --- Runs given callbacks.
  5. --
  6. -- Note: this function is also called from C++
  7. -- @tparam table callbacks a table with registered callbacks, like `core.registered_on_*`
  8. -- @tparam number mode a RunCallbacksMode, as defined in src/script/common/c_internal.h
  9. -- @param ... arguments for the callback
  10. -- @return depends on mode
  11. function core.run_callbacks(callbacks, mode, ...)
  12. assert(type(callbacks) == "table")
  13. local cb_len = #callbacks
  14. if cb_len == 0 then
  15. if mode == 2 or mode == 3 then
  16. return true
  17. elseif mode == 4 or mode == 5 then
  18. return false
  19. end
  20. end
  21. local ret
  22. for i = 1, cb_len do
  23. local cb_ret = callbacks[i](...)
  24. if mode == 0 and i == 1 or mode == 1 and i == cb_len then
  25. ret = cb_ret
  26. elseif mode == 2 then
  27. if not cb_ret or i == 1 then
  28. ret = cb_ret
  29. end
  30. elseif mode == 3 then
  31. if cb_ret then
  32. return cb_ret
  33. end
  34. ret = cb_ret
  35. elseif mode == 4 then
  36. if (cb_ret and not ret) or i == 1 then
  37. ret = cb_ret
  38. end
  39. elseif mode == 5 and cb_ret then
  40. return cb_ret
  41. end
  42. end
  43. return ret
  44. end
  45. --
  46. -- Callback registration
  47. --
  48. local function make_registration()
  49. local t = {}
  50. local registerfunc = function(func)
  51. t[#t + 1] = func
  52. core.callback_origins[func] = {
  53. mod = core.get_current_modname() or "??",
  54. name = getinfo(1, "n").name or "??"
  55. }
  56. --local origin = core.callback_origins[func]
  57. --print(origin.name .. ": " .. origin.mod .. " registering cbk " .. tostring(func))
  58. end
  59. return t, registerfunc
  60. end
  61. core.registered_globalsteps, core.register_globalstep = make_registration()
  62. core.registered_on_mods_loaded, core.register_on_mods_loaded = make_registration()
  63. core.registered_on_shutdown, core.register_on_shutdown = make_registration()
  64. core.registered_on_receiving_chat_message, core.register_on_receiving_chat_message = make_registration()
  65. core.registered_on_sending_chat_message, core.register_on_sending_chat_message = make_registration()
  66. core.registered_on_chatcommand, core.register_on_chatcommand = make_registration()
  67. core.registered_on_death, core.register_on_death = make_registration()
  68. core.registered_on_hp_modification, core.register_on_hp_modification = make_registration()
  69. core.registered_on_damage_taken, core.register_on_damage_taken = make_registration()
  70. core.registered_on_formspec_input, core.register_on_formspec_input = make_registration()
  71. core.registered_on_dignode, core.register_on_dignode = make_registration()
  72. core.registered_on_punchnode, core.register_on_punchnode = make_registration()
  73. core.registered_on_placenode, core.register_on_placenode = make_registration()
  74. core.registered_on_item_use, core.register_on_item_use = make_registration()
  75. core.registered_on_modchannel_message, core.register_on_modchannel_message = make_registration()
  76. core.registered_on_modchannel_signal, core.register_on_modchannel_signal = make_registration()
  77. core.registered_on_inventory_open, core.register_on_inventory_open = make_registration()