register.lua 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. core.callback_origins = {}
  2. local getinfo = debug.getinfo
  3. debug.getinfo = nil
  4. function core.run_callbacks(callbacks, mode, ...)
  5. assert(type(callbacks) == "table")
  6. local cb_len = #callbacks
  7. if cb_len == 0 then
  8. if mode == 2 or mode == 3 then
  9. return true
  10. elseif mode == 4 or mode == 5 then
  11. return false
  12. end
  13. end
  14. local ret
  15. for i = 1, cb_len do
  16. local cb_ret = callbacks[i](...)
  17. if mode == 0 and i == 1 or mode == 1 and i == cb_len then
  18. ret = cb_ret
  19. elseif mode == 2 then
  20. if not cb_ret or i == 1 then
  21. ret = cb_ret
  22. end
  23. elseif mode == 3 then
  24. if cb_ret then
  25. return cb_ret
  26. end
  27. ret = cb_ret
  28. elseif mode == 4 then
  29. if (cb_ret and not ret) or i == 1 then
  30. ret = cb_ret
  31. end
  32. elseif mode == 5 and cb_ret then
  33. return cb_ret
  34. end
  35. end
  36. return ret
  37. end
  38. --
  39. -- Callback registration
  40. --
  41. local function make_registration()
  42. local t = {}
  43. local registerfunc = function(func)
  44. t[#t + 1] = func
  45. core.callback_origins[func] = {
  46. mod = core.get_current_modname() or "??",
  47. name = getinfo(1, "n").name or "??"
  48. }
  49. --local origin = core.callback_origins[func]
  50. --print(origin.name .. ": " .. origin.mod .. " registering cbk " .. tostring(func))
  51. end
  52. return t, registerfunc
  53. end
  54. core.registered_globalsteps, core.register_globalstep = make_registration()
  55. core.registered_on_shutdown, core.register_on_shutdown = make_registration()
  56. core.registered_on_connect, core.register_on_connect = make_registration()
  57. core.registered_on_receiving_chat_message, core.register_on_receiving_chat_message = make_registration()
  58. core.registered_on_sending_chat_message, core.register_on_sending_chat_message = make_registration()
  59. core.registered_on_death, core.register_on_death = make_registration()
  60. core.registered_on_hp_modification, core.register_on_hp_modification = make_registration()
  61. core.registered_on_damage_taken, core.register_on_damage_taken = make_registration()
  62. core.registered_on_formspec_input, core.register_on_formspec_input = make_registration()
  63. core.registered_on_dignode, core.register_on_dignode = make_registration()
  64. core.registered_on_punchnode, core.register_on_punchnode = make_registration()
  65. core.registered_on_placenode, core.register_on_placenode = make_registration()
  66. core.registered_on_item_use, core.register_on_item_use = make_registration()
  67. core.registered_on_modchannel_message, core.register_on_modchannel_message = make_registration()
  68. core.registered_on_modchannel_signal, core.register_on_modchannel_signal = make_registration()
  69. core.registered_on_inventory_open, core.register_on_inventory_open = make_registration()