Skip to content

Commit

Permalink
feat(lua): Added an unregister function that is returned by callback …
Browse files Browse the repository at this point in the history
…registration functions in Lua.
  • Loading branch information
snowyu committed Jul 5, 2023
1 parent 1b95998 commit b3c693c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 37 deletions.
44 changes: 28 additions & 16 deletions builtin/client/register.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,56 @@ function core.run_callbacks(callbacks, mode, ...)
end
local ret
for i = 1, cb_len do
local cb_ret = callbacks[i](...)
if type(callbacks[i]) == 'function' then
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
if mode == 0 and i == 1 or mode == 1 and i == cb_len 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
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
elseif mode == 5 and cb_ret then
return cb_ret
end
end
return ret
end

local function get_nil_index(list)
for i = 1, #list do
if list[i] == nil then return i end
end
end

--
-- Callback registration
--

local function make_registration()
local t = {}
local registerfunc = function(func)
t[#t + 1] = func
local i = get_nil_index(t) or #t + 1
t[i] = func
core.callback_origins[func] = {
mod = core.get_current_modname() or "??",
name = getinfo(1, "n").name or "??"
}
--local origin = core.callback_origins[func]
--print(origin.name .. ": " .. origin.mod .. " registering cbk " .. tostring(func))
return function()
t[i] = nil
end
end
return t, registerfunc
end
Expand Down
54 changes: 33 additions & 21 deletions builtin/game/register.lua
Original file line number Diff line number Diff line change
Expand Up @@ -441,29 +441,31 @@ function core.run_callbacks(callbacks, mode, ...)
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
if type(callbacks[i]) == 'function' then
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
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
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
elseif mode == 5 and cb_ret then
return cb_ret
end
end
return ret
Expand All @@ -481,20 +483,30 @@ function core.run_priv_callbacks(name, priv, caller, method)
end
end

local function get_nil_index(list)
for i = 1, #list do
if list[i] == nil then return i end
end
end

--
-- Callback registration
--

local function make_registration()
local t = {}
local registerfunc = function(func)
t[#t + 1] = func
local i = get_nil_index(t) or #t + 1
t[i] = 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))
return function()
t[i] = nil
end
end
return t, registerfunc
end
Expand Down
3 changes: 3 additions & 0 deletions doc/client_lua_api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,11 @@ Minetest namespace reference
`"info"`, or `"verbose"`. Default is `"none"`.

### Global callback registration functions

Call these functions only at load time!

These callback registration functions will return an unregister function that can be used to unregister the corresponding registration at a later time.

* `minetest.register_globalstep(function(dtime))`
* Called every client environment step, usually interval of 0.1s
* `minetest.register_on_mods_loaded(function())`
Expand Down
2 changes: 2 additions & 0 deletions doc/lua_api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5210,6 +5210,8 @@ Global callback registration functions

Call these functions only at load time!

These callback registration functions will return an unregister function that can be used to unregister the corresponding registration at a later time.

* `minetest.register_globalstep(function(dtime))`
* Called every server step, usually interval of 0.1s
* `minetest.register_on_mods_loaded(function())`
Expand Down

0 comments on commit b3c693c

Please sign in to comment.