From d0ab562b1fb22490799f42f3f90b61f01b593bab Mon Sep 17 00:00:00 2001 From: wm4 Date: Wed, 5 Aug 2020 22:58:19 +0200 Subject: lua: make hook processing more flexible This can now opt to not continue a hook after the hook callback returns. This makes it easier for scripts, and may make it unnecessary to run reentrant event loops etc. for scripts that want to wait before continuing while still running the event loop. --- player/lua/defaults.lua | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'player') diff --git a/player/lua/defaults.lua b/player/lua/defaults.lua index 72920cea67..95f8952f5d 100644 --- a/player/lua/defaults.lua +++ b/player/lua/defaults.lua @@ -547,12 +547,35 @@ end local hook_table = {} +local hook_mt = {} +hook_mt.__index = hook_mt + +function hook_mt.cont(t) + if t._id == nil then + mp.msg.error("hook already continued") + else + mp.raw_hook_continue(t._id) + t._id = nil + end +end + +function hook_mt.defer(t) + t._defer = true +end + mp.register_event("hook", function(ev) local fn = hook_table[tonumber(ev.id)] + local hookobj = { + _id = ev.hook_id, + _defer = false, + } + setmetatable(hookobj, hook_mt) if fn then - fn() + fn(hookobj) + end + if (not hookobj._defer) and hookobj._id ~= nil then + hookobj:cont() end - mp.raw_hook_continue(ev.hook_id) end) function mp.add_hook(name, pri, cb) -- cgit v1.2.3