[Hooks] Clean up code by making sure obj is never nil.

Set obj to _G for global functions.

This should reduces the headache of "if obj then [obj][method] else [method] end" and make code look cleaner and less confusing in the process.
This commit is contained in:
FireSiku 2019-01-12 02:06:14 -05:00
parent 9b00e6e66e
commit bf67d37ad8

View file

@ -43,12 +43,7 @@ local _origs = {}
-- This will tell us if we already have the given function in our registry. -- This will tell us if we already have the given function in our registry.
local function is_orig_hooked(obj, method) local function is_orig_hooked(obj, method)
local orig_registry = _origs if _origs[obj] and _origs[obj][method] then
if obj then
if orig_registry[obj] and orig_registry[obj][method] then
return true
end
elseif not obj and orig_registry[method] then
return true return true
end end
return false return false
@ -57,18 +52,10 @@ end
-- Since we replace the original function, we need to keep its reference around. -- Since we replace the original function, we need to keep its reference around.
-- This will grab the cached reference if we hooked it before, otherwise return the function. -- This will grab the cached reference if we hooked it before, otherwise return the function.
local function get_orig_function(obj, method) local function get_orig_function(obj, method)
if obj then if is_orig_hooked(obj, method) then
if is_orig_hooked(obj, method) then return _origs[obj][method]
return _origs[obj][method]
else
return obj[method]
end
else else
if is_orig_hooked(obj, method) then return obj[method]
return _origs[method]
else
return rawget(_G, method)
end
end end
end end
@ -185,20 +172,15 @@ local function create_internal_hook(orig, obj, method)
return unpack(values, 1, num_values) return unpack(values, 1, num_values)
end end
if obj then if not _origs[obj] then _origs[obj] = {} end
if not _origs[obj] then _origs[obj] = {} end _origs[obj][method] = orig
_origs[obj][method] = orig obj[method] = fn
obj[method] = fn
else
_origs[method] = orig
_G[method] = fn
end
end end
-- This function handles the handling the hook data and adding them to the registry. -- This function handles the handling the hook data and adding them to the registry.
-- Origin Hooks have to be unique by nature so we have to make sure we don't allow multiple mods to do it. -- Origin Hooks have to be unique by nature so we have to make sure we don't allow multiple mods to do it.
local function create_hook(mod, orig, obj, method, handler, func_name, hook_type) local function create_hook(mod, orig, obj, method, handler, func_name, hook_type)
mod:info("(%s): Hooking '%s' from [%s] (Origin: %s)", func_name, method, obj or "_G", orig) mod:info("(%s): Hooking '%s' from [%s] (Origin: %s)", func_name, method, obj, orig)
if not is_orig_hooked(obj, method) then if not is_orig_hooked(obj, method) then
create_internal_hook(orig, obj, method) create_internal_hook(orig, obj, method)
@ -259,9 +241,9 @@ local function generic_hook(mod, obj, method, handler, func_name)
return return
end end
-- Shift the arguments if needed -- Shift the arguments if no obj is provided. obj becomes the global table.
if not handler then if not handler then
obj, method, handler = nil, obj, method obj, method, handler = _G, obj, method
if not method then if not method then
mod:error("(%s): trying to create hook without giving a method name.", func_name) mod:error("(%s): trying to create hook without giving a method name.", func_name)
return return
@ -286,14 +268,11 @@ local function generic_hook(mod, obj, method, handler, func_name)
return return
end end
end end
-- obj is a either nil or a table reference at this point, it cannot be a string anymore. -- obj should always be a table reference at this point --
-- Quick check to make sure the target exists -- Quick check to make sure the target exists
if obj and not obj[method] then if not obj[method] then
mod:error("(%s): trying to hook method that doesn't exist: [%s.%s]", func_name, obj, method) mod:error("(%s): trying to hook function or method that doesn't exist: [%s.%s]", func_name, obj, method)
return
elseif not obj and not rawget(_G, method) then
mod:error("(%s): trying to hook function that doesn't exist: [%s]", func_name, method)
return return
end end
@ -316,7 +295,7 @@ local function generic_hook_toggle(mod, obj, method, enabled_state)
-- Shift the arguments if needed -- Shift the arguments if needed
if not method then if not method then
obj, method = nil, obj obj, method = _G, obj
if not method then if not method then
mod:error("(%s): trying to toggle hook without giving a method name.", func_name) mod:error("(%s): trying to toggle hook without giving a method name.", func_name)
return return
@ -437,3 +416,4 @@ vmf.apply_delayed_hooks = function(status, state)
end end
end end
end end