Throw error if mod is trying to hook something that isnt a function

This commit is contained in:
FireSiku 2018-06-10 20:35:49 -04:00
parent 6b06745986
commit bee94c1665

View file

@ -6,9 +6,9 @@ local vmf = get_mod("VMF")
-- hook_type is an identifier to help distinguish the different api calls. -- hook_type is an identifier to help distinguish the different api calls.
local HOOK_TYPES = { local HOOK_TYPES = {
hook = 1, hook = 1,
safe = 2, hook_safe = 2,
origin = 3, hook_origin = 3,
} }
-- Constants to ease on table lookups when not needed -- Constants to ease on table lookups when not needed
@ -291,6 +291,11 @@ local function generic_hook(self, obj, method, handler, func_name)
-- obj can't be a string for these now. -- obj can't be a string for these now.
local orig = get_orig_function(self, obj, method) local orig = get_orig_function(self, obj, method)
if type(orig) ~= "function" then
self:error("(%s): trying to hook %s (a %s), not a function.", func_name, method, type(orig))
return
end
return create_hook(self, orig, obj, method, handler, func_name, hook_type) return create_hook(self, orig, obj, method, handler, func_name, hook_type)
end end
@ -336,7 +341,7 @@ end
-- The handler is never given the a "func" parameter. -- The handler is never given the a "func" parameter.
-- These will always be executed the original function and the hook chain. -- These will always be executed the original function and the hook chain.
function VMFMod:hook_safe(obj, method, handler) function VMFMod:hook_safe(obj, method, handler)
return generic_hook(self, obj, method, handler, "safe") return generic_hook(self, obj, method, handler, "hook_safe")
end end
-- :hook() will allow you to hook a function, allowing your handler to replace the function in the stack, -- :hook() will allow you to hook a function, allowing your handler to replace the function in the stack,
@ -354,7 +359,7 @@ end
-- This there is a limit of a single origin hook for any given function. -- This there is a limit of a single origin hook for any given function.
-- This should only be used as a last resort due to its limitation and its potential to break the game if not careful. -- This should only be used as a last resort due to its limitation and its potential to break the game if not careful.
function VMFMod:hook_origin(obj, method, handler) function VMFMod:hook_origin(obj, method, handler)
return generic_hook(self, obj, method, handler, "origin") return generic_hook(self, obj, method, handler, "hook_origin")
end end
-- Enable/disable functions for all hook types: -- Enable/disable functions for all hook types: