Skip to content

Commit

Permalink
Experimental new hook registration system based on on_event()
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Mar 17, 2018
1 parent 4ab12cf commit 20a6e0c
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions data/lua/core.lua
Expand Up @@ -372,6 +372,56 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
wml.array_access.set(key, value)
end
})

-- Facility to register hooks without interfering with other hooks
-- Based on on_event()
local hooks = {}
local hooks_mt = {
__metatable = "Wesnoth hooks registry",
__newindex = function(_, k, v)
wesnoth.hooks.register(k, v)
end,
__index = function(_, k)
-- Return a COPY of the hooks list
local matching = {}
for i = 1, #hooks[k] do
table.insert(matching, hooks[k][i])
end
return matching
end
}

wesnoth.hooks = setmetatable({}, hooks_mt)

function wesnoth.hooks.register(name, arg1, arg2)
local priority = 0
local handler
if type(arg1) == "function" then
handler = arg1
else
priority = arg1
handler = arg2
end
if hooks[name] == nil then
-- Register the handler in the old way
hooks[name] = {}
local old_hook = wesnoth.game_events[name] or function(...) end
wesnoth.game_events[name] = function(...)
local ret = old_hook(...)
for k,v in pairs(hooks[name] or {}) do
v.h(...)
end
end
end
local hl = hooks[name]
table.insert(hooks[hl], {h = handler, p = priority})
-- sort it.
for i = #hl - 1, 1, -1 do
if hl[i].p < hl[i + 1].p then
hl[i], hl[i + 1] = hl[i + 1], hl[i]
end
end
end
end

-- Some C++ functions are deprecated; apply the messages here.
Expand Down

0 comments on commit 20a6e0c

Please sign in to comment.