Merge pull request #24 from FireSiku/old_hooks_removal

Remove old hooks notation
This commit is contained in:
Azumgi 2018-09-06 15:01:27 +03:00 committed by GitHub
commit 502707f21a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -86,20 +86,6 @@ local function get_object_reference(obj)
return obj, false return obj, false
end end
-- VT1 hooked everything using a "Obj.Method" string
-- Add backward compatibility for that format.
local function split_function_string(str)
local find_position = string.find(str, "%.")
local method, obj
if find_position then
method = string.sub(str, find_position + 1)
obj = string.sub(str, 1, find_position - 1)
else
method = str
end
return method, obj
end
-- We need to get the number of return values for accurate unpacking -- We need to get the number of return values for accurate unpacking
-- This is based on Lupo/Propjoe table.pack, but without putting the number inside the table -- This is based on Lupo/Propjoe table.pack, but without putting the number inside the table
local function get_return_values(...) local function get_return_values(...)
@ -225,7 +211,7 @@ local function create_hook(mod, orig, obj, method, handler, func_name, hook_type
_registry[mod][orig] = create_hook_data(mod, obj, handler, hook_type) _registry[mod][orig] = create_hook_data(mod, obj, handler, hook_type)
local hook_registry = _hooks[hook_type] local hook_registry = _hooks[hook_type]
-- Add to the hook to registry. Origin hooks are unique, so we check for that too. -- Add the hook to registry. Origin hooks are unique, so we check for that too.
if hook_type == HOOK_TYPE_ORIGIN then if hook_type == HOOK_TYPE_ORIGIN then
if hook_registry[orig] then if hook_registry[orig] then
mod:error("(%s): Attempting to hook origin of already hooked function %s", func_name, method) mod:error("(%s): Attempting to hook origin of already hooked function %s", func_name, method)
@ -241,15 +227,11 @@ local function create_hook(mod, orig, obj, method, handler, func_name, hook_type
-- Revisit purpose when lua files are in plain text. -- Revisit purpose when lua files are in plain text.
if can_rehook(mod, hook_data, obj, hook_type) then if can_rehook(mod, hook_data, obj, hook_type) then
hook_data.handler = handler hook_data.handler = handler
elseif mod:get_internal_data("allow_rehooking") then
-- If we can't rehook but rehooking is enabled, send a warning that something went wrong
mod:warning("(%s): Attempting to rehook active hook [%s] with different obj or hook_type.", func_name, method)
else else
-- This should be a warning log, but currently there are no differences between warning and error. mod:warning("(%s): Attempting to rehook active hook [%s].", func_name, method)
-- Wouldn't want to scare users that mods are broken because this used to be acceptable.
-- This should be changed to permanently be a warning log after new_hooks deprecation period is over.
if vmf:get("developer_mode") then
mod:warning("(%s): Attempting to rehook active hook [%s] with different hook_type.", func_name, method)
else
mod:info("(%s): Attempting to rehook active hook [%s] with different hook_type.", func_name, method)
end
end end
end end
@ -266,8 +248,10 @@ end
-- mod, string (obj), string (method), function (handler), string (func_name) -- mod, string (obj), string (method), function (handler), string (func_name)
-- Giving an object table and a method string and hook function -- Giving an object table and a method string and hook function
-- mod, table (obj), string (method), function (handler), string (func_name) -- mod, table (obj), string (method), function (handler), string (func_name)
-- Giving a method string or a Obj.Method string (VT1 Style) and a hook function -- Giving a method string and a hook function (hooking global functions)
-- mod, string (method), function (handler), nil, string (func_name) -- mod, string (method), function (handler), nil, string (func_name)
-- Giving a nil value followed by a method stirng and hook function (alternate way for global functions)
-- mod, nil, string (method), function (handler), string (func_name)
local function generic_hook(mod, obj, method, handler, func_name) local function generic_hook(mod, obj, method, handler, func_name)
if vmf.check_wrong_argument_type(mod, func_name, "obj", obj, "string", "table", "nil") or if vmf.check_wrong_argument_type(mod, func_name, "obj", obj, "string", "table", "nil") or
@ -277,12 +261,11 @@ local function generic_hook(mod, obj, method, handler, func_name)
return return
end end
-- Adjust the arguments. -- Shift the arguments if needed
if type(method) == "function" then if type(method) == "function" then
handler = method obj, method, handler = nil, obj, method
method, obj = split_function_string(obj)
if not method then if not method then
mod:error("(%s): trying to create hook without giving a method name. %s", func_name) mod:error("(%s): trying to create hook without giving a method name.", func_name)
return return
end end
end end
@ -305,6 +288,7 @@ 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.
-- Quick check to make sure the target exists -- Quick check to make sure the target exists
if obj and not obj[method] then if obj and not obj[method] then
@ -315,7 +299,6 @@ local function generic_hook(mod, obj, method, handler, func_name)
return return
end end
-- obj can't be a string for these now.
local orig = get_orig_function(obj, method) local orig = get_orig_function(obj, method)
if type(orig) ~= "function" then if type(orig) ~= "function" then
mod:error("(%s): trying to hook %s (a %s), not a function.", func_name, method, type(orig)) mod:error("(%s): trying to hook %s (a %s), not a function.", func_name, method, type(orig))
@ -333,12 +316,12 @@ local function generic_hook_toggle(mod, obj, method, enabled_state)
return return
end end
-- Adjust the arguments. -- Shift the arguments if needed
if not method then if not method then
if type(obj) == "string" then obj, method = nil, obj
method, obj = split_function_string(obj) if not method then
else 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. %s", func_name) return
end end
end end