Add mod:enable/disable_all_hooks()

This commit is contained in:
FireSiku 2018-05-30 00:41:19 -04:00
parent 06b23556a3
commit be0e9075ef

View file

@ -39,18 +39,18 @@ _registry.origs = {}
-- #################################################################################################################### -- ####################################################################################################################
local function is_orig_hooked(obj, method) local function is_orig_hooked(obj, method)
if obj and _registry.origs[obj] and _registry.origs[obj][method] then local orig_registry = _registry.origs
if obj and orig_registry[obj] and orig_registry[obj][method] then
return true return true
elseif _registry.origs[method] then elseif orig_registry[method] then
return true return true
end end
return false return false
end end
local function is_existing_hook(self, orig, hook_type) local function is_existing_hook(self, orig, hook_type)
if _registry[self][hook_type] and local registry = _registry[self][hook_type]
_registry[self][hook_type].handler and if registry and registry.handler and registry.handler[orig] then
_registry[self][hook_type].handler[orig] then
return true return true
end end
end end
@ -63,14 +63,15 @@ end
-- Since all hooks of the chain contains the call to the previous one, we don't need to do any manual loops. -- Since all hooks of the chain contains the call to the previous one, we don't need to do any manual loops.
-- This continues until the end of the chain, where the original function is called. -- This continues until the end of the chain, where the original function is called.
local function get_hook_chain(orig) local function get_hook_chain(orig)
local hooks = _registry.hooks[HOOK_TYPE_NORMAL][orig] local hook_registry = _registry.hooks
local hooks = hook_registry[HOOK_TYPE_NORMAL][orig]
if hooks and #hooks > 0 then if hooks and #hooks > 0 then
return hooks[#hooks] return hooks[#hooks]
end end
-- We can't simply return orig here, or it would cause rawhooks to depend on load order. -- We can't simply return orig here, or it would cause rawhooks to depend on load order.
return function(...) return function(...)
if _registry.hooks[HOOK_TYPE_RAW][orig] then if hook_registry[HOOK_TYPE_RAW][orig] then
return _registry.hooks[HOOK_TYPE_RAW][orig](...) return hook_registry[HOOK_TYPE_RAW][orig](...)
else else
return orig(...) return orig(...)
end end
@ -288,6 +289,24 @@ function HookMixin:new_rawhook(obj, method, handler)
create_hook(self, orig, obj, method, handler, HOOK_TYPE_RAW) create_hook(self, orig, obj, method, handler, HOOK_TYPE_RAW)
end end
function HookMixin:enable_all_hooks()
-- Using pairs because the self table may contain nils, and order isnt important.
for _, hooks in pairs(_registry[self]) do
for orig, _ in pairs(hooks.active) do
hooks.active[orig] = true
end
end
end
function HookMixin:disable_all_hooks()
-- Using pairs because the self table may contain nils, and order isnt important.
for _, hooks in pairs(_registry[self]) do
for orig, _ in pairs(hooks.active) do
hooks.active[orig] = false
end
end
end
-- #################################################################################################################### -- ####################################################################################################################
-- ##### VMF internal functions and variables ######################################################################### -- ##### VMF internal functions and variables #########################################################################
-- #################################################################################################################### -- ####################################################################################################################